quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
particles.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <iomanip>
3#include <iostream>
4#include <random>
5
6#include <counter.h>
7#include <quadgrid_cpp.h>
8#include <particles.h>
9#include <quadgrid_config.h>
10
13 static std::random_device rd;
14 static std::mt19937 gen (rd ());
15 static std::uniform_real_distribution<> dis (0.0, 1.0);
16 return dis (gen) * grid.num_cols () * grid.hx ();
17}
18
21 static std::random_device rd;
22 static std::mt19937 gen (rd ());
23 static std::uniform_real_distribution<> dis (0.0, 1.0);
24 return dis (gen) * grid.num_rows () * grid.hy ();
25}
26
28(
29 idx_t n, const vector_t<std::string>& ipropnames,
30 const vector_t<std::string>& dpropnames,
31 const quadgrid_t<vector_t<real_t>>& grid_
32 ) : particles_t (n, grid_) {
33
34 init_props (ipropnames, dpropnames);
35
37 (
38 [this] { return this->default_x_generator (); },
39 [this] { return this->default_y_generator (); }
40 );
41
43}
44
45
47(
48 idx_t n, const vector_t<std::string>& ipropnames,
49 const vector_t<std::string>& dpropnames,
50 const quadgrid_t<vector_t<real_t>>& grid_,
51 const vector_t<real_t> & xgen,
52 const vector_t<real_t> & ygen
53 ) : particles_t (n, grid_) {
54
55 x = xgen;
56 y = ygen;
57
58 init_props (ipropnames, dpropnames);
59
61}
62
64(
65 idx_t n, const vector_t<std::string>& ipropnames,
66 const vector_t<std::string>& dpropnames,
67 const quadgrid_t<vector_t<real_t>>& grid_,
68 std::function<real_t ()> xgen,
69 std::function<real_t ()> ygen
70 ) : particles_t (n, grid_) {
71
72 init_props (ipropnames, dpropnames);
73
74 init_particle_positions (xgen, ygen);
75
77}
78
79
80void
82(
83 const vector_t<std::string>& ipropnames,
84 const vector_t<std::string>& dpropnames
85 ) {
86
87 for (idx_t ii = 0; ii < ipropnames.size (); ++ii) {
88 iprops[ipropnames[ii]].assign (num_particles, 0);
89 }
90
91 for (idx_t ii = 0; ii < dpropnames.size (); ++ii) {
92 dprops[dpropnames[ii]].assign (num_particles, 0.0);
93 }
94}
95
96#ifdef USE_THRUST
97void
99 device_ptcl_to_grd = ptcl_to_grd;
100 device_grid_M = M;
101 device_x = x;
102 device_y = y;
103
104 for (const auto & p : dprops)
105 device_dprops[p.first] = p.second;
106
107 for (const auto & p : iprops)
108 device_iprops[p.first] = p.second;
109
110 std::cout<<"Memcpy Host to Device"<<std::endl;
111}
112
113void
115{
116 thrust::copy (device_x.cbegin (), device_x.cend (), x.begin ());
117 thrust::copy (device_y.cbegin (), device_y.cend (), y.begin ());
118 thrust::copy (device_grid_M.cbegin (), device_grid_M.cend (), M.begin ());
119 thrust::copy (device_ptcl_to_grd.cbegin(), device_ptcl_to_grd.cend(), ptcl_to_grd.begin());
120
121 for (const auto & p : device_dprops)
122 dprops[p.first] = p.second;
123
124 std::cout<<"Memcpy Device to Host"<<std::endl;
125}
126
127#endif
128
129
130void
132 static idx_t loop = 0;
133 ++loop;
134
135 for (auto & igrid : grd_to_ptcl)
136 igrid.second.clear ();
137
138 ptcl_to_grd.assign (this->num_particles, 0);
139
140 for (auto ii = 0; ii < this->num_particles; ++ii) {
141 idx_t c = static_cast<idx_t> (std::floor (x[ii] / grid.hx ()));
142 idx_t r = static_cast<idx_t> (std::floor (y[ii] / grid.hy ()));
143
144 grd_to_ptcl[grid.sub2gind (r, c)].push_back (ii);
145
146 }
147
149
150 /*
151 std::cout << "grd_to_ptcl" << "\n";
152
153 for (auto const & ii : grd_to_ptcl) {
154 for (auto const & jj : ii.second) {
155 std::cout << ii.first << " " << jj << "\n";
156 }
157 }
158
159 std::cout << "ptcl_to_grd" << "\n";
160 for (idx_t ii = 0; ii < this->num_particles; ++ii) {
161 std::cout << ptcl_to_grd[ii] << " " << ii << "\n";
162 }
163
164 assert (false);
165 */
166}
167
168/*
169void
170particles_t::mark_by_cell_color () {
171 ptcl_grd_color.assign (this->num_particles, particles_t::cell_color::red);
172 for (auto ii = 0; ii < this->num_particles; ++ii) {
173 idx_t c = static_cast<idx_t> (std::floor (x[ii] / grid.hx ()));
174 idx_t r = static_cast<idx_t> (std::floor (y[ii] / grid.hy ()));
175 if (c % 2 == 0) {
176 if (r % 2 == 0) {
177 ptcl_grd_color[ii] = particles_t::cell_color::red;
178 } else {
179 ptcl_grd_color[ii] = particles_t::cell_color::green;
180 }
181 } else {
182 if (r % 2 == 0) {
183 ptcl_grd_color[ii] = particles_t::cell_color::blue;
184 } else {
185 ptcl_grd_color[ii] = particles_t::cell_color::black;
186 }
187 }
188
189 }
190}
191*/
192
193void
195(
196 std::function<real_t ()> xgentr,
197 std::function<real_t ()> ygentr
198 ) {
199 x.resize (num_particles);
200 y.resize (num_particles);
201
202 std::generate (x.begin (), x.end (), xgentr);
203 std::generate (y.begin (), y.end (), ygentr);
204}
205
206
207void
209 M.assign (grid.num_global_nodes (), 0.0);
210 for (auto icell = grid.begin_cell_sweep ();
211 icell != grid.end_cell_sweep (); ++icell) {
212 for (auto inode = 0;
213 inode < quadgrid_t<vector_t<real_t>>::cell_t::nodes_per_cell;
214 ++inode) {
215 M[icell->gt (inode)] += (grid.hx () / 2.) * (grid.hy () / 2.);
216 }
217 }
218}
219
220template<>
221void
223 nlohmann::json j = *this;
224 os << j;
225}
226
227template<>
228void
230
231 os << "\"x\", " << "\"y\"";
232
233 for (auto const & ii : dprops)
234 os << ", \"" << ii.first << "\"";
235
236 for (auto const & ii : iprops)
237 os << ", \"" << ii.first << "\"";
238
239 os << std::endl;
240
241
242 for (idx_t jj = 0; jj < x.size (); ++jj) {
243
244 os << x[jj] << ", " << y[jj];
245
246 for (auto const & ii : dprops)
247 os << ", " << std::setprecision (16) << ii.second[jj];
248
249 for (auto const & ii : iprops)
250 os << ", " << ii.second[jj];
251
252 os << std::endl;
253 }
254}
255
256template<>
257void
259(std::ostream & os) const {
260
261 os << "# name: x" << std::endl
262 << "# type: matrix" << std::endl
263 << "# rows: 1" << std::endl
264 << "# columns: " << x.size () << std::endl;
265 for (auto const & kk : x) {
266 os << std::setprecision(16) << kk << " ";
267 }
268 os << std::endl;
269
270 os << "# name: y" << std::endl
271 << "# type: matrix" << std::endl
272 << "# rows: 1" << std::endl
273 << "# columns: " << y.size () << std::endl;
274 for (auto const & kk : y) {
275 os << std::setprecision(16) << kk << " ";
276 }
277 os << std::endl;
278
279 os << "# name: dprops" << std::endl
280 << "# type: scalar struct" << std::endl
281 << "# ndims: 2" << std::endl
282 << "1 1" << std::endl
283 << "# length: " << dprops.size () << std::endl;
284
285
286 for (auto const & ii : dprops) {
287 os << "# name: " << ii.first << std::endl
288 << "# type: matrix" << std::endl
289 << "# rows: 1" << std::endl
290 << "# columns: " << ii.second.size () << std::endl;
291 for (auto const & kk : ii.second) {
292 os << std::setprecision(16) << kk << " ";
293 }
294 os << std::endl;
295 }
296 os << std::endl;
297
298 os << "# name: iprops" << std::endl
299 << "# type: scalar struct" << std::endl
300 << "# ndims: 2" << std::endl
301 << "1 1" << std::endl
302 << "# length: " << iprops.size () << std::endl;
303
304 for (auto const & ii : iprops) {
305 os << "# name: " << ii.first << std::endl
306 << "# type: int64 matrix" << std::endl
307 << "# ndims: 2" << std::endl
308 << "1 " << ii.second.size () << std::endl;
309 for (auto const & kk : ii.second) {
310 os << kk << " ";
311 }
312 os << std::endl;
313 }
314 os << std::endl;
315}
316
317void
318to_json (nlohmann::json &j, const particles_t &p) {
319 j = nlohmann::json{
320 {"num_particles", p.num_particles},
321 {"x", p.x},
322 {"y", p.y},
323 {"dprops", p.dprops},
324 {"iprops", p.iprops},
325 {"grid_properties",
326 {{"nx", p.grid.num_cols ()},
327 {"ny", p.grid.num_rows ()},
328 {"hx", p.grid.hx ()},
329 {"hy", p.grid.hy ()}}
330 }
331 };
332}
333
334void
336
337 for (idx_t ii = 0; ii < num_particles - 1; ++ii) {
338 if (ii != ordering[ii]) {
339
340 for (auto &dprop : dprops) {
341 auto &col = dprop.second;
342 std::swap (col[ii], col[ordering[ii]]);
343 }
344
345 for (auto &iprop : iprops) {
346 auto &col = iprop.second;
347 std::swap (col[ii], col[ordering[ii]]);
348 }
349
350 std::swap (x[ii], x[ordering[ii]]);
351 std::swap (y[ii], y[ordering[ii]]);
352
353 for (int jj = ii; jj < ordering.size (); ++jj) {
354 if (ordering[jj] == ii) {
355 ordering[jj] = ordering[ii];
356 ordering[ii] = ii;
357 break;
358 }
359 }
360
361 }
362 }
363
364}
real_t hy() const
idx_t num_cols() const
idx_t num_global_nodes() const
HOST static DEVICE idx_t sub2gind(idx_t r, idx_t c, idx_t nr)
idx_t num_rows() const
cell_iterator begin_cell_sweep()
real_t hx() const
cell_iterator end_cell_sweep()
static constexpr morton_code_t M[]
Definition mmorton.cc:4
void to_json(nlohmann::json &j, const particles_t &p)
Adaptor to allow implicit conversion from particles_t to json.
double real_t
#define vector_t
Class to represent particles embedded in a grid.
Definition particles.h:29
void memcpy_host_to_device()
Copy Host To Device.
void init_particle_positions(std::function< real_t()> xgentr, std::function< real_t()> ygentr)
Initialize particle positions with generator functions.
idx_t num_particles
number of particles.
Definition particles.h:34
vector_t< idx_t > ptcl_to_grd
particles->grid connectivity.
Definition particles.h:46
void init_particle_mesh()
Build grid/particles connectivity.
std::map< std::string, vector_t< idx_t > > iprops
integer type quantities associated with the particles.
Definition particles.h:39
real_t default_x_generator()
The default generator function used to set up x-coordinates of particle positions if none is is speci...
Definition particles.cpp:12
void init_props(const vector_t< std::string > &ipropnames, const vector_t< std::string > &dpropnames)
Initialize particle properties.
Definition particles.cpp:82
quadgrid_t< vector_t< real_t > >::idx_t idx_t
datatype for indexing into vectors of properties
Definition particles.h:32
real_t default_y_generator()
The default generator function used to set up y-coordinates of particle positions if none is is speci...
Definition particles.cpp:20
void print(std::ostream &os) const
Template for export function.
Definition particles.h:109
vector_t< real_t > M
Mass matrix to be used for transfers if required.
Definition particles.h:44
std::map< idx_t, vector_t< idx_t > > grd_to_ptcl
grid->particles connectivity.
Definition particles.h:45
vector_t< real_t > x
x coordinate of particle positions.
Definition particles.h:35
const quadgrid_t< vector_t< real_t > > & grid
refernce to a grid object.
Definition particles.h:48
std::map< std::string, vector_t< real_t > > dprops
double type quantities associated with the particles.
Definition particles.h:42
vector_t< real_t > y
y coordinate of particle positions.
Definition particles.h:36
void build_mass()
Construct a mass matrix.
void reorder(vector_t< idx_t > &)
Reorder coordinates an properties according to the ordering vvector.
particles_t(idx_t n, const quadgrid_t< vector_t< real_t > > &grid_)
Simplest form of constructor.
Definition particles.h:118
void memcpy_device_to_host()
Copy Device To Host.
void update_ptcl_to_grd()
Updates theptcl_to_grd map only, without changing.