| 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 |
// -*- Mode: c++ -*- |
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include "Ioss_DBUsage.h" |
| 11 |
#include "Ioss_DatabaseIO.h" |
| 12 |
#include "Ioss_Field.h" |
| 13 |
#include "Ioss_Map.h" |
| 14 |
#include "Ioss_Utils.h" |
| 15 |
#include <algorithm> |
| 16 |
#include <cstdint> |
| 17 |
#include <ctime> |
| 18 |
#include <exodusII.h> |
| 19 |
#include <map> |
| 20 |
#include <set> |
| 21 |
#include <sstream> |
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "Ioss_CodeTypes.h" |
| 26 |
#include "Ioss_DataSize.h" |
| 27 |
#include "Ioss_State.h" |
| 28 |
#include "ioex_export.h" |
| 29 |
|
| 30 |
namespace Ioss { |
| 31 |
class Assembly; |
| 32 |
class Blob; |
| 33 |
class GroupingEntity; |
| 34 |
class Region; |
| 35 |
class EntityBlock; |
| 36 |
class NodeBlock; |
| 37 |
class EdgeBlock; |
| 38 |
class FaceBlock; |
| 39 |
class ElementBlock; |
| 40 |
class EntitySet; |
| 41 |
class NodeSet; |
| 42 |
class EdgeSet; |
| 43 |
class FaceSet; |
| 44 |
class ElementSet; |
| 45 |
class SideBlock; |
| 46 |
class SideSet; |
| 47 |
class StructuredBlock; |
| 48 |
class CommSet; |
| 49 |
class ElementTopology; |
| 50 |
class Field; |
| 51 |
class Map; |
| 52 |
class PropertyManager; |
| 53 |
} // namespace Ioss |
| 54 |
|
| 55 |
/** \brief A namespace for the exodus database format. |
| 56 |
*/ |
| 57 |
namespace Ioex { |
| 58 |
struct CommunicationMetaData; |
| 59 |
|
| 60 |
// Used for variable name index mapping |
| 61 |
using VariableNameMap = std::map<std::string, int, std::less<>>; |
| 62 |
using VNMValuePair = VariableNameMap::value_type; |
| 63 |
|
| 64 |
// Used to store reduction variables |
| 65 |
using ValueContainer = std::vector<double>; |
| 66 |
|
| 67 |
// Used for persistent entity IDs |
| 68 |
// The set contains a pair of <ex_entity_type, int>. |
| 69 |
// The ex_entity_type is the exodus entity type defined in |
| 70 |
// exodus's exodusII.h. A couple examples are: |
| 71 |
// EX_ELEM_BLOCK element block and EX_NODE_SET nodeset. |
| 72 |
// |
| 73 |
// The 'int' is the entity id. The set is used for output databases |
| 74 |
// to ensure that there are no id collisions. |
| 75 |
using EntityIdSet = std::set<std::pair<int64_t, int64_t>>; |
| 76 |
|
| 77 |
class IOEX_EXPORT BaseDatabaseIO : public Ioss::DatabaseIO |
| 78 |
{ |
| 79 |
public: |
| 80 |
BaseDatabaseIO(Ioss::Region *region, const std::string &filename, Ioss::DatabaseUsage db_usage, |
| 81 |
Ioss_MPI_Comm communicator, const Ioss::PropertyManager &props); |
| 82 |
|
| 83 |
~BaseDatabaseIO() override; |
| 84 |
|
| 85 |
IOSS_NODISCARD std::string get_format() const override { return "Exodus"; } |
| 86 |
|
| 87 |
// Check capabilities of input/output database... Returns an |
| 88 |
// unsigned int with the supported Ioss::EntityTypes or'ed |
| 89 |
// together. If "return_value & Ioss::EntityType" is set, then the |
| 90 |
// database supports that type (e.g. return_value & Ioss::FACESET) |
| 91 |
IOSS_NODISCARD unsigned entity_field_support() const override; |
| 92 |
|
| 93 |
IOSS_NODISCARD std::string get_internal_change_set_name() const override { return m_groupName; } |
| 94 |
|
| 95 |
/** \brief Checks if a database type supports groups |
| 96 |
* |
| 97 |
* \returns True if successful. |
| 98 |
*/ |
| 99 |
bool supports_group() const; |
| 100 |
|
| 101 |
/** \brief If a database type supports groups and if the database |
| 102 |
* contains groups, open the specified group. |
| 103 |
* |
| 104 |
* If the group_name begins with '/', it specifies the absolute path |
| 105 |
* name from the root with '/' separating groups. Otherwise, the |
| 106 |
* group_name specifies a child group of the currently active group. |
| 107 |
* If group_name == "/" then the root group is opened. |
| 108 |
* |
| 109 |
* \param[in] group_name The name of the group to open. |
| 110 |
* \returns True if successful. |
| 111 |
*/ |
| 112 |
bool open_group(const std::string &group_name) |
| 113 |
{ |
| 114 |
IOSS_FUNC_ENTER(m_); |
| 115 |
return open_group_nl(group_name); |
| 116 |
} |
| 117 |
|
| 118 |
/** \brief If a database type supports groups, create the specified |
| 119 |
* group as a child of the current group. |
| 120 |
* |
| 121 |
* The name of the group must not contain a '/' character. |
| 122 |
* If the command is successful, then the group will be the |
| 123 |
* active group for all subsequent writes to the database. |
| 124 |
* |
| 125 |
* \param[in] group_name The name of the subgroup to create. |
| 126 |
* \returns True if successful. |
| 127 |
*/ |
| 128 |
bool create_subgroup(const std::string &group_name) |
| 129 |
{ |
| 130 |
IOSS_FUNC_ENTER(m_); |
| 131 |
return create_subgroup_nl(group_name); |
| 132 |
} |
| 133 |
|
| 134 |
/** \brief If a database type supports groups, and if the database |
| 135 |
* contains groups, open the root group for the current group. |
| 136 |
*/ |
| 137 |
bool open_root_group() |
| 138 |
{ |
| 139 |
IOSS_FUNC_ENTER(m_); |
| 140 |
return open_root_group_nl(); |
| 141 |
} |
| 142 |
|
| 143 |
/** \brief If a database type supports groups, and if the database |
| 144 |
* contains groups, return the number of child groups for |
| 145 |
* the current group. |
| 146 |
*/ |
| 147 |
int num_child_group() const; |
| 148 |
|
| 149 |
/** \brief If a database type supports groups, open the child group |
| 150 |
* of the current group at the specified [zero-based] index |
| 151 |
* |
| 152 |
* \param[in] child_index The [zero-based] index of the subgroup to open. |
| 153 |
* \returns True if successful. |
| 154 |
*/ |
| 155 |
bool open_child_group(int child_index) |
| 156 |
{ |
| 157 |
IOSS_FUNC_ENTER(m_); |
| 158 |
return open_child_group_nl(child_index); |
| 159 |
} |
| 160 |
|
| 161 |
/** \brief If a database type supports groups, return a list of group names |
| 162 |
* |
| 163 |
* \param[in] return_full_names Flag to control return of relative |
| 164 |
* or full group name paths. |
| 165 |
* \returns True if successful. |
| 166 |
*/ |
| 167 |
Ioss::NameList groups_describe(bool return_full_names = false) const; |
| 168 |
|
| 169 |
protected: |
| 170 |
// Check to see if database state is ok... |
| 171 |
// If 'write_message' true, then output a warning message indicating the problem. |
| 172 |
// If 'error_message' non-null, then put the warning message into the string and return it. |
| 173 |
// If 'bad_count' non-null, it counts the number of processors where the file does not exist. |
| 174 |
// if ok returns false, but *bad_count==0, then the routine does not support this argument. |
| 175 |
IOSS_NODISCARD bool ok_nl(bool write_message = false, std::string *error_message = nullptr, |
| 176 |
int *bad_count = nullptr) const override; |
| 177 |
|
| 178 |
void release_memory_nl() override; |
| 179 |
|
| 180 |
bool supports_internal_change_set_nl() override; |
| 181 |
bool open_internal_change_set_nl(const std::string &set_name) override; |
| 182 |
bool open_internal_change_set_nl(int index) override; |
| 183 |
bool create_internal_change_set_nl(const std::string &set_name) override; |
| 184 |
int num_internal_change_set_nl() override; |
| 185 |
Ioss::NameList internal_change_set_describe_nl(bool return_full_names) override; |
| 186 |
|
| 187 |
bool open_root_group_nl() const; |
| 188 |
bool open_group_nl(const std::string &group_name) const; |
| 189 |
bool open_child_group_nl(int index) const; |
| 190 |
bool create_subgroup_nl(const std::string &group_name); |
| 191 |
|
| 192 |
bool begin_nl(Ioss::State state) override; |
| 193 |
bool end_nl(Ioss::State state) override; |
| 194 |
|
| 195 |
void open_state_file(int state); |
| 196 |
|
| 197 |
bool begin_state_nl(int state, double time) override; |
| 198 |
bool end_state_nl(int state, double time) override; |
| 199 |
|
| 200 |
IOSS_NODISCARD int maximum_symbol_length() const override { return maximumNameLength; } |
| 201 |
|
| 202 |
// NOTE: If this is called after write_meta_data, it will have no affect. |
| 203 |
// Also, it only affects output databases, not input. |
| 204 |
void set_maximum_symbol_length(int requested_symbol_size) override |
| 205 |
{ |
| 206 |
if (!is_input()) { |
| 207 |
maximumNameLength = requested_symbol_size; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
size_t handle_block_ids(const Ioss::EntityBlock *eb, ex_entity_type map_type, |
| 212 |
Ioss::Map &entity_map, void *ids, size_t num_to_get, |
| 213 |
size_t offset) const; |
| 214 |
|
| 215 |
void compute_block_membership_nl(Ioss::SideBlock *efblock, |
| 216 |
Ioss::NameList &block_membership) const override; |
| 217 |
|
| 218 |
IOSS_NODISCARD int int_byte_size_db() const override; |
| 219 |
void set_int_byte_size_api(Ioss::DataSize size) const override; |
| 220 |
|
| 221 |
int64_t get_field_internal(const Ioss::Region *reg, const Ioss::Field &field, void *data, |
| 222 |
size_t data_size) const override = 0; |
| 223 |
int64_t get_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field, void *data, |
| 224 |
size_t data_size) const override = 0; |
| 225 |
int64_t get_field_internal(const Ioss::EdgeBlock *nb, const Ioss::Field &field, void *data, |
| 226 |
size_t data_size) const override = 0; |
| 227 |
int64_t get_field_internal(const Ioss::FaceBlock *nb, const Ioss::Field &field, void *data, |
| 228 |
size_t data_size) const override = 0; |
| 229 |
int64_t get_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field, void *data, |
| 230 |
size_t data_size) const override = 0; |
| 231 |
int64_t get_field_internal(const Ioss::StructuredBlock *sb, const Ioss::Field &field, |
| 232 |
void *data, size_t data_size) const override = 0; |
| 233 |
int64_t get_field_internal(const Ioss::SideBlock *fb, const Ioss::Field &field, void *data, |
| 234 |
size_t data_size) const override = 0; |
| 235 |
int64_t get_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field, void *data, |
| 236 |
size_t data_size) const override = 0; |
| 237 |
int64_t get_field_internal(const Ioss::EdgeSet *ns, const Ioss::Field &field, void *data, |
| 238 |
size_t data_size) const override = 0; |
| 239 |
int64_t get_field_internal(const Ioss::FaceSet *ns, const Ioss::Field &field, void *data, |
| 240 |
size_t data_size) const override = 0; |
| 241 |
int64_t get_field_internal(const Ioss::ElementSet *ns, const Ioss::Field &field, void *data, |
| 242 |
size_t data_size) const override = 0; |
| 243 |
int64_t get_field_internal(const Ioss::SideSet *fs, const Ioss::Field &field, void *data, |
| 244 |
size_t data_size) const override = 0; |
| 245 |
int64_t get_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field, void *data, |
| 246 |
size_t data_size) const override = 0; |
| 247 |
int64_t get_field_internal(const Ioss::Assembly *as, const Ioss::Field &field, void *data, |
| 248 |
size_t data_size) const override = 0; |
| 249 |
int64_t get_field_internal(const Ioss::Blob *blob, const Ioss::Field &field, void *data, |
| 250 |
size_t data_size) const override = 0; |
| 251 |
|
| 252 |
int64_t put_field_internal(const Ioss::Region *reg, const Ioss::Field &field, void *data, |
| 253 |
size_t data_size) const override = 0; |
| 254 |
int64_t put_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field, void *data, |
| 255 |
size_t data_size) const override = 0; |
| 256 |
int64_t put_field_internal(const Ioss::EdgeBlock *nb, const Ioss::Field &field, void *data, |
| 257 |
size_t data_size) const override = 0; |
| 258 |
int64_t put_field_internal(const Ioss::FaceBlock *nb, const Ioss::Field &field, void *data, |
| 259 |
size_t data_size) const override = 0; |
| 260 |
int64_t put_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field, void *data, |
| 261 |
size_t data_size) const override = 0; |
| 262 |
int64_t put_field_internal(const Ioss::StructuredBlock *sb, const Ioss::Field &field, |
| 263 |
void *data, size_t data_size) const override = 0; |
| 264 |
int64_t put_field_internal(const Ioss::SideBlock *fb, const Ioss::Field &field, void *data, |
| 265 |
size_t data_size) const override = 0; |
| 266 |
int64_t put_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field, void *data, |
| 267 |
size_t data_size) const override = 0; |
| 268 |
int64_t put_field_internal(const Ioss::EdgeSet *ns, const Ioss::Field &field, void *data, |
| 269 |
size_t data_size) const override = 0; |
| 270 |
int64_t put_field_internal(const Ioss::FaceSet *ns, const Ioss::Field &field, void *data, |
| 271 |
size_t data_size) const override = 0; |
| 272 |
int64_t put_field_internal(const Ioss::ElementSet *ns, const Ioss::Field &field, void *data, |
| 273 |
size_t data_size) const override = 0; |
| 274 |
int64_t put_field_internal(const Ioss::SideSet *fs, const Ioss::Field &field, void *data, |
| 275 |
size_t data_size) const override = 0; |
| 276 |
int64_t put_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field, void *data, |
| 277 |
size_t data_size) const override = 0; |
| 278 |
int64_t put_field_internal(const Ioss::Assembly *as, const Ioss::Field &field, void *data, |
| 279 |
size_t data_size) const override = 0; |
| 280 |
int64_t put_field_internal(const Ioss::Blob *blob, const Ioss::Field &field, void *data, |
| 281 |
size_t data_size) const override = 0; |
| 282 |
|
| 283 |
virtual void write_meta_data(Ioss::IfDatabaseExistsBehavior behavior) = 0; |
| 284 |
void write_results_metadata(bool gather_data, Ioss::IfDatabaseExistsBehavior behavior); |
| 285 |
|
| 286 |
void openDatabase_nl() const override { (void)get_file_pointer(); } |
| 287 |
|
| 288 |
void closeDatabase_nl() const override |
| 289 |
{ |
| 290 |
free_file_pointer(); |
| 291 |
close_dw(); |
| 292 |
} |
| 293 |
|
| 294 |
IOSS_NODISCARD int get_file_pointer() const override = 0; // Open file and set exodusFilePtr. |
| 295 |
|
| 296 |
virtual int free_file_pointer() const; // Close file and set exodusFilePtr. |
| 297 |
|
| 298 |
virtual bool open_input_file(bool write_message, std::string *error_msg, int *bad_count, |
| 299 |
bool abort_if_error) const = 0; |
| 300 |
virtual bool handle_output_file(bool write_message, std::string *error_msg, int *bad_count, |
| 301 |
bool overwrite, bool abort_if_error) const = 0; |
| 302 |
void finalize_file_open() const; |
| 303 |
|
| 304 |
IOSS_NODISCARD int |
| 305 |
get_current_state() const; // Get current state with error checks and usage message. |
| 306 |
void put_qa(); |
| 307 |
void put_info(); |
| 308 |
|
| 309 |
template <typename T> |
| 310 |
void internal_gather_results_metadata(ex_entity_type type, std::vector<T *> entities); |
| 311 |
|
| 312 |
void generate_sideset_truth_table(); |
| 313 |
|
| 314 |
void output_results_names(ex_entity_type type, VariableNameMap &variables, |
| 315 |
bool reduction) const; |
| 316 |
int gather_names(VariableNameMap &variables, const Ioss::GroupingEntity *ge, int index, |
| 317 |
bool reduction); |
| 318 |
|
| 319 |
void get_nodeblocks(); |
| 320 |
void get_assemblies(); |
| 321 |
void get_blobs(); |
| 322 |
|
| 323 |
void update_block_omissions_from_assemblies(); |
| 324 |
|
| 325 |
void add_attribute_fields(Ioss::GroupingEntity *block, int attribute_count, |
| 326 |
const std::string &type); |
| 327 |
|
| 328 |
void common_write_metadata(Ioss::IfDatabaseExistsBehavior behavior); |
| 329 |
void output_other_metadata(); |
| 330 |
void output_field_metadata(); |
| 331 |
|
| 332 |
int64_t internal_add_results_fields(ex_entity_type type, Ioss::GroupingEntity *entity, |
| 333 |
int64_t position, int64_t block_count, |
| 334 |
Ioss::IntVector &truth_table, |
| 335 |
Ioex::VariableNameMap &variables); |
| 336 |
int64_t add_results_fields(Ioss::GroupingEntity *entity, int64_t position = 0); |
| 337 |
int64_t add_reduction_results_fields(Ioss::GroupingEntity *entity); |
| 338 |
void add_mesh_reduction_fields(int64_t id, Ioss::GroupingEntity *entity); |
| 339 |
std::vector<Ioss::Field> get_fields_via_field_metadata(Ioss::GroupingEntity *entity, |
| 340 |
ex_entity_type type, |
| 341 |
Ioss::NameList &names); |
| 342 |
|
| 343 |
void add_region_fields(); |
| 344 |
void store_reduction_field(const Ioss::Field &field, const Ioss::GroupingEntity *ge, |
| 345 |
void *variables) const; |
| 346 |
|
| 347 |
void get_reduction_field(const Ioss::Field &field, const Ioss::GroupingEntity *ge, |
| 348 |
void *variables) const; |
| 349 |
void write_reduction_fields() const; |
| 350 |
void read_reduction_fields() const; |
| 351 |
|
| 352 |
// Handle special output time requests -- primarily restart (cycle, keep, overwrite) |
| 353 |
// Given the global region step, return the step on the database... |
| 354 |
IOSS_NODISCARD int get_database_step(int global_step) const; |
| 355 |
|
| 356 |
void flush_database_nl() const override; |
| 357 |
void finalize_write(int state, double sim_time); |
| 358 |
|
| 359 |
mutable int m_exodusFilePtr{-1}; |
| 360 |
// If using links to file-per-state, the file pointer for "base" file. |
| 361 |
mutable int m_exodusBasePtr{-1}; |
| 362 |
|
| 363 |
mutable std::string m_groupName; |
| 364 |
|
| 365 |
mutable EntityIdSet ids_; |
| 366 |
|
| 367 |
mutable int exodusMode{EX_CLOBBER}; |
| 368 |
mutable int dbRealWordSize{8}; |
| 369 |
|
| 370 |
mutable int maximumNameLength{32}; |
| 371 |
int spatialDimension{0}; |
| 372 |
|
| 373 |
int64_t edgeCount{0}; |
| 374 |
int64_t faceCount{0}; |
| 375 |
|
| 376 |
mutable std::map<ex_entity_type, int> m_groupCount; |
| 377 |
|
| 378 |
// Communication Set Data |
| 379 |
Ioss::Int64Vector nodeCmapIds; |
| 380 |
Ioss::Int64Vector nodeCmapNodeCnts; |
| 381 |
Ioss::Int64Vector elemCmapIds; |
| 382 |
Ioss::Int64Vector elemCmapElemCnts; |
| 383 |
int64_t commsetNodeCount{0}; |
| 384 |
int64_t commsetElemCount{0}; |
| 385 |
|
| 386 |
// --- Nodal/Element/Attribute Variable Names -- Maps from sierra |
| 387 |
// field names to index of nodal/element/attribute variable in |
| 388 |
// exodusII. Note that the component suffix of the field is added on |
| 389 |
// prior to searching the map for the index. For example, given the |
| 390 |
// Sierra field 'displ' which is a VECTOR_3D, the names stored in |
| 391 |
// 'elementMap' would be 'displ_x', 'displ_y' and 'displ_z'. All |
| 392 |
// names are converted to lowercase. |
| 393 |
|
| 394 |
mutable std::map<ex_entity_type, Ioss::IntVector> m_truthTable; |
| 395 |
mutable std::map<ex_entity_type, VariableNameMap> m_variables; |
| 396 |
mutable std::map<ex_entity_type, VariableNameMap> m_reductionVariables; |
| 397 |
|
| 398 |
mutable std::map<ex_entity_type, std::map<int64_t, ValueContainer>> m_reductionValues; |
| 399 |
|
| 400 |
mutable std::vector<unsigned char> nodeConnectivityStatus; |
| 401 |
|
| 402 |
// For a database with omitted blocks, this map contains the indices of the |
| 403 |
// active nodes for each nodeset. If the nodeset is not reduced in size, |
| 404 |
// the map's vector will be empty for that nodeset. If the vector is not |
| 405 |
// empty, then some nodes on that nodeset are only connected to omitted elements. |
| 406 |
mutable std::map<std::string, Ioss::Int64Vector> activeNodeSetNodesIndex; |
| 407 |
|
| 408 |
time_t timeLastFlush{0}; |
| 409 |
int flushInterval{-1}; |
| 410 |
int m_timestepCount{0}; |
| 411 |
|
| 412 |
mutable bool fileExists{false}; // False if file has never been opened/created |
| 413 |
mutable bool minimizeOpenFiles{false}; |
| 414 |
|
| 415 |
mutable bool blockAdjacenciesCalculated{false}; // True if the lazy creation of |
| 416 |
// block adjacencies has been calculated. |
| 417 |
mutable bool nodeConnectivityStatusCalculated{ |
| 418 |
false}; // True if the lazy creation of |
| 419 |
// nodeConnectivityStatus has been calculated. |
| 420 |
}; |
| 421 |
} // namespace Ioex |