Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comprehending shortcuts in fortran 77

Status
Not open for further replies.

Grunde

Programmer
Jul 6, 2011
10
NO
I am currently working on a translation of an old program from fortran 77 to matlab, since matlab is more prominent in this business nowadays. Prior to this assignment, I didn't know anything about fortran, but after some days of reading and testing I felt ready. (I already know matlab and C++)

Now, I have realized that the author of the source code I'm working on used some tricks not described in the manuals, and so i'm looking for interpretation help here.

Firstly, I found a mysterious if-construct. It assigns a new, implicitly declared variable, which is then only used once. (the two lines of the construct are not adjacent in my code).

if (statement) EAS = something
something_else = EAS/some_expression

now, if the if construct does not activate, what happens? My compiler can't get it to work, but the program (compiled in the eighties) goes smoothly.

Secondly, there's a nested do-loop, where the outermost doesn't seem to be terminated anywhere; It's got a label, and the label is found further down, but it's not paired up with a continue or end do. Does it still terminate the loop at this point?

Help would be much appreciated

Double, double, toil and trouble
fire burn and cauldron bubble
 
Re: if statement - same as in any other language - EAS takes the value it had before the if statement. Change the code to
Code:
print *, 'before ', EAS
if (statement) EAS = something
print *, 'after  ', EAS

Does this code live in a subroutine? Did the old compiler switch on a flag for save variables i.e. it retain values on re-entry.

do loops are paired up with enddo if there is no label and the label if one is specified. It does not have to be a continue: it can be any legal statement. Yes the loop still terminates.
 
So, matlab is more prominent in this business? What an ironic statement! Did you know that matlab is written in Fortran?!

what's your business, anyway? Clearly, they were using Fortran in it, too.

In any case, it's tough to tell what is happening by simply taking your word for it...

...when something like this happens to me (I have also inherited 20, 30, 40 year old codes) I quickly do a grep on the file and list all the occurrences of a variable to see when and how is being used, etc...that filters everything else out and sends you directly to specific line numbers in the code...then, you can inspect the surrounding code.

if a variable does not show anywhere but inside an 'if' statement and another calculation...then, when the 'if' does not execute, such variable remains with a value of zero. Needless to say, the variable was not declared and they simply took advantage of the implicit feature of Fortran.

do loop endings may scape your sight if they do not end in an 'end do' or 'continue' statement; rather, sometimes they end in a regular executable statement which has been labeled with the corresponding numeric xx label, i.e., the same that appeared in the "do xx i=1,4"

also, keep in mind that back in the day they did not have many advantages of rapid development cycle...maybe they had to carry the cards to the reader, had a lame text-base screen, no syntax hightlight, etc...so, it is very probable that something escape to them too...so, it is not inconceivable the existence of a bug or two, but to catch them you really need to understand the intent of the program and every line...

then again, if you do not know Fortran well, something may get lost in the translation and you will end up introducing new bugs or your own...because, I would simply update the sources with minimal changes just to make it compile again.

Also, I hope that your program does not much because matlab, with all has gotten to offer, is a lot slower than pure Fortran.

Good luck and be careful.



 
@xwb
the print-before-and-after routine is very good, and I am a frequent user of it, but I haven't been able to get the entire program to compile and the lines your propose won't compile in isolation. Thank you for the description of the do-termination, it is very helpful.

@salgerman
I'm in the business of designing bridges and tunnels, and while I know fortran runs a lot faster, matlab has got a lot of intrinsic functions that makes it very neat for writing quick programs for small to medium scale number crunching. The original fortran program runs in 0.05 secs on a 2004-computer; I can afford it to run for more than an hour, so speed's not really an issue. More than half of my coworkers know matlab, no one knows fortran, and they want to be able to change the code and add to it at will. There's the story for you :)

I have searched through the program and all the subroutines, and this variable of mine does not occur elsewhere, but it will make sense in the program if it's set to zero by default if the if-statement does not activate.

Thanks for the help, it is very much appreciated!

Double, double, toil and trouble
fire burn and cauldron bubble
 
Well, Grunde, I hear your story...rapid prototyping in matlab...

...say, have you guys heard of Python?

Python is easy to learn and has a huge engineering/scientific community behind it. People have develop quite a few powerful modules for it that make it behave like matlab...it has numpy, scipy, matplotlib...with those three modules, you could replace matlab altogether and save yourself thousands of dollars a year on software licensing fees...how many matlab licenses you guys have to buy a year? how much do they cost?

You may want to search the web, but here are a few links to start with as it relate to your story:

 
I was just reading up on those pages, and I'm going to have a closer look at python for future use.

As for why we're using matlab here, there are mainly two reasons;

1. None of the guys here are really working a lot with programming, but they have learned matlab, and thus can read code and add to it with not so much effort. I'm guessing converting to python would give most of them nightmares, even though it seems not so difficult.
2. a lot of the calculations in question are involving matrices and integration, for which matlab has a lot of intuitive tools. It may be that python has as well, especially numpy, but we know where matlab hides them.

About the licenses, I don't know much about them, they're above my level of authority, but I'm guessing the reschooling of the engineers here could ultimately cost more. After all, if anything gets expensive, chances are octave will run the program just as fine as matlab does.

To sum it up, i appreciate your concern, I will definitely look at python, but I don't see that the reasons for using python weighs heavily enough to change from matlab for the task at hand in my current environment.

Double, double, toil and trouble
fire burn and cauldron bubble
 
Why don't you let this give you a head-start?:


What you could do is use Michael Metcalf's F77 to F90 converter ( and then use the above to convert to MATLAB? I don't know how well the former works (I can vouch for the latter!), but maybe try and see if it can do most of the job for you?

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Thanks Nick, that's an excellent piece of advice!

I have tried the f2matlab, but I haven't had my hands on a piece of f2f90-code as neat as this one. Still, it doesn't seem to help me much here, as I have tried it on three different subroutines (long and short) and all the original sleazy shortcuts are kept as they are through the translation (which gives some errors in matlab, and I'm no further).

Also, the author of the original code has been using nested goto statement-loops with arithmetic ifs, which I think is best unwrapped on a blank sheet. However, f2matlab is a wonderful tool, and it works fine for getting an overview of normal loops and ifs.

Double, double, toil and trouble
fire burn and cauldron bubble
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top