Langevin C++ source

Loading...
Searching...
No Matches
langevin_integrate_rungekutta.cpp
Go to the documentation of this file.
1
5
6#include "langevin_types.hpp"
7#include "langevin_base.hpp"
8
13 void BaseLangevin::integrate_rungekutta(rng_t& rng)
14{
15 auto step1 = [&](grid_t& aux_grid, grid_t& k1_grid, const double dtf)
16 {
17 for (auto i=0; i<n_cells; i++)
18 {
19 k1_grid[i] = nonlinear_rhs(i, density_grid);
20 aux_grid[i] = density_grid[i] + k1_grid[i]*dtf;
21 }
22 };
23 auto step2or3 = [&](
24 const grid_t& aux_grid_in, grid_t& aux_grid_out, grid_t& k23_grid,
25 const double dtf)
26 {
27 for (auto i=0; i<n_cells; i++)
28 {
29 k23_grid[i] = nonlinear_rhs(i, aux_grid_in);
30 aux_grid_out[i] = density_grid[i] + k23_grid[i]*dtf;
31 }
32 };
33 auto step4 = [&](
34 const grid_t& aux_grid, const grid_t& k1_grid, const grid_t& k2_grid,
35 const grid_t& k3_grid, rng_t& rng, const double dtf)
36 {
37 mean_density = 0.0;
38 for (auto i=0; i<n_cells; i++)
39 {
40 // Runge-Kutta 4th step
41 auto k4 = nonlinear_rhs(i, aux_grid);
42 density_grid[i] += (k1_grid[i] + 2*(k2_grid[i]+k3_grid[i]) +k4)*dtf;
43 // Stochastic step
44 poisson_sampler = poisson_dist_t(lambda_on_explcdt*density_grid[i]);
45 gamma_sampler = gamma_dist_t(poisson_sampler(rng), 1/lambda);
46 density_grid[i] = gamma_sampler(rng);
47 // Incrementally compute mean density
48 mean_density += density_grid[i];
49 }
50 mean_density /= static_cast<double>(n_cells);
51 };
52
53 step1(aux_grid1, k1_grid, dt/2);
54 step2or3(aux_grid1, aux_grid2, k2_grid, dt/2);
55 step2or3(aux_grid2, aux_grid1, k3_grid, dt);
56 step4(aux_grid1, k1_grid, k2_grid, k3_grid, rng, dt/6);
57}
BaseLangevin::poisson_sampler
poisson_dist_t poisson_sampler
Function generating Poisson variates.
Definition langevin_base.hpp:33
BaseLangevin::density_grid
grid_t density_grid
Density field grid.
Definition langevin_base.hpp:21
BaseLangevin::lambda
double lambda
Dornic method stochastic-step variable.
Definition langevin_base.hpp:44
BaseLangevin::dt
double dt
Time step, i.e, epoch-to-epoch Δt.
Definition langevin_base.hpp:26
BaseLangevin::n_cells
int n_cells
Total number of cells in n-D grid.
Definition langevin_base.hpp:19
BaseLangevin::lambda_on_explcdt
double lambda_on_explcdt
Dornic method stochastic-step variable.
Definition langevin_base.hpp:46
BaseLangevin::nonlinear_rhs
virtual double nonlinear_rhs(const int i_cell, const grid_t &field) const
Method to set nonlinear RHS of Langevin equation for deterministic integration step: to be defined by...
Definition langevin_base.hpp:89
BaseLangevin::aux_grid1
grid_t aux_grid1
Temporary density grid used to perform an integration step.
Definition langevin_base.hpp:55
BaseLangevin::mean_density
double mean_density
Grid-average of density field.
Definition langevin_base.hpp:30
BaseLangevin::integrate_rungekutta
void integrate_rungekutta(rng_t &rng)
Runge-Kutta + stochastic integration + grid update.
Definition langevin_integrate_rungekutta.cpp:13
BaseLangevin::aux_grid2
grid_t aux_grid2
Temporary density grid used to perform an integration step.
Definition langevin_base.hpp:57
BaseLangevin::k1_grid
grid_t k1_grid
Runge-Kutta variable grid #1.
Definition langevin_base.hpp:49
BaseLangevin::k2_grid
grid_t k2_grid
Runge-Kutta variable grid #2.
Definition langevin_base.hpp:51
BaseLangevin::gamma_sampler
gamma_dist_t gamma_sampler
Function generating gamma variates.
Definition langevin_base.hpp:35
BaseLangevin::k3_grid
grid_t k3_grid
Runge-Kutta variable grid #3.
Definition langevin_base.hpp:53
langevin_base.hpp
Base class for Langevin equation integrator.
langevin_types.hpp
Type definitions for BaseLangevin integrator.
gamma_dist_t
std::gamma_distribution< double > gamma_dist_t
Type for function generating gamma variates.
Definition langevin_types.hpp:37
grid_t
std::vector< double > grid_t
Type for density grid.
Definition langevin_types.hpp:24
poisson_dist_t
std::poisson_distribution< int > poisson_dist_t
Type for function generating Poisson variates.
Definition langevin_types.hpp:35
rng_t
std::mt19937 rng_t
Use Mersenne Twister random number generator.
Definition langevin_types.hpp:16