Debian Codegraph @

packages/seacas/applications/nem_slice/elb_exo_util.C

raw
1 /*
2 * Copyright(C) 1999-2025 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include <exodusII.h> // for ex_close, ex_inquire, etc
10
11 #include <algorithm>
12 #include <cassert>
13 #include <copy_string_cpp.h>
14 #include <cstddef> // for size_t
15 #include <cstdio> // for nullptr
16 #include <cstdlib> // for malloc, free, calloc
17 #include <cstring> // for strlen
18 #include <fmt/ostream.h>
19 #include <string>
20 #include <vector>
21
22 #include "elb.h" // for Weight_Description, etc
23 #include "elb_elem.h" // for get_elem_type, ElementType, etc
24 #include "elb_err.h" // for Gen_Error, MAX_ERR_MSG
25 #include "elb_exo.h"
26 #include "elb_groups.h" // for parse_groups
27 #include "elb_util.h" // for in_list, roundfloat
28 #include "vector_data.h"
29
30 /*****************************************************************************/
31 /*****************************************************************************/
32 /*****************************************************************************/
33 /* Function read_exo_weights() begins:
34 *----------------------------------------------------------------------------
35 * This function reads the nodal or elemental values from an ExodusII file
36 * which will be used by Chaco for weighting of the graph.
37 *****************************************************************************/
38 template int read_exo_weights(Problem_Description *prob, Weight_Description *weight, int dummy);
39 template int read_exo_weights(Problem_Description *prob, Weight_Description *weight, int64_t dummy);
40
41 template <typename INT>
42 int read_exo_weights(Problem_Description *prob, Weight_Description *weight, INT /*dummy*/)
43 {
44 int exoid;
45 /*---------------------------Execution Begins--------------------------------*/
46
47 /* Open the ExodusII file containing the weights */
48 int mode = EX_READ | prob->int64api;
49 int cpu_ws = 0;
50 int io_ws = 0;
51 float version;
52 if ((exoid = ex_open(weight->exo_filename.c_str(), mode, &cpu_ws, &io_ws, &version)) < 0) {
53 std::string ctemp = fmt::format("fatal: could not open ExodusII file {}", weight->exo_filename);
54 Gen_Error(0, ctemp);
55 return 0;
56 }
57
58 std::vector<float> values(weight->nvals);
59 if (prob->type == DecompType::NODAL) {
60 size_t tmp_nodes = ex_inquire_int(exoid, EX_INQ_NODES);
61 /* check to make sure the sizes agree */
62 if ((size_t)weight->nvals != tmp_nodes) {
63 Gen_Error(0, "fatal: different number of nodes in mesh and weight files");
64 ex_close(exoid);
65 return 0;
66 }
67
68 weight->ow.resize(weight->nvals);
69 /* Read in the nodal values */
70 if (ex_get_var(exoid, weight->exo_tindx, EX_NODAL, weight->exo_vindx, 1, weight->nvals,
71 Data(values)) < 0) {
72 Gen_Error(0, "fatal: unable to read nodal values");
73 ex_close(exoid);
74 return 0;
75 }
76 }
77 else {
78 size_t tmp_elem = ex_inquire_int(exoid, EX_INQ_ELEM);
79 /* check to make sure the sizes agree */
80 if ((size_t)weight->nvals != tmp_elem) {
81 Gen_Error(0, "fatal: different number of elems in mesh and weight files");
82 ex_close(exoid);
83 return 0;
84 }
85
86 /* Get the number of element blocks */
87 int neblks = ex_inquire_int(exoid, EX_INQ_ELEM_BLK);
88 std::vector<INT> eblk_ids(neblks);
89 std::vector<INT> eblk_ecnts(neblks);
90
91 if (ex_get_ids(exoid, EX_ELEM_BLOCK, Data(eblk_ids)) < 0) {
92 Gen_Error(0, "fatal: unable to get element block IDs");
93 ex_close(exoid);
94 return 0;
95 }
96
97 /* Get the count of elements in each element block */
98 for (int cnt = 0; cnt < neblks; cnt++) {
99 INT dum1;
100 INT dum2;
101 char elem_type[MAX_STR_LENGTH + 1];
102 if (ex_get_block(exoid, EX_ELEM_BLOCK, eblk_ids[cnt], elem_type, &(eblk_ecnts[cnt]), &dum1,
103 nullptr, nullptr, &dum2) < 0) {
104 Gen_Error(0, "fatal: unable to get element block");
105 ex_close(exoid);
106 return 0;
107 }
108 }
109
110 /* Get the element variables */
111 size_t offset = 0;
112 for (int cnt = 0; cnt < neblks; cnt++) {
113 if (ex_get_var(exoid, weight->exo_tindx, EX_ELEM_BLOCK, weight->exo_vindx, eblk_ids[cnt],
114 eblk_ecnts[cnt], &(values[offset])) < 0) {
115 Gen_Error(0, "fatal: unable to get element variable");
116 ex_close(exoid);
117 return 0;
118 }
119 offset += eblk_ecnts[cnt];
120 }
121 }
122
123 /* Close the ExodusII weighting file */
124 if (ex_close(exoid) < 0) {
125 std::string ctemp =
126 fmt::format("warning: failed to close ExodusII file {}", weight->exo_filename);
127 Gen_Error(0, ctemp);
128 }
129
130 /* now I need to translate the values to positive integers */
131
132 /* first find the minimum value */
133 float minval = *std::min_element(values.begin(), values.end());
134
135 /* now translate the values to be greater than 1 and convert to ints */
136 for (int cnt = 0; cnt < weight->nvals; cnt++) {
137 values[cnt] += 1.0f - minval;
138 weight->vertices[cnt] = roundfloat(values[cnt]);
139 }
140 return 1;
141 } /*------------------------End read_exo_weights()----------------------*/
142
143 /*****************************************************************************/
144 /*****************************************************************************/
145
146 /*****************************************************************************/
147 /* Function read_mesh_params() begins:
148 *----------------------------------------------------------------------------
149 * This function reads in information about the finite element mesh.
150 *****************************************************************************/
151 template int read_mesh_params(const std::string &exo_file, Problem_Description *problem,
152 Mesh_Description<int> *mesh, Sphere_Info *sphere);
153 template int read_mesh_params(const std::string &exo_file, Problem_Description *problem,
154 Mesh_Description<int64_t> *mesh, Sphere_Info *sphere);
155
156 template <typename INT>
157 int read_mesh_params(const std::string &exo_file, Problem_Description *problem,
158 Mesh_Description<INT> *mesh, Sphere_Info *sphere)
159 {
160 int exoid;
161 int cpu_ws = 0;
162 int io_ws = 0;
163 float version;
164 char elem_type[MAX_STR_LENGTH + 1];
165 /*---------------------------Execution Begins--------------------------------*/
166
167 /* Open the ExodusII geometry file */
168 int mode = EX_READ | problem->int64api;
169 if ((exoid = ex_open(exo_file.c_str(), mode, &cpu_ws, &io_ws, &version)) < 0) {
170 Gen_Error(0, "fatal: unable to open ExodusII file for mesh params");
171 return 0;
172 }
173
174 exoid += problem->selected_change_set;
175
176 /* Get the init info */
177 ex_init_params exo{};
178 if (ex_get_init_ext(exoid, &exo)) {
179 Gen_Error(0, "fatal: unable to get init info from ExodusII file");
180 ex_close(exoid);
181 return 0;
182 }
183 copy_string(mesh->title, exo.title);
184 mesh->num_dims = exo.num_dim;
185 mesh->num_nodes = exo.num_nodes;
186 mesh->num_elems = exo.num_elem;
187 mesh->num_el_blks = exo.num_elem_blk;
188 mesh->num_node_sets = exo.num_node_sets;
189 mesh->num_side_sets = exo.num_side_sets;
190
191 /* Read the element block IDs */
192 mesh->eb_ids.resize(mesh->num_el_blks);
193 mesh->eb_cnts.resize(mesh->num_el_blks);
194 mesh->eb_npe.resize(mesh->num_el_blks);
195 mesh->eb_type.resize(mesh->num_el_blks);
196
197 if (ex_get_ids(exoid, EX_ELEM_BLOCK, Data(mesh->eb_ids)) < 0) {
198 Gen_Error(0, "fatal: unable to get element block IDs");
199 ex_close(exoid);
200 return 0;
201 }
202
203 /* Get the length of the concatenated node set node list */
204 if (mesh->num_node_sets > 0) {
205 mesh->ns_list_len = ex_inquire_int(exoid, EX_INQ_NS_NODE_LEN);
206 }
207 else {
208 mesh->ns_list_len = 0;
209 }
210
211 /* Allocate and initialize memory for the sphere adjustment */
212 sphere->adjust.resize(mesh->num_el_blks);
213 sphere->begin.resize(mesh->num_el_blks);
214 sphere->end.resize(mesh->num_el_blks);
215
216 /* Determine the maximum number of nodes per element */
217 mesh->max_np_elem = 0;
218 for (size_t cnt = 0; cnt < mesh->num_el_blks; cnt++) {
219 INT nodes_in_elem;
220
221 if (ex_get_block(exoid, EX_ELEM_BLOCK, mesh->eb_ids[cnt], elem_type, &(mesh->eb_cnts[cnt]),
222 &nodes_in_elem, nullptr, nullptr, nullptr) < 0) {
223 Gen_Error(0, "fatal: unable to get element block");
224 ex_close(exoid);
225 return 0;
226 }
227
228 if (mesh->eb_cnts[cnt] == 0) {
229 continue;
230 }
231
232 mesh->eb_npe[cnt] = nodes_in_elem;
233 mesh->eb_type[cnt] = get_elem_type(elem_type, nodes_in_elem, mesh->num_dims);
234
235 if (cnt == 0) {
236 sphere->end[0] = mesh->eb_cnts[cnt];
237 }
238
239 if (mesh->eb_type[cnt] == ElementType::SPHERE && problem->no_sph != 1) {
240 sphere->num += mesh->eb_cnts[cnt];
241 sphere->adjust[cnt] = 0;
242 }
243 else {
244 sphere->adjust[cnt] = sphere->num;
245 }
246
247 if (cnt != 0) {
248 sphere->begin[cnt] = sphere->end[cnt - 1];
249 sphere->end[cnt] = sphere->begin[cnt] + mesh->eb_cnts[cnt];
250 }
251
252 mesh->max_np_elem = std::max(mesh->max_np_elem, (size_t)nodes_in_elem);
253 }
254
255 /* Close the ExodusII file */
256 if (ex_close(exoid) < 0) {
257 Gen_Error(1, "warning: unable to close ExodusII file");
258 }
259
260 fmt::print("ExodusII mesh information\n");
261 if (strlen(mesh->title) > 0) {
262 fmt::print("\ttitle: {}\n", mesh->title);
263 }
264 fmt::print("\tgeometry dimension: {}\n", mesh->num_dims);
265 fmt::print("\tnumber of nodes: {}\tnumber of elements: {}\n", fmt::group_digits(mesh->num_nodes),
266 fmt::group_digits(mesh->num_elems));
267 fmt::print("\tnumber of element blocks: {}\n", mesh->num_el_blks);
268 fmt::print("\tnumber of node sets: {}\tnumber of side sets: {}\n", mesh->num_node_sets,
269 mesh->num_side_sets);
270
271 return 1;
272
273 } /*--------------------------End read_mesh_params()-------------------------*/
274
275 /*****************************************************************************/
276 /*****************************************************************************/
277 /*****************************************************************************/
278 /* Function read_mesh_params() begins:
279 *----------------------------------------------------------------------------
280 * This function reads in the finite element mesh.
281 *****************************************************************************/
282 template int read_mesh(const std::string &exo_file, Problem_Description *problem,
283 Mesh_Description<int> *mesh, Weight_Description *weight);
284 template int read_mesh(const std::string &exo_file, Problem_Description *problem,
285 Mesh_Description<int64_t> *mesh, Weight_Description *weight);
286
287 template <typename INT>
288 int read_mesh(const std::string &exo_file, Problem_Description *problem,
289 Mesh_Description<INT> *mesh, Weight_Description *weight)
290 {
291 float version;
292 float *xptr;
293 float *yptr;
294 float *zptr;
295 /*---------------------------Execution Begins--------------------------------*/
296
297 /* Open the ExodusII file */
298 int exoid;
299 int cpu_ws = 0;
300 int io_ws = 0;
301 int mode = EX_READ | problem->int64api;
302 if ((exoid = ex_open(exo_file.c_str(), mode, &cpu_ws, &io_ws, &version)) < 0) {
303 Gen_Error(0, "fatal: unable to open ExodusII mesh file");
304 return 0;
305 }
306
307 exoid += problem->selected_change_set;
308
309 /* Read the coordinates, if desired */
310 xptr = yptr = zptr = nullptr;
311
312 if (problem->read_coords == ELB_TRUE) {
313 switch (mesh->num_dims) {
314 case 3: zptr = Data(mesh->coords) + 2 * (mesh->num_nodes); FALL_THROUGH;
315 case 2: yptr = Data(mesh->coords) + (mesh->num_nodes); FALL_THROUGH;
316 case 1: xptr = Data(mesh->coords);
317 }
318
319 if (ex_get_coord(exoid, xptr, yptr, zptr) < 0) {
320 Gen_Error(0, "fatal: unable to read coordinate values for mesh");
321 return 0;
322 }
323
324 } /* End "if(problem->read_coords == ELB_TRUE)" */
325
326 /* Read the element connectivity */
327 size_t gelem_cnt = 0;
328 for (size_t cnt = 0; cnt < mesh->num_el_blks; cnt++) {
329 if (mesh->eb_cnts[cnt] == 0) {
330 continue;
331 }
332
333 std::vector<INT> blk_connect(mesh->eb_cnts[cnt] * mesh->eb_npe[cnt]);
334
335 /* Get the connectivity for this element block */
336 if (ex_get_conn(exoid, EX_ELEM_BLOCK, mesh->eb_ids[cnt], Data(blk_connect), nullptr, nullptr) <
337 0) {
338 Gen_Error(0, "fatal: failed to get element connectivity");
339 return 0;
340 }
341
342 /* find out if this element block is weighted */
343 int wgt = -1;
344 if (weight->type & WeightingOptions::EL_BLK) {
345 wgt = in_list(mesh->eb_ids[cnt], weight->elemblk);
346 }
347
348 /* Fill the 2D global connectivity array */
349 if (((problem->type == DecompType::ELEMENTAL) && (weight->type & WeightingOptions::EL_BLK)) ||
350 ((problem->type == DecompType::NODAL) && (weight->type & WeightingOptions::EL_BLK))) {
351
352 for (int64_t cnt2 = 0; cnt2 < mesh->eb_cnts[cnt]; cnt2++) {
353 mesh->elem_type[gelem_cnt] = mesh->eb_type[cnt];
354
355 /* while going through the blocks, take care of the weighting */
356 if ((problem->type == DecompType::ELEMENTAL) && (weight->type & WeightingOptions::EL_BLK)) {
357 /* is this block weighted */
358 if (wgt >= 0) {
359 /* check if there is a read value */
360 if (weight->vertices[gelem_cnt] >= 1) {
361 /* and if it should be overwritten */
362 if (weight->ow_read) {
363 weight->vertices[gelem_cnt] = weight->elemblk_wgt[wgt];
364 }
365 }
366 else {
367 weight->vertices[gelem_cnt] = weight->elemblk_wgt[wgt];
368 }
369 }
370 else {
371 /* now check if this weight has been initialized */
372 if (weight->vertices[gelem_cnt] < 1) {
373 weight->vertices[gelem_cnt] = 1;
374 }
375 }
376 }
377
378 for (int64_t cnt3 = 0; cnt3 < mesh->eb_npe[cnt]; cnt3++) {
379 INT node = blk_connect[cnt3 + cnt2 * mesh->eb_npe[cnt]] - 1;
380 assert(node >= 0);
381 mesh->connect[gelem_cnt][cnt3] = node;
382
383 /* deal with the weighting if necessary */
384 if ((problem->type == DecompType::NODAL) && (weight->type & EL_BLK)) {
385 /* is this block weighted */
386 if (wgt >= 0) {
387 /* check if I read an exodus file */
388 if (weight->type & READ_EXO) {
389 /* check if it can be overwritten */
390 if (weight->ow_read) {
391 /* check if it has been overwritten already */
392 if (weight->ow[node]) {
393 weight->vertices[node] =
394 std::max(weight->vertices[node], weight->elemblk_wgt[wgt]);
395 }
396 else {
397 weight->vertices[node] = weight->elemblk_wgt[wgt];
398 weight->ow[node] = 1; /* read value has been overwritten */
399 }
400 }
401 }
402 else {
403 weight->vertices[node] = std::max(weight->vertices[node], weight->elemblk_wgt[wgt]);
404 }
405 }
406 else {
407 /* now check if this weight has been initialized */
408 if (weight->vertices[node] < 1) {
409 weight->vertices[node] = 1;
410 }
411 }
412 }
413 }
414 gelem_cnt++;
415 }
416 }
417 else {
418 // No weights...
419 for (int64_t cnt2 = 0; cnt2 < mesh->eb_cnts[cnt]; cnt2++) {
420 mesh->elem_type[gelem_cnt] = mesh->eb_type[cnt];
421
422 for (int64_t cnt3 = 0; cnt3 < mesh->eb_npe[cnt]; cnt3++) {
423 INT node = blk_connect[cnt2 * mesh->eb_npe[cnt] + cnt3] - 1;
424 assert(node >= 0);
425 mesh->connect[gelem_cnt][cnt3] = node;
426 }
427 gelem_cnt++;
428 }
429 }
430 } /* End "for(cnt=0; cnt < mesh->num_el_blks; cnt++)" */
431
432 /* if there is a group designator, then parse it here */
433 if (problem->groups != nullptr) {
434 if (!parse_groups(mesh, problem)) {
435 Gen_Error(0, "fatal: unable to parse group designator");
436 ex_close(exoid);
437 return 0;
438 }
439 }
440 else {
441 problem->num_groups = 1; /* there is always one group */
442 }
443
444 /* Close the ExodusII file */
445 if (ex_close(exoid) < 0) {
446 Gen_Error(0, "warning: failed to close ExodusII mesh file");
447 }
448
449 return 1;
450
451 } /*---------------------------End read_mesh()-------------------------------*/
452
453 /*****************************************************************************/
454 /*****************************************************************************/
455 /*****************************************************************************/
456 /* Function init_weight_struct() begins:
457 *----------------------------------------------------------------------------
458 * This function initializes the weight structure given the current mesh.
459 *****************************************************************************/
460 template int init_weight_struct(Problem_Description *problem, Mesh_Description<int> *mesh,
461 Weight_Description *weight);
462 template int init_weight_struct(Problem_Description *problem, Mesh_Description<int64_t> *mesh,
463 Weight_Description *weight);
464
465 template <typename INT>
466 int init_weight_struct(Problem_Description *problem, Mesh_Description<INT> *mesh,
467 Weight_Description *weight)
468 {
469 if (problem->type == DecompType::NODAL) {
470 weight->nvals = mesh->num_nodes;
471 }
472 else {
473 weight->nvals = mesh->num_elems;
474 }
475
476 /* Allocate memory */
477 weight->vertices.resize(weight->nvals);
478 return 1;
479 } /*-----------------------End init_weight_struct()--------------------------*/