-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBox3d.hpp
More file actions
371 lines (321 loc) · 12.8 KB
/
Box3d.hpp
File metadata and controls
371 lines (321 loc) · 12.8 KB
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
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2022, Lawrence Livermore National Security, LLC.
//
// Produced at the Lawrence Livermore National Laboratory
//
// LLNL-CODE-758885
//
// All rights reserved.
//
// This file is part of Comb.
//
// For details, see https://github.com/LLNL/Comb
// Please also see the LICENSE file for MIT license.
//////////////////////////////////////////////////////////////////////////////
#ifndef _BOX3D_HPP
#define _BOX3D_HPP
#include "config.hpp"
#include "memory.hpp"
#include "exec_utils.hpp"
#include "MeshInfo.hpp"
struct IdxTemplate
{
enum struct location: IdxT
{ min =-1
, mid = 0
, max = 1
};
location idx;
IdxT offset;
constexpr IdxTemplate(location idx_, IdxT offset_) : idx(idx_), offset(offset_) {}
};
struct Box3d
{
static Box3d make_ghost_box(MeshInfo const& info)
{
IdxT local_ghost_min[3] { 0
, 0
, 0 };
IdxT local_ghost_max[3] { info.len[0]
, info.len[1]
, info.len[2] };
return Box3d{info, local_ghost_min, local_ghost_max};
}
static Box3d make_owned_box(MeshInfo const& info)
{
IdxT local_own_min[3] { info.global_own_min[0] - info.global_offset[0]
, info.global_own_min[1] - info.global_offset[1]
, info.global_own_min[2] - info.global_offset[2] };
IdxT local_own_max[3] { info.global_own_max[0] - info.global_offset[0]
, info.global_own_max[1] - info.global_offset[1]
, info.global_own_max[2] - info.global_offset[2] };
return Box3d{info, local_own_min, local_own_max};
}
MeshInfo info;
IdxT min[3];
IdxT sizes[3];
Box3d(MeshInfo const& info_, const IdxT local_min[], const IdxT local_max[])
: info(info_)
, min{ local_min[0]
, local_min[1]
, local_min[2] }
, sizes{ local_max[0] - local_min[0]
, local_max[1] - local_min[1]
, local_max[2] - local_min[2] }
{
//LOGPRINTF("Box3d i %d %d j %d %d k %d %d\n", min[0], max[0], min[1], max[1], min[2], max[2]);
//assert((imax-imin)*(jmax-jmin)*(kmax-kmin) <= 13*3*3);
}
void print(const char* name) const
{
fgprintf(FileGroup::proc, "Box3d %32s local (%i %i %i)-(%i %i %i) info (%i %i %i)-(%i %i %i) global (%i %i %i)-(%i %i %i)\n",
name,
min[0], min[1], min[2], min[0]+sizes[0], min[1]+sizes[1], min[2]+sizes[2],
info.min[0], info.min[1], info.min[2], info.max[0], info.max[1], info.max[2],
info.global_min[0], info.global_min[1], info.global_min[2], info.global_max[0], info.global_max[1], info.global_max[2] );
}
void correct_periodicity()
{
// nothing to do here, only have local information
// correct info periodicity
info.correct_periodicity();
}
// get the intersection between two boxes from the same GlobalMeshInfo
// returns a box with indices in the local index space of this->info
Box3d intersect(Box3d const& other) const
{
assert(info.global == other.info.global);
// find min, max in global coords
IdxT omin[3] { std::max(min[0] + info.global_offset[0], other.min[0] + other.info.global_offset[0])
, std::max(min[1] + info.global_offset[1], other.min[1] + other.info.global_offset[1])
, std::max(min[2] + info.global_offset[2], other.min[2] + other.info.global_offset[2]) };
IdxT omax[3] { std::min(min[0] + sizes[0] + info.global_offset[0], other.min[0] + other.sizes[0] + other.info.global_offset[0])
, std::min(min[1] + sizes[1] + info.global_offset[1], other.min[1] + other.sizes[1] + other.info.global_offset[1])
, std::min(min[2] + sizes[2] + info.global_offset[2], other.min[2] + other.sizes[2] + other.info.global_offset[2]) };
for (IdxT dim = 0; dim < 3; ++dim) {
/*
// make work with periodicity, may not be necessary
if (info.global.periodic[dim]) {
// correct for periodicity
// move other.min[dim] so in the range [ min[dim], min[dim]+info.global.sizes[dim] )
IdxT this_min = min[dim] + info.global_offset[dim];
IdxT other_min = other.min[dim] + other.info.global_offset[dim];
IdxT diff_min = other_min - this_min;
IdxT quot = diff_min / info.global.sizes[dim];
other_min -= (quot+(diff_min < 0 ? 1 : 0)) * info.global.sizes[dim];
IdxT this_remaining_size = sizes[dim] - (other_min - this_min);
omin[dim] = other_min;
omax[dim] = other_min + std::min(this_remaining_size, other.sizes[dim]);
}
assert(0 <= omin[dim] - min[dim] && omin[dim] - min[dim] < info.global.sizes[dim]);
assert(omax[dim] <= min[0] + sizes[0] + info.global_offset[0]); // this happens when omax would wrap around to the beginning of this
*/
// switch back to local coords
omin[dim] -= info.global_offset[dim];
omax[dim] -= info.global_offset[dim];
// correct max that are less than min to min (size 0 no overlap)
if (omax[dim] < omin[dim]) omax[dim] = omin[dim];
}
return Box3d{ info, omin, omax };
}
size_t size() const
{
return sizes[0] * sizes[1] * sizes[2] ;
}
bool operator==(Box3d const& other) const
{
return info == other.info &&
min[0] == other.min[0] &&
min[1] == other.min[1] &&
min[2] == other.min[2] &&
sizes[0] == other.sizes[0] &&
sizes[1] == other.sizes[1] &&
sizes[2] == other.sizes[2] ;
}
bool operator<(Box3d const& other) const
{
if (this == &other) return false;
int cmp = compare_totalsize(other);
if (cmp != 0) return cmp < 0;
for (IdxT dim = 0; dim < 3; ++dim) {
cmp = compare_size(dim, other);
if (cmp != 0) return cmp < 0;
}
for (IdxT dim = 0; dim < 3; ++dim) {
cmp = compare_min(dim, other);
if (cmp != 0) return cmp < 0;
}
return info < other.info;
}
int compare_totalsize(Box3d const& other) const
{
if (size() < other.size()) {
return -1;
} else if (size() == other.size()) {
return 0;
} else {
return 1;
}
}
int compare_size(IdxT dim, Box3d const& other) const
{
if (sizes[dim] < other.sizes[dim]) {
return -1;
} else if (sizes[dim] == other.sizes[dim]) {
return 0;
} else {
return 1;
}
}
int compare_min(IdxT dim, Box3d const& other) const
{
if (min[dim] < other.min[dim]) {
return -1;
} else if (min[dim] == other.min[dim]) {
return 0;
} else {
return 1;
}
}
bool equivalent(Box3d const& other) const
{
// check coordinates are equivalent
return min[0] == other.min[0] &&
min[1] == other.min[1] &&
min[2] == other.min[2] &&
sizes[0] == other.sizes[0] &&
sizes[1] == other.sizes[1] &&
sizes[2] == other.sizes[2] ;
}
bool global_equivalent(Box3d const& other) const
{
IdxT global_min[3] { min[0] + info.global_offset[0]
, min[1] + info.global_offset[1]
, min[2] + info.global_offset[2] };
IdxT other_min[3] { other.min[0] + other.info.global_offset[0]
, other.min[1] + other.info.global_offset[1]
, other.min[2] + other.info.global_offset[2] };
bool equiv = true;
// check sizes are the same
for(IdxT dim = 0; dim < 3; ++dim) {
equiv = equiv && (sizes[dim] == other.sizes[dim]);
}
// check indices are equivalent
for(IdxT dim = 0; dim < 3; ++dim) {
if (info.global.periodic[dim]) {
global_min[dim] = global_min[dim] % info.global.sizes[dim];
if (global_min[dim] < 0) global_min[dim] += info.global.sizes[dim];
}
if (other.info.global.periodic[dim]) {
other_min[dim] = other_min[dim] % other.info.global.sizes[dim];
if (other_min[dim] < 0) other_min[dim] += other.info.global.sizes[dim];
}
equiv = equiv && (global_min[dim] == other_min[dim]);
}
return equiv;
}
#ifdef COMB_ENABLE_MPI
MPI_Datatype get_type_subarray() const
{
MPI_Datatype mpi_type = detail::MPI::Type_create_subarray(3, info.len, sizes, min, MPI_ORDER_FORTRAN, MPI_DOUBLE);
detail::MPI::Type_commit(&mpi_type);
return mpi_type;
}
#endif
template < typename context >
void set_indices(context& con, LidxT* index_list) const
{
IdxT imin = min[0];
IdxT jmin = min[1];
IdxT kmin = min[2];
IdxT imax = min[0] + sizes[0];
IdxT jmax = min[1] + sizes[1];
IdxT kmax = min[2] + sizes[2];
IdxT ilen = imax - imin;
IdxT jlen = jmax - jmin;
IdxT klen = kmax - kmin;
con.for_all_3d(klen, jlen, ilen, make_set_idxr_idxr(detail::indexer_offset_kji{kmin, jmin, imin, info.len[0]*info.len[1], info.len[0]}, index_list, detail::indexer_kji{ilen*jlen, ilen}));
//for(IdxT idx = 0; idx < ilen*jlen*klen; ++idx) {
// LOGPRINTF("indices[%i] = %i\n", idx, index_list[idx]);
// assert(0 <= index_list[idx] && index_list[idx] < ilen*jlen*klen);
//}
con.synchronize();
}
};
struct Box3dTemplate
{
using IT = IdxTemplate;
using ITL = IdxTemplate::location;
static Box3d get_connection_inner_box(MeshInfo const& info, const int neighbor_coords[])
{
int bounds[3] { neighbor_coords[0] - info.global_coords[0]
, neighbor_coords[1] - info.global_coords[1]
, neighbor_coords[2] - info.global_coords[2] };
ITL locs[3] { (bounds[0] == -1) ? ITL::min : ( (bounds[0] == 1) ? ITL::max : ITL::mid )
, (bounds[1] == -1) ? ITL::min : ( (bounds[1] == 1) ? ITL::max : ITL::mid )
, (bounds[2] == -1) ? ITL::min : ( (bounds[2] == 1) ? ITL::max : ITL::mid ) };
return Box3dTemplate::make_Box3dTemplate_inner(info.ghost_widths, locs).make_box(info);
}
static Box3d get_connection_ghost_box(MeshInfo const& info, const int neighbor_coords[])
{
int bounds[3] { neighbor_coords[0] - info.global_coords[0]
, neighbor_coords[1] - info.global_coords[1]
, neighbor_coords[2] - info.global_coords[2] };
ITL locs[3] { (bounds[0] == -1) ? ITL::min : ( (bounds[0] == 1) ? ITL::max : ITL::mid )
, (bounds[1] == -1) ? ITL::min : ( (bounds[1] == 1) ? ITL::max : ITL::mid )
, (bounds[2] == -1) ? ITL::min : ( (bounds[2] == 1) ? ITL::max : ITL::mid ) };
return Box3dTemplate::make_Box3dTemplate_ghost(info.ghost_widths, locs).make_box(info);
}
static Box3dTemplate make_Box3dTemplate_inner(const IdxT widths[], const ITL bounds[])
{
IT tmin[3] { IT{(bounds[0] == ITL::mid) ? ITL::min : bounds[0], (bounds[0] == ITL::max) ? -widths[0] : 0 }
, IT{(bounds[1] == ITL::mid) ? ITL::min : bounds[1], (bounds[1] == ITL::max) ? -widths[1] : 0 }
, IT{(bounds[2] == ITL::mid) ? ITL::min : bounds[2], (bounds[2] == ITL::max) ? -widths[2] : 0 } };
IT tmax[3] { IT{(bounds[0] == ITL::mid) ? ITL::max : bounds[0], (bounds[0] == ITL::min) ? widths[0] : 0 }
, IT{(bounds[1] == ITL::mid) ? ITL::max : bounds[1], (bounds[1] == ITL::min) ? widths[1] : 0 }
, IT{(bounds[2] == ITL::mid) ? ITL::max : bounds[2], (bounds[2] == ITL::min) ? widths[2] : 0 } };
return Box3dTemplate{ tmin, tmax };
}
static Box3dTemplate make_Box3dTemplate_ghost(const IdxT widths[], const ITL bounds[])
{
IT tmin[3] { IT{(bounds[0] == ITL::mid) ? ITL::min : bounds[0], (bounds[0] == ITL::min) ? -widths[0] : 0 }
, IT{(bounds[1] == ITL::mid) ? ITL::min : bounds[1], (bounds[1] == ITL::min) ? -widths[1] : 0 }
, IT{(bounds[2] == ITL::mid) ? ITL::min : bounds[2], (bounds[2] == ITL::min) ? -widths[2] : 0 } };
IT tmax[3] { IT{(bounds[0] == ITL::mid) ? ITL::max : bounds[0], (bounds[0] == ITL::max) ? widths[0] : 0 }
, IT{(bounds[1] == ITL::mid) ? ITL::max : bounds[1], (bounds[1] == ITL::max) ? widths[1] : 0 }
, IT{(bounds[2] == ITL::mid) ? ITL::max : bounds[2], (bounds[2] == ITL::max) ? widths[2] : 0 } };
return Box3dTemplate{ tmin, tmax };
}
IT tmin[3];
IT tmax[3];
constexpr Box3dTemplate(IT const tmin_[], IT const tmax_[])
: tmin{tmin_[0], tmin_[1], tmin_[2]}
, tmax{tmax_[0], tmax_[1], tmax_[2]}
{
}
Box3d make_box(MeshInfo const& info)
{
IdxT min[3] { idx(0, info, tmin[0])
, idx(1, info, tmin[1])
, idx(2, info, tmin[2]) };
IdxT max[3] { idx(0, info, tmax[0])
, idx(1, info, tmax[1])
, idx(2, info, tmax[2]) };
return Box3d{info, min, max};
}
private:
IdxT idx(IdxT dim, MeshInfo const& info, IT it)
{
IdxT idx = 0;
switch (it.idx) {
case ITL::min:
idx = info.min[dim]; break;
case ITL::max:
idx = info.max[dim]; break;
default:
assert(0); break;
}
return idx + it.offset;
}
};
#endif // _BOX3D_HPP