![見出し画像](https://assets.st-note.com/production/uploads/images/121922018/rectangle_large_type_2_85077615bd4d9925bb5542bd9e50e689.png?width=1200)
Photo by
maxima_s
FORTRAN テスト01
方針:
①文献の反応式をFORTRANで記述できるようGPTに投げる
文献:https://www.cgr.mlit.go.jp/ctc/pdf-document-years/2013/yamaguchi_3-1.pdf
反応式:(6)
初期値:適当
program ReactionSimulation
implicit none
real(8) :: Mc, k, beta, Ms, H, CO3, a, b, c
real(8) :: dt, dx
real(8) :: time
integer :: i, j, max_steps
! 初期条件の設定
Mc = 1.0
k = 4.80e8
beta = 9.0
Ms = 1.0
H = 1.0
CO3 = 1.0
a = 1.0
b = 0.5
c = 1.0
! 数値計算のパラメータ
dt = 0.00001
dx = 0.01
max_steps = 1000
! 出力ファイルのオープン
open(1, file='output.dat', status='replace')
! 時間ループ
do i = 1, max_steps
! 空間ループ
do j = 1, max_steps
! 反応式の計算
Mc = Mc - dt * k * exp(-beta * Ms) * Mc**a * H**b * CO3**c
end do
! 時間の更新
time = time + dt
! 結果をファイルに書き込む
write(1, *) time, Mc
end do
! ファイルを閉じる
close(1)
end program ReactionSimulation
②コードをVSにペースト
ファイル名:csh_test.f90
③ターミナル開いて、以下のコマンドでコンパイル
PS C:\fortran> gfortran -g -o csh_test csh_test.f90
PS C:\fortran> ./csh_test.exe
csh_test.exeが出力されている
![](https://assets.st-note.com/img/1700185193643-98htKytdXA.png?width=1200)
④gnuplotをpowershellで開く
以下の出力になれば、OK?
PS C:\fortran> gnuplot
G N U P L O T
Version 5.4 patchlevel 6 last modified 2023-02-09
Copyright (C) 1986-1993, 1998, 2004, 2007-2023
Thomas Williams, Colin Kelley and many others
gnuplot home: http://www.gnuplot.info
faq, bugs, etc: type "help FAQ"
immediate help: type "help" (plot window: hit 'h')
Terminal type is now 'qt'
Encoding set to 'sjis'.
⑤gnuplotに出力するファイルを指示する
FORTRANコードの”! 出力ファイルのオープン”の下の行で指定したファイル名:output.data
指定したファイルが生成されている↓
![](https://assets.st-note.com/img/1700185430203-ZZ5z8qB2PK.png?width=1200)
指示するためのコード↓
gnuplot> plot 'output.dat' with lines title 'Mc vs. Time'
実行結果↓
gnuplot> plot 'output.dat' with lines title 'Mc vs. Time'
Warning: empty y range [0:0], adjusting to [-1:1]
![](https://assets.st-note.com/img/1700185611830-2LDksQqFg7.png?width=1200)
エラーっぽいので、gpt
この警告メッセージは、Gnuplotがデータのy軸範囲を自動的に調整しようとしていることを示しています。y軸範囲が[0:0]として検出され、これは空の範囲であるため、[-1:1]に調整されています。
このような警告が表示される場合、通常はデータに問題があるか、y軸の範囲が制約されている可能性があります。以下の手順を試してみてください:
データファイルの確認: output.dat ファイルが正しく生成され、データが含まれているか確認してください。ファイルをテキストエディタで開き、時間と反応の値が正しく書き込まれているか確認します。
Gnuplotスクリプトの修正: Gnuplotスクリプトにおいて、y軸の範囲を手動で指定してみてください。例えば、y軸を0から1000の範囲に設定するには、以下のようにします。
gnuplot
Copy code
set title 'Reaction Simulation Results'
set xlabel 'Time'
set ylabel 'Mc'
set yrange [0:1000]
plot 'output.dat' with lines title 'Mc vs. Time'
これで、y軸の範囲が手動で指定され、警告が解消されるかもしれません。ただし、y軸範囲を手動で設定する際には、データの範囲に合わせて適切な値を設定する必要があります。
少し沼りそうなので、一旦中止
【エラーの推定】
①Mcの微分方程式をgptに適当に作らせたから
②描画範囲が間違っている
③そもそも正しく出力されないコードになっている
<単純な関数使って、コードの正誤判断したい
【今後の方針】
①答えが欲しいダイレクトに
②研究員への照会
>データ持ってなかった
③参考書
>もう少しリサーチする(時間ある?)
④打ち切る
⑤全然関係ない現象を対象としてコード書いてみる