quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
particles_imp.h
Go to the documentation of this file.
1#ifndef PARTICLES_IMP_H
2#define PARTICLES_IMP_H
3#include "counter.h"
4
5template<typename str>
6void
8(std::map<std::string, std::vector<double>> & vars,
9 std::initializer_list<str> const & pvarnames,
10 std::initializer_list<str> const & gvarnames,
11 bool apply_mass) const {
12 using strlist = std::initializer_list<str> const &;
14 (vars, pvarnames, gvarnames, apply_mass);
15}
16
17template<typename GT, typename PT>
18void
20(std::map<std::string, std::vector<double>> & vars,
21 PT const & pvarnames,
22 GT const & gvarnames,
23 bool apply_mass) const {
24
26 double N = 0.0, xx = 0.0, yy = 0.0;
27 idx_t idx = 0;
28
29
30 for (std::size_t ivar = 0; ivar < std::size(gvarnames); ++ivar) {
31 auto & gvar = vars[getkey(gvarnames, ivar)];
32 auto const & dprop = dprops.at (getkey(pvarnames, ivar));
33
34 for (idx_t ip = 0; ip <= this->num_particles; ++ip) {
35 xx = x[ip];
36 yy = y[ip];
37 auto icell = grid[ptcl_to_grd[ip]];
38 for (idx_t inode = 0; inode < 4; ++inode) {
39 N = icell.shp(xx, yy, inode) * dprop[ip];
40 gvar[icell.gt(inode)] += N;
41 }
42 }
43
44
45 }
46
47#pragma omp barrier
48 if (apply_mass)
49 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar)
50#pragma omp for
51 for (idx_t ii = 0; ii < M.size (); ++ii) {
52 vars[getkey(gvarnames, ivar)][ii] /= M[ii];
53 }
54}
55
56
57template<typename str>
58void
60(std::map<std::string, std::vector<double>> & vars,
61 std::initializer_list<str> const & pxvarnames,
62 std::initializer_list<str> const & pyvarnames,
63 std::string const &area,
64 std::initializer_list<str> const & gvarnames,
65 bool apply_mass) const {
66 using strlist = std::initializer_list<str> const &;
68 (vars, pxvarnames, pyvarnames, area, gvarnames, apply_mass);
69}
70
71
72template<typename GT, typename PT>
73void
75(std::map<std::string, std::vector<double>> & vars,
76 PT const & pxvarnames,
77 PT const & pyvarnames,
78 std::string const &area,
79 GT const & gvarnames,
80 bool apply_mass) const {
81
83 double xx = 0.0, yy = 0.0, Nx = 0.0, Ny = 0.0;
84
85 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar) {
86 auto & gvar = vars[getkey(gvarnames, ivar)];
87 auto const & dpropx = dprops.at (getkey(pxvarnames, ivar));
88 auto const & dpropy = dprops.at (getkey(pyvarnames, ivar));
89 auto const & dproparea = dprops.at (area);
90
91 for (idx_t ip = 0; ip <= this->num_particles; ++ip) {
92 xx = x[ip];
93 yy = y[ip];
94 auto icell = grid[ptcl_to_grd[ip]];
95 for (idx_t inode = 0; inode < 4; ++inode) {
96 Nx = icell.shg (xx, yy, 0, inode);
97 Ny = icell.shg (xx, yy, 1, inode);
98 gvar[icell.gt(inode)] += (Nx * dpropx[ip] + Ny * dpropy[ip]) * dproparea[ip];
99 }
100 }
101
102 }
103
104 if (apply_mass)
105 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar)
106 for (idx_t ii = 0; ii < M.size (); ++ii) {
107 vars[getkey(gvarnames, ivar)][ii] /= M[ii];
108 }
109
110}
111
112template<typename str>
113void
115(const std::map<std::string, std::vector<double>> & vars,
116 std::initializer_list<str> const & gvarnames,
117 std::initializer_list<str> const & pvarnames,
118 bool apply_mass) {
119 using strlist = std::initializer_list<str> const &;
120 g2p<strlist, strlist> (vars, gvarnames,
121 pvarnames, apply_mass);
122}
123
126template<typename GVAR_t, typename PVAR_t, typename P2C_t>
127class
129
131 const PVAR_t x;
132 const PVAR_t y;
133 const GVAR_t M;
134 const GVAR_t gvar;
135 const P2C_t ptcl_to_grd;
137 const double hx;
138 const double hy;
139 PVAR_t dprop;
141
142public :
143
144 g2p_helper_t (const PVAR_t x_, const PVAR_t y_, const GVAR_t M_,
145 const GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_,
146 const double hx_, const double hy_, PVAR_t dprop_, bool apply_mass_)
147 : x(x_), y(y_), gvar(gvar_),
148 ptcl_to_grd(ptcl_to_grd_), nrows(nrows_), hx(hx_), hy(hy_),
149 dprop(dprop_), apply_mass(apply_mass_) {};
150
151 void
152 operator() (idx_t ip) {
153 using qgt = quadgrid_t<GVAR_t>;
154 double N = 0.0;
155 auto xx = x[ip];
156 auto yy = y[ip];
157 auto r = qgt::gind2row (ptcl_to_grd[ip], nrows);
158 auto c = qgt::gind2col (ptcl_to_grd[ip], nrows);
159 for (idx_t inode = 0; inode < 4; ++inode) {
160 N = apply_mass ? qgt::shp (xx, yy, inode, c, r, hx, hy) * M[qgt::gt(inode, c, r, nrows)] :
161 qgt::shp (xx, yy, inode, c, r, hx, hy);
162 dprop[ip] += N * gvar[qgt::gt(inode, c, r, nrows)];
163 }
164 }
165};
166
167
168template<typename GT, typename PT>
169void
171(const std::map<std::string, std::vector<double>>& vars,
172 GT const & gvarnames,
173 PT const & pvarnames,
174 bool apply_mass) {
175
177 double N = 0.0, xx = 0.0, yy = 0.0;
178 idx_t idx = 0;
179
180 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar) {
181 auto & dprop = dprops.at (getkey (pvarnames, ivar));
182 auto const & gvar = vars.at (getkey (gvarnames, ivar));
183
184 // for (idx_t ip = 0; ip < this->num_particles; ++ip) {
185 // xx = x[ip];
186 // yy = y[ip];
187 // auto icell = grid[ptcl_to_grd[ip]];
188 // for (idx_t inode = 0; inode < 4; ++inode) {
189 // N = apply_mass ?
190 // icell.shp(xx, yy, inode) * M[icell.gt(inode)] :
191 // icell.shp(xx, yy, inode);
192 // dprop[ip] += N * gvar[icell.gt(inode)];
193 // }
194 // }
195
196 g2p_helper_t helper (x.begin (), y.begin (), M.cbegin (),
197 gvar.cbegin (), ptcl_to_grd.cbegin (),
198 grid.num_rows (), grid.hx (), grid.hy (),
199 dprop.begin (), apply_mass);
200
201 range rng (0, this->num_particles);
202 std::for_each (rng.begin (), rng.end (), helper);
203
204 }
205}
206
207template<typename str>
208void
210(const std::map<std::string, std::vector<double>>& vars,
211 std::initializer_list<str> const & gvarnames,
212 std::initializer_list<str> const & pxvarnames,
213 std::initializer_list<str> const & pyvarnames,
214 bool apply_mass) {
215 using strlist = std::initializer_list<str> const &;
216 g2pd<strlist, strlist> (vars, gvarnames, pxvarnames,
217 pyvarnames, apply_mass);
218}
219
222template<typename GVAR_t, typename PVAR_t, typename P2C_t>
223class
225
227 const PVAR_t x;
228 const PVAR_t y;
229 const GVAR_t M;
230 const GVAR_t gvar;
231 const P2C_t ptcl_to_grd;
233 const double hx;
234 const double hy;
235 PVAR_t dpropx;
236 PVAR_t dpropy;
238
239public :
240
241 g2pd_helper_t (const PVAR_t x_, const PVAR_t y_, const GVAR_t M_,
242 const GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_,
243 const double hx_, const double hy_, PVAR_t dpropx_, PVAR_t dpropy_,
244 bool apply_mass_)
245 : x(x_), y(y_), gvar(gvar_),
246 ptcl_to_grd(ptcl_to_grd_), nrows(nrows_), hx(hx_), hy(hy_),
247 dpropx(dpropx_), dpropy(dpropy_), apply_mass(apply_mass_) {};
248
249 void
250 operator() (idx_t ip) {
251 using qgt = quadgrid_t<GVAR_t>;
252 double Nx = 0.0, Ny = 0.0;
253 auto xx = x[ip];
254 auto yy = y[ip];
255 auto r = qgt::gind2row (ptcl_to_grd[ip], nrows);
256 auto c = qgt::gind2col (ptcl_to_grd[ip], nrows);
257
258 for (idx_t inode = 0; inode < 4; ++inode) {
259 Nx = apply_mass ?
260 qgt::shg (xx, yy, 0, inode, c, r, hx, hy) * M[qgt::gt(inode, c, r, nrows)] :
261 qgt::shg (xx, yy, 0, inode, c, r, hx, hy);
262 Ny = apply_mass ?
263 qgt::shg (xx, yy, 1, inode, c, r, hx, hy) * M[qgt::gt(inode, c, r, nrows)] :
264 qgt::shg (xx, yy, 1, inode, c, r, hx, hy);
265 dpropx[ip] += Nx * gvar[qgt::gt(inode, c, r, nrows)];
266 dpropy[ip] += Ny * gvar[qgt::gt(inode, c, r, nrows)];
267
268 }
269 }
270};
271
272template<typename GT, typename PT>
273void
275(const std::map<std::string, std::vector<double>>& vars,
276 GT const & gvarnames,
277 PT const & pxvarnames,
278 PT const & pyvarnames,
279 bool apply_mass) {
280
282 double Nx = 0.0, Ny = 0.0, xx = 0.0, yy = 0.0;
283
284 for (std::size_t ivar = 0; ivar < std::size (gvarnames); ++ivar) {
285 auto const & gvar = vars.at (getkey (gvarnames, ivar));
286 auto & dpropx = dprops.at (getkey (pxvarnames, ivar));
287 auto & dpropy = dprops.at (getkey (pyvarnames, ivar));
288
289 // for (idx_t ip = 0; ip <= this->num_particles; ++ip) {
290 // xx = x[ip];
291 // yy = y[ip];
292 // auto icell = grid[ptcl_to_grd[ip]];
293 // for (idx_t inode = 0; inode < 4; ++inode) {
294 // Nx = apply_mass ?
295 // icell.shg(xx, yy, 0, inode) * M[icell.gt(inode)] :
296 // icell.shg(xx, yy, 0, inode);
297 // Ny = apply_mass ?
298 // icell.shg(xx, yy, 1, inode) * M[icell.gt(inode)] :
299 // icell.shg(xx, yy, 1, inode);
300 // dpropx[ip] += Nx * gvar[icell.gt(inode)];
301 // dpropy[ip] += Ny * gvar[icell.gt(inode)];
302 // }
303 // }
304
305 g2pd_helper_t helper (x.begin (), y.begin (), M.cbegin (),
306 gvar.cbegin (), ptcl_to_grd.cbegin (),
307 grid.num_rows (), grid.hx (), grid.hy (),
308 dpropx.begin (), dpropy.begin (), apply_mass);
309
310 range rng (0, this->num_particles);
311 std::for_each (rng.begin (), rng.end (), helper);
312
313 }
314}
315
316template<>
317void
319(std::ostream & os) const;
320
321template<>
322void
324(std::ostream & os) const;
325
326template<>
327void
329(std::ostream & os) const;
330
331#endif
Template class for the implementation of the g2p method.
Definition particles_imp.h:128
bool apply_mass
Definition particles_imp.h:140
PVAR_t dprop
Definition particles_imp.h:139
const double hy
Definition particles_imp.h:138
const GVAR_t M
Definition particles_imp.h:133
const double hx
Definition particles_imp.h:137
const P2C_t ptcl_to_grd
Definition particles_imp.h:135
const PVAR_t x
Definition particles_imp.h:131
particles_t::idx_t idx_t
Definition particles_imp.h:130
const GVAR_t gvar
Definition particles_imp.h:134
g2p_helper_t(const PVAR_t x_, const PVAR_t y_, const GVAR_t M_, const GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_, const double hx_, const double hy_, PVAR_t dprop_, bool apply_mass_)
Definition particles_imp.h:144
const idx_t nrows
Definition particles_imp.h:136
const PVAR_t y
Definition particles_imp.h:132
Template class for the implementation g2pd method.
Definition particles_imp.h:224
particles_t::idx_t idx_t
Definition particles_imp.h:226
const P2C_t ptcl_to_grd
Definition particles_imp.h:231
const GVAR_t gvar
Definition particles_imp.h:230
PVAR_t dpropx
Definition particles_imp.h:235
bool apply_mass
Definition particles_imp.h:237
const double hy
Definition particles_imp.h:234
PVAR_t dpropy
Definition particles_imp.h:236
const idx_t nrows
Definition particles_imp.h:232
const GVAR_t M
Definition particles_imp.h:229
g2pd_helper_t(const PVAR_t x_, const PVAR_t y_, const GVAR_t M_, const GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_, const double hx_, const double hy_, PVAR_t dpropx_, PVAR_t dpropy_, bool apply_mass_)
Definition particles_imp.h:241
const double hx
Definition particles_imp.h:233
const PVAR_t x
Definition particles_imp.h:227
const PVAR_t y
Definition particles_imp.h:228
Definition quadgrid_cpp.h:24
idx_t & ip(const std::string &name, idx_t ii)
shortcut for iprops.at (name) [ii]
Definition particles.h:265
void p2gd(std::map< std::string, std::vector< double > > &vars, PT const &pxvarnames, PT const &pyvarnames, std::string const &area, GT const &gvarnames, bool apply_mass=false) const
Definition particles_imp.h:75
idx_t num_particles
number of particles.
Definition particles.h:34
void g2p(const std::map< std::string, std::vector< double > > &vars, bool apply_mass=false)
Definition particles.h:346
quadgrid_t< std::vector< double > >::idx_t idx_t
datatype for indexing into vectors of properties
Definition particles.h:32
const quadgrid_t< std::vector< double > > & grid
refernce to a grid object.
Definition particles.h:48
static const std::string & getkey(std::map< std::string, std::vector< double > > const &varnames, std::size_t ivar)
Definition particles.h:277
void print(std::ostream &os) const
Template for export function.
Definition particles.h:97
std::map< std::string, std::vector< double > > dprops
double type quantities associated with the particles.
Definition particles.h:42
std::vector< idx_t > ptcl_to_grd
particles->grid connectivity.
Definition particles.h:46
std::vector< double > x
x coordinate of particle positions.
Definition particles.h:35
std::vector< double > y
y coordinate of particle positions.
Definition particles.h:36
void g2pd(const std::map< std::string, std::vector< double > > &vars, GT const &gvarnames, PT const &pxvarnames, PT const &pyvarnames, bool apply_mass=false)
Definition particles_imp.h:275
std::vector< double > M
Mass matrix to be used for transfers if required.
Definition particles.h:44
void p2g(std::map< std::string, std::vector< double > > &vars, bool apply_mass=false) const
Map particle variables to the grid.
Definition particles.h:302
Definition counter.h:7
iterator begin() const
Definition counter.h:33
iterator end() const
Definition counter.h:34