How to convert a big fortran IV code to matlab ?
32 views (last 30 days)
Show older comments
Hello everyone,
I am in deep trouble trying to convert a very old fortran IV code written in 1984 to matlab.
I have tried firstly to find a tool in order to go from fortran IV to f90, but I am a complete beginner of fortran language...
The code is 2500 lines and has many many subroutines, I am losing hope and I am thinking of convert it manually... What do you think ?
Here is the study linked to the code : https://ntrs.nasa.gov/citations/19850022698
Thanks for your help
0 Comments
Answers (4)
Jan
on 16 Mar 2022
I'd never trust an automatic translation. But if you try it, make it as simple as possible: no intermediate conversion to FORTRAN 90.
Fortunately 2500 lines of FORTRAN is small enough to be conveted manually. This will be abou 1000 or less lines of Matlab code.
But what about using the FORTRAN code directly through a Mex wrapper? Do you have a FORTRAN compiler? Then all you have to do is to convert the Matlab variables to the equivalent representation of FORTRAN variables.
I've used f2c successfully to do this with an integrator. The original code has over 100'000 lines of code. Calling C from Matlab is easy also.
6 Comments
Steven Lord
on 17 Mar 2022
Rather than trying to convert each line of Fortran code into MATLAB, if you do want to perform that conversion rather than using that code through MEX I'd take a slightly higher level approach: the skyscraper view rather than the ground level view (looking at each line) or the 30,000 foot view (looking at the application as a whole.)
You indicated the code has many subroutines. That's where I'd start attacking the problem. Take a subroutine and read through it carefully. Understand what it requires as inputs, what it returns as outputs, and what it does (not in terms of individual instructions like "add flour to a bowl", "add eggs to the bowl", "mix", etc. but in terms of purpose like "this subroutine bakes a cake.") Then write an equivalent MATLAB function that does what you understand that subroutine to do. It may not achieve that purpose in the same way or with equivalent operations but that's okay.
To lock down the behavior of that function, write tests for it. [Don't worry if you're not familiar with using classes in MATLAB; that documentation page describes how to write class-based tests, function-based tests, and/or script-based tests.] You may also want to extract out the Fortran subroutines and pass those same test cases into it to check that the Fortran and MATLAB code gives the same results.
Once you have one function written and extracted out of the Fortran code, look for subroutines that depend only on the subroutines you've already converted. Keep carving off bite-sized pieces until you're done or until you have the components necessary to tackle the larger bits.
It's not an easy task. Believe me, I've done this conversion for some pretty involved Fortran code myself. But it can be manageable.
Ben Barrowes
on 17 Mar 2022
Your link goes to a PDF scan of the code. Do you have this code in a proper Fortran text file? Have you been able to compile it? Those are necessary first steps before trying to convert the code into Matlab.
Once you have running Fortran code, f2matlab has a good shot at getting you closer to runnable matlab code, but you would have to refactor all the goto's out first.
If you get stuck on the conversion, send me a PM With the working Fortran code, and I can take a look.
Clément Carré
on 17 Mar 2022
5 Comments
Ben Barrowes
on 21 Mar 2022
Yes! Spag and its author John Appleyard are still around and actively developed. John responds to my requests for improvements and is good to work with. Go here and click on the link to plusfort:
Clément Carré
on 21 Mar 2022
4 Comments
Paul
on 22 Mar 2022
Aren't there F-LINT tools that at least would be helpful in finding that COMMON block variable mashing and other old-school FORTRAN tricks that might also be problematic for converting to Matlab? I don't recall if the F-LINT tools will fix the code, but I'm pretty sure they will find all instances.
James Tursa
on 22 Mar 2022
Edited: James Tursa
on 22 Mar 2022
@Ben Barrowes Those COMMOM blocks with different named variables might not be that bad. It appears to me that maybe this was done simply as a memory saving device, and not as a means to pass variables between routines. E.g., That DUMMX variable doesn't appear anywhere else in the code, so I suspect it is simply there to create a memory allocation block to be used as workspace by the other routines, and that these routines don't actually use it a a means to pass variable values back & forth. A quick glance at a routine that uses this block with different named variables reveals that the first thing it does is read values into the variables, which supports this supposition. Regardless, your point is well taken and one would have to examine every routine that uses this block to verify that it is simply used as a temporary workspace and not for variable passing. If that could be verified, then in MATLAB simply just ignore the COMMON block and declare the variables directly.
A quick check (but not a proof) of this concept would be to get rid of all the COMMON blocks and instead declare the variables directly as local variables and see if the program produces the same output. If it does, then go through the effort of trying to verify that no variable passing is intended. If it doesn't, then find the places where variable passing is taking place and determine if it is intended or if it is an original coding error (i.e., it is always possible that there is a bug in the way these COMMON blocks were originally coded).
Why did programmers do this originally? Back in the old days programs were run in batches on large mainframe machines. It was not uncommon for the department or organization running the program to be charged not just for CPU time but for memory footprint also. A department or organization could save money by employing these memory saving techniques. So I try not to judge these programmers too harshly when I see this stuff. We just have to deal with it if we want to reuse the code ...
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!