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

  • #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:
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
  • #2
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 Vanadium 50 and physicsuser2023
  • #3
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
 
  • #4
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 physicsuser2023
  • #5
Thank you for guidance, i check the code again
 

1. What are common errors when setting up boundary conditions in Fortran for solving Laplace's equation in 3D?

Common errors include incorrectly defining the boundary conditions on the edges and corners of the domain, not accounting for the discretization at boundaries, and using inconsistent units or scales. It's crucial to ensure that boundary conditions are precisely defined for all faces of the computational domain and are consistent with the physical problem being modeled.

2. How do you debug segmentation faults in Fortran code used for solving Laplace's equation in 3D?

Segmentation faults often occur due to accessing memory out of the allocated range, typically from incorrect array indexing. To debug these, check array declarations and ensure that loops do not exceed the array bounds. Utilizing tools like Valgrind or gdb can help identify where the out-of-bounds access occurs. Additionally, compiling the code with bounds checking enabled can help catch such errors during development.

3. What are typical convergence issues in iterative solvers for 3D Laplace's equation in Fortran, and how can they be resolved?

Convergence issues in iterative solvers can arise from a poor choice of relaxation parameters or initial guesses. To resolve these, consider experimenting with different solver parameters, improving the initial guess, or employing a more robust preconditioning method. Ensuring that the discretization mesh is fine enough and that the solver tolerates are properly set can also enhance convergence.

4. How does one ensure numerical stability when solving Laplace's equation in 3D using Fortran?

Numerical stability can be ensured by choosing appropriate discretization schemes and solver parameters. Stability is often influenced by the mesh size and time step in transient simulations, so adhering to the Courant-Friedrichs-Lewy (CFL) condition is beneficial. For steady-state solutions, using stable boundary conditions and iterative methods like Conjugate Gradient or Multigrid methods can also help maintain stability.

5. Why might the solution to Laplace's equation in 3D using Fortran code not match expected physical results, and how can this be corrected?

This discrepancy can occur due to several factors, including incorrect formulation of the problem, numerical errors, or insufficient resolution of the computational mesh. To correct this, verify the physical model and boundary conditions, refine the mesh, or consider using higher-order numerical methods. Additionally, validating the code with analytical solutions or experimental data for simpler cases can help identify and correct the issue.

Similar threads

  • Programming and Computer Science
Replies
1
Views
944
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Atomic and Condensed Matter
Replies
3
Views
870
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top