'Varargin' in Fortran
Consider the following: I would like to solve a differential equation in Fortran (with a Runge-Kutta routine), e.g. x'=-a*x, where a is a constant. I then want to solve this for different a. 754
I now would like to write the code such that it can be applied to other problems. But in general I do not know what additional input my differential equation may need (and what size and type they have). So my question is if it is possible in Fortran to pass all additional parameters to my Runge-Kutta method (at best with a single property) so that it passes it on to my differential equation so that it can be used there? With a known set of parameters this is easy but what about an unknown set?
My approach so far is this:
PROGRAM main
USE RKModule
IMPLICIT NONE
INTEGER <IP_ADDRESS> l
REAL*8, PARAMETER <IP_ADDRESS> num_t = 1e3
REAL*8, PARAMETER <IP_ADDRESS> min_t = 0.0d0
REAL*8, PARAMETER <IP_ADDRESS> max_t = 10.0d0
REAL*8, DIMENSION(1), PARAMETER <IP_ADDRESS> a = 1.8d0
REAL*8, DIMENSION(num_t) <IP_ADDRESS> dGrid_t, grid_t
COMPLEX*16, DIMENSION(num_t) <IP_ADDRESS> sol
COMPLEX*16, DIMENSION(1) <IP_ADDRESS> x_in, x_out
DO l = 1, num_t
dGrid_t(l) = (max_t - min_t) / DBLE(num_t)
grid_t(l) = min_t + DBLE(l-0.5d0)*dGrid_t(l)
END DO
x_in(1) = dcmplx(1.0d0, 0.0d0)
x_out(1) = dcmplx(0.0d0, 0.0d0)
DO l = 1, num_t
sol(l) = x_in(1)
CALL rk45(x_out, DGL, x_in, grid_t(l), dGrid_t(l), a)
x_in = x_out
END DO
OPEN(UNIT = 1000, FILE = 'sol.dat', ACTION = 'WRITE')
WRITE(1000, FMT = '(e14.7e3, 1x, e14.7e3)', ADVANCE = "NO") sol
CLOSE(1000)
CONTAINS
SUBROUTINE DGL(t,f,df,vararg_in)
REAL*8, INTENT(IN) <IP_ADDRESS> t
COMPLEX*16, DIMENSION(:), INTENT(IN) <IP_ADDRESS> f
COMPLEX*16, DIMENSION(:), INTENT(OUT) <IP_ADDRESS> df
REAL*8, DIMENSION(:), INTENT(IN), OPTIONAL <IP_ADDRESS> vararg_in
REAL*8, DIMENSION(1) <IP_ADDRESS> b
b = 1. 935