Newer
Older
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
#pragma once
// [grid-id.h]: grid data identifier
//
// -------------------------------------------------------------------------------------------- //
#include "nse-sys.h"
#include "mpi-com.h"
#include <string.h>
#ifdef USE_CXX_11
#include <initializer_list>
#endif
//#define _USE_DEPRECATED_WST_FORMAT
namespace nse
{
enum maskType {
solidCell = 0, fluidCell = 1, externalCell = 2
};
template< typename T >
class GridId {
public:
GridId();
GridId(const GridId& id);
~GridId();
void init(); // initialize id
// set:
void set_grid_type(const int grid_type);
void set_dim_num(const int ndim);
void set_domain_dim(const int dim, const T x, const T length);
void set_grid_dim(const int dim, const int nx, const int gcx);
void reset_data_type_size(); // to: [sizeof(T)]
// set specs:
#ifdef USE_CXX_11
void set_domain_specs(const std::initializer_list<T> specs);
void set_grid_specs(const std::initializer_list<int> specs);
#else
void set_domain_specs(int nspecs, const T* specs);
void set_grid_specs(int nspecs, const int* specs);
#endif
// header:
int key() const;
int grid_type() const;
int dim_num() const;
int data_type_size() const;
// get grid, domain by dim:
void domain_dim(const int dim, T* x, T* length) const;
void grid_dim(const int dim, int* nx, int* gcx) const;
// get specs:
T domain_spec(int idx) const;
int grid_spec(int idx) const;
// check id based on header values and input num dims
bool check(const int ndim) const;
// broadcast GridId object on communicator
void mpi_broadcast(const int host, const MPI_Comm comm);
// ------------------------------------------------------------------------------------ //
// static constants
// ------------------------------------------------------------------------------------ //
static const int max_dim = 3; // using <3> to comply with WST-format
static const int hsize = 4;
static const int dsize = 24;
static const int gsize = 24;
static const int id_byte_size =
hsize * sizeof(int) + dsize * sizeof(T) + gsize * sizeof(int);
// static constants (read 3D data only), switch: comply with WST-format
// ------------------------------------------------------------------------------------ //
#ifndef _USE_DEPRECATED_WST_FORMAT
static const int hsize_r3d = hsize;
static const int dsize_r3d = dsize;
static const int gsize_r3d = gsize;
#else
static const int hsize_r3d = 4;
static const int dsize_r3d = 7;
static const int gsize_r3d = 6;
#endif
static const int id_byte_size_r3d =
hsize_r3d * sizeof(int) + dsize_r3d * sizeof(T) + gsize_r3d * sizeof(int);
public:
// data triple [header,domain,grid]
// ------------------------------------------------------------------------------------ //
int header[hsize];
T domain[dsize];
int grid[gsize];
};
}
// Implementation
// -------------------------------------------------------------------------------------------- //
template< typename T >
nse::GridId<T>::GridId()
{
memset(header, 0, GridId<T>::hsize * sizeof(int));
memset(domain, 0, GridId<T>::dsize * sizeof(T));
memset(grid, 0, GridId<T>::gsize * sizeof(int));
}
template< typename T >
nse::GridId<T>::GridId(const GridId& id)
{
memcpy(header, id.header, GridId<T>::hsize * sizeof(int));
memcpy(domain, id.domain, GridId<T>::dsize * sizeof(T));
memcpy(grid, id.grid, GridId<T>::gsize * sizeof(int));
}
template< typename T >
nse::GridId<T>::~GridId() { }
template< typename T >
void nse::GridId<T>::init()
{
memset(header, 0, GridId<T>::hsize * sizeof(int));
memset(domain, 0, GridId<T>::dsize * sizeof(T));
memset(grid, 0, GridId<T>::gsize * sizeof(int));
header[0] = 'n' + 's' + 'e'; // file identifier //
header[3] = sizeof(T); // data type size //
}
template< typename T >
void nse::GridId<T>::set_grid_type(const int grid_type)
{
if (grid_type >= 0)
header[1] = grid_type;
}
template< typename T >
void nse::GridId<T>::set_dim_num(const int ndim)
{
if ((ndim <= 0) || (ndim > max_dim)) return;
header[2] = ndim;
}
template< typename T >
void nse::GridId<T>::set_domain_dim(const int dim, const T x, const T length)
{
if ((dim <= 0) || (dim > dim_num())) return;
domain[dim - 1] = x;
domain[max_dim + dim - 1] = length;
}
template< typename T >
void nse::GridId<T>::set_grid_dim(const int dim, const int nx, const int gcx)
{
if ((dim <= 0) || (dim > dim_num())) return;
grid[dim - 1] = nx;
grid[max_dim + dim - 1] = gcx;
}
template< typename T >
void nse::GridId<T>::reset_data_type_size()
{
header[3] = sizeof(T);
}
#ifdef USE_CXX_11
template< typename T >
void nse::GridId<T>::set_domain_specs(const std::initializer_list<T> specs)
{
int ptr = 2 * max_dim;
for (auto sp : specs) {
domain[ptr] = sp;
ptr++;
if (ptr >= GridId<T>::dsize) break;
}
}
template< typename T >
void nse::GridId<T>::set_grid_specs(const std::initializer_list<int> specs)
{
int ptr = 2 * max_dim;
for (auto sp : specs) {
grid[ptr] = sp;
ptr++;
if (ptr >= GridId<T>::gsize) break;
}
}
#else
template< typename T >
void nse::GridId<T>::set_domain_specs(int nspecs, const T* specs)
{
const int ptr = 2 * max_dim;
for (int k = 0; k < nspecs; k++) {
if (ptr + k >= GridId<T>::dsize) break;
domain[ptr + k] = specs[k];
}
}
template< typename T >
void nse::GridId<T>::set_grid_specs(int nspecs, const int* specs)
{
const int ptr = 2 * max_dim;
for (int k = 0; k < nspecs; k++) {
if (ptr + k >= GridId<T>::gsize) break;
grid[ptr + k] = specs[k];
}
}
#endif
template< typename T >
inline int nse::GridId<T>::key() const { return header[0]; }
template< typename T >
inline int nse::GridId<T>::grid_type() const { return header[1]; }
template< typename T >
inline int nse::GridId<T>::dim_num() const { return header[2]; }
template< typename T >
inline int nse::GridId<T>::data_type_size() const { return header[3]; }
template< typename T >
void nse::GridId<T>::domain_dim(const int dim, T* x, T* length) const
{
if ((dim <= 0) || (dim > dim_num())) return;
(*x) = domain[dim - 1];
(*length) = domain[max_dim + dim - 1];
}
template< typename T >
void nse::GridId<T>::grid_dim(const int dim, int* nx, int* gcx) const
{
if ((dim <= 0) || (dim > dim_num())) return;
(*nx) = grid[dim - 1];
(*gcx) = grid[max_dim + dim - 1];
}
template< typename T >
T nse::GridId<T>::domain_spec(int idx) const
{
return domain[2 * max_dim + idx];
}
template< typename T >
int nse::GridId<T>::grid_spec(int idx) const
{
return grid[2 * max_dim + idx];
}
template< typename T >
bool nse::GridId<T>::check(const int ndim) const
{
return (
(key() == 'n' + 's' + 'e') && // file identifier
(dim_num() == ndim) && // dims - strict
((data_type_size() == sizeof(float)) ||
(data_type_size() == sizeof(double))) // appropriate data type
);
}
template< typename T >
void nse::GridId<T>::mpi_broadcast(const int host, const MPI_Comm comm)
{
nse::mpi_broadcast(header, GridId<T>::hsize, host, comm);
nse::mpi_broadcast(domain, GridId<T>::dsize, host, comm);
nse::mpi_broadcast(grid, GridId<T>::gsize, host, comm);
}
// -------------------------------------------------------------------------------------------- //