Debian Codegraph @

packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_BlockMesh.C

raw
1 // Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include <Ioss_Utils.h>
8 #include <catalyst_tests/Iocatalyst_BlockMesh.h>
9
10 namespace Iocatalyst {
11
12 BlockMesh::ID BlockMesh::_id = 1;
13
14 BlockMesh::BlockMesh()
15 {
16 partition.id = 0;
17 partition.size = 1;
18 extents.i = 1;
19 extents.j = 1;
20 extents.k = 1;
21 partitionExtents.i = 1;
22 partitionExtents.j = 1;
23 partitionExtents.k = 1;
24 partitionStart.i = 0;
25 partitionStart.j = 0;
26 partitionStart.k = 0;
27 id = _id++;
28 }
29
30 BlockMesh::~BlockMesh() {}
31
32 void BlockMesh::init(const Partition &part, const Extent &numBlocks, const Extent &origin)
33 {
34 if (part.id < 0 || part.size < 0 || part.id >= part.size) {
35 std::ostringstream errmsg;
36 errmsg << "Invalid partition: id = " << part.id << std::string(", size = ") << part.size
37 << "\n";
38 IOSS_ERROR(errmsg);
39 }
40 this->partition = part;
41 this->extents = numBlocks;
42 this->origin = origin;
43 splitBlock();
44 }
45
46 void BlockMesh::splitBlock()
47 {
48 // Split algorithm from vtkExtentTranslator.cxx SplitExtent()
49
50 unsigned long size[3];
51 int numPiecesInFirstHalf;
52 int splitAxis;
53 long int mid;
54 int ext[6];
55 int numPieces = getPartition().size;
56 int piece = getPartition().id;
57 fillExtents(ext);
58 setPartitionFromExtents(ext);
59
60 while (numPieces > 1) {
61 size[0] = ext[1] - ext[0];
62 size[1] = ext[3] - ext[2];
63 size[2] = ext[5] - ext[4];
64
65 if (size[2] >= size[1] && size[2] >= size[0] && size[2] / 2 >= 1) {
66 splitAxis = 2;
67 }
68 else if (size[1] >= size[0] && size[1] / 2 >= 1) {
69 splitAxis = 1;
70 }
71 else if (size[0] / 2 >= 1) {
72 splitAxis = 0;
73 }
74 else {
75 splitAxis = -1;
76 }
77
78 if (splitAxis == -1) {
79 if (piece == 0) {
80 numPieces = 1;
81 }
82 else {
83 setPartitionEmpty();
84 return;
85 }
86 }
87 else {
88 numPiecesInFirstHalf = (numPieces / 2);
89 mid = size[splitAxis];
90 mid = (mid * numPiecesInFirstHalf) / numPieces + ext[splitAxis * 2];
91 if (piece < numPiecesInFirstHalf) {
92 ext[splitAxis * 2 + 1] = mid;
93 numPieces = numPiecesInFirstHalf;
94 }
95 else {
96 ext[splitAxis * 2] = mid;
97 numPieces = numPieces - numPiecesInFirstHalf;
98 piece -= numPiecesInFirstHalf;
99 }
100 }
101 }
102 setPartitionFromExtents(ext);
103 }
104
105 void BlockMesh::fillExtents(int *ext)
106 {
107 if (getExtents().i == 0) {
108 ext[0] = 0;
109 ext[1] = -1;
110 }
111 else {
112 ext[0] = 0;
113 ext[1] = getExtents().i;
114 }
115
116 if (getExtents().j == 0) {
117 ext[2] = 0;
118 ext[3] = -1;
119 }
120 else {
121 ext[2] = 0;
122 ext[3] = getExtents().j;
123 }
124
125 if (getExtents().k == 0) {
126 ext[4] = 0;
127 ext[5] = -1;
128 }
129 else {
130 ext[4] = 0;
131 ext[5] = getExtents().k;
132 }
133 }
134
135 void BlockMesh::setPartitionFromExtents(int ext[6])
136 {
137 int sizeX = ext[1] - ext[0];
138 int sizeY = ext[3] - ext[2];
139 int sizeZ = ext[5] - ext[4];
140 if (sizeX <= 0 || sizeY <= 0 || sizeZ <= 0) {
141 setPartitionEmpty();
142 return;
143 }
144
145 partitionExtents.i = sizeX;
146 partitionStart.i = ext[0];
147
148 partitionExtents.j = sizeY;
149 partitionStart.j = ext[2];
150
151 partitionExtents.k = sizeZ;
152 partitionStart.k = ext[4];
153 }
154
155 void BlockMesh::setPartitionEmpty()
156 {
157 partitionExtents.i = 0;
158 partitionExtents.j = 0;
159 partitionExtents.k = 0;
160 partitionStart.i = 0;
161 partitionStart.j = 0;
162 partitionStart.k = 0;
163 }
164
165 bool BlockMesh::isPartitionEmpty() const
166 {
167 return partitionExtents.i == 0 || partitionExtents.j == 0 || partitionExtents.k == 0;
168 }
169
170 BlockMesh::ID BlockMesh::getID() const { return id; }
171
172 int BlockMesh::getNumPoints() const { return getNumInBlockMesh(POINT_OFFSET); }
173
174 int BlockMesh::getNumPartitionPoints() const { return getNumInPartition(POINT_OFFSET); }
175
176 BlockMesh::IDList BlockMesh::getPartitionPointIDs() const
177 {
178 return getPartitionIDs(POINT_OFFSET);
179 }
180
181 BlockMesh::ID BlockMesh::getPointIDfromCoords(unsigned int i, unsigned int j,
182 unsigned int k) const
183 {
184 Extent coords = {i, j, k};
185 Extent bounds = {extents.i + POINT_OFFSET, extents.j + POINT_OFFSET, extents.k + POINT_OFFSET};
186 return getIDfromCoords(coords, bounds);
187 }
188
189 BlockMesh::Point BlockMesh::getPointCoordsForPointID(ID pointID) const
190 {
191 Point p;
192 Extent bounds = {extents.i + POINT_OFFSET, extents.j + POINT_OFFSET, extents.k + POINT_OFFSET};
193 Extent ext = getCoordsForID(pointID, bounds);
194 p.x = origin.i + ext.i * BLOCK_LENGTH;
195 p.y = origin.j + ext.j * BLOCK_LENGTH;
196 p.z = origin.k + ext.k * BLOCK_LENGTH;
197 return p;
198 }
199
200 BlockMesh::ID BlockMesh::getGlobalIDForPointID(ID pointID)
201 {
202 Extent pointExt = getExtents();
203 pointExt.i += 1;
204 pointExt.j += 1;
205 pointExt.k += 1;
206 auto coords = getCoordsForID(pointID, pointExt);
207 coords.i += origin.i;
208 coords.j += origin.j;
209 coords.k += origin.k;
210 return getIDfromCoords(coords, getGlobalPointExtents());
211 }
212
213 int BlockMesh::getNumBlocks() const { return getNumInBlockMesh(BLOCK_OFFSET); }
214
215 int BlockMesh::getNumPartitionBlocks() const { return getNumInPartition(BLOCK_OFFSET); }
216
217 BlockMesh::IDList BlockMesh::getPartitionBlockIDs() const
218 {
219 return getPartitionIDs(BLOCK_OFFSET);
220 }
221
222 BlockMesh::BlockConn BlockMesh::getBlockConnectivityPointIDs(ID blockID) const
223 {
224 BlockConn conn;
225 Extent bounds = {extents.i + BLOCK_OFFSET, extents.j + BLOCK_OFFSET, extents.k + BLOCK_OFFSET};
226 Extent e = getCoordsForID(blockID, bounds);
227 conn[0] = getPointIDfromCoords(e.i, e.j, e.k);
228 conn[1] = getPointIDfromCoords(e.i + 1, e.j, e.k);
229 conn[2] = getPointIDfromCoords(e.i + 1, e.j + 1, e.k);
230 conn[3] = getPointIDfromCoords(e.i, e.j + 1, e.k);
231 conn[4] = getPointIDfromCoords(e.i, e.j, e.k + 1);
232 conn[5] = getPointIDfromCoords(e.i + 1, e.j, e.k + 1);
233 conn[6] = getPointIDfromCoords(e.i + 1, e.j + 1, e.k + 1);
234 conn[7] = getPointIDfromCoords(e.i, e.j + 1, e.k + 1);
235 return conn;
236 }
237
238 BlockMesh::ID BlockMesh::getGlobalIDForBlockID(ID blockID)
239 {
240 auto coords = getCoordsForID(blockID, getExtents());
241 coords.i += origin.i;
242 coords.j += origin.j;
243 coords.k += origin.k;
244 return getIDfromCoords(coords, getGlobalBlockExtents());
245 }
246
247 BlockMesh::IDList BlockMesh::getPartitionIDs(unsigned int offset) const
248 {
249 BlockMesh::IDList ids;
250 Extent bounds = {extents.i + offset, extents.j + offset, extents.k + offset};
251 if (!isPartitionEmpty()) {
252 for (unsigned int k = partitionStart.k; k < partitionStart.k + partitionExtents.k + offset;
253 k++) {
254 for (unsigned int j = partitionStart.j; j < partitionStart.j + partitionExtents.j + offset;
255 j++) {
256 for (unsigned int i = partitionStart.i;
257 i < partitionStart.i + partitionExtents.i + offset; i++) {
258 Extent coords = {i, j, k};
259 ids.push_back(getIDfromCoords(coords, bounds));
260 }
261 }
262 }
263 }
264 return ids;
265 }
266
267 BlockMesh::ID BlockMesh::getIDfromCoords(Extent coords, Extent bounds)
268 {
269 return coords.k * bounds.i * bounds.j + coords.j * bounds.i + coords.i + 1;
270 }
271
272 BlockMesh::Extent BlockMesh::getCoordsForID(ID id, Extent bounds)
273 {
274 Extent ext;
275 int zeroBasedID = id - 1;
276 unsigned int sizeXY = bounds.i * bounds.j;
277 ext.k = zeroBasedID / sizeXY;
278 ext.j = (zeroBasedID - ext.k * sizeXY) / bounds.i;
279 ext.i = zeroBasedID - ext.k * sizeXY - ext.j * bounds.i;
280 return ext;
281 }
282
283 int BlockMesh::getNumInPartition(unsigned int offset) const
284 {
285 return (partitionExtents.i + offset) * (partitionExtents.j + offset) *
286 (partitionExtents.k + offset);
287 }
288
289 int BlockMesh::getNumInBlockMesh(unsigned int offset) const
290 {
291 return (extents.i + offset) * (extents.j + offset) * (extents.k + offset);
292 }
293
294 std::map<std::string, double> *BlockMesh::getTransientCellFieldMap()
295 {
296 return &(this->transientCellFields);
297 }
298
299 std::map<std::string, double> *BlockMesh::getTransientPointFieldMap()
300 {
301 return &(this->transientPointFields);
302 }
303
304 void BlockMesh::addTransientCellField(std::string f_name, double f_value)
305 {
306 this->transientCellFields.insert({f_name, f_value});
307 }
308
309 void BlockMesh::addTransientPointField(std::string f_name, double f_value)
310 {
311 this->transientPointFields.insert({f_name, f_value});
312 }
313
314 } // namespace Iocatalyst