quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
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 <json.hpp>
8#include <map>
9#include <mpi.h>
10#include <vector>
11
12template <class distributed_vector>
13class
15{
16
17public:
18
19 using idx_t = int;
20
21 class cell_t;
22
26 double hx, hy;
33 };
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;
49 q.num_owned_nodes = (q.numrows+1)*(q.numcols+1);
50
51 }
52
53 class
55 {
56
57 public:
58
59 cell_iterator (cell_t* _data = nullptr)
60 : data (_data) { };
61
62 void
63 operator++ ();
64
65 cell_t&
66 operator* ()
67 { return *(this->data); };
68
69 const cell_t&
70 operator* () const
71 { return *(this->data); };
72
73 cell_t*
74 operator-> ()
75 { return this->data; };
76
77 const cell_t*
78 operator-> () const
79 { return this->data; };
80
81 bool
82 operator== (const cell_iterator& other)
83 { return (data == other.data); }
84
85 bool
86 operator!= (const cell_iterator& other)
87 { return ! ((*this) == other); }
88
89
90 private :
92 };
93
94 class
96 {
97
98 public:
99
100 void
101 operator++ ();
102
103 neighbor_iterator (cell_t *_data = nullptr,
104 int _face_idx = -1)
105 : cell_iterator (_data), face_idx (_face_idx) { };
106
107 int
109 { return face_idx; };
110
111 private:
113
114 private :
116 };
117
118 class
119 cell_t
120 {
121
122 friend class cell_iterator;
123
124 public:
125
126 static constexpr idx_t nodes_per_cell = 4;
127 static constexpr idx_t edges_per_cell = 4;
128 static constexpr idx_t NOT_ON_BOUNDARY = -1;
129
131 : grid_properties (_gp), rowidx (0), colidx (0), is_ghost (false) { };
132
133 double
134 p (idx_t i, idx_t j) const;
135
136 double
138
139 idx_t
140 t (idx_t i) const;
141
142 idx_t
143 gt (idx_t i) const;
144
145 idx_t
146 e (idx_t i) const;
147
148 double
149 shp (double x, double y, idx_t inode) const;
150
151 double
152 shg (double x, double y, idx_t idir, idx_t inode) const;
153
156
159
162 { return neighbor_iterator (); };
163
166 { return neighbor_iterator (); };
167
168
169 idx_t
171 { return local_cell_idx; };
172
173 idx_t
175 { return global_cell_idx; };
176
177 idx_t
179 { return grid_properties.end_cell_col; };
180
181 idx_t
183 { return grid_properties.end_cell_row; };
184
185 idx_t
187 { return grid_properties.start_cell_col; };
188
189 idx_t
191 { return grid_properties.start_cell_row; };
192
193 idx_t
194 num_rows () const
195 { return grid_properties.numrows; };
196
197 idx_t
198 num_cols () const
199 { return grid_properties.numcols; };
200
201 idx_t
202 row_idx () const
203 { return rowidx; };
204
205 idx_t
206 col_idx () const
207 { return colidx; };
208
209 idx_t
210 sub2gind (idx_t r, idx_t c) const {
211 return (r + grid_properties.numrows * c);
212 }
213
214 idx_t
215 gind2row (idx_t idx) const {
216 return (idx / grid_properties.numrows);
217 }
218
219 idx_t
220 gind2col (idx_t idx) const {
221 return (idx % grid_properties.numrows);
222 }
223
224 void
225 reset () {
226 rowidx = grid_properties.start_cell_row;
227 colidx = grid_properties.start_cell_col;
228 global_cell_idx = sub2gind (rowidx, colidx);
229 local_cell_idx = global_cell_idx -
230 sub2gind (grid_properties.start_cell_row,
231 grid_properties.start_cell_col);
232 };
233
234 private:
235
242
243 };
244
245
247 quadgrid_t (MPI_Comm _comm = MPI_COMM_WORLD) :
248 comm (_comm), rank (0), size (1),
249 current_cell (grid_properties),
250 current_neighbor (grid_properties)
251 {
252 int flag = 0;
253 MPI_Initialized (&flag);
254 if (flag) {
255 MPI_Comm_rank (comm, &rank);
256 MPI_Comm_size (comm, &size);
257 } else {
258 rank = 0;
259 size = 1;
260 }
261 grid_properties.numrows = 0;
262 grid_properties.numcols = 0;
263 grid_properties.hx = 0.;
264 grid_properties.hy = 0.;
265 grid_properties.start_cell_row = 0;
266 grid_properties.end_cell_row = 0;
267 grid_properties.start_cell_col = 0;
268 grid_properties.end_cell_col = 0;
269 grid_properties.start_owned_nodes = 0;
270 grid_properties.num_owned_nodes = 0;
271 };
272
274 quadgrid_t (const nlohmann::json &j, MPI_Comm _comm = MPI_COMM_WORLD) :
275 quadgrid_t(_comm) { from_json (j, grid_properties); };
276
278 quadgrid_t (const quadgrid_t &) = delete;
279
281 quadgrid_t &
282 operator= (const quadgrid_t &) = delete;
283
285 ~quadgrid_t () = default;
286
287 void
288 set_sizes (idx_t numrows, idx_t numcols,
289 double hx, double hy);
290
291 void
292 vtk_export (const char *filename,
293 const std::map<std::string,
294 distributed_vector> & f) const;
295
296 void
297 octave_ascii_export (const char *filename,
298 const std::map<std::string,
299 distributed_vector> & f) const;
300
302 begin_cell_sweep ();
303
304 const cell_iterator
305 begin_cell_sweep () const;
306
309 { return cell_iterator (); };
310
311 const cell_iterator
313 { return cell_iterator (); };
314
315 idx_t
317 { return grid_properties.num_owned_nodes; };
318
319 idx_t
320 num_local_nodes () const;
321
322 idx_t
323 num_global_nodes () const;
324
325 idx_t
326 num_local_cells () const;
327
328 idx_t
329 num_global_cells () const;
330
331 idx_t
332 num_rows () const
333 { return grid_properties.numrows; };
334
335 idx_t
336 num_cols () const
337 { return grid_properties.numcols; };
338
339 double
340 hx () const
341 { return grid_properties.hx; };
342
343 double
344 hy () const
345 { return grid_properties.hy; };
346
347 idx_t
348 sub2gind (idx_t r, idx_t c) const {
349 return (r + grid_properties.numrows * c);
350 }
351
352 idx_t
353 gind2row (idx_t idx) const {
354 return (idx / grid_properties.numrows);
355 }
356
357 idx_t
358 gind2col (idx_t idx) const {
359 return (idx % grid_properties.numrows);
360 }
361
362 MPI_Comm comm;
363 int rank;
364 int size;
365
366private :
367
370
372
373};
374
375
376
377#include "quadgrid_cpp_imp.h"
378
379#endif /* QUADGRID_H */
380
quadgrid_t< std::vector< double > >::idx_t idx_t
Definition: check_shg.cpp:7
Definition: quadgrid_cpp.h:55
cell_t * data
Definition: quadgrid_cpp.h:91
cell_iterator(cell_t *_data=nullptr)
Definition: quadgrid_cpp.h:59
Definition: quadgrid_cpp.h:120
idx_t start_cell_row() const
Definition: quadgrid_cpp.h:190
idx_t end_cell_col() const
Definition: quadgrid_cpp.h:178
idx_t sub2gind(idx_t r, idx_t c) const
Definition: quadgrid_cpp.h:210
idx_t global_cell_idx
Definition: quadgrid_cpp.h:240
idx_t row_idx() const
Definition: quadgrid_cpp.h:202
idx_t num_rows() const
Definition: quadgrid_cpp.h:194
idx_t rowidx
Definition: quadgrid_cpp.h:237
idx_t colidx
Definition: quadgrid_cpp.h:238
const neighbor_iterator begin_neighbor_sweep() const
const neighbor_iterator end_neighbor_sweep() const
Definition: quadgrid_cpp.h:165
cell_t(const grid_properties_t &_gp)
Definition: quadgrid_cpp.h:130
idx_t local_cell_idx
Definition: quadgrid_cpp.h:239
idx_t end_cell_row() const
Definition: quadgrid_cpp.h:182
void reset()
Definition: quadgrid_cpp.h:225
idx_t get_global_cell_idx() const
Definition: quadgrid_cpp.h:174
bool is_ghost
Definition: quadgrid_cpp.h:232
const grid_properties_t & grid_properties
Definition: quadgrid_cpp.h:241
idx_t col_idx() const
Definition: quadgrid_cpp.h:206
neighbor_iterator end_neighbor_sweep()
Definition: quadgrid_cpp.h:161
idx_t gind2col(idx_t idx) const
Definition: quadgrid_cpp.h:220
idx_t get_local_cell_idx() const
Definition: quadgrid_cpp.h:170
idx_t gind2row(idx_t idx) const
Definition: quadgrid_cpp.h:215
double centroid(idx_t i)
neighbor_iterator begin_neighbor_sweep()
idx_t num_cols() const
Definition: quadgrid_cpp.h:198
idx_t start_cell_col() const
Definition: quadgrid_cpp.h:186
Definition: quadgrid_cpp.h:96
neighbor_iterator(cell_t *_data=nullptr, int _face_idx=-1)
Definition: quadgrid_cpp.h:103
cell_t * data
Face index in 0...3 (-1 if not defined).
Definition: quadgrid_cpp.h:115
idx_t face_idx
Definition: quadgrid_cpp.h:109
int get_face_idx()
Definition: quadgrid_cpp.h:108
Definition: quadgrid_cpp.h:15
idx_t sub2gind(idx_t r, idx_t c) const
Definition: quadgrid_cpp.h:348
idx_t gind2row(idx_t idx) const
Definition: quadgrid_cpp.h:353
cell_t current_cell
Definition: quadgrid_cpp.h:368
idx_t num_owned_nodes()
Definition: quadgrid_cpp.h:316
idx_t num_cols() const
Definition: quadgrid_cpp.h:336
double hy() const
Definition: quadgrid_cpp.h:344
double hx() const
Definition: quadgrid_cpp.h:340
quadgrid_t(const quadgrid_t &)=delete
Delete copy constructor.
MPI_Comm comm
Definition: quadgrid_cpp.h:362
idx_t num_rows() const
Definition: quadgrid_cpp.h:332
int idx_t
Definition: quadgrid_cpp.h:19
int size
Definition: quadgrid_cpp.h:364
cell_t current_neighbor
Definition: quadgrid_cpp.h:369
void from_json(const nlohmann::json &j, grid_properties_t &q)
Definition: quadgrid_cpp.h:36
quadgrid_t(const nlohmann::json &j, MPI_Comm _comm=MPI_COMM_WORLD)
Ctor that reads grid properties from a json object.
Definition: quadgrid_cpp.h:274
const cell_iterator end_cell_sweep() const
Definition: quadgrid_cpp.h:312
grid_properties_t grid_properties
Definition: quadgrid_cpp.h:371
quadgrid_t(MPI_Comm _comm=MPI_COMM_WORLD)
Default constructor, set all pointers to nullptr.
Definition: quadgrid_cpp.h:247
cell_iterator end_cell_sweep()
Definition: quadgrid_cpp.h:308
int rank
Definition: quadgrid_cpp.h:363
~quadgrid_t()=default
Destructor.
idx_t gind2col(idx_t idx) const
Definition: quadgrid_cpp.h:358
x
Definition: make_input_json.m:5
num_global_nodes
Definition: make_input_json.m:3
y
Definition: make_input_json.m:6
in grid_properties
Definition: make_input_json.m:2
Definition: quadgrid_cpp.h:23
idx_t start_cell_row
Definition: quadgrid_cpp.h:27
double hy
Definition: quadgrid_cpp.h:26
idx_t end_cell_row
Definition: quadgrid_cpp.h:28
idx_t end_cell_col
Definition: quadgrid_cpp.h:30
double hx
Definition: quadgrid_cpp.h:26
idx_t start_cell_col
Definition: quadgrid_cpp.h:29
idx_t numcols
Definition: quadgrid_cpp.h:25
idx_t numrows
Definition: quadgrid_cpp.h:24
idx_t num_owned_nodes
Definition: quadgrid_cpp.h:32
idx_t start_owned_nodes
Definition: quadgrid_cpp.h:31