Search code examples
fortrangfortranfortran77

Run a code in fortran multiple times with different input parameters


I would like to run a fortran 77 program multiple times with different set of input parameters; I already made sure that output file names change for different input parameters I use, but I am not sure how I can run a program with different set of input parameters without having to go to the code each time to change the parameters.

To illustrate my question, here is a simple code.

   PROGRAM CODE
   IMPLICIT DOUBLE PRECISION (A-H, J-Z)
   COMMON / param  / radius

   radius = 2
   write(*,*) 'radius = ', radius
   write(*,*) 'the area is = ', 3.14*radius*radius

   END 

Say I want to run this code with different radiuses, and instead of having to go into the code and manually change the value, I want to have a file with different parameter choices and have it run multiple times.

Of course, there is a solution to this by creating an array of different parameter choices and looping. However, I do not want to do this, since I actually have multiple parameters I want to change for each run.

In response to one of the comments below, if I have a file with different input choices for each run, how do I have the program grab different rows for different parameter choices for each run?


Solution

  • There is a "pedestrian"-type approach that I have used many times.

    To avoid recompilation, a solution is to hardcode the name of the parameter file and to read the data from this file. Every run of the code must have its own copy of the parameters and so its own directory.

    I give below an example for a single parameter but you can generalize it if needed. It relies on a driver script in bash.

    Fortran program:

          PROGRAM CODE
          IMPLICIT DOUBLE PRECISION (A-H, J-Z)
          COMMON / param  / radius
    
          open(11, file='parameters.txt')
          read(11,*) radius
          close(11)
          write(*,*) 'radius = ', radius
          write(*,*) 'the area is = ', 3.14*radius*radius
    
          END 
    

    bash program:

    for radius in 01 02 05 10
    do
    RUNDIR=run_${radius}
    mkdir ${RUNDIR}
    echo "${radius}" > ${RUNDIR}/parameters.txt
    (cd ${RUNDIR} ; ../code)
    done
    

    Compile the Fortran code:

    gfortran -std=legacy -o code code.f
    

    and execute the parametric run as:

    bash parametricrun.sh
    
    1. What the Fortran code does: Open the file named parameters.txt and read the first entry for the value of radius.

    2. What the bash script does: For a number of values of the parameter, create a new directory, create a file named parameters.txt in that directory then execute the Fortran program code in that directory.

    Comments:

    1. This can be extended to several variables, either one per line or several per line in parameters.txt, using a second loop in the bash program.

    2. If you can use a more modern version of Fortran, please mention it. There are more options there.

    3. Other languages (Python, as arclight suggests, or others) can be used for scripting. Many computing clusters use bash to run jobs, so that might be used for parametric runs (the value of radius could be passed via the job queuing system then).

    4. I used 11 arbitrarily for the file unit number. Your actual situation requires to use an available unit number for your program indeed.