quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
quadgrid_cpp.h
Go to the documentation of this file.
1#ifndef QUADGRID_H
2#define QUADGRID_H
3
4#include <algorithm>
5#include <fstream>
6#include <iomanip>
7#include <quadgrid_config.h>
8#include <json.hpp>
9#include <map>
10#include <vector>
11
12template <class distributed_vector>
13class
15{
16
17public:
18
19 using idx_t = int;
20
21 class cell_t;
22
34
35 void
36 from_json (const nlohmann::json &j, grid_properties_t &q) {
37
38 j.at ("nx").get_to (q.numcols);
39 j.at ("ny").get_to (q.numrows);
40 j.at ("hx").get_to (q.hx);
41 j.at ("hy").get_to (q.hy);
42
43
44 q.start_cell_row = 0;
45 q.end_cell_row = q.numrows - 1;
46 q.start_cell_col = 0;
47 q.end_cell_col = q.numcols - 1;
48 q.start_owned_nodes = 0;
49 q.num_owned_nodes = (q.numrows+1)*(q.numcols+1);
50
51 }
52
54 static idx_t
55 gind2col (idx_t idx, idx_t numrows) {
56 return (idx / numrows);
57 }
58
60 static idx_t
61 gind2row (idx_t idx, idx_t numrows) {
62 return (idx % numrows);
63 }
64
66 static idx_t
67 gt (idx_t inode, idx_t cidx, idx_t ridx, idx_t numrows) {
68 idx_t bottom_left = 0;
69 // should check that inode < 4 in an efficient way
70 bottom_left = ridx + cidx * (numrows + 1);
71 switch (inode) {
72 case 0 :
73 return (bottom_left);
74 break;
75 case 1 :
76 return (bottom_left + 1);
77 break;
78 case 2 :
79 return (bottom_left + (numrows + 1));
80 break;
81 case 3 :
82 return (bottom_left + (numrows + 2));
83 break;
84 default :
85 return -1;
86 }
87 }
88
89
90 //-----------------------------------
91 //
92 // Numbering of nodes and edges :
93 //
94 // 1
95 // |
96 // V
97 // 1 -> O---------------O <- 3
98 // | |
99 // | |
100 // 2 -> | | <- 3
101 // | |
102 // | |
103 // 0 -> O---------------O <- 2
104 // ^
105 // |
106 // 0
107 //
108 //-----------------------------------
110 static real_t
111 p (idx_t idir, idx_t inode, idx_t colidx, idx_t rowidx, real_t hx, real_t hy) {
112 real_t bottom_left = 0.0;
113 // should check that inode < 4 in an efficient way
114 if (idir == 0) {
115 bottom_left = colidx * hx;
116 if (inode > 1)
117 bottom_left += hx;
118 } else {
119 bottom_left = rowidx * hy;
120 if (inode == 1 || inode == 3)
121 bottom_left += hy;
122 }
123 return (bottom_left);
124 }
125
127 static real_t
128 shp (real_t x, real_t y, idx_t inode,
129 idx_t c, idx_t r, real_t hx, real_t hy) {
130
131 switch (inode) {
132 case 3 :
133 return ((x - p(0,0,c,r,hx,hy))/hx * (y - p(1,0,c,r,hx,hy))/hy);
134 break;
135 case 2 :
136 return ((x - p(0,0,c,r,hx,hy))/hx * (1. - (y - p(1,0,c,r,hx,hy))/hy));
137 break;
138 case 1 :
139 return ((1. - (x - p(0,0,c,r,hx,hy))/hx) * (y - p(1,0,c,r,hx,hy))/hy);
140 break;
141 case 0 :
142 return ((1. - (x - p(0,0,c,r,hx,hy))/hx) * (1. - (y - p(1,0,c,r,hx,hy))/hy));
143 break;
144 default :
145 return 0;
146 }
147
148 }
149
151 static real_t
152 shg (real_t x, real_t y, idx_t idir, idx_t inode,
153 idx_t c, idx_t r, real_t hx, real_t hy) {
154 switch (inode) {
155 case 3 :
156 if (idir == 0) {
157 return ((1. / hx) * ((y - p(1,0,c,r,hx,hy)) / hy));
158 }
159 else if (idir == 1) {
160 return (((x - p(0,0,c,r,hx,hy)) / hx) * (1. / hy));
161 }
162 break;
163 case 2 :
164 if (idir == 0) {
165 return ((1. / hx) * ((1. - (y - p(1,0,c,r,hx,hy)) / hy)));
166 }
167 else if (idir == 1) {
168 return (((x - p(0,0,c,r,hx,hy)) / hx) * (- 1. / hy));
169 }
170 break;
171 case 1 :
172 if (idir == 0) {
173 return ((- 1. / hx) * ((y - p(1,0,c,r,hx,hy)) / hy));
174 }
175 else if (idir == 1) {
176 return ((1. - (x - p(0,0,c,r,hx,hy)) / hx) * (1. / hy));
177 }
178 break;
179 case 0 :
180 if (idir == 0) {
181 return ((- 1. / hx) * (1. - (y - p(1,0,c,r,hx,hy)) / hy));
182 }
183 else if (idir == 1) {
184 return ((1. - (x - p(0,0,c,r,hx,hy))/ hx) * (- 1. / hy));
185 }
186 break;
187 }
188 return 0.;
189 };
190
192 static idx_t
194 return (r + nr * c);
195 }
196
197
198 class
200 {
201
202 public:
203
204 cell_iterator (cell_t* _data = nullptr)
205 : data (_data) { };
206
207 void
208 operator++ ();
209
210 cell_t&
211 operator* ()
212 { return *(this->data); };
213
214 const cell_t&
215 operator* () const
216 { return *(this->data); };
217
218 cell_t*
219 operator-> ()
220 { return this->data; };
221
222 const cell_t*
223 operator-> () const
224 { return this->data; };
225
226 bool
227 operator== (const cell_iterator& other)
228 { return (data == other.data); }
229
230 bool
231 operator!= (const cell_iterator& other)
232 { return ! ((*this) == other); }
233
234
235 private :
237 };
238
239 class
241 {
242
243 public:
244
245 void
246 operator++ ();
247
248 neighbor_iterator (cell_t *_data = nullptr,
249 int _face_idx = -1)
250 : cell_iterator (_data), face_idx (_face_idx) { };
251
252 int
254 { return face_idx; };
255
256 private:
258
259 private :
261 };
262
263 class
264 cell_t
265 {
266
267 friend class cell_iterator;
268 friend class quadgrid_t;
269
270 public:
271
272 static constexpr idx_t nodes_per_cell = 4;
273 static constexpr idx_t edges_per_cell = 4;
274 static constexpr idx_t NOT_ON_BOUNDARY = -1;
275
277 : grid_properties (_gp), rowidx (0), colidx (0), is_ghost (false) { };
278
279 real_t
280 p (idx_t i, idx_t j) const;
281
282 real_t
284
285 idx_t
286 t (idx_t i) const;
287
288 idx_t
289 gt (idx_t i) const {
290 // should check that inode < 4 in an efficient way
291 switch (i) {
292 case 0 :
293 case 1 :
294 case 2 :
295 case 3 :
296 return quadgrid_t::gt (i, col_idx (), row_idx (), num_rows ());
297 break;
298 default :
299 return -1;
300 }
301 }
302
303 idx_t
304 e (idx_t i) const;
305
306 real_t
307 shp (real_t x, real_t y, idx_t inode) const;
308
309 real_t
310 shp_new (real_t x, real_t y, idx_t inode) const;
311
312 real_t
313 shg (real_t x, real_t y, idx_t idir, idx_t inode) const;
314
317
320
323 { return neighbor_iterator (); };
324
327 { return neighbor_iterator (); };
328
329 idx_t
331 { return local_cell_idx; };
332
333 idx_t
335 { return global_cell_idx; };
336
337 idx_t
339 { return grid_properties.end_cell_col; };
340
341 idx_t
343 { return grid_properties.end_cell_row; };
344
345 idx_t
347 { return grid_properties.start_cell_col; };
348
349 idx_t
351 { return grid_properties.start_cell_row; };
352
353 idx_t
354 num_rows () const
355 { return grid_properties.numrows; };
356
357 idx_t
358 num_cols () const
359 { return grid_properties.numcols; };
360
361 idx_t
362 row_idx () const
363 { return rowidx; };
364
365 idx_t
366 col_idx () const
367 { return colidx; };
368
369
370 idx_t
371 sub2gind (idx_t r, idx_t c) const {
372 return quadgrid_t::sub2gind (r, c, grid_properties.numrows);
373 }
374
375 idx_t
376 gind2row (idx_t idx) const {
377 return quadgrid_t:: gind2row (idx, grid_properties.numrows);
378 }
379
380 idx_t
381 gind2col (idx_t idx) const {
382 return quadgrid_t:: gind2col (idx, grid_properties.numrows);
383 }
384
385 void
386 reset () {
387 rowidx = grid_properties.start_cell_row;
388 colidx = grid_properties.start_cell_col;
391 sub2gind (grid_properties.start_cell_row,
392 grid_properties.start_cell_col);
393 };
394
395 private:
396
403
404 };
405
406
409 comm (_comm), rank (0), size (1),
412 {
413 int flag = 0;
414 MPI_Initialized (&flag);
415 if (flag) {
418 } else {
419 rank = 0;
420 size = 1;
421 }
422 grid_properties.numrows = 0;
423 grid_properties.numcols = 0;
424 grid_properties.hx = 0.;
425 grid_properties.hy = 0.;
426 grid_properties.start_cell_row = 0;
427 grid_properties.end_cell_row = 0;
428 grid_properties.start_cell_col = 0;
429 grid_properties.end_cell_col = 0;
430 grid_properties.start_owned_nodes = 0;
431 grid_properties.num_owned_nodes = 0;
432 };
433
435 quadgrid_t (const nlohmann::json &j, MPI_Comm _comm = MPI_COMM_WORLD) :
436 quadgrid_t(_comm) { from_json (j, grid_properties); };
437
439 quadgrid_t (const quadgrid_t &) = delete;
440
442 quadgrid_t &
443 operator= (const quadgrid_t &) = delete;
444
446 ~quadgrid_t () = default;
447
448 void
449 set_sizes (idx_t numrows, idx_t numcols,
450 real_t hx, real_t hy);
451
452 void
453 vtk_export (const char *filename,
454 const std::map<std::string,
455 distributed_vector> & f) const;
456
457 void
458 octave_ascii_export (const char *filename,
459 const std::map<std::string,
460 distributed_vector> & f) const;
461
462 cell_iterator
464
465 const cell_iterator
467
468 cell_iterator
470 { return cell_iterator (); };
471
472 const cell_iterator
474 { return cell_iterator (); };
475
476 idx_t
478 { return grid_properties.num_owned_nodes; };
479
480 idx_t
482
483 idx_t
485
486 idx_t
488
489 idx_t
491
492 idx_t
493 num_rows () const
494 { return grid_properties.numrows; };
495
496 idx_t
497 num_cols () const
498 { return grid_properties.numcols; };
499
500 real_t
501 hx () const
502 { return grid_properties.hx; };
503
504 real_t
505 hy () const
506 { return grid_properties.hy; };
507
508 idx_t
509 sub2gind (idx_t r, idx_t c) const {
510 return (r + grid_properties.numrows * c);
511 }
512
513 idx_t
514 gind2row (idx_t idx) const {
515 return quadgrid_t:: gind2row (idx, grid_properties.numrows);
516 }
517
518 idx_t
519 gind2col (idx_t idx) const {
520 return quadgrid_t:: gind2col (idx, grid_properties.numrows);
521 }
522
523 const cell_t&
524 operator[] (idx_t tmp) const;
525
527 int rank;
528 int size;
529
530private :
531
532 mutable cell_t current_cell;
533 mutable cell_t current_neighbor;
534
535 grid_properties_t grid_properties;
536
537};
538
539
540
541#include "quadgrid_cpp_imp.h"
542
543#endif /* QUADGRID_H */
544
cell_iterator(cell_t *_data=nullptr)
idx_t start_cell_row() const
idx_t end_cell_col() const
real_t shp_new(real_t x, real_t y, idx_t inode) const
idx_t sub2gind(idx_t r, idx_t c) const
static constexpr idx_t NOT_ON_BOUNDARY
real_t shg(real_t x, real_t y, idx_t idir, idx_t inode) const
idx_t row_idx() const
idx_t num_rows() const
static constexpr idx_t edges_per_cell
const neighbor_iterator begin_neighbor_sweep() const
const neighbor_iterator end_neighbor_sweep() const
cell_t(const grid_properties_t &_gp)
idx_t end_cell_row() const
real_t centroid(idx_t i)
idx_t get_global_cell_idx() const
const grid_properties_t & grid_properties
idx_t col_idx() const
idx_t gt(idx_t i) const
neighbor_iterator end_neighbor_sweep()
idx_t gind2col(idx_t idx) const
idx_t get_local_cell_idx() const
friend class cell_iterator
idx_t gind2row(idx_t idx) const
friend class quadgrid_t
static constexpr idx_t nodes_per_cell
idx_t t(idx_t i) const
neighbor_iterator begin_neighbor_sweep()
idx_t num_cols() const
idx_t start_cell_col() const
neighbor_iterator(cell_t *_data=nullptr, int _face_idx=-1)
cell_t * data
Face index in 0...3 (-1 if not defined).
const cell_t & operator[](idx_t tmp) const
idx_t sub2gind(idx_t r, idx_t c) const
idx_t gind2row(idx_t idx) const
idx_t num_local_cells() const
cell_t current_cell
real_t hy() const
idx_t num_owned_nodes()
HOST static DEVICE idx_t gind2row(idx_t idx, idx_t numrows)
idx_t num_cols() const
HOST static DEVICE real_t p(idx_t idir, idx_t inode, idx_t colidx, idx_t rowidx, real_t hx, real_t hy)
void vtk_export(const char *filename, const std::map< std::string, distributed_vector > &f) const
void set_sizes(idx_t numrows, idx_t numcols, real_t hx, real_t hy)
quadgrid_t & operator=(const quadgrid_t &)=delete
Delete assignment operator.
quadgrid_t(const quadgrid_t &)=delete
Delete copy constructor.
idx_t num_local_nodes() const
idx_t num_global_nodes() const
HOST static DEVICE idx_t sub2gind(idx_t r, idx_t c, idx_t nr)
const cell_iterator begin_cell_sweep() const
MPI_Comm comm
HOST static DEVICE real_t shp(real_t x, real_t y, idx_t inode, idx_t c, idx_t r, real_t hx, real_t hy)
idx_t num_rows() const
HOST static DEVICE real_t shg(real_t x, real_t y, idx_t idir, idx_t inode, idx_t c, idx_t r, real_t hx, real_t hy)
int idx_t
HOST static DEVICE idx_t gt(idx_t inode, idx_t cidx, idx_t ridx, idx_t numrows)
HOST static DEVICE idx_t gind2col(idx_t idx, idx_t numrows)
cell_t current_neighbor
cell_iterator begin_cell_sweep()
void octave_ascii_export(const char *filename, const std::map< std::string, distributed_vector > &f) const
void from_json(const nlohmann::json &j, grid_properties_t &q)
quadgrid_t(const nlohmann::json &j, MPI_Comm _comm=MPI_COMM_WORLD)
Ctor that reads grid properties from a json object.
const cell_iterator end_cell_sweep() const
real_t hx() const
grid_properties_t grid_properties
quadgrid_t(MPI_Comm _comm=MPI_COMM_WORLD)
Default constructor, set all pointers to nullptr.
cell_iterator end_cell_sweep()
~quadgrid_t()=default
Destructor.
idx_t gind2col(idx_t idx) const
idx_t num_global_cells() const
#define MPI_COMM_WORLD
#define DEVICE
#define MPI_Comm_size(x, y)
double real_t
#define HOST
#define MPI_Initialized(x)
#define MPI_Comm
#define MPI_Comm_rank(x, y)