Clear Filters
Clear Filters

How to convert a big fortran IV code to matlab ?

53 views (last 30 days)
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 :
Thanks for your help

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.
Clément Carré
Clément Carré on 17 Mar 2022
Thank you for all your answers.
I first tried to convert it manually, lines by lines, and as you said it is not a good idea, I am directly blocked at line 57, where there are "go to" statements that are complicated to convert properly to matlab.
For the automatic translation, I will try to post the complete code. I tried, as you said, to use directly the f77 code but I get the same error message. However the f2matlab code succeed to do the preliminary simple changes and recognizes the subroutines.
Unfortunately, this code do many things : calculations, read airfoil profile files, exploit them, smooth them by using least square method, generate camber and thickness distribution and plot them. Moreover, there are many GOTO statements... is the f2matlab even handling GOTO statements ?
I will definitely try to use the code through MEX wrapper.
Steven Lord
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.

Sign in to comment.

Ben Barrowes
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.
  1 Comment
Clément Carré
Clément Carré on 17 Mar 2022
Edited: Clément Carré on 17 Mar 2022
I have just posted an answer with the .for code in .txt file.
I compiled it in Visual Studio Code but 144 errors appeared, I have to admit I should have tried to make it run in fortran before trying to convert it.
Ok and yes there are many goto statements...
Thank you for your help, if I am stuck I will not hesitate.

Sign in to comment.

Clément Carré
Clément Carré on 17 Mar 2022
Here is the code in a .txt file.
Paul on 18 Mar 2022
I was going to ask about tools like Polyhedron's Spag. Is that still around?
Ben Barrowes
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:

Sign in to comment.

Clément Carré
Clément Carré on 21 Mar 2022
I finally succeeded to compile the fortran code. Some modifications in fortran have been made with my professor for it to work properly.
Here is a dropbox folder where i put the fortran code updated, the executable file and an airfoil profile input.
If you are interested or if the .exe file does not work, I also have the output files.
For my part, I am still trying to understand this code in order to convert it manually by the way Steven Lord suggested.
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
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 ...

Sign in to comment.


Find more on Fortran with MATLAB in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!