|
| 1 | +module time_integration_m |
| 2 | + implicit none |
| 3 | + |
| 4 | +#ifndef HAVE_TEMPLATE_SUPPORT |
| 5 | + logical prevent_empty_module |
| 6 | +#else |
| 7 | + |
| 8 | + private |
| 9 | + public :: runge_kutta_4th_order_t |
| 10 | + public :: binary_operator_r |
| 11 | + |
| 12 | + requirement binary_operator_r(T, U, V, op) |
| 13 | + |
| 14 | +#if defined(__LFORTRAN__) && (__lfortran_major__ < 1) && (__lfortran_minor__ < 1) && (__lfortran_patch_level__ < 1) |
| 15 | + type, deferred :: T |
| 16 | + type, deferred :: U |
| 17 | + type, deferred :: V |
| 18 | + |
| 19 | + interface |
| 20 | +#else |
| 21 | + deferred type :: T, U, V |
| 22 | + |
| 23 | + deferred interface |
| 24 | +#endif |
| 25 | + pure function op(x,y) result(z) |
| 26 | + type(T), intent(in) :: x |
| 27 | + type(U), intent(in) :: y |
| 28 | + type(V) :: z |
| 29 | + end function |
| 30 | + end interface |
| 31 | + |
| 32 | + end requirement |
| 33 | + |
| 34 | +#if defined(__LFORTRAN__) && (__lfortran_major__ < 1) && (__lfortran_minor__ < 1) && (__lfortran_patch_level__ < 1) |
| 35 | + template runge_kutta_4th_order_t( & |
| 36 | + TFA, TFB, TFC, TFD, R & ! types |
| 37 | + ,tmult, rtmult, taplus, tcplus, rhs, rdiv & ! functions |
| 38 | + ) |
| 39 | + type, deferred :: TFA |
| 40 | + type, deferred :: TFB |
| 41 | + type, deferred :: TFC |
| 42 | + type, deferred :: TFD |
| 43 | + type, deferred :: R |
| 44 | +#else |
| 45 | + template runge_kutta_4th_order_t{ & |
| 46 | + TFA, TFB, TFC, TFD, R & ! types |
| 47 | + ,tmult, rtmult, taplus, tcplus, rhs, rdiv & ! functions |
| 48 | + } |
| 49 | + deferred type :: TFA, TFB, TFC, TFD, R |
| 50 | +#endif |
| 51 | + |
| 52 | + require :: binary_operator_r(integer, TFC, TFC, tmult) |
| 53 | + require :: binary_operator_r( TFC, R, TFB, rtmult) |
| 54 | + require :: binary_operator_r( TFA, TFB, TFA, taplus) |
| 55 | + require :: binary_operator_r( TFC, TFC, TFC, tcplus) |
| 56 | + require :: binary_operator_r( TFA, TFD, TFC, rhs) |
| 57 | + require :: binary_operator_r( R, integer, R, rdiv) |
| 58 | + |
| 59 | + interface operator(+) |
| 60 | + module procedure taplus, tcplus |
| 61 | + end interface |
| 62 | + |
| 63 | + interface operator(*) |
| 64 | + module procedure rtmult, tmult |
| 65 | + end interface |
| 66 | + |
| 67 | + interface operator(/) |
| 68 | + module procedure rdiv |
| 69 | + end interface |
| 70 | + |
| 71 | + contains |
| 72 | + |
| 73 | + pure function step(s, v, dt) result(s_next) |
| 74 | + type(TFA), intent(in) :: s |
| 75 | + type(TFD), intent(in) :: v |
| 76 | + type(R) , intent(in) :: dt |
| 77 | + type(TFA) s_next |
| 78 | + |
| 79 | + associate(k1 => rhs(s, v)) |
| 80 | + associate(k2 => rhs(s + k1*(dt/2), v)) |
| 81 | + associate(k3 => rhs(s + k2*(dt/2), v)) |
| 82 | + associate(k4 => rhs(s + k3* dt, v)) |
| 83 | + s_next = s + (k1 + 2*k2 + 2*k3 + k4) * (dt/6) |
| 84 | + end associate; end associate; end associate; end associate |
| 85 | + end function |
| 86 | + |
| 87 | + end template |
| 88 | + |
| 89 | +#endif |
| 90 | + |
| 91 | +end module |
0 commit comments