5

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
Jeff Faraci
  • 403
  • 13
  • 28
  • Why are you looping over i or j and not using the loop variable at all inside the loop? You should *either* use the colon notation *or* loops in this case. – Ross Sep 23 '16 at 19:51
  • @Ross I am looping over i in the first loop, and using the loop variable, i, in the first line since x is defined in terms of i. Also in the loop over j, I use the loop variable in the first line when I define y. Isn't this correct or no? How would I do this using the colon notation? Does the colon notation work inside the loop notation? (Seems I am mixing both). Thanks for your help! – Jeff Faraci Sep 23 '16 at 20:01
  • 1
    I missed that x is a point value. Then, I think you are close, but you need to set `u(i,ny)` instead of `u(:,ny)`. A colon indicates 'all locations in this row/column', and you only want to set one at a time based on the x-value. – Ross Sep 23 '16 at 20:03
  • @Ross Thanks for the help, that makes sense. – Jeff Faraci Sep 23 '16 at 20:06
  • 2
    When in doubt, first write it out explicitly using a loop. The time it takes to rewrite a working loop into array code is almost always far less than writing working nontrivial array code from the outset. You can always optimize and clarify the code later once it's working. – arclight Sep 23 '16 at 20:08

1 Answers1

4

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.

Ross
  • 2,130
  • 14
  • 25