FORTRAN: 2 conditions in 1 DO statement

  • Context: Comp Sci 
  • Thread starter Thread starter RJLiberator
  • Start date Start date
  • Tags Tags
    Conditions Fortran
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
RJLiberator
Gold Member
Messages
1,094
Reaction score
63

Homework Statement



I am working on a code that sends a 6480 x 10 data set into a 64800 line LAT / LON / DATA gridded set.

In part of my code, I'd like to have 2 conditions in 1 DO statement and I am not sure if it is possible.

Homework Equations

The Attempt at a Solution


[/B]
DO x = 1, 180
vmin=vmin+1
DO y = 1, 360, 10
DO v = vmin, vmin+35, 1

I'd like to combine DO y=1, 360, 10 and DO v=vmin, vmin+35, 1 so they increment simultaneously. Is this possible?
 
Physics news on Phys.org
RJLiberator said:

Homework Statement



I am working on a code that sends a 6480 x 10 data set into a 64800 line LAT / LON / DATA gridded set.

In part of my code, I'd like to have 2 conditions in 1 DO statement and I am not sure if it is possible.

Homework Equations

The Attempt at a Solution


[/B]
DO x = 1, 180
vmin=vmin+1
DO y = 1, 360, 10
DO v = vmin, vmin+35, 1

I'd like to combine DO y=1, 360, 10 and DO v=vmin, vmin+35, 1 so they increment simultaneously. Is this possible?
You might be able to combine both DO loops into one, as the y and v loops execute 36 times. However, I'm puzzled by the y loop, as it starts off at 1, and is incremented by 10 each iteration. That is, it will take on the values 1, 11, 21, ..., 351.

I think the 2nd and 3rd DO loops in your code could be rewritten more simply like this:
Fortran:
y = 1
v = vmin
DO i = 1, 36
   ! stuff you want to have happen
   y = y + 10
   v = v + 1
END DO

Since you asked only about the y and v loops, I have addressed only those two. From what you wrote, I can't tell if the 2nd and 3rd loops are supposed to be inside the loop on x or not.

Also, when you post code, please use code tags, similar to what I did. What you type will look like this, but will render in a browser like my example above.
Fortran:
your code
 
Thanks for your reply.

The reason I had an increment of 10 was that I need to write out my data like so:

Fortran:
         write(11,666)x,y,j1(v)  
           write(11,666)x,y+1,j2(v)  
           write(11,666)x,y+2,j3(v)  
           write(11,666)x,y+3,j4(v)  
           write(11,666)x,y+4,j5(v)  
           write(11,666)x,y+5,j6(v)
           write(11,666)x,y+6,j7(v)  
           write(11,666)x,y+7,j8(v)  
           write(11,666)x,y+8,j9(v)  
           write(11,666)x,y+9,j10(v)

That way, y=1 will then take on values 1, 2, 3, 4,...10, then when y increments by 10, it will take on values 11, 12, 13... 20.

Your suggestion nailed it.

Now I've only have to do with formatting issues, otherwise everything seems to be working properly.
 
RJLiberator said:
Thanks for your reply.

The reason I had an increment of 10 was that I need to write out my data like so:

Fortran:
         write(11,666)x,y,j1(v)
           write(11,666)x,y+1,j2(v)
           write(11,666)x,y+2,j3(v)
           write(11,666)x,y+3,j4(v)
           write(11,666)x,y+4,j5(v)
           write(11,666)x,y+5,j6(v)
           write(11,666)x,y+6,j7(v)
           write(11,666)x,y+7,j8(v)
           write(11,666)x,y+8,j9(v)
           write(11,666)x,y+9,j10(v)

That way, y=1 will then take on values 1, 2, 3, 4,...10, then when y increments by 10, it will take on values 11, 12, 13... 20.

Your suggestion nailed it.

Now I've only have to do with formatting issues, otherwise everything seems to be working properly.
There must be a cleaner way to do this. Are the j1( ), j2(), etc. calls to functions? At first I thought they might be arrays, but for an array the index v has to be an integer. The code below assumes that j1, j2, etc. are the names of functions.

Fortran:
real function jay(i, vel)
integer i
real vel
if (i .eq. 1)    ! calculate the value that j1(v) would have returned
   jay = ...
else if (i .eq. 2)   ! calculate the value that j2(v) would have returned
   jay = ...
...     ! and so on
end if
end function

BTW, j is a terrible name for a function in that i and j are used almost exclusively as loop control variables in DO loops.

Now the output:
Fortran:
do i = 1, 10
   write(11, 666) x, y + i, jay(i, v)
end do