Need help with Jacobi relaxation method for Dirichlet boundary conditions

Click For Summary
SUMMARY

The forum discussion focuses on the implementation of the Jacobi relaxation method for solving Dirichlet boundary conditions in a Fortran program. The code provided initializes variables, allocates memory for arrays, and sets boundary conditions, but contains a syntax error on line 94 related to the assignment operator. The error is identified as a missing '*' operator in the expression for updating the phi array. Additionally, the importance of code readability is emphasized, suggesting the use of spaces and line breaks for clarity.

PREREQUISITES
  • Fortran programming language proficiency
  • Understanding of numerical methods, specifically the Jacobi relaxation method
  • Familiarity with Dirichlet boundary conditions
  • Knowledge of array manipulation and memory allocation in Fortran
NEXT STEPS
  • Review Fortran syntax for assignment operations and operator precedence
  • Explore advanced numerical methods for solving partial differential equations
  • Learn about optimizing Fortran code for performance and readability
  • Investigate debugging techniques for Fortran programs, focusing on array indexing issues
USEFUL FOR

Researchers, engineers, and students working on computational fluid dynamics or numerical simulations, particularly those implementing the Jacobi method in Fortran.

Nikolas_Ex_Aguirre
Messages
1
Reaction score
0
TL;DR
I have a problem with this code that i made. every time that i run it, this shows an error (is the same in the image), and i don't know how to fix this. It's a jacobi relaxation method for a dirichlet boundary conditions.
Code:
program r_jacobi

  implicit none

!!!!Variables!!!

  real*8 V, V_1, V_2, Lx, Ly
  integer n ,i , j, k, nx, ny
  real*8, allocatable :: arrx(:), arry(:), phi(:,:,:)
  real*8 x, xi, xf, y, yi, yf, dx, dy

  real*8 d, q, bx, by

  V=1
  V_1=V
  V_2=-V

  Lx = 2
  Ly = 1
  nx = 200
  ny = nx/2

  n = 200
  k = 200
  allocate(arrx(nx))
  allocate(arry(ny))
  allocate(phi(n,nx,ny))!!!GRID!!!

  xi = 0
  xf = Lx
  dx = (xf-xi)/(nx-1)
  yi = 0
  yf = Ly
  dy = (yf-yi)/(ny-1)

  d = 2*(dx**(-1)+dy**(-1))  do j = 1,ny
     do i = 1,nx
        x = xi+(i-1)*dx
        arrx(i) = x
        y = yi+(j-1)*dy
        arry(j)=y
     end do
  end do

!!!BC!!!

  do j = 1,ny
     do i = 1,nx
        phi(1,i,j) = (V_1+V_2)/2
     end do
  end do
 
  do i = 1, nx/2
     phi(1,i,1) = V_1
     phi(1,i,ny) = V_1
  end do

  do i = nx/2 +1, nx
     phi(1,i,1) = V_2
     phi(1,i,ny) = V_2
  end do

  do j = 1, ny
     phi(1,1,j) = V_1
     phi(1,nx,j) = V_2
  end do
!!!jacobi!!! 
!delta_2^x = u^n_j+1_k - 2 u^n_j_k + u^n_j-1_k
!delta_2^y = u^n_j_k+1 - 2 u^n_j_k + u^n_j_k-1

!alpha^0_jk = -2(1/dx^2 + 1/dy^2)
!alpha^1_jk = alpha^2_jk = -1/dx^2
!alpha^3_jk = alpha^4_jk = -1/dy^2  do k = 1, n
     do j = 2, ny-1
        do i = 2, nx-1
           bx = phi(k,i+1,j) - 2*phi(k,i,j) + phi(k,i-1,j)
           by = phi(k,i,j+1) - 2*phi(k,i,j) + phi(k,i,j-1)
           q = -dx**(-2)*(phi(k,i,j))*bx - dy**(-2)*phi(k,i,j)*by
           phi(k+1,i,j)=d**(-1)(q+dx**(-1)*(phi(k,i+1,j)+phi(k,i-1,j))+dy**(-1)*(phi(k,i,j+1)&
                +phi(k,i,j-1)))
        end do
     end do
  end do 
end program r_jacobi
error.png
 
Last edited by a moderator:
Technology news on Phys.org
Nikolas_Ex_Aguirre said:
Fortran:
phi(k+1,i,j)=d**(-1)(q+dx**(-1)*(phi(k,i+1,j)+phi(k,i-1,j))+dy**(-1)*(phi(k,i,j+1)& +phi(k,i,j-1)))
The error message says there is a problem with line 94, which I've copied above. It looks to me like you are missing '*" to the right of the assignment operator, and between d**(-1) and the stuff following it. In other words,
phi(k+1, i, j) = d**(-1) * (a + dx**(-1) * ...
There might be other errors as well.

Do yourself and anyone else who has to read your code by inserting some spaces. If this makes the line too long, break up the line into multiple assignment statements.
 
  • Like
Likes   Reactions: dlgoff and berkeman

Similar threads

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