comparison rkmain.f90 @ 2:206e90e91191

make rkmod.f90
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Sun, 21 Jul 2024 03:56:49 +0900
parents 954faa9f6837
children 2347cff808b0
comparison
equal deleted inserted replaced
1:954faa9f6837 2:206e90e91191
1 program rkmain 1 program rkmain
2 use rungekutta !ルンゲクッタ解法をまとめたモジュール 2 use rkmod
3 implicit none 3 implicit none
4 real(kind=dpkind),dimension(3) :: x !状態変数 4 real(kind=dpkind),dimension(3) :: x
5 type(rk) :: rktype !ルンゲクッタ法ソルバに関連する型 5 real(kind=dpkind) :: max_t
6 real(kind=dpkind),allocatable,dimension(:,:) :: r 6 real(kind=dpkind),allocatable,dimension(:,:) :: r
7 real(kind=dpkind) :: max_t
8 7
9 !初期値の指定 8 x = [0.5d0,0.5d0,0.5d0]
10 x = [0.5e0,0.5e0,0.5e0] 9 max_t = 5d1
11 max_t = 5e1
12 call calc(x, max_t, r) 10 call calc(x, max_t, r)
13 print *, r; 11 print *, r
14
15 !========================================
16 contains
17 !微分方程式の右辺を定義
18 function rhside(self,time,wk)
19 class(rk),intent(inout) :: self !parameter
20 real(kind=dpkind) :: time
21 real(kind=dpkind),dimension(:) :: wk
22 real(kind=dpkind),dimension(size(wk)) :: rhside
23 real(kind=dpkind),parameter :: a=10e0,b=28e0,c=8e0/3e0
24 rhside(1)=a*(wk(2)-wk(1))
25 rhside(2)=b*wk(1)-wk(2)-wk(1)*wk(3)
26 rhside(3)=wk(1)*wk(2)-c*wk(3)
27 end function
28 !Rから呼び出すサブルーチン
29 subroutine calc(x, max_t, r)
30 implicit none
31 real(kind=dpkind),dimension(3),intent(in) :: x
32 real(kind=dpkind),intent(in) :: max_t
33 real(kind=dpkind),allocatable,dimension(:,:),intent(out) :: r
34 real(kind=dpkind) :: zero = 0
35 integer i, max_i
36 !微分方程式の右辺を計算する関数、解法、時間刻みの指定
37 call rktype%setup(rhside,"rk4",1d-5)
38 max_i = int(max_t / 0.01)
39 allocate(r(0:max_i, 4))
40 !時刻max_t秒まで、0.01秒毎に配列に記録。
41 !ただし、内部的には刻み幅1e-5秒としている。
42 r(0, :) = [zero, x] ! 開始点
43 do i = 1, max_i
44 call rktype%solver(rktype%time+1e-2,x) !積分実行
45 r(i, :) = [rktype%time, x]
46 end do
47 end subroutine
48 end program 12 end program
49 13