Debian Codegraph @

packages/seacas/libraries/ioss/src/main/sphgen.C

raw
1 // Copyright(C) 1999-2024 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 "Ionit_Initializer.h"
8 #include "Ioss_Hex8.h"
9 #include "Ioss_Utils.h"
10 #include <cmath>
11 #include <cstdlib>
12 #include <cstring>
13 #include <fmt/core.h>
14 #include <fmt/format.h>
15 #include <fmt/ranges.h>
16 #include <stdio.h>
17 #include <string>
18 #include <vector>
19
20 #include "Ioss_DBUsage.h"
21 #include "Ioss_DatabaseIO.h"
22 #include "Ioss_ElementBlock.h"
23 #include "Ioss_ElementTopology.h"
24 #include "Ioss_Field.h"
25 #include "Ioss_IOFactory.h"
26 #include "Ioss_NodeBlock.h"
27 #include "Ioss_NodeSet.h"
28 #include "Ioss_ParallelUtils.h"
29 #include "Ioss_Property.h"
30 #include "Ioss_Region.h"
31 #include "Ioss_ScopeGuard.h"
32 #include "Ioss_State.h"
33
34 // ========================================================================
35
36 namespace {
37 struct Globals
38 {
39 std::string working_directory{};
40 double scale_factor{};
41 };
42
43 void hex_volume(Ioss::ElementBlock *block, const std::vector<double> &coordinates,
44 double *centroids, std::vector<double> &volume, std::vector<double> &radius,
45 double scale_factor);
46
47 void show_usage(const std::string &prog);
48
49 void create_sph(const std::string &inpfile, const std::string &input_type,
50 const std::string &outfile, const std::string &output_type, Globals &globals);
51 } // namespace
52 // ========================================================================
53
54 namespace {
55 std::string codename;
56 std::string version = "0.91";
57 } // namespace
58
59 int main(int argc, char *argv[])
60 {
61 #ifdef SEACAS_HAVE_MPI
62 MPI_Init(&argc, &argv);
63 ON_BLOCK_EXIT(MPI_Finalize);
64 #endif
65
66 std::string in_type = "exodus";
67 std::string out_type = "exodus";
68
69 Globals globals;
70
71 codename = argv[0];
72 size_t ind = codename.find_last_of('/', codename.size());
73 if (ind != std::string::npos) {
74 codename = codename.substr(ind + 1, codename.size());
75 }
76
77 Ioss::Init::Initializer io;
78
79 std::string input_file;
80 globals.scale_factor = 8.0;
81
82 // Skip past any options...
83 int i = 1;
84 while (i < argc && argv[i][0] == '-') {
85 if (std::strcmp("-directory", argv[i]) == 0 || std::strcmp("--directory", argv[i]) == 0 ||
86 std::strcmp("-d", argv[i]) == 0) {
87 i++;
88 globals.working_directory = argv[i++];
89 }
90 else if (std::strcmp("--in_type", argv[i]) == 0) {
91 i++;
92 in_type = argv[i++];
93 }
94 else if (std::strcmp("--out_type", argv[i]) == 0) {
95 i++;
96 out_type = argv[i++];
97 }
98 else if (std::strcmp("-i", argv[i]) == 0 || std::strcmp("--input", argv[i]) == 0) {
99 i++;
100 input_file = argv[i++];
101 }
102 else if (std::strcmp("--scale_factor", argv[i]) == 0) {
103 i++;
104 globals.scale_factor = std::strtod(argv[i++], nullptr);
105 }
106
107 // Found an option. See if it has an argument...
108 else if (i + 1 < argc && argv[i + 1][0] == '-') {
109 // No argument, another option
110 i++;
111 }
112 else {
113 // Skip the argument...
114 i += 2;
115 }
116 }
117
118 std::string in_file;
119 std::string out_file;
120
121 // If two or more arguments are specified at end of command line, they are
122 // the input and output files to be converted.
123 // The file types are assumed to be as 'hardwired' above...
124
125 std::string cwd = globals.working_directory;
126
127 if (input_file.empty()) {
128 if (argc - i == 2) {
129 in_file = Ioss::Utils::local_filename(argv[i++], in_type, cwd);
130 out_file = Ioss::Utils::local_filename(argv[i++], out_type, cwd);
131 }
132 }
133
134 if (in_file.empty() || out_file.empty()) {
135 show_usage(codename);
136 return (EXIT_FAILURE);
137 }
138
139 fmt::print(stderr, "Input: '{}', Type: {}\n", in_file, in_type);
140 fmt::print(stderr, "Output: '{}', Type: {}\n\n", out_file, out_type);
141
142 create_sph(in_file, in_type, out_file, out_type, globals);
143
144 fmt::print(stderr, "\n{} execution successful.\n", codename);
145 return EXIT_SUCCESS;
146 }
147
148 namespace {
149 void show_usage(const std::string &prog)
150 {
151 fmt::print(
152 stderr,
153 "\nUSAGE: {} in_file out_file\n"
154 "...or: {} command_file\n"
155 " version: {}\n\n"
156 "\tConverts all HEX element blocks to SPHERE element blocks\n"
157 "\tand creates a nodeset for each element block containing the node at the center of "
158 "the sphere.\n"
159 "\tignores all other element block types and deletes all existing nodesets.\n"
160 "Options:\n"
161 "\t--directory or -d {{dir}} : specifies current working directory\n"
162 "\t--input or -i {{file}} : read input and output filename data from file\n"
163 "\t--in_type {{pamgen|generated|exodus}} : set input type to the argument. Default "
164 "exodus\n"
165 "\t--out_type {{exodus}} : set output type to the argument. Default exodus\n"
166 "\t--scale_factor : radius = (volume/scale_factor)^1/3; default = 8.0\n"
167 "\t\t which gives the half-side-length of a cube with that volume\n"
168 "\t\t use 4*pi/3 = 4.18879 for the radius of a sphere with that volume.\n",
169 prog, prog, version);
170
171 Ioss::NameList db_types = Ioss::IOFactory::describe();
172 fmt::print(stderr, "\nSupports database types:\n\t{}\n\n", fmt::join(db_types, " "));
173 }
174
175 void create_sph(const std::string &inpfile, const std::string &input_type,
176 const std::string &outfile, const std::string &output_type, Globals &globals)
177 {
178 //========================================================================
179 // INPUT ...
180 // NOTE: The "READ_RESTART" mode ensures that the node and element ids will be mapped.
181 //========================================================================
182 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(input_type, inpfile, Ioss::READ_RESTART,
183 Ioss::ParallelUtils::comm_world());
184 if (dbi == nullptr || !dbi->ok(true)) {
185 std::exit(EXIT_FAILURE);
186 }
187
188 // NOTE: 'region' owns 'db' pointer at this time...
189 Ioss::Region region(dbi, "region_1");
190
191 if (!region.get_nodesets().empty()) {
192 fmt::print(stderr, " *** All nodesets will be replaced by element block nodesets\n");
193 }
194
195 //========================================================================
196 // OUTPUT ...
197 //========================================================================
198 Ioss::DatabaseIO *dbo = Ioss::IOFactory::create(output_type, outfile, Ioss::WRITE_RESTART,
199 Ioss::ParallelUtils::comm_world());
200 if (dbo == nullptr || !dbo->ok(true)) {
201 std::exit(EXIT_FAILURE);
202 }
203
204 // NOTE: 'output_region' owns 'dbo' pointer at this time
205 Ioss::Region output_region(dbo, "region_2");
206
207 // Set the qa information...
208 output_region.property_add(Ioss::Property(std::string("code_name"), codename));
209 output_region.property_add(Ioss::Property(std::string("code_version"), version));
210
211 // Get all elementblocks.
212 // ... For all elementblocks of type 'HEX8'
213 // ... Calculate the element volume and the radius of a sphere
214 // which would have that same volume.
215 // ... Output a new element block of type 'SPHERE' with
216 // attributes volume and radius
217 // ... Output a nodeset with same id containing all nodes
218 // that are in the element block.
219 // ...
220
221 // Create the output mesh...
222 output_region.begin_mode(Ioss::STATE_DEFINE_MODEL);
223
224 // Iterate through all element blocks...
225 // The first time, just count the number of hex elements since
226 // that will be the number of output element and nodes in the
227 // sphere mesh.
228 size_t sph_node_count = 0;
229 const auto &ebs = region.get_element_blocks();
230 auto I = ebs.begin();
231 while (I != ebs.end()) {
232 Ioss::ElementBlock *eb = *I;
233 ++I;
234 std::string type = eb->topology()->name();
235 std::string name = eb->name();
236 if (type == Ioss::Hex8::name) {
237 sph_node_count += eb->entity_count();
238
239 // Add the element block...
240 int num_elem = eb->entity_count();
241 auto *ebn = new Ioss::ElementBlock(output_region.get_database(), name, "sphere", num_elem);
242 int id = eb->get_property("id").get_int();
243 ebn->property_add(Ioss::Property("id", id));
244 ebn->field_add(Ioss::Field("radius", Ioss::Field::REAL, "scalar", Ioss::Field::ATTRIBUTE,
245 num_elem, 1));
246 ebn->field_add(Ioss::Field("volume", Ioss::Field::REAL, "scalar", Ioss::Field::ATTRIBUTE,
247 num_elem, 2));
248 output_region.add(ebn);
249
250 // Add a nodeset for each sph element block...
251 name += "_nodes";
252 auto ns = new Ioss::NodeSet(output_region.get_database(), name, num_elem);
253 ns->property_add(Ioss::Property("id", id));
254 output_region.add(ns);
255 }
256 else {
257 fmt::print("Skipping block {} which contains {} elements.\n", name, type);
258 }
259 }
260
261 // Define a node block...
262 std::string block_name = "nodeblock_1";
263 int spatial_dimension = region.get_property("spatial_dimension").get_int();
264 auto block = new Ioss::NodeBlock(output_region.get_database(), block_name, sph_node_count,
265 spatial_dimension);
266 block->property_add(Ioss::Property("id", 1));
267 output_region.add(block);
268
269 output_region.end_mode(Ioss::STATE_DEFINE_MODEL);
270
271 output_region.begin_mode(Ioss::STATE_MODEL);
272
273 auto *nb = region.get_node_blocks()[0];
274 std::vector<double> coordinates;
275 nb->get_field_data("mesh_model_coordinates", coordinates);
276
277 auto *onb = output_region.get_node_blocks()[0];
278 std::vector<int> node_ids(sph_node_count);
279 for (size_t i = 0; i < sph_node_count; i++) {
280 node_ids[i] = i + 1;
281 }
282 onb->put_field_data("ids", node_ids);
283
284 std::vector<double> centroids(spatial_dimension * sph_node_count);
285
286 I = ebs.begin();
287 size_t offset = 0;
288 while (I != ebs.end()) {
289 if ((*I)->topology()->name() == Ioss::Hex8::name) {
290
291 std::vector<double> volume;
292 std::vector<double> radius;
293
294 hex_volume(*I, coordinates, &centroids[3 * offset], volume, radius, globals.scale_factor);
295
296 // Find corresponding output element block...
297 auto *output_eb = output_region.get_element_block((*I)->name());
298 if (output_eb == nullptr) {
299 fmt::print(stderr, "ERROR: Could not find output element block '{}'\n", (*I)->name());
300 std::exit(EXIT_FAILURE);
301 }
302
303 auto *output_ns = output_region.get_nodeset((*I)->name() + "_nodes");
304 if (output_ns == nullptr) {
305 fmt::print(stderr, "ERROR: Could not find output node set '{}_nodes'\n", (*I)->name());
306 std::exit(EXIT_FAILURE);
307 }
308
309 // Connectivity for the sphere element blocks is just their local element location
310 size_t num_elem = output_eb->entity_count();
311 std::vector<int> connectivity(num_elem);
312 for (size_t i = 0; i < num_elem; i++) {
313 connectivity[i] = offset + 1 + i;
314 }
315 output_eb->put_field_data("ids", connectivity);
316 output_eb->put_field_data("connectivity_raw", connectivity);
317 output_eb->put_field_data("volume", volume);
318 output_eb->put_field_data("radius", radius);
319
320 output_ns->put_field_data("ids", connectivity);
321
322 offset += output_eb->entity_count();
323 }
324 ++I;
325 }
326
327 onb->put_field_data("mesh_model_coordinates", centroids);
328
329 output_region.end_mode(Ioss::STATE_MODEL);
330 }
331
332 void comp_grad12x(double *const grad_ptr, const double *const x, const double *const y,
333 const double *const z)
334 {
335 double R42 = (z[3] - z[1]);
336 double R52 = (z[4] - z[1]);
337 double R54 = (z[4] - z[3]);
338
339 double R63 = (z[5] - z[2]);
340 double R83 = (z[7] - z[2]);
341 double R86 = (z[7] - z[5]);
342
343 double R31 = (z[2] - z[0]);
344 double R61 = (z[5] - z[0]);
345 double R74 = (z[6] - z[3]);
346
347 double R72 = (z[6] - z[1]);
348 double R75 = (z[6] - z[4]);
349 double R81 = (z[7] - z[0]);
350
351 double t1 = (R63 + R54);
352 double t2 = (R61 + R74);
353 double t3 = (R72 + R81);
354
355 double t4 = (R86 + R42);
356 double t5 = (R83 + R52);
357 double t6 = (R75 + R31);
358
359 grad_ptr[0] =
360 (y[1] * t1) - (y[2] * R42) - (y[3] * t5) + (y[4] * t4) + (y[5] * R52) - (y[7] * R54);
361 grad_ptr[1] =
362 (y[2] * t2) + (y[3] * R31) - (y[0] * t1) - (y[5] * t6) + (y[6] * R63) - (y[4] * R61);
363 grad_ptr[2] =
364 (y[3] * t3) + (y[0] * R42) - (y[1] * t2) - (y[6] * t4) + (y[7] * R74) - (y[5] * R72);
365 grad_ptr[3] =
366 (y[0] * t5) - (y[1] * R31) - (y[2] * t3) + (y[7] * t6) + (y[4] * R81) - (y[6] * R83);
367 grad_ptr[4] =
368 (y[5] * t3) + (y[6] * R86) - (y[7] * t2) - (y[0] * t4) - (y[3] * R81) + (y[1] * R61);
369 grad_ptr[5] =
370 (y[6] * t5) - (y[4] * t3) - (y[7] * R75) + (y[1] * t6) - (y[0] * R52) + (y[2] * R72);
371 grad_ptr[6] =
372 (y[7] * t1) - (y[5] * t5) - (y[4] * R86) + (y[2] * t4) - (y[1] * R63) + (y[3] * R83);
373 grad_ptr[7] =
374 (y[4] * t2) - (y[6] * t1) + (y[5] * R75) - (y[3] * t6) - (y[2] * R74) + (y[0] * R54);
375
376 R42 = (x[3] - x[1]);
377 R52 = (x[4] - x[1]);
378 R54 = (x[4] - x[3]);
379
380 R63 = (x[5] - x[2]);
381 R83 = (x[7] - x[2]);
382 R86 = (x[7] - x[5]);
383
384 R31 = (x[2] - x[0]);
385 R61 = (x[5] - x[0]);
386 R74 = (x[6] - x[3]);
387
388 R72 = (x[6] - x[1]);
389 R75 = (x[6] - x[4]);
390 R81 = (x[7] - x[0]);
391
392 t1 = (R63 + R54);
393 t2 = (R61 + R74);
394 t3 = (R72 + R81);
395
396 t4 = (R86 + R42);
397 t5 = (R83 + R52);
398 t6 = (R75 + R31);
399
400 grad_ptr[8] =
401 (z[1] * t1) - (z[2] * R42) - (z[3] * t5) + (z[4] * t4) + (z[5] * R52) - (z[7] * R54);
402 grad_ptr[9] =
403 (z[2] * t2) + (z[3] * R31) - (z[0] * t1) - (z[5] * t6) + (z[6] * R63) - (z[4] * R61);
404 grad_ptr[10] =
405 (z[3] * t3) + (z[0] * R42) - (z[1] * t2) - (z[6] * t4) + (z[7] * R74) - (z[5] * R72);
406 grad_ptr[11] =
407 (z[0] * t5) - (z[1] * R31) - (z[2] * t3) + (z[7] * t6) + (z[4] * R81) - (z[6] * R83);
408 grad_ptr[12] =
409 (z[5] * t3) + (z[6] * R86) - (z[7] * t2) - (z[0] * t4) - (z[3] * R81) + (z[1] * R61);
410 grad_ptr[13] =
411 (z[6] * t5) - (z[4] * t3) - (z[7] * R75) + (z[1] * t6) - (z[0] * R52) + (z[2] * R72);
412 grad_ptr[14] =
413 (z[7] * t1) - (z[5] * t5) - (z[4] * R86) + (z[2] * t4) - (z[1] * R63) + (z[3] * R83);
414 grad_ptr[15] =
415 (z[4] * t2) - (z[6] * t1) + (z[5] * R75) - (z[3] * t6) - (z[2] * R74) + (z[0] * R54);
416
417 R42 = (y[3] - y[1]);
418 R52 = (y[4] - y[1]);
419 R54 = (y[4] - y[3]);
420
421 R63 = (y[5] - y[2]);
422 R83 = (y[7] - y[2]);
423 R86 = (y[7] - y[5]);
424
425 R31 = (y[2] - y[0]);
426 R61 = (y[5] - y[0]);
427 R74 = (y[6] - y[3]);
428
429 R72 = (y[6] - y[1]);
430 R75 = (y[6] - y[4]);
431 R81 = (y[7] - y[0]);
432
433 t1 = (R63 + R54);
434 t2 = (R61 + R74);
435 t3 = (R72 + R81);
436
437 t4 = (R86 + R42);
438 t5 = (R83 + R52);
439 t6 = (R75 + R31);
440
441 grad_ptr[16] =
442 (x[1] * t1) - (x[2] * R42) - (x[3] * t5) + (x[4] * t4) + (x[5] * R52) - (x[7] * R54);
443 grad_ptr[17] =
444 (x[2] * t2) + (x[3] * R31) - (x[0] * t1) - (x[5] * t6) + (x[6] * R63) - (x[4] * R61);
445 grad_ptr[18] =
446 (x[3] * t3) + (x[0] * R42) - (x[1] * t2) - (x[6] * t4) + (x[7] * R74) - (x[5] * R72);
447 grad_ptr[19] =
448 (x[0] * t5) - (x[1] * R31) - (x[2] * t3) + (x[7] * t6) + (x[4] * R81) - (x[6] * R83);
449 grad_ptr[20] =
450 (x[5] * t3) + (x[6] * R86) - (x[7] * t2) - (x[0] * t4) - (x[3] * R81) + (x[1] * R61);
451 grad_ptr[21] =
452 (x[6] * t5) - (x[4] * t3) - (x[7] * R75) + (x[1] * t6) - (x[0] * R52) + (x[2] * R72);
453 grad_ptr[22] =
454 (x[7] * t1) - (x[5] * t5) - (x[4] * R86) + (x[2] * t4) - (x[1] * R63) + (x[3] * R83);
455 grad_ptr[23] =
456 (x[4] * t2) - (x[6] * t1) + (x[5] * R75) - (x[3] * t6) - (x[2] * R74) + (x[0] * R54);
457 }
458
459 inline double dot8(const double *const x1, const double *const x2)
460 {
461 double d1 = x1[0] * x2[0] + x1[1] * x2[1];
462 double d2 = x1[2] * x2[2] + x1[3] * x2[3];
463 double d4 = x1[4] * x2[4] + x1[5] * x2[5];
464 double d5 = x1[6] * x2[6] + x1[7] * x2[7];
465 return d1 + d2 + d4 + d5;
466 }
467
468 void hex_volume(Ioss::ElementBlock *block, const std::vector<double> &coordinates,
469 double *centroids, std::vector<double> &volume, std::vector<double> &radius,
470 double scale_factor)
471 {
472 const double one12th = 1.0 / 12.0;
473 size_t nelem = block->entity_count();
474 std::vector<int> connectivity;
475 block->get_field_data("connectivity_raw", connectivity);
476
477 volume.resize(nelem);
478 radius.resize(nelem);
479
480 double gradop12x[24];
481 double x[8];
482 double y[8];
483 double z[8];
484
485 for (size_t ielem = 0; ielem < nelem; ++ielem) {
486 for (size_t j = 0; j < 8; j++) {
487 size_t node = connectivity[ielem * 8 + j] - 1;
488 x[j] = coordinates[node * 3 + 0];
489 y[j] = coordinates[node * 3 + 1];
490 z[j] = coordinates[node * 3 + 2];
491 }
492
493 centroids[ielem * 3 + 0] = .125 * (x[0] + x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7]);
494 centroids[ielem * 3 + 1] = .125 * (y[0] + y[1] + y[2] + y[3] + y[4] + y[5] + y[6] + y[7]);
495 centroids[ielem * 3 + 2] = .125 * (z[0] + z[1] + z[2] + z[3] + z[4] + z[5] + z[6] + z[7]);
496
497 comp_grad12x(&gradop12x[0], x, y, z);
498 const double volume12x = dot8(x, &gradop12x[0]);
499 volume[ielem] = volume12x * one12th;
500 radius[ielem] = std::pow(volume[ielem] / scale_factor, (1. / 3.));
501 }
502 }
503 } // namespace