Need help with Jacobi relaxation method for Dirichlet boundary conditions

In summary, the grid on which thejacobi algorithm runs is composed of 200x200 cells. The alpha values for each cell are determined by a=-2(1/dx^2 + 1/dy^2), alpha^0_jk=-2(1/dx^2 + 1/dy^2), alpha^1_jk=alpha^2_jk=-1/dx^2, and alpha^3_jk=alpha^4_jk=-1/dy^2.
  • #1
Nikolas_Ex_Aguirre
1
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
  • #2
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

1. How does the Jacobi relaxation method work for solving Dirichlet boundary conditions?

The Jacobi relaxation method is an iterative technique used to solve partial differential equations, specifically Dirichlet boundary conditions. It involves breaking down the problem into a grid and updating the values of each grid point using a weighted average of its neighboring grid points. This process is repeated until the solution converges to a desired level of accuracy.

2. What are the advantages of using the Jacobi relaxation method for Dirichlet boundary conditions?

One advantage of the Jacobi relaxation method is its simplicity and ease of implementation. It also has a relatively low computational cost compared to other methods, making it suitable for solving large and complex problems. Additionally, the Jacobi method is guaranteed to converge as long as the problem is well-posed.

3. Are there any limitations to using the Jacobi relaxation method for Dirichlet boundary conditions?

One limitation of the Jacobi relaxation method is its slow convergence rate. This means that it may require a large number of iterations to reach a desired level of accuracy, making it less efficient for problems with high precision requirements. It also may not perform well for problems with irregular or non-uniform grids.

4. How do you determine the optimal weight factor for the Jacobi relaxation method?

The weight factor, also known as the relaxation parameter, determines the rate of convergence for the Jacobi method. It is typically chosen empirically, with values between 0 and 2 being common. A higher weight factor can speed up convergence, but too high of a value can cause instability and prevent convergence. To determine the optimal weight factor, it is recommended to experiment with different values and choose the one that results in the fastest and most stable convergence.

5. Can the Jacobi relaxation method be used for problems with non-constant coefficients?

Yes, the Jacobi relaxation method can be extended to handle problems with non-constant coefficients, such as non-linear partial differential equations. This is done by updating the coefficients at each iteration based on the current solution. However, this can significantly increase the complexity and computational cost of the method.

Similar threads

  • Programming and Computer Science
Replies
4
Views
619
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
7
Views
3K
Replies
2
Views
1K
  • Differential Equations
Replies
8
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Replies
1
Views
1K
Back
Top