Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_GroupingEntity.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 "Ioss_DatabaseIO.h"
8 #include "Ioss_GroupingEntity.h"
9 #include "Ioss_Property.h"
10 #include "Ioss_Region.h"
11 #include "Ioss_Utils.h"
12 #include "Ioss_VariableType.h"
13 #include <cassert>
14 #include <cstddef>
15 #include <fmt/ostream.h>
16 #include <iostream>
17 #include <string>
18
19 #include "Ioss_CodeTypes.h"
20 #include "Ioss_EntityType.h"
21 #include "Ioss_Field.h"
22 #include "Ioss_FieldManager.h"
23 #include "Ioss_ParallelUtils.h"
24 #include "Ioss_PropertyManager.h"
25 #include "Ioss_State.h"
26
27 /** \brief Base class constructor adds "name" and "entity_count" properties to the entity.
28 *
29 * \param[in] io_database The database associated with the entity.
30 * \param[in] my_name The entity name.
31 * \param[in] entity_cnt The number of subentities in the entity.
32 *
33 */
34 Ioss::GroupingEntity::GroupingEntity(Ioss::DatabaseIO *io_database, const std::string &my_name,
35 int64_t entity_cnt)
36 : entityCount(entity_cnt), entityName(my_name), database_(io_database),
37 hash_(Ioss::Utils::hash(my_name))
38 {
39 properties.add(Ioss::Property("name", my_name));
40 properties.add(Ioss::Property("entity_count", entity_cnt));
41 properties.add(Ioss::Property(this, "attribute_count", Ioss::Property::INTEGER));
42
43 if (my_name != "null_entity") {
44 Ioss::Field::BasicType int_type = Ioss::Field::INTEGER;
45 if (io_database != nullptr) {
46 int_type = field_int_type();
47 }
48 fields.add(Ioss::Field("ids", int_type, "scalar", Ioss::Field::MESH, entity_cnt));
49 }
50 }
51
52 Ioss::GroupingEntity::GroupingEntity(const Ioss::GroupingEntity &other)
53 : properties(other.properties), fields(other.fields), entityCount(other.entityCount),
54 entityName(other.entityName), attributeCount(other.attributeCount),
55 entityState(other.entityState), hash_(other.hash_)
56 {
57 }
58
59 Ioss::GroupingEntity::~GroupingEntity()
60 {
61 // Only deleted by owning entity (Ioss::Region)
62 database_ = nullptr;
63 }
64
65 // Default implementation is to do nothing. Redefined in Ioss::Region
66 // to actually delete the database.
67 void Ioss::GroupingEntity::delete_database() {}
68
69 void Ioss::GroupingEntity::really_delete_database()
70 {
71 delete database_;
72 database_ = nullptr;
73 }
74
75 const Ioss::GroupingEntity *Ioss::GroupingEntity::contained_in() const
76 {
77 if (database_ == nullptr) {
78 return nullptr;
79 }
80 return database_->get_region();
81 }
82
83 std::string Ioss::GroupingEntity::generic_name() const
84 {
85 int64_t id = get_optional_property("id", 0);
86 return Ioss::Utils::encode_entity_name(short_type_string(), id);
87 }
88
89 bool Ioss::GroupingEntity::is_alias(const std::string &my_name) const
90 {
91 Region *region = database_->get_region();
92 return region->get_alias(my_name, type()) == entityName;
93 }
94
95 Ioss::DatabaseIO *Ioss::GroupingEntity::get_database() const
96 {
97 assert(database_ != nullptr);
98 return database_;
99 }
100
101 /** \brief Get the file name associated with the database containing this entity.
102 *
103 * \returns The file name.
104 */
105 std::string Ioss::GroupingEntity::get_filename() const
106 {
107 // Ok for database_ to be nullptr at this point.
108 if (database_ == nullptr) {
109 return {};
110 }
111
112 return database_->get_filename();
113 }
114
115 void Ioss::GroupingEntity::set_database(Ioss::DatabaseIO *io_database)
116 {
117 assert(database_ == nullptr); // Must be unset if we are setting it.
118 assert(io_database != nullptr); // Must be set to valid value
119 database_ = io_database;
120 }
121
122 void Ioss::GroupingEntity::reset_database(Ioss::DatabaseIO *io_database)
123 {
124 assert(io_database != nullptr); // Must be set to valid value
125 database_ = io_database;
126 }
127
128 // Discuss Data Object functions:
129 // ---Affect the containing object:
130 // open(in string object_name, out ?)
131 // close()
132 // destroy()
133 // ---Affect what the object contains:
134 // set(in string propertyname, in any property_value)
135 // get(in string propertyname, out any property_value)
136 // add(in string propertyname);
137 // delete(in string propertyname)
138 // describe(out vector<Ioss::Properties>)
139 //
140
141 /** \brief Get the current Ioss::State of the entity.
142 *
143 * \returns The current state.
144 */
145 Ioss::State Ioss::GroupingEntity::get_state() const { return entityState; }
146
147 /** \brief Calculate and get an implicit property.
148 *
149 * These are calculated from data stored in the EntityBlock instead of having
150 * an explicit value assigned. An example would be 'element_block_count' for a region.
151 * Note that even though this is a pure virtual function, an implementation
152 * is provided to return properties that are common to all 'block'-type grouping entities.
153 * Derived classes should call 'GroupingEntity::get_implicit_property'
154 * if the requested property is not specific to their type.
155 */
156 Ioss::Property Ioss::GroupingEntity::get_implicit_property(const std::string &my_name) const
157 {
158 // Handle properties generic to all GroupingEntities.
159 // These include:
160 if (my_name == "attribute_count") {
161 count_attributes();
162 return {my_name, static_cast<int>(attributeCount)};
163 }
164
165 // End of the line. No property of this name exists.
166 IOSS_ERROR(fmt::format("\nERROR: Property '{}' does not exist on {} {}\n\n", my_name,
167 type_string(), name()));
168 }
169
170 bool Ioss::GroupingEntity::check_for_duplicate(const Ioss::Field &new_field) const
171 {
172 // See if a field with the same name exists...
173 if (field_exists(new_field.get_name())) {
174 auto behavior = get_database()->get_duplicate_field_behavior();
175 if (behavior != DuplicateFieldBehavior::IGNORE_) {
176 // Get the existing field so we can compare with `new_field`
177 const Ioss::Field &field = fields.getref(new_field.get_name());
178 if (field != new_field) {
179 std::string warn_err = behavior == DuplicateFieldBehavior::WARNING_ ? "" : "ERROR: ";
180 std::ostringstream errmsg;
181 fmt::print(errmsg,
182 "{}Duplicate incompatible fields named '{}' on {} {}:\n"
183 "\tExisting field: {} {} of size {} bytes with role '{}' and storage '{}',\n"
184 "\tDuplicate field: {} {} of size {} bytes with role '{}' and storage '{}'.",
185 warn_err, new_field.get_name(), type_string(), name(), field.raw_count(),
186 field.type_string(), field.get_size(), field.role_string(),
187 field.raw_storage()->name(), new_field.raw_count(), new_field.type_string(),
188 new_field.get_size(), new_field.role_string(), new_field.raw_storage()->name());
189 if (behavior == DuplicateFieldBehavior::WARNING_) {
190 auto util = get_database()->util();
191 if (util.parallel_rank() == 0) {
192 fmt::print(Ioss::WarnOut(), "{}\n", errmsg.str());
193 }
194 return true;
195 }
196 IOSS_ERROR(errmsg);
197 }
198 }
199 }
200 return false;
201 }
202
203 /** \brief Add a field to the entity's field manager.
204 *
205 * Assumes that a field with the same name does not already exist.
206 *
207 * \param[in] new_field The field to add
208 *
209 */
210 void Ioss::GroupingEntity::field_add(Ioss::Field new_field)
211 {
212 size_t field_size = new_field.raw_count();
213
214 if (new_field.get_role() == Ioss::Field::REDUCTION) {
215 if (field_size == 0) {
216 new_field.reset_count(1);
217 }
218 if (!check_for_duplicate(new_field)) {
219 fields.add(new_field);
220 }
221 return;
222 }
223
224 size_t entity_size = entity_count();
225 if (field_size == 0 && entity_size != 0) {
226 // Set field size to match entity size...
227 new_field.reset_count(entity_size);
228 }
229 else if (entity_size != field_size && type() != REGION) {
230 std::string filename = get_database()->get_filename();
231 IOSS_ERROR(fmt::format(
232 "IO System error: The {} '{}' has a size of {},\nbut the field '{}' which is being "
233 "output on that entity has a size of {}\non database '{}'.\nThe sizes must match. "
234 "This is an application error that should be reported.",
235 type_string(), name(), entity_size, new_field.get_name(), field_size, filename));
236 }
237 if (!check_for_duplicate(new_field)) {
238 fields.add(new_field);
239 }
240 }
241
242 /** \brief Read field data from the database file into memory using a pointer.
243 *
244 * \param[in] field_name The name of the field to read.
245 * \param[out] data The data.
246 * \param[in] data_size The number of bytes of data to be read.
247 * \returns The number of values read.
248 *
249 */
250 int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name, void *data,
251 size_t data_size) const
252 {
253 verify_field_exists(field_name, "input");
254
255 Ioss::Field field = get_field(field_name);
256 int64_t retval = internal_get_field_data(field, data, data_size);
257
258 // At this point, transform the field if specified...
259 if (retval >= 0) {
260 field.transform(data);
261 }
262
263 return retval;
264 }
265
266 /** Zero-copy API. *IF* a field is zero-copyable, then this function will set the `data`
267 * pointer to point to a chunk of memory of size `data_size` bytes containing the field
268 * data for the specified field. If the field is not zero-copyable, then the `data`
269 * pointer will point to `nullptr` and `data_size` will be 0 and `retval` will be -2.
270 * TODO: Verify that returning `-2` on error makes sense or helps at all...
271 */
272 int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name, void **data,
273 size_t *data_size) const
274 {
275 verify_field_exists(field_name, "input");
276
277 int64_t retval = -1;
278 Ioss::Field field = get_field(field_name);
279 if (field.zero_copy_enabled()) {
280 retval = internal_get_zc_field_data(field, data, data_size);
281 }
282 else {
283 retval = -2;
284 *data = nullptr;
285 *data_size = 0;
286 }
287 return retval;
288 }
289
290 /** \brief Write field data from memory into the database file using a pointer.
291 *
292 * \param[in] field_name The name of the field to write.
293 * \param[in] data The data.
294 * \param[in] data_size The number of bytes of data to be written.
295 * \returns The number of values written.
296 *
297 */
298 int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name, void *data,
299 size_t data_size) const
300 {
301 verify_field_exists(field_name, "input");
302
303 Ioss::Field field = get_field(field_name);
304 field.transform(data);
305 return internal_put_field_data(field, data, data_size);
306 }
307
308 /** \brief Get the number of fields with the given role (MESH, ATTRIBUTE, TRANSIENT, REDUCTION,
309 * etc.)
310 * in the entity's field manager.
311 *
312 * \returns The number of fields with the given role.
313 */
314 size_t Ioss::GroupingEntity::field_count(Ioss::Field::RoleType role) const
315 {
316 IOSS_FUNC_ENTER(m_);
317 Ioss::NameList names = field_describe(role);
318 return names.size();
319 }
320
321 void Ioss::GroupingEntity::count_attributes() const
322 {
323 if (attributeCount > 0) {
324 return;
325 }
326
327 // If the set has a field named "attribute", then the number of
328 // attributes is equal to the component count of that field...
329 Ioss::NameList results_fields = field_describe(Ioss::Field::ATTRIBUTE);
330
331 Ioss::NameList::const_iterator IF;
332 int64_t attribute_count = 0;
333 for (IF = results_fields.begin(); IF != results_fields.end(); ++IF) {
334 const std::string &field_name = *IF;
335 if (field_name != "attribute" || results_fields.size() == 1) {
336 Ioss::Field field = get_field(field_name);
337 attribute_count += field.raw_storage()->component_count();
338 }
339 }
340 attributeCount = attribute_count;
341 }
342
343 void Ioss::GroupingEntity::verify_field_exists(const std::string &field_name,
344 const std::string &inout) const
345 {
346 if (!field_exists(field_name)) {
347 std::string filename = get_database()->get_filename();
348 IOSS_ERROR(
349 fmt::format("\nERROR: On database '{}', Field '{}' does not exist for {} on {} {}\n\n",
350 filename, field_name, inout, type_string(), name()));
351 }
352 }
353
354 void Ioss::GroupingEntity::property_update(const std::string &property, int64_t value) const
355 {
356 if (property_exists(property)) {
357 if (get_property(property).get_int() != value) {
358 auto *nge = const_cast<Ioss::GroupingEntity *>(this);
359 nge->property_erase(property);
360 nge->property_add(Ioss::Property(property, value));
361 }
362 }
363 else {
364 auto *nge = const_cast<Ioss::GroupingEntity *>(this);
365 nge->property_add(Ioss::Property(property, value));
366 }
367 }
368
369 void Ioss::GroupingEntity::property_update(const std::string &property,
370 const std::string &value) const
371 {
372 if (property_exists(property)) {
373 if (get_property(property).get_string() != value) {
374 auto *nge = const_cast<Ioss::GroupingEntity *>(this);
375 nge->property_erase(property);
376 nge->property_add(Ioss::Property(property, value));
377 }
378 }
379 else {
380 auto *nge = const_cast<Ioss::GroupingEntity *>(this);
381 nge->property_add(Ioss::Property(property, value));
382 }
383 }
384
385 bool Ioss::GroupingEntity::equal_(const Ioss::GroupingEntity &rhs, bool quiet) const
386 {
387 bool same = true;
388 if (this->entityName != rhs.entityName) {
389 if (quiet) {
390 return false;
391 }
392 fmt::print(Ioss::OUTPUT(), "GroupingEntity: entityName mismatch ({} vs. {})\n",
393 this->entityName, rhs.entityName);
394 same = false;
395 }
396
397 if (this->entityCount != rhs.entityCount) {
398 if (quiet) {
399 return false;
400 }
401 fmt::print(Ioss::OUTPUT(), "GroupingEntity: entityCount mismatch ([] vs. [])\n",
402 this->entityCount, rhs.entityCount);
403 same = false;
404 }
405
406 if (this->attributeCount != rhs.attributeCount) {
407 if (quiet) {
408 return false;
409 }
410 fmt::print(Ioss::OUTPUT(), "GroupingEntity: attributeCount mismatch ([] vs. [])\n",
411 this->attributeCount, rhs.attributeCount);
412 same = false;
413 }
414
415 if (this->entityState != rhs.entityState) {
416 if (quiet) {
417 return false;
418 }
419 fmt::print(Ioss::OUTPUT(), "GroupingEntity: entityState mismatch ([] vs. [])\n",
420 static_cast<int>(this->entityState), static_cast<int>(rhs.entityState));
421 same = false;
422 }
423
424 if (this->hash_ != rhs.hash_) {
425 if (quiet) {
426 return false;
427 }
428 fmt::print(Ioss::OUTPUT(), "GroupingEntity: hash_ mismatch ({} vs. {})\n", this->hash_,
429 rhs.hash_);
430 same = false;
431 }
432
433 /* COMPARE properties */
434 Ioss::NameList lhs_properties = this->property_describe();
435 Ioss::NameList rhs_properties = rhs.property_describe();
436
437 if (lhs_properties.size() != rhs_properties.size()) {
438 if (quiet) {
439 return false;
440 }
441 fmt::print(Ioss::OUTPUT(), "GroupingEntity: NUMBER of properties are different ({} vs. {})\n",
442 lhs_properties.size(), rhs_properties.size());
443 same = false;
444 }
445
446 for (auto &lhs_property : lhs_properties) {
447 auto it = std::find(rhs_properties.begin(), rhs_properties.end(), lhs_property);
448 if (it == rhs_properties.end()) {
449 if (!quiet) {
450 fmt::print(Ioss::OUTPUT(), "WARNING: {}: INPUT property ({}) not found in input #2\n",
451 name(), lhs_property);
452 same = false;
453 }
454 continue;
455 }
456
457 if (lhs_property == "IOSS_INTERNAL_CONTAINED_IN") {
458 continue;
459 }
460
461 if (this->properties.get(lhs_property) != rhs.properties.get(lhs_property)) {
462 // EMPIRICALLY, different representations (e.g., CGNS vs. Exodus) of the same mesh
463 // can have different values for the "original_block_order" property.
464 if (lhs_property == "original_block_order") {
465 if (!quiet) {
466 fmt::print(Ioss::OUTPUT(),
467 "WARNING: {}: values for \"original_block_order\" DIFFER ({} vs. {})\n",
468 name(), this->properties.get(lhs_property).get_int(),
469 rhs.properties.get(lhs_property).get_int());
470 }
471 }
472 else {
473 if (!quiet) {
474 auto lhs_prop = this->properties.get(lhs_property);
475 auto rhs_prop = rhs.properties.get(lhs_property);
476 if (lhs_prop.get_type() == Ioss::Property::STRING) {
477 auto p1_value = lhs_prop.get_string();
478 auto p2_value = rhs_prop.get_string();
479 fmt::print(Ioss::OUTPUT(),
480 "{}: PROPERTY value mismatch [STRING] ({}): ('{}' vs '{}')\n", name(),
481 lhs_property, p1_value, p2_value);
482 }
483 else if (lhs_prop.get_type() == Ioss::Property::INTEGER) {
484 fmt::print(Ioss::OUTPUT(), "{}: PROPERTY value mismatch [INTEGER] ({}): ({} vs {})\n",
485 name(), lhs_property, lhs_prop.get_int(), rhs_prop.get_int());
486 }
487 else {
488 fmt::print(Ioss::OUTPUT(), "{}: PROPERTY value mismatch ({}): unsupported type\n",
489 name(), lhs_property);
490 }
491 }
492 else {
493 return false;
494 }
495 }
496 same = false;
497 }
498 }
499
500 if (!quiet) {
501 for (auto &rhs_property : rhs_properties) {
502 auto it = std::find(lhs_properties.begin(), lhs_properties.end(), rhs_property);
503 if (it == lhs_properties.end()) {
504 fmt::print(Ioss::OUTPUT(), "WARNING: {}: OUTPUT property ({}) not found in input #1\n",
505 name(), rhs_property);
506 same = false;
507 }
508 }
509 }
510
511 /* COMPARE fields */
512 Ioss::NameList lhs_fields = this->field_describe();
513 Ioss::NameList rhs_fields = rhs.field_describe();
514
515 if (lhs_fields.size() != rhs_fields.size()) {
516 if (!quiet) {
517 fmt::print(Ioss::OUTPUT(), "\n{}: NUMBER of fields is different ({} vs. {})\n", name(),
518 lhs_fields.size(), rhs_fields.size());
519 same = false;
520 }
521 else {
522 return false;
523 }
524 }
525
526 for (auto &field : lhs_fields) {
527 if (!quiet) {
528 const auto &f1 = this->fields.get(field);
529 if (rhs.field_exists(field)) {
530 const auto &f2 = rhs.fields.get(field);
531 if (!f1.equal(f2)) {
532 fmt::print(Ioss::OUTPUT(), "{}: FIELD ({}) mismatch\n\n", name(), field);
533 same = false;
534 }
535 }
536 else {
537 fmt::print(Ioss::OUTPUT(), "{}: FIELD ({}) not found in input #2\n", name(), field);
538 same = false;
539 }
540 }
541 else {
542 if (!this->fields.get(field).equal(rhs.fields.get(field))) {
543 return false;
544 }
545 }
546 }
547
548 // See which fields are missing from input #1...
549 // NOTE: `quiet` mode has already exited by this point.
550 for (auto &field : rhs_fields) {
551 if (!this->field_exists(field)) {
552 fmt::print(Ioss::OUTPUT(), "{}: FIELD ({}) not found in input #1\n", name(), field);
553 same = false;
554 }
555 }
556 return same;
557 }
558
559 bool Ioss::GroupingEntity::operator==(const Ioss::GroupingEntity &rhs) const
560 {
561 return equal_(rhs, true);
562 }
563
564 bool Ioss::GroupingEntity::operator!=(const Ioss::GroupingEntity &rhs) const
565 {
566 return !(*this == rhs);
567 }
568
569 bool Ioss::GroupingEntity::equal(const Ioss::GroupingEntity &rhs) const
570 {
571 return equal_(rhs, false);
572 }