Hi I am having trouble with imposing boundary conditions on a 2 dimensional discretization problem in fortran. My discretization grid is a 2 dimensional square that goes from -L to L in x,y directions.
I want to impose the boundary condition such that, on the boundary line at x=L , the value of the function is specified. I also want to specify the boundary condition at the boundary line y=L. Then do the same for x,y=-L.
A basic attempt at the correct syntax is below. I am wondering if this syntax is correct and/or the quickest way at doing what I am doing. I assume there is a way to do it without do loops also using the colon notation just not sure how.
My syntax may be incorrect as I am unsure how to properly use the u(:) notation, or what the : really is doing for me. Thanks!
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
Colons are unnecessary here and, as arclight points out, often confuse the issue for beginners. You're looping over the boundary with an index value (either i
or j
), which is correct. You just have to set the corresponding u
value indexed with your index variable. For example, for the loop over 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
A colon is useful in other places, and it references a subset of the array. u(:,ny)
is the same as u(-nx:nx,ny)
, which is a 1D array for each possible i-index at j-index of ny. So you had been setting the entire boundary condition at once to a single value.
Another quick piece of advice: be sure to indent loops and other structures. Code is much more readable that way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With