Debian Codegraph @

packages/seacas/libraries/ioss/src/gen_struc/Iogs_GeneratedMesh.C

raw
1 // Copyright(C) 1999-2025 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_EntityType.h" // for EntityType, etc
8 #include "Ioss_Hex8.h"
9 #include "Ioss_Utils.h"
10 #include "gen_struc/Iogs_GeneratedMesh.h"
11 #include <cassert> // for assert
12 #include <cmath> // for atan2, cos, sin
13 #include <fmt/core.h>
14 #include <fmt/format.h>
15 #include <fmt/ostream.h>
16 #include <iosfwd>
17 #include <numeric>
18 #include <string>
19 #include <tokenize.h> // for tokenize
20 #include <vector> // for vector
21
22 #include "Ioss_CodeTypes.h"
23
24 namespace Iogs {
25 GeneratedMesh::GeneratedMesh(int64_t /*num_x */, int64_t /* num_y */, int64_t /* num_z */,
26 int proc_count, int my_proc)
27 : processorCount(proc_count), myProcessor(my_proc)
28 {
29 initialize();
30 }
31
32 GeneratedMesh::GeneratedMesh(const std::string &parameters, int proc_count, int my_proc)
33 : processorCount(proc_count), myProcessor(my_proc)
34 {
35 // Possible that the 'parameters' has the working directory path
36 // prepended to the parameter list. Strip off everything in front
37 // of the last '/' (if any)...
38 auto params = Ioss::tokenize(parameters, "/");
39
40 auto groups = Ioss::tokenize(params.back(), "|+");
41
42 // First 'group' is the interval specification -- IxJxK
43 auto tokens = Ioss::tokenize(groups[0], "x");
44 assert(tokens.size() == 3);
45 numX = std::stoll(tokens[0]);
46 numY = std::stoll(tokens[1]);
47 numZ = std::stoll(tokens[2]);
48
49 initialize();
50 parse_options(groups);
51 }
52
53 GeneratedMesh::GeneratedMesh() { initialize(); }
54
55 void GeneratedMesh::initialize()
56 {
57 if (processorCount > numZ) {
58 IOSS_ERROR(
59 fmt::format("ERROR: ({})\n"
60 " The number of mesh intervals in the Z direction ({})\n"
61 " must be at least as large as the number of processors ({}).\n"
62 " The current parameters do not meet that requirement. Execution will "
63 "terminate.\n",
64 __func__, numZ, processorCount));
65 }
66
67 if (processorCount > 1) {
68 myNumZ = numZ / processorCount;
69 if (myProcessor < (numZ % processorCount)) {
70 myNumZ++;
71 }
72
73 // Determine myStartZ for this processor...
74 size_t extra = numZ % processorCount;
75 if (extra > myProcessor) {
76 extra = myProcessor;
77 }
78 size_t per_proc = numZ / processorCount;
79 myStartZ = myProcessor * per_proc + extra;
80 }
81 else {
82 myNumZ = numZ;
83 }
84
85 for (int i = 0; i < 3; i++) {
86 for (int j = 0; j < 3; j++) {
87 rotmat[i][j] = 0.0;
88 }
89 rotmat[i][i] = 1.0;
90 }
91
92 variableCount[Ioss::COMMSET] = 0;
93 variableCount[Ioss::EDGEBLOCK] = 0;
94 variableCount[Ioss::EDGESET] = 0;
95 variableCount[Ioss::ELEMENTBLOCK] = 0;
96 variableCount[Ioss::ELEMENTSET] = 0;
97 variableCount[Ioss::FACEBLOCK] = 0;
98 variableCount[Ioss::FACESET] = 0;
99 variableCount[Ioss::INVALID_TYPE] = 0;
100 variableCount[Ioss::NODEBLOCK] = 0;
101 variableCount[Ioss::REGION] = 0;
102 variableCount[Ioss::SIDEBLOCK] = 0;
103 variableCount[Ioss::SIDESET] = 0;
104 variableCount[Ioss::SUPERELEMENT] = 0;
105 }
106
107 int64_t GeneratedMesh::add_sideset(ShellLocation loc)
108 {
109 sidesets.push_back(loc);
110 return sidesets.size();
111 }
112
113 void GeneratedMesh::set_bbox(double xmin, double ymin, double zmin, double xmax, double ymax,
114 double zmax)
115 {
116 // NOTE: All calculations are based on the currently
117 // active interval settings. If scale or offset or zdecomp
118 // specified later in the option list, you may not get the
119 // desired bounding box.
120 if (numX == 0 || numY == 0 || numZ == 0) {
121 IOSS_ERROR(fmt::format("ERROR: ({})\n"
122 " All interval counts must be greater than 0.\n"
123 " numX = {}, numY = {}, numZ = {}\n",
124 __func__, numX, numY, numZ));
125 }
126
127 double x_range = xmax - xmin;
128 double y_range = ymax - ymin;
129 double z_range = zmax - zmin;
130
131 sclX = x_range / static_cast<double>(numX);
132 sclY = y_range / static_cast<double>(numY);
133 sclZ = z_range / static_cast<double>(numZ);
134
135 offX = xmin;
136 offY = ymin;
137 offZ = zmin;
138 }
139
140 void GeneratedMesh::set_scale(double scl_x, double scl_y, double scl_z)
141 {
142 sclX = scl_x;
143 sclY = scl_y;
144 sclZ = scl_z;
145 }
146
147 void GeneratedMesh::set_offset(double off_x, double off_y, double off_z)
148 {
149 offX = off_x;
150 offY = off_y;
151 offZ = off_z;
152 }
153
154 void GeneratedMesh::parse_options(const Ioss::NameList &groups)
155 {
156 for (size_t i = 1; i < groups.size(); i++) {
157 auto option = Ioss::tokenize(groups[i], ":");
158 // option[0] is the type of the option and option[1] is the argument to the option.
159
160 if (option[0] == "sideset") {
161 // Option of the form "sideset:xXyYzZ"
162 // The argument specifies whether there is a sideset
163 // at the location. 'x' is minX, 'X' is maxX, etc.
164 for (auto opt : option[1]) {
165 switch (opt) {
166 case 'x': add_sideset(MX); break;
167 case 'X': add_sideset(PX); break;
168 case 'y': add_sideset(MY); break;
169 case 'Y': add_sideset(PY); break;
170 case 'z': add_sideset(MZ); break;
171 case 'Z': add_sideset(PZ); break;
172 default:
173 IOSS_ERROR(fmt::format("ERROR: Unrecognized sideset location option '{}'.", opt));
174 }
175 }
176 }
177 else if (option[0] == "scale") {
178 // Option of the form "scale:xs,ys,zs
179 auto tokens = Ioss::tokenize(option[1], ",");
180 assert(tokens.size() == 3);
181 sclX = std::stod(tokens[0]);
182 sclY = std::stod(tokens[1]);
183 sclZ = std::stod(tokens[2]);
184 }
185
186 else if (option[0] == "offset") {
187 // Option of the form "offset:xo,yo,zo
188 auto tokens = Ioss::tokenize(option[1], ",");
189 assert(tokens.size() == 3);
190 offX = std::stod(tokens[0]);
191 offY = std::stod(tokens[1]);
192 offZ = std::stod(tokens[2]);
193 }
194
195 else if (option[0] == "zdecomp") {
196 // Option of the form "zdecomp:1,1,2,2,1,2,...
197 // Specifies the number of intervals in the z direction
198 // for each processor. The number of tokens must match
199 // the number of processors. Note that the new numZ will
200 // be the sum of the intervals specified in this command.
201 auto tokens = Ioss::tokenize(option[1], ",");
202 assert(tokens.size() == processorCount);
203 Ioss::Int64Vector Zs;
204 numZ = 0;
205 for (size_t j = 0; j < processorCount; j++) {
206 Zs.push_back(std::stoll(tokens[j]));
207 numZ += Zs[j];
208 }
209 myNumZ = Zs[myProcessor];
210 myStartZ = 0;
211 for (size_t j = 0; j < myProcessor; j++) {
212 myStartZ += Zs[j];
213 }
214 }
215
216 else if (option[0] == "bbox") {
217 // Bounding-Box Option of the form "bbox:xmin,ymin,zmin,xmax,ymax,zmaxo
218 auto tokens = Ioss::tokenize(option[1], ",");
219 assert(tokens.size() == 6);
220 double xmin = std::stod(tokens[0]);
221 double ymin = std::stod(tokens[1]);
222 double zmin = std::stod(tokens[2]);
223 double xmax = std::stod(tokens[3]);
224 double ymax = std::stod(tokens[4]);
225 double zmax = std::stod(tokens[5]);
226
227 set_bbox(xmin, ymin, zmin, xmax, ymax, zmax);
228 }
229
230 else if (option[0] == "rotate") {
231 // Rotate Option of the form "rotate:axis,angle,axis,angle,...
232 auto tokens = Ioss::tokenize(option[1], ",");
233 assert(tokens.size() % 2 == 0);
234 for (size_t ir = 0; ir < tokens.size();) {
235 std::string axis = tokens[ir++];
236 double angle_degree = std::stod(tokens[ir++]);
237 set_rotation(axis, angle_degree);
238 }
239 }
240
241 else if (option[0] == "times") {
242 timestepCount = std::stoll(option[1]);
243 }
244
245 else if (option[0] == "variables") {
246 // Variables Option of the form "variables:global,10,element,100,..."
247 auto tokens = Ioss::tokenize(option[1], ",");
248 assert(tokens.size() % 2 == 0);
249 for (size_t ir = 0; ir < tokens.size();) {
250 std::string type = tokens[ir++];
251 int count = std::stoi(tokens[ir++]);
252 set_variable_count(type, count);
253 }
254 if (timestepCount == 0) {
255 timestepCount = 1;
256 }
257 }
258
259 else if (option[0] == "help") {
260 fmt::print(Ioss::OUTPUT(),
261 "\nValid Options for GeneratedMesh parameter string:\n"
262 "\tIxJxK -- specifies intervals; must be first option. Ex: 4x10x12\n"
263 "\toffset:xoff, yoff, zoff\n"
264 "\tscale: xscl, yscl, zscl\n"
265 "\tzdecomp:n1,n2,n3,...,n#proc\n"
266 "\tbbox: xmin, ymin, zmin, xmax, ymax, zmax\n"
267 "\trotate: axis,angle,axis,angle,...\n"
268 "\tsideset:xXyYzZ (specifies which plane to apply sideset)\n"
269 "\tvariables:type,count,... "
270 "type=global|element|node|nodal|sideset|surface\n"
271 "\ttimes:count (number of timesteps to generate)\n"
272 "\tshow -- show mesh parameters\n"
273 "\thelp -- show this list\n\n");
274 }
275
276 else if (option[0] == "show") {
277 show_parameters();
278 }
279 else {
280 fmt::print(Ioss::WarnOut(), "Unrecognized option '{}'. It will be ignored.\n", option[0]);
281 }
282 }
283 }
284
285 void GeneratedMesh::show_parameters() const
286 {
287 if (myProcessor == 0) {
288 fmt::print(Ioss::OUTPUT(),
289 "\nMesh Parameters:\n"
290 "\tIntervals: {} by {} by {}\n"
291 "\tX = {} * (0..{}) + {}\tRange: {} <= X <= {}\n"
292 "\tY = {} * (0..{}) + {}\tRange: {} <= Y <= {}\n"
293 "\tZ = {} * (0..{}) + {}\tRange: {} <= Z <= {}\n\n"
294 "\tNode Count (total) = {:12}\n"
295 "\tCell Count (total) = {:12}\n"
296 "\tBlock Count = {:12}\n"
297 "\tSideSet Count = {:12}\n"
298 "\tTimestep Count = {:12}\n\n",
299 numX, numY, numZ, sclX, numX, offX, offX, offX + numX * sclX, sclY, numY, offY,
300 offY, offY + numY * sclY, sclZ, numZ, offZ, offZ, offZ + numZ * sclZ,
301 fmt::group_digits(node_count()), fmt::group_digits(element_count()),
302 fmt::group_digits(structured_block_count()), fmt::group_digits(sideset_count()),
303 fmt::group_digits(timestep_count()));
304
305 if (doRotation) {
306 fmt::print(Ioss::OUTPUT(), "\tRotation Matrix: \n\t");
307 for (const auto &elem : rotmat) {
308 for (double jj : elem) {
309 fmt::print(Ioss::OUTPUT(), fmt::runtime("{:14.e}\t"), jj);
310 }
311 fmt::print(Ioss::OUTPUT(), "\n\t");
312 }
313 fmt::print(Ioss::OUTPUT(), "\n");
314 }
315 }
316 }
317
318 int64_t GeneratedMesh::node_count() const { return (numX + 1) * (numY + 1) * (numZ + 1); }
319
320 int64_t GeneratedMesh::node_count_proc() const { return (numX + 1) * (numY + 1) * (myNumZ + 1); }
321
322 int64_t GeneratedMesh::structured_block_count() const { return 1; }
323
324 int64_t GeneratedMesh::sideset_count() const { return sidesets.size(); }
325
326 int64_t GeneratedMesh::element_count() const
327 {
328 int64_t count = element_count(1);
329 return count;
330 }
331
332 int64_t GeneratedMesh::element_count_proc() const
333 {
334 int64_t count = 0;
335 for (int64_t i = 0; i < structured_block_count(); i++) {
336 count += element_count_proc(i + 1);
337 }
338 return count;
339 }
340
341 int64_t GeneratedMesh::element_count(int64_t block_number) const
342 {
343 assert(block_number <= structured_block_count());
344 IOSS_ASSERT_USED(block_number);
345 return numX * numY * numZ;
346 }
347
348 int64_t GeneratedMesh::element_count_proc(int64_t block_number) const
349 {
350 assert(block_number <= structured_block_count());
351 IOSS_ASSERT_USED(block_number);
352 return numX * numY * myNumZ;
353 }
354
355 int64_t GeneratedMesh::sideset_side_count(int64_t id) const
356 {
357 // id is position in sideset list + 1
358 assert(id > 0 && (size_t)id <= sidesets.size());
359 ShellLocation loc = sidesets[id - 1];
360 switch (loc) {
361 case MX:
362 case PX: return numY * numZ;
363 case MY:
364 case PY: return numX * numZ;
365 case MZ:
366 case PZ: return numX * numY;
367 }
368 return 0;
369 }
370
371 int64_t GeneratedMesh::sideset_side_count_proc(int64_t id) const
372 {
373 // id is position in sideset list + 1
374 assert(id > 0 && (size_t)id <= sidesets.size());
375 ShellLocation loc = sidesets[id - 1];
376 switch (loc) {
377 case MX:
378 case PX: return numY * myNumZ;
379 case MY:
380 case PY: return numX * myNumZ;
381 case MZ:
382 if (myProcessor == 0) {
383 return numX * numY;
384 }
385 else {
386 return 0;
387 }
388 case PZ:
389 if (myProcessor == processorCount - 1) {
390 return numX * numY;
391 }
392 else {
393 return 0;
394 }
395 }
396 return 0;
397 }
398
399 std::pair<std::string, int> GeneratedMesh::topology_type(int64_t block_number) const
400 {
401 assert(block_number <= structured_block_count() && block_number > 0);
402 IOSS_ASSERT_USED(block_number);
403 return std::make_pair(std::string(Ioss::Hex8::name), 8);
404 }
405
406 void GeneratedMesh::node_map(Ioss::Int64Vector &map) const
407 {
408 map.resize(node_count_proc());
409 int64_t offset = myStartZ * (numX + 1) * (numY + 1);
410 std::iota(map.begin(), map.end(), offset + 1);
411 }
412
413 void GeneratedMesh::node_map(Ioss::IntVector &map) const
414 {
415 map.resize(node_count_proc());
416 int offset = myStartZ * (numX + 1) * (numY + 1);
417 std::iota(map.begin(), map.end(), offset + 1);
418 }
419
420 int64_t GeneratedMesh::communication_node_count_proc() const
421 {
422 int64_t count = (numX + 1) * (numY + 1);
423 if (myProcessor != 0 && myProcessor != processorCount - 1) {
424 count *= 2;
425 }
426
427 return count;
428 }
429
430 void GeneratedMesh::owning_processor(int *owner, int64_t num_node)
431 {
432 for (int64_t i = 0; i < num_node; i++) {
433 owner[i] = myProcessor;
434 }
435
436 if (myProcessor != 0) {
437 int64_t count = (numX + 1) * (numY + 1);
438 for (int64_t i = 0; i < count; i++) {
439 owner[i] = myProcessor - 1;
440 }
441 }
442 }
443
444 void GeneratedMesh::build_node_map(Ioss::Int64Vector &map, std::vector<int> &proc, int64_t slab,
445 size_t slabOffset, size_t adjacentProc, size_t index)
446 {
447 int64_t offset = (myStartZ + slabOffset) * (numX + 1) * (numY + 1);
448 for (int64_t i = 0; i < slab; i++) {
449 map[index] = offset + i + 1;
450 proc[index++] = static_cast<int>(adjacentProc);
451 }
452 }
453
454 void GeneratedMesh::node_communication_map(Ioss::Int64Vector &map, std::vector<int> &proc)
455 {
456 bool isFirstProc = myProcessor == 0;
457 bool isLastProc = myProcessor == processorCount - 1;
458
459 int64_t count = (numX + 1) * (numY + 1);
460 int64_t slab = count;
461 if (!isFirstProc && !isLastProc) {
462 count *= 2;
463 }
464 map.resize(count);
465 proc.resize(count);
466
467 size_t offset = 0;
468 if (!isFirstProc) {
469 build_node_map(map, proc, slab, 0, myProcessor - 1, offset);
470 offset += slab;
471 }
472 if (!isLastProc) {
473 build_node_map(map, proc, slab, myNumZ, myProcessor + 1, offset);
474 }
475 }
476
477 void GeneratedMesh::element_map(int64_t block_number, Ioss::Int64Vector &map) const
478 {
479 raw_element_map(block_number, map);
480 }
481
482 void GeneratedMesh::element_map(int64_t block_number, Ioss::IntVector &map) const
483 {
484 raw_element_map(block_number, map);
485 }
486
487 template <typename INT>
488 void GeneratedMesh::raw_element_map(int64_t block_number, std::vector<INT> &map) const
489 {
490 assert(block_number <= structured_block_count() && block_number > 0);
491
492 INT count = element_count_proc(block_number);
493 map.reserve(count);
494
495 if (block_number == 1) {
496 // Hex/Tet block...
497 count = element_count_proc(1);
498 INT offset = myStartZ * numX * numY;
499 for (INT i = 0; i < count; i++) {
500 map.push_back(offset + i + 1);
501 }
502 }
503 }
504
505 void GeneratedMesh::element_map(Ioss::Int64Vector &map) const { raw_element_map(map); }
506
507 void GeneratedMesh::element_map(Ioss::IntVector &map) const { raw_element_map(map); }
508
509 template <typename INT> void GeneratedMesh::raw_element_map(std::vector<INT> &map) const
510 {
511 INT count = element_count_proc();
512 map.reserve(count);
513
514 // Hex block...
515 count = element_count_proc(1);
516 INT offset = myStartZ * numX * numY;
517 for (INT i = 0; i < count; i++) {
518 map.push_back(offset + i + 1);
519 }
520 }
521
522 void GeneratedMesh::element_surface_map(ShellLocation loc, Ioss::Int64Vector &map) const
523 {
524 int64_t count = 0;
525 map.resize(2 * count);
526 int64_t index = 0;
527 int64_t offset = 0;
528
529 // For hex elements
530 switch (loc) {
531 case MX:
532 offset = myStartZ * numX * numY + 1; // 1-based elem id
533 for (size_t k = 0; k < myNumZ; ++k) {
534 for (size_t j = 0; j < numY; ++j) {
535 map[index++] = offset;
536 map[index++] = 3; // 0-based local face id
537 offset += numX;
538 }
539 }
540 break;
541
542 case PX:
543 offset = myStartZ * numX * numY + numX;
544 for (size_t k = 0; k < myNumZ; ++k) {
545 for (size_t j = 0; j < numY; ++j) {
546 map[index++] = offset; // 1-based elem id
547 map[index++] = 1; // 0-based local face id
548 offset += numX;
549 }
550 }
551 break;
552
553 case MY:
554 offset = myStartZ * numX * numY + 1;
555 for (size_t k = 0; k < myNumZ; ++k) {
556 for (size_t i = 0; i < numX; ++i) {
557 map[index++] = offset++;
558 map[index++] = 0; // 0-based local face id
559 }
560 offset += numX * (numY - 1);
561 }
562 break;
563
564 case PY:
565 offset = myStartZ * numX * numY + numX * (numY - 1) + 1;
566 for (size_t k = 0; k < myNumZ; ++k) {
567 for (size_t i = 0; i < numX; ++i) {
568 map[index++] = offset++;
569 map[index++] = 2; // 0-based local face id
570 }
571 offset += numX * (numY - 1);
572 }
573 break;
574
575 case MZ:
576 if (myProcessor == 0) {
577 offset = 1;
578 for (size_t i = 0; i < numY; i++) {
579 for (size_t j = 0; j < numX; j++) {
580 map[index++] = offset++;
581 map[index++] = 4;
582 }
583 }
584 }
585 break;
586
587 case PZ:
588 if (myProcessor == processorCount - 1) {
589 offset = (numZ - 1) * numX * numY + 1;
590 for (size_t i = 0, k = 0; i < numY; i++) {
591 for (size_t j = 0; j < numX; j++, k++) {
592 map[index++] = offset++;
593 map[index++] = 5;
594 }
595 }
596 }
597 break;
598 }
599 }
600
601 void GeneratedMesh::coordinates(std::vector<double> &coord) const
602 {
603 /* create global coordinates */
604 int64_t count = node_count_proc();
605 coord.resize(count * 3);
606 coordinates(Data(coord));
607 }
608
609 void GeneratedMesh::coordinates(double *coord) const
610 {
611 /* create global coordinates */
612 int64_t count = node_count_proc();
613
614 int64_t k = 0;
615 for (size_t m = myStartZ; m < myStartZ + myNumZ + 1; m++) {
616 for (size_t i = 0; i < numY + 1; i++) {
617 for (size_t j = 0; j < numX + 1; j++) {
618 coord[k++] = sclX * static_cast<double>(j) + offX;
619 coord[k++] = sclY * static_cast<double>(i) + offY;
620 coord[k++] = sclZ * static_cast<double>(m) + offZ;
621 }
622 }
623 }
624
625 if (doRotation) {
626 for (int64_t i = 0; i < count * 3; i += 3) {
627 double xn = coord[i + 0];
628 double yn = coord[i + 1];
629 double zn = coord[i + 2];
630 coord[i + 0] = xn * rotmat[0][0] + yn * rotmat[1][0] + zn * rotmat[2][0];
631 coord[i + 1] = xn * rotmat[0][1] + yn * rotmat[1][1] + zn * rotmat[2][1];
632 coord[i + 2] = xn * rotmat[0][2] + yn * rotmat[1][2] + zn * rotmat[2][2];
633 }
634 }
635 }
636
637 void GeneratedMesh::coordinates(std::vector<double> &x, std::vector<double> &y,
638 std::vector<double> &z) const
639 {
640 /* create global coordinates */
641 int64_t count = node_count_proc();
642 x.reserve(count);
643 y.reserve(count);
644 z.reserve(count);
645
646 for (size_t m = myStartZ; m < myStartZ + myNumZ + 1; m++) {
647 for (size_t i = 0; i < numY + 1; i++) {
648 for (size_t j = 0; j < numX + 1; j++) {
649 x.push_back(sclX * static_cast<double>(j) + offX);
650 y.push_back(sclY * static_cast<double>(i) + offY);
651 z.push_back(sclZ * static_cast<double>(m) + offZ);
652 }
653 }
654 }
655 if (doRotation) {
656 for (int64_t i = 0; i < count; i++) {
657 double xn = x[i];
658 double yn = y[i];
659 double zn = z[i];
660 x.push_back(xn * rotmat[0][0] + yn * rotmat[1][0] + zn * rotmat[2][0]);
661 y.push_back(xn * rotmat[0][1] + yn * rotmat[1][1] + zn * rotmat[2][1]);
662 z.push_back(xn * rotmat[0][2] + yn * rotmat[1][2] + zn * rotmat[2][2]);
663 }
664 }
665 }
666
667 void GeneratedMesh::coordinates(int component, std::vector<double> &xyz) const
668 {
669 assert(!doRotation);
670 /* create global coordinates */
671 size_t count = node_count_proc();
672 xyz.reserve(count);
673
674 if (component == 1) {
675 for (size_t m = myStartZ; m < myStartZ + myNumZ + 1; m++) {
676 for (size_t i = 0; i < numY + 1; i++) {
677 for (size_t j = 0; j < numX + 1; j++) {
678 xyz.push_back(sclX * static_cast<double>(j) + offX);
679 }
680 }
681 }
682 }
683 else if (component == 2) {
684 for (size_t m = myStartZ; m < myStartZ + myNumZ + 1; m++) {
685 for (size_t i = 0; i < numY + 1; i++) {
686 for (size_t j = 0; j < numX + 1; j++) {
687 xyz.push_back(sclY * static_cast<double>(i) + offY);
688 }
689 }
690 }
691 }
692 else if (component == 3) {
693 for (size_t m = myStartZ; m < myStartZ + myNumZ + 1; m++) {
694 for (size_t i = 0; i < numY + 1; i++) {
695 for (size_t j = 0; j < numX + 1; j++) {
696 xyz.push_back(sclZ * static_cast<double>(m) + offZ);
697 }
698 }
699 }
700 }
701 }
702
703 void GeneratedMesh::coordinates(int component, int /* zone */, double *xyz) const
704 {
705 assert(!doRotation);
706 /* create global coordinates */
707 if (component == 0) {
708 size_t jjj = 0;
709 for (size_t m = 0; m < numZ + 1; m++) {
710 for (size_t i = 0; i < numY + 1; i++) {
711 for (size_t j = 0; j < numX + 1; j++) {
712 xyz[jjj++] = (sclX * static_cast<double>(j) + offX);
713 xyz[jjj++] = (sclY * static_cast<double>(i) + offY);
714 xyz[jjj++] = (sclZ * static_cast<double>(m) + offZ);
715 }
716 }
717 }
718 }
719 else if (component == 1) {
720 size_t jjj = 0;
721 for (size_t m = 0; m < numZ + 1; m++) {
722 for (size_t i = 0; i < numY + 1; i++) {
723 for (size_t j = 0; j < numX + 1; j++) {
724 xyz[jjj++] = (sclX * static_cast<double>(j) + offX);
725 }
726 }
727 }
728 }
729 else if (component == 2) {
730 size_t jjj = 0;
731 for (size_t m = 0; m < numZ + 1; m++) {
732 for (size_t i = 0; i < numY + 1; i++) {
733 for (size_t j = 0; j < numX + 1; j++) {
734 xyz[jjj++] = (sclY * static_cast<double>(i) + offY);
735 }
736 }
737 }
738 }
739 else if (component == 3) {
740 size_t jjj = 0;
741 for (size_t m = 0; m < numZ + 1; m++) {
742 for (size_t i = 0; i < numY + 1; i++) {
743 for (size_t j = 0; j < numX + 1; j++) {
744 xyz[jjj++] = (sclZ * static_cast<double>(m) + offZ);
745 }
746 }
747 }
748 }
749 }
750
751 void GeneratedMesh::connectivity(int64_t block_number, Ioss::Int64Vector &connect) const
752 {
753 if (block_number == 1) { // HEX Element Block
754 connect.resize(element_count_proc(block_number) * 8);
755 }
756 raw_connectivity(block_number, Data(connect));
757 }
758
759 void GeneratedMesh::connectivity(int64_t block_number, Ioss::IntVector &connect) const
760 {
761 if (block_number == 1) { // HEX Element Block
762 connect.resize(element_count_proc(block_number) * 8);
763 }
764 raw_connectivity(block_number, Data(connect));
765 }
766
767 void GeneratedMesh::connectivity(int64_t block_number, int64_t *connect) const
768 {
769 raw_connectivity(block_number, connect);
770 }
771
772 void GeneratedMesh::connectivity(int64_t block_number, int *connect) const
773 {
774 raw_connectivity(block_number, connect);
775 }
776
777 template <typename INT>
778 void GeneratedMesh::raw_connectivity(int64_t block_number, INT *connect) const
779 {
780 assert(block_number <= structured_block_count());
781
782 INT xp1yp1 = (numX + 1) * (numY + 1);
783
784 /* build connectivity array (node list) for mesh */
785 if (block_number == 1) { // main block elements
786
787 // Hex elements
788 size_t cnt = 0;
789 for (size_t m = myStartZ; m < myNumZ + myStartZ; m++) {
790 for (size_t i = 0, k = 0; i < numY; i++) {
791 for (size_t j = 0; j < numX; j++, k++) {
792 size_t base = (m * xp1yp1) + k + i + 1;
793
794 connect[cnt++] = base;
795 connect[cnt++] = base + 1;
796 connect[cnt++] = base + numX + 2;
797 connect[cnt++] = base + numX + 1;
798
799 connect[cnt++] = xp1yp1 + base;
800 connect[cnt++] = xp1yp1 + base + 1;
801 connect[cnt++] = xp1yp1 + base + numX + 2;
802 connect[cnt++] = xp1yp1 + base + numX + 1;
803 }
804 }
805 }
806 }
807 return;
808 }
809
810 void GeneratedMesh::sideset_elem_sides(int64_t id, Ioss::Int64Vector &elem_sides) const
811 {
812 // id is position in sideset list + 1
813 assert(id > 0 && (size_t)id <= sidesets.size());
814 ShellLocation loc = sidesets[id - 1];
815 element_surface_map(loc, elem_sides);
816 }
817
818 Ioss::NameList GeneratedMesh::sideset_touching_blocks(int64_t /*set_id*/) const
819 {
820 Ioss::NameList result(1, "block_1");
821 return result;
822 }
823
824 void GeneratedMesh::set_variable_count(const std::string &type, size_t count)
825 {
826 if (type == "global") {
827 variableCount[Ioss::REGION] = count;
828 }
829 else if (type == "element") {
830 variableCount[Ioss::ELEMENTBLOCK] = count;
831 }
832 else if (type == "nodal" || type == "node") {
833 variableCount[Ioss::NODEBLOCK] = count;
834 }
835 else if (type == "surface" || type == "sideset") {
836 variableCount[Ioss::SIDEBLOCK] = count;
837 }
838 else {
839 fmt::print(Ioss::WarnOut(),
840 "(Iogs::GeneratedMesh::set_variable_count)\n"
841 " Unrecognized variable type '{}'. Valid types are:\n"
842 " global, element, node, nodal, surface, sideset.\n",
843 type);
844 }
845 }
846
847 void GeneratedMesh::set_rotation(const std::string &axis, double angle_degrees)
848 {
849 // PI / 180. Used in converting angle in degrees to radians
850 static double degang = std::atan2(0.0, -1.0) / 180.0;
851
852 doRotation = true;
853
854 int n1 = -1;
855 int n2 = -1;
856 int n3 = -1;
857
858 if (axis == "x" || axis == "X") {
859 n1 = 1;
860 n2 = 2;
861 n3 = 0;
862 }
863 else if (axis == "y" || axis == "Y") {
864 n1 = 2;
865 n2 = 0;
866 n3 = 1;
867 }
868 else if (axis == "z" || axis == "Z") {
869 n1 = 0;
870 n2 = 1;
871 n3 = 2;
872 }
873 else {
874 fmt::print("\nInvalid axis specification '{}'. Valid options are 'x', 'y', or 'z'\n", axis);
875 return;
876 }
877
878 double ang = angle_degrees * degang; // Convert angle in degrees to radians
879 double cosang = std::cos(ang);
880 double sinang = std::sin(ang);
881
882 assert(n1 >= 0 && n2 >= 0 && n3 >= 0);
883 std::array<std::array<double, 3>, 3> by;
884 by[n1][n1] = cosang;
885 by[n2][n1] = -sinang;
886 by[n1][n3] = 0.0;
887 by[n1][n2] = sinang;
888 by[n2][n2] = cosang;
889 by[n2][n3] = 0.0;
890 by[n3][n1] = 0.0;
891 by[n3][n2] = 0.0;
892 by[n3][n3] = 1.0;
893
894 std::array<std::array<double, 3>, 3> res;
895 for (int i = 0; i < 3; i++) {
896 res[i][0] = rotmat[i][0] * by[0][0] + rotmat[i][1] * by[1][0] + rotmat[i][2] * by[2][0];
897 res[i][1] = rotmat[i][0] * by[0][1] + rotmat[i][1] * by[1][1] + rotmat[i][2] * by[2][1];
898 res[i][2] = rotmat[i][0] * by[0][2] + rotmat[i][1] * by[1][2] + rotmat[i][2] * by[2][2];
899 }
900 rotmat = res;
901 }
902 } // namespace Iogs