22 std::vector<double> &x;
23 std::vector<double> &y;
24 std::vector<double> &vx;
25 std::vector<double> &vy;
32 std::function<double (
void)>
normal;
37 stepper (std::vector<double> &x_, std::vector<double> &y_,
38 std::vector<double> &vx_, std::vector<double> &vy_,
39 std::vector<double> &dist_, std::vector<double> &dist_dx_,
40 std::vector<double> &dist_dy_,
const double hx_,
const double hy_,
41 double dt_,
double D_, std::function<
double (
void)> &noise_)
52 void operator() (
int n) {
60 double dx =
vx[n] *
dt + dxb;
61 double dy =
vy[n] *
dt + dyb;
63 if (
dist[n] <= 2*dx ||
dist[n] < 2*dy) {
65 double verx =
dist_dx[n] / normdist;
66 double very =
dist_dy[n] / normdist;
67 if (
dist[n] < 0) { verx =
x[n] > 1.6 ? -1. : 1.; very = 0.; }
77 if (
x[n] > 8.9) {
x[n] = .1;
y[n] = 1.6; };
78 y[n] = std::min (3.1, std::max (0.1,
y[n]));
79 x[n] = std::min (8.9, std::max (0.1,
x[n]));
90 cdf::timer::timer_t timer;
93 constexpr auto filename =
"velocity_comsol.json";
96 std::ifstream inbuf (filename);
110 std::map<std::string, std::vector<double>> vars=
111 j[
"grid_vars"].get<std::map<std::string, std::vector<double>>> ();
116 std::random_device rd2;
118 std::normal_distribution<> normal;
119 std::function<double ()> noise = [&gen2, &rd2, &normal] () {
return normal(gen2); };
131 1.e-3, 1.e-1, noise);
133 p.
g2p (vars, {
"vx",
"vy",
"dist"}, {
"VX",
"VY",
"DIST"});
135 constexpr int nsave = 300;
136 constexpr double tmax = 2 * 8.5 / 40.;
137 constexpr double dtsave = tmax / nsave;
141 for (
int isave = 0; isave < nsave; ++isave) {
146 p.
p2g (vars, {
"M"}, {
"rho"},
true);
149 timer.tic(
"save particles");
151 const std::string ofilename =
"particle";
152 const std::string ofileext =
".csv";
153 const std::string numfile = std::string(
".") + std::to_string(isave);
155 std::ofstream outbuf (ofilename + numfile + ofileext);
158 timer.toc(
"save particles");
161 timer.tic(
"save grid");
162 const std::string gfilename = std::string(
"grid.") + std::to_string(isave) + std::string(
".vts");
164 timer.toc(
"save grid");
166 while (t < dtsave * (isave + 1)) {
170 timer.tic(
"clean particle properties interpolated from grid");
171 std::fill(p.
dprops[
"VX"].begin (), p.
dprops[
"VX"].end (), 0.0);
172 std::fill(p.
dprops[
"VY"].begin (), p.
dprops[
"VY"].end (), 0.0);
173 std::fill(p.
dprops[
"DIST"].begin (), p.
dprops[
"DIST"].end (), 0.0);
174 std::fill(p.
dprops[
"DIST_DX"].begin (), p.
dprops[
"DIST_DX"].end (), 0.0);
175 std::fill(p.
dprops[
"DIST_DY"].begin (), p.
dprops[
"DIST_DY"].end (), 0.0);
176 timer.toc(
"clean particle properties interpolated from grid");
178 timer.tic(
"clean rho");
179 std::fill(vars[
"rho"].begin (), vars[
"rho"].end (), 0.0);
180 timer.toc(
"clean rho");
184 p.
g2p (vars, {
"vx",
"vy",
"dist"}, {
"VX",
"VY",
"DIST"});
189 auto maxvx = std::max_element (p.
dprops[
"VX"].begin (), p.
dprops[
"VX"].end ());
190 state.
dt = .5 * qg.
hx() / (*maxvx);
191 auto maxvy = std::max_element (p.
dprops[
"VY"].begin (), p.
dprops[
"VY"].end ());
192 state.
dt = std::min (state.
dt, .5 * qg.
hy() / (*maxvy));
193 if (t + state.
dt > dtsave * (isave + 1)) state.
dt = dtsave * (isave + 1) - t;
194 std::cout <<
"hx = " << qg.
hx() <<
" hy = " << qg.
hy() <<
" vx = " << (*maxvx) <<
" vy = " << (*maxvy) << std::endl;
195 std::cout <<
"t = " << t <<
" dt = " << state.
dt <<
" t + dt = " << t + state.
dt << std::endl;
200 p.
g2pd (vars, {
"dist"}, {
"DIST_DX"}, {
"DIST_DY"});
204 timer.tic(
"move partcles");
213 std::for_each (rng.
begin (), rng.
end (), state);
215 timer.toc(
"move partcles");
219 timer.tic(
"init_particle_mesh");
221 timer.toc(
"init_particle_mesh");
230 p.
p2g (vars, {
"M"}, {
"rho"},
true);
233 timer.tic(
"save particles");
235 const std::string ofilename =
"particle";
236 const std::string ofileext =
".csv";
237 const std::string numfile = std::string(
".") + std::to_string(nsave);
239 std::ofstream outbuf (ofilename + numfile + ofileext);
242 timer.toc(
"save particles");
245 timer.tic(
"save grid");
246 const std::string gfilename = std::string(
"grid.") + std::to_string(nsave) + std::string(
".vts");
248 timer.toc(
"save grid");
251 timer.print_report();
void vtk_export(const char *filename, const std::map< std::string, distributed_vector > &f) const
Functor class for moving particles.
std::vector< double > & vx
stepper(std::vector< double > &x_, std::vector< double > &y_, std::vector< double > &vx_, std::vector< double > &vy_, std::vector< double > &dist_, std::vector< double > &dist_dx_, std::vector< double > &dist_dy_, const double hx_, const double hy_, double dt_, double D_, std::function< double(void)> &noise_)
std::vector< double > & y
std::function< double(void)> normal
std::vector< double > & dist_dy
std::vector< double > & x
std::vector< double > & vy
std::vector< double > & dist_dx
std::vector< double > & dist
int main()
main implementing the time loop.
Class to represent particles embedded in a grid.
void p2g(std::map< std::string, device_vector_t< real_t > > &vars, bool apply_mass=false)
Map particle variables to the grid.
void g2pd(const std::map< std::string, device_vector_t< real_t > > &vars, GT const &gvarnames, PT const &pxvarnames, PT const &pyvarnames, bool apply_mass=false)
void g2p(const std::map< std::string, device_vector_t< real_t > > &vars, bool apply_mass=false)
idx_t num_particles
number of particles.
void init_particle_mesh()
Build grid/particles connectivity.
void print(std::ostream &os) const
Template for export function.
vector_t< real_t > x
x coordinate of particle positions.
std::map< std::string, vector_t< real_t > > dprops
double type quantities associated with the particles.
vector_t< real_t > y
y coordinate of particle positions.
void build_mass()
Construct a mass matrix.