在fortran中用不同的输入参数多次运行代码 [英] Run a code in fortran multiple times with different input parameters

查看:284
本文介绍了在fortran中用不同的输入参数多次运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同的输入参数组多次运行fortran 77程序;我已经确定输出文件名称会根据我使用的不同输入参数而变化,但我不确定如何使用不同的输入参数集来运行程序,而不必每次都去代码来更改参数。



为了说明我的问题,这里是一个简单的代码。

 程序代码
IMPLICIT DOUBLE PRECISION(AH,JZ)
COMMON / param / radius

radius = 2
write(*,*)'radius =',radius
写(*,*)'区域=',3.14 *半径*半径

END

假设我想用不同的半径运行这些代码,而不必进入代码并手动更改该值,我希望有一个具有不同参数选择的文件并使其运行多次。

当然,通过创建一个不同的参数选择和循环数组可以解决这个问题。然而,我不想这样做,因为我实际上有多个参数,我想在每次运行时更改。



针对下面的评论之一,如果我每次运行都有一个具有不同输入选项的文件,我如何让程序为每次运行为不同的参数选择抓取不同的行? 解决方案



为了避免重新编译,一个解决方案是硬编码参数文件的名称和从这个文件读取数据。每次运行代码都必须有自己的参数副本,以及它自己的目录。



我给出了一个单个参数的例子,但是如果需要的话,您可以推广它。它依赖于bash中的驱动程序脚本。



Fortran程序:

 程序代码
隐式双精度(AH,JZ)
公共/参数/半径

open(11,file ='parameters.txt')
read 11,*)radius
close(11)
write(*,*)'radius =',radius
write(*,*)'area is =',3.14 * radius *半径

END



bash程式:

  for 01 02 05 10 
do
RUNDIR = run _ $ {radius}
mkdir $ {RUNDIR}
echo$ {radius}> $ {RUNDIR} /parameters.txt
(cd $ {RUNDIR}; ../code)
完成

编译Fortran代码:

  gfortran -std = legacy -o code code.f 

并执行参数运行:

  bash parametricrun.sh 




  1. Fortran代码的功能:打开名为 parameters.txt 的文件,并读取第一个值作为radius的值。


  2. bash脚本的作用:对于参数的许多值,创建一个新目录,在该目录中创建一个名为 parameters.txt 的文件,然后在该目录中执行Fortran程序 code

评论:


  1. 这可以扩展到几个变量,每行一个或者每行几个 parameters.txt ,在bash程序中使用第二个循环。

  2. n使用更现代的Fortran版本,请提及它。这里有更多的选项。 其他语言(Python,arclight建议的或其他)可用于脚本编写。许多计算集群使用bash来运行作业,因此可能用于参数化运行(然后可以通过作业排队系统传递radius的值)。

    >我随意使用了11个文件单元号。您的实际情况确实需要为您的程序使用可用的单位编号。


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?

解决方案

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.

这篇关于在fortran中用不同的输入参数多次运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆