Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_ChangeSet.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 "Ioss_ChangeSet.h"
8
9 #include "Ioss_CodeTypes.h"
10 #include "Ioss_DBUsage.h"
11
12 #include "Ioss_DynamicTopology.h"
13 #include "Ioss_EntityBlock.h"
14 #include "Ioss_EntityType.h"
15 #include "Ioss_Field.h"
16 #include "Ioss_FileInfo.h"
17 #include "Ioss_GroupingEntity.h"
18 #include "Ioss_IOFactory.h"
19
20 #include <fmt/core.h>
21 #include <fmt/format.h>
22 #include <fmt/ostream.h>
23
24 #include <assert.h>
25 #include <iomanip>
26
27 namespace {
28 int file_exists(const Ioss::ParallelUtils &util, const std::string &filename,
29 std::string &message, bool filePerRank)
30 {
31 std::string filenameBase = filename;
32 const int par_size = util.parallel_size();
33 const int par_rank = util.parallel_rank();
34
35 if (par_size > 1 && !filePerRank) {
36 filenameBase = Ioss::Utils::decode_filename(filenameBase, par_rank, par_size);
37 }
38
39 Ioss::FileInfo file = Ioss::FileInfo(filenameBase);
40 return file.parallel_exists(util.communicator(), message);
41 }
42
43 std::string get_decomposition_property(const Ioss::PropertyManager &properties,
44 Ioss::DatabaseUsage db_usage)
45 {
46 std::string decomp_method;
47 std::string decomp_property;
48 if (db_usage == Ioss::READ_MODEL) {
49 decomp_property = "MODEL_DECOMPOSITION_METHOD";
50 }
51 else if (db_usage == Ioss::READ_RESTART || db_usage == Ioss::QUERY_TIMESTEPS_ONLY) {
52 decomp_property = "RESTART_DECOMPOSITION_METHOD";
53 }
54
55 // Applies to either read_model or read_restart
56 if (properties.exists("DECOMPOSITION_METHOD")) {
57 std::string method = properties.get("DECOMPOSITION_METHOD").get_string();
58 return Ioss::Utils::uppercase(method);
59 }
60
61 // Check for property...
62 if (properties.exists(decomp_property)) {
63 std::string method = properties.get(decomp_property).get_string();
64 return Ioss::Utils::uppercase(method);
65 }
66 return decomp_method;
67 }
68
69 bool internal_decomp_specified(const Ioss::PropertyManager &props, Ioss::DatabaseUsage usage)
70 {
71 bool internalDecompSpecified = false;
72
73 std::string method = get_decomposition_property(props, usage);
74 if (!method.empty() && method != "EXTERNAL") {
75 internalDecompSpecified = true;
76 }
77
78 return internalDecompSpecified;
79 }
80
81 } // namespace
82
83 namespace Ioss {
84
85 ChangeSet::ChangeSet(Ioss::Region *region)
86 : m_database(region->get_database()),
87 m_ioDB(region->get_property("base_filename").get_string()),
88 m_dbType(region->get_property("database_type").get_string()),
89 m_fileCyclicCount(region->get_file_cyclic_count())
90 {
91 }
92
93 ChangeSet::ChangeSet(Ioss::DatabaseIO *db, const std::string &dbName, const std::string &dbType,
94 unsigned fileCyclicCount)
95 : m_database(db), m_ioDB(dbName), m_dbType(dbType), m_fileCyclicCount(fileCyclicCount)
96 {
97 }
98
99 ChangeSet::~ChangeSet() { clear_change_sets(); }
100
101 DatabaseIO *ChangeSet::get_database() const { return m_database; }
102
103 const ParallelUtils &ChangeSet::util() const { return get_database()->util(); }
104
105 void ChangeSet::get_cyclic_multi_file_change_sets()
106 {
107 auto db = get_database();
108
109 m_databaseFormat = CHANGE_SET_CYCLIC_MULTI_FILES;
110
111 Ioss::FileNameGenerator generator =
112 Ioss::construct_cyclic_filename_generator(m_fileCyclicCount);
113
114 bool found = true;
115 int step = 0;
116 int fileCyclicCount = m_fileCyclicCount;
117
118 while (found && (step < fileCyclicCount)) {
119 ++step;
120
121 std::string expanded = Ioss::expand_topology_files(
122 generator, util(), m_ioDB, db->get_property_manager(), db->usage(), step);
123 if (!expanded.empty()) {
124 m_changeSetNames.push_back(expanded);
125 }
126 else {
127 found = false;
128 }
129 }
130
131 m_changeSetDatabases.resize(m_changeSetNames.size(), nullptr);
132 }
133
134 void ChangeSet::get_linear_multi_file_change_sets()
135 {
136 auto db = get_database();
137
138 m_databaseFormat = CHANGE_SET_LINEAR_MULTI_FILES;
139
140 Ioss::FileNameGenerator generator = Ioss::construct_linear_filename_generator();
141
142 bool found = true;
143 int step = 0;
144
145 while (found) {
146 ++step;
147
148 std::string expanded = Ioss::expand_topology_files(
149 generator, util(), m_ioDB, db->get_property_manager(), db->usage(), step);
150 if (!expanded.empty()) {
151 m_changeSetNames.push_back(expanded);
152 }
153 else {
154 found = false;
155 }
156 }
157
158 m_changeSetDatabases.resize(m_changeSetNames.size(), nullptr);
159 }
160
161 void ChangeSet::populate_change_sets(bool loadAllFiles)
162 {
163 clear_change_sets();
164
165 if (!loadAllFiles) {
166 // Load only the current db file
167 m_databaseFormat = CHANGE_SET_LINEAR_MULTI_FILES;
168 m_changeSetNames.push_back(get_database()->get_filename());
169 m_changeSetDatabases.resize(m_changeSetNames.size(), nullptr);
170 return;
171 }
172
173 if (get_file_cyclic_count() > 0) {
174 get_cyclic_multi_file_change_sets();
175 }
176 else {
177 get_linear_multi_file_change_sets();
178 }
179 }
180
181 void ChangeSet::verify_change_set_index(unsigned index) const
182 {
183 if (index >= m_changeSetNames.size()) {
184 IOSS_ERROR(fmt::format("Invalid change set index {} with a max value of {}\n", index,
185 m_changeSetNames.size() - 1));
186 }
187 }
188
189 std::string ChangeSet::get_change_set_name(unsigned index) const
190 {
191 verify_change_set_index(index);
192
193 return m_changeSetNames[index];
194 }
195
196 void ChangeSet::close_change_set(unsigned index)
197 {
198 verify_change_set_index(index);
199
200 auto db = m_changeSetDatabases[index];
201 if (nullptr != db) {
202 db->closeDatabase();
203 delete db;
204 m_changeSetDatabases[index] = nullptr;
205 }
206 }
207
208 Ioss::DatabaseIO *ChangeSet::open_change_set(unsigned index, Ioss::DatabaseUsage usage)
209 {
210 verify_change_set_index(index);
211
212 Ioss::DatabaseIO *db = nullptr;
213
214 auto csdb = m_changeSetDatabases[index];
215 if (nullptr != csdb) {
216 if (csdb->usage() == usage) {
217 return csdb;
218 }
219 else {
220 csdb->closeDatabase();
221 delete csdb;
222 m_changeSetDatabases[index] = nullptr;
223 }
224 }
225
226 // open db
227 const std::string &ioDB = m_changeSetNames[index];
228 const std::string dbType = m_dbType;
229
230 db = Ioss::IOFactory::create(dbType, ioDB, usage, util().communicator(),
231 get_database()->get_property_manager());
232 std::string error_message;
233 bool is_valid_file = db != nullptr && db->ok(false, &error_message, nullptr);
234 if (!is_valid_file) {
235 delete db;
236 std::ostringstream errmsg;
237 errmsg << error_message;
238 errmsg << __FILE__ << ", " << __FUNCTION__ << ", filename " << ioDB
239 << " is not a valid file\n";
240 IOSS_ERROR(errmsg);
241 }
242
243 m_changeSetDatabases[index] = db;
244
245 return db;
246 }
247
248 void ChangeSet::clear_change_sets()
249 {
250 m_changeSetNames.clear();
251
252 for (size_t i = 0; i < m_changeSetDatabases.size(); i++) {
253 auto db = m_changeSetDatabases[i];
254 if (nullptr != db) {
255 db->closeDatabase();
256 delete db;
257 m_changeSetDatabases[i] = nullptr;
258 }
259 }
260
261 m_changeSetDatabases.clear();
262 }
263
264 std::string ChangeSet::get_cyclic_database_filename(const std::string &baseFileName,
265 unsigned int fileCyclicCount,
266 unsigned int step)
267 {
268 Ioss::FileNameGenerator generator = Ioss::construct_cyclic_filename_generator(fileCyclicCount);
269 return generator(baseFileName, step);
270 }
271
272 std::string ChangeSet::get_linear_database_filename(const std::string &baseFileName,
273 unsigned int step)
274 {
275 Ioss::FileNameGenerator generator = Ioss::construct_linear_filename_generator();
276 return generator(baseFileName, step);
277 }
278
279 std::string expand_topology_files(FileNameGenerator generator, const Ioss::ParallelUtils &util,
280 const std::string &basename,
281 const Ioss::PropertyManager &properties,
282 Ioss::DatabaseUsage usage, int step)
283 {
284 // See if there are multiple topology files
285
286 // If the file exists on all processors, return the filename.
287 // If the file does not exist on any processors, return "";
288 // If the file exists on some, but not all, throw an exception.
289
290 std::string filename = generator(basename, step);
291
292 bool internalDecompSpecified = internal_decomp_specified(properties, usage);
293 std::string message;
294 int exists = ::file_exists(util, filename, message, internalDecompSpecified);
295
296 int par_size = util.parallel_size();
297 int par_rank = util.parallel_rank();
298
299 if (exists > 0 && exists < par_size) {
300 std::ostringstream errmsg;
301 errmsg << "ERROR: Unable to open input database";
302 if (par_rank == 0) {
303 errmsg << " '" << filename << "'" << "\n\ton processor(s): " << message;
304 }
305 else {
306 errmsg << ". See processor 0 output for more details.\n";
307 }
308 IOSS_ERROR(errmsg);
309 }
310
311 if (exists == par_size) {
312 return filename;
313 }
314
315 // Does not exist on any processors
316 return std::string();
317 }
318
319 std::pair<std::string, Ioss::DatabaseIO *>
320 expand_topology_files(FileNameGenerator generator, const Ioss::ParallelUtils &util,
321 const std::string &basename, const std::string &db_type,
322 const Ioss::PropertyManager &properties, Ioss::DatabaseUsage usage,
323 int step)
324 {
325 // See if there are multiple topology files
326
327 // If the file exists on all processors, return the filename.
328 // If the file does not exist on any processors, return "";
329 // If the file exists on some, but not all, throw an exception.
330
331 Ioss::DatabaseIO *db = nullptr;
332 std::string filename =
333 expand_topology_files(generator, util, basename, properties, usage, step);
334
335 if (!filename.empty()) {
336 db = Ioss::IOFactory::create(db_type, filename, usage, util.communicator(), properties);
337 int bad_count = 0;
338 std::string error_message;
339 bool is_valid_file = db != nullptr && db->ok(false, &error_message, &bad_count);
340 if (is_valid_file) {
341 return std::make_pair(filename, db);
342 }
343 else {
344 delete db;
345 std::ostringstream errmsg;
346 errmsg << error_message;
347 errmsg << __FILE__ << ", " << __FUNCTION__ << ", filename " << filename
348 << " is not a valid file\n";
349 IOSS_ERROR(errmsg);
350 }
351 }
352
353 // Does not exist on any processors
354 return std::make_pair(filename, db);
355 }
356
357 Ioss::FileNameGenerator construct_cyclic_filename_generator(unsigned cyclicCount)
358 {
359 if (cyclicCount > 26) {
360 cyclicCount = 26;
361 }
362
363 Ioss::FileNameGenerator generator = [cyclicCount](const std::string &baseFileName,
364 unsigned step) {
365 static std::string suffix = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
366 std::string filename = baseFileName;
367 if (step == 0)
368 step++;
369 filename += "-" + suffix.substr((step - 1) % cyclicCount, 1);
370 return filename;
371 };
372
373 return generator;
374 }
375
376 Ioss::FileNameGenerator construct_linear_filename_generator()
377 {
378 Ioss::FileNameGenerator generator = [](const std::string &baseFileName, unsigned step) {
379 std::ostringstream filename;
380 filename << baseFileName;
381 if (step > 1) {
382 filename << "-s" << std::setw(4) << std::setfill('0') << step;
383 }
384 return filename.str();
385 };
386
387 return generator;
388 }
389 } // namespace Ioss