quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
particles_t Struct Reference

Class to represent particles embedded in a grid. More...

#include <particles.h>

Classes

class  update_ptcl_to_grd_host

Public Types

enum class  output_format : idx_t { csv = 0 , octave_ascii = 1 , json = 2 }
 Enumeration of available output format. More...
enum class  cell_color : idx_t {
  red = 0 , green = 1 , blue = 2 , black = 3 ,
  all_colors = 4
}
 Enumeration of cell colors. More...
using idx_t = quadgrid_t<vector_t<real_t>>::idx_t
 datatype for indexing into vectors of properties

Public Member Functions

real_t default_x_generator ()
 The default generator function used to set up x-coordinates of particle positions if none is is specified.
real_t default_y_generator ()
 The default generator function used to set up y-coordinates of particle positions if none is is specified.
template<output_format fmt>
void print (std::ostream &os) const
 Template for export function.
 particles_t (idx_t n, const quadgrid_t< vector_t< real_t > > &grid_)
 Simplest form of constructor.
 particles_t (const nlohmann::json &j, const quadgrid_t< vector_t< real_t > > &grid_)
 Ctor to import data from json.
 particles_t (idx_t n, const vector_t< std::string > &ipropnames, const vector_t< std::string > &dpropnames, const quadgrid_t< vector_t< real_t > > &grid_)
 Constructor with default position generators.
 particles_t (idx_t n, const vector_t< std::string > &ipropnames, const vector_t< std::string > &dpropnames, const quadgrid_t< vector_t< real_t > > &grid_, const vector_t< real_t > &xv, const vector_t< real_t > &yv)
 Constructor with custom position vectors.
 particles_t (idx_t n, const vector_t< std::string > &ipropnames, const vector_t< std::string > &dpropnames, const quadgrid_t< vector_t< real_t > > &grid_, std::function< real_t()> xgen, std::function< real_t()> ygen)
 Constructor with custom position generators.
void init_props (const vector_t< std::string > &ipropnames, const vector_t< std::string > &dpropnames)
 Initialize particle properties.
void remove_in_region (std::function< bool(real_t, real_t)> fun)
 Erase particcles based on coordinates.
void memcpy_host_to_device ()
 Copy Host To Device.
void memcpy_device_to_host ()
 Copy Device To Host.
void init_particle_mesh ()
 Build grid/particles connectivity.
template<class backend>
void update_ptcl_to_grd ()
 Updates theptcl_to_grd map only, without changing.
void mark_by_cell_color ()
 Mark particles by cell color.
void reorder (vector_t< idx_t > &)
 Reorder coordinates an properties according to the ordering vvector.
void init_particle_positions (std::function< real_t()> xgentr, std::function< real_t()> ygentr)
 Initialize particle positions with generator functions.
void build_mass ()
 Construct a mass matrix.
real_t & dp (const std::string &name, idx_t ii)
 shortcut for dprops.at (name) [ii]
const real_t & dp (const std::string &name, idx_t ii) const
 shortcut for dprops.at (name) [ii]
idx_t & ip (const std::string &name, idx_t ii)
 shortcut for iprops.at (name) [ii]
const idx_t & ip (const std::string &name, idx_t ii) const
 shortcut for iprops.at (name) [ii]
void p2g (std::map< std::string, device_vector_t< real_t > > &vars, bool apply_mass=false)
 Map particle variables to the grid.
template<typename GT, typename PT>
void p2g (std::map< std::string, device_vector_t< real_t > > &vars, PT const &pvarnames, GT const &gvarnames, bool apply_mass=false)
 Map particle variables to the grid.
template<typename str>
void p2g (std::map< std::string, device_vector_t< real_t > > &vars, std::initializer_list< str > const &pvarnames, std::initializer_list< str > const &gvarnames, bool apply_mass=false)
template<typename UnaryFunction>
void p2g (std::map< std::string, device_vector_t< real_t > > &vars, UnaryFunction helper)
template<typename GT, typename PT>
void p2g_print (std::map< std::string, vector_t< real_t > > &vars, PT const &pvarnames, GT const &gvarnames, bool apply_mass=false) const
template<typename str>
void p2g_print (std::map< std::string, vector_t< real_t > > &vars, std::initializer_list< str > const &pvarnames, std::initializer_list< str > const &gvarnames, bool apply_mass=false) const
template<typename GT, typename PT>
void p2gd (std::map< std::string, device_vector_t< real_t > > &vars, PT const &pxvarnames, PT const &pyvarnames, std::string const &area, GT const &gvarnames, bool apply_mass=false)
template<typename str>
void p2gd (std::map< std::string, device_vector_t< real_t > > &vars, std::initializer_list< str > const &pxvarnames, std::initializer_list< str > const &pyvarnames, std::string const &area, std::initializer_list< str > const &gvarnames, bool apply_mass=false)
template<typename UnaryFunction>
void p2gd (std::map< std::string, device_vector_t< real_t > > &vars, UnaryFunction helper)
void g2p (const std::map< std::string, device_vector_t< real_t > > &vars, bool apply_mass=false)
template<typename str>
void g2p (const std::map< std::string, device_vector_t< real_t > > &vars, std::initializer_list< str > const &gvarnames, std::initializer_list< str > const &pvarnames, bool apply_mass=false)
template<typename GT, typename PT>
void g2p (const std::map< std::string, device_vector_t< real_t > > &vars, GT const &gvarnames, PT const &pvarnames, bool apply_mass=false)
template<typename UnaryFunction>
void g2p (std::map< std::string, device_vector_t< real_t > > &vars, UnaryFunction helper)
template<typename GT, typename PT>
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)
template<typename str>
void g2pd (const std::map< std::string, device_vector_t< real_t > > &vars, std::initializer_list< str > const &gvarnames, std::initializer_list< str > const &pxvarnames, std::initializer_list< str > const &pyvarnames, bool apply_mass=false)
template<typename UnaryFunction>
void g2pd (std::map< std::string, device_vector_t< real_t > > &vars, UnaryFunction helper)

Static Public Member Functions

static const std::string & getkey (std::map< std::string, device_vector_t< real_t > > const &varnames, std::size_t ivar)
static const std::string & getkey (vector_t< std::string > const &varnames, std::size_t ivar)
static const char * getkey (std::initializer_list< const char * > const &varnames, std::size_t ivar)

Public Attributes

idx_t num_particles
 number of particles.
vector_t< real_t > x
 x coordinate of particle positions.
vector_t< real_t > y
 y coordinate of particle positions.
std::map< std::string, vector_t< idx_t > > iprops
 integer type quantities associated with the particles.
std::map< std::string, vector_t< real_t > > dprops
 double type quantities associated with the particles.
vector_t< real_t > M
 Mass matrix to be used for transfers if required.
std::map< idx_t, vector_t< idx_t > > grd_to_ptcl
 grid->particles connectivity.
vector_t< idx_t > ptcl_to_grd
 particles->grid connectivity.
vector_t< idx_t > ptcl_grd_color
 color of particle's cell.
const quadgrid_t< vector_t< real_t > > & grid
 refernce to a grid object.

Detailed Description

Class to represent particles embedded in a grid.

Offers methods for transfer of quantities from particles to grid or vice-versa (particles_t::p2g, g2p, p2gd, g2pd). Initial positions of the particles are chosen at random unless otherwise specified. To each particle a set one can associate a set of double and one of int which are stored in the std::map variables dprops and iprops, respectively. Can compute a (lumped) mass matrix to be used in the transfer functions. If particles are moved, the connectivity must be updated invoking the method init_particle_mesh ()

Definition at line 28 of file particles.h.

Member Typedef Documentation

◆ idx_t

datatype for indexing into vectors of properties

Definition at line 32 of file particles.h.

Member Enumeration Documentation

◆ cell_color

enum class particles_t::cell_color : idx_t
strong

Enumeration of cell colors.

Enumerator
red 
green 
blue 
black 
all_colors 

Definition at line 77 of file particles.h.

78 : idx_t {
79 red = 0,
80 green = 1,
81 blue = 2,
82 black = 3,
83 all_colors = 4
84 };

◆ output_format

enum class particles_t::output_format : idx_t
strong

Enumeration of available output format.

Enumerator
csv 

comma separated ascii file with headers, can be read by common spreadsheet apps or by Paraview or Octave.

octave_ascii 

GNU Octave ascii data format, can be loaded via the load command in GNU Octave.

json 

Definition at line 63 of file particles.h.

64 : idx_t {
65 csv = 0,
68
69 octave_ascii = 1,
71
72 json = 2
74 };

Constructor & Destructor Documentation

◆ particles_t() [1/5]

particles_t::particles_t ( idx_t n,
const quadgrid_t< vector_t< real_t > > & grid_ )
inline

Simplest form of constructor.

Particle positions are not assigned, they must be set manually later.

Parameters
nnumber of particles
grid_quadgrid_t object, sizes need to have been already set up.

Definition at line 118 of file particles.h.

119 : num_particles(n), grid(grid_) { }
idx_t num_particles
number of particles.
Definition particles.h:34
const quadgrid_t< vector_t< real_t > > & grid
refernce to a grid object.
Definition particles.h:48

◆ particles_t() [2/5]

particles_t::particles_t ( const nlohmann::json & j,
const quadgrid_t< vector_t< real_t > > & grid_ )
inline

Ctor to import data from json.

Grid data may be stored in the same json object but must be read separately before invoking this constructor.

Definition at line 125 of file particles.h.

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 }
#define vector_t
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
vector_t< real_t > x
x coordinate of particle positions.
Definition particles.h:35
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

◆ particles_t() [3/5]

particles_t::particles_t ( idx_t n,
const vector_t< std::string > & ipropnames,
const vector_t< std::string > & dpropnames,
const quadgrid_t< vector_t< real_t > > & grid_ )

Constructor with default position generators.

Distributes particles randomly over the grid.

Parameters
nnumber of particles
grid_quadgrid_t object, sizes need to have been already set up.
ipropnameskeys for entries in the particles_t::iprops map.
dpropnameskeys for entries in the particles_t::dprops map.

Definition at line 27 of file particles.cpp.

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}
void init_particle_positions(std::function< real_t()> xgentr, std::function< real_t()> ygentr)
Initialize particle positions with generator functions.
void init_particle_mesh()
Build grid/particles connectivity.
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
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
particles_t(idx_t n, const quadgrid_t< vector_t< real_t > > &grid_)
Simplest form of constructor.
Definition particles.h:118

◆ particles_t() [4/5]

particles_t::particles_t ( idx_t n,
const vector_t< std::string > & ipropnames,
const vector_t< std::string > & dpropnames,
const quadgrid_t< vector_t< real_t > > & grid_,
const vector_t< real_t > & xv,
const vector_t< real_t > & yv )

Constructor with custom position vectors.

Distributes particles based on the given vectors xv and yv. The vectors are copied, and left unchangend they can be deleted to reclaim memory if needed.

Parameters
nnumber of particles
grid_quadgrid_t object, sizes need to have been already set up.
ipropnameskeys for entries in the particles_t::iprops map.
dpropnameskeys for entries in the particles_t::dprops map.
xvthe x-coordinates of new particles.
yvthe y-coordinates of new particles.

Definition at line 46 of file particles.cpp.

53 : particles_t (n, grid_) {
54
55 x = xgen;
56 y = ygen;
57
58 init_props (ipropnames, dpropnames);
59
61}

◆ particles_t() [5/5]

particles_t::particles_t ( idx_t n,
const vector_t< std::string > & ipropnames,
const vector_t< std::string > & dpropnames,
const quadgrid_t< vector_t< real_t > > & grid_,
std::function< real_t()> xgen,
std::function< real_t()> ygen )

Constructor with custom position generators.

Distributes particles based on the given generator functions xgen and ygen. Each call to these functions should return the x- and y-coordinate of a new particle.

Parameters
nnumber of particles
grid_quadgrid_t object, sizes need to have been already set up.
ipropnameskeys for entries in the particles_t::iprops map.
dpropnameskeys for entries in the particles_t::dprops map.
xgengenerator function for the x-coordinates of new particles.
ygengenerator function for the y-coordinates of new particles.

Definition at line 63 of file particles.cpp.

70 : particles_t (n, grid_) {
71
72 init_props (ipropnames, dpropnames);
73
74 init_particle_positions (xgen, ygen);
75
77}

Member Function Documentation

◆ build_mass()

void particles_t::build_mass ( )

Construct a mass matrix.

Must be invoked manually before invoking any of the transfer methods with flag use_mass set to true

Definition at line 208 of file particles.cpp.

208 {
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}
real_t hy() const
idx_t num_global_nodes() const
cell_iterator begin_cell_sweep()
real_t hx() const
cell_iterator end_cell_sweep()
vector_t< real_t > M
Mass matrix to be used for transfers if required.
Definition particles.h:44

◆ default_x_generator()

real_t particles_t::default_x_generator ( )

The default generator function used to set up x-coordinates of particle positions if none is is specified.

Generates a uniform random distribution.

Definition at line 12 of file particles.cpp.

12 {
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}
idx_t num_cols() const

◆ default_y_generator()

real_t particles_t::default_y_generator ( )

The default generator function used to set up y-coordinates of particle positions if none is is specified.

Generates a uniform random distribution.

Definition at line 20 of file particles.cpp.

20 {
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}
idx_t num_rows() const

◆ dp() [1/2]

real_t & particles_t::dp ( const std::string & name,
idx_t ii )
inline

shortcut for dprops.at (name) [ii]

Definition at line 282 of file particles.h.

282 {
283 return dprops.at (name) [ii];
284 }

◆ dp() [2/2]

const real_t & particles_t::dp ( const std::string & name,
idx_t ii ) const
inline

shortcut for dprops.at (name) [ii]

Definition at line 288 of file particles.h.

288 {
289 return dprops.at (name) [ii];
290 }

◆ g2p() [1/4]

void particles_t::g2p ( const std::map< std::string, device_vector_t< real_t > > & vars,
bool apply_mass = false )
inline

Definition at line 401 of file particles.h.

402 {
403 g2p (vars, vars, vars, apply_mass);
404 }
void g2p(const std::map< std::string, device_vector_t< real_t > > &vars, bool apply_mass=false)
Definition particles.h:401

◆ g2p() [2/4]

template<typename GT, typename PT>
void particles_t::g2p ( const std::map< std::string, device_vector_t< real_t > > & vars,
GT const & gvarnames,
PT const & pvarnames,
bool apply_mass = false )

Definition at line 324 of file particles_imp.h.

328 {
329
331 //real_t N = 0.0, xx = 0.0, yy = 0.0;
332 //idx_t idx = 0;
333
334 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar) {
335
336 #ifdef USE_THRUST
337 auto & device_dprop = device_dprops.at(getkey(pvarnames, ivar));
338 auto const & device_gvar = device_grid_vars[getkey(gvarnames, ivar)];
339 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
340 g2p_helper_t helper(thrust::raw_pointer_cast(device_x.data()), thrust::raw_pointer_cast(device_y.data()), device_grid_M.cbegin(), device_gvar.cbegin(),
341 device_ptcl_to_grd.cbegin(), grid.num_rows (), grid.hx (), grid.hy (), thrust::raw_pointer_cast(device_dprop.data()), apply_mass);
342 #else
343 auto & dprop = dprops.at (getkey (pvarnames, ivar));
344 auto const & gvar = vars.at (getkey (gvarnames, ivar));
345 range<idx_t> rng (0, this->num_particles);
346 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
347 g2p_helper_t helper (x.begin (), y.begin (), M.cbegin(),
348 gvar.cbegin (), ptcl_to_grd.cbegin (),
349 grid.num_rows (), grid.hx (), grid.hy (),
350 dprop.begin (), apply_mass);
351 #endif
352
353 algorithm_namespace::for_each(device_exec_policy, first_p, last_p, helper);
354
355 }
356}
#define device_exec_policy
vector_t< idx_t > ptcl_to_grd
particles->grid connectivity.
Definition particles.h:46
static const std::string & getkey(std::map< std::string, device_vector_t< real_t > > const &varnames, std::size_t ivar)
Definition particles.h:306

◆ g2p() [3/4]

template<typename str>
void particles_t::g2p ( const std::map< std::string, device_vector_t< real_t > > & vars,
std::initializer_list< str > const & gvarnames,
std::initializer_list< str > const & pvarnames,
bool apply_mass = false )

Definition at line 266 of file particles_imp.h.

270 {
271 using strlist = std::initializer_list<str> const &;
272 g2p<strlist, strlist> (vars, gvarnames,
273 pvarnames, apply_mass);
274}

◆ g2p() [4/4]

template<typename UnaryFunction>
void particles_t::g2p ( std::map< std::string, device_vector_t< real_t > > & vars,
UnaryFunction helper )

Definition at line 361 of file particles_imp.h.

363 {
364
365 #ifdef USE_THRUST
366 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
367 #else
368 range<idx_t> rng (0, this->num_particles);
369 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
370 #endif
371
372 algorithm_namespace::for_each(device_exec_policy, first_p, last_p, helper);
373
374};

◆ g2pd() [1/3]

template<typename GT, typename PT>
void particles_t::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 )

Definition at line 446 of file particles_imp.h.

451 {
452
453 using idx_t = quadgrid_t<vector_t<real_t>>::idx_t;
454 //real_t Nx = 0.0, Ny = 0.0, xx = 0.0, yy = 0.0;
455
456 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar) {
457
458
459 // for (idx_t ip = 0; ip <= this->num_particles; ++ip) {
460 // xx = x[ip];
461 // yy = y[ip];
462 // auto icell = grid[ptcl_to_grd[ip]];
463 // for (idx_t inode = 0; inode < 4; ++inode) {
464 // Nx = apply_mass ?
465 // icell.shg(xx, yy, 0, inode) * M[icell.gt(inode)] :
466 // icell.shg(xx, yy, 0, inode);
467 // Ny = apply_mass ?
468 // icell.shg(xx, yy, 1, inode) * M[icell.gt(inode)] :
469 // icell.shg(xx, yy, 1, inode);
470 // dpropx[ip] += Nx * gvar[icell.gt(inode)];
471 // dpropy[ip] += Ny * gvar[icell.gt(inode)];
472 // }
473 // }
474
475
476
477 #ifdef USE_THRUST
478 auto const & device_gvar = device_grid_vars.at (getkey (gvarnames, ivar));
479 auto & device_dpropx = device_dprops.at (getkey (pxvarnames, ivar));
480 auto & device_dpropy = device_dprops.at (getkey (pyvarnames, ivar));
481 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
482 g2pd_helper_t helper(thrust::raw_pointer_cast(device_x.data()), thrust::raw_pointer_cast(device_y.data()), device_grid_M.cbegin(), device_gvar.cbegin(),
483 device_ptcl_to_grd.cbegin(), grid.num_rows (), grid.hx (), grid.hy (), thrust::raw_pointer_cast(device_dpropx.data()),
484 thrust::raw_pointer_cast(device_dpropy.data()), apply_mass);
485
486 #else
487 auto const & gvar = vars.at (getkey (gvarnames, ivar));
488 auto & dpropx = dprops.at (getkey (pxvarnames, ivar));
489 auto & dpropy = dprops.at (getkey (pyvarnames, ivar));
490
491 range<idx_t> rng (0, this->num_particles);
492 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
493 g2pd_helper_t helper (x.begin (), y.begin (), M.cbegin (),
494 gvar.cbegin (), ptcl_to_grd.cbegin (),
495 grid.num_rows (), grid.hx (), grid.hy (),
496 dpropx.begin (), dpropy.begin (), apply_mass);
497 #endif
498
499 algorithm_namespace::for_each(first_p, last_p, helper);
500
501 }
502}

◆ g2pd() [2/3]

template<typename str>
void particles_t::g2pd ( const std::map< std::string, device_vector_t< real_t > > & vars,
std::initializer_list< str > const & gvarnames,
std::initializer_list< str > const & pxvarnames,
std::initializer_list< str > const & pyvarnames,
bool apply_mass = false )

Definition at line 380 of file particles_imp.h.

385 {
386 using strlist = std::initializer_list<str> const &;
387 g2pd<strlist, strlist> (vars, gvarnames, pxvarnames,
388 pyvarnames, apply_mass);
389}
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)

◆ g2pd() [3/3]

template<typename UnaryFunction>
void particles_t::g2pd ( std::map< std::string, device_vector_t< real_t > > & vars,
UnaryFunction helper )

Definition at line 508 of file particles_imp.h.

510 {
511
512 #ifdef USE_THRUST
513 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
514 #else
515 range<idx_t> rng (0, this->num_particles);
516 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
517 #endif
518
519 algorithm_namespace::for_each(device_exec_policy, first_p, last_p, helper);
520 };

◆ getkey() [1/3]

const char * particles_t::getkey ( std::initializer_list< const char * > const & varnames,
std::size_t ivar )
inlinestatic

Definition at line 320 of file particles.h.

321 {
322 return *(std::next (varnames.begin (), ivar));
323 };

◆ getkey() [2/3]

const std::string & particles_t::getkey ( std::map< std::string, device_vector_t< real_t > > const & varnames,
std::size_t ivar )
inlinestatic

Definition at line 306 of file particles.h.

307 {
308 return std::next (varnames.begin (), ivar)->first;
309 };

◆ getkey() [3/3]

const std::string & particles_t::getkey ( vector_t< std::string > const & varnames,
std::size_t ivar )
inlinestatic

Definition at line 313 of file particles.h.

314 {
315 return varnames[ivar];
316 };

◆ init_particle_mesh()

void particles_t::init_particle_mesh ( )

Build grid/particles connectivity.

Builds/updates the grd_to_ptcl and ptcl_to_grd maps. Must be used whenever particles cross cell boundaries.

Definition at line 131 of file particles.cpp.

131 {
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}
HOST static DEVICE idx_t sub2gind(idx_t r, idx_t c, idx_t nr)
std::map< idx_t, vector_t< idx_t > > grd_to_ptcl
grid->particles connectivity.
Definition particles.h:45
void update_ptcl_to_grd()
Updates theptcl_to_grd map only, without changing.

◆ init_particle_positions()

void particles_t::init_particle_positions ( std::function< real_t()> xgentr,
std::function< real_t()> ygentr )

Initialize particle positions with generator functions.

Invoked automatically if the generators are passed to the CTOR, must be invoked manually otherwise.

Definition at line 194 of file particles.cpp.

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}

◆ init_props()

void particles_t::init_props ( const vector_t< std::string > & ipropnames,
const vector_t< std::string > & dpropnames )

Initialize particle properties.

Allocates vectors to store particle properties, this is invoked automatically if the CTOR is invoked specifying property names, must be invoked manually otherwise.

Parameters
ipropnameskeys for entries in the particles_t::iprops map.
dpropnameskeys for entries in the particles_t::dprops map.

Definition at line 81 of file particles.cpp.

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}

◆ ip() [1/2]

idx_t & particles_t::ip ( const std::string & name,
idx_t ii )
inline

shortcut for iprops.at (name) [ii]

Definition at line 294 of file particles.h.

294 {
295 return iprops.at (name) [ii];
296 }

◆ ip() [2/2]

const idx_t & particles_t::ip ( const std::string & name,
idx_t ii ) const
inline

shortcut for iprops.at (name) [ii]

Definition at line 300 of file particles.h.

300 {
301 return iprops.at (name) [ii];
302 }

◆ mark_by_cell_color()

void particles_t::mark_by_cell_color ( )

Mark particles by cell color.

◆ memcpy_device_to_host()

void particles_t::memcpy_device_to_host ( )

Copy Device To Host.

◆ memcpy_host_to_device()

void particles_t::memcpy_host_to_device ( )

Copy Host To Device.

◆ p2g() [1/4]

void particles_t::p2g ( std::map< std::string, device_vector_t< real_t > > & vars,
bool apply_mass = false )
inline

Map particle variables to the grid.

Assume all fields of vars are to be mapped, and use the same field names for particle and grid variables.

Definition at line 331 of file particles.h.

332 {
333 p2g (vars, vars, vars, apply_mass);
334 }
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

◆ p2g() [2/4]

template<typename GT, typename PT>
void particles_t::p2g ( std::map< std::string, device_vector_t< real_t > > & vars,
PT const & pvarnames,
GT const & gvarnames,
bool apply_mass = false )

Map particle variables to the grid.

Choose which quantities need to be mapped according to the strings in gvarnames, and use the same field names for particle and grid variables.

Definition at line 65 of file particles_imp.h.

69 {
70
71 using idx_t = quadgrid_t<device_vector_t<real_t>>::idx_t;
72 //real_t N = 0.0, xx = 0.0, yy = 0.0;
73 //idx_t idx = 0;
74
75
76 for (std::size_t ivar = 0; ivar < std::size(gvarnames); ++ivar) {
77
78 #ifdef USE_THRUST
79 auto & device_gvar = device_grid_vars[getkey(gvarnames, ivar)];
80 auto const & device_dprop = device_dprops.at(getkey(pvarnames, ivar));
81 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
82 p2g_helper_t helper(device_x.cbegin(), device_y.cbegin(), thrust::raw_pointer_cast(device_grid_M.data()),
83 thrust::raw_pointer_cast(device_gvar.data()), device_ptcl_to_grd.cbegin(), grid.num_rows(), grid.hx(),
84 grid.hy(), device_dprop.cbegin(), apply_mass);
85 #else
86 auto & gvar = vars[getkey(gvarnames, ivar)];
87 auto const & dprop = dprops.at (getkey(pvarnames, ivar));
88 range<idx_t> rng (0, this->num_particles);
89 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
90 p2g_helper_t helper(x.cbegin(), y.cbegin(), M.begin(), gvar.begin(), ptcl_to_grd.cbegin(), grid.num_rows(), grid.hx(), grid.hy(), dprop.cbegin(), apply_mass);
91 #endif
92
93 algorithm_namespace::for_each(device_exec_policy, first_p, last_p, helper);
94
95 }
96
97}

◆ p2g() [3/4]

template<typename str>
void particles_t::p2g ( std::map< std::string, device_vector_t< real_t > > & vars,
std::initializer_list< str > const & pvarnames,
std::initializer_list< str > const & gvarnames,
bool apply_mass = false )

Definition at line 7 of file particles_imp.h.

11 {
12 using strlist = std::initializer_list<str> const &;
14 (vars, pvarnames, gvarnames, apply_mass);
15}

◆ p2g() [4/4]

template<typename UnaryFunction>
void particles_t::p2g ( std::map< std::string, device_vector_t< real_t > > & vars,
UnaryFunction helper )

Definition at line 102 of file particles_imp.h.

104 {
105
106 #ifdef USE_THRUST
107 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
108 #else
109 range<idx_t> rng (0, this->num_particles);
110 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
111 #endif
112
113 algorithm_namespace::for_each(device_exec_policy, first_p, last_p, helper);
114
115};

◆ p2g_print() [1/2]

template<typename GT, typename PT>
void particles_t::p2g_print ( std::map< std::string, vector_t< real_t > > & vars,
PT const & pvarnames,
GT const & gvarnames,
bool apply_mass = false ) const

Definition at line 580 of file particles_imp.h.

584 {
585
586 using idx_t = quadgrid_t<std::vector<double>>::idx_t;
587 double N = 0.0, xx = 0.0, yy = 0.0;
588
589
590 for (std::size_t ivar = 0; ivar < std::size(gvarnames); ++ivar) {
591 auto & gvar = vars[getkey(gvarnames, ivar)];
592 auto const & dprop = dprops.at (getkey(pvarnames, ivar));
593
594 for (idx_t ip = 0; ip <= this->num_particles; ++ip) {
595 xx = x[ip];
596 yy = y[ip];
597 auto icell = grid[ptcl_to_grd[ip]];
598 for (idx_t inode = 0; inode < 4; ++inode) {
599 N = icell.shp(xx, yy, inode) * dprop[ip];
600 gvar[icell.gt(inode)] += N;
601 }
602 }
603 }
604
605 if (apply_mass)
606 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar)
607 for (idx_t ii = 0; ii < M.size (); ++ii) {
608 vars[getkey(gvarnames, ivar)][ii] /= M[ii];
609 }
610}
idx_t & ip(const std::string &name, idx_t ii)
shortcut for iprops.at (name) [ii]
Definition particles.h:294

◆ p2g_print() [2/2]

template<typename str>
void particles_t::p2g_print ( std::map< std::string, vector_t< real_t > > & vars,
std::initializer_list< str > const & pvarnames,
std::initializer_list< str > const & gvarnames,
bool apply_mass = false ) const

Definition at line 568 of file particles_imp.h.

572 {
573 using strlist = std::initializer_list<str> const &;
575 (vars, pvarnames, gvarnames, apply_mass);
576}
void p2g_print(std::map< std::string, vector_t< real_t > > &vars, PT const &pvarnames, GT const &gvarnames, bool apply_mass=false) const

◆ p2gd() [1/3]

template<typename GT, typename PT>
void particles_t::p2gd ( std::map< std::string, device_vector_t< real_t > > & vars,
PT const & pxvarnames,
PT const & pyvarnames,
std::string const & area,
GT const & gvarnames,
bool apply_mass = false )

Definition at line 185 of file particles_imp.h.

191 {
192
193 using idx_t = quadgrid_t<device_vector_t<real_t>>::idx_t;
194 //real_t xx = 0.0, yy = 0.0, Nx = 0.0, Ny = 0.0;
195
196 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar) {
197
198 #ifdef USE_THRUST
199 auto & device_gvar = device_grid_vars[getkey(gvarnames, ivar)];
200 auto const & device_dpropx = device_dprops.at (getkey(pxvarnames, ivar));
201 auto const & device_dpropy = device_dprops.at (getkey(pyvarnames, ivar));
202 auto const & device_dproparea = device_dprops.at (area);
203
204 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
205
206 p2gd_helper_t helper(device_x.cbegin(), device_y.cbegin(), thrust::raw_pointer_cast(device_grid_M.data()),
207 thrust::raw_pointer_cast(device_gvar.data()), device_ptcl_to_grd.cbegin(), grid.num_rows(), grid.hx(),
208 grid.hy(), device_dpropx.cbegin(), device_dpropy.cbegin(), device_dproparea.cbegin(), apply_mass);
209
210 #else
211 auto & gvar = vars[getkey(gvarnames, ivar)];
212 auto const & dpropx = dprops.at (getkey(pxvarnames, ivar));
213 auto const & dpropy = dprops.at (getkey(pyvarnames, ivar));
214 auto const & dproparea = dprops.at (area);
215
216 range<idx_t> rng (0, this->num_particles);
217 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
218 p2gd_helper_t helper(x.cbegin(), y.cbegin(), M.cbegin(), gvar.begin(), ptcl_to_grd.cbegin(), grid.num_rows(), grid.hx(), grid.hy(),
219 dpropx.begin(), dpropy.begin(), dproparea.begin(), apply_mass);
220 #endif
221
222 algorithm_namespace::for_each(first_p, last_p, helper);
223
224 /*for (idx_t ip = 0; ip <= this->num_particles; ++ip) {
225 xx = x[ip];
226 yy = y[ip];
227 auto icell = grid[ptcl_to_grd[ip]];
228 for (idx_t inode = 0; inode < 4; ++inode) {
229 Nx = icell.shg (xx, yy, 0, inode);
230 Ny = icell.shg (xx, yy, 1, inode);
231 gvar[icell.gt(inode)] += (Nx * dpropx[ip] + Ny * dpropy[ip]) * dproparea[ip];
232 }
233 }*/
234
235 }
236
237 /*if (apply_mass)
238 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar)
239 for (idx_t ii = 0; ii < M.size (); ++ii) {
240 vars[getkey(gvarnames, ivar)][ii] /= M[ii];
241 }*/
242
243}

◆ p2gd() [2/3]

template<typename str>
void particles_t::p2gd ( std::map< std::string, device_vector_t< real_t > > & vars,
std::initializer_list< str > const & pxvarnames,
std::initializer_list< str > const & pyvarnames,
std::string const & area,
std::initializer_list< str > const & gvarnames,
bool apply_mass = false )

Definition at line 120 of file particles_imp.h.

126 {
127 using strlist = std::initializer_list<str> const &;
129 (vars, pxvarnames, pyvarnames, area, gvarnames, apply_mass);
130}
void p2gd(std::map< std::string, device_vector_t< real_t > > &vars, PT const &pxvarnames, PT const &pyvarnames, std::string const &area, GT const &gvarnames, bool apply_mass=false)

◆ p2gd() [3/3]

template<typename UnaryFunction>
void particles_t::p2gd ( std::map< std::string, device_vector_t< real_t > > & vars,
UnaryFunction helper )

Definition at line 248 of file particles_imp.h.

250 {
251
252 #ifdef USE_THRUST
253 thrust::counting_iterator<idx_t> first_p(0), last_p(this -> num_particles);
254 #else
255 range<idx_t> rng (0, this->num_particles);
256 range<idx_t>::iterator first_p = rng.begin(), last_p = rng.end();
257 #endif
258
259 algorithm_namespace::for_each(device_exec_policy, first_p, last_p, helper);
260
261 };

◆ print()

template<output_format fmt>
void particles_t::print ( std::ostream & os) const
inline

Template for export function.

If a format is not specified, just outputs an error message.

Definition at line 109 of file particles.h.

109 {
110 os << "output format not implementd" << std::endl;
111 }

◆ remove_in_region()

void particles_t::remove_in_region ( std::function< bool(real_t, real_t)> fun)
inline

Erase particcles based on coordinates.

Given a function to decide whether a particle lies inside a region or not, remove all particles for which the function returns true, and also erase corresponding entries in dprops and iprops.

Definition at line 200 of file particles.h.

200 {
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 };

◆ reorder()

void particles_t::reorder ( vector_t< idx_t > & ordering)

Reorder coordinates an properties according to the ordering vvector.

Definition at line 335 of file particles.cpp.

335 {
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}

◆ update_ptcl_to_grd()

template<class backend>
void particles_t::update_ptcl_to_grd ( )

Updates theptcl_to_grd map only, without changing.

Definition at line 561 of file particles_imp.h.

561 {
562 backend::update(*this);
563};

Member Data Documentation

◆ dprops

std::map<std::string, vector_t<real_t> > particles_t::dprops

double type quantities associated with the particles.

Definition at line 42 of file particles.h.

◆ grd_to_ptcl

std::map<idx_t, vector_t<idx_t> > particles_t::grd_to_ptcl

grid->particles connectivity.

Definition at line 45 of file particles.h.

◆ grid

const quadgrid_t<vector_t<real_t> >& particles_t::grid

refernce to a grid object.

Definition at line 48 of file particles.h.

◆ iprops

std::map<std::string, vector_t<idx_t> > particles_t::iprops

integer type quantities associated with the particles.

Definition at line 39 of file particles.h.

◆ M

vector_t<real_t> particles_t::M

Mass matrix to be used for transfers if required.

Definition at line 44 of file particles.h.

◆ num_particles

idx_t particles_t::num_particles

number of particles.

Definition at line 34 of file particles.h.

◆ ptcl_grd_color

vector_t<idx_t> particles_t::ptcl_grd_color

color of particle's cell.

Definition at line 47 of file particles.h.

◆ ptcl_to_grd

vector_t<idx_t> particles_t::ptcl_to_grd

particles->grid connectivity.

Definition at line 46 of file particles.h.

◆ x

vector_t<real_t> particles_t::x

x coordinate of particle positions.

Definition at line 35 of file particles.h.

◆ y

vector_t<real_t> particles_t::y

y coordinate of particle positions.

Definition at line 36 of file particles.h.


The documentation for this struct was generated from the following files: