-
Notifications
You must be signed in to change notification settings - Fork 10
/
dccrg_length.hpp
147 lines (111 loc) · 3.02 KB
/
dccrg_length.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
/*
Dccrg class for storing the grid length and related operations.
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_LENGTH_HPP
#define DCCRG_LENGTH_HPP
#include "array"
#include "iostream"
#include "stdint.h"
namespace dccrg {
/*!
\brief Length of the grid in cells of refinement level 0 and related operations.
*/
class Grid_Length
{
public:
/*!
Represents the length of the grid in cells of
refinement level 0 in each dimension.
*/
typedef std::array<uint64_t, 3> type;
/*!
Sets the size of the grid in cells of refinement level 0 to 1.
*/
Grid_Length()
{
this->length[0] =
this->length[1] =
this->length[2] = 1;
}
/*!
Creates a grid of given length in cells of refinement level 0.
*/
Grid_Length(const Grid_Length::type& given_length)
{
if (!this->set(given_length)) {
abort();
}
}
/*!
Sets the grid to a default constructed state.
*/
~Grid_Length()
{
this->length[0] =
this->length[1] =
this->length[2] = 1;
}
/*!
The length of the grid in cells of refinement level 0.
*/
const Grid_Length::type& get() const
{
return this->length;
}
/*!
Sets the length of the grid in cells of refinement level 0.
Returns true if successful, probably invalidating all previous cell
information (cell numbers, indices, etc.)
Returns false if unsuccessful and in that case has no effect.
Automatically maximizes the maximum refinement level of the grid.
*/
bool set(const Grid_Length::type& given_length)
{
if (
given_length[0] == 0
|| given_length[1] == 0
|| given_length[2] == 0
) {
std::cerr << __FILE__ << ":" << __LINE__
<< "All lengths given must be > 0 but are "
<< given_length[0] << " "
<< given_length[1] << " "
<< given_length[2]
<< std::endl;
return false;
}
const Grid_Length::type old_length = this->length;
this->length = given_length;
const double max_number_of_initial_cells
= double(this->length[0])
+ double(this->length[1])
+ double(this->length[2]);
if (max_number_of_initial_cells > double(~uint64_t(0))) {
std::cerr
<< "Grid would have too many cells of refinement level 0 for uint64_t: "
<< max_number_of_initial_cells
<< " (length_x, length_y, length_z: "
<< this->length[0] << " " << this->length[1] << " " << this->length[2]
<< ")"
<< std::endl;
this->length = old_length;
return false;
}
return true;
}
private:
Grid_Length::type length;
};
} // namespace
#endif