0

Update: get_command_argument sounds really helpful, however, I believe am using an older version of Fortran. (I think 90?)

I am new to Fortran and need to use a Fortran code to calculate something. I need to call this Fortran code repeatedly from a Matlab program that would be impractical to rewrite in Fortran, so I am using the "system" command in matlab to call the program, which is basically like calling from the command line. Currently, the the fortran code contains a main function and several subroutines (the main one, FUNFIT, is dependent on the others and takes X, Y as inputs), and currently looks something like this:

  implicit double precision (A-H), double precision (O-Z)
  X = 5;
  Y = 7;
  call FUNFIT(X,Y,Pn,FN,UN,CV,CH,CHI,PM,US)
  end

  subroutine FUNFIT(X, Y, Pn,FN,UN,CV,CH,CHI,PM,US)
  *I don't think details are needed here*
  end
  
  subroutine SOME_OTHER_SUBROUTINE( . . .)

I want to be able to call FUNFIT from the command line (allowing it to have access to the other subroutines) and input X, Y. So basically, I want to directly call FUNFIT from a shell, with inputs given.

However, I am having trouble with calling the code and defining the inputs. I have not been able to find an example of a Fortran "main" function with an input. If I just compile it with the "main" function commented out, I don't know how to call FUNFIT. How can I call FUNFIT (either directly, or through the use of a main function) from the command line and directly input values of X, Y each time? I am at a loss. I know it is ridiculous, but my first attempt has been to re-write and re-compile the program with new values of X, Y each time I call it, which has been prohibitively slow. Thanks for helping out this novice Fortran programmer.

jnovack
  • 7,629
  • 2
  • 26
  • 40
  • The linked question leads to one example of using `get_command_argument`, and you can find other examples using that subroutine's name for search. If this isn't exactly what you are after with your question, please update it to provide more detail. – francescalus Sep 28 '20 at 15:07
  • The Matlabby way of doing this is to encapsulate the Fortran routine(s) you want to use in a *MEX* file. For which activity there is a ton of informative resources available at your favourite search engine. – High Performance Mark Sep 28 '20 at 15:18
  • If your compiler doesn't support the Fortran 2003 features required, then you have to rely on a non-standard alternative (such as in [this question](https://stackoverflow.com/q/14652225/3157076) ). Or indeed the MEX interfacing mentioned in the other comment. – francescalus Sep 28 '20 at 16:03
  • Or get a newer compiler; F2003 is 15(-ish) years old at this point. – janneb Sep 28 '20 at 17:05
  • 1
    We have other duplicates about the other older options, there is no need to reopen. However, you *really* have to say which exact compiler you have because those older ways are compiler-specific. – Vladimir F Героям слава Sep 29 '20 at 13:48
  • 1
    See https://stackoverflow.com/questions/14652225/fortran77-parameter-when-executing-program and https://stackoverflow.com/questions/63932163/how-to-get-command-line-arguments-in-openvms-fortran and https://stackoverflow.com/questions/15369096/implementing-getarg-subroutine-call – Vladimir F Героям слава Sep 29 '20 at 13:52

0 Answers0