-
Notifications
You must be signed in to change notification settings - Fork 10
/
dccrg_cartesian_geometry.hpp
826 lines (663 loc) · 18.9 KB
/
dccrg_cartesian_geometry.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
Dccrg class for a cartesian geometry in which cells are cubes.
Copyright 2009, 2010, 2011, 2012, 2013,
2014, 2015, 2016 Finnish Meteorological Institute
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DCCRG_CARTESIAN_GEOMETRY_HPP
#define DCCRG_CARTESIAN_GEOMETRY_HPP
#include "cassert"
#include "cmath"
#include "cstdlib"
#include "fstream"
#include "iostream"
#include "limits"
#include "cstdint"
#include "vector"
#include "mpi.h"
#include "dccrg_length.hpp"
#include "dccrg_mapping.hpp"
#include "dccrg_mpi_support.hpp"
#include "dccrg_topology.hpp"
#include "dccrg_types.hpp"
namespace dccrg {
/*!
\brief Parameters required for the Cartesian_Geometry class of dccrg.
Is given to the Cartesian_Geometry::set() function.
*/
class Cartesian_Geometry_Parameters
{
public:
std::array<double, 3>
//! starting coordinate of the grid
start,
//! length of cells of refinement level 0
level_0_cell_length;
Cartesian_Geometry_Parameters()
{
this->start[0] =
this->start[1] =
this->start[2] = 0;
this->level_0_cell_length[0] =
this->level_0_cell_length[1] =
this->level_0_cell_length[2] = 1;
}
Cartesian_Geometry_Parameters(
const std::array<double, 3>& given_start,
const std::array<double, 3>& given_level_0_cell_length
) {
this->start = given_start;
for (size_t dimension = 0; dimension < this->level_0_cell_length.size(); dimension++) {
if (given_level_0_cell_length[dimension] <= 0) {
std::cerr << "Cell length in dimension " << dimension
<< " must be > 0, but is "
<< given_level_0_cell_length[dimension]
<< std::endl;
abort();
}
}
this->level_0_cell_length = given_level_0_cell_length;
}
};
/*!
\brief Geometry class for dccrg with cubic cells
A geometry class in which the sizes of cells of
refinement level 0 are given by three floating points numbers.
*/
class Cartesian_Geometry
{
public:
/*!
Unique identifier of this geometry class, used when
storing the geometry to a file.
*/
static const int geometry_id = 1;
/*!
Parameter type that is defined by every geometry class
and used to refer to their own parameter type.
*/
typedef Cartesian_Geometry_Parameters Parameters;
/*!
Public read-only version of the grid's length in cells of refinement level 0.
\see Grid_Length
*/
const Grid_Length& length;
/*!
Public read-only version of the mapping of a
cell's ids to its size and location in the grid.
\see Mapping
*/
const Mapping& mapping;
/*!
Public read-only version of the grid's topology.
\see Grid_Topology
*/
const Grid_Topology& topology;
/*!
Creates and sets the geometry of the grid to the following:
- starting corner at (0, 0, 0)
- size of unrefined cells in each direction: 1
\see
Grid_Length Mapping Grid_Topology
*/
Cartesian_Geometry(
const Grid_Length& given_length,
const Mapping& given_mapping,
const Grid_Topology& given_topology
) :
length(given_length),
mapping(given_mapping),
topology(given_topology)
{}
/*!
Sets the geometry of the grid to the following:
- starting corner at (0, 0, 0)
- size of unrefined cells in each direction: 1
*/
~Cartesian_Geometry()
{
this->parameters.start[0] = 0;
this->parameters.start[1] = 0;
this->parameters.start[2] = 0;
this->parameters.level_0_cell_length[0] = 1;
this->parameters.level_0_cell_length[1] = 1;
this->parameters.level_0_cell_length[2] = 1;
}
/*!
Returns the parameters of the grid's geometry.
*/
const Parameters& get() const
{
return this->parameters;
}
/*!
Sets the geometry of the grid to given values.
Returns true on success. On failure returns false
and has no effect.
*/
bool set(const Parameters& given_parameters)
{
for (size_t
dimension = 0;
dimension < this->parameters.level_0_cell_length.size();
dimension++
) {
if (given_parameters.level_0_cell_length[dimension] <= 0) {
std::cerr << "Cell length in dimension " << dimension
<< " must be > 0, but is "
<< given_parameters.level_0_cell_length[dimension]
<< std::endl;
return false;
}
}
// TODO: check that all cell coordinates fit into a double
this->parameters = given_parameters;
return true;
}
/*!
Sets the same geometry as in the given one.
*/
bool set(const Cartesian_Geometry& other)
{
return this->set(other.get());
}
/*!
Returns the starting corner of the grid.
Starting corner is defined as the corner
with minimum value of the coordinate in each
dimension.
*/
std::array<double, 3> get_start() const
{
const std::array<double, 3> ret_val = {{
this->parameters.start[0],
this->parameters.start[1],
this->parameters.start[2]
}};
return ret_val;
}
/*!
Returns the end corner of the grid.
End corner is defined as the corner with
maximum value of the coordinate in each
dimension.
*/
std::array<double, 3> get_end() const
{
// in cells of refinement level 0
const std::array<uint64_t, 3> length = this->length.get();
const std::array<double, 3>
grid_start = this->get_start(),
ret_val = {{
grid_start[0]
+ double(length[0])
* this->parameters.level_0_cell_length[0],
grid_start[1]
+ double(length[1])
* this->parameters.level_0_cell_length[1],
grid_start[2]
+ double(length[2])
* this->parameters.level_0_cell_length[2],
}};
return ret_val;
}
/*!
Returns the length of cells of refinement level 0.
*/
std::array<double, 3> get_level_0_cell_length() const
{
return this->parameters.level_0_cell_length;
}
/*!
Returns the length of given cell.
A quiet NaN is returned if the given error_cell,
or the given cell cannot exist in the grid.
*/
std::array<double, 3> get_length(const uint64_t cell) const
{
const int refinement_level = this->mapping.get_refinement_level(cell);
if (cell == error_cell
|| refinement_level < 0
|| refinement_level > this->mapping.get_maximum_refinement_level()) {
const std::array<double, 3> error_val = {{
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN()
}};
return error_val;
}
const double scaling_factor = 1.0 / double(uint64_t(1) << refinement_level);
const std::array<double, 3> ret_val = {{
this->parameters.level_0_cell_length[0] * scaling_factor,
this->parameters.level_0_cell_length[1] * scaling_factor,
this->parameters.level_0_cell_length[2] * scaling_factor
}};
return ret_val;
}
/*!
Returns the center of given cell.
A quiet NaN is returned if given error_cell,
or the given cell cannot exist in the grid.
*/
std::array<double, 3> get_center(const uint64_t cell) const
{
const int refinement_level = this->mapping.get_refinement_level(cell);
if (cell == error_cell
|| refinement_level < 0
|| refinement_level > this->mapping.get_maximum_refinement_level()) {
const std::array<double, 3> error_val = {{
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN()
}};
return error_val;
}
const Types<3>::indices_t indices = this->mapping.get_indices(cell);
const int max_ref_lvl = this->mapping.get_maximum_refinement_level();
const std::array<double, 3>
grid_start = this->get_start(),
cell_length = this->get_length(cell),
level_0_cell_length = this->get_level_0_cell_length(),
ret_val = {{
grid_start[0]
+ double(indices[0])
* level_0_cell_length[0]
/ double(uint64_t(1) << max_ref_lvl)
+ cell_length[0] / 2,
grid_start[1]
+ double(indices[1])
* level_0_cell_length[1]
/ double(uint64_t(1) << max_ref_lvl)
+ cell_length[1] / 2,
grid_start[2]
+ double(indices[2])
* level_0_cell_length[2]
/ double(uint64_t(1) << max_ref_lvl)
+ cell_length[2] / 2
}};
return ret_val;
}
/*!
Returns the cell's corner closest to starting corner of the grid.
In other words if the cell occupies the range:
\verbatim
[x1..x2],
[y1..y2],
...
\endverbatim
returns (x1, y1, ...).
*/
std::array<double, 3> get_min(const uint64_t cell) const
{
const std::array<double, 3>
center = this->get_center(cell),
length = this->get_length(cell),
ret_val = {{
center[0] - length[0] / 2,
center[1] - length[1] / 2,
center[2] - length[2] / 2
}};
return ret_val;
}
/*!
Returns the cell's corner furthest from the starting corner of the grid.
In other words if the cell occupies the range:
\verbatim
[x1..x2],
[y1..y2],
...
\endverbatim
returns (x2, y2, ...).
*/
std::array<double, 3> get_max(const uint64_t cell) const
{
const std::array<double, 3>
center = this->get_center(cell),
length = this->get_length(cell),
ret_val = {{
center[0] + length[0] / 2,
center[1] + length[1] / 2,
center[2] + length[2] / 2
}};
return ret_val;
}
/*!
Returns the center of a cell of given refinement level at given index.
A quiet NaN is returned if given error_cell,
or the given cell cannot exist in the grid.
*/
std::array<double, 3> get_center(
const int refinement_level,
const Types<3>::indices_t index
) const {
const std::array<double, 3> error_val = {{
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN()
}};
if (refinement_level < 0
|| refinement_level > this->mapping.get_maximum_refinement_level()) {
return error_val;
}
const uint64_t index_scaling_factor
= uint64_t(1) << this->mapping.get_maximum_refinement_level();
const Types<3>::indices_t max_index = {{
this->length.get()[0] * index_scaling_factor,
this->length.get()[1] * index_scaling_factor,
this->length.get()[2] * index_scaling_factor
}};
if (
index[0] > max_index[0]
|| index[1] > max_index[1]
|| index[2] > max_index[2]
) {
return error_val;
}
const double
coordinate_scaling_factor = 1.0 / double(index_scaling_factor),
cell_offset_scaling_factor = 1.0 / double(uint64_t(1) << refinement_level) / 2;
const std::array<double, 3>
grid_start = this->get_start(),
ret_val = {{
grid_start[0]
+ double(index[0])
* this->parameters.level_0_cell_length[0]
* coordinate_scaling_factor
+ this->parameters.level_0_cell_length[0]
* cell_offset_scaling_factor,
grid_start[1]
+ double(index[1])
* this->parameters.level_0_cell_length[1]
* coordinate_scaling_factor
+ this->parameters.level_0_cell_length[1]
* cell_offset_scaling_factor,
grid_start[2]
+ double(index[2])
* this->parameters.level_0_cell_length[2]
* coordinate_scaling_factor
+ this->parameters.level_0_cell_length[2]
* cell_offset_scaling_factor
}};
return ret_val;
}
/*!
Returns a cell of given refinement level at given location.
Returns error_cell if given a location outside of the current
grid either in coordinate or refinement level.
*/
uint64_t get_cell(
const int refinement_level,
const std::array<double, 3>& coordinate
) const {
if (refinement_level < 0
|| refinement_level > this->mapping.get_maximum_refinement_level()) {
return error_cell;
}
const Types<3>::indices_t indices = this->get_indices(coordinate);
return this->mapping.get_cell_from_indices(indices, refinement_level);
}
/*!
Returns the real value of given coordinate in this geometry.
For each dimension:
Returns given coordinate if it is inside this geometry.
Returns a quiet NaN if this geometry is not periodic in
the dimension and given coordinate is outside of the geometry.
If this geometry is periodic in the dimension returns a value
inside the geometry that is at the same location in the
geometry as given coordinate.
*/
std::array<double, 3> get_real_coordinate(
const std::array<double, 3>& given_coordinate
) const {
const std::array<double, 3>
start = this->get_start(),
end = this->get_end();
std::array<double, 3> ret_val = {{
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN()
}};
for (size_t dimension = 0; dimension < given_coordinate.size(); dimension++) {
if (given_coordinate[dimension] >= start[dimension]
&& given_coordinate[dimension] <= end[dimension]) {
ret_val[dimension] = given_coordinate[dimension];
} else if (this->topology.is_periodic(dimension)) {
const double grid_length = end[dimension] - start[dimension];
if (given_coordinate[dimension] < start[dimension]) {
const double distance = start[dimension] - given_coordinate[dimension];
ret_val[dimension]
= given_coordinate[dimension]
+ grid_length * ceil(distance / grid_length);
} else {
const double distance = given_coordinate[dimension] - end[dimension];
ret_val[dimension]
= given_coordinate[dimension]
- grid_length * ceil(distance / grid_length);
}
}
}
return ret_val;
}
/*!
Returns the index (starting from 0) of given coordinate in each dimension.
Returns error_index for coordinates outside of the grid in a particular
dimension if the grid is not periodic in that dimension.
*/
Types<3>::indices_t get_indices(const std::array<double, 3>& given_coordinate) const
{
Types<3>::indices_t ret_val = {{
error_index,
error_index,
error_index
}};
const std::array<double, 3>
grid_start = this->get_start(),
grid_end = this->get_end(),
coordinate = this->get_real_coordinate(given_coordinate),
level_0_cell_length = this->get_level_0_cell_length();
for (size_t dimension = 0; dimension < given_coordinate.size(); dimension++) {
if (coordinate[dimension] >= grid_start[dimension]
&& coordinate[dimension] <= grid_end[dimension]) {
ret_val[dimension] = uint64_t(
floor(
(coordinate[dimension] - grid_start[dimension])
/ (level_0_cell_length[dimension]
/ double(uint64_t(1) << this->mapping.get_maximum_refinement_level())
)
)
);
}
}
return ret_val;
}
/*!
Writes the geometry into given open file starting at given offset.
Returns true on success, false otherwise.
The number of bytes written by this function can be obtained
from geometry_data_size().
*/
bool write(MPI_File file, MPI_Offset offset) const
{
int ret_val = -1;
const int temp_id = Cartesian_Geometry::geometry_id;
ret_val = MPI_File_write_at(
file,
offset,
(void*) &temp_id,
1,
MPI_INT,
MPI_STATUS_IGNORE
);
if (ret_val != MPI_SUCCESS) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't write geometry id to file: " << Error_String()(ret_val)
<< std::endl;
return false;
}
offset += sizeof(int);
ret_val = MPI_File_write_at(
file,
offset,
(void*) this->parameters.start.data(),
3,
MPI_DOUBLE,
MPI_STATUS_IGNORE
);
if (ret_val != MPI_SUCCESS) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't write geometry start to file: " << Error_String()(ret_val)
<< std::endl;
return false;
}
offset += 3 * sizeof(double);
ret_val = MPI_File_write_at(
file,
offset,
(void*) this->parameters.level_0_cell_length.data(),
3,
MPI_DOUBLE,
MPI_STATUS_IGNORE
);
if (ret_val != MPI_SUCCESS) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't write level 0 cell length to file: " << Error_String()(ret_val)
<< std::endl;
return false;
}
return true;
}
/*!
Reads the geometry from given open file starting at given offset.
Returns true on success, false otherwise.
Unless you know what you're doing must be called by all processes
with identical arguments.
*/
bool read(MPI_File file, MPI_Offset offset)
{
int
read_geometry_id = Cartesian_Geometry::geometry_id + 1,
ret_val = -1;
ret_val = MPI_File_read_at(
file,
offset,
(void*) &read_geometry_id,
1,
MPI_INT,
MPI_STATUS_IGNORE
);
if (ret_val != MPI_SUCCESS) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't read geometry id from file: " << Error_String()(ret_val)
<< std::endl;
return false;
}
offset += sizeof(int);
// TODO: don't error out if given No_Geometry
if (read_geometry_id != Cartesian_Geometry::geometry_id) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Wrong geometry: " << read_geometry_id
<< ", should be " << Cartesian_Geometry::geometry_id
<< std::endl;
return false;
}
Parameters read_parameters;
ret_val = MPI_File_read_at(
file,
offset,
(void*) read_parameters.start.data(),
3,
MPI_DOUBLE,
MPI_STATUS_IGNORE
);
if (ret_val != MPI_SUCCESS) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't read geometry start from file: " << Error_String()(ret_val)
<< std::endl;
return false;
}
offset += 3 * sizeof(double);
ret_val = MPI_File_read_at(
file,
offset,
(void*) read_parameters.level_0_cell_length.data(),
3,
MPI_DOUBLE,
MPI_STATUS_IGNORE
);
if (ret_val != MPI_SUCCESS) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't read level 0 cell length from file: " << Error_String()(ret_val)
<< std::endl;
return false;
}
if (!this->set(read_parameters)) {
return false;
}
return true;
}
/*! As read(MPI_File...) but for ifstream
*/
bool read(std::ifstream& file)
{
int read_geometry_id = Cartesian_Geometry::geometry_id + 1;
file.read(
reinterpret_cast<char*>(&read_geometry_id),
sizeof read_geometry_id
);
if (not file.good()) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't read geometry id" << std::endl;
return false;
}
// TODO: don't error out if given No_Geometry
if (read_geometry_id != Cartesian_Geometry::geometry_id) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Wrong geometry: " << read_geometry_id
<< ", should be " << Cartesian_Geometry::geometry_id
<< std::endl;
return false;
}
Parameters read_parameters;
file.read(
reinterpret_cast<char*>(read_parameters.start.data()),
3 * sizeof(double)
);
if (not file.good()) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't read geometry start" << std::endl;
return false;
}
file.read(
reinterpret_cast<char*>(read_parameters.level_0_cell_length.data()),
3 * sizeof(double)
);
if (not file.good()) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't read level 0 cell lengths" << std::endl;
return false;
}
if (!this->set(read_parameters)) {
return false;
}
return true;
}
/*!
Returns the number of bytes that will be required / was required for geometry data.
*/
size_t data_size() const
{
return sizeof(int) + 6 * sizeof(double);
}
private:
Parameters parameters;
}; // class
} // namespace
#endif