I need to use a Fortran code to solve stochastic differential equation (SDE). I looked at the famous Fortran code website by Burkardt,
https://people.math.sc.edu/Burkardt/f_src/stochastic_rk/stochastic_rk.html
I particular looked at the rk4_ti_step subroutine in stochastic_rk.f90 code,
https://people.math.sc.edu/Burkardt/f_src/stochastic_rk/stochastic_rk.f90
My optimized version is below,
subroutine rk4_ti_step_mod ( x, t, h, q, fi, gi, seed, xstar )
use random
implicit none
real ( kind = 8 ), external :: fi
real ( kind = 8 ), external :: gi
real ( kind = 8 ) h
real ( kind = 8 ) k1
real ( kind = 8 ) k2
real ( kind = 8 ) k3
real ( kind = 8 ) k4
real ( kind = 8 ) q
real ( kind = 8 ) r8_normal_01
integer ( kind = 4 ) seed
real ( kind = 8 ) t
real ( kind = 8 ) t1
real ( kind = 8 ) t2
real ( kind = 8 ) t3
real ( kind = 8 ) t4
real ( kind = 8 ) w1
real ( kind = 8 ) w2
real ( kind = 8 ) w3
real ( kind = 8 ) w4
real ( kind = 8 ) x
real ( kind = 8 ) x1
real ( kind = 8 ) x2
real ( kind = 8 ) x3
real ( kind = 8 ) x4
real ( kind = 8 ) xstar
real ( kind = 8 ) :: qoh
real ( kind = 8 ) :: normal(4)
real ( kind = 8 ), parameter :: a21 = 2.71644396264860D+00 &
,a31 = - 6.95653259006152D+00 &
,a32 = 0.78313689457981D+00 &
,a41 = 0.0D+00 &
,a42 = 0.48257353309214D+00 &
,a43 = 0.26171080165848D+00 &
,a51 = 0.47012396888046D+00 &
,a52 = 0.36597075368373D+00 &
,a53 = 0.08906615686702D+00 &
,a54 = 0.07483912056879D+00 &
,q1 = 2.12709852335625D+00 &
,q2 = 2.73245878238737D+00 &
,q3 = 11.22760917474960D+00 &
,q4 = 13.36199560336697D+00
real ( kind = 8 ), parameter, dimension(4) :: qarray = [ 2.12709852335625D+00 &
,2.73245878238737D+00 &
,11.22760917474960D+00 &
,13.36199560336697D+00 ]
real ( kind = 8 ) :: warray(4)
integer (kind = 4) :: i
qoh = q / h
normal = gaussian(4)
do i =1,4
warray(i) = normal(i)*sqrt(qarray(i)*qoh)
enddo
t1 = t
x1 = x
k1 = h * ( fi ( x1 ) + gi ( x1 ) * warray(1) )
t2 = t1 + a21 * h
x2 = x1 + a21 * k1
k2 = h * ( fi ( x2 ) + gi ( x2 ) * warray(2) )
t3 = t1 + ( a31 + a32 )* h
x3 = x1 + a31 * k1 + a32 * k2
k3 = h * ( fi ( x3 ) + gi ( x3 ) * warray(3) )
t4 = t1 + ( a41 + a42 + a43 ) * h
x4 = x1 + a41 * k1 + a42 * k2
k4 = h * ( fi ( x4 ) + gi ( x4 ) * warray(4) )
xstar = x1 + a51 * k1 + a52 * k2 + a53 * k3 + a54 * k4
return
end
Note that I use my module of random number, and gaussian is my random number function, this part does not matter.
I just wonder,
- Can anyone give some suggestions as to can the code be further optimized?
- Does anyone know what is the best/fastest SDE Fortran subroutine? Or what algorithm is the best?
Thank you very much!