quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
quadgrid_cpp_imp.h
Go to the documentation of this file.
1
2template <class T>
8
9
10
11template <class T>
17
18
19
20template <class T>
21void
23 double hx, double hy) {
24 grid_properties.numrows = numrows;
25 grid_properties.numcols = numcols;
28 grid_properties.start_cell_row = 0;
29 grid_properties.end_cell_row = numrows - 1;
30 grid_properties.start_cell_col = 0;
31 grid_properties.end_cell_col = numcols - 1;
32 grid_properties.start_owned_nodes = 0;
33 grid_properties.num_owned_nodes = (numrows+1)*(numcols+1);
34}
35
36
37
38template <class T>
39void
41 static idx_t tmp;
42 if (data != nullptr) {
43 tmp = data->rowidx + data->num_rows () * data->colidx + 1;
44 if (tmp > (data->end_cell_row () +
45 data->num_rows () * data->end_cell_col ()))
46 data = nullptr;
47 else {
48 data->rowidx = tmp % data->num_rows ();
49 data->colidx = tmp / data->num_rows ();
50 data->global_cell_idx = tmp;
51 data->local_cell_idx = tmp -
52 (data->start_cell_row () +
53 data->num_rows () * data->start_cell_col ());
54 }
55 }
56};
57
58template <class T>
59const typename quadgrid_t<T>::cell_t&
61 assert (tmp <= (current_cell.end_cell_row () +
62 current_cell.num_rows () * current_cell.end_cell_col ()));
63 current_cell.rowidx = tmp % current_cell.num_rows ();
64 current_cell.colidx = tmp / current_cell.num_rows ();
65 current_cell.global_cell_idx = tmp;
66 current_cell.local_cell_idx = tmp -
67 (current_cell.start_cell_row () +
68 current_cell.num_rows () * current_cell.start_cell_col ());
69 return current_cell;
70};
71
72
73
74template <class T>
75void
77 static idx_t tmp = 0;
78 if (data != nullptr) {
79 tmp = ++face_idx;
81 data = nullptr;
82 face_idx = -1;
83 }
84 else {
85 switch (face_idx) {
86 case 0 :
87 if (data->rowidx == 0)
88 face_idx++;
89 else {
90 data->rowidx = tmp % data->num_rows ();
91 data->colidx = tmp / data->num_rows ();
92 data->global_cell_idx = tmp;
93 data->local_cell_idx = tmp -
94 (data->start_cell_row () +
95 data->num_rows () * data->start_cell_col ());
96 }
97 case 1 :
98 if (data->rowidx == data->num_rows () - 1)
99 face_idx++;
100 else {
101
102
103 }
104 case 2 :
105 if (data->colidx == 1)
106 face_idx++;
107 else {
108
109
110 }
111 case 3 :
112 if (data->colidx == data->num_cols () - 1) {
113 face_idx = -1;
114 data = nullptr;
115 }
116 else {
117
118
119 }
120 }
121 data->rowidx = tmp % data->num_rows ();
122 data->colidx = tmp / data->num_rows ();
123 data->global_cell_idx = tmp;
124 data->local_cell_idx = tmp -x
125 (data->start_cell_row () +
126 data->num_rows () * data->start_cell_col ());
127 }
128 }
129};
130
131
132
133
134template <class T>
137 return grid_properties.numrows*grid_properties.numcols;
138}
139
140
141
142
143template <class T>
146 return grid_properties.numrows*grid_properties.numcols;
147}
148
149
150
151
152template <class T>
155 return (grid_properties.numrows+1)*(grid_properties.numcols+1);
156}
157
158template <class T>
161 return (grid_properties.numrows+1)*(grid_properties.numcols+1);
162}
163
164
165template <class T>
168 static idx_t glob = 0;
169 glob = gt (inode);
170 // should check that inode < 4 in an efficient way
171 if (glob < grid_properties.start_owned_nodes
172 || glob >= (grid_properties.start_owned_nodes
173 + grid_properties.num_owned_nodes))
174 return (glob);
175 else
176 return (glob - grid_properties.start_owned_nodes);
177}
178
179
180//-----------------------------------
181//
182// Numbering of nodes and edges :
183//
184// 1
185// |
186// V
187// 1 -> O---------------O <- 3
188// | |
189// | |
190// 2 -> | | <- 3
191// | |
192// | |
193// 0 -> O---------------O <- 2
194// ^
195// |
196// 0
197//
198//-----------------------------------
199template <class T>
200double
202 typename quadgrid_t<T>::idx_t inode) const {
203
204 return quadgrid_t::p (idir, inode, col_idx (), row_idx (),
206
207}
208
209
210template <class T>
213 // should check that iedge < 4 in an efficient way
214
215 if (row_idx () == 0 && iedge == 0)
216 return 0;
217
218 if (row_idx () == num_rows () - 1 && iedge == 1)
219 return 1;
220
221 if (col_idx () == 0 && iedge == 2)
222 return 2;
223
224 if (col_idx () == num_cols () - 1 && iedge == 3)
225 return 3;
226
227 return (NOT_ON_BOUNDARY);
228}
229
230
231template <class T>
232double
233quadgrid_t<T>::cell_t::shp (double x, double y, idx_t inode) const {
234
235 switch (inode) {
236 case 3 :
237 case 2 :
238 case 1 :
239 case 0 :
240 return quadgrid_t::shp (x, y, inode, col_idx (), row_idx (), grid_properties.hx, grid_properties.hy);
241 break;
242 default :
243 throw std::out_of_range ("inode must be in range 0..3");
244 }
245};
246
247/*
248template <class T>
249 double
250 quadgrid_t<T>::cell_t::shp (double x, double y, idx_t inode) const {
251 switch (inode) {
252 case 3 :
253 return ((x - p(0,0))/grid_properties.hx *
254 (y - p(1,0))/grid_properties.hy);
255 break;
256 case 2 :
257 return ((x - p(0,0))/grid_properties.hx *
258 (1. - (y - p(1,0))/grid_properties.hy));
259 break;
260 case 1 :
261 return ((1. - (x - p(0,0))/grid_properties.hx) *
262 (y - p(1,0))/grid_properties.hy);
263 break;
264 case 0 :
265 return ((1. - (x - p(0,0))/grid_properties.hx) *
266 (1. - (y - p(1,0))/grid_properties.hy));
267 break;
268 default :
269 throw std::out_of_range ("inode must be in range 0..3");
270 }
271};
272*/
273
274template <class T>
275double
276quadgrid_t<T>::cell_t::shg (double x, double y, idx_t idir, idx_t inode) const {
277 switch (inode) {
278 case 3 :
279 case 2 :
280 case 1 :
281 case 0 :
282 return quadgrid_t::shg (x, y, idir, inode, col_idx (), row_idx (), grid_properties.hx, grid_properties.hy);
283 break;
284 default :
285 throw std::out_of_range ("inode must be in range 0..3, idir must be either 0 or 1");
286 }
287 return 0.;
288};
289
290// template <class T>
291// double
292// quadgrid_t<T>::cell_t::shg (double x, double y, idx_t idir, idx_t inode) const {
293// switch (inode) {
294// case 3 :
295// if (idir == 0) {
296// return ((1. / grid_properties.hx) *
297// ((y - p(1,0)) / grid_properties.hy));
298// }
299// else if (idir == 1) {
300// return (((x - p(0,0)) / grid_properties.hx) *
301// (1. / grid_properties.hy));
302// }
303// break;
304// case 2 :
305// if (idir == 0) {
306// return ((1. / grid_properties.hx) *
307// ((1. - (y - p(1,0)) / grid_properties.hy)));
308// }
309// else if (idir == 1) {
310// return (((x - p(0,0)) / grid_properties.hx) *
311// (- 1. / grid_properties.hy));
312// }
313// break;
314// case 1 :
315// if (idir == 0) {
316// return ((- 1. / grid_properties.hx) *
317// ((y - p(1,0)) / grid_properties.hy));
318// }
319// else if (idir == 1) {
320// return ((1. - (x - p(0,0)) / grid_properties.hx) *
321// (1. / grid_properties.hy));
322// }
323// break;
324// case 0 :
325// if (idir == 0) {
326// return ((- 1. /grid_properties.hx) *
327// (1. - (y - p(1,0)) / grid_properties.hy));
328// }
329// else if (idir == 1) {
330// return ((1. - (x - p(0,0))/grid_properties.hx) *
331// (- 1. / grid_properties.hy));
332// }
333// break;
334// default :
335// throw std::out_of_range ("inode must be in range 0..3, idir must be either 0 or 1");
336// }
337// return 0.;
338// };
339
340
341
342
343template <class T>
344void
345quadgrid_t<T>::vtk_export (const char *filename,
346 const std::map<std::string, T> & f) const {
347
348 std::ofstream ofs (filename, std::ofstream::out);
349
350 // This is the XML format of a VTS file to write :
351
352 ofs <<
353 "<VTKFile type=\"StructuredGrid\" version=\"StructuredGrid\" byte_order=\"LittleEndian\">\n\
354 <StructuredGrid WholeExtent=\"0 " << num_rows() << " 0 " << num_cols() << " 0 0\">\n \
355 <Piece Extent=\"0 " << num_rows() << " 0 " << num_cols() << " 0 0\">\n";
356
357 ofs << " <PointData Scalars=\"";
358 for (auto const & ii : f) {
359 ofs << ii.first << ",";
360 }
361 ofs << "\">\n";
362
363 for (auto const & ii : f) {
364 ofs << " <DataArray type=\"Float64\" Name=\"" << ii.first <<"\" format=\"ascii\">\n ";
365 for (auto const & jj : ii.second) {
366 ofs << jj << " ";
367 }
368 ofs << std::endl << " </DataArray>" << std::endl;
369 }
370
371 ofs << " </PointData>\n";
372
373 ofs << " <Points>\n <DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">\n";
374 for (idx_t ii = 0; ii <= num_cols(); ++ii) {
375 ofs << " ";
376 for (idx_t jj = 0; jj <= num_rows(); ++jj) {
377 ofs << std::setprecision(16) << hx()*ii << " " << hy()*jj << " 0 ";
378 }
379 ofs << std::endl;
380 }
381 ofs << " </DataArray>\n </Points>\n";
382
383
384 ofs <<
385 " </Piece>\n\
386 </StructuredGrid>\n\
387</VTKFile>\n";
388
389 ofs.close ();
390}
391
392
393template <class T>
394void
396(const char *filename,
397 std::map<std::string, T> const & vars) const {
398
399 std::ofstream os (filename, std::ofstream::out);
400
401 os << "# name: p" << std::endl
402 << "# type: matrix" << std::endl
403 << "# rows: 2" << std::endl
404 << "# columns: " << num_owned_nodes () << std::endl;
405
406 for (idx_t jj = 0; jj < num_cols () + 1; ++jj) {
407 for (idx_t ii = 0; ii < num_rows () + 1; ++ii) {
408 os << std::setprecision(16) << jj*hx() << " ";
409 }
410 }
411 os << std::endl;
412
413 for (idx_t jj = 0; jj < num_cols () + 1; ++jj) {
414 for (idx_t ii = 0; ii < num_rows () + 1; ++ii) {
415 os << std::setprecision(16) << ii*hy() << " ";
416 }
417 }
418 os << std::endl;
419
420 os << "# name: t" << std::endl
421 << "# type: matrix" << std::endl
422 << "# rows: 4" << std::endl
423 << "# columns: " << num_local_cells () << std::endl;
424 for (auto inode = 0;
425 inode < quadgrid_t<std::vector<double>>::cell_t::nodes_per_cell;
426 ++inode) {
427 for (auto icell = begin_cell_sweep ();
428 icell != end_cell_sweep (); ++icell) {
429 os << std::setprecision(16) << icell->gt(inode) << " ";
430 }
431 os << std::endl;
432 }
433
434
435 os << "# name: vars" << std::endl
436 << "# type: scalar struct" << std::endl
437 << "# ndims: 2" << std::endl
438 << "1 1" << std::endl
439 << "# length: " << vars.size () << std::endl;
440
441
442 for (auto const & ii : vars) {
443 os << "# name: " << ii.first << std::endl
444 << "# type: matrix" << std::endl
445 << "# rows: 1" << std::endl
446 << "# columns: " << ii.second.size () << std::endl;
447 for (auto const & kk : ii.second) {
448 os << std::setprecision(16) << kk << " ";
449 }
450 os << std::endl;
451 }
452 os << std::endl;
453
454 os.close ();
455}
456
457
Definition quadgrid_cpp.h:205
cell_t * data
Definition quadgrid_cpp.h:241
void operator++()
Definition quadgrid_cpp_imp.h:40
Definition quadgrid_cpp.h:270
idx_t row_idx() const
Definition quadgrid_cpp.h:367
double p(idx_t i, idx_t j) const
Definition quadgrid_cpp_imp.h:201
static constexpr idx_t edges_per_cell
Definition quadgrid_cpp.h:278
const grid_properties_t & grid_properties
Definition quadgrid_cpp.h:407
idx_t col_idx() const
Definition quadgrid_cpp.h:371
idx_t e(idx_t i) const
Definition quadgrid_cpp_imp.h:212
double shp(double x, double y, idx_t inode) const
Definition quadgrid_cpp_imp.h:233
double shg(double x, double y, idx_t idir, idx_t inode) const
Definition quadgrid_cpp_imp.h:276
static constexpr idx_t nodes_per_cell
Definition quadgrid_cpp.h:277
idx_t t(idx_t i) const
Definition quadgrid_cpp_imp.h:167
void operator++()
Definition quadgrid_cpp_imp.h:76
cell_t * data
Face index in 0...3 (-1 if not defined).
Definition quadgrid_cpp.h:265
idx_t face_idx
Definition quadgrid_cpp.h:262
static double shp(double x, double y, idx_t inode, idx_t c, idx_t r, double hx, double hy)
Definition quadgrid_cpp.h:134
const cell_t & operator[](idx_t tmp) const
Definition quadgrid_cpp_imp.h:60
idx_t num_local_cells() const
Definition quadgrid_cpp_imp.h:136
cell_t current_cell
Definition quadgrid_cpp.h:537
static double shg(double x, double y, idx_t idir, idx_t inode, idx_t c, idx_t r, double hx, double hy)
Definition quadgrid_cpp.h:157
static idx_t gt(idx_t inode, idx_t cidx, idx_t ridx, idx_t numrows)
Definition quadgrid_cpp.h:73
idx_t num_owned_nodes()
Definition quadgrid_cpp.h:482
idx_t num_cols() const
Definition quadgrid_cpp.h:502
static double p(idx_t idir, idx_t inode, idx_t colidx, idx_t rowidx, double hx, double hy)
Definition quadgrid_cpp.h:117
double hy() const
Definition quadgrid_cpp.h:510
void vtk_export(const char *filename, const std::map< std::string, distributed_vector > &f) const
Definition quadgrid_cpp_imp.h:345
void set_sizes(idx_t numrows, idx_t numcols, double hx, double hy)
Definition quadgrid_cpp_imp.h:22
double hx() const
Definition quadgrid_cpp.h:506
idx_t num_local_nodes() const
Definition quadgrid_cpp_imp.h:160
idx_t num_global_nodes() const
Definition quadgrid_cpp_imp.h:154
idx_t num_rows() const
Definition quadgrid_cpp.h:498
int idx_t
Definition quadgrid_cpp.h:28
cell_iterator begin_cell_sweep()
Definition quadgrid_cpp_imp.h:4
void octave_ascii_export(const char *filename, const std::map< std::string, distributed_vector > &f) const
Definition quadgrid_cpp_imp.h:396
grid_properties_t grid_properties
Definition quadgrid_cpp.h:540
cell_iterator end_cell_sweep()
Definition quadgrid_cpp.h:474
idx_t num_global_cells() const
Definition quadgrid_cpp_imp.h:145
x
Definition make_input_json.m:5
y
Definition make_input_json.m:6