• 検索結果がありません。

5回目の講義ノート

N/A
N/A
Protected

Academic year: 2021

シェア "5回目の講義ノート"

Copied!
16
0
0

読み込み中.... (全文を見る)

全文

(1)

シミュレーション物理 5

運動方程式の方法 :

サブルーチンの使い方

(2)

サブルーチン (subroutine)

多くのプログラムは,小さい部分に分割出

来る

例えば運動方程式の方法の場合;

初期値を決める

時間発展を行う

結果を書く

このとき,時間発展の部分はいろいろなプ

ログラムで同じ構造をしている;時間 t の状

態を運動方程式に従って t+t にするだけ。

(3)

サブルーチンを使う理由

スペースの節約

デバッグがしやすい

一度書いて間違いがないことを確かめたらもう

そのまま直さないでよい

(4)

関数とサブルーチン

関数: exp(x), sin(x) のように変数を与え

てある手続きで別の数を出すも

の。 Fortran90 を始め,ほとんどのプログ

ラミング言語では,我々が自分で関数を

作ることが出来る。

サブルーチン:変数に対してある手続き

を行い,別の変数を作るプロセス。変数

は複数でもよい。

(5)

関数プログラムの例

ここでは華氏から摂氏の変換プログラムを扱う。

摂氏= ( 華氏 -32)*5/9

(私は華氏 68 度=摂氏 20 度として,それからのずれを 5/9 0.5

して,計算している。例えばニュースで華氏 80 度と言われたら

(80-32)*5/9=26.67

をやらず, (80-68)/2+20=26 と概算してる。)

(6)

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

(7)

別のやり方:ファイルを分ける

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 で判断させる

(8)

関数を独立させる

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

(9)

関数のモジュール化

似たような関数をまとめたい

モジュール化

例えば華氏摂氏の逆で摂氏華氏を行う関

(10)

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

(11)

モジュールとはこんなもの

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

(12)

モジュールの形のプログラムの

コンパイルの仕方

まず使うモジュールプログラム(ここでは

temperaturelibs.f90

とする)を、あらかじめコン

パイルしておく。

• f90 -c temperaturelibs.f90

(-c

はコンパイルだけ

やり、実行ファイルは作るなというオプション )

 すると、 temperaturelibs.o, temperaturelibs.mod

というファイルができる。

次に

f90 fahrenheit2celsius3.f90 temperaturelibs.o

というコマンドで実行ファイルを作る。

(13)

サブルーチンとは?

関数に似ている

関数よりも広く,

手続き

を行う。一群の変

数をいれると一群の変数を返してくれる

一つの関数の値だけでなく,例えば行列を

変数にすると,その固有値,固有ベクトル

を返してくれるようなものがサブルーチン

具体的に, f2c をサブルーチンの形で書き

直そう。

(14)

program fahrenheit2celsius

!---! This is a program to convert F to C

! 2005/5/16 Written by T. Ohtsuki

! 2005/5/17 subroutine version

!---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

call f2c(fahrenheit,celsius)

print *,"It is ",celsius,"degree in celsius"

end program fahrenheit2celsius

(15)

subroutine f2c(fah,cel)

!---! function to convert fahrenheit to celsius

!---IMPLICIT NONE

integer,parameter::double=selected_real_kind(14)

REAL(kind=double),

INTENT(IN)

:: fah

REAL(kind=double),

INTENT(OUT)

:: cel

cel=(fah-32._double)/1.8_double

END subroutine f2c

(16)

課題 

中点法のプログラムをサブルーチン化す

る。

来週の授業までに出す。出来た人は,落

下運動のプログラムにこれを適用し,

作を確認して

,来週の出席メールに添付

すること。

参照

関連したドキュメント

By performing center manifold reduction, the normal forms on the center manifold are derived to obtain the bifurcation diagrams of the model such as Hopf, homoclinic and double

We define the elliptic Hecke algebras for arbitrary marked elliptic root systems in terms of the corresponding elliptic Dynkin diagrams and make a ‘dictionary’ between the elliptic

In Section 4, we use double-critical decomposable graphs to study the maximum ratio between the number of double-critical edges in a non-complete critical graph and the size of

So far as the large time behaviour of solutions is concerned, we have noticed a few papers (e.g. [5, 9, 10, 14]) including some results about the ω-limit set of each single solution

The Bruhat ordering of every nontrivial quotient of a dihedral group is a chain, so all such Coxeter groups and their quotients have tight Bruhat orders by Theorem 2.3.. Also, we

Any nonstandard area-minimizing double bubble in H n in which at least one of the enclosed regions is connected consists of a topological sphere intersecting the axis of symmetry

Any nonstandard area-minimizing double bubble in H n in which at least one of the enclosed regions is connected consists of a topological sphere intersecting the axis of symmetry

Any nonstandard area-minimizing double bubble in H n in which at least one of the enclosed regions is connected consists of a topological sphere intersecting the axis of symmetry