Errors in Fortran code for solving Laplace's equation in 3D

Click For Summary

Discussion Overview

The discussion revolves around errors encountered in a Fortran code designed to solve Laplace's equation in three dimensions. Participants are examining issues related to memory allocation and initialization of arrays within the code, as well as seeking advice on appropriate input values for the simulation.

Discussion Character

  • Technical explanation
  • Debugging
  • Exploratory

Main Points Raised

  • One participant describes their Fortran code for calculating potential values and mentions specific errors encountered when running the program with different cube dimensions.
  • Another participant points out that with a DELTA of 0.01 and dimensions of 10x10x10, the resulting arrays would require an impractical amount of memory, leading to allocation errors.
  • A participant suggests that the error related to the 3x3x3 case may stem from uninitialized elements in the PHI array, specifically noting that some entries were never set in the INITIALIZE subroutine.
  • There is a discussion about the importance of tracking down the exact line where errors occur for easier debugging, with suggestions to include debug statements in the code.
  • One participant requests suggestions for suitable input values to avoid the allocation error while still achieving meaningful results.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the memory allocation issue and the need for proper initialization of the array. However, there is no consensus on specific input values that would resolve the errors, as this remains an open question.

Contextual Notes

The discussion highlights limitations related to array initialization and memory management in Fortran, particularly when dealing with large multidimensional arrays. The dependence on specific input values and the implications of array indexing in Fortran are also noted.

physicsuser2023
Messages
3
Reaction score
0
TL;DR
I encountered errors while running a Fortran code to calculate potential values inside a cube, with the first error being a reference to an undefined variable and the second error being insufficient storage allocation.
hey everyone. I wrote a code in Fortran to calculate potential values or solve the Laplace equation inside a cube according to the boundary conditions mentioned in the code.
this is my code:
Fortran:
program laplace_cubic

  implicit none

  REAL*8 :: LX, LY, LZ, DELTA, MAX_ERR, ERR
  INTEGER :: NX, NY, NZ, I, J, K, M
  INTEGER*8 :: MAX_COUNT
  REAL*8, DIMENSION(:,:,:), ALLOCATABLE :: PHI, PHIO
 
  write(*, *) 'LX='
  read(*, *) LX
  write(*, *) 'LY='
  read(*, *) LY
  write(*, *) 'LZ='
  read(*, *) LZ
  write(*, *) 'ENTER MAX COUNTER='
  read(*, *) MAX_COUNT
  write(*, *) 'ENTER MAX ERROR='
  read(*, *) MAX_ERR

  DELTA = 0.01
  NX = INT(LX/DELTA)
  NY = INT(LY/DELTA)
  NZ = INT(LZ/DELTA)
 
  ALLOCATE(PHI(0:NX, 0:NY, 0:NZ))
  ALLOCATE(PHIO(0:NX, 0:NY, 0:NZ))

  CALL INITIALIZE(PHI, NX, NY, NZ, LX, LY, LZ, DELTA)

DO M = 1, MAX_COUNT
    PHIO=PHI
    ERR=0.0
    DO I = 1, NX - 1
      DO J = 1, NY - 1
        DO K = 1, NZ - 1
          PHI(I, J, K) = (PHIO(I + 1, J, K) + PHIO(I - 1, J, K) + PHIO(I, J + 1, K) + &
               PHIO(I, J - 1, K) + PHIO(I, J, K + 1) + PHIO(I, J, K - 1)) / 6.0
          ERR = ERR + (PHI(I, J, K) - PHIO(I, J, K))**2
        END DO
      END DO
    END DO
    IF (ERR < MAX_ERR) EXIT
END DO

OPEN(100, FILE = 'laplaceoutput.TXT')
  DO I = 0, NX
    DO J = 0, NY
      DO K = 0, NZ
        WRITE(100, *) I * DELTA, J * DELTA, K * DELTA, PHI(I, J, K)
      END DO
    END DO
  END DO

end program laplace_cubic

SUBROUTINE INITIALIZE(PHI,NX,NY,NZ,LX,LY,LZ,DELTA)
   
    IMPLICIT NONE

    REAL,PARAMETER::PI=3.1415
    INTEGER::I,J,K
    INTEGER,INTENT(IN)::NX,NY,NZ
    REAL*8::DELTA,X,Y,Z,LX,LY,LZ
    REAL*8,DIMENSION(0:NX,0:NY,0:NZ)::PHI

DO J = 1, NY - 1
  DO K = 1, NZ - 1
    PHI(0, J, K) = 1.0  ! potential inside the back face (zy plane)
    PHI(NX,J,K) = 0.0
  END DO
END DO

DO J = 0, NY
  Y = J * DELTA
  PHI(0, J, 0) = SIN(PI*Y/LY)  !sinusoidal potential along y-axis
END DO

DO K = 0, NZ
  Z = K * DELTA
  PHI(0, 0, K) = SIN(PI*Z/LZ)  ! sinusoidal potential along z-axis
END DO

DO J = 1, NY-1
  DO I = 1, NX-1
    PHI(I, J, 0) = 1.0  ! potential inside the bottom face (xy plane)
    PHI(I,J,NZ) = 0.0
  END DO
END DO

DO I = 0, NX
   X= I * DELTA
  PHI(I, 0, 0) = SIN(PI*X/LX)  ! sinusoidal potential along x-axis
END DO

DO I = 1, NX-1
  DO K = 1, NZ-1
    PHI(I, 0, K) = 1.0  ! potential inside the left face (XZ plane)
    PHI(I,NY,K) = 0.0
  END DO
END DO

DO K = 1, NZ-1
  DO J = 1, NY-1
    DO I = 1, NX-1
      PHI(I, J, K) = 0.5  ! potential inside the cube
    END DO
  END DO
END DO

END SUBROUTINE INITIALIZE
I first entered the dimensions of the cube as 3x3x3, Iteration or MAX COUNTER as 1000, and MAX ERROR as 0.000001. After these inputs, I enter to make the program perform the operation, but it showed me an error: error 112, reference to undefined variable, array element or function result.
I then set the dimensions of the cube to 10x10x10 and the rest of the inputs the same as before. But this time I received another error: ALLOCATE was unable to obtain sufficient storage.
I tested a code similar to the above code for the two-dimensional case and it worked.
what's the solution?
 
Technology news on Phys.org
With DELTA at .01, 10x10x10 results in both PHI and PHI0 arrays being dimensioned 1000x1000x1000.
So you were trying to allocate memory for 2 billion real values (16,000,000,000 bytes). So there is no mystery about the allocation error.

For the other error, it will be a lot easier to track down if you note where this error occurred in the code. I don't know what debug tools you have, but at a minimum, you can run the code after putting some "WRITE(*,*) 'at line #999' statements in.
 
  • Like
Likes   Reactions: Vanadium 50 and physicsuser2023
Thank you for your guidance. Now I understand that the error related to 10*10*10 is a normal and reasonable thing. So can you suggest me some suitable input values to fix this error? Values for delta and other inputs. thanks
But the error related to 3*3*3. its said there is in line 32
 
OK. So you have not fully initialized PHI. Some of the 301x301x301 entries in that array were not set in function INITIALIZE. On the face of it, it appears that you are never setting array elements with indexes of 300.
In Fortran, an array of 0:N has N+1 elements.
(So my calculation above should have been (1001x1001x1001).)
 
  • Like
Likes   Reactions: physicsuser2023
Thank you for guidance, i check the code again
 

Similar threads

Replies
1
Views
2K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
9K