- #1
physicsuser2023
- 3
- 0
- TL;DR Summary
- 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:
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?
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 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?