This is a combination of these two previous questions:
I need to solve the following differential equation for $f_N$,
$$\frac{d}{d t} f_{N}(\mathbf{k}, t)=D(\mathbf{k}, t)\left[f_{N}(\mathbf{k}, t)-f_{N}^{\mathrm{eq}}(\mathbf{k}, t)\right]$$
where $\mathbf k$ is a parameter and $t$ is time. I need the values of $f_N$ at specific times $t=T$; where $T$ is a log-spaced array from $10^{-6}$ to $10^3$. In Python
, as suggested in the first link, I would do something like this:
solve_ivp(lambda t, y: ODE__system(t, y),
t_span=(1e-6, 1e3), y0=[0], t_eval=T, vectorized=False,
method='BDF', rtol=1e-12, atol=1e-15)
I need to do the same in C++
. For the log-spaced times, I have the following piece of code:
std::vector<double> logspace(double a, double b, int k) {
/*
y = linspace(start, stop, num=num, endpoint=endpoint, axis=axis)
if dtype is None:
return _nx.power(base, y)
return _nx.power(base, y).astype(dtype, copy=False)
*/
const auto exp_scale = (b - a) / (k - 1);
std::vector<double> logspace;
logspace.reserve(k);
for (int i = 0; i < k; i++) {
logspace.push_back(a + i * exp_scale);
}
std::for_each(logspace.begin(), logspace.end(),
[](double &x) { x = pow(10, x); });
return logspace;
}
...
std::vector<double> T = logspace(-6, 3, 500);
Regarding the ODE, I thought boost::odeint
with a dense output stepper would help -as suggested in the second question- but I don't understand how to implement a non-constant step. Any guidance would be highly appreciated.