Langevin C++ source

Loading...
Searching...
No Matches
sim_dplangevin.cpp
Go to the documentation of this file.
1
5
6#include "sim_dplangevin.hpp"
7
11 SimDP::SimDP(
12 const double linear, const double quadratic,
13 const double diffusion, const double noise,
14 const double t_final,
15 const double dx, const double dt,
16 const int random_seed,
17 const GridDimension grid_dimension,
18 const int_vec_t grid_size,
19 const gt_vec_t grid_topologies,
20 const bc_vec_t boundary_conditions,
21 const dbl_vec_t bc_values,
22 const InitialCondition initial_condition,
23 const dbl_vec_t ic_values,
24 const IntegrationMethod integration_method,
25 const bool do_snapshot_grid,
26 const bool do_verbose
27) : coefficients(linear, quadratic, diffusion, noise),
28 p(
29 t_final,
30 dx,
31 dt,
32 random_seed,
33 grid_dimension,
34 grid_size,
35 grid_topologies,
36 boundary_conditions,
37 bc_values,
38 initial_condition,
39 ic_values,
40 integration_method
41 ),
42 do_snapshot_grid(do_snapshot_grid),
43 do_verbose(do_verbose)
44{
45 rng = new rng_t(p.random_seed);
46 dpLangevin = new DPLangevin(p);
47 if (do_verbose)
48 {
49 coefficients.print();
50 p.print();
51 }
52}
53
58 bool SimDP::initialize(int n_decimals)
59{
60 if (not dpLangevin->construct_grid(p)) {
61 std::cout
62 << "SimDP::initialize failure: couldn't construct grid"
63 << std::endl;
64 return false;
65 }
66 if (not dpLangevin->initialize_grid(p, *rng)) {
67 std::cout
68 << "SimDP::initialize failure: couldn't initialize grid"
69 << std::endl;
70 return false;
71 }
72 dpLangevin->prepare(coefficients);
73 this->n_decimals = n_decimals;
74 n_epochs = count_epochs();
75 t_epochs = dbl_vec_t(n_epochs, 0.0);
76 mean_densities = dbl_vec_t(n_epochs, 0.0);
77 // Treat epoch#0 as the initial grid state
78 // So after initialization, we are nominally at epoch#1
79 i_next_epoch = 1;
80 t_next_epoch = p.dt;
81 if (not choose_integrator())
82 {
83 std::cout << "SimDP::initialize failure: unable to choose integrator" << std::endl;
84 return false;
85 }
86 if (not dpLangevin->check_boundary_conditions(p))
87 {
88 std::cout << "SimDP::initialize failure: wrong number of boundary conditions" << std::endl;
89 return false;
90 }
91 is_initialized = true;
92 return is_initialized;
93}
94
99 bool SimDP::run(const int n_next_epochs)
100{
101 if (not is_initialized)
102 {
103 std::cout << "SimDP::run failure: must initialize first" << std::endl;
104 return false;
105 }
106 did_integrate = integrate(n_next_epochs);
107 return did_integrate;
108}
109
114 bool SimDP::postprocess()
115{
116 if (not is_initialized) {
117 std::cout
118 << "SimDP::postprocess failure: no data to process yet"
119 << std::endl;
120 return false;
121 }
122 bool did_process_grid;
123 if (do_snapshot_grid) {
124 did_process_grid = pyprep_density_grid();
125 }
126 else { did_process_grid = true; }
127 bool did_process_time_series = (
128 pyprep_t_epochs() and pyprep_mean_densities()
129 );
130 return (did_process_grid and did_process_time_series);
131}
DPLangevin
DPLangevin model application of BaseLangevin class integrator.
Definition dplangevin.hpp:16
SimDP::initialize
bool initialize(int n_decimals)
Initialize the model simulation.
Definition sim_dplangevin.cpp:58
SimDP::run
bool run(const int n_next_epochs)
Execute the model simulation for n_next_epochs
Definition sim_dplangevin.cpp:99
SimDP::postprocess
bool postprocess()
Process the model results data if available.
Definition sim_dplangevin.cpp:114
SimDP::do_verbose
bool do_verbose
Flag whether to report sim parameters etc.
Definition sim_dplangevin.hpp:61
SimDP::i_next_epoch
int i_next_epoch
Index of next epoch aka time step.
Definition sim_dplangevin.hpp:37
SimDP::do_snapshot_grid
bool do_snapshot_grid
Flag whether to take density grid snapshots.
Definition sim_dplangevin.hpp:59
SimDP::p
Parameters p
Model simulation parameters.
Definition sim_dplangevin.hpp:24
SimDP::mean_densities
dbl_vec_t mean_densities
Vector time-series of grid-averaged field density values.
Definition sim_dplangevin.hpp:47
SimDP::SimDP
SimDP(const double linear, const double quadratic, const double diffusion, const double noise, const double t_final, const double dx, const double dt, const int random_seed, const GridDimension grid_dimension, const int_vec_t grid_size, const gt_vec_t grid_topologies, const bc_vec_t boundary_conditions, const dbl_vec_t bc_values, const InitialCondition initial_condition, const dbl_vec_t ic_values, const IntegrationMethod integration_method, const bool do_snapshot_grid, const bool do_verbose)
Constructor.
Definition sim_dplangevin.cpp:11
SimDP::count_epochs
int count_epochs() const
Count upcoming number of epochs by running a dummy time-stepping loop.
Definition sim_dplangevin_private.cpp:32
SimDP::coefficients
Coefficients coefficients
Langevin equation coefficients.
Definition sim_dplangevin.hpp:22
SimDP::choose_integrator
bool choose_integrator()
Chooses function implementing either Runge-Kutta or Euler integration methods.
Definition sim_dplangevin_private.cpp:43
SimDP::t_next_epoch
double t_next_epoch
Time of next epoch.
Definition sim_dplangevin.hpp:41
SimDP::is_initialized
bool is_initialized
Flag whether simulation has been initialized or not.
Definition sim_dplangevin.hpp:57
SimDP::pyprep_mean_densities
bool pyprep_mean_densities()
Generate a Python-compatible version of the mean densities time-series vector.
Definition sim_dplangevin_private.cpp:120
SimDP::n_epochs
int n_epochs
Total number of simulation time steps aka "epochs".
Definition sim_dplangevin.hpp:33
SimDP::t_epochs
dbl_vec_t t_epochs
Vector time-series of epochs.
Definition sim_dplangevin.hpp:43
SimDP::pyprep_t_epochs
bool pyprep_t_epochs()
Generate a Python-compatible version of the epochs time-series vector.
Definition sim_dplangevin_private.cpp:108
SimDP::n_decimals
int n_decimals
Truncation number of decimal places when summing Δt.
Definition sim_dplangevin.hpp:45
SimDP::dpLangevin
DPLangevin * dpLangevin
Instance of DP Langevin integrator class (pointer to instance)
Definition sim_dplangevin.hpp:28
SimDP::did_integrate
bool did_integrate
Flag whether integration step was successful or not.
Definition sim_dplangevin.hpp:55
SimDP::pyprep_density_grid
bool pyprep_density_grid()
Generate a Python-compatible version of the current density grid.
Definition sim_dplangevin_private.cpp:132
SimDP::rng
rng_t * rng
Random number generation function (Mersenne prime) (pointer to RNG)
Definition sim_dplangevin.hpp:26
SimDP::integrate
bool integrate(const int n_next_epochs)
Perform Dornic-type integration of the DP Langevin equation for n_next_epochs
Definition sim_dplangevin_private.cpp:58
GridDimension
GridDimension
Density field grid dimension: only 1D or 2D grids implemented so far.
Definition langevin_enums.hpp:15
IntegrationMethod
IntegrationMethod
Deterministic integration method: default is 4th-order Runge-Kutta; can be explicit Euler.
Definition langevin_enums.hpp:58
InitialCondition
InitialCondition
Grid density field initial condition: random uniform or Gaussian variates; constant everywhere; or ce...
Definition langevin_enums.hpp:49
gt_vec_t
std::vector< GridTopology > gt_vec_t
Type for specifying grid topology in each direction x, y, z...
Definition langevin_types.hpp:45
int_vec_t
std::vector< int > int_vec_t
Type for vectors of integers.
Definition langevin_types.hpp:21
dbl_vec_t
std::vector< double > dbl_vec_t
Type for vectors of doubles.
Definition langevin_types.hpp:19
rng_t
std::mt19937 rng_t
Use Mersenne Twister random number generator.
Definition langevin_types.hpp:16
sim_dplangevin.hpp
Class that manages simulation of DPLangevin equation.