Langevin C++ source

Loading...
Searching...
No Matches
sim_dplangevin_private.cpp
Go to the documentation of this file.
1
5
6#include "sim_dplangevin.hpp"
7
8// double round_up(const double value, const int n_decimals) {
9// const double multiplier = std::pow(10, 10);
10// const unsigned int int_value
11// = static_cast<unsigned int>((value) * multiplier);
12// const double rounded_value = (static_cast<double>(int_value)) / multiplier;
13// return rounded_value;
14// // return std::round((value+1e-14) * multiplier) / multiplier;
15// }
16
17 double round_time(const double time) {
18 const double multiplier = std::pow(10, 15);
19 if (std::abs(time)<std::pow(10, -15)) {
20 return 0.0;
21 }
22 else {
23 return std::round((std::abs(time)*multiplier+0.5))/multiplier;
24 }
25 // const unsigned int int_value
26 // = static_cast<unsigned int>((value) * multiplier);
27 // const double rounded_value = (static_cast<double>(int_value)) / multiplier;
28 // return rounded_value;
29}
30
32 int SimDP::count_epochs() const
33{
34 int n_epochs;
35 double t=0;
36 for (n_epochs=0; t<p.t_final; t=round_time(t+p.dt))
37 {
38 n_epochs++;
39 }
40 return n_epochs+1;
41}
42
43 bool SimDP::choose_integrator()
44{
45 switch (p.integration_method)
46 {
47 case (IntegrationMethod::RUNGE_KUTTA):
48 integrator = &DPLangevin::integrate_rungekutta;
49 return true;
50 case (IntegrationMethod::EULER):
51 integrator = &DPLangevin::integrate_euler;
52 return true;
53 default:
54 return false;
55 }
56}
57
58 bool SimDP::integrate(const int n_next_epochs)
59{
60 // Check a further n_next_epochs won't exceed total permitted steps
61 if (t_epochs.size() < i_next_epoch+n_next_epochs)
62 {
63 std::cout << "Too many epochs: "
64 << t_epochs.size()
65 << " < "
66 << i_next_epoch+n_next_epochs
67 << std::endl;
68 return false;
69 }
70
71 // Perform (possibly another another) n_next_epochs integration steps
72 int i;
73 double t;
74 // For the very first epoch, record mean density right now
75 if (i_next_epoch==1) {
76 dpLangevin->apply_boundary_conditions(p, 0);
77 mean_densities[0] = dpLangevin->get_mean_density();
78 i_current_epoch = 0;
79 t_current_epoch = 0;
80 }
81 // Loop over integration steps.
82 // Effectively increment epoch counter and add to Δt to time counter
83 // so that both point the state *after* each integration step is complete.
84 // In so doing, we will record t_epochs.size() + 1 total integration steps.
85 t_epochs[0] = 0;
86 for (
87 i=i_next_epoch, t=t_next_epoch;
88 i<i_next_epoch+n_next_epochs;
89 t=round_time(t+p.dt), i++
90 )
91 {
92 // Reapply boundary conditions prior to integrating
93 dpLangevin->apply_boundary_conditions(p, i);
94 // Perform a single integration over Δt
95 (dpLangevin->*integrator)(*rng);
96 // Record this epoch
97 t_epochs[i] = t;
98 mean_densities[i] = dpLangevin->get_mean_density();
99 i_current_epoch = i;
100 t_current_epoch = t;
101 };
102 // Set epoch and time counters to point to *after* the last integration step
103 i_next_epoch = i;
104 t_next_epoch = t;
105 return true;
106}
107
108 bool SimDP::pyprep_t_epochs()
109{
110 py_array_t epochs_array(n_epochs);
111 auto epochs_proxy = epochs_array.mutable_unchecked();
112 for (auto i=0; i<n_epochs; i++)
113 {
114 epochs_proxy(i) = t_epochs[i];
115 };
116 pyarray_t_epochs = epochs_array;
117 return true;
118}
119
120 bool SimDP::pyprep_mean_densities()
121{
122 py_array_t mean_densities_array(n_epochs);
123 auto mean_densities_proxy = mean_densities_array.mutable_unchecked();
124 for (auto i=0; i<n_epochs; i++)
125 {
126 mean_densities_proxy(i) = mean_densities[i];
127 };
128 pyarray_mean_densities = mean_densities_array;
129 return true;
130}
131
132 bool SimDP::pyprep_density_grid()
133{
134 if (not (p.n_cells == p.n_x * p.n_y * p.n_z))
135 {
136 std::cout << "prep_density: grid size problem" << std::endl;
137 return false;
138 }
139 // Assume we're working with a <=2d grid for now
140 py_array_t density_array({p.n_x, p.n_y});
141 auto density_proxy = density_array.mutable_unchecked();
142 for (auto i=0; i<p.n_cells; i++)
143 {
144 int x = i % p.n_x;
145 int y = i / p.n_x;
146 density_proxy(x, y) = dpLangevin->get_density_grid_value(i);
147 };
148 pyarray_density = density_array;
149 return true;
150}
BaseLangevin::integrate_rungekutta
void integrate_rungekutta(rng_t &rng)
Runge-Kutta + stochastic integration + grid update.
Definition langevin_integrate_rungekutta.cpp:13
BaseLangevin::integrate_euler
void integrate_euler(rng_t &rng)
Explicit Euler + stochastic integration + grid update.
Definition langevin_integrate_euler.cpp:10
SimDP::i_current_epoch
int i_current_epoch
Index of current epoch aka time step.
Definition sim_dplangevin.hpp:35
SimDP::i_next_epoch
int i_next_epoch
Index of next epoch aka time step.
Definition sim_dplangevin.hpp:37
SimDP::p
Parameters p
Model simulation parameters.
Definition sim_dplangevin.hpp:24
SimDP::pyarray_t_epochs
py_array_t pyarray_t_epochs
Python-compatible array of epochs time-series.
Definition sim_dplangevin.hpp:49
SimDP::mean_densities
dbl_vec_t mean_densities
Vector time-series of grid-averaged field density values.
Definition sim_dplangevin.hpp:47
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::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::integrator
void(DPLangevin::* integrator)(rng_t &)
Integrator: either a Runge-Kutta or an Euler method.
Definition sim_dplangevin.hpp:30
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::pyarray_density
py_array_t pyarray_density
Python-compatible array of current density grid.
Definition sim_dplangevin.hpp:53
SimDP::pyarray_mean_densities
py_array_t pyarray_mean_densities
Python-compatible array of mean density time-series.
Definition sim_dplangevin.hpp:51
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::dpLangevin
DPLangevin * dpLangevin
Instance of DP Langevin integrator class (pointer to instance)
Definition sim_dplangevin.hpp:28
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
SimDP::t_current_epoch
double t_current_epoch
Time of current epoch.
Definition sim_dplangevin.hpp:39
py_array_t
py::array_t< double, py::array::c_style > py_array_t
Type for Python arrays of doubles.
Definition langevin_types.hpp:50
sim_dplangevin.hpp
Class that manages simulation of DPLangevin equation.