Debian Codegraph @

packages/seacas/applications/exodiff/exo_entity.C

raw
1 // Copyright(C) 1999-2021, 2023 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 "ED_SystemInterface.h"
8 #include "exo_entity.h"
9 #include "exodusII.h" // for ex_get_var, EX_INVALID_ID, etc
10 #include "fmt/color.h"
11 #include "fmt/ostream.h"
12 #include "smart_assert.h" // for SMART_ASSERT
13 #include "stringx.h" // for to_lower
14 #include <cstdint> // for int64_t
15 #include <cstdlib> // for exit
16 #include <cstring> // for strlen
17 #include <string>
18 #include <vector>
19
20 namespace {
21 size_t get_index(int file_id, EXOTYPE exo_type, size_t id, const char *label);
22 size_t get_num_entities(int file_id, EXOTYPE exo_type);
23 size_t get_num_variables(int file_id, EXOTYPE type, const char *label);
24 size_t get_num_attributes(int file_id, EXOTYPE type, size_t id, const char *label);
25 #ifndef NDEBUG
26 size_t get_num_timesteps(int file_id);
27 #endif
28 } // namespace
29
30 Exo_Entity::Exo_Entity(int file_id, size_t id) : fileId(file_id), id_(id)
31 {
32 SMART_ASSERT(file_id > 0);
33 SMART_ASSERT((int)id > EX_INVALID_ID);
34 }
35
36 Exo_Entity::Exo_Entity(int file_id, size_t id, size_t nnodes)
37 : fileId(file_id), id_(id), numEntity(nnodes)
38 {
39 SMART_ASSERT(file_id > 0);
40 SMART_ASSERT((int)id > EX_INVALID_ID);
41 }
42
43 Exo_Entity::~Exo_Entity()
44 {
45 delete[] truth_;
46 if (numVars > 0) {
47 for (int i = 0; i < numVars; ++i) {
48 delete[] results_[i];
49 }
50 delete[] results_;
51 }
52 if (numAttr > 0) {
53 for (int i = 0; i < numAttr; ++i) {
54 delete[] attributes_[i];
55 }
56 }
57 }
58
59 void Exo_Entity::initialize(int file_id, size_t id)
60 {
61 fileId = file_id;
62 id_ = id;
63
64 index_ = get_index(fileId, exodus_type(), id_, label());
65
66 entity_load_params();
67
68 internal_load_params();
69 }
70
71 bool Exo_Entity::is_valid_var(size_t var_index) const
72 {
73 SMART_ASSERT((int)var_index < numVars);
74 if (truth_ == nullptr) {
75 get_truth_table();
76 }
77
78 return (truth_[var_index] != 0);
79 }
80
81 std::string Exo_Entity::Load_Results(int time_step, int var_index)
82 {
83 SMART_ASSERT(Check_State());
84
85 if (fileId < 0) {
86 return "exodiff: ERROR: Invalid file id!";
87 }
88 if (id_ == EX_INVALID_ID) {
89 return "exodiff: ERROR: Must initialize block parameters first!";
90 }
91 if (var_index < 0 || var_index >= numVars) {
92 Error("Exo_Entity::Load_Results(): var_index is invalid. Aborting...\n");
93 }
94 SMART_ASSERT(time_step >= 1 && time_step <= (int)get_num_timesteps(fileId));
95
96 if (time_step != currentStep) {
97 Free_Results();
98 currentStep = time_step;
99 }
100
101 if (truth_ == nullptr) {
102 get_truth_table();
103 }
104
105 if (truth_[var_index] != 0) {
106 if ((results_[var_index] == nullptr) && (numEntity != 0u)) {
107 results_[var_index] = new double[numEntity];
108 SMART_ASSERT(results_[var_index] != nullptr);
109 }
110 if (numEntity != 0u) {
111 int err = 0;
112 err = ex_get_var(fileId, time_step, exodus_type(), var_index + 1, id_, numEntity,
113 results_[var_index]);
114
115 if (err < 0) {
116 Error(fmt::format("Exo_Entity::Load_Results(): Call to exodus routine"
117 " returned error value! {} id = {}\n"
118 "Aborting...\n",
119 label(), id_));
120 }
121 else if (err > 0) {
122 return fmt::format("WARNING: Number {} returned from call to exodus get variable routine.",
123 err);
124 }
125 }
126 else {
127 return std::string("WARNING: No items in this ") + label();
128 }
129 }
130 else {
131 return std::string("WARNING: Variable not stored in this ") + label();
132 }
133 return "";
134 }
135
136 std::string Exo_Entity::Load_Results(int t1, int t2, double proportion, int var_index)
137 {
138 static std::vector<double> results2;
139
140 SMART_ASSERT(Check_State());
141
142 if (fileId < 0) {
143 return "exodiff: ERROR: Invalid file id!";
144 }
145 if (id_ == EX_INVALID_ID) {
146 return "exodiff: ERROR: Must initialize block parameters first!";
147 }
148 SMART_ASSERT(var_index >= 0 && var_index < numVars);
149 SMART_ASSERT(t1 >= 1 && t1 <= (int)get_num_timesteps(fileId));
150 SMART_ASSERT(t2 >= 1 && t2 <= (int)get_num_timesteps(fileId));
151
152 if (t1 != currentStep) {
153 Free_Results();
154 currentStep = t1;
155 }
156
157 if (truth_ == nullptr) {
158 get_truth_table();
159 }
160
161 if (truth_[var_index] != 0) {
162 if ((results_[var_index] == nullptr) && (numEntity != 0u)) {
163 results_[var_index] = new double[numEntity];
164 SMART_ASSERT(results_[var_index] != nullptr);
165 }
166 if (numEntity != 0u) {
167 int err =
168 ex_get_var(fileId, t1, exodus_type(), var_index + 1, id_, numEntity, results_[var_index]);
169
170 if (err < 0) {
171 Error(fmt::format(
172 "Exo_Entity::Load_Results(): Call to exodus routine returned error value! {} id = {}\n"
173 "Aborting...\n",
174 label(), id_));
175 }
176 else if (err > 0) {
177 return fmt::format("WARNING: Number {} returned from call to exodus get variable routine.",
178 err);
179 }
180
181 if (t1 != t2) {
182 results2.resize(numEntity);
183 err = ex_get_var(fileId, t2, exodus_type(), var_index + 1, id_, numEntity, Data(results2));
184
185 if (err < 0) {
186 Error(fmt::format("Exo_Entity::Load_Results(): Call to exodus routine"
187 " returned error value! {} id = {}\n"
188 "Aborting...\n",
189 label(), id_));
190 }
191
192 double *results1 = results_[var_index];
193 for (size_t i = 0; i < numEntity; i++) {
194 results1[i] = (1.0 - proportion) * results1[i] + proportion * results2[i];
195 }
196 }
197 }
198 else {
199 return std::string("WARNING: No items in this ") + label();
200 }
201 }
202 else {
203 return std::string("WARNING: Variable not stored in this ") + label();
204 }
205 return "";
206 }
207
208 const double *Exo_Entity::Get_Results(int var_index) const
209 {
210 SMART_ASSERT(Check_State());
211 if (currentStep == 0) {
212 return nullptr;
213 }
214 SMART_ASSERT(var_index >= 0 && var_index < numVars);
215 if (var_index >= 0 && var_index < numVars) {
216 return results_[var_index];
217 }
218
219 return nullptr;
220 }
221
222 void Exo_Entity::Free_Results()
223 {
224 SMART_ASSERT(Check_State());
225
226 currentStep = 0;
227 for (int v = 0; v < numVars; ++v) {
228 delete[] results_[v];
229 results_[v] = nullptr;
230 }
231 }
232
233 void Exo_Entity::get_truth_table() const
234 {
235 if (numVars > 0 && truth_ == nullptr) {
236 truth_ = new int[numVars];
237 SMART_ASSERT(truth_ != nullptr);
238 // initialize to true for the case of no objects in the block (some older
239 // versions of ex_get_object_truth_vector do not set the values at all)
240 for (int i = 0; i < numVars; ++i) {
241 truth_[i] = 1;
242 }
243 int err = ex_get_object_truth_vector(fileId, exodus_type(), id_, numVars, truth_);
244 if (err < 0) {
245 Error("Exo_Entity::get_truth_table(): ex_get_object_truth_vector returned error.\n");
246 }
247 }
248 }
249
250 std::string Exo_Entity::Load_Attributes(int attr_index)
251 {
252 SMART_ASSERT(Check_State());
253
254 if (fileId < 0) {
255 return "exodiff: ERROR: Invalid file id!";
256 }
257 if (id_ == EX_INVALID_ID) {
258 return "exodiff: ERROR: Must initialize block parameters first!";
259 }
260 SMART_ASSERT(attr_index >= 0 && attr_index < numAttr);
261
262 if ((attributes_[attr_index] == nullptr) && (numEntity != 0u)) {
263 attributes_[attr_index] = new double[numEntity];
264 SMART_ASSERT(attributes_[attr_index] != nullptr);
265 }
266
267 if (numEntity != 0u) {
268 int err = 0;
269 err = ex_get_one_attr(fileId, exodus_type(), id_, attr_index + 1, attributes_[attr_index]);
270
271 if (err < 0) {
272 Error(fmt::format("Exo_Entity::Load_Attributes(): Call to exodus routine"
273 " returned error value! {} id = {}\n"
274 "Aborting...\n",
275 label(), id_));
276 }
277 else if (err > 0) {
278 return fmt::format("WARNING: Number {} returned from call to exodus get variable routine.",
279 err);
280 }
281 }
282 else {
283 return std::string("WARNING: No items in this ") + label();
284 }
285
286 return "";
287 }
288
289 const double *Exo_Entity::Get_Attributes(int attr_index) const
290 {
291 SMART_ASSERT(Check_State());
292 SMART_ASSERT(attr_index >= 0 && attr_index < numAttr);
293 return attributes_[attr_index];
294 }
295
296 void Exo_Entity::Free_Attributes()
297 {
298 SMART_ASSERT(Check_State());
299
300 for (int v = 0; v < numAttr; ++v) {
301 delete[] attributes_[v];
302 attributes_[v] = nullptr;
303 }
304 }
305
306 const std::string &Exo_Entity::Get_Attribute_Name(int attr_index) const
307 {
308 SMART_ASSERT(attr_index >= 0 && attr_index < numAttr);
309 return attributeNames[attr_index];
310 }
311
312 int Exo_Entity::Find_Attribute_Index(const std::string &name) const
313 {
314 std::string lower_name = name;
315 to_lower(lower_name);
316 int indx = -1;
317 for (int i = 0; i < numAttr; i++) {
318 if (attributeNames[i] == lower_name) {
319 indx = i;
320 break;
321 }
322 }
323 return indx;
324 }
325
326 void Exo_Entity::internal_load_params()
327 {
328 int name_size = ex_inquire_int(fileId, EX_INQ_MAX_READ_NAME_LENGTH);
329 {
330 std::vector<char> name(name_size + 1);
331 ex_get_name(fileId, exodus_type(), id_, Data(name));
332 if (name[0] != '\0') {
333 name_ = Data(name);
334 to_lower(name_);
335 generatedName_ = false;
336 }
337 else {
338 name_ = short_label();
339 name_ += "_";
340 name_ += std::to_string(id_);
341 generatedName_ = true;
342 }
343 }
344 numVars = get_num_variables(fileId, exodus_type(), label());
345 if (numVars != 0) {
346 results_ = new double *[numVars];
347 SMART_ASSERT(results_ != nullptr);
348 for (int i = 0; i < numVars; ++i) {
349 results_[i] = nullptr;
350 }
351 }
352
353 if (exodus_type() != EX_ASSEMBLY) {
354 numAttr = get_num_attributes(fileId, exodus_type(), id_, label());
355 if (numAttr != 0) {
356 attributes_.resize(numAttr);
357
358 char **names = get_name_array(numAttr, name_size);
359 int err = ex_get_attr_names(fileId, exodus_type(), id_, names);
360 if (err < 0) {
361 Error(fmt::format(
362 "Exo_Read::Get_Init_Data(): Failed to get {} attribute names! Aborting...\n",
363 label()));
364 }
365
366 for (int vg = 0; vg < numAttr; ++vg) {
367 SMART_ASSERT(names[vg] != nullptr);
368 if (std::strlen(names[vg]) == 0) {
369 std::string name = "attribute_" + std::to_string(vg + 1);
370 attributeNames.push_back(name);
371 }
372 else if (static_cast<int>(std::strlen(names[vg])) > name_size) {
373 fmt::print(stderr, fmt::fg(fmt::color::red),
374 "exodiff: ERROR: {} attribute names appear corrupt\n"
375 " A length is 0 or greater than name_size({})\n"
376 " Here are the names that I received from"
377 " a call to ex_get_attr_names(...):\n",
378 label(), name_size);
379 for (int k = 1; k <= numAttr; ++k) {
380 fmt::print(stderr, fmt::fg(fmt::color::red), "\t\t{}) \"{}\"\n", k, names[k - 1]);
381 }
382 fmt::print(stderr, fmt::fg(fmt::color::red), " Aborting...\n");
383 exit(1);
384 }
385 else {
386 std::string n(names[vg]);
387 to_lower(n);
388 attributeNames.push_back(n);
389 }
390 }
391 free_name_array(names, numAttr);
392 }
393 }
394 }
395
396 namespace {
397 size_t get_index(int file_id, EXOTYPE exo_type, size_t id, const char *label)
398 {
399 // Get ids...
400 size_t count = get_num_entities(file_id, exo_type);
401 if ((ex_int64_status(file_id) & EX_IDS_INT64_API) != 0) {
402 std::vector<int64_t> ids(count);
403 ex_get_ids(file_id, exo_type, Data(ids));
404
405 for (size_t i = 0; i < count; i++) {
406 if (static_cast<size_t>(ids[i]) == id) {
407 return i;
408 }
409 }
410 }
411 else {
412 std::vector<int> ids(count);
413 ex_get_ids(file_id, exo_type, Data(ids));
414
415 for (size_t i = 0; i < count; i++) {
416 if (static_cast<size_t>(ids[i]) == id) {
417 return i;
418 }
419 }
420 }
421
422 Error(fmt::format("{} id {} does not exist!\n", label, id));
423 return 0;
424 }
425
426 size_t get_num_entities(int file_id, EXOTYPE exo_type)
427 {
428 ex_inquiry inquiry = EX_INQ_INVALID;
429 switch (exo_type) {
430 case EX_ASSEMBLY: inquiry = EX_INQ_ASSEMBLY; break;
431 case EX_ELEM_BLOCK: inquiry = EX_INQ_ELEM_BLK; break;
432 case EX_NODE_SET: inquiry = EX_INQ_NODE_SETS; break;
433 case EX_SIDE_SET: inquiry = EX_INQ_SIDE_SETS; break;
434 case EX_EDGE_BLOCK: inquiry = EX_INQ_EDGE_BLK; break;
435 case EX_FACE_BLOCK: inquiry = EX_INQ_FACE_BLK; break;
436 default: Error("Invalid entity type in get_num_entities\n");
437 }
438 SMART_ASSERT(inquiry != EX_INQ_INVALID);
439 return ex_inquire_int(file_id, inquiry);
440 }
441
442 size_t get_num_variables(int file_id, EXOTYPE type, const char *label)
443 {
444 int num_vars = 0;
445 int err = ex_get_variable_param(file_id, type, &num_vars);
446 if (err < 0) {
447 Error(fmt::format("Failed to get number of '{}' variables! Aborting...\n", label));
448 }
449 return num_vars;
450 }
451
452 size_t get_num_attributes(int file_id, EXOTYPE type, size_t id, const char *label)
453 {
454 int num_attr = 0;
455 int err = ex_get_attr_param(file_id, type, id, &num_attr);
456 if (err < 0) {
457 Error(fmt::format("Failed to get number of '{}' attributes! Aborting...\n", label));
458 }
459 return num_attr;
460 }
461
462 #ifndef NDEBUG
463 size_t get_num_timesteps(int file_id) { return ex_inquire_int(file_id, EX_INQ_TIME); }
464 #endif
465 } // namespace