Fortran中的2D边界条件

Jef*_*aci 5 fortran boundary numerical-methods differential-equations

嗨我在Fortran中对二维离散化问题施加边界条件时遇到了麻烦.我的离散化网格是二维正方形,在x,y方向上从-L到L.

我想强加边界条件,使得在x = L的边界线上,指定函数的值.我还想在边界线y = L处指定边界条件.然后为x,y = -L做同样的事情.

以下是对正确语法的基本尝试.我想知道这种语法是否正确和/或最快捷的方式来做我正在做的事情.我假设有一种方法可以在没有do循环的情况下使用冒号,但不确定如何.

我的语法可能不正确,因为我不确定如何正确使用u(:)符号,或者:我真正在做什么.谢谢!

integer :: i,j
integer, parameter :: nx=60, ny=60
real, parameter :: h=0.1 !step size
real, dimension(-nx:nx,-ny:ny) :: u
real :: L

L=h*nx

do i = -nx, nx

x = real(i)*h

u(:,ny) = cos(atan(L/x)) ! is this correct?
u(:,-ny) = cos(atan((-L)/x))

end do

do j = -ny, ny

y = real(j)*h

u(nx, :) = cos(atan(y/L))
u(-nx, :) = cos(atan(y/(-L)))

end do
Run Code Online (Sandbox Code Playgroud)

Ros*_*oss 4

冒号在这里是不必要的,正如 arclight 指出的那样,冒号常常让初学者感到困惑。i您正在使用索引值(或)遍历边界j,这是正确的。您只需设置u用索引变量索引的相应值。例如,对于循环i

do i = -nx, nx
   x = real(i)*h
   u(i,ny) = cos(atan(L/x))
   u(i,-ny) = cos(atan((-L)/x))
end do
Run Code Online (Sandbox Code Playgroud)

冒号在其他地方很有用,它引用数组的子集。u(:,ny)与 相同u(-nx:nx,ny),它是 ny 的 j 索引处每个可能的 i 索引的一维数组。因此,您一次将整个边界条件设置为单个值。

另一个快速建议:一定要缩进循环和其他结构。这样代码就更具可读性了。