Debian Codegraph @

packages/seacas/libraries/ioss/src/unit_tests/UnitTestDynamicTopology.C

raw
1 // Copyright(C) 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 <string>
8 #include <vector>
9
10 #ifdef SEACAS_HAVE_MPI
11 #include "mpi.h"
12 #endif
13 #include "gtest/gtest.h"
14
15 #include <algorithm>
16 #include <cctype> // std::tolower
17 #include <fstream>
18 #include <functional>
19 #include <iostream>
20 #include <random>
21
22 #include <unistd.h> // for unlink
23
24 #include "Ionit_Initializer.h"
25 #include "Ioss_ChangeSet.h"
26 #include "Ioss_ChangeSetFactory.h"
27 #include "Ioss_DBUsage.h"
28 #include "Ioss_DatabaseIO.h" // for DatabaseIO
29 #include "Ioss_DynamicTopology.h"
30 #include "Ioss_DynamicTopologyBroker.h"
31 #include "Ioss_DynamicTopologyFileControl.h"
32 #include "Ioss_ElementBlock.h"
33 #include "Ioss_Field.h" // for Field, etc
34 #include "Ioss_FileInfo.h"
35 #include "Ioss_IOFactory.h"
36 #include "Ioss_NodeBlock.h"
37 #include "Ioss_ParallelUtils.h"
38 #include "Ioss_Property.h"
39 #include "Ioss_Region.h"
40 #include "Ioss_Utils.h"
41
42 #include "exodus/Ioex_DatabaseIO.h"
43
44 namespace {
45 std::string get_many_block_mesh_desc(unsigned numBlocks)
46 {
47 std::ostringstream oss;
48 std::vector<unsigned> elementIds(numBlocks);
49 std::iota(elementIds.begin(), elementIds.end(), 1);
50
51 unsigned proc = 0;
52 for (unsigned i = 0; i < numBlocks; ++i) {
53 unsigned elemId = elementIds[i];
54 unsigned firstNodeId = i * 4 + 1;
55 oss << proc << "," << elemId << ",HEX_8,";
56 for (unsigned node = firstNodeId; node < firstNodeId + 8; ++node) {
57 oss << node << ",";
58 }
59 unsigned blockId = i + 1;
60 oss << "block_" << blockId;
61
62 if (i < numBlocks - 1) {
63 oss << "\n";
64 }
65
66 proc++;
67 }
68
69 oss << "|coordinates:";
70
71 std::vector<double> planeCoords = {0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0};
72
73 for (double coord : planeCoords) {
74 oss << coord << ",";
75 }
76
77 for (unsigned i = 1; i <= numBlocks; ++i) {
78 for (unsigned point = 0; point < 4; ++point) {
79 planeCoords[3 * point + 2] += 1;
80 }
81
82 for (double coord : planeCoords) {
83 oss << coord << ",";
84 }
85 }
86
87 return oss.str();
88 }
89
90 void define_model(const Ioss::Region &i_region, Ioss::Region &o_region)
91 {
92 Ioss::DatabaseIO *o_database = o_region.get_database();
93
94 o_region.begin_mode(Ioss::STATE_DEFINE_MODEL);
95
96 Ioss::NodeBlock *i_nb = i_region.get_node_blocks()[0];
97 int64_t spatial_dim = 3;
98 int64_t num_nodes = i_nb->entity_count();
99 Ioss::NodeBlock *o_nb = new Ioss::NodeBlock(o_database, "nodeblock_1", num_nodes, spatial_dim);
100 o_region.add(o_nb);
101
102 for (Ioss::ElementBlock *i_eb : i_region.get_element_blocks()) {
103 Ioss::ElementBlock *o_eb = new Ioss::ElementBlock(
104 o_database, i_eb->name(), i_eb->topology()->name(), i_eb->entity_count());
105 o_eb->property_add(i_eb->get_property("id"));
106 o_region.add(o_eb);
107 }
108
109 o_region.end_mode(Ioss::STATE_DEFINE_MODEL);
110 }
111
112 void write_model(const Ioss::Region &i_region, Ioss::Region &o_region)
113 {
114 Ioss::NodeBlock *i_nb = i_region.get_node_blocks()[0];
115 Ioss::NodeBlock *o_nb = o_region.get_node_blocks()[0];
116
117 o_region.begin_mode(Ioss::STATE_MODEL);
118 std::vector<double> coordinates;
119 std::vector<int> node_ids;
120 i_nb->get_field_data("ids", node_ids);
121 i_nb->get_field_data("mesh_model_coordinates", coordinates);
122
123 o_nb->put_field_data("ids", node_ids);
124 o_nb->put_field_data("mesh_model_coordinates", coordinates);
125
126 for (Ioss::ElementBlock *i_eb : i_region.get_element_blocks()) {
127 Ioss::ElementBlock *o_eb = o_region.get_element_block(i_eb->name());
128 std::vector<int> elem_ids;
129 std::vector<int> connectivity;
130
131 i_eb->get_field_data("ids", elem_ids);
132 i_eb->get_field_data("connectivity", connectivity);
133
134 o_eb->put_field_data("ids", elem_ids);
135 o_eb->put_field_data("connectivity", connectivity);
136 }
137
138 o_region.end_mode(Ioss::STATE_MODEL);
139 }
140
141 void define_transient(Ioss::Region &o_region, const std::string &elemFieldName)
142 {
143 o_region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
144
145 for (Ioss::ElementBlock *o_eb : o_region.get_element_blocks()) {
146 size_t num_elem = o_eb->get_property("entity_count").get_int();
147 std::string storage = "scalar";
148
149 Ioss::Field field(elemFieldName, Ioss::Field::REAL, storage, 1, Ioss::Field::Field::TRANSIENT,
150 num_elem);
151 o_eb->field_add(field);
152 }
153 o_region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
154 }
155
156 int write_transient(Ioss::Region &o_region, const std::string &elemFieldName, const double time)
157 {
158 o_region.begin_mode(Ioss::STATE_TRANSIENT);
159 int step = o_region.add_state(time);
160 o_region.begin_state(step);
161
162 for (Ioss::ElementBlock *o_eb : o_region.get_element_blocks()) {
163 size_t num_elem = o_eb->get_property("entity_count").get_int();
164
165 std::vector<double> field_data(num_elem);
166 std::vector<int> elem_ids;
167
168 o_eb->get_field_data("ids", elem_ids);
169 for (size_t i = 0; i < elem_ids.size(); i++) {
170 field_data[i] = (double)elem_ids[i] + 100 * time;
171 }
172
173 o_eb->put_field_data(elemFieldName, field_data);
174 }
175
176 o_region.end_state(step);
177 o_region.end_mode(Ioss::STATE_TRANSIENT);
178
179 return step;
180 }
181
182 class Observer : public Ioss::DynamicTopologyObserver
183 {
184 public:
185 Observer(Ioss::Region &inputRegion_, const std::string &elemFieldName_,
186 const Ioss::FileControlOption fileControlOption_)
187 : Ioss::DynamicTopologyObserver(nullptr), inputRegion(inputRegion_),
188 elemFieldName(elemFieldName_), fileControlOption(fileControlOption_)
189 {
190 }
191
192 virtual ~Observer() {}
193
194 void define_model() override { ::define_model(inputRegion, *get_region()); }
195
196 void write_model() override { ::write_model(inputRegion, *get_region()); }
197
198 void define_transient() override { ::define_transient(*get_region(), elemFieldName); }
199
200 Ioss::FileControlOption get_control_option() const { return fileControlOption; }
201
202 private:
203 Observer();
204
205 Ioss::Region &inputRegion;
206 const std::string elemFieldName;
207 Ioss::FileControlOption fileControlOption;
208 };
209
210 struct OutputParams
211 {
212 OutputParams(const std::string &outFile_) : outFile(outFile_) {}
213
214 OutputParams(const std::string &outFile_, const std::string &elemFieldName_)
215 : outFile(outFile_), elemFieldName(elemFieldName_)
216 {
217 }
218
219 void set_data(const std::vector<double> &output_times_, const std::vector<bool> &output_steps_,
220 const std::vector<bool> &modification_steps_)
221 {
222 ASSERT_EQ(output_times_.size(), output_steps_.size());
223 ASSERT_EQ(output_times_.size(), modification_steps_.size());
224
225 size_t numSteps = output_times_.size();
226 for (size_t i = 1; i < numSteps; i++) {
227 // Monotone increasing
228 ASSERT_TRUE(output_times_[i] > output_times_[i - 1]);
229 }
230
231 output_times = output_times_;
232 output_steps = output_steps_;
233 modification_steps = modification_steps_;
234 }
235
236 void set_data(const std::vector<bool> &output_steps_,
237 const std::vector<bool> &modification_steps_)
238 {
239 ASSERT_EQ(output_steps_.size(), modification_steps_.size());
240
241 size_t numSteps = output_steps_.size();
242 for (size_t i = 0; i < numSteps; i++) {
243 output_times.push_back((double)i);
244 }
245 output_steps = output_steps_;
246 modification_steps = modification_steps_;
247 }
248
249 struct OutputParams &add(const double time, const bool do_output, const bool do_modification)
250 {
251 size_t numSteps = output_times.size();
252 if (numSteps > 0) {
253 // Monotone increasing
254 EXPECT_TRUE(time > output_times[numSteps - 1]);
255 }
256
257 output_times.push_back(time);
258 output_steps.push_back(do_output);
259 modification_steps.push_back(do_modification);
260
261 return *this;
262 }
263
264 void clear()
265 {
266 output_times.clear();
267 output_steps.clear();
268 modification_steps.clear();
269 }
270
271 void set_cyclic_count(unsigned cyclicCount_) { cyclicCount = cyclicCount_; }
272
273 unsigned cyclicCount{0};
274 std::string outFile{"file.g"};
275 std::string elemFieldName{"elem_field"};
276 std::vector<double> output_times;
277 std::vector<bool> output_steps;
278 std::vector<bool> modification_steps;
279 };
280
281 void do_output(Ioss::Region &o_region, const OutputParams &params, size_t step, double &minTime,
282 int &maxStep, bool &doneOutputAfterModification)
283 {
284 if (params.output_steps[step]) {
285 if (!doneOutputAfterModification) {
286 minTime = params.output_times[step];
287 }
288
289 write_transient(o_region, params.elemFieldName, params.output_times[step]);
290
291 auto min_result = o_region.get_min_time();
292 EXPECT_EQ(1, min_result.first);
293 EXPECT_NEAR(minTime, min_result.second, 1.0e-6);
294
295 auto max_result = o_region.get_max_time();
296 EXPECT_EQ(maxStep, max_result.first);
297 EXPECT_NEAR(params.output_times[step], max_result.second, 1.0e-6);
298
299 maxStep++;
300 doneOutputAfterModification = true;
301 }
302 }
303
304 void run_topology_change(const Ioss::Region &i_region, Ioss::Region &o_region,
305 const OutputParams &params)
306 {
307 auto observer = o_region.get_mesh_modification_observer();
308
309 define_model(i_region, o_region);
310 write_model(i_region, o_region);
311
312 define_transient(o_region, params.elemFieldName);
313
314 auto numSteps = params.output_steps.size();
315
316 int maxStep = 1;
317
318 double minTime = numSteps > 0 ? params.output_times[0] : 0.0;
319
320 bool doneOutputAfterModification = true;
321
322 for (size_t i = 0; i < numSteps; i++) {
323 if (params.modification_steps[i]) {
324 observer->set_topology_modification(Ioss::TOPOLOGY_UNKNOWN);
325 maxStep = 1;
326 doneOutputAfterModification = false;
327 }
328
329 do_output(o_region, params, i, minTime, maxStep, doneOutputAfterModification);
330 }
331 }
332
333 void cleanup_linear_multi_files(const std::string &outFile, int numOutputs = 1)
334 {
335 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
336
337 for (int i = 1; i <= numOutputs; i++) {
338 std::string baseFile =
339 Ioss::DynamicTopologyFileControl::get_linear_database_filename(outFile, i);
340 std::string parallelFile =
341 Ioss::Utils::decode_filename(baseFile, util.parallel_rank(), util.parallel_size());
342 unlink(parallelFile.c_str());
343 }
344 }
345
346 std::shared_ptr<Ioss::Region> construct_region(const OutputParams &params,
347 Ioss::PropertyManager &propertyManager,
348 Ioss::DatabaseUsage db_usage,
349 const std::string &name)
350 {
351 std::string outFile = params.outFile;
352 if (params.cyclicCount > 0) {
353 outFile = Ioss::DynamicTopologyFileControl::get_cyclic_database_filename(
354 params.outFile, params.cyclicCount, 0);
355 }
356 propertyManager.add(Ioss::Property("base_filename", params.outFile));
357 Ioss::DatabaseIO *database = Ioss::IOFactory::create(
358 "exodus", outFile, db_usage, Ioss::ParallelUtils::comm_world(), propertyManager);
359
360 auto region = std::make_shared<Ioss::Region>(database, name);
361 region->set_file_cyclic_count(params.cyclicCount);
362 EXPECT_TRUE(database != nullptr);
363 EXPECT_TRUE(database->ok(true));
364
365 return region;
366 }
367
368 void run_multi_file_topology_change(const OutputParams &params)
369 {
370 Ioss::Init::Initializer io;
371 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
372
373 int numBlocks = util.parallel_size();
374
375 std::string meshDesc = get_many_block_mesh_desc(numBlocks);
376
377 Ioss::PropertyManager propertyManager;
378
379 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
380 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
381 Ioss::Region i_region(i_database, "input_model");
382 EXPECT_TRUE(i_database != nullptr);
383 EXPECT_TRUE(i_database->ok(true));
384
385 auto o_region = construct_region(params, propertyManager, Ioss::WRITE_RESULTS, "output_model");
386
387 auto fileControlOption = Ioss::FileControlOption::CONTROL_AUTO_MULTI_FILE;
388 auto observer = std::make_shared<Observer>(i_region, params.elemFieldName, fileControlOption);
389 o_region->register_mesh_modification_observer(observer);
390
391 run_topology_change(i_region, *o_region, params);
392 }
393
394 TEST(TestDynamicWrite, multi_file_simple_topology_modification)
395 {
396 std::string outFile("multiFileManyBlocks.g");
397 std::string elemFieldName = "elem_field";
398
399 OutputParams params(outFile, elemFieldName);
400
401 std::vector<bool> output_steps{true, true, true, true, true, true};
402 std::vector<bool> modification_steps{false, true, false, true, true, false};
403
404 params.set_data(output_steps, modification_steps);
405
406 cleanup_linear_multi_files(outFile, params.output_times.size());
407 run_multi_file_topology_change(params);
408 cleanup_linear_multi_files(outFile, params.output_times.size());
409 }
410
411 void cleanup_cyclic_multi_files(const std::string &outFile, unsigned cyclicCount = 3)
412 {
413 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
414
415 for (unsigned i = 1; i <= cyclicCount; i++) {
416 std::string baseFile =
417 Ioss::DynamicTopologyFileControl::get_cyclic_database_filename(outFile, cyclicCount, i);
418 std::string parallelFile =
419 Ioss::Utils::decode_filename(baseFile, util.parallel_rank(), util.parallel_size());
420 unlink(parallelFile.c_str());
421 }
422 }
423
424 TEST(TestDynamicWrite, multi_file_cyclic_topology_modification)
425 {
426 std::string outFile("cyclicMultiFileManyBlocks.g");
427 std::string elemFieldName = "elem_field";
428
429 OutputParams params(outFile, elemFieldName);
430
431 std::vector<double> output_times{0.0, 0.5, 1.5, 1.75, 2.0, 3.0};
432 std::vector<bool> output_steps{true, true, true, true, true, true};
433 std::vector<bool> modification_steps{false, true, false, true, true, false};
434
435 params.set_data(output_times, output_steps, modification_steps);
436 params.set_cyclic_count(3);
437
438 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
439 run_multi_file_topology_change(params);
440 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
441 }
442
443 void fill_internal_file_change_set_gold_names(const int numChangeSets,
444 std::vector<std::string> &gold_names,
445 std::vector<std::string> &gold_full_names)
446 {
447 gold_names.clear();
448 gold_full_names.clear();
449
450 for (int i = 1; i <= numChangeSets; i++) {
451 std::string setName = Ioss::DynamicTopologyFileControl::get_internal_file_change_set_name(i);
452
453 gold_names.push_back(setName);
454 gold_full_names.push_back("/" + setName);
455 }
456 }
457
458 void test_internal_file_change_set_names(Ioss::DatabaseIO *database)
459 {
460 Ioss::NameList names = database->internal_change_set_describe(false);
461 Ioss::NameList full_names = database->internal_change_set_describe(true);
462
463 std::vector<std::string> gold_names;
464 std::vector<std::string> gold_full_names;
465
466 fill_internal_file_change_set_gold_names(database->num_internal_change_set(), gold_names,
467 gold_full_names);
468
469 EXPECT_EQ(gold_names, names);
470 EXPECT_EQ(gold_full_names, full_names);
471 }
472
473 void cleanup_single_file(const std::string &outFile)
474 {
475 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
476
477 std::string file =
478 Ioss::Utils::decode_filename(outFile, util.parallel_rank(), util.parallel_size());
479 unlink(file.c_str());
480 }
481
482 void run_single_file_simple_topology_change(const OutputParams &params)
483 {
484 Ioss::Init::Initializer io;
485 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
486
487 int numBlocks = util.parallel_size();
488
489 std::string meshDesc = get_many_block_mesh_desc(numBlocks);
490
491 Ioss::PropertyManager propertyManager;
492
493 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
494 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
495 Ioss::Region i_region(i_database, "input_model");
496 EXPECT_TRUE(i_database != nullptr);
497 EXPECT_TRUE(i_database->ok(true));
498
499 propertyManager.add(Ioss::Property("ENABLE_FILE_GROUPS", 1));
500 propertyManager.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP));
501 Ioss::DatabaseIO *o_database =
502 Ioss::IOFactory::create("exodus", params.outFile, Ioss::WRITE_RESULTS,
503 Ioss::ParallelUtils::comm_world(), propertyManager);
504 Ioss::Region o_region(o_database, "output_model");
505 EXPECT_TRUE(o_database != nullptr);
506 EXPECT_TRUE(o_database->ok(true));
507 EXPECT_TRUE(o_database->supports_internal_change_set());
508
509 auto fileControlOption = Ioss::FileControlOption::CONTROL_AUTO_GROUP_FILE;
510 auto observer = std::make_shared<Observer>(i_region, params.elemFieldName, fileControlOption);
511 o_region.register_mesh_modification_observer(observer);
512
513 run_topology_change(i_region, o_region, params);
514 test_internal_file_change_set_names(o_database);
515 }
516
517 TEST(TestDynamicWrite, single_file_simple_topology_modification)
518 {
519 std::string outFile("singleFileManyBlocks.g");
520 std::string elemFieldName = "elem_field";
521
522 OutputParams params(outFile, elemFieldName);
523
524 params.add(0.0, true, false)
525 .add(1.0, true, true)
526 .add(2.0, true, false)
527 .add(3.0, true, true)
528 .add(4.0, true, true)
529 .add(5.0, true, false);
530
531 cleanup_single_file(outFile);
532 run_single_file_simple_topology_change(params);
533 cleanup_single_file(outFile);
534 }
535
536 TEST(TestDynamicWrite, single_file_groups_not_enabled)
537 {
538 Ioss::Init::Initializer io;
539 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
540
541 int numBlocks = util.parallel_size();
542 if (numBlocks > 1)
543 GTEST_SKIP();
544
545 std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1"
546 "|coordinates:0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,1,1,0,1,1";
547
548 Ioss::PropertyManager propertyManager;
549
550 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
551 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
552 Ioss::Region i_region(i_database, "input_model");
553 EXPECT_TRUE(i_database != nullptr);
554 EXPECT_TRUE(i_database->ok(true));
555
556 std::string outFile("singleFileGroupsNotEnabled.g");
557 std::string elemFieldName = "elem_field";
558 cleanup_single_file(outFile);
559
560 // Need the line below to allow this to pass
561 // propertyManager.add(Ioss::Property("ENABLE_FILE_GROUPS", 1));
562 propertyManager.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP));
563 Ioss::DatabaseIO *o_database = Ioss::IOFactory::create(
564 "exodus", outFile, Ioss::WRITE_RESULTS, Ioss::ParallelUtils::comm_world(), propertyManager);
565 Ioss::Region o_region(o_database, "output_model");
566 EXPECT_TRUE(o_database != nullptr);
567 EXPECT_TRUE(o_database->ok(true));
568
569 auto fileControlOption = Ioss::FileControlOption::CONTROL_AUTO_GROUP_FILE;
570 auto observer = std::make_shared<Observer>(i_region, elemFieldName, fileControlOption);
571 EXPECT_THROW(o_region.register_mesh_modification_observer(observer), std::runtime_error);
572 cleanup_single_file(outFile);
573 }
574
575 TEST(TestDynamicWrite, create_subgroup_with_file_reopen)
576 {
577 std::string outFile("subgroupManyBlocks.g");
578 std::string elemFieldName = "elem_field";
579
580 Ioss::Init::Initializer io;
581 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
582
583 std::string file1 =
584 Ioss::Utils::decode_filename(outFile, util.parallel_rank(), util.parallel_size());
585 unlink(file1.c_str());
586
587 int numBlocks = util.parallel_size();
588 if (numBlocks > 1)
589 GTEST_SKIP();
590
591 std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1"
592 "|coordinates:0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,1,1,0,1,1";
593
594 Ioss::PropertyManager propertyManager;
595
596 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
597 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
598 Ioss::Region i_region(i_database, "input_model");
599 EXPECT_TRUE(i_database != nullptr);
600 EXPECT_TRUE(i_database->ok(true));
601
602 {
603 propertyManager.add(Ioss::Property("ENABLE_FILE_GROUPS", 1));
604 Ioss::DatabaseIO *o_database =
605 Ioss::IOFactory::create("exodus", outFile, Ioss::WRITE_RESULTS,
606 Ioss::ParallelUtils::comm_world(), propertyManager);
607
608 Ioex::BaseDatabaseIO *ex_database = dynamic_cast<Ioex::BaseDatabaseIO *>(o_database);
609 EXPECT_TRUE(nullptr != ex_database);
610
611 Ioss::Region o_region(o_database, "output_model");
612 EXPECT_TRUE(o_database != nullptr);
613 EXPECT_TRUE(o_database->ok(true));
614 EXPECT_TRUE(o_database->supports_internal_change_set());
615
616 ex_database->create_subgroup("GROUP_1");
617 }
618
619 {
620 propertyManager.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP));
621 Ioss::DatabaseIO *o_database =
622 Ioss::IOFactory::create("exodus", outFile, Ioss::WRITE_RESULTS,
623 Ioss::ParallelUtils::comm_world(), propertyManager);
624
625 Ioex::BaseDatabaseIO *ex_database = dynamic_cast<Ioex::BaseDatabaseIO *>(o_database);
626 EXPECT_TRUE(nullptr != ex_database);
627
628 Ioss::Region o_region(o_database, "output_model");
629 EXPECT_TRUE(o_database != nullptr);
630 EXPECT_TRUE(o_database->ok(true));
631
632 EXPECT_TRUE(ex_database->supports_group());
633
634 // Group pointer is automatically at first child
635 ex_database->create_subgroup("GROUP_2");
636
637 Ioss::NameList names = ex_database->groups_describe(false);
638 Ioss::NameList full_names = ex_database->groups_describe(true);
639
640 std::vector<std::string> gold_names{"/", "GROUP_1", "GROUP_2"};
641 std::vector<std::string> gold_full_names{"/", "/GROUP_1", "/GROUP_1/GROUP_2"};
642
643 EXPECT_EQ(gold_names, names);
644 EXPECT_EQ(gold_full_names, full_names);
645 }
646
647 unlink(file1.c_str());
648 }
649
650 TEST(TestDynamicWrite, create_subgroup_with_file_persistence_and_child_group)
651 {
652 std::string outFile("subgroupManyBlocks.g");
653 std::string elemFieldName = "elem_field";
654
655 Ioss::Init::Initializer io;
656 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
657
658 std::string file1 =
659 Ioss::Utils::decode_filename(outFile, util.parallel_rank(), util.parallel_size());
660 unlink(file1.c_str());
661
662 int numBlocks = util.parallel_size();
663 if (numBlocks > 1)
664 GTEST_SKIP();
665
666 std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1"
667 "|coordinates:0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,1,1,0,1,1";
668
669 Ioss::PropertyManager propertyManager;
670
671 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
672 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
673 Ioss::Region i_region(i_database, "input_model");
674 EXPECT_TRUE(i_database != nullptr);
675 EXPECT_TRUE(i_database->ok(true));
676
677 {
678 propertyManager.add(Ioss::Property("ENABLE_FILE_GROUPS", 1));
679 propertyManager.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP));
680 Ioss::DatabaseIO *o_database =
681 Ioss::IOFactory::create("exodus", outFile, Ioss::WRITE_RESULTS,
682 Ioss::ParallelUtils::comm_world(), propertyManager);
683
684 Ioex::BaseDatabaseIO *ex_database = dynamic_cast<Ioex::BaseDatabaseIO *>(o_database);
685 EXPECT_TRUE(nullptr != ex_database);
686
687 Ioss::Region o_region(o_database, "output_model");
688 EXPECT_TRUE(o_database != nullptr);
689 EXPECT_TRUE(o_database->ok(true));
690
691 EXPECT_TRUE(ex_database->supports_group());
692
693 ex_database->create_subgroup("GROUP_1");
694
695 // Group pointer is at "GROUP_1" ... "GROUP_2" is a child
696 ex_database->create_subgroup("GROUP_2");
697
698 Ioss::NameList names = ex_database->groups_describe(false);
699 Ioss::NameList full_names = ex_database->groups_describe(true);
700
701 std::vector<std::string> gold_names{"/", "GROUP_1", "GROUP_2"};
702 std::vector<std::string> gold_full_names{"/", "/GROUP_1", "/GROUP_1/GROUP_2"};
703
704 EXPECT_EQ(gold_names, names);
705 EXPECT_EQ(gold_full_names, full_names);
706 }
707
708 unlink(file1.c_str());
709 }
710
711 TEST(TestDynamicWrite, create_subgroup_with_file_persistence_and_no_child_group)
712 {
713 std::string outFile("subgroupManyBlocks.g");
714 std::string elemFieldName = "elem_field";
715
716 Ioss::Init::Initializer io;
717 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
718
719 std::string file1 =
720 Ioss::Utils::decode_filename(outFile, util.parallel_rank(), util.parallel_size());
721 unlink(file1.c_str());
722
723 int numBlocks = util.parallel_size();
724 if (numBlocks > 1)
725 GTEST_SKIP();
726
727 std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1"
728 "|coordinates:0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,1,1,0,1,1";
729
730 Ioss::PropertyManager propertyManager;
731
732 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
733 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
734 Ioss::Region i_region(i_database, "input_model");
735 EXPECT_TRUE(i_database != nullptr);
736 EXPECT_TRUE(i_database->ok(true));
737
738 {
739 propertyManager.add(Ioss::Property("ENABLE_FILE_GROUPS", 1));
740 propertyManager.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP));
741 Ioss::DatabaseIO *o_database =
742 Ioss::IOFactory::create("exodus", outFile, Ioss::WRITE_RESULTS,
743 Ioss::ParallelUtils::comm_world(), propertyManager);
744
745 Ioex::BaseDatabaseIO *ex_database = dynamic_cast<Ioex::BaseDatabaseIO *>(o_database);
746 EXPECT_TRUE(nullptr != ex_database);
747
748 Ioss::Region o_region(o_database, "output_model");
749 EXPECT_TRUE(o_database != nullptr);
750 EXPECT_TRUE(o_database->ok(true));
751
752 EXPECT_TRUE(ex_database->supports_group());
753
754 ex_database->create_subgroup("GROUP_1");
755
756 // Group pointer is reset to root group
757 EXPECT_TRUE(ex_database->open_root_group());
758 ex_database->create_subgroup("GROUP_2");
759
760 Ioss::NameList names = ex_database->groups_describe(false);
761 Ioss::NameList full_names = ex_database->groups_describe(true);
762
763 std::vector<std::string> gold_names{"/", "GROUP_1", "GROUP_2"};
764 std::vector<std::string> gold_full_names{"/", "/GROUP_1", "/GROUP_2"};
765
766 EXPECT_EQ(gold_names, names);
767 EXPECT_EQ(gold_full_names, full_names);
768 }
769
770 unlink(file1.c_str());
771 }
772
773 void run_topology_change_with_multiple_output(const Ioss::Region &i_region,
774 Ioss::Region &o_region1, Ioss::Region &o_region2,
775 const OutputParams &params1,
776 const OutputParams &params2)
777 {
778 ASSERT_EQ(params1.modification_steps, params2.modification_steps);
779
780 auto observer1 = o_region1.get_mesh_modification_observer();
781 auto observer2 = o_region2.get_mesh_modification_observer();
782
783 define_model(i_region, o_region1);
784 write_model(i_region, o_region1);
785 define_transient(o_region1, params1.elemFieldName);
786
787 define_model(i_region, o_region2);
788 write_model(i_region, o_region2);
789 define_transient(o_region2, params2.elemFieldName);
790
791 auto numSteps = params1.output_steps.size();
792
793 int maxStep1 = 1;
794 int maxStep2 = 1;
795
796 double minTime1 = numSteps > 0 ? params1.output_times[0] : 0.0;
797 double minTime2 = numSteps > 0 ? params2.output_times[0] : 0.0;
798
799 bool doneOutputAfterModification1 = true;
800 bool doneOutputAfterModification2 = true;
801
802 for (size_t i = 0; i < numSteps; i++) {
803 if (params1.modification_steps[i]) {
804 EXPECT_TRUE(params2.modification_steps[i]);
805
806 observer1->set_topology_modification(Ioss::TOPOLOGY_UNKNOWN);
807 maxStep1 = 1;
808 maxStep2 = 1;
809
810 EXPECT_EQ(Ioss::TOPOLOGY_UNKNOWN, observer1->get_topology_modification());
811 EXPECT_EQ(Ioss::TOPOLOGY_UNKNOWN, observer2->get_topology_modification());
812
813 doneOutputAfterModification1 = false;
814 doneOutputAfterModification2 = false;
815 }
816
817 do_output(o_region1, params1, i, minTime1, maxStep1, doneOutputAfterModification1);
818 do_output(o_region2, params2, i, minTime2, maxStep2, doneOutputAfterModification2);
819 }
820 }
821
822 void run_single_file_simple_topology_change_with_multiple_output(const std::string &model,
823 const OutputParams &params1,
824 const OutputParams &params2)
825 {
826 Ioss::Init::Initializer io;
827 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
828
829 auto broker = Ioss::DynamicTopologyBroker::broker();
830 broker->register_model(model);
831
832 int numBlocks = util.parallel_size();
833
834 std::string meshDesc = get_many_block_mesh_desc(numBlocks);
835
836 Ioss::PropertyManager propertyManager;
837
838 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
839 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
840 Ioss::Region i_region(i_database, "input_model");
841 EXPECT_TRUE(i_database != nullptr);
842 EXPECT_TRUE(i_database->ok(true));
843
844 propertyManager.add(Ioss::Property("ENABLE_FILE_GROUPS", 1));
845 propertyManager.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP));
846
847 Ioss::DatabaseIO *o_database1 =
848 Ioss::IOFactory::create("exodus", params1.outFile, Ioss::WRITE_RESULTS,
849 Ioss::ParallelUtils::comm_world(), propertyManager);
850 Ioss::Region o_region1(o_database1, "region1");
851 EXPECT_TRUE(o_database1 != nullptr);
852 EXPECT_TRUE(o_database1->ok(true));
853 EXPECT_TRUE(o_database1->supports_internal_change_set());
854
855 auto fileControlOption = Ioss::FileControlOption::CONTROL_AUTO_GROUP_FILE;
856 auto observer1 = std::make_shared<Observer>(i_region, params1.elemFieldName, fileControlOption);
857 broker->register_observer(model, observer1, o_region1);
858
859 Ioss::DatabaseIO *o_database2 =
860 Ioss::IOFactory::create("exodus", params2.outFile, Ioss::WRITE_RESULTS,
861 Ioss::ParallelUtils::comm_world(), propertyManager);
862 Ioss::Region o_region2(o_database2, "region2");
863 EXPECT_TRUE(o_database2 != nullptr);
864 EXPECT_TRUE(o_database2->ok(true));
865 EXPECT_TRUE(o_database2->supports_internal_change_set());
866
867 auto observer2 = std::make_shared<Observer>(i_region, params2.elemFieldName, fileControlOption);
868 broker->register_observer(model, observer2, o_region2);
869
870 run_topology_change_with_multiple_output(i_region, o_region1, o_region2, params1, params2);
871
872 test_internal_file_change_set_names(o_database1);
873 test_internal_file_change_set_names(o_database2);
874 }
875
876 TEST(TestDynamicWrite, single_file_simple_topology_modification_with_multiple_output)
877 {
878 std::string outFile1("singleFileManyBlocks1.g");
879 std::string outFile2("singleFileManyBlocks2.g");
880 std::string elemFieldName = "elem_field";
881 std::string model = "multiple-output";
882
883 OutputParams params1(outFile1, elemFieldName);
884
885 params1.add(0.0, true, false)
886 .add(1.0, true, true)
887 .add(2.0, false, false)
888 .add(3.0, true, true)
889 .add(4.0, true, false)
890 .add(5.0, true, true);
891
892 OutputParams params2(outFile2, elemFieldName);
893
894 params2.add(0.0, true, false)
895 .add(1.0, true, true)
896 .add(2.0, true, false)
897 .add(3.0, false, true)
898 .add(4.0, true, false)
899 .add(5.0, true, true);
900
901 cleanup_single_file(outFile1);
902 cleanup_single_file(outFile2);
903 run_single_file_simple_topology_change_with_multiple_output(model, params1, params2);
904 cleanup_single_file(outFile1);
905 cleanup_single_file(outFile2);
906 }
907
908 TEST(TestDynamicWrite, same_model_triggers_same_modification_for_all_observers)
909 {
910 Ioss::Init::Initializer io;
911 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
912
913 std::string outFile1("sameModelManyBlocks1.g");
914 std::string outFile2("sameModelManyBlocks2.g");
915 std::string elemFieldName("elem_field");
916 std::string model("same-model");
917
918 auto broker = Ioss::DynamicTopologyBroker::broker();
919 broker->register_model(model);
920
921 std::string file1 =
922 Ioss::Utils::decode_filename(outFile1, util.parallel_rank(), util.parallel_size());
923 std::string file2 =
924 Ioss::Utils::decode_filename(outFile2, util.parallel_rank(), util.parallel_size());
925
926 unlink(file1.c_str());
927 unlink(file2.c_str());
928
929 int numBlocks = util.parallel_size();
930 if (numBlocks > 1)
931 GTEST_SKIP();
932
933 std::string meshDesc = get_many_block_mesh_desc(numBlocks);
934
935 Ioss::PropertyManager propertyManager;
936
937 Ioss::DatabaseIO *i_database = Ioss::IOFactory::create(
938 "textmesh", meshDesc, Ioss::READ_MODEL, Ioss::ParallelUtils::comm_world(), propertyManager);
939 Ioss::Region i_region(i_database, "input_model");
940 EXPECT_TRUE(i_database != nullptr);
941 EXPECT_TRUE(i_database->ok(true));
942
943 {
944 propertyManager.add(Ioss::Property("ENABLE_FILE_GROUPS", 1));
945 propertyManager.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP));
946
947 Ioss::DatabaseIO *o_database1 =
948 Ioss::IOFactory::create("exodus", outFile1, Ioss::WRITE_RESULTS,
949 Ioss::ParallelUtils::comm_world(), propertyManager);
950 Ioss::Region o_region1(o_database1, "region1");
951 EXPECT_TRUE(o_database1 != nullptr);
952 EXPECT_TRUE(o_database1->ok(true));
953 EXPECT_TRUE(o_database1->supports_internal_change_set());
954
955 auto fileControlOption = Ioss::FileControlOption::CONTROL_AUTO_GROUP_FILE;
956 auto observer1 = std::make_shared<Observer>(i_region, elemFieldName, fileControlOption);
957 broker->register_observer(model, observer1, o_region1);
958
959 Ioss::DatabaseIO *o_database2 =
960 Ioss::IOFactory::create("exodus", outFile2, Ioss::WRITE_RESULTS,
961 Ioss::ParallelUtils::comm_world(), propertyManager);
962 Ioss::Region o_region2(o_database2, "region2");
963 EXPECT_TRUE(o_database2 != nullptr);
964 EXPECT_TRUE(o_database2->ok(true));
965 EXPECT_TRUE(o_database2->supports_internal_change_set());
966
967 auto observer2 = std::make_shared<Observer>(i_region, elemFieldName, fileControlOption);
968 broker->register_observer(model, observer2, o_region2);
969
970 EXPECT_EQ(Ioss::TOPOLOGY_SAME, observer1->get_topology_modification());
971 EXPECT_EQ(Ioss::TOPOLOGY_SAME, observer2->get_topology_modification());
972
973 observer1->set_topology_modification(Ioss::TOPOLOGY_UNKNOWN);
974
975 EXPECT_EQ(Ioss::TOPOLOGY_UNKNOWN, observer1->get_topology_modification());
976 EXPECT_EQ(Ioss::TOPOLOGY_UNKNOWN, observer2->get_topology_modification());
977 }
978
979 unlink(file1.c_str());
980 unlink(file2.c_str());
981 }
982
983 void test_single_file_simple_topology_change_data(Ioss::Region &i_region,
984 const std::string &elemFieldName, int gold_step,
985 double gold_time)
986 {
987 i_region.begin_state(gold_step);
988 for (Ioss::ElementBlock *i_eb : i_region.get_element_blocks()) {
989 size_t num_elem = i_eb->get_property("entity_count").get_int();
990
991 std::vector<double> field_data(num_elem);
992 std::vector<int> elem_ids;
993
994 i_eb->get_field_data(elemFieldName, field_data);
995 i_eb->get_field_data("ids", elem_ids);
996
997 for (size_t i = 0; i < elem_ids.size(); i++) {
998 double gold_value = (double)elem_ids[i] + 100 * gold_time;
999 EXPECT_NEAR(gold_value, field_data[i], 1.0e-6);
1000 }
1001 }
1002 }
1003
1004 void read_and_test_single_file_simple_topology_change(const OutputParams &params)
1005 {
1006 Ioss::PropertyManager propertyManager;
1007 auto i_region = construct_region(params, propertyManager, Ioss::READ_RESTART, "input_model");
1008 Ioss::DatabaseIO *i_database = i_region->get_database();
1009
1010 test_internal_file_change_set_names(i_database);
1011
1012 EXPECT_TRUE(i_database->supports_internal_change_set());
1013
1014 auto numSteps = params.output_steps.size();
1015
1016 int numMods = 0;
1017
1018 int maxStep = 1;
1019
1020 double minTime = numSteps > 0 ? params.output_times[0] : 0.0;
1021 double maxTime = numSteps > 0 ? params.output_times[0] : 0.0;
1022
1023 bool doneOutputAfterModification = true;
1024
1025 Ioss::NameList names = i_database->internal_change_set_describe(false);
1026
1027 for (size_t i = 0; i < numSteps; i++) {
1028 maxTime = params.output_times[i];
1029
1030 for (size_t j = i + 1; j < numSteps; j++) {
1031 if (params.modification_steps[j]) {
1032 maxTime = params.output_times[j - 1];
1033 break;
1034 }
1035
1036 maxTime = params.output_times[j];
1037 }
1038
1039 if (params.modification_steps[i]) {
1040 numMods++;
1041 maxStep = 1;
1042
1043 EXPECT_TRUE(i_region->load_internal_change_set_mesh(names[numMods]));
1044
1045 doneOutputAfterModification = false;
1046 }
1047
1048 if (params.output_steps[i]) {
1049 if (!doneOutputAfterModification) {
1050 minTime = params.output_times[i];
1051 }
1052 auto min_result = i_region->get_min_time();
1053 EXPECT_EQ(1, min_result.first);
1054 EXPECT_NEAR(minTime, min_result.second, 1.0e-6);
1055 test_single_file_simple_topology_change_data(*i_region, params.elemFieldName, 1, minTime);
1056
1057 if ((((i + 1) < numSteps) && params.modification_steps[i + 1]) || (i == (numSteps - 1))) {
1058 auto max_result = i_region->get_max_time();
1059 EXPECT_EQ(maxStep, max_result.first);
1060 EXPECT_NEAR(maxTime, max_result.second, 1.0e-6);
1061
1062 test_single_file_simple_topology_change_data(*i_region, params.elemFieldName, maxStep,
1063 maxTime);
1064 }
1065
1066 maxStep++;
1067 doneOutputAfterModification = true;
1068 }
1069 }
1070 }
1071
1072 TEST(TestDynamicRead, single_file_simple_topology_modification)
1073 {
1074 std::string outFile("singleFileManyBlocks.g");
1075 std::string elemFieldName = "elem_field";
1076
1077 OutputParams params(outFile, elemFieldName);
1078
1079 params.add(0.0, true, false)
1080 .add(1.0, true, true)
1081 .add(2.0, true, false)
1082 .add(3.0, true, true)
1083 .add(4.0, true, true)
1084 .add(5.0, true, false);
1085
1086 cleanup_single_file(outFile);
1087 run_single_file_simple_topology_change(params);
1088 read_and_test_single_file_simple_topology_change(params);
1089 cleanup_single_file(outFile);
1090 }
1091
1092 std::tuple<std::string, int, double> read_and_locate_db_state(const OutputParams &params,
1093 double targetTime)
1094 {
1095 Ioss::PropertyManager propertyManager;
1096 auto region = construct_region(params, propertyManager, Ioss::READ_RESTART, "input_model");
1097 return region->locate_db_state(targetTime);
1098 }
1099
1100 void run_single_file_locate_db_time_state(const std::string &outFile, const OutputParams &params,
1101 double targetTime, const std::string &goldSet,
1102 int goldState, double goldTime)
1103 {
1104 cleanup_single_file(outFile);
1105 run_single_file_simple_topology_change(params);
1106
1107 std::string changeSet;
1108 int nearestState;
1109 double nearestTime;
1110
1111 std::tie(changeSet, nearestState, nearestTime) = read_and_locate_db_state(params, targetTime);
1112
1113 EXPECT_EQ(goldSet, changeSet);
1114 EXPECT_EQ(goldState, nearestState);
1115 EXPECT_EQ(goldTime, nearestTime);
1116
1117 cleanup_single_file(outFile);
1118 }
1119
1120 TEST(TestDynamicRead, single_file_locate_db_time_state)
1121 {
1122 std::string outFile("singleFileManyBlocksPositiveTime.g");
1123 std::string elemFieldName = "elem_field";
1124
1125 OutputParams params(outFile, elemFieldName);
1126
1127 params.add(0.0, true, false)
1128 .add(1.0, true, true)
1129 .add(2.0, true, false)
1130 .add(3.0, true, true)
1131 .add(4.0, true, true)
1132 .add(5.0, true, false);
1133
1134 double targetTime = 3.5;
1135
1136 std::string goldSet = Ioss::DynamicTopologyFileControl::get_internal_file_change_set_name(3);
1137 int goldState = 1;
1138 double goldTime = 3.0;
1139
1140 run_single_file_locate_db_time_state(outFile, params, targetTime, goldSet, goldState, goldTime);
1141 }
1142
1143 TEST(TestDynamicRead, single_file_locate_db_time_state_all_negative_time)
1144 {
1145 std::string outFile("singleFileManyBlocksNegativeTime.g");
1146 std::string elemFieldName = "elem_field";
1147
1148 OutputParams params(outFile, elemFieldName);
1149
1150 params.add(-5.0, true, false)
1151 .add(-4.0, true, true)
1152 .add(-3.0, true, false)
1153 .add(-2.0, true, true)
1154 .add(-1.0, true, true)
1155 .add(-0.0, true, false);
1156
1157 double targetTime = -1.5;
1158
1159 std::string goldSet = Ioss::DynamicTopologyFileControl::get_internal_file_change_set_name(4);
1160 int goldState = 1;
1161 double goldTime = -1.0;
1162
1163 run_single_file_locate_db_time_state(outFile, params, targetTime, goldSet, goldState, goldTime);
1164 }
1165
1166 void run_multi_file_locate_db_time_state(const std::string &outFile, const OutputParams &params,
1167 double targetTime, const std::string &goldFile,
1168 int goldState, double goldTime)
1169 {
1170 if (params.cyclicCount > 0) {
1171 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1172 }
1173 else {
1174 cleanup_linear_multi_files(outFile, params.output_times.size());
1175 }
1176
1177 run_multi_file_topology_change(params);
1178
1179 std::string file;
1180 int nearestState;
1181 double nearestTime;
1182
1183 std::tie(file, nearestState, nearestTime) = read_and_locate_db_state(params, targetTime);
1184
1185 EXPECT_EQ(goldFile, file);
1186 EXPECT_EQ(goldState, nearestState);
1187 EXPECT_EQ(goldTime, nearestTime);
1188
1189 if (params.cyclicCount > 0) {
1190 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1191 }
1192 else {
1193 cleanup_linear_multi_files(outFile, params.output_times.size());
1194 }
1195 }
1196
1197 TEST(TestDynamicRead, linear_multi_file_locate_db_time_state)
1198 {
1199 std::string outFile("linearMultiFileManyBlocksPositiveTime.g");
1200 std::string elemFieldName = "elem_field";
1201
1202 OutputParams params(outFile, elemFieldName);
1203
1204 params.add(0.0, true, false)
1205 .add(1.0, true, true)
1206 .add(2.0, true, false)
1207 .add(3.0, true, true)
1208 .add(4.0, true, true)
1209 .add(5.0, true, false);
1210
1211 double targetTime = 3.5;
1212
1213 std::string goldFile =
1214 Ioss::DynamicTopologyFileControl::get_linear_database_filename(outFile, 3);
1215 int goldState = 1;
1216 double goldTime = 3.0;
1217
1218 run_multi_file_locate_db_time_state(outFile, params, targetTime, goldFile, goldState, goldTime);
1219 }
1220
1221 TEST(TestDynamicRead, cyclic_multi_file_locate_db_time_state)
1222 {
1223 std::string outFile("cyclicMultiFileManyBlocksPositiveTime.g");
1224 std::string elemFieldName = "elem_field";
1225
1226 OutputParams params(outFile, elemFieldName);
1227
1228 std::vector<double> output_times{0.0, 0.5, 1.5, 1.75, 2.0, 3.0};
1229 std::vector<bool> output_steps{true, true, true, true, true, true};
1230 std::vector<bool> modification_steps{false, true, false, true, true, false};
1231 // -A -B -B -C -B -B
1232
1233 params.set_data(output_times, output_steps, modification_steps);
1234 params.set_cyclic_count(3);
1235
1236 double targetTime = 1.95;
1237
1238 std::string goldFile = Ioss::DynamicTopologyFileControl::get_cyclic_database_filename(
1239 outFile, params.cyclicCount, 2);
1240 int goldState = 1;
1241 double goldTime = 2.0;
1242
1243 run_multi_file_locate_db_time_state(outFile, params, targetTime, goldFile, goldState, goldTime);
1244 }
1245
1246 std::tuple<std::string, int, double> read_and_locate_db_max_time(const OutputParams &params)
1247 {
1248 Ioss::PropertyManager propertyManager;
1249
1250 std::string outFile = params.outFile;
1251
1252 if (params.cyclicCount > 0) {
1253 outFile = Ioss::DynamicTopologyFileControl::get_cyclic_database_filename(
1254 params.outFile, params.cyclicCount, 0);
1255 propertyManager.add(Ioss::Property("base_filename", params.outFile));
1256 }
1257
1258 Ioss::DatabaseIO *db = Ioss::IOFactory::create(
1259 "exodus", outFile, Ioss::READ_RESTART, Ioss::ParallelUtils::comm_world(), propertyManager);
1260
1261 Ioss::Region region(db, "input_model");
1262 region.set_file_cyclic_count(params.cyclicCount);
1263
1264 EXPECT_TRUE(db != nullptr);
1265 EXPECT_TRUE(db->ok(true));
1266
1267 return region.get_db_max_time();
1268 }
1269
1270 TEST(TestDynamicRead, single_file_locate_db_max_time)
1271 {
1272 std::string outFile("singleFileManyBlocksMaxTime.g");
1273 std::string elemFieldName = "elem_field";
1274
1275 OutputParams params(outFile, elemFieldName);
1276
1277 params.add(0.0, true, false)
1278 .add(1.0, true, true)
1279 .add(2.0, true, false)
1280 .add(3.0, true, true)
1281 .add(4.0, true, true)
1282 .add(5.0, true, false);
1283
1284 cleanup_single_file(outFile);
1285 run_single_file_simple_topology_change(params);
1286
1287 std::string maxSet;
1288 int maxState;
1289 double maxTime;
1290
1291 std::tie(maxSet, maxState, maxTime) = read_and_locate_db_max_time(params);
1292
1293 std::string goldSet = Ioss::DynamicTopologyFileControl::get_internal_file_change_set_name(4);
1294 int goldState = 2;
1295 double goldTime = 5.0;
1296
1297 EXPECT_EQ(goldSet, maxSet);
1298 EXPECT_EQ(goldState, maxState);
1299 EXPECT_EQ(goldTime, maxTime);
1300
1301 cleanup_single_file(outFile);
1302 }
1303
1304 TEST(TestDynamicRead, linear_multi_file_locate_db_max_time)
1305 {
1306 std::string outFile("linearMultiFileManyBlocksMaxTime.g");
1307 std::string elemFieldName = "elem_field";
1308
1309 OutputParams params(outFile, elemFieldName);
1310
1311 params.add(0.0, true, false)
1312 .add(1.0, true, true)
1313 .add(2.0, true, false)
1314 .add(3.0, true, true)
1315 .add(4.0, true, true)
1316 .add(5.0, true, false);
1317
1318 cleanup_linear_multi_files(outFile, params.output_times.size());
1319 run_multi_file_topology_change(params);
1320
1321 std::string maxFile;
1322 int maxState;
1323 double maxTime;
1324
1325 std::tie(maxFile, maxState, maxTime) = read_and_locate_db_max_time(params);
1326
1327 std::string goldFile =
1328 Ioss::DynamicTopologyFileControl::get_linear_database_filename(outFile, 4);
1329 int goldState = 2;
1330 double goldTime = 5.0;
1331
1332 EXPECT_EQ(goldFile, maxFile);
1333 EXPECT_EQ(goldState, maxState);
1334 EXPECT_EQ(goldTime, maxTime);
1335
1336 cleanup_linear_multi_files(outFile, params.output_times.size());
1337 }
1338
1339 TEST(TestDynamicRead, cyclic_multi_file_locate_db_max_time)
1340 {
1341 std::string outFile("cyclicMultiFileManyBlocksMaxTime.g");
1342 std::string elemFieldName = "elem_field";
1343
1344 OutputParams params(outFile, elemFieldName);
1345
1346 std::vector<double> output_times{0.0, 0.5, 1.5, 1.75, 2.0, 3.0};
1347 std::vector<bool> output_steps{true, true, true, true, true, true};
1348 std::vector<bool> modification_steps{false, true, false, true, true, false};
1349 // -A -B -B -C -B -B
1350
1351 params.set_data(output_times, output_steps, modification_steps);
1352 params.set_cyclic_count(3);
1353
1354 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1355 run_multi_file_topology_change(params);
1356
1357 std::string maxFile;
1358 int maxState;
1359 double maxTime;
1360
1361 std::tie(maxFile, maxState, maxTime) = read_and_locate_db_max_time(params);
1362
1363 std::string goldFile = Ioss::DynamicTopologyFileControl::get_cyclic_database_filename(
1364 outFile, params.cyclicCount, 2); // -B
1365 int goldState = 2;
1366 double goldTime = 3.0;
1367
1368 EXPECT_EQ(goldFile, maxFile);
1369 EXPECT_EQ(goldState, maxState);
1370 EXPECT_EQ(goldTime, maxTime);
1371
1372 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1373 }
1374
1375 std::tuple<std::string, int, double> read_and_locate_db_min_time(const OutputParams &params)
1376 {
1377 Ioss::PropertyManager propertyManager;
1378 auto region = construct_region(params, propertyManager, Ioss::READ_RESTART, "input_model");
1379
1380 return region->get_db_min_time();
1381 }
1382
1383 TEST(TestDynamicRead, single_file_locate_db_min_time)
1384 {
1385 std::string outFile("singleFileManyBlocksMinTime.g");
1386 std::string elemFieldName = "elem_field";
1387
1388 OutputParams params(outFile, elemFieldName);
1389
1390 params.add(0.0, true, false)
1391 .add(1.0, true, true)
1392 .add(2.0, true, false)
1393 .add(3.0, true, true)
1394 .add(4.0, true, true)
1395 .add(5.0, true, false);
1396
1397 cleanup_single_file(outFile);
1398 run_single_file_simple_topology_change(params);
1399
1400 std::string minSet;
1401 int minState;
1402 double minTime;
1403
1404 std::tie(minSet, minState, minTime) = read_and_locate_db_min_time(params);
1405
1406 std::string goldSet = Ioss::DynamicTopologyFileControl::get_internal_file_change_set_name(1);
1407 int goldState = 1;
1408 double goldTime = 0.0;
1409
1410 EXPECT_EQ(goldSet, minSet);
1411 EXPECT_EQ(goldState, minState);
1412 EXPECT_EQ(goldTime, minTime);
1413
1414 cleanup_single_file(outFile);
1415 }
1416
1417 TEST(TestDynamicRead, linear_multi_file_locate_db_min_time)
1418 {
1419 std::string outFile("linearMultiFileManyBlocksMinTime.g");
1420 std::string elemFieldName = "elem_field";
1421
1422 OutputParams params(outFile, elemFieldName);
1423
1424 params.add(0.0, true, false)
1425 .add(1.0, true, true)
1426 .add(2.0, true, false)
1427 .add(3.0, true, true)
1428 .add(4.0, true, true)
1429 .add(5.0, true, false);
1430
1431 cleanup_linear_multi_files(outFile, params.output_times.size());
1432 run_multi_file_topology_change(params);
1433
1434 std::string minFile;
1435 int minState;
1436 double minTime;
1437
1438 std::tie(minFile, minState, minTime) = read_and_locate_db_min_time(params);
1439
1440 std::string goldFile =
1441 Ioss::DynamicTopologyFileControl::get_linear_database_filename(outFile, 1);
1442 int goldState = 1;
1443 double goldTime = 0.0;
1444
1445 EXPECT_EQ(goldFile, minFile);
1446 EXPECT_EQ(goldState, minState);
1447 EXPECT_EQ(goldTime, minTime);
1448
1449 cleanup_linear_multi_files(outFile, params.output_times.size());
1450 }
1451
1452 TEST(TestDynamicRead, cyclic_multi_file_locate_db_min_time)
1453 {
1454 std::string outFile("cyclicMultiFileManyBlocksMinTime.g");
1455 std::string elemFieldName = "elem_field";
1456
1457 OutputParams params(outFile, elemFieldName);
1458
1459 std::vector<double> output_times{0.0, 0.5, 1.5, 1.75, 2.0, 3.0};
1460 std::vector<bool> output_steps{true, true, true, true, true, true};
1461 std::vector<bool> modification_steps{false, true, false, true, true, false};
1462 // -A -B -B -C -B -B
1463
1464 params.set_data(output_times, output_steps, modification_steps);
1465 params.set_cyclic_count(3);
1466
1467 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1468 run_multi_file_topology_change(params);
1469
1470 std::string minFile;
1471 int minState;
1472 double minTime;
1473
1474 std::tie(minFile, minState, minTime) = read_and_locate_db_min_time(params);
1475
1476 std::string goldFile = Ioss::DynamicTopologyFileControl::get_cyclic_database_filename(
1477 outFile, params.cyclicCount, 1);
1478 int goldState = 1;
1479 double goldTime = 0.0;
1480
1481 EXPECT_EQ(goldFile, minFile);
1482 EXPECT_EQ(goldState, minState);
1483 EXPECT_EQ(goldTime, minTime);
1484
1485 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1486 }
1487
1488 unsigned get_num_change_sets(const OutputParams &params)
1489 {
1490 auto numSteps = params.output_steps.size();
1491
1492 unsigned numMods = 0;
1493 int numModInc = 0;
1494
1495 int currentModStep = -1;
1496
1497 for (size_t i = 0; i < numSteps; i++) {
1498 if (params.modification_steps[i]) {
1499 currentModStep = i;
1500 numMods++;
1501 }
1502
1503 if (params.output_steps[i] && (currentModStep < 0)) {
1504 numModInc = 1;
1505 }
1506 }
1507
1508 numMods += numModInc;
1509
1510 return numMods;
1511 }
1512
1513 void read_and_test_single_file_topology_change_set(const OutputParams &params)
1514 {
1515 Ioss::PropertyManager propertyManager;
1516 auto i_region = construct_region(params, propertyManager, Ioss::READ_RESTART, "input_model");
1517
1518 auto changeSet = Ioss::ChangeSetFactory::create(i_region.get());
1519 changeSet->populate_change_sets();
1520
1521 EXPECT_EQ(Ioss::CHANGE_SET_INTERNAL_FILES, changeSet->database_format());
1522
1523 std::vector<std::string> gold_names;
1524 std::vector<std::string> gold_full_names;
1525
1526 unsigned numMods = get_num_change_sets(params);
1527
1528 fill_internal_file_change_set_gold_names(numMods, gold_names, gold_full_names);
1529
1530 EXPECT_EQ(gold_names, changeSet->names());
1531 }
1532
1533 TEST(TestChangeSet, single_file_simple_topology_modification)
1534 {
1535 std::string outFile("singleFileManyBlocksChangeSet.g");
1536 std::string elemFieldName = "elem_field";
1537
1538 OutputParams params(outFile, elemFieldName);
1539
1540 params.add(0.0, true, false)
1541 .add(1.0, true, true)
1542 .add(2.0, true, false)
1543 .add(3.0, true, true)
1544 .add(4.0, true, true)
1545 .add(5.0, true, false);
1546
1547 cleanup_single_file(outFile);
1548 run_single_file_simple_topology_change(params);
1549 read_and_test_single_file_topology_change_set(params);
1550 cleanup_single_file(outFile);
1551 }
1552
1553 void read_and_test_cyclic_multi_file_topology_change_set(const OutputParams &params)
1554 {
1555 ASSERT_TRUE(params.cyclicCount > 0);
1556
1557 Ioss::PropertyManager propertyManager;
1558 auto i_region = construct_region(params, propertyManager, Ioss::READ_RESTART, "input_model");
1559
1560 auto changeSet = Ioss::ChangeSetFactory::create(i_region.get());
1561 changeSet->populate_change_sets();
1562
1563 EXPECT_EQ(Ioss::CHANGE_SET_CYCLIC_MULTI_FILES, changeSet->database_format());
1564
1565 std::vector<std::string> gold_names;
1566
1567 unsigned numMods = get_num_change_sets(params);
1568 if (numMods > params.cyclicCount) {
1569 numMods = params.cyclicCount;
1570 }
1571
1572 for (unsigned i = 1; i <= numMods; i++) {
1573 gold_names.push_back(Ioss::DynamicTopologyFileControl::get_cyclic_database_filename(
1574 params.outFile, params.cyclicCount, i));
1575 }
1576
1577 EXPECT_EQ(gold_names, changeSet->names());
1578 }
1579
1580 TEST(TestChangeSet, multi_file_cyclic_topology_modification)
1581 {
1582 std::string outFile("cyclicMultiFileManyBlocksChangeSet.g");
1583 std::string elemFieldName = "elem_field";
1584
1585 OutputParams params(outFile, elemFieldName);
1586
1587 std::vector<double> output_times{0.0, 0.5, 1.5, 1.75, 2.0, 3.0};
1588 std::vector<bool> output_steps{true, true, true, true, true, true};
1589 std::vector<bool> modification_steps{false, true, false, true, true, false};
1590
1591 params.set_data(output_times, output_steps, modification_steps);
1592 params.set_cyclic_count(3);
1593
1594 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1595 run_multi_file_topology_change(params);
1596 read_and_test_cyclic_multi_file_topology_change_set(params);
1597 cleanup_cyclic_multi_files(outFile, params.cyclicCount);
1598 }
1599
1600 void read_and_test_linear_multi_file_topology_change_set(const OutputParams &params)
1601 {
1602 ASSERT_TRUE(params.cyclicCount == 0);
1603
1604 Ioss::PropertyManager propertyManager;
1605 auto i_region = construct_region(params, propertyManager, Ioss::READ_RESTART, "input_model");
1606
1607 auto changeSet = Ioss::ChangeSetFactory::create(i_region.get());
1608 changeSet->populate_change_sets();
1609
1610 EXPECT_EQ(Ioss::CHANGE_SET_LINEAR_MULTI_FILES, changeSet->database_format());
1611
1612 std::vector<std::string> gold_names;
1613
1614 unsigned numMods = get_num_change_sets(params);
1615
1616 for (unsigned i = 1; i <= numMods; i++) {
1617 gold_names.push_back(
1618 Ioss::DynamicTopologyFileControl::get_linear_database_filename(params.outFile, i));
1619 }
1620
1621 EXPECT_EQ(gold_names, changeSet->names());
1622 }
1623
1624 TEST(TestChangeSet, multi_file_linear_topology_modification)
1625 {
1626 std::string outFile("linearMultiFileManyBlocksChangeSet.g");
1627 std::string elemFieldName = "elem_field";
1628
1629 OutputParams params(outFile, elemFieldName);
1630
1631 std::vector<double> output_times{0.0, 0.5, 1.5, 1.75, 2.0, 3.0};
1632 std::vector<bool> output_steps{true, true, true, true, true, true};
1633 std::vector<bool> modification_steps{false, true, false, true, true, false};
1634
1635 params.set_data(output_times, output_steps, modification_steps);
1636
1637 cleanup_linear_multi_files(outFile, params.output_times.size());
1638 run_multi_file_topology_change(params);
1639 read_and_test_linear_multi_file_topology_change_set(params);
1640 cleanup_linear_multi_files(outFile, params.output_times.size());
1641 }
1642 } // namespace