シミュレーション物理 5
運動方程式の方法 :
サブルーチンの使い方
サブルーチン (subroutine)
•
多くのプログラムは,小さい部分に分割出
来る
•
例えば運動方程式の方法の場合;
–
初期値を決める
–
時間発展を行う
–
結果を書く
•
このとき,時間発展の部分はいろいろなプ
ログラムで同じ構造をしている;時間 t の状
態を運動方程式に従って t+t にするだけ。
サブルーチンを使う理由
•
スペースの節約
•
デバッグがしやすい
一度書いて間違いがないことを確かめたらもう
そのまま直さないでよい
関数とサブルーチン
•
関数: exp(x), sin(x) のように変数を与え
てある手続きで別の数を出すも
の。 Fortran90 を始め,ほとんどのプログ
ラミング言語では,我々が自分で関数を
作ることが出来る。
•
サブルーチン:変数に対してある手続き
を行い,別の変数を作るプロセス。変数
は複数でもよい。
関数プログラムの例
ここでは華氏から摂氏の変換プログラムを扱う。
摂氏= ( 華氏 -32)*5/9
(私は華氏 68 度=摂氏 20 度として,それからのずれを 5/9 0.5
≒
して,計算している。例えばニュースで華氏 80 度と言われたら
(80-32)*5/9=26.67
をやらず, (80-68)/2+20=26 と概算してる。)
program fahrenheit2celsius
!---! This is a program to convert F to C ! 2005/5/16 Written by T. Ohtsuki
!---implicit none ! Always begin with this statement integer,parameter::double=selected_real_kind(14) real(kind=double):: fahrenheit,celsius
!---main
program---print *, "Input temperature in Fahrenheit." read *, fahrenheit
celsius=f2c(fahrenheit)
print *,"It is ",celsius,"degree in celsius"
contains
!--- Subroutines ---function f2c(temperature)
!---! function to convert fahrenheit to celsius
!---IMPLICIT NONE
integer,parameter::double=selected_real_kind(14) REAL(kind=double), INTENT(IN) :: temperature REAL(kind=double):: f2c
f2c=(temperature-32._double)/1.8_double END function f2c
end program fahrenheit2celsius
別のやり方:ファイルを分ける
program fahrenheit2celsius
!---! This is a program to convert F to C ! 2005/5/16 Written by T. Ohtsuki
!---implicit none ! Always begin with this statement integer,parameter::double=selected_real_kind(14) real(kind=double):: fahrenheit,celsius interface function f2c(temperature) integer,parameter::double=selected_real_kind(14) real(kind=double)::f2c real(kind=double),intent(IN)::temperature end function f2c end interface !---main
program---print *, "Input temperature in Fahrenheit." read *, fahrenheit
celsius=f2c(fahrenheit)
print *,"It is ",celsius,"degree in celsius" end program fahrenheit2celsius
これは fahrenheit2celsius2.f90 として save
このプログラムでは f2c の
変数の型がわからないので
この interface で判断させる
関数を独立させる
function f2c(temperature)
!---! function to convert fahrenheit to celsius
!---IMPLICIT NONE
integer,parameter::double=selected_real_kind(14)
REAL(kind=double), INTENT(IN) :: temperature
REAL(kind=double):: f2c
!interface
を使わない例
f2c=(temperature-32._double)/1.8_double
END function f2c
前のを fahrenheit2celsius2.f90, これを f2c.f90 と名付ける。
f90 fahrenheit2celsius2.f90 f2c.f90
で実行ファイルを作る。
これは f2c.f90 として save
関数のモジュール化
•
似たような関数をまとめたい
–
モジュール化
–
例えば華氏摂氏の逆で摂氏華氏を行う関
program fahrenheit2celsius
!---! This is a program to convert F to C
! 2005/5/16 Written by T. Ohtsuki
!---use temperaturelibs
implicit none ! Always begin with this statement
integer,parameter::double=selected_real_kind(14)
real(kind=double):: fahrenheit,celsius
!---main
program---print *, "Input temperature in Fahrenheit."
read *, fahrenheit
celsius=f2c(fahrenheit)
print *,"It is ",celsius,"degree in celsius"
print *, "Input temperature in Celsius."
read *, celsius
fahrenheit=c2f(celsius)
print *,"It is ",fahrenheit,"degree in fahrenheit."
end program fahrenheit2celsius
モジュールとはこんなもの
module temperaturelibs implicit none contains function f2c(temperature)!---! function to convert fahrenheit to celsius
!---IMPLICIT NONE
integer,parameter::double=selected_real_kind(14) REAL(kind=double), INTENT(IN) :: temperature REAL(kind=double):: f2c
f2c=(temperature-32._double)/1.8_double END function f2c
function c2f(temperature)
!---! function to convert fahrenheit to celsius
!---IMPLICIT NONE
integer,parameter::double=selected_real_kind(14) REAL(kind=double), INTENT(IN) :: temperature REAL(kind=double):: c2f
c2f=temperature*1.8_double+32 END function c2f