Debian Codegraph @

packages/seacas/libraries/ioss/src/main/io_modify.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_Assembly.h"
9 #include "Ioss_Blob.h"
10 #include "Ioss_DBUsage.h"
11 #include "Ioss_DatabaseIO.h"
12 #include "Ioss_ElementBlock.h"
13 #include "Ioss_ElementTopology.h"
14 #include "Ioss_FileInfo.h"
15 #include "Ioss_Getline.h"
16 #include "Ioss_Glob.h"
17 #include "Ioss_GroupingEntity.h"
18 #include "Ioss_IOFactory.h"
19 #include "Ioss_NodeBlock.h"
20 #include "Ioss_NodeSet.h"
21 #include "Ioss_Property.h"
22 #include "Ioss_Region.h"
23 #include "Ioss_SideBlock.h"
24 #include "Ioss_SideSet.h"
25 #include "Ioss_StructuredBlock.h"
26 #include "Ioss_Utils.h"
27 #include <cassert>
28 #include <cmath>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <exception>
32 #include <fmt/color.h>
33 #include <fmt/core.h>
34 #include <fmt/format.h>
35 #include <fmt/ostream.h>
36 #include <fmt/ranges.h>
37 #include <fstream>
38 #include <iostream>
39 #include <map>
40 #include <regex>
41 #include <stdint.h>
42 #include <string>
43 #include <tokenize.h>
44 #include <unistd.h>
45 #include <vector>
46
47 #include "Ioss_EntityType.h"
48 #include "Ioss_ParallelUtils.h"
49 #include "Ioss_PropertyManager.h"
50 #include "Ioss_ScopeGuard.h"
51 #include "Ioss_State.h"
52 #include "modify_interface.h"
53
54 // `io_modify` is only built if Exodus is enabled...
55 #include "exodus/Ioex_Internals.h"
56 #include "exodus/Ioex_Utils.h"
57 #include <exodusII.h>
58
59 #if defined(SEACAS_HAVE_CGNS)
60 #include "cgns/Iocgns_Utils.h"
61 #endif
62
63 #if defined(__IOSS_WINDOWS__)
64 #include <io.h>
65
66 #define isatty _isatty
67 #endif
68 // ========================================================================
69
70 using real = double;
71
72 namespace {
73 std::string codename;
74 std::string version = "2.07 (2024-04-15)";
75
76 std::vector<Ioss::GroupingEntity *> attributes_modified;
77
78 Ioss::EntityType get_entity_type(const std::string &type)
79 {
80 if (Ioss::Utils::substr_equal(type, "elementblock")) {
81 return Ioss::ELEMENTBLOCK;
82 }
83 else if (Ioss::Utils::substr_equal(type, "block")) {
84 return Ioss::ELEMENTBLOCK;
85 }
86 else if (Ioss::Utils::substr_equal(type, "structuredblock")) {
87 return Ioss::STRUCTUREDBLOCK;
88 }
89 else if (Ioss::Utils::substr_equal(type, "nodeblock")) {
90 return Ioss::NODEBLOCK;
91 }
92 else if (Ioss::Utils::substr_equal(type, "nodeset")) {
93 return Ioss::NODESET;
94 }
95 else if (Ioss::Utils::substr_equal(type, "nset")) {
96 return Ioss::NODESET;
97 }
98 else if (Ioss::Utils::substr_equal(type, "nodelist")) {
99 return Ioss::NODESET;
100 }
101 else if (Ioss::Utils::substr_equal(type, "sideset")) {
102 return Ioss::SIDESET;
103 }
104 else if (Ioss::Utils::substr_equal(type, "sset")) {
105 return Ioss::SIDESET;
106 }
107 else if (Ioss::Utils::substr_equal(type, "surface")) {
108 return Ioss::SIDESET;
109 }
110 else if (Ioss::Utils::substr_equal(type, "assembly")) {
111 return Ioss::ASSEMBLY;
112 }
113 else if (Ioss::Utils::substr_equal(type, "blob")) {
114 return Ioss::BLOB;
115 }
116 return Ioss::INVALID_TYPE;
117 }
118
119 Ioss::NameList get_name_list(const Ioss::Region &region, Ioss::EntityType type);
120 void handle_help(const std::string &topic);
121 bool handle_delete(const Ioss::NameList &tokens, Ioss::Region &region);
122 void handle_list(const Ioss::NameList &tokens, const Ioss::Region &region,
123 bool show_attribute = false);
124 void handle_graph(const Ioss::NameList &tokens, const Ioss::Region &region);
125 bool handle_assembly(const Ioss::NameList &tokens, Ioss::Region &region, bool allow_modify);
126 bool handle_attribute(const Ioss::NameList &tokens, Ioss::Region &region);
127 bool handle_geometry(const Ioss::NameList &tokens, Ioss::Region &region);
128 bool handle_time(const Ioss::NameList &tokens, Ioss::Region &region);
129 bool handle_rename(const Ioss::NameList &tokens, Ioss::Region &region);
130 void update_assembly_info(Ioss::Region &region, const Modify::Interface &interFace);
131
132 void modify_time(Ioss::Region &region, double scale, double offset);
133
134 void offset_filtered_coordinates(Ioss::Region &region, real offset[3],
135 const std::vector<const Ioss::GroupingEntity *> &blocks);
136 void scale_filtered_coordinates(Ioss::Region &region, real scale[3],
137 const std::vector<const Ioss::GroupingEntity *> &blocks);
138 void rotate_filtered_coordinates(Ioss::Region &region, real rotation_matrix[3][3],
139 const std::vector<const Ioss::GroupingEntity *> &blocks);
140 bool update_rotation_matrix(real rotation_matrix[3][3], const std::string &axis, double angle);
141
142 void set_db_properties(const Modify::Interface &interFace, Ioss::DatabaseIO *dbi);
143
144 void info_entity(const Ioss::StructuredBlock *sb, bool show_property = false);
145 void info_entity(const Ioss::NodeBlock *nb, bool show_property = false);
146 void info_entity(const Ioss::ElementBlock *eb, bool show_property = false);
147 void info_entity(const Ioss::NodeSet *ns, bool show_property = false);
148 void info_entity(const Ioss::SideSet *ss, bool show_property = false);
149 void info_entity(const Ioss::Assembly *as, bool show_property = false);
150 void info_entity(const Ioss::Blob *blob, bool show_property = false);
151 void info_entity(const Ioss::Region &region, bool show_property = false);
152 void info_time(const Ioss::Region &region);
153
154 template <typename T>
155 void info_entities(const std::vector<T *> &entities, const Ioss::NameList &tokens,
156 const Ioss::Region &region, const std::string &type,
157 bool show_property = false)
158 {
159 if (entities.empty()) {
160 fmt::print("\n\t*** There are no {} in this model.\n", type);
161 return;
162 }
163 if (tokens.size() == 4 && Ioss::Utils::substr_equal(tokens[2], "matches")) {
164 // 0 1 2 3
165 // LIST {entity_type} MATCHES {regex}
166 auto entity_type = get_entity_type(tokens[1]);
167 if (entity_type == Ioss::INVALID_TYPE) {
168 fmt::print(stderr, fg(fmt::color::yellow), "WARNING: Unrecognized entity type '{}'.\n",
169 tokens[1]);
170 }
171 std::regex reg(tokens[3], std::regex::extended);
172 Ioss::NameList names = get_name_list(region, entity_type);
173
174 // Check for match against all names in list...
175 bool matched = false;
176 for (const auto &name : names) {
177 if (std::regex_match(name, reg)) {
178 const auto *entity = region.get_entity(name, entity_type);
179 const T *ge = dynamic_cast<const T *>(entity);
180 if (ge != nullptr) {
181 info_entity(ge, show_property);
182 matched = true;
183 }
184 }
185 }
186 if (!matched) {
187 fmt::print(stderr, fg(fmt::color::yellow),
188 "WARNING: Regular Expression '{}' did not match any {}\n", tokens[3], type);
189 }
190 }
191 else if (tokens.size() == 4 && Ioss::Utils::substr_equal(tokens[2], "glob")) {
192 // 0 1 2 3
193 // LIST {entity_type} GLOB {glob}
194 auto entity_type = get_entity_type(tokens[1]);
195 if (entity_type == Ioss::INVALID_TYPE) {
196 fmt::print(stderr, fg(fmt::color::yellow), "WARNING: Unrecognized entity type '{}'.\n",
197 tokens[1]);
198 }
199 Ioss::glob::glob glob(tokens[3]);
200 Ioss::NameList names = get_name_list(region, entity_type);
201
202 // Check for match against all names in list...
203 bool matched = false;
204 for (const auto &name : names) {
205 if (Ioss::glob::glob_match(name, glob)) {
206 const auto *entity = region.get_entity(name, entity_type);
207 const T *ge = dynamic_cast<const T *>(entity);
208 if (ge != nullptr) {
209 info_entity(ge, show_property);
210 matched = true;
211 }
212 }
213 }
214 if (!matched) {
215 fmt::print(stderr, fg(fmt::color::yellow),
216 "WARNING: Glob Expression '{}' did not match any {}\n", tokens[3], type);
217 }
218 }
219 else if (tokens.size() == 2 ||
220 (tokens.size() == 3 && Ioss::Utils::substr_equal(tokens[2], "list"))) {
221 for (const T *ge : entities) {
222 info_entity(ge, show_property);
223 }
224 }
225 else {
226 for (size_t i = 2; i < tokens.size(); i++) {
227 const T *ge = dynamic_cast<T *>(region.get_entity(tokens[i]));
228 if (ge != nullptr) {
229 info_entity(ge, show_property);
230 }
231 }
232 }
233 }
234
235 std::string name(const Ioss::GroupingEntity *entity)
236 {
237 return entity->type_string() + " '" + entity->name() + "'";
238 }
239
240 int64_t id(const Ioss::GroupingEntity *entity)
241 {
242 int64_t id = entity->get_optional_property("id", -1);
243 return id;
244 }
245
246 int64_t get_next_assembly_id(const Ioss::Region &region)
247 {
248 // Determines the current maximum assembly id and then
249 // returns ids larger than that value. The ids it
250 // returns are multiples of 100 just because.
251 static int64_t next_id = 0;
252 if (next_id == 0) {
253 const auto &assemblies = region.get_assemblies();
254 for (const auto *assembly : assemblies) {
255 auto my_id = id(assembly);
256 next_id = std::max(next_id, my_id);
257 }
258 next_id = (next_id / 100);
259 }
260 ++next_id;
261 return next_id * 100;
262 }
263
264 Ioss::PropertyManager set_properties(const Modify::Interface & /* interFace */)
265 {
266 Ioss::PropertyManager properties{};
267 return properties;
268 }
269 } // namespace
270
271 int main(int argc, char *argv[])
272 {
273 #ifdef SEACAS_HAVE_MPI
274 MPI_Init(&argc, &argv);
275 ON_BLOCK_EXIT(MPI_Finalize);
276 #endif
277
278 codename = argv[0];
279 size_t ind = codename.find_last_of('/', codename.size());
280 if (ind != std::string::npos) {
281 codename = codename.substr(ind + 1, codename.size());
282 }
283
284 fmt::print(fg(fmt::color::cyan), "\n *** {}, Version {}\n", codename, version);
285 Modify::Interface interFace;
286 interFace.parse_options(argc, argv);
287
288 Ioss::Init::Initializer io;
289
290 std::string inpfile = interFace.filename();
291 std::string input_type = interFace.type();
292
293 //========================================================================
294 // INPUT ...
295 // NOTE: The "READ_RESTART" mode ensures that the node and element ids will be mapped.
296 //========================================================================
297 Ioss::PropertyManager properties = set_properties(interFace);
298 properties.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_MODIFY));
299
300 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(input_type, inpfile, Ioss::WRITE_RESTART,
301 Ioss::ParallelUtils::comm_world(), properties);
302 if (dbi == nullptr || !dbi->ok(true)) {
303 std::exit(EXIT_FAILURE);
304 }
305
306 set_db_properties(interFace, dbi);
307
308 // NOTE: 'region' owns 'db' pointer at this time...
309 Ioss::Region region(dbi, "region");
310 region.begin_mode(Ioss::STATE_DEFINE_MODEL);
311
312 region.output_summary(std::cout, true);
313
314 bool from_term = (isatty(0) != 0 && isatty(1) != 0);
315 bool changed = false;
316 bool allow_modify = interFace.modify_existing_assembly();
317
318 while (true) {
319 std::string input;
320 if (from_term) {
321 fmt::print(fg(fmt::terminal_color::magenta), "\n");
322 const char *cinput = Ioss::getline_int("COMMAND> ");
323 if (cinput && cinput[0] == '\0') {
324 break;
325 }
326 if (cinput) {
327 Ioss::gl_histadd(cinput);
328 }
329 input = cinput;
330 }
331 else {
332 std::getline(std::cin, input);
333 }
334
335 // NOTE: getline_int returns the trailing '\n'
336 auto tokens = Ioss::tokenize(input, " ,\n");
337 if (tokens.empty()) {
338 continue;
339 }
340 if (Ioss::Utils::substr_equal(tokens[0], "exit") ||
341 Ioss::Utils::substr_equal(tokens[0], "end")) {
342 break;
343 }
344 if (Ioss::Utils::substr_equal(tokens[0], "quit")) {
345 changed = false;
346 break;
347 }
348
349 if (Ioss::Utils::str_equal(tokens[0], "help")) {
350 handle_help(tokens.back());
351 }
352 else if (Ioss::Utils::substr_equal(tokens[0], "allow") &&
353 Ioss::Utils::substr_equal(tokens[1], "modifications")) {
354 allow_modify = true;
355 fmt::print(fg(fmt::color::cyan), "\t*** Modifications to existing assemblies now allowed.\n");
356 }
357 else if (Ioss::Utils::substr_equal(tokens[0], "list")) {
358 handle_list(tokens, region);
359 }
360 else if (Ioss::Utils::substr_equal(tokens[0], "graph")) {
361 handle_graph(tokens, region);
362 }
363 else if (Ioss::Utils::substr_equal(tokens[0], "delete")) {
364 handle_delete(tokens, region);
365 }
366 else if (Ioss::Utils::substr_equal(tokens[0], "assembly")) {
367 changed |= handle_assembly(tokens, region, allow_modify);
368 }
369 else if (Ioss::Utils::substr_equal(tokens[0], "attribute")) {
370 changed |= handle_attribute(tokens, region);
371 }
372 else if (Ioss::Utils::substr_equal(tokens[0], "geometry")) {
373 changed |= handle_geometry(tokens, region);
374 }
375 else if (Ioss::Utils::substr_equal(tokens[0], "time")) {
376 changed |= handle_time(tokens, region);
377 }
378 else if (Ioss::Utils::substr_equal(tokens[0], "rename")) {
379 changed |= handle_rename(tokens, region);
380 }
381 else {
382 fmt::print(stderr, fg(fmt::color::yellow), "\tWARNING: Unrecognized command: {}\n",
383 tokens[0]);
384 }
385 }
386
387 if (changed) {
388 update_assembly_info(region, interFace);
389 fmt::print(fg(fmt::color::cyan),
390 "\n\t*** Database assembly structure modified. File update required.\n");
391 }
392 fmt::print("\n{} execution successful.\n", codename);
393 return EXIT_SUCCESS;
394 }
395
396 namespace {
397 void info_entity(const Ioss::StructuredBlock *sb, bool show_property)
398 {
399 fmt::print("\n{} {} [{}, Offset = {}] ", name(sb), fmt::join(sb->get_ijk_global(), "x"),
400 fmt::join(sb->get_ijk_local(), "x"), fmt::join(sb->get_ijk_offset(), ", "));
401
402 int64_t num_cell = sb->get_property("cell_count").get_int();
403 int64_t num_node = sb->get_property("node_count").get_int();
404 fmt::print("{:14} cells, {:14} nodes\n", fmt::group_digits(num_cell),
405 fmt::group_digits(num_node));
406 if (show_property) {
407 Ioss::Utils::info_property(sb, Ioss::Property::ATTRIBUTE, "\tAttributes (Reduction): ", "\t");
408 }
409 }
410
411 void info_entity(const Ioss::Region &region, bool show_property)
412 {
413 fmt::print("\nRegion (global)\n");
414 if (show_property) {
415 Ioss::Utils::info_property(&region, Ioss::Property::ATTRIBUTE,
416 "\tAttributes (Reduction): ", "\t");
417 }
418 }
419
420 void info_entity(const Ioss::Assembly *as, bool show_property)
421 {
422 std::string modifier;
423 if (as->property_exists("created")) {
424 modifier = " [created]";
425 }
426 else if (as->property_exists("modified")) {
427 modifier = " [modified]";
428 }
429
430 fmt::print("\n{} id: {:6d}, contains: {} member(s) of type {:>10s}.{}\n\tMembers: ", name(as),
431 id(as), as->member_count(), as->contains_string(), modifier);
432 for (const auto &mem : as->get_members()) {
433 fmt::print("'{}' ", mem->name());
434 }
435 fmt::print("\n");
436 if (show_property) {
437 Ioss::Utils::info_property(as, Ioss::Property::ATTRIBUTE, "\tAttributes (Reduction): ", "\t");
438 }
439 }
440
441 void info_entity(const Ioss::Blob *blob, bool show_property)
442 {
443 fmt::print("\n{} id: {:6d}, contains: {} item(s).\n", name(blob), id(blob),
444 blob->entity_count());
445 if (show_property) {
446 Ioss::Utils::info_property(blob, Ioss::Property::ATTRIBUTE,
447 "\tAttributes (Reduction): ", "\t");
448 }
449 }
450
451 void info_entity(const Ioss::ElementBlock *eb, bool show_property)
452 {
453 int64_t num_elem = eb->entity_count();
454
455 std::string type = eb->topology()->name();
456 int64_t num_attrib = eb->get_property("attribute_count").get_int();
457 fmt::print("\n{} id: {:6d}, topology: {:>10s}, {:14} elements, {:3d} attributes.\n", name(eb),
458 id(eb), type, fmt::group_digits(num_elem), num_attrib);
459 if (show_property) {
460 Ioss::Utils::info_property(eb, Ioss::Property::ATTRIBUTE, "\tAttributes (Reduction): ", "\t");
461 }
462 }
463
464 void info_entity(const Ioss::SideSet *ss, bool show_property)
465 {
466 fmt::print("\n{} id: {:6d}", name(ss), id(ss));
467 if (ss->property_exists("bc_type")) {
468 #if defined(SEACAS_HAVE_CGNS)
469 auto bc_type = ss->get_property("bc_type").get_int();
470 fmt::print(", boundary condition type: {} ({})\n", BCTypeName[bc_type], bc_type);
471 #else
472 fmt::print(", boundary condition type: {}\n", ss->get_property("bc_type").get_int());
473 #endif
474 }
475 const Ioss::SideBlockContainer &fbs = ss->get_side_blocks();
476 for (auto &fb : fbs) {
477 int64_t count = fb->entity_count();
478 int64_t num_attrib = fb->get_property("attribute_count").get_int();
479 int64_t num_dist = fb->get_property("distribution_factor_count").get_int();
480 fmt::print("\t{}, {:8} sides, {:3d} attributes, {:8} distribution factors.\n", name(fb),
481 fmt::group_digits(count), num_attrib, fmt::group_digits(num_dist));
482 }
483 if (show_property) {
484 Ioss::Utils::info_property(ss, Ioss::Property::ATTRIBUTE, "\tAttributes (Reduction): ", "\t");
485 }
486 }
487
488 void info_entity(const Ioss::NodeSet *ns, bool show_property)
489 {
490 int64_t count = ns->entity_count();
491 int64_t num_attrib = ns->get_property("attribute_count").get_int();
492 int64_t num_dist = ns->get_property("distribution_factor_count").get_int();
493 fmt::print("\n{} id: {:6d}, {:8} nodes, {:3d} attributes, {:8} distribution factors.\n",
494 name(ns), id(ns), fmt::group_digits(count), num_attrib, fmt::group_digits(num_dist));
495 if (show_property) {
496 Ioss::Utils::info_property(ns, Ioss::Property::ATTRIBUTE, "\tAttributes (Reduction): ", "\t");
497 }
498 }
499
500 void info_entity(const Ioss::NodeBlock *nb, bool show_property)
501 {
502 int64_t num_nodes = nb->entity_count();
503 int64_t num_attrib = nb->get_property("attribute_count").get_int();
504 fmt::print("\n{} {:14} nodes, {:3d} attributes.\n", name(nb), fmt::group_digits(num_nodes),
505 num_attrib);
506 if (show_property) {
507 Ioss::Utils::info_property(nb, Ioss::Property::ATTRIBUTE, "\tAttributes (Reduction): ", "\t");
508 }
509 }
510
511 void info_time(const Ioss::Region &region)
512 {
513 fmt::print("\n");
514 size_t ts_count = region.get_optional_property("state_count", 0);
515 auto width = Ioss::Utils::number_width(ts_count);
516
517 for (size_t step = 1; step <= ts_count; step++) {
518 fmt::print("\tStep {:{}}, Time = {}\n", step, width, region.get_state_time(step));
519 }
520 }
521
522 void set_db_properties(const Modify::Interface & /* interFace */, Ioss::DatabaseIO *dbi)
523 {
524 if (dbi == nullptr || !dbi->ok(true)) {
525 std::exit(EXIT_FAILURE);
526 }
527 }
528
529 void handle_help(const std::string &topic)
530 {
531 bool all = Ioss::Utils::substr_equal(topic, "help");
532 if (all) {
533 fmt::print(fmt::emphasis::bold, "\n\tHELP");
534 fmt::print(" [list | assembly | graph | attribute | regex | glob]\n");
535 fmt::print(fmt::emphasis::bold, "\n\tEND | EXIT\n");
536 fmt::print("\t\tEnd command input and output changed assembly definitions (if any).\n");
537 fmt::print(fmt::emphasis::bold, "\n\tQUIT\n");
538 fmt::print("\t\tEnd command input and exit with no rewriting of database. Some changes may "
539 "have already been made.\n");
540
541 fmt::print(fmt::emphasis::bold, "\n\tALLOW MODIFICATIONS\n");
542 fmt::print("\t\tBy default, io_modify will only allow creation of new assemblies.\n"
543 "\t\tIf this command is specified, then can modify assemblies that already exist "
544 "in database.\n"
545 "\t\tThis will cause the database to be rewritten. Without this option, it is "
546 "updated in place.\n");
547 }
548 if (all || Ioss::Utils::substr_equal(topic, "list")) {
549 fmt::print(fmt::emphasis::bold, "\n\tLIST ");
550 fmt::print(
551 "elementblock|block|structuredblock|assembly|nodeset|sideset|blob|times|summary\n");
552 fmt::print(fmt::emphasis::bold, "\tLIST ");
553 fmt::print("elementblock|block|structuredblock|assembly|nodeset|sideset|blob "
554 "{{names...}}\n");
555 fmt::print(fmt::emphasis::bold, "\tLIST ");
556 fmt::print("elementblock|block|structuredblock|assembly|nodeset|sideset|blob "
557 "MATCHES {{regex}}\n");
558 fmt::print(fmt::emphasis::bold, "\tLIST ");
559 fmt::print("elementblock|block|structuredblock|assembly|nodeset|sideset|blob "
560 "GLOB {{glob}}\n");
561 }
562 if (all || Ioss::Utils::substr_equal(topic, "assembly")) {
563 fmt::print("\n\tFor all commands, if an assembly named `name` does not exist, it will be "
564 "created.\n");
565 fmt::print(fmt::emphasis::bold, "\tASSEMBLY ");
566 fmt::print("{{name}}\n");
567 fmt::print("\t\tCreates an empty assembly named `name` if it does not exist.\n");
568
569 fmt::print(fmt::emphasis::bold, "\n\tASSEMBLY ");
570 fmt::print("{{name}} ADD {{name1}} {{name2}} ... {{nameL}}\n");
571 fmt::print("\t\tAdds the specified entities to the assembly. All entities must be the same "
572 "type.\n");
573
574 fmt::print(fmt::emphasis::bold, "\n\tASSEMBLY ");
575 fmt::print("{{name}} REMOVE {{name1}} {{name2}} ... {{nameL}}\n");
576 fmt::print("\t\tRemoves the specified entities from the assembly.\n");
577
578 fmt::print(fmt::emphasis::bold, "\n\tASSEMBLY ");
579 fmt::print("{{name}} TYPE {{type}} MATCHES {{regex}}\n");
580 fmt::print("\t\tAdds the entities of the specified type to the assembly.\n"
581 "\t\tAll entities whose name matches the {{regex}} will be added.\n");
582 fmt::print(fmt::emphasis::bold, "\n\tASSEMBLY ");
583 fmt::print("{{name}} TYPE {{type}} GLOB {{glob}}\n");
584 fmt::print("\t\tAdds the entities of the specified type to the assembly.\n"
585 "\t\tAll entities whose name matches the {{glob}} will be added.\n");
586
587 fmt::print(fmt::emphasis::bold, "\n\tASSEMBLY ");
588 fmt::print("{{name}} TYPE {{type}} NAMED {{list of one or more names}}\n");
589 fmt::print("\t\tAdds the entities of the specified type to the assembly.\n"
590 "\t\tAll entities whose names are listed will be added.\n");
591
592 fmt::print(fmt::emphasis::bold, "\n\tASSEMBLY ");
593 fmt::print("{{name}} TYPE {{type}} RANGE {{id}} TO {{id}} BY {{step}}\n");
594 fmt::print("\t\tAdds the entities of the specified type to the assembly.\n"
595 "\t\tAll entities whose id matches the specified range will be added.\n"
596 "\t\tNo message will be output for ids not matching an entity.\n");
597
598 fmt::print(fmt::emphasis::bold, "\n\tASSEMBLY ");
599 fmt::print("{{name}} TYPE {{type}} IDS {{id}}, {{id2}}, ..., {{idL}}\n");
600 fmt::print(
601 "\t\tAdds the entities of the specified type to the assembly.\n"
602 "\t\tAll entities whose id matches an id in the list will be added.\n"
603 "\t\tA warning message will be output if there is no entity with the requested id.\n");
604
605 fmt::print(fmt::emphasis::bold, "\n\tDELETE ");
606 fmt::print("{{name}}\n");
607 fmt::print("\t\tRemove the assembly with the specified name.\n"
608 "\t\tCurrently only supported for assemblies created during this execution; not "
609 "for assemblies\n"
610 "\t\texisting on the input database.\n");
611 }
612 if (all || Ioss::Utils::substr_equal(topic, "graph")) {
613 fmt::print(fmt::emphasis::bold, "\n\tGRAPH OUTPUT ");
614 fmt::print("[filename]\n");
615 fmt::print(
616 "\t\tCreate a 'dot' input file with the structure of the assembly graph.\n"
617 "\t\tFile is named 'filename' or defaults to 'assembly.dot' if filename not given.\n");
618 fmt::print(fmt::emphasis::bold, "\tGRAPH CHECK\n");
619 fmt::print("\t\tCheck validity of assembly graph--are there any cycles.\n");
620 }
621 if (all || Ioss::Utils::substr_equal(topic, "attribute")) {
622 fmt::print(fmt::emphasis::bold, "\n\tATTRIBUTE ");
623 fmt::print("{{ent_name}} ADD {{att_name}} STRING {{values...}}\n");
624 fmt::print(fmt::emphasis::bold, "\tATTRIBUTE ");
625 fmt::print("{{ent_name}} ADD {{att_name}} DOUBLE {{values...}}\n");
626 fmt::print(fmt::emphasis::bold, "\tATTRIBUTE ");
627 fmt::print("{{ent_name}} NAME {{att_name}} INTEGER {{values...}}\n");
628 fmt::print("\t\tAdd an attribute to the specified entity 'ent_name'.\n"
629 "\t\tThe attribute will be named 'att_name' with value(s) 'values...'\n"
630 "\t\tCan also modify the values of an existing attribute.'\n");
631 fmt::print(fmt::emphasis::bold, "\tATTRIBUTE LIST ");
632 fmt::print("{{ent_name...}}\n"
633 "\t\tList attributes for the selected entities\n");
634 fmt::print(fmt::emphasis::bold, "\tATTRIBUTE ");
635 fmt::print("{{ent_type}} LIST\n"
636 "\t\tList attributes for all entities in the specified entity type\n");
637 fmt::print(fmt::emphasis::bold, "\tATTRIBUTE ");
638 fmt::print("{{ent_type}} MATCH {{regex}}\n"
639 "\t\tList attributes for all entities in the specified entity type whose name "
640 "matches the regex.\n");
641 fmt::print(fmt::emphasis::bold, "\tATTRIBUTE ");
642 fmt::print("{{ent_type}} GLOB {{glob}}\n"
643 "\t\tList attributes for all entities in the specified entity type whose name "
644 "matches the glob.\n");
645 }
646 if (all || Ioss::Utils::substr_equal(topic, "rename")) {
647 fmt::print(fmt::emphasis::bold, "\n\tRENAME ");
648 fmt::print("{{name}} TO {{new_name}}\n");
649 fmt::print(fmt::emphasis::bold, "\tRENAME ");
650 fmt::print("{{type}} {{id}} TO {{new_name}}\n");
651 }
652 if (all || Ioss::Utils::substr_equal(topic, "geometry")) {
653 fmt::print(fmt::emphasis::bold, "\n\tGEOMETRY ROTATE ");
654 fmt::print("{{X|Y|Z}} {{angle}} ...\n");
655 fmt::print(fmt::emphasis::bold, "\tGEOMETRY MIRROR ");
656 fmt::print("{{X|Y|Z}} ...\n");
657 fmt::print(fmt::emphasis::bold, "\tGEOMETRY SCALE ");
658 fmt::print("{{X|Y|Z}} {{scale_factor}} ...\n");
659 fmt::print(fmt::emphasis::bold, "\tGEOMETRY OFFSET ");
660 fmt::print("{{X|Y|Z}} {{offset}} ...\n");
661 fmt::print(fmt::emphasis::bold, "\tGEOMETRY ROTATE ");
662 fmt::print("{{ELEMENTBLOCKS|BLOCKS|ASSEMBLY}} {{names}} {{X|Y|Z}} {{angle}}\n");
663 fmt::print(fmt::emphasis::bold, "\tGEOMETRY MIRROR ");
664 fmt::print("{{ELEMENTBLOCKS|BLOCKS|ASSEMBLY}} {{names}} {{X|Y|Z}} ...\n");
665 fmt::print(fmt::emphasis::bold, "\tGEOMETRY SCALE ");
666 fmt::print("{{ELEMENTBLOCKS|BLOCKS|ASSEMBLY}} {{names}} {{X|Y|Z}} {{scale_factor}} ... "
667 "\n");
668 fmt::print(fmt::emphasis::bold, "\tGEOMETRY OFFSET ");
669 fmt::print("{{ELEMENTBLOCKS|BLOCKS|ASSEMBLY}} {{names}} {{X|Y|Z}} {{offset}} ...\n");
670 }
671 if (all || Ioss::Utils::substr_equal(topic, "time")) {
672 fmt::print(fmt::emphasis::bold, "\n\tTIME SCALE ");
673 fmt::print("{{scale}} (T_out = T_in * {{scale}})\n");
674 fmt::print(fmt::emphasis::bold, "\tTIME OFFSET ");
675 fmt::print("{{offset}} (T_out = T_in + {{offset}})\n");
676 }
677 if (all || Ioss::Utils::substr_equal(topic, "regex")) {
678 fmt::print("\n\tRegular Expression help (used in ASSEMBLY MATCHES and LIST MATCHES and "
679 "ATTRIBUTE LIST MATCHES options)\n"
680 "\t\tSupports \"POSIX Extended Regular Expressions\" syntax\n"
681 "\t\tSee https://www.regular-expressions.info/posix.html\n"
682 "\t\tQuickStart: https://www.regular-expressions.info/quickstart.html\n");
683 }
684 if (all || Ioss::Utils::substr_equal(topic, "glob")) {
685 fmt::print(
686 "\n\tGlob help (used in ASSEMBLY GLOB and LIST GLOB and ATTRIBUTE LIST GLOB options)\n"
687 "\t\t?(pattern-list) Matches zero or one occurrence of the given patterns\n"
688 "\t\t*(pattern-list) Matches zero or more occurrences of the given patterns\n"
689 "\t\t+(pattern-list) Matches one or more occurrences of the given patterns\n"
690 "\t\t@(pattern-list) Matches one of the given patterns\n"
691 "\t\t!(pattern-list) Matches anything except one of the given patterns\n"
692 "\tGlob Examples\n"
693 "\t\tblock* : All names that start with 'block'\n"
694 "\t\t[A-Z]* : All names that start with a capital letter\n");
695 }
696 }
697
698 void handle_list(const Ioss::NameList &tokens, const Ioss::Region &region, bool show_attribute)
699 {
700 if (tokens.size() > 1) {
701 if (Ioss::Utils::substr_equal(tokens[1], "summary")) {
702 region.output_summary(std::cout);
703 }
704 else if (Ioss::Utils::substr_equal(tokens[1], "region")) {
705 info_entity(region, show_attribute);
706 }
707 else if (Ioss::Utils::substr_equal(tokens[1], "elementblock")) {
708 const auto &entities = region.get_element_blocks();
709 info_entities(entities, tokens, region, "Element Blocks", show_attribute);
710 }
711 else if (Ioss::Utils::substr_equal(tokens[1], "assembly") ||
712 Ioss::Utils::substr_equal(tokens[1], "assemblies")) {
713 const auto &entities = region.get_assemblies();
714 info_entities(entities, tokens, region, "Assemblies", show_attribute);
715 }
716 else if (Ioss::Utils::substr_equal(tokens[1], "nodeset") ||
717 Ioss::Utils::substr_equal(tokens[1], "nset")) {
718 const auto &entities = region.get_nodesets();
719 info_entities(entities, tokens, region, "NodeSets", show_attribute);
720 }
721 else if (Ioss::Utils::substr_equal(tokens[1], "nodeblock")) {
722 const auto &entities = region.get_node_blocks();
723 info_entities(entities, tokens, region, "Node Block", show_attribute);
724 }
725 else if (Ioss::Utils::substr_equal(tokens[1], "structuredblock")) {
726 const auto &entities = region.get_structured_blocks();
727 info_entities(entities, tokens, region, "Structured Blocks", show_attribute);
728 }
729 else if (Ioss::Utils::substr_equal(tokens[1], "blocks")) {
730 const auto type = region.mesh_type();
731 if (type == Ioss::MeshType::UNSTRUCTURED) {
732 const auto &entities = region.get_element_blocks();
733 info_entities(entities, tokens, region, "Element Blocks", show_attribute);
734 }
735 else if (type == Ioss::MeshType::STRUCTURED) {
736 const auto &entities = region.get_structured_blocks();
737 info_entities(entities, tokens, region, "Structured Blocks", show_attribute);
738 }
739 }
740 else if (Ioss::Utils::substr_equal(tokens[1], "sideset") ||
741 Ioss::Utils::substr_equal(tokens[1], "sset")) {
742 const auto &entities = region.get_sidesets();
743 info_entities(entities, tokens, region, "SideSets", show_attribute);
744 }
745 else if (Ioss::Utils::substr_equal(tokens[1], "blobs")) {
746 const auto &entities = region.get_blobs();
747 info_entities(entities, tokens, region, "Blobs", show_attribute);
748 }
749 else if (Ioss::Utils::substr_equal(tokens[1], "times")) {
750 info_time(region);
751 }
752 else {
753 fmt::print(stderr, fg(fmt::color::yellow), "\tWARNING: Unrecognized list option '{}'\n",
754 tokens[1]);
755 handle_help("list");
756 }
757 }
758 else {
759 handle_help("list");
760 }
761 }
762
763 class Graph
764 {
765 std::map<std::string, int> m_vertices;
766 Ioss::NameList m_vertex;
767 std::vector<Ioss::IntVector> m_adj; // Pointer to an array containing adjacency std::lists
768
769 bool is_cyclic_internal(int v, std::vector<bool> &visited, std::vector<bool> &recStack);
770 int vertex(const std::string &node);
771 int size() const { return (int)m_vertices.size(); }
772
773 public:
774 void add_edge(const std::string &v, const std::string &w); // to add an edge to graph
775 bool is_cyclic(); // returns true if there is a cycle in this graph
776 };
777
778 int Graph::vertex(const std::string &node)
779 {
780 if (m_vertices.find(node) == m_vertices.end()) {
781 int index = size();
782 m_vertices[node] = index;
783 m_vertex.push_back(node);
784 // fmt::print("Node {} is index {}\n", node, index);
785 }
786 assert(node == m_vertex[m_vertices[node]]);
787 return m_vertices[node];
788 }
789
790 void Graph::add_edge(const std::string &v, const std::string &w)
791 {
792 int vv = vertex(v);
793 int ww = vertex(w);
794 if ((int)m_adj.size() <= vv) {
795 m_adj.resize(vv + 1);
796 }
797 assert((int)m_adj.size() > vv);
798 m_adj[vv].push_back(ww); // Add w to v’s std::list.
799 }
800
801 // This function is a variation of DFSUtil() in https://www.geeksforgeeks.org/archives/18212
802 bool Graph::is_cyclic_internal(int v, std::vector<bool> &visited, std::vector<bool> &recStack)
803 {
804 if (visited[v] == false) {
805 // Mark the current node as visited and part of recursion stack
806 visited[v] = true;
807 recStack[v] = true;
808
809 // Recur for all the m_vertices adjacent to this vertex
810 if (v < (int)m_adj.size()) {
811 for (auto i = m_adj[v].begin(); i != m_adj[v].end(); ++i) {
812 if (!visited[*i] && is_cyclic_internal(*i, visited, recStack)) {
813 if (*i != 0 && v != 0) {
814 fmt::print(fg(fmt::color::yellow), "\t*** Cycle contains {} -> {}\n", m_vertex[v],
815 m_vertex[*i]);
816 }
817 return true;
818 }
819 else if (recStack[*i]) {
820 if (*i != 0 && v != 0) {
821 fmt::print(fg(fmt::color::yellow), "\t*** Cycle contains {} -> {}\n", m_vertex[v],
822 m_vertex[*i]);
823 }
824 return true;
825 }
826 }
827 }
828 }
829 recStack[v] = false; // remove the vertex from recursion stack
830 return false;
831 }
832
833 // Returns true if the graph contains a cycle, else false.
834 // This function is a variation of DFS() in https://www.geeksforgeeks.org/archives/18212
835 bool Graph::is_cyclic()
836 {
837 // Mark all the m_vertices as not visited and not part of recursion
838 // stack
839 std::vector<bool> visited(size());
840 std::vector<bool> recStack(size());
841 for (int i = 0; i < size(); i++) {
842 visited[i] = false;
843 recStack[i] = false;
844 }
845
846 // Call the recursive helper function to detect cycle in different
847 // DFS trees
848 for (int i = 0; i < size(); i++)
849 if (is_cyclic_internal(i, visited, recStack))
850 return true;
851
852 return false;
853 }
854
855 void handle_graph(const Ioss::NameList &tokens, const Ioss::Region &region)
856 {
857 if (tokens.size() == 1) {
858 handle_help("graph");
859 return;
860 }
861
862 if (Ioss::Utils::substr_equal(tokens[1], "output")) {
863 std::string filename = "assembly.dot";
864 if (tokens.size() > 2) {
865 filename = tokens[2];
866 }
867 // Create a file containing a 'dot' representation of the assembly graph.
868 std::ofstream dot(filename);
869 fmt::print(dot, "digraph assemblies {{\n"
870 "node [shape=rectangle]; ");
871 const auto &assemblies = region.get_assemblies();
872 for (const auto *assembly : assemblies) {
873 fmt::print(dot, "{} ", assembly->name());
874 }
875 fmt::print(dot, ";\nnode [shape=oval];\n");
876
877 for (const auto *assembly : assemblies) {
878 const auto &members = assembly->get_members();
879 for (const auto *member : members) {
880 fmt::print(dot, "{} -> {}\n", assembly->name(), member->name());
881 }
882 }
883 fmt::print(dot, "}}\n");
884 fmt::print(fg(fmt::color::cyan), "\t*** Assembly graph output to '{}'.\n", filename);
885 }
886 else if (Ioss::Utils::substr_equal(tokens[1], "check")) {
887 // Check graph for cycles.
888 Graph assem;
889 const auto &assemblies = region.get_assemblies();
890 for (const auto *assembly : assemblies) {
891 assem.add_edge(region.name(), assembly->name());
892 }
893 for (const auto *assembly : assemblies) {
894 const auto &members = assembly->get_members();
895 for (const auto *member : members) {
896 assem.add_edge(assembly->name(), member->name());
897 }
898 }
899 if (assem.is_cyclic()) {
900 fmt::print(stderr, fg(fmt::color::yellow),
901 "\tWARNING: Assembly graph is invalid--contains at least one cycle.\n",
902 tokens[1]);
903 }
904 else {
905 fmt::print(stderr, fg(fmt::color::cyan), "\t*** Assembly graph is valid--no cycles.\n",
906 tokens[1]);
907 }
908 }
909 else {
910 fmt::print(stderr, fg(fmt::color::yellow), "\tWARNING: Unrecognized graph option '{}'\n",
911 tokens[1]);
912 handle_help("graph");
913 }
914 }
915
916 bool handle_delete(const Ioss::NameList &tokens, Ioss::Region &region)
917 {
918 // Returns true if requested assembly was deleted.
919 // False if assembly does not exist, or was not deletable (not created during this run)
920 // NOTE: If the assembly is a member of other assemblies, it will be removed from them
921 // Since currently, the removed assembly must have been created during this run,
922 // then it also must have been added to the other assemblies also during this run,
923 // so the modified flag has already been set on them and we shouldn't have to
924 // do anything else here to track whether in modify mode or to mark assemblies
925 // as modified. Once allow the deletion of existing assemblies, then will have
926 // to do better tracking of what other assemblies were modified as a result of
927 // removing/deleting this assembly.
928 if (tokens.size() > 1) {
929 Ioss::Assembly *assem = region.get_assembly(tokens[1]);
930 if (assem == nullptr) {
931 fmt::print(stderr, fg(fmt::color::yellow),
932 "WARNING: Requested Assembly '{}' does not exist.\n", tokens[1]);
933 return false;
934 }
935 else {
936 if (assem->property_exists("created")) {
937 if (region.remove(assem)) {
938 fmt::print(fg(fmt::color::cyan), "\t***Assembly '{}' deleted successfully.\n",
939 tokens[1]);
940 return true;
941 }
942 }
943 else {
944 fmt::print(stderr, fg(fmt::color::red),
945 "ERROR: Requested Assembly '{}' was not created during this execution. Not "
946 "deletable.\n",
947 tokens[1]);
948 return false;
949 }
950 }
951 }
952 else {
953 handle_help("delete");
954 }
955 return false;
956 }
957
958 bool handle_attribute(const Ioss::NameList &tokens, Ioss::Region &region)
959 {
960 // 0 1 2 3 4 5...
961 // ATTRIBUTE {{ent_name}} ADD {{att_name}} STRING {{values...}}
962 // ATTRIBUTE {{ent_name}} ADD {{att_name}} DOUBLE {{values...}}
963 // ATTRIBUTE {{ent_name}} ADD {{att_name}} INTEGER {{values...}}
964 // ATTRIBUTE LIST {{ent_name}} ...
965 // ATTRIBUTE {{ent_type}} LIST
966 // ATTRIBUTE {{ent_type}} MATCH {regex}
967
968 // Get requested entity...
969 if (Ioss::Utils::substr_equal(tokens[2], "add")) {
970 // Must be at least 6 tokens...
971 if (tokens.size() < 6) {
972 fmt::print(stderr, fg(fmt::color::red),
973 "ERROR: ATTRIBUTE Command does not have enough tokens to be valid.\n");
974 fmt::print(stderr, "\t\t{}\n", fmt::join(tokens, " "));
975 handle_help("attribute");
976 return false;
977 }
978
979 Ioss::GroupingEntity *ge = nullptr;
980 if (Ioss::Utils::substr_equal(tokens[1], "region")) {
981 ge = &region;
982 }
983 else {
984 ge = region.get_entity(tokens[1]);
985 }
986 if (ge == nullptr) {
987 fmt::print(stderr, fg(fmt::color::red), "ERROR: Entity '{}' not found.\n", tokens[1]);
988 return false;
989 }
990
991 // Now get name of attribute/property to create...
992 const std::string &att_name = tokens[3];
993
994 // Now, the attribute type and whether vector or scalar...
995 size_t value_count = tokens.size() - 5;
996 if (Ioss::Utils::substr_equal(tokens[4], "string")) {
997 std::string value = tokens[5];
998 for (size_t i = 6; i < tokens.size(); i++) {
999 value += " " + tokens[i];
1000 }
1001 ge->property_add(Ioss::Property(att_name, value, Ioss::Property::ATTRIBUTE));
1002 }
1003 else if (Ioss::Utils::substr_equal(tokens[4], "double")) {
1004 std::vector<double> values(value_count);
1005 for (size_t i = 0; i < value_count; i++) {
1006 try {
1007 double val = std::stod(tokens[i + 5]);
1008 values[i] = val;
1009 }
1010 catch (const std::exception &x) {
1011 fmt::print(stderr, fg(fmt::color::red), "ERROR: Invalid double value entered: {}. {}\n",
1012 tokens[i + 5], x.what());
1013 return false;
1014 }
1015 }
1016
1017 ge->property_add(Ioss::Property(att_name, values, Ioss::Property::ATTRIBUTE));
1018 }
1019 else if (Ioss::Utils::substr_equal(tokens[4], "integer")) {
1020 Ioss::IntVector values(value_count);
1021 for (size_t i = 0; i < value_count; i++) {
1022 try {
1023 int val = std::stoi(tokens[i + 5]);
1024 values[i] = val;
1025 }
1026 catch (const std::exception &x) {
1027 fmt::print(stderr, fg(fmt::color::red),
1028 "ERROR: Invalid integer value entered: {}. {}\n", tokens[i + 5], x.what());
1029 return false;
1030 }
1031 }
1032 ge->property_add(Ioss::Property(att_name, values, Ioss::Property::ATTRIBUTE));
1033 }
1034 else {
1035 fmt::print(stderr, fg(fmt::color::red),
1036 "ERROR: Unrecognized attribute type '{}' for attribute {}.\n", tokens[4],
1037 att_name);
1038 handle_help("attribute");
1039 return false;
1040 }
1041 attributes_modified.push_back(ge);
1042 return true;
1043 }
1044 else if (Ioss::Utils::substr_equal(tokens[1], "list")) {
1045 // ATTRIBUTE LIST {{ent_name}} ...
1046 for (size_t i = 2; i < tokens.size(); i++) {
1047 Ioss::GroupingEntity *ge = nullptr;
1048 if (Ioss::Utils::substr_equal(tokens[i], "region")) {
1049 ge = &region;
1050 }
1051 else {
1052 ge = region.get_entity(tokens[i]);
1053 }
1054 if (ge != nullptr) {
1055 std::string prefix =
1056 fmt::format("\n{} id: {:6d}\n\tAttributes (Reduction): ", name(ge), id(ge));
1057 Ioss::Utils::info_property(ge, Ioss::Property::ATTRIBUTE, prefix, "\t");
1058 }
1059 }
1060 return false;
1061 }
1062 else if (Ioss::Utils::substr_equal(tokens[2], "list")) {
1063 // ATTRIBUTE {{ent_type}} LIST
1064 handle_list(tokens, region, true);
1065 return false;
1066 }
1067 else if (Ioss::Utils::substr_equal(tokens[2], "matches")) {
1068 // ATTRIBUTE {{ent_type}} MATCH {regex}
1069 handle_list(tokens, region, true);
1070 return false;
1071 }
1072 else if (Ioss::Utils::substr_equal(tokens[2], "glob")) {
1073 // ATTRIBUTE {{ent_type}} MATCH {regex}
1074 handle_list(tokens, region, true);
1075 return false;
1076 }
1077 fmt::print(stderr, fg(fmt::color::red), "ERROR: Unrecognized attribute command.\n");
1078 handle_help("attribute");
1079 return false;
1080 }
1081
1082 bool handle_rename(const Ioss::NameList &tokens, Ioss::Region &region)
1083 {
1084 // 0 1 2 3 4
1085 // RENAME {{ent_name}} TO {{new_name}}
1086 // RENAME {{ent_type}} {{id}} TO {{new_name}}
1087
1088 // Must be at least 4 tokens...
1089 if (tokens.size() < 4) {
1090 fmt::print(stderr, fg(fmt::color::red),
1091 "ERROR: RENAME Command does not have enough tokens to be valid.\n");
1092 fmt::print(stderr, "\t\t{}\n", fmt::join(tokens, " "));
1093 handle_help("rename");
1094 return false;
1095 }
1096
1097 // See if asking for actual entity by name or by type + id
1098 Ioss::GroupingEntity *ge = nullptr;
1099 const std::string &new_name = tokens[tokens.size() - 1];
1100
1101 if (tokens.size() == 5 && Ioss::Utils::str_equal(tokens[3], "to")) {
1102 // Type + ID
1103 auto entity_type = get_entity_type(tokens[1]);
1104 if (entity_type == Ioss::INVALID_TYPE) {
1105 fmt::print(stderr, fg(fmt::color::yellow), "WARNING: Unrecognized entity type '{}'.\n",
1106 tokens[1]);
1107 return false;
1108 }
1109
1110 int entity_id = std::stoi(tokens[2]);
1111 ge = region.get_entity(entity_id, entity_type);
1112 if (ge == nullptr) {
1113 fmt::print(stderr, fg(fmt::color::red), "ERROR: Entity type '{}' with id {} not found.\n",
1114 tokens[1], tokens[2]);
1115 return false;
1116 }
1117 }
1118 else if (tokens.size() == 4 && Ioss::Utils::str_equal(tokens[2], "to")) {
1119 ge = region.get_entity(tokens[1]);
1120 if (ge == nullptr) {
1121 fmt::print(stderr, fg(fmt::color::red), "ERROR: Entity '{}' not found.\n", tokens[1]);
1122 return false;
1123 }
1124 }
1125 else {
1126 fmt::print(stderr,
1127 #if !defined __NVCC__
1128 fg(fmt::color::yellow),
1129 #endif
1130 "\tWARNING: Unrecognized rename syntax '{}'\n", fmt::join(tokens, " "));
1131 handle_help("rename");
1132 }
1133
1134 if (ge != nullptr) {
1135 int exoid = region.get_database()->get_file_pointer();
1136 auto ioss_type = ge->type();
1137 ex_entity_type exo_type = Ioex::map_exodus_type(ioss_type);
1138 auto ierr = ex_put_name(exoid, exo_type, id(ge), new_name.c_str());
1139 if (ierr != EX_NOERR) {
1140 Ioex::exodus_error(exoid, __LINE__, __func__, __FILE__);
1141 }
1142 ge->set_name(new_name);
1143 }
1144 return false;
1145 }
1146
1147 void build_block_list(Ioss::Region &region, const Ioss::GroupingEntity *ge,
1148 std::vector<const Ioss::GroupingEntity *> &blocks)
1149 {
1150 if (ge) {
1151 if (ge->type() == Ioss::ELEMENTBLOCK) {
1152 auto *eb = dynamic_cast<const Ioss::ElementBlock *>(ge);
1153 if (eb != nullptr) {
1154 blocks.push_back(eb);
1155 }
1156 return;
1157 }
1158 else if (ge->type() == Ioss::STRUCTUREDBLOCK) {
1159 auto *sb = dynamic_cast<const Ioss::StructuredBlock *>(ge);
1160 if (sb != nullptr) {
1161 blocks.push_back(sb);
1162 }
1163 return;
1164 }
1165 else if (ge->type() == Ioss::ASSEMBLY) {
1166 auto *as = dynamic_cast<const Ioss::Assembly *>(ge);
1167 const auto &members = as->get_members();
1168 for (const auto *member : members) {
1169 build_block_list(region, member, blocks);
1170 }
1171 return;
1172 }
1173 }
1174 }
1175
1176 std::vector<int> get_filtered_node_list(Ioss::Region &region,
1177 const std::vector<const Ioss::GroupingEntity *> &blocks)
1178 {
1179 const auto type = region.get_database()->get_format();
1180 if (type != "Exodus") {
1181 return std::vector<int>();
1182 }
1183 auto node_count = region.get_property("node_count").get_int();
1184 if (blocks.empty() ||
1185 blocks.size() == (size_t)region.get_property("element_block_count").get_int()) {
1186 return Ioss::IntVector(node_count, 1);
1187 }
1188 else {
1189 Ioss::IntVector node_filter(node_count);
1190 // Iterate all element blocks in 'blocks', get connectivity_raw
1191 // and set `node_filter` to 1 for nodes in connectivity list.
1192 if (region.get_database()->int_byte_size_api() == 4) {
1193 Ioss::IntVector connect;
1194 for (const auto *block : blocks) {
1195 block->get_field_data("connectivity_raw", connect);
1196 for (auto node : connect) {
1197 node_filter[node - 1] = 1;
1198 }
1199 }
1200 }
1201 else {
1202 std::vector<int64_t> connect;
1203 for (const auto *block : blocks) {
1204 block->get_field_data("connectivity_raw", connect);
1205 for (auto node : connect) {
1206 node_filter[node - 1] = 1;
1207 }
1208 }
1209 }
1210 return node_filter;
1211 }
1212 }
1213
1214 bool handle_time(const Ioss::NameList &tokens, Ioss::Region &region)
1215 {
1216 // 0 1 2
1217 // TIME SCALE {{scale}}
1218 // TIME OFFSET {{offset}
1219 if (tokens.size() < 3) {
1220 fmt::print(stderr, fg(fmt::color::red),
1221 "ERROR: TIME Command does not have enough tokens to be valid.\n");
1222 fmt::print(stderr, "\t\t{}\n", fmt::join(tokens, " "));
1223 handle_help("time");
1224 return false;
1225 }
1226 if (Ioss::Utils::substr_equal(tokens[1], "scale")) {
1227 double scale = std::stod(tokens[2]);
1228 modify_time(region, scale, 0.0);
1229 fmt::print(fg(fmt::color::cyan), "\t*** Database time scaled: T_out = T_in * {}.\n", scale);
1230 return false;
1231 }
1232 if (Ioss::Utils::substr_equal(tokens[1], "offset")) {
1233 double offset = std::stod(tokens[2]);
1234 modify_time(region, 1.0, offset);
1235 fmt::print(fg(fmt::color::cyan), "\t*** Database time offset: T_out = T_in + {}.\n", offset);
1236 return false;
1237 }
1238 fmt::print(stderr, fg(fmt::color::red), "ERROR: Unrecognized time command.\n");
1239 handle_help("time");
1240 return false;
1241 }
1242
1243 bool handle_geometry(const Ioss::NameList &tokens, Ioss::Region &region)
1244 {
1245 // 0 1 2 3 4 5...
1246 // GEOMETRY ROTATE {{X|Y|Z}} {{angle}} ...
1247 // GEOMETRY MIRROR {{X|Y|Z}} ...
1248 // GEOMETRY SCALE {{X|Y|Z}} {{scale}} ...
1249 // GEOMETRY OFFSET {{X|Y|Z}} {{offset}} ...
1250 // GEOMETRY ROTATE {{ELEMENTBLOCKS|BLOCKS|ASSEMBLY}} {{names}} {{X|Y|Z}} {{angle}} ...
1251 // GEOMETRY SCALE {{ELEMENTBLOCKS|BLOCKS|ASSEMBLY}} {{names}} {{X|Y|Z}} {{scale}} ...
1252 // GEOMETRY OFFSET {{ELEMENTBLOCKS|BLOCKS|ASSEMBLY}} {{names}} {{X|Y|Z}} {{offset}} ...
1253
1254 if (tokens.size() < 3) {
1255 fmt::print(stderr, fg(fmt::color::red),
1256 "ERROR: GEOMETRY Command does not have enough tokens to be valid.\n");
1257 fmt::print(stderr, "\t\t{}\n", fmt::join(tokens, " "));
1258 handle_help("geometry");
1259 return false;
1260 }
1261
1262 const auto type = region.mesh_type();
1263
1264 // See if applying just selected (and connected) blocks.
1265 size_t idx = 2;
1266 std::vector<const Ioss::GroupingEntity *> blocks;
1267 if (Ioss::Utils::substr_equal(tokens[idx], "elementblocks") ||
1268 Ioss::Utils::substr_equal(tokens[idx], "structuredblocks") ||
1269 Ioss::Utils::substr_equal(tokens[idx], "blocks") ||
1270 Ioss::Utils::substr_equal(tokens[idx], "assembly")) {
1271 // Parse list of block|assembly names...
1272 idx++;
1273 while (!(Ioss::Utils::str_equal(tokens[idx], "x") ||
1274 Ioss::Utils::str_equal(tokens[idx], "y") ||
1275 Ioss::Utils::str_equal(tokens[idx], "z"))) {
1276 const auto &name = tokens[idx++];
1277 auto *ge =
1278 region.get_entity(name, type == Ioss::MeshType::UNSTRUCTURED ? Ioss::ELEMENTBLOCK
1279 : Ioss::STRUCTUREDBLOCK);
1280 if (ge == nullptr) {
1281 ge = region.get_entity(name, Ioss::ASSEMBLY);
1282 }
1283 build_block_list(region, ge, blocks);
1284 }
1285
1286 // Now filter out any duplicates that might be in the list...
1287 Ioss::Utils::uniquify(blocks);
1288 }
1289
1290 // If blocks is non-empty, then we are applying geometry modification to a subset of the model.
1291 // In that case, we need to get all blocks that are connected to the user-specified blocks...
1292 if (!blocks.empty()) {
1293 if (type == Ioss::MeshType::UNSTRUCTURED) {
1294 auto tmp(blocks);
1295 for (const auto *block : blocks) {
1296 auto *eb = dynamic_cast<const Ioss::ElementBlock *>(block);
1297 if (eb != nullptr) {
1298 const auto &connected = eb->get_block_adjacencies();
1299 for (const auto &connect : connected) {
1300 auto *elb = region.get_element_block(connect);
1301 tmp.push_back(elb);
1302 }
1303 }
1304 }
1305 Ioss::Utils::uniquify(tmp);
1306 blocks = tmp;
1307 }
1308 }
1309
1310 if (blocks.empty()) {
1311 fmt::print(fg(fmt::color::cyan), "\n\t*** {} transformation will be applied to ALL blocks.\n",
1312 tokens[1]);
1313 }
1314 else {
1315 fmt::print(fg(fmt::color::cyan), "\n\t*** {} transformation will be applied to blocks:\n\t",
1316 tokens[1]);
1317 for (const auto *block : blocks) {
1318 fmt::print(fg(fmt::color::cyan), "{}, ", block->name());
1319 }
1320 fmt::print("\n");
1321 }
1322
1323 if (Ioss::Utils::substr_equal(tokens[1], "rotate")) {
1324 real rotation_matrix[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
1325
1326 // Get rotation axis...
1327 do {
1328 const std::string &axis = tokens[idx++];
1329 if (axis != "x" && axis != "y" && axis != "z" && axis != "X" && axis != "Y" &&
1330 axis != "Z") {
1331 fmt::print(stderr, fg(fmt::color::red),
1332 "ERROR: Unrecognized axis {}. Expected one of xyzXYZ.\n", axis);
1333 handle_help("geometry");
1334 return false;
1335 }
1336 double angle = std::stod(tokens[idx++]);
1337 auto ok = update_rotation_matrix(rotation_matrix, axis, angle);
1338 if (!ok) {
1339 return false;
1340 }
1341 } while (idx < tokens.size());
1342
1343 // Do the rotation...
1344 rotate_filtered_coordinates(region, rotation_matrix, blocks);
1345 fmt::print(fg(fmt::color::cyan), "\t*** Database coordinates rotated.\n");
1346 return false;
1347 }
1348
1349 if (Ioss::Utils::substr_equal(tokens[1], "scale")) {
1350 real scale[3] = {1.0, 1.0, 1.0};
1351
1352 // Get scale axis and scale factor...
1353 do {
1354 const std::string &axis = tokens[idx++];
1355 if (axis != "x" && axis != "y" && axis != "z" && axis != "X" && axis != "Y" &&
1356 axis != "Z") {
1357 fmt::print(stderr, fg(fmt::color::red),
1358 "ERROR: Unrecognized axis {}. Expected one of xyzXYZ.\n", axis);
1359 handle_help("geometry");
1360 return false;
1361 }
1362 double factor = std::stod(tokens[idx++]);
1363 if (Ioss::Utils::substr_equal(axis, "x")) {
1364 scale[0] = factor;
1365 }
1366 else if (Ioss::Utils::substr_equal(axis, "y")) {
1367 scale[1] = factor;
1368 }
1369 else if (Ioss::Utils::substr_equal(axis, "z")) {
1370 scale[2] = factor;
1371 }
1372 } while (idx < tokens.size());
1373
1374 // Do the transformation...
1375 scale_filtered_coordinates(region, scale, blocks);
1376 fmt::print(fg(fmt::color::cyan), "\t*** Database coordinate(s) scaled.\n");
1377 return false;
1378 }
1379
1380 if (Ioss::Utils::substr_equal(tokens[1], "mirror")) {
1381 real scale[3] = {1.0, 1.0, 1.0};
1382
1383 // Get mirror axis ...
1384 do {
1385 const std::string &axis = tokens[idx++];
1386 if (axis != "x" && axis != "y" && axis != "z" && axis != "X" && axis != "Y" &&
1387 axis != "Z") {
1388 fmt::print(stderr, fg(fmt::color::red),
1389 "ERROR: Unrecognized axis {}. Expected one of xyzXYZ.\n", axis);
1390 handle_help("geometry");
1391 return false;
1392 }
1393 if (Ioss::Utils::substr_equal(axis, "x")) {
1394 scale[0] = -1.0;
1395 }
1396 else if (Ioss::Utils::substr_equal(axis, "y")) {
1397 scale[1] = -1.0;
1398 }
1399 else if (Ioss::Utils::substr_equal(axis, "z")) {
1400 scale[2] = -1.0;
1401 }
1402 } while (idx < tokens.size());
1403
1404 // Do the transformation...
1405 scale_filtered_coordinates(region, scale, blocks);
1406 fmt::print(fg(fmt::color::cyan), "\t*** Database coordinate(s) mirrored.\n");
1407 return false;
1408 }
1409
1410 if (Ioss::Utils::substr_equal(tokens[1], "offset")) {
1411 real offset[3] = {1.0, 1.0, 1.0};
1412
1413 // Get offset axis and offset factor...
1414 do {
1415 const std::string &axis = tokens[idx++];
1416 if (axis != "x" && axis != "y" && axis != "z" && axis != "X" && axis != "Y" &&
1417 axis != "Z") {
1418 fmt::print(stderr, fg(fmt::color::red),
1419 "ERROR: Unrecognized axis {}. Expected one of xyzXYZ.\n", axis);
1420 handle_help("geometry");
1421 return false;
1422 }
1423 double factor = std::stod(tokens[idx++]);
1424 if (Ioss::Utils::substr_equal(axis, "x")) {
1425 offset[0] = factor;
1426 }
1427 else if (Ioss::Utils::substr_equal(axis, "y")) {
1428 offset[1] = factor;
1429 }
1430 else if (Ioss::Utils::substr_equal(axis, "z")) {
1431 offset[2] = factor;
1432 }
1433 } while (idx < tokens.size());
1434
1435 // Do the transformation...
1436 offset_filtered_coordinates(region, offset, blocks);
1437 fmt::print(fg(fmt::color::cyan), "\t*** Database coordinate(s) offset.\n");
1438 return false;
1439 }
1440
1441 fmt::print(stderr, fg(fmt::color::red), "ERROR: Unrecognized geometry command.\n");
1442 handle_help("geometry");
1443 return false;
1444 }
1445
1446 bool handle_assembly(const Ioss::NameList &tokens, Ioss::Region &region, bool allow_modify)
1447 {
1448 bool changed = false;
1449 Ioss::Assembly *assem = nullptr;
1450
1451 if (tokens.size() > 1) {
1452 assem = region.get_assembly(tokens[1]);
1453 if (assem == nullptr) {
1454 // New assembly...
1455 assem = new Ioss::Assembly(region.get_database(), tokens[1]);
1456 auto my_id = get_next_assembly_id(region);
1457 assem->property_add(Ioss::Property("id", my_id));
1458 assem->property_add(Ioss::Property("created", 1));
1459 region.add(assem);
1460 fmt::print(fg(fmt::color::cyan), "\t*** Created Assembly '{}' with id {}.\n", tokens[1],
1461 my_id);
1462 // Don't set changed to true; only set if members modified
1463 }
1464 }
1465 else {
1466 handle_help("assembly");
1467 return false;
1468 }
1469
1470 if (assem == nullptr) {
1471 fmt::print(stderr, fg(fmt::color::red), "ERROR: Unable to create or access assembly '{}'.\n",
1472 tokens[1]);
1473 return false;
1474 }
1475
1476 bool created = false;
1477 if (assem->property_exists("created")) {
1478 created = assem->get_property("created").get_int() == 1;
1479 }
1480 if (!allow_modify && !created) {
1481 fmt::print(stderr, fg(fmt::color::red),
1482 "ERROR: Unable to modify an existing assembly '{}'.\n\tRestart with "
1483 "`--allow_modifications` option or enter 'ALLOW MODIFICATIONS' command\n",
1484 tokens[1]);
1485 return false;
1486 }
1487
1488 if (tokens.size() > 2) {
1489 try {
1490 if (Ioss::Utils::substr_equal(tokens[2], "add")) {
1491 // List of names ...
1492 for (size_t i = 3; i < tokens.size(); i++) {
1493 auto *member = region.get_entity(tokens[i]);
1494 if (member != nullptr) {
1495 if (assem->add(member)) {
1496 changed = true;
1497 }
1498 }
1499 else {
1500 fmt::print(fg(fmt::color::yellow), "\t*** Entity '{}' does not exist. Not added.\n",
1501 tokens[i]);
1502 }
1503 }
1504 }
1505 else if (Ioss::Utils::substr_equal(tokens[2], "remove")) {
1506 // List of names ...
1507 for (size_t i = 3; i < tokens.size(); i++) {
1508 auto *member = region.get_entity(tokens[i]);
1509 if (member != nullptr) {
1510 if (assem->remove(member)) {
1511 changed = true;
1512 }
1513 }
1514 }
1515 }
1516 else if (Ioss::Utils::substr_equal(tokens[2], "type")) {
1517 // Determine type of add...
1518 Ioss::EntityType type = get_entity_type(tokens[3]);
1519 if (type == Ioss::INVALID_TYPE) {
1520 fmt::print(stderr, fg(fmt::color::red), "ERROR: Unrecognized entity type: '{}'\n",
1521 tokens[3]);
1522 return changed;
1523 }
1524 if (Ioss::Utils::substr_equal(tokens[4], "matches")) {
1525 // regex match on names
1526 // Get list of all names for this entity type...
1527 Ioss::NameList names = get_name_list(region, type);
1528
1529 std::regex reg(tokens[5], std::regex::extended);
1530
1531 // Check for match against all names in list...
1532 for (const auto &name : names) {
1533 if (std::regex_match(name, reg)) {
1534 const auto *entity = region.get_entity(name, type);
1535 if (entity != nullptr) {
1536 if (assem->add(entity)) {
1537 changed = true;
1538 }
1539 }
1540 }
1541 }
1542 }
1543 else if (Ioss::Utils::substr_equal(tokens[4], "glob")) {
1544 // regex match on names
1545 // Get list of all names for this entity type...
1546 Ioss::NameList names = get_name_list(region, type);
1547
1548 Ioss::glob::glob glob(tokens[5]);
1549
1550 // Check for match against all names in list...
1551 for (const auto &name : names) {
1552 if (Ioss::glob::glob_match(name, glob)) {
1553 const auto *entity = region.get_entity(name, type);
1554 if (entity != nullptr) {
1555 if (assem->add(entity)) {
1556 changed = true;
1557 }
1558 }
1559 }
1560 }
1561 }
1562 else if (Ioss::Utils::substr_equal(tokens[4], "named")) {
1563 // list of names
1564 for (size_t i = 5; i < tokens.size(); i++) {
1565 const auto *entity = region.get_entity(tokens[i], type);
1566 if (entity != nullptr) {
1567 if (assem->add(entity)) {
1568 changed = true;
1569 }
1570 }
1571 }
1572 }
1573 else if (Ioss::Utils::substr_equal(tokens[4], "ids")) {
1574 // list of ids
1575 for (size_t i = 5; i < tokens.size(); i++) {
1576 size_t id = std::stoul(tokens[i]);
1577 const auto *entity = region.get_entity(id, type);
1578 if (entity != nullptr) {
1579 if (assem->add(entity)) {
1580 changed = true;
1581 }
1582 }
1583 }
1584 }
1585 else if (Ioss::Utils::substr_equal(tokens[4], "range")) {
1586 // 0 1 2 3 4 5 6 7 8 9
1587 // ASSEMBLY {{name}} TYPE {{type}} RANGE {{id}} TO {{id}} BY {{step}}
1588 size_t begin = std::stoul(tokens[5]);
1589 size_t end = begin;
1590 size_t step = 1;
1591 if (tokens.size() >= 8 && Ioss::Utils::substr_equal(tokens[6], "to")) {
1592 end = std::stoul(tokens[7]);
1593 }
1594 if (tokens.size() >= 10 && Ioss::Utils::substr_equal(tokens[8], "by")) {
1595 step = std::stoul(tokens[9]);
1596 }
1597 for (size_t id = begin; id <= end; id += step) {
1598 const auto *entity = region.get_entity(id, type);
1599 if (entity != nullptr) {
1600 if (assem->add(entity)) {
1601 changed = true;
1602 }
1603 }
1604 }
1605 }
1606 }
1607 else {
1608 fmt::print(stderr, fg(fmt::color::red), "ERROR: Unrecognized assembly option '{}'.\n",
1609 tokens[2]);
1610 return changed;
1611 }
1612 }
1613 catch (const std::exception &x) {
1614 fmt::print(stderr, fg(fmt::color::red), "{}\n", x.what());
1615 }
1616 }
1617
1618 if (changed) {
1619 assem->property_add(Ioss::Property("modified", true));
1620 }
1621 else {
1622 fmt::print(stderr, fg(fmt::color::yellow), "WARNING: Command did not modify assembly '{}'\n",
1623 assem->name());
1624 }
1625
1626 return changed;
1627 }
1628
1629 template <typename T> Ioss::NameList get_entity_names(const std::vector<T *> &entity_list)
1630 {
1631 Ioss::NameList names;
1632 names.reserve(entity_list.size());
1633
1634 for (const auto *entity : entity_list) {
1635 names.push_back(entity->name());
1636 }
1637 return names;
1638 }
1639
1640 Ioss::NameList get_name_list(const Ioss::Region &region, Ioss::EntityType type)
1641 {
1642 Ioss::NameList names;
1643 switch (type) {
1644 case Ioss::ELEMENTBLOCK: {
1645 const auto &entities = region.get_element_blocks();
1646 names = get_entity_names(entities);
1647 } break;
1648 case Ioss::NODESET: {
1649 const auto &entities = region.get_nodesets();
1650 names = get_entity_names(entities);
1651 } break;
1652 case Ioss::SIDESET: {
1653 const auto &entities = region.get_sidesets();
1654 names = get_entity_names(entities);
1655 } break;
1656 case Ioss::ASSEMBLY: {
1657 const auto &entities = region.get_assemblies();
1658 names = get_entity_names(entities);
1659 } break;
1660 case Ioss::BLOB: {
1661 const auto &entities = region.get_blobs();
1662 names = get_entity_names(entities);
1663 } break;
1664 default: break;
1665 }
1666 return names;
1667 }
1668
1669 #if defined(SEACAS_HAVE_CGNS)
1670 void update_cgns_assembly_info(Ioss::Region &region, const Modify::Interface & /* interFace */)
1671 {
1672 region.end_mode(Ioss::STATE_DEFINE_MODEL);
1673 int file_ptr = region.get_database()->get_file_pointer();
1674
1675 fmt::print(fg(fmt::color::cyan), "\n\t*** Database changed. Updating assembly definitions.\n");
1676 const auto &assemblies = region.get_assemblies();
1677 for (const auto *assembly : assemblies) {
1678 if (assembly->property_exists("modified")) {
1679 if (assembly->property_exists("created")) {
1680 fmt::print(fg(fmt::color::cyan), "\t*** Creating assembly '{}'\n", assembly->name());
1681 Iocgns::Utils::output_assembly(file_ptr, assembly, false, true);
1682 }
1683 else {
1684 fmt::print(stderr, fg(fmt::color::yellow),
1685 "WARNING: Can not modify existing assembly '{}' yet.\n", assembly->name());
1686 }
1687 }
1688 }
1689 }
1690 #endif
1691
1692 void update_exodus_assembly_info(Ioss::Region &region, const Modify::Interface &interFace)
1693 {
1694 std::vector<Ioex::Assembly> ex_assemblies;
1695 bool modify_existing = false;
1696
1697 region.end_mode(Ioss::STATE_DEFINE_MODEL);
1698 fmt::print(fg(fmt::color::cyan), "\n\t*** Database changed. Updating assembly definitions.\n");
1699 const auto &assemblies = region.get_assemblies();
1700 for (const auto *assembly : assemblies) {
1701 if (assembly->property_exists("modified")) {
1702 ex_assemblies.emplace_back(*assembly);
1703 if (!assembly->property_exists("created")) {
1704 fmt::print(fg(fmt::color::cyan), "\t*** Modifying assembly '{}'\n", assembly->name());
1705 modify_existing = true;
1706 }
1707 else {
1708 fmt::print(fg(fmt::color::cyan), "\t*** Creating assembly '{}'\n", assembly->name());
1709 }
1710 }
1711 }
1712
1713 int exoid = region.get_database()->get_file_pointer();
1714 if (modify_existing) {
1715 // Need to create a temporary database to copy the database into.
1716 // Make sure has same int size as current.
1717 int byte_size = region.get_database()->int_byte_size_db();
1718 Ioss::PropertyManager properties;
1719 properties.add(Ioss::Property("INTEGER_SIZE_DB", byte_size));
1720 std::string out_file = interFace.filename() + ".mod";
1721 std::string file_type = interFace.type();
1722 Ioss::DatabaseIO *dbo = Ioss::IOFactory::create(
1723 file_type, out_file, Ioss::WRITE_RESTART, Ioss::ParallelUtils::comm_world(), properties);
1724
1725 if (dbo == nullptr || !dbo->ok(true)) {
1726 std::exit(EXIT_FAILURE);
1727 }
1728 // NOTE: 'region' owns 'db' pointer at this time...
1729 Ioss::Region reg_out(dbo, "region_tmp");
1730
1731 int out_exoid = reg_out.get_database()->get_file_pointer();
1732 Ioex::Internals::update_assembly_data(out_exoid, ex_assemblies, 1);
1733 Ioex::Internals::copy_database(exoid, out_exoid);
1734 Ioex::Internals::update_assembly_data(out_exoid, ex_assemblies, 2);
1735 Ioex::write_reduction_attributes(exoid, attributes_modified);
1736
1737 // Now, remove old file and replace with new...
1738 region.get_database()->closeDatabase();
1739 reg_out.get_database()->closeDatabase();
1740 Ioss::FileInfo in_file(interFace.filename());
1741 in_file.remove_file();
1742
1743 if (std::rename(out_file.c_str(), interFace.filename().c_str()) != 0) {
1744 fmt::print(stderr, fg(fmt::color::red), "ERROR: Could not update modified file {} to {}.\n",
1745 out_file, interFace.filename());
1746 return;
1747 }
1748 }
1749 else {
1750 Ioex::Internals::update_assembly_data(exoid, ex_assemblies);
1751 Ioex::write_reduction_attributes(exoid, attributes_modified);
1752 }
1753 }
1754
1755 void update_assembly_info(Ioss::Region &region, const Modify::Interface &interFace)
1756 {
1757 // Determine type of underlying database...
1758 const auto type = region.get_database()->get_format();
1759 if (type == "Exodus") {
1760 update_exodus_assembly_info(region, interFace);
1761 }
1762 else if (type == "CGNS") {
1763 #if defined(SEACAS_HAVE_CGNS)
1764 update_cgns_assembly_info(region, interFace);
1765 #else
1766 fmt::print(stderr, fg(fmt::color::red), "ERROR: CGNS capability is not enabled.\n");
1767 #endif
1768 }
1769 else {
1770 fmt::print(stderr, fg(fmt::color::red),
1771 "ERROR: Can not modify the database '{}' of type '{}'.\n", interFace.filename(),
1772 type);
1773 }
1774 }
1775
1776 void rotate_filtered_coordinates(const Ioss::GroupingEntity *nb, real rotation_matrix[3][3],
1777 const std::vector<int> &filter)
1778 {
1779 // `filter` is of size number of nodes. Value = 1 then rotate; value = 0 leave as is.
1780
1781 // Get original coordinates...
1782 std::vector<double> coord;
1783 nb->get_field_data("mesh_model_coordinates", coord);
1784 size_t node_count = coord.size() / 3;
1785
1786 // Do the rotation...
1787 for (size_t i = 0; i < node_count; i++) {
1788 if (filter.empty() || filter[i] == 1) {
1789 real x = coord[3 * i + 0];
1790 real y = coord[3 * i + 1];
1791 real z = coord[3 * i + 2];
1792
1793 real xn = x * rotation_matrix[0][0] + y * rotation_matrix[1][0] + z * rotation_matrix[2][0];
1794 real yn = x * rotation_matrix[0][1] + y * rotation_matrix[1][1] + z * rotation_matrix[2][1];
1795 real zn = x * rotation_matrix[0][2] + y * rotation_matrix[1][2] + z * rotation_matrix[2][2];
1796
1797 coord[3 * i + 0] = xn;
1798 coord[3 * i + 1] = yn;
1799 coord[3 * i + 2] = zn;
1800 }
1801 }
1802
1803 // Output updated coordinates...
1804 nb->put_field_data("mesh_model_coordinates", coord);
1805 }
1806
1807 void rotate_filtered_coordinates(Ioss::Region &region, real rotation_matrix[3][3],
1808 const std::vector<const Ioss::GroupingEntity *> &blocks)
1809 {
1810 const auto type = region.mesh_type();
1811 if (type == Ioss::MeshType::UNSTRUCTURED) {
1812 auto filter = get_filtered_node_list(region, blocks);
1813 Ioss::NodeBlock *nb = region.get_node_block("nodeblock_1");
1814 rotate_filtered_coordinates(nb, rotation_matrix, filter);
1815 }
1816 else if (type == Ioss::MeshType::STRUCTURED) {
1817 if (blocks.empty()) {
1818 auto sblocks = region.get_structured_blocks();
1819 for (const auto &sb : sblocks) {
1820 rotate_filtered_coordinates(sb, rotation_matrix, std::vector<int>());
1821 }
1822 }
1823 else {
1824 for (const auto &blk : blocks) {
1825 auto *sb = dynamic_cast<const Ioss::StructuredBlock *>(blk);
1826 if (sb != nullptr) {
1827 rotate_filtered_coordinates(sb, rotation_matrix, std::vector<int>());
1828 }
1829 }
1830 }
1831 }
1832 }
1833
1834 void offset_filtered_coordinates(const Ioss::GroupingEntity *nb, real offset[3],
1835 const std::vector<int> &filter)
1836 {
1837 // `filter` is of size number of nodes. Value = 1 transform; value = 0 leave as is.
1838
1839 // Get original coordinates...
1840 std::vector<double> coord;
1841 nb->get_field_data("mesh_model_coordinates", coord);
1842 size_t node_count = coord.size() / 3;
1843
1844 // Do the transformation...
1845 for (size_t i = 0; i < node_count; i++) {
1846 if (filter.empty() || filter[i] == 1) {
1847 coord[3 * i + 0] += offset[0];
1848 coord[3 * i + 1] += offset[1];
1849 coord[3 * i + 2] += offset[2];
1850 }
1851 }
1852
1853 // Output updated coordinates...
1854 nb->put_field_data("mesh_model_coordinates", coord);
1855 }
1856
1857 void offset_filtered_coordinates(Ioss::Region &region, real offset[3],
1858 const std::vector<const Ioss::GroupingEntity *> &blocks)
1859 {
1860 const auto type = region.mesh_type();
1861 if (type == Ioss::MeshType::UNSTRUCTURED) {
1862 auto filter = get_filtered_node_list(region, blocks);
1863 Ioss::NodeBlock *nb = region.get_node_block("nodeblock_1");
1864 offset_filtered_coordinates(nb, offset, filter);
1865 }
1866 else if (type == Ioss::MeshType::STRUCTURED) {
1867 if (blocks.empty()) {
1868 auto sblocks = region.get_structured_blocks();
1869 for (const auto &sb : sblocks) {
1870 offset_filtered_coordinates(sb, offset, std::vector<int>());
1871 }
1872 }
1873 else {
1874 for (const auto &blk : blocks) {
1875 auto *sb = dynamic_cast<const Ioss::StructuredBlock *>(blk);
1876 if (sb != nullptr) {
1877 offset_filtered_coordinates(sb, offset, std::vector<int>());
1878 }
1879 }
1880 }
1881 }
1882 }
1883
1884 void scale_filtered_coordinates(const Ioss::GroupingEntity *nb, real scale[3],
1885 const std::vector<int> &filter)
1886 {
1887 // Get original coordinates...
1888 std::vector<double> coord;
1889 nb->get_field_data("mesh_model_coordinates", coord);
1890 size_t node_count = coord.size() / 3;
1891
1892 // Do the transformation...
1893 for (size_t i = 0; i < node_count; i++) {
1894 if (filter.empty() || filter[i] == 1) {
1895 coord[3 * i + 0] *= scale[0];
1896 coord[3 * i + 1] *= scale[1];
1897 coord[3 * i + 2] *= scale[2];
1898 }
1899 }
1900
1901 // Output updated coordinates...
1902 nb->put_field_data("mesh_model_coordinates", coord);
1903 }
1904
1905 void scale_filtered_coordinates(Ioss::Region &region, real scale[3],
1906 const std::vector<const Ioss::GroupingEntity *> &blocks)
1907 {
1908 const auto type = region.mesh_type();
1909 if (type == Ioss::MeshType::UNSTRUCTURED) {
1910 auto filter = get_filtered_node_list(region, blocks);
1911 Ioss::NodeBlock *nb = region.get_node_block("nodeblock_1");
1912 scale_filtered_coordinates(nb, scale, filter);
1913 }
1914 else if (type == Ioss::MeshType::STRUCTURED) {
1915 if (blocks.empty()) {
1916 auto sblocks = region.get_structured_blocks();
1917 for (const auto &sb : sblocks) {
1918 scale_filtered_coordinates(sb, scale, std::vector<int>());
1919 }
1920 }
1921 else {
1922 for (const auto &blk : blocks) {
1923 auto *sb = dynamic_cast<const Ioss::StructuredBlock *>(blk);
1924 if (sb != nullptr) {
1925 scale_filtered_coordinates(sb, scale, std::vector<int>());
1926 }
1927 }
1928 }
1929 }
1930 }
1931
1932 bool update_rotation_matrix(real rotation_matrix[3][3], const std::string &axis, double angle)
1933 {
1934 int n1 = 0;
1935 int n2 = 0;
1936 int n3 = 0;
1937
1938 if (Ioss::Utils::substr_equal(axis, "x")) {
1939 n1 = 1;
1940 n2 = 2;
1941 n3 = 0;
1942 }
1943 else if (Ioss::Utils::substr_equal(axis, "y")) {
1944 n1 = 2;
1945 n2 = 0;
1946 n3 = 1;
1947 }
1948 else if (Ioss::Utils::substr_equal(axis, "z")) {
1949 n1 = 0;
1950 n2 = 1;
1951 n3 = 2;
1952 }
1953 else {
1954 fmt::print(stderr, fg(fmt::color::red),
1955 "ERROR: Requested rotation axis '{}' is not valid. Must be 'x', 'y', or 'z'\n",
1956 axis);
1957 return false;
1958 }
1959
1960 static real degang = std::atan2(0.0L, -1.0L) / 180.0;
1961 real ang_rad = angle * degang;
1962
1963 auto cosang = std::cos(ang_rad);
1964 auto sinang = std::sin(ang_rad);
1965
1966 real by[3][3];
1967 real res[3][3];
1968
1969 by[n1][n1] = cosang;
1970 by[n2][n1] = -sinang;
1971 by[n1][n3] = 0.0;
1972 by[n1][n2] = sinang;
1973 by[n2][n2] = cosang;
1974 by[n2][n3] = 0.0;
1975 by[n3][n1] = 0.0;
1976 by[n3][n2] = 0.0;
1977 by[n3][n3] = 1.0;
1978
1979 for (int i = 0; i < 3; i++) {
1980 for (int j = 0; j < 3; j++) {
1981 res[i][j] = rotation_matrix[i][0] * by[0][j] + rotation_matrix[i][1] * by[1][j] +
1982 rotation_matrix[i][2] * by[2][j];
1983 }
1984 }
1985
1986 for (int i = 0; i < 3; i++) {
1987 for (int j = 0; j < 3; j++) {
1988 rotation_matrix[i][j] = res[i][j];
1989 }
1990 }
1991 return true;
1992 }
1993
1994 void modify_time(Ioss::Region &region, double scale, double offset)
1995 {
1996 size_t ts_count = region.get_optional_property("state_count", 0);
1997 std::vector<double> times(ts_count);
1998
1999 int exoid = region.get_database()->get_file_pointer();
2000 ex_get_all_times(exoid, Data(times));
2001
2002 for (size_t step = 0; step < ts_count; step++) {
2003 times[step] = times[step] * scale + offset;
2004 ex_put_time(exoid, step + 1, &times[step]);
2005 }
2006 // Now update the `last_time_attribute`...
2007 auto max_time = *std::max_element(times.begin(), times.end());
2008 Ioex::update_last_time_attribute(exoid, max_time);
2009 (void)region.get_min_time(); // Triggers reloading region stateTimes vector.
2010 }
2011 } // nameSpace