quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
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 <json.hpp>
9#include <map>
10#include <quadgrid_cpp.h>
11#include <string>
12
14using assignment_t = std::function <double& (double&, const double&)>;
15
16namespace ASSIGNMENT_OPS {
17 extern assignment_t EQ; // = [] (double& TO, const double& FROM) -> double& { return TO = FROM; };
18 extern assignment_t PLUS_EQ; // = [] (double& TO, const double& FROM) -> double& { return TO += FROM; };
19 extern assignment_t TIMES_EQ; // = [] (double& TO, const double& FROM) -> double& { return TO *= FROM; };
20}
21
23
35
36struct
38
41
43 std::vector<double> x;
44 std::vector<double> y;
45
47 std::map<std::string, std::vector<idx_t>> iprops;
48
50 std::map<std::string, std::vector<double>> dprops;
51
52 std::vector<double> M;
53 std::map<idx_t, std::vector<idx_t>> grd_to_ptcl;
55
57 enum class
59 csv = 0,
62
63 octave_ascii = 1,
65
66 json = 2
68 };
69
73
75 double
76 default_x_generator ();
77
81
83 double
84 default_y_generator ();
85
87
90 template<output_format fmt>
91 void
92 print (std::ostream & os) const {
93 os << "output format not implementd" << std::endl;
94 }
95
97
101 particles_t (idx_t n, const quadgrid_t<std::vector<double>>& grid_)
102 : num_particles(n), grid(grid_) { }
103
105
108 particles_t (const nlohmann::json &j,
109 const quadgrid_t<std::vector<double>>& grid_)
110 : grid(grid_)
111 {
112 j["dprops"].get_to<std::map<std::string, std::vector<double>>> (dprops);
113 j["iprops"].get_to<std::map<std::string, std::vector<int>>> (iprops);
114 j["x"].get_to<std::vector<double>> (x);
115 j["y"].get_to<std::vector<double>> (y);
116 j["num_particles"].get_to<idx_t> (num_particles);
117 }
118
120
127 particles_t (idx_t n, const std::vector<std::string>& ipropnames,
128 const std::vector<std::string>& dpropnames,
129 const quadgrid_t<std::vector<double>>& grid_);
130
132
142 particles_t (idx_t n, const std::vector<std::string>& ipropnames,
143 const std::vector<std::string>& dpropnames,
144 const quadgrid_t<std::vector<double>>& grid_,
145 const std::vector<double> & xv,
146 const std::vector<double> & yv);
147
149
159 particles_t (idx_t n, const std::vector<std::string>& ipropnames,
160 const std::vector<std::string>& dpropnames,
161 const quadgrid_t<std::vector<double>>& grid_,
162 std::function<double ()> xgen,
163 std::function<double ()> ygen);
164
166
172 void
173 init_props (const std::vector<std::string>& ipropnames,
174 const std::vector<std::string>& dpropnames);
175
177
182 void
183 remove_in_region (std::function<bool (double, double)> fun) {
184 std::vector<idx_t> vin{};
185 for (idx_t i = 0; i < num_particles; ++i) {
186 if (fun (x[i], y[i])) {
187 vin.push_back (i);
188 }
189 }
190
191 for (auto &dprop : dprops) {
192 for (auto id = vin.rbegin (); id != vin.rend (); ++id) {
193 dprop.second.erase (dprop.second.begin () + (*id));
194 }
195 }
196
197 for (auto &iprop : iprops) {
198 for (auto id = vin.rbegin (); id != vin.rend (); ++id) {
199 iprop.second.erase (iprop.second.begin () + (*id));
200 }
201 }
202
203 for (auto id = vin.rbegin (); id != vin.rend (); ++id) {
204 x.erase (x.begin () + (*id));
205 y.erase (y.begin () + (*id));
207 }
208 };
209
211
214 void
215 init_particle_mesh ();
216
218
221 void
222 init_particle_positions (std::function<double ()> xgentr,
223 std::function<double ()> ygentr);
224
226
229 void
230 build_mass ();
231
233 double &
234 dp (const std::string & name, idx_t ii) {
235 return dprops.at (name) [ii];
236 }
237
239 const double &
240 dp (const std::string & name, idx_t ii) const {
241 return dprops.at (name) [ii];
242 }
243
245 idx_t &
246 ip (const std::string & name, idx_t ii) {
247 return iprops.at (name) [ii];
248 }
249
251 const idx_t &
252 ip (const std::string & name, idx_t ii) const {
253 return iprops.at (name) [ii];
254 }
255
256 static
257 const std::string &
258 getkey(std::map<std::string, std::vector<double>> const &varnames,
259 std::size_t ivar) {
260 return std::next (varnames.begin (), ivar)->first;
261 };
262
263 static
264 const std::string &
265 getkey(std::vector<std::string> const &varnames,
266 std::size_t ivar) {
267 return varnames[ivar];
268 };
269
270 static
271 const char*
272 getkey(std::initializer_list<const char *> const &varnames,
273 std::size_t ivar) {
274 return *(std::next (varnames.begin (), ivar));
275 };
276
278
282 void
283 p2g (std::map<std::string, std::vector<double>> & vars,
284 bool apply_mass = false) const {
285 p2g (vars, vars, vars, apply_mass);
286 }
287
289
294 template<typename GT, typename PT>
295 void
296 p2g (std::map<std::string, std::vector<double>> & vars,
297 PT const & pvarnames,
298 GT const & gvarnames,
299 bool apply_mass = false,
301
302 template<typename str>
303 void
304 p2g (std::map<std::string, std::vector<double>> & vars,
305 std::initializer_list<str> const & pvarnames,
306 std::initializer_list<str> const & gvarnames,
307 bool apply_mass = false,
309
310 template<typename GT, typename PT>
311 void
312 p2gd (std::map<std::string, std::vector<double>> & vars,
313 PT const & pxvarnames,
314 PT const & pyvarnames,
315 std::string const &area,
316 GT const & gvarnames,
317 bool apply_mass = false,
319
320 template<typename str>
321 void
322 p2gd (std::map<std::string, std::vector<double>> & vars,
323 std::initializer_list<str> const & pxvarnames,
324 std::initializer_list<str> const & pyvarnames,
325 std::string const & area,
326 std::initializer_list<str> const & gvarnames,
327 bool apply_mass = false,
329
330 void
331 g2p (const std::map<std::string, std::vector<double>>& vars,
332 bool apply_mass = false, assignment_t OP = ASSIGNMENT_OPS::PLUS_EQ) {
333 g2p (vars, vars, vars, apply_mass, OP);
334 }
335
336 template<typename str>
337 void
338 g2p (const std::map<std::string, std::vector<double>>& vars,
339 std::initializer_list<str> const & gvarnames,
340 std::initializer_list<str> const & pvarnames,
341 bool apply_mass = false,
343
344 template<typename GT, typename PT>
345 void
346 g2p (const std::map<std::string, std::vector<double>>& vars,
347 GT const & gvarnames,
348 PT const & pvarnames,
349 bool apply_mass = false,
351
352 template<typename GT, typename PT>
353 void
354 g2pd (const std::map<std::string, std::vector<double>>& vars,
355 GT const & gvarnames,
356 PT const & pxvarnames,
357 PT const & pyvarnames,
358 bool apply_mass = false,
360
361 template<typename str>
362 void
363 g2pd (const std::map<std::string, std::vector<double>>& vars,
364 std::initializer_list<str> const & gvarnames,
365 std::initializer_list<str> const &pxvarnames,
366 std::initializer_list<str> const & pyvarnames,
367 bool apply_mass = false,
369
370};
371
374void
375to_json (nlohmann::json &j, const particles_t &p);
376
377
378#include "particles_imp.h"
379
380#endif /* PARTICLES_H */
quadgrid_t< std::vector< double > >::idx_t idx_t
Definition: check_shg.cpp:7
Definition: quadgrid_cpp.h:15
in num_particles
Definition: make_input_json.m:10
in dprops
Definition: make_input_json.m:13
x
Definition: make_input_json.m:5
y
Definition: make_input_json.m:6
Definition: particles.cpp:8
assignment_t TIMES_EQ
Definition: particles.cpp:11
assignment_t EQ
Definition: particles.cpp:9
assignment_t PLUS_EQ
Definition: particles.cpp:10
void to_json(nlohmann::json &j, const particles_t &p)
Adaptor to allow implicit conversion from particles_t to json.
Definition: particles.cpp:242
std::function< double &(double &, const double &)> assignment_t
datatype for assignment operators
Definition: particles.h:14
Class to represent particles embedded in a grid.
Definition: particles.h:37
const idx_t & ip(const std::string &name, idx_t ii) const
shortcut for iprops.at (name) [ii]
Definition: particles.h:252
void g2p(const std::map< std::string, std::vector< double > > &vars, bool apply_mass=false, assignment_t OP=ASSIGNMENT_OPS::PLUS_EQ)
Definition: particles.h:331
idx_t & ip(const std::string &name, idx_t ii)
shortcut for iprops.at (name) [ii]
Definition: particles.h:246
std::map< idx_t, std::vector< idx_t > > grd_to_ptcl
grid/particles connectivity.
Definition: particles.h:53
std::map< std::string, std::vector< idx_t > > iprops
integer type quantities associated with the particles.
Definition: particles.h:47
particles_t(idx_t n, const quadgrid_t< std::vector< double > > &grid_)
Simplest form of constructor.
Definition: particles.h:101
double & dp(const std::string &name, idx_t ii)
shortcut for dprops.at (name) [ii]
Definition: particles.h:234
idx_t num_particles
number of particles.
Definition: particles.h:42
const quadgrid_t< std::vector< double > > & grid
refernce to a grid object.
Definition: particles.h:54
output_format
Enumeration of available output format.
Definition: particles.h:58
static const char * getkey(std::initializer_list< const char * > const &varnames, std::size_t ivar)
Definition: particles.h:272
static const std::string & getkey(std::map< std::string, std::vector< double > > const &varnames, std::size_t ivar)
Definition: particles.h:258
void print(std::ostream &os) const
Template for export function.
Definition: particles.h:92
std::map< std::string, std::vector< double > > dprops
double type quantities associated with the particles.
Definition: particles.h:50
void remove_in_region(std::function< bool(double, double)> fun)
Erase particcles based on coordinates.
Definition: particles.h:183
const double & dp(const std::string &name, idx_t ii) const
shortcut for dprops.at (name) [ii]
Definition: particles.h:240
std::vector< double > x
x coordinate of particle positions.
Definition: particles.h:43
quadgrid_t< std::vector< double > >::idx_t idx_t
datatype for indexing into vectors of properties
Definition: particles.h:40
particles_t(const nlohmann::json &j, const quadgrid_t< std::vector< double > > &grid_)
Ctor to import data from json.
Definition: particles.h:108
std::vector< double > y
y coordinate of particle positions.
Definition: particles.h:44
static const std::string & getkey(std::vector< std::string > const &varnames, std::size_t ivar)
Definition: particles.h:265
std::vector< double > M
Mass matrix to be used for transfers if required.
Definition: particles.h:52
void p2g(std::map< std::string, std::vector< double > > &vars, bool apply_mass=false) const
Map particle variables to the grid.
Definition: particles.h:283