Fortran Need help with Jacobi relaxation method for Dirichlet boundary conditions

AI Thread Summary
The discussion focuses on a Fortran program implementing the Jacobi method for solving partial differential equations. Key points include the initialization of variables, grid allocation, boundary conditions, and the Jacobi iteration process. The program sets up a computational grid defined by dimensions Lx and Ly, with nx and ny points. Boundary conditions are applied to the phi array, which represents the solution at each grid point. The Jacobi iteration is performed to update the phi values based on finite difference approximations for spatial derivatives. An error is highlighted in line 94, indicating a missing multiplication operator, which could lead to compilation issues. Additionally, suggestions are made to improve code readability by adding spaces and breaking long lines into multiple statements.
Nikolas_Ex_Aguirre
Messages
1
Reaction score
0
TL;DR Summary
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 dlgoff and berkeman
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
6
Views
7K
Replies
12
Views
3K
Replies
7
Views
3K
Replies
3
Views
2K
Replies
9
Views
9K
Replies
4
Views
2K
Replies
1
Views
3K
Back
Top