3

I am using python as an interface to several fortran files in my model. I want to duplicate a fortran file several times but at each copy, I will change the parameters that describe my model.

For example: I have the fortran file below

!file.f
! This is a fortran code

!Parameters
alpha = 0.5
beta = 100
...

I want to copy file.f several times such that I will have file1.f, file2.f, file3.f, etc. However, at each file duplicated I want to change the parameters alpha and beta automatically. Thanks

EDIT: Let me explain a little further. I am using python to implement data assimilation (kalman filtering) to models that have already been developed in fortran. Basically, how it works is at each specified time step the fortran models stop running, then I integrate real world data to model data and I do this in python. Then after the integration (assimilation), I rerun the same models however this time using new parameters that I obtained from fusing data from model and observations and the new initial conditions. I use python to do everything except run the model which is being done by fortran.

4

4 に答える 4

4

最も一貫した方法は、テンプレートエンジンを使用することだと思います。Pythonには多くの機能があり、通常はWebアプリケーション内にデプロイされます。

ただし、エンジンのテンプレートを作成する目的は、静的テキストとして変更する必要のないコードの大部分を取得できるようにすることであり、特別なマークアップを使用して、Pythonコード内で生成された変数でそれを補間します。

パラメータの複雑さによっては、別のテンプレートエンジンをまったく必要とせず、次の例のようにPython文字列フォーマット機能を使用することもできます。

テンプレートエンジンは、テンプレート内のループと条件を展開する機能として、少し余分な容量を提供する可能性があります。

例-Fortranテンプレートを次のように記述します。

!file.f
! This is a fortran code

!Parameters
alpha = %(alpha)s
beta = %(beta)s

そして、Pythonコードで、次のように記述します。

template = open("fortram_template.for", "rt").read()
data = {"alpha": 0.5, "beta": 100}

with open("fortram_1.for", "wt") as output:
    output.write (template % data)
于 2012-01-13T02:10:09.413 に答える
2

Fortranの解決策は、パラメーター行を別のテキストファイルに書き込み、次にincludeそのファイルを次のような行でFortranソースに書き込むことです。

include 'parameters.inc'

このようにして、メインのFortranコードを含むファイルに触れることなく、パラメーターファイルを簡単に再生成できます。

于 2012-01-13T02:20:53.693 に答える
2

これがあなたができることの例です。さまざまな (アルファ、ベータ) ペアをalpha_betaリストに配置しました。ファイル名をインクリメントする値としてペア位置のインデックスを使用することを選択していますが、これalpha_betaを行うにはさまざまな方法があります。このコードは、.f ファイルについて多くのことを想定しているという点で脆弱ですが、これらのファイルを生成する際に個人的に使用するためのものであるため、問題ないと思います (たとえば、提供された情報に基づいて、ファイルの行頭にある alpha という単語のインスタンスは 1 つだけです。そうでない場合は、正規表現を使用した方がよいかもしれません)。

alpha_beta = [(0.1, 16), (0.9, 50), (0.4, 76)]
file_name = 'file'
file_ext = '.txt'

for index, vars in enumerate(alpha_beta, start=1):
    with open(file_name + file_ext) as f:
        alpha, beta = vars
        new_file = open(file_name + str(index) + file_ext, 'w')
        for line in f:
            if line.startswith('alpha'):
                new_file.write('alpha = %s \n' % str(alpha))
            elif line.startswith('beta'):
                new_file.write('beta = %s \n' % str(beta))
            else:
                new_file.write(line)
        new_file.close()
于 2012-01-13T02:13:19.993 に答える
1

まず第一に、いくつかのコメントで述べられているように、このソリューションはやり過ぎだと思います。次の 2 つのオプションのいずれかを選択します。多くの Fortran コーディングを行う予定がない場合は、2 つ目のオプションを選択することをお勧めします。

  1. 次のような一時入力ファイルから使用するパラメーターを読み取ります。

    !file.f
    !This is a fortran code
    
    !Parameters
    open (unit=input, file='tmp_inp.txt', action='read', iostat=ierr)
    read (input, '(...)') alpha, beta
    

    次に、一時入力ファイルの値を Python または sed で変更します。

  2. f2py を使用して fortran コードを python に接続することにより、パラメーターを引数として python 内から fortran サブルーチンに渡します。Fortran コードは次のようになります。

    !file.f
    !This is a fortran code
    subroutine my_sub(alpha, beta)
    ...
    

    次に、f2py でコンパイルします。

    f2py -c -m my_fortran_code file.f
    

    最後に、Python 内から次のように呼び出します。

    #python code
    from my_fortran_code import my_sub
    my_sub(alpha, beta)
    

どちらのソリューションでも、いくつかの入力パラメーターを変更するためだけに Fortran コードを再コンパイルする必要はありません。

于 2012-01-13T06:43:05.387 に答える