Langevin C++ source

Loading...
Searching...
No Matches
langevin_ic.cpp
Go to the documentation of this file.
1
5
6#include "langevin_types.hpp"
7#include "langevin_base.hpp"
8
9 bool BaseLangevin::initialize_grid(const Parameters p, rng_t& rng)
10{
11 // Set grid cells to have uniformly random values
12 // between min_value and max_value
13 auto ic_random_uniform = [&](
14 rng_t& rng,
15 const double min_value,
16 const double max_value
17 )
18 {
19 uniform_dist_t uniform_sampler(min_value, max_value);
20 mean_density = 0.0;
21 for (auto i=0; i<density_grid.size(); i++)
22 {
23 density_grid[i] = uniform_sampler(rng);
24 mean_density += density_grid[i];
25 }
26 mean_density /= static_cast<double>(n_cells);
27 };
28
29 // Set grid cells to have Gaussian-distributed random values
30 // with given mean and standard deviation
31 auto ic_random_gaussian = [&](
32 rng_t& rng,
33 const double mean,
34 const double stddev
35 )
36 {
37 gaussian_dist_t gaussian_sampler(mean, stddev);
38 mean_density = 0.0;
39 for (auto i=0; i<density_grid.size(); i++)
40 {
41 density_grid[i] = gaussian_sampler(rng);
42 mean_density += density_grid[i];
43 }
44 mean_density /= static_cast<double>(n_cells);
45 };
46
47 // Set all the grid cells to have same value
48 auto ic_constant_value = [&](const double density_value)
49 {
50 density_grid = grid_t(n_cells, density_value);
51 mean_density = density_value;
52 };
53
54 // Set all the grid cells to zero except a single specified cell
55 auto ic_single_seed = [&](const int i_cell, const double value)
56 {
57 density_grid[i_cell] = value;
58 mean_density = value / static_cast<double>(n_cells);
59 };
60
61 switch (p.initial_condition)
62 {
63 int i_cell;
64 case (InitialCondition::CONSTANT_VALUE):
65 ic_constant_value(p.ic_values.at(0));
66 return true;
67 case (InitialCondition::SINGLE_SEED):
68 if (p.grid_dimension==GridDimension::D1)
69 {
70 i_cell = ( static_cast<int>(p.ic_values.at(1)) );
71 if (i_cell<0 or i_cell>=p.n_x) { return false; }
72 }
73 else if (p.grid_dimension==GridDimension::D2)
74 {
75 i_cell = (static_cast<int>(p.ic_values.at(1))
76 + static_cast<int>(p.ic_values.at(2))*p.n_x);
77 if (i_cell<0 or i_cell>=p.n_x*p.n_y) { return false; }
78 }
79 else if (p.grid_dimension==GridDimension::D3)
80 {
81 return false;
82 }
83 else
84 {
85 return false;
86 }
87 ic_single_seed(i_cell, p.ic_values.at(0));
88 return true;
89 case (InitialCondition::RANDOM_UNIFORM):
90 ic_random_uniform(rng, p.ic_values.at(0), p.ic_values.at(1));
91 return true;
92 case (InitialCondition::RANDOM_GAUSSIAN):
93 ic_random_gaussian(rng, p.ic_values.at(0), p.ic_values.at(1));
94 return true;
95 default:
96 return false;
97 }
98}
BaseLangevin::density_grid
grid_t density_grid
Density field grid.
Definition langevin_base.hpp:21
BaseLangevin::initialize_grid
bool initialize_grid(const Parameters parameters, rng_t &rng)
Initial condition for density field: uniformly random.
Definition langevin_ic.cpp:9
BaseLangevin::n_cells
int n_cells
Total number of cells in n-D grid.
Definition langevin_base.hpp:19
BaseLangevin::mean_density
double mean_density
Grid-average of density field.
Definition langevin_base.hpp:30
BaseLangevin::gaussian_sampler
gaussian_dist_t gaussian_sampler
Function generating normal variates.
Definition langevin_base.hpp:37
langevin_base.hpp
Base class for Langevin equation integrator.
langevin_types.hpp
Type definitions for BaseLangevin integrator.
grid_t
std::vector< double > grid_t
Type for density grid.
Definition langevin_types.hpp:24
uniform_dist_t
std::uniform_real_distribution< double > uniform_dist_t
Type for function generating uniformly distributed variates.
Definition langevin_types.hpp:41
gaussian_dist_t
std::normal_distribution< double > gaussian_dist_t
Type for function generating Gaussian variates.
Definition langevin_types.hpp:39
rng_t
std::mt19937 rng_t
Use Mersenne Twister random number generator.
Definition langevin_types.hpp:16
Parameters
Container for BaseLangevin integrator parameters.
Definition langevin_parameters.hpp:21