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