Jeremy_Tamu
Jeremy_Tamu

Reputation: 755

Fortran error: size of variable is too large

I have a long program and the goal is to solve the matrix system ax=b. When I run it, it reveals that "error: size of variable is too large".

program ddm

  integer :: i,j,k
  integer, parameter :: FN=1,FML=80,FMH=80
  integer, parameter :: NBE=1*80*80 !NBE=FN*FML*FMH

  double precision, dimension(1:3*NBE,1:3*NBE) :: AA
  double precision, dimension(1:3*NBE) :: BB
  double precision :: XX(3*NBE)
  double precision, dimension(1:NBE) :: DSL,DSH,DNN
  double precision, dimension(1:FML,1:FMH) :: DSL1,DSH1,DNN1

  ! Construct a block matrix
  AA(1:NBE,1:NBE) = SLSL
  AA(1:NBE,NBE+1:2*NBE) = SLSH
  AA(1:NBE,2*NBE+1:3*NBE) = SLNN

  AA(NBE+1:2*NBE,1:NBE) = SHSL
  AA(NBE+1:2*NBE,NBE+1:2*NBE) = SHSH
  AA(NBE+1:2*NBE,2*NBE+1:3*NBE) = SHNN

  AA(2*NBE+1:3*NBE,1:NBE) = NNSL
  AA(2*NBE+1:3*NBE,NBE+1:2*NBE) = NNSH
  AA(2*NBE+1:3*NBE,2*NBE+1:3*NBE) = NNNN

  ! Construct a block matrix for boundary condition
  BB(1:NBE) = SLBC
  BB(NBE+1:2*NBE) = SHBC
  BB(2*NBE+1:3*NBE) = NNBC

  call GE(AA,BB,XX,3*NBE)

  DSL = XX(1:NBE)
  DSH = XX(NBE+1:2*NBE)
  DNN = XX(2*NBE+1:3*NBE)

  DSL1 = reshape(DSL,(/FML,FMH/))
  DSH1 = reshape(DSH,(/FML,FMH/))
  DNN1 = reshape(DNN,(/FML,FMH/))

  open(unit=2, file='DNN2.txt', ACTION="write", STATUS="replace")
  do i=1,80
      write(2,'(*(F14.7))') real(DNN1(i,:))
  end do

end program ddm

Note: GE(AA,BB,XX,3*NBE) is the function for solving the matrix system. Below is the GE function.

subroutine GE(a,b,x,n)
!===========================================================
! Solutions to a system of linear equations A*x=b
! Method: Gauss elimination (with scaling and pivoting)
!-----------------------------------------------------------
! input ...
! a(n,n) - array of coefficients for matrix A
! b(n) - array of the right hand coefficients b
! n - number of equations (size of matrix A)
! output ...
! x(n) - solutions
! coments ...
! the original arrays a(n,n) and b(n) will be destroyed
! during the calculation
!===========================================================

implicit none
integer n
double precision a(n,n),b(n),x(n)
double precision s(n)
double precision c, pivot, store
integer i, j, k, l

! step 1: begin forward elimination
do k=1, n-1

! step 2: "scaling"
! s(i) will have the largest element from row i
do i=k,n ! loop over rows
s(i) = 0.0
do j=k,n ! loop over elements of row i
s(i) = max(s(i),abs(a(i,j)))
end do
end do

! step 3: "pivoting 1"
! find a row with the largest pivoting element
pivot = abs(a(k,k)/s(k))
l = k
do j=k+1,n
if(abs(a(j,k)/s(j)) > pivot) then
pivot = abs(a(j,k)/s(j))
l = j
end if
end do
! Check if the system has a sigular matrix
if(pivot == 0.0) then
write(*,*) "The matrix is singular"
return
end if

! step 4: "pivoting 2" interchange rows k and l (if needed)
if (l /= k) then
do j=k,n
store = a(k,j)
a(k,j) = a(l,j)
a(l,j) = store
end do
store = b(k)
b(k) = b(l)
b(l) = store
end if

! step 5: the elimination (after scaling and pivoting)
do i=k+1,n
c=a(i,k)/a(k,k)
a(i,k) = 0.0
b(i)=b(i)- c*b(k)
do j=k+1,n
a(i,j) = a(i,j)-c*a(k,j)
end do
end do
end do

! step 6: back substiturion
x(n) = b(n)/a(n,n)
do i=n-1,1,-1
c=0.0
do j=i+1,n
c= c + a(i,j)*x(j)
end do
x(i) = (b(i)- c)/a(i,i)
end do

end subroutine GE

Upvotes: 1

Views: 2072

Answers (1)

innoSPG
innoSPG

Reputation: 4656

Turn your arrays (at least AA, BB, XX) into allocatable arrays and allocate them by yourself in the code. You are hitting the memory limit of statically allocated arrays. There is a limit of 2GB on some systems if I remember well (experts will confirm or give the right numbers).

Upvotes: 3

Related Questions