quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
particles.h
Go to the documentation of this file.
1#ifndef PARTICLES_H
2#define PARTICLES_H
3
4#include <algorithm>
5#include <functional>
6#include <iomanip>
7#include <iostream>
8#include <quadgrid_config.h>
9#include <json.hpp>
10#include <map>
11#include <quadgrid_cpp.h>
12#include <string>
13
15
27
28struct
30
33
37
39 std::map<std::string, vector_t<idx_t>> iprops;
40
42 std::map<std::string, vector_t<real_t>> dprops;
43
45 std::map<idx_t, vector_t<idx_t>> grd_to_ptcl;
49
50 #ifdef USE_THRUST
51 device_vector_t<idx_t> device_ptcl_to_grd;
52 device_vector_t<real_t> device_x, device_y;
53
54 std::map<std::string, device_vector_t<real_t>> device_dprops;
55 std::map<std::string, device_vector_t<idx_t>> device_iprops;
56
57 device_vector_t<real_t> device_grid_M; //mass matrix
58 std::map<std::string, device_vector_t<real_t>> device_grid_vars;
59
60 #endif
61
63 enum class
65 csv = 0,
68
69 octave_ascii = 1,
71
72 json = 2
74 };
75
77 enum class
79 red = 0,
80 green = 1,
81 blue = 2,
82 black = 3,
83 all_colors = 4
84 };
85
86
90
92 real_t
93 default_x_generator ();
94
98
100 real_t
101 default_y_generator ();
102
104
107 template<output_format fmt>
108 void
109 print (std::ostream & os) const {
110 os << "output format not implementd" << std::endl;
111 }
112
114
119 : num_particles(n), grid(grid_) { }
120
122
125 particles_t (const nlohmann::json &j,
126 const quadgrid_t<vector_t<real_t>>& grid_)
127 : grid(grid_)
128 {
129 j["dprops"].get_to<std::map<std::string, vector_t<real_t>>> (dprops);
130 j["iprops"].get_to<std::map<std::string, vector_t<int>>> (iprops);
131 j["x"].get_to<vector_t<real_t>> (x);
132 j["y"].get_to<vector_t<real_t>> (y);
133 j["num_particles"].get_to<idx_t> (num_particles);
134 }
135
137
144 particles_t (idx_t n, const vector_t<std::string>& ipropnames,
145 const vector_t<std::string>& dpropnames,
146 const quadgrid_t<vector_t<real_t>>& grid_);
147
149
159 particles_t (idx_t n, const vector_t<std::string>& ipropnames,
160 const vector_t<std::string>& dpropnames,
161 const quadgrid_t<vector_t<real_t>>& grid_,
162 const vector_t<real_t> & xv,
163 const vector_t<real_t> & yv);
164
166
176 particles_t (idx_t n, const vector_t<std::string>& ipropnames,
177 const vector_t<std::string>& dpropnames,
178 const quadgrid_t<vector_t<real_t>>& grid_,
179 std::function<real_t ()> xgen,
180 std::function<real_t ()> ygen);
181
183
189 void
190 init_props (const vector_t<std::string>& ipropnames,
191 const vector_t<std::string>& dpropnames);
192
194
199 void
200 remove_in_region (std::function<bool (real_t, real_t)> fun) {
201 vector_t<idx_t> vin{};
202 for (idx_t i = 0; i < num_particles; ++i) {
203 if (fun (x[i], y[i])) {
204 vin.push_back (i);
205 }
206 }
207
208 for (auto &dprop : dprops) {
209 for (auto id = vin.rbegin (); id != vin.rend (); ++id) {
210 dprop.second.erase (dprop.second.begin () + (*id));
211 }
212 }
213
214 for (auto &iprop : iprops) {
215 for (auto id = vin.rbegin (); id != vin.rend (); ++id) {
216 iprop.second.erase (iprop.second.begin () + (*id));
217 }
218 }
219
220 for (auto id = vin.rbegin (); id != vin.rend (); ++id) {
221 x.erase (x.begin () + (*id));
222 y.erase (y.begin () + (*id));
224 }
225 };
226
228 void
230
232 void
234
236
239 void
241
243 // `grd_to_ptcl`, if not needed.
244 // backend is the policy
245 template<class backend>
246 void
248
249 //policies for update_ptcl_to_grd()
250 class
252
253 class
254 update_ptcl_to_grd_device;
255
257
258 void
260
262 void
264
266
269 void
270 init_particle_positions (std::function<real_t ()> xgentr,
271 std::function<real_t ()> ygentr);
272
274
277 void
278 build_mass ();
279
281 real_t &
282 dp (const std::string & name, idx_t ii) {
283 return dprops.at (name) [ii];
284 }
285
287 const real_t &
288 dp (const std::string & name, idx_t ii) const {
289 return dprops.at (name) [ii];
290 }
291
293 idx_t &
294 ip (const std::string & name, idx_t ii) {
295 return iprops.at (name) [ii];
296 }
297
299 const idx_t &
300 ip (const std::string & name, idx_t ii) const {
301 return iprops.at (name) [ii];
302 }
303
304 static
305 const std::string &
306 getkey(std::map<std::string, device_vector_t<real_t>> const &varnames,
307 std::size_t ivar) {
308 return std::next (varnames.begin (), ivar)->first;
309 };
310
311 static
312 const std::string &
314 std::size_t ivar) {
315 return varnames[ivar];
316 };
317
318 static
319 const char*
320 getkey(std::initializer_list<const char *> const &varnames,
321 std::size_t ivar) {
322 return *(std::next (varnames.begin (), ivar));
323 };
324
326
330 void
331 p2g (std::map<std::string, device_vector_t<real_t>> & vars,
332 bool apply_mass = false) {
333 p2g (vars, vars, vars, apply_mass);
334 }
335
337
342 template<typename GT, typename PT>
343 void
344 p2g (std::map<std::string, device_vector_t<real_t>> & vars,
345 PT const & pvarnames,
346 GT const & gvarnames,
347 bool apply_mass = false) ;
348
349 template<typename str>
350 void
351 p2g (std::map<std::string, device_vector_t<real_t>> & vars,
352 std::initializer_list<str> const & pvarnames,
353 std::initializer_list<str> const & gvarnames,
354 bool apply_mass = false) ;
355
356 //p2g with custom helper
357 template<typename UnaryFunction>
358 void
359 p2g (std::map<std::string, device_vector_t<real_t>> & vars,
360 UnaryFunction helper);
361
362 template<typename GT, typename PT>
363 void
364 p2g_print (std::map<std::string, vector_t<real_t>> & vars,
365 PT const & pvarnames,
366 GT const & gvarnames,
367 bool apply_mass = false) const;
368
369 template<typename str>
370 void
371 p2g_print (std::map<std::string, vector_t<real_t>> & vars,
372 std::initializer_list<str> const & pvarnames,
373 std::initializer_list<str> const & gvarnames,
374 bool apply_mass = false) const;
375
376 template<typename GT, typename PT>
377 void
378 p2gd (std::map<std::string, device_vector_t<real_t>> & vars,
379 PT const & pxvarnames,
380 PT const & pyvarnames,
381 std::string const &area,
382 GT const & gvarnames,
383 bool apply_mass = false);
384
385 template<typename str>
386 void
387 p2gd (std::map<std::string, device_vector_t<real_t>> & vars,
388 std::initializer_list<str> const & pxvarnames,
389 std::initializer_list<str> const & pyvarnames,
390 std::string const & area,
391 std::initializer_list<str> const & gvarnames,
392 bool apply_mass = false);
393
394 //p2gd with custom helper
395 template<typename UnaryFunction>
396 void
397 p2gd (std::map<std::string, device_vector_t<real_t>> & vars,
398 UnaryFunction helper);
399
400 void
401 g2p (const std::map<std::string, device_vector_t<real_t>>& vars,
402 bool apply_mass = false) {
403 g2p (vars, vars, vars, apply_mass);
404 }
405
406 template<typename str>
407 void
408 g2p (const std::map<std::string, device_vector_t<real_t>>& vars,
409 std::initializer_list<str> const & gvarnames,
410 std::initializer_list<str> const & pvarnames,
411 bool apply_mass = false);
412
413 template<typename GT, typename PT>
414 void
415 g2p (const std::map<std::string, device_vector_t<real_t>>& vars,
416 GT const & gvarnames,
417 PT const & pvarnames,
418 bool apply_mass = false);
419
420 //g2p with custom helper
421 template<typename UnaryFunction>
422 void
423 g2p (std::map<std::string, device_vector_t<real_t>> & vars,
424 UnaryFunction helper);
425
426 template<typename GT, typename PT>
427 void
428 g2pd (const std::map<std::string, device_vector_t<real_t>>& vars,
429 GT const & gvarnames,
430 PT const & pxvarnames,
431 PT const & pyvarnames,
432 bool apply_mass = false);
433
434 template<typename str>
435 void
436 g2pd (const std::map<std::string, device_vector_t<real_t>>& vars,
437 std::initializer_list<str> const & gvarnames,
438 std::initializer_list<str> const &pxvarnames,
439 std::initializer_list<str> const & pyvarnames,
440 bool apply_mass = false);
441
442 //g2pd with custom helper
443 template<typename UnaryFunction>
444 void
445 g2pd (std::map<std::string, device_vector_t<real_t>> & vars,
446 UnaryFunction helper);
447
448};
449
452void
453to_json (nlohmann::json &j, const particles_t &p);
454
457template<typename P2G_t, typename COORD_t>
458class
460
463 const COORD_t x;
464 const COORD_t y;
465 const real_t hx;
466 const real_t hy;
468
469public :
470 ptcl_to_grd_update_t (P2G_t ptcl_to_grd_,
471 const COORD_t x_, const COORD_t y_,
472 const real_t hx_, const real_t hy_, const idx_t nrows_)
473 : ptcl_to_grd(ptcl_to_grd_), x(x_), y(y_), hx(hx_), hy(hy_), nrows(nrows_) {};
474
475 HOST DEVICE
476 void operator() (particles_t::idx_t ii) {
477 ptcl_to_grd[ii] = quadgrid_t<COORD_t>::sub2gind (static_cast<idx_t> (std::floor (y[ii] / hy)),
478 static_cast<idx_t> (std::floor (x[ii] / hx)),
479 nrows);
480 }
481};
482
483
484#include "particles_imp.h"
485
486#endif /* PARTICLES_H */
const real_t hx
Definition particles.h:465
const idx_t nrows
Definition particles.h:467
ptcl_to_grd_update_t(P2G_t ptcl_to_grd_, const COORD_t x_, const COORD_t y_, const real_t hx_, const real_t hy_, const idx_t nrows_)
Definition particles.h:470
const COORD_t x
Definition particles.h:463
particles_t::idx_t idx_t
Definition particles.h:461
const COORD_t y
Definition particles.h:464
const real_t hy
Definition particles.h:466
HOST static DEVICE idx_t sub2gind(idx_t r, idx_t c, idx_t nr)
void to_json(nlohmann::json &j, const particles_t &p)
Adaptor to allow implicit conversion from particles_t to json.
#define DEVICE
double real_t
#define HOST
#define vector_t
#define device_vector_t
Class to represent particles embedded in a grid.
Definition particles.h:29
void p2g(std::map< std::string, device_vector_t< real_t > > &vars, bool apply_mass=false)
Map particle variables to the grid.
Definition particles.h:331
const idx_t & ip(const std::string &name, idx_t ii) const
shortcut for iprops.at (name) [ii]
Definition particles.h:300
static const std::string & getkey(vector_t< std::string > const &varnames, std::size_t ivar)
Definition particles.h:313
idx_t & ip(const std::string &name, idx_t ii)
shortcut for iprops.at (name) [ii]
Definition particles.h:294
void memcpy_host_to_device()
Copy Host To Device.
real_t & dp(const std::string &name, idx_t ii)
shortcut for dprops.at (name) [ii]
Definition particles.h:282
void init_particle_positions(std::function< real_t()> xgentr, std::function< real_t()> ygentr)
Initialize particle positions with generator functions.
void g2p(const std::map< std::string, device_vector_t< real_t > > &vars, bool apply_mass=false)
Definition particles.h:401
void mark_by_cell_color()
Mark particles by cell color.
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
const real_t & dp(const std::string &name, idx_t ii) const
shortcut for dprops.at (name) [ii]
Definition particles.h:288
void init_particle_mesh()
Build grid/particles connectivity.
particles_t(const nlohmann::json &j, const quadgrid_t< vector_t< real_t > > &grid_)
Ctor to import data from json.
Definition particles.h:125
vector_t< idx_t > ptcl_grd_color
color of particle's cell.
Definition particles.h:47
std::map< std::string, vector_t< idx_t > > iprops
integer type quantities associated with the particles.
Definition particles.h:39
quadgrid_t< vector_t< real_t > >::idx_t idx_t
datatype for indexing into vectors of properties
Definition particles.h:32
cell_color
Enumeration of cell colors.
Definition particles.h:78
void remove_in_region(std::function< bool(real_t, real_t)> fun)
Erase particcles based on coordinates.
Definition particles.h:200
output_format
Enumeration of available output format.
Definition particles.h:64
static const char * getkey(std::initializer_list< const char * > const &varnames, std::size_t ivar)
Definition particles.h:320
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
static const std::string & getkey(std::map< std::string, device_vector_t< real_t > > const &varnames, std::size_t ivar)
Definition particles.h:306
void memcpy_device_to_host()
Copy Device To Host.
void update_ptcl_to_grd()
Updates theptcl_to_grd map only, without changing.