FORTRAN: 2 conditions in 1 DO statement

  • Context: Comp Sci 
  • Thread starter Thread starter RJLiberator
  • Start date Start date
  • Tags Tags
    Conditions Fortran
Click For Summary

Discussion Overview

The discussion revolves around a coding problem in FORTRAN related to combining two DO loops for data processing. Participants explore the feasibility of incrementing two variables simultaneously within a single DO statement while working with a specific data set structure.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses uncertainty about combining two DO loops into one for simultaneous increments of variables y and v.
  • Another participant suggests a possible simplification of the loops, proposing a single DO loop that increments both y and v together, while noting the original increment pattern of y.
  • A later reply clarifies the purpose of the increment of 10 for y, explaining how it relates to the output format of data being written, and acknowledges that the suggestion provided was helpful.
  • Further, a participant questions the nature of the functions j1, j2, etc., speculating whether they are functions or arrays, and proposes a cleaner way to handle the output using a single DO loop for writing data.

Areas of Agreement / Disagreement

Participants generally agree on the need to simplify the code, but there remains some uncertainty regarding the implementation details and the nature of the functions involved.

Contextual Notes

There are unresolved questions about the definitions and behaviors of the functions j1, j2, etc., and how they interact with the loop variables. Additionally, the exact structure of the output and its formatting issues are mentioned but not fully resolved.

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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 11 ·
Replies
11
Views
6K