VTK  9.4.20250309
vtkIOSSReaderInternal.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include "vtkIOSSReader.h" // For enums
6#include "vtkIOSSUtilities.h" // For enums
7
8#include <vtk_ioss.h>
9
10// clang-format off
11#include VTK_IOSS(Ionit_Initializer.h)
12#include VTK_IOSS(Ioss_Assembly.h)
13#include VTK_IOSS(Ioss_DatabaseIO.h)
14#include VTK_IOSS(Ioss_EdgeBlock.h)
15#include VTK_IOSS(Ioss_EdgeSet.h)
16#include VTK_IOSS(Ioss_ElementBlock.h)
17#include VTK_IOSS(Ioss_ElementSet.h)
18#include VTK_IOSS(Ioss_ElementTopology.h)
19#include VTK_IOSS(Ioss_FaceBlock.h)
20#include VTK_IOSS(Ioss_FaceSet.h)
21#include VTK_IOSS(Ioss_IOFactory.h)
22#include VTK_IOSS(Ioss_NodeBlock.h)
23#include VTK_IOSS(Ioss_NodeSet.h)
24#include VTK_IOSS(Ioss_Region.h)
25#include VTK_IOSS(Ioss_SideBlock.h)
26#include VTK_IOSS(Ioss_SideSet.h)
27#include VTK_IOSS(Ioss_StructuredBlock.h)
28// clang-format on
29
30#include <algorithm>
31#include <array>
32#include <map>
33#include <set>
34#include <string>
35#include <vector>
36
37VTK_ABI_NAMESPACE_BEGIN
38
39class vtkCellData;
40class vtkDataAssembly;
42class vtkFieldData;
43class vtkIdTypeArray;
45class vtkPointData;
46class vtkPointSet;
50
52{
53 int ProcessCount = 0;
54 std::set<int> Ranks;
55
56 bool operator==(const DatabasePartitionInfo& other) const
57 {
58 return this->ProcessCount == other.ProcessCount && this->Ranks == other.Ranks;
59 }
60};
61
62// Opaque handle used to identify a specific Region
63using DatabaseHandle = std::pair<std::string, int>;
64
75{
76protected:
77 // It's okay to instantiate this multiple times.
78 Ioss::Init::Initializer io;
79
81
82 using DatabaseNamesType = std::map<std::string, DatabasePartitionInfo>;
86
87 std::map<std::string, std::vector<std::pair<int, double>>> DatabaseTimes;
88 std::vector<double> TimestepValues;
90
91 // a collection of names for blocks and sets in the file(s).
92 std::array<std::set<vtkIOSSUtilities::EntityNameType>, vtkIOSSReader::NUMBER_OF_ENTITY_TYPES>
95
96 // Keeps track of idx of a partitioned dataset in the output.
97 std::map<std::pair<Ioss::EntityType, std::string>, unsigned int> DatasetIndexMap;
98
99 std::map<DatabaseHandle, std::shared_ptr<Ioss::Region>> RegionMap;
100
102
105
108
109public:
111
113 : IOSSReader(reader)
114 {
115 }
116 virtual ~vtkIOSSReaderInternal() = default; // Force polymorphism
117
118 Ioss::PropertyManager DatabaseProperties;
119 std::set<std::string> FileNames;
121
122 std::set<std::string> Selectors;
123
124 const std::vector<double>& GetTimeSteps() const { return this->TimestepValues; }
126
127 void SetDisplacementMagnitude(double s) { this->DisplacementMagnitude = s; }
129
131
134 void ClearCache() { this->Cache.Clear(); }
136 void ClearCacheUnused() { this->Cache.ClearUnused(); }
138
156
172
177 vtkIOSSReader* self, const std::vector<DatabaseHandle>& dbaseHandles);
178
184
188 bool UpdateAssembly(vtkIOSSReader* self, int* tag);
189
191
197
202
206 std::vector<vtkSmartPointer<vtkDataSet>> GetDataSets(const std::string& blockname,
207 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle, int timestep,
208 vtkIOSSReader* self);
209
216 vtkSmartPointer<vtkDataSet> GetExodusEntityDataSet(const std::vector<std::string>& blockNames,
217 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle, int timestep,
218 vtkIOSSReader* self);
219
224
228 bool GetGlobalFields(vtkFieldData* fd, const DatabaseHandle& handle, int timestep);
229
233 bool HaveRestartFiles() const { return this->DatabaseTimes.size() > 1; }
234
239 std::vector<DatabaseHandle> GetDatabaseHandles(int piece, int npieces, int timestep) const;
240
244 std::string GetRawFileName(const DatabaseHandle& handle, bool shortname = false) const
245 {
246 auto iter = this->DatabaseNames.find(handle.first);
247 if (iter == this->DatabaseNames.end())
248 {
249 throw std::runtime_error("bad database handle!");
250 }
251
252 const int& fileid = handle.second;
253 auto dbasename = shortname ? vtksys::SystemTools::GetFilenameName(handle.first) : handle.first;
254
255 auto& dinfo = iter->second;
256 if (dinfo.ProcessCount > 0)
257 {
258 return Ioss::Utils::decode_filename(
259 dbasename, dinfo.ProcessCount, *std::next(dinfo.Ranks.begin(), fileid));
260 }
261 return dbasename;
262 }
263
268 int GetFileProcessor(const DatabaseHandle& handle) const
269 {
270 auto iter = this->DatabaseNames.find(handle.first);
271 if (iter == this->DatabaseNames.end())
272 {
273 throw std::runtime_error("bad database handle!");
274 }
275 const int& fileid = handle.second;
276 auto& dinfo = iter->second;
277 if (dinfo.ProcessCount > 0)
278 {
279 return *std::next(dinfo.Ranks.begin(), fileid);
280 }
281
282 // this is not a spatially partitioned file; just return 0.
283 return 0;
284 }
285
289 bool HaveCreatedRegions(const std::vector<DatabaseHandle>& dbaseHandles)
290 {
291 if (this->RegionMap.empty())
292 {
293 return false;
294 }
295 const bool allHandlesAreNew = std::all_of(dbaseHandles.begin(), dbaseHandles.end(),
296 [&](const DatabaseHandle& handle)
297 { return this->RegionMap.find(handle) == this->RegionMap.end(); });
298 return !allHandlesAreNew;
299 }
300
305 {
306 // RegionMap is where all the handles are kept. All we need to do is release
307 // them.
308 for (const auto& pair : this->RegionMap)
309 {
310 pair.second->get_database()->closeDatabase();
311 }
312 }
313
319 void ReleaseRegions() { this->RegionMap.clear(); }
320
324 void Reset()
325 {
326 this->Cache.Clear();
327 this->RegionMap.clear();
328 this->DatabaseNames.clear();
329 this->IOSSReader->RemoveAllSelections();
330 this->DatabaseNamesMTime = vtkTimeStamp();
331 this->SelectionsMTime = vtkTimeStamp();
332 this->TimestepValuesMTime = vtkTimeStamp();
333 }
334
335 void ResetDatabaseNamesMTime() { this->DatabaseNamesMTime = vtkTimeStamp(); }
336
337protected:
338 std::vector<int> GetFileIds(const std::string& dbasename, int myrank, int numRanks) const;
339 Ioss::Region* GetRegion(const std::string& dbasename, int fileid);
340 Ioss::Region* GetRegion(const DatabaseHandle& handle)
341 {
342 return this->GetRegion(handle.first, handle.second);
343 }
344
346
357 vtkSmartPointer<vtkAbstractArray> GetField(const std::string& fieldname, Ioss::Region* region,
358 const Ioss::GroupingEntity* group_entity, const DatabaseHandle& handle, int timestep,
359 vtkIdTypeArray* ids_to_extract = nullptr, const std::string& cache_key_suffix = std::string());
361
373 std::vector<std::pair<int, vtkSmartPointer<vtkCellArray>>> GetTopology(
374 const std::string& blockname, vtkIOSSReader::EntityType vtk_entity_type,
375 const DatabaseHandle& handle);
376
381 std::pair<vtkSmartPointer<vtkUnsignedCharArray>, vtkSmartPointer<vtkCellArray>> CombineTopologies(
382 const std::vector<std::pair<int, vtkSmartPointer<vtkCellArray>>>& topologies);
383
396 bool GetTopology(vtkUnstructuredGrid* grid, const std::string& blockname,
397 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle);
398
412 const std::string& blockname, const DatabaseHandle& handle);
413
427 vtkUnstructuredGrid* grid, const std::string& blockname, const DatabaseHandle& handle);
428
432 bool GetGeometry(vtkStructuredGrid* grid, const Ioss::StructuredBlock* groupEntity);
433
450 bool GetMesh(vtkUnstructuredGrid* grid, const std::string& blockname,
451 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle,
452 bool remove_unused_points);
453
465 bool GetEntityMesh(vtkUnstructuredGrid* grid, const std::vector<std::string>& blockNames,
466 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle);
467
475 bool GetMesh(vtkStructuredGrid* grid, const std::string& blockname,
476 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle);
477
483 bool GenerateEntityIdArray(vtkCellData* cd, vtkIdType numberOfCells, const std::string& blockname,
484 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle);
485
502 bool GetFields(vtkDataSetAttributes* dsa, vtkDataArraySelection* selection, Ioss::Region* region,
503 Ioss::GroupingEntity* group_entity, const DatabaseHandle& handle, int timestep,
504 bool read_ioss_ids, vtkIdTypeArray* ids_to_extract = nullptr,
505 const std::string& cache_key_suffix = std::string());
506
515 Ioss::Region* region, Ioss::GroupingEntity* group_entity, const DatabaseHandle& handle,
516 int timestep, bool read_ioss_ids, bool mergeExodusEntityBlocks = false);
517
522 bool ApplyDisplacements(vtkPointSet* grid, Ioss::Region* region,
523 Ioss::GroupingEntity* group_entity, const DatabaseHandle& handle, int timestep,
524 bool mergeExodusEntityBlocks = false);
525
529 bool GenerateFileId(vtkDataSetAttributes* cellData, vtkIdType numberOfCells,
530 Ioss::GroupingEntity* group_entity, const DatabaseHandle& handle);
531
537
538 unsigned int GetDataSetIndexForEntity(const Ioss::GroupingEntity* entity) const
539 {
540 return this->DatasetIndexMap.at(std::make_pair(entity->type(), entity->name()));
541 }
542
546 void GenerateElementAndSideIds(vtkDataSet* dataset, Ioss::SideSet* sideSet,
547 const DatabaseHandle& handle, const std::string& blockname,
548 vtkIOSSReader::EntityType vtk_entity_type);
549
551
555 std::vector<vtkSmartPointer<vtkDataSet>> GetExodusDataSets(const std::string& blockname,
556 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle, int timestep,
557 vtkIOSSReader* self);
558
559 std::vector<vtkSmartPointer<vtkDataSet>> GetCGNSDataSets(const std::string& blockname,
560 vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle& handle, int timestep,
561 vtkIOSSReader* self);
563
564 bool BuildAssembly(Ioss::Region* region, vtkDataAssembly* assembly, int root, bool add_leaves);
565
571};
572
573VTK_ABI_NAMESPACE_END
Abstract superclass for all arrays.
represent and manipulate cell attribute data
Store on/off settings for data arrays, etc.
hierarchical representation to use with vtkPartitionedDataSetCollection
represent and manipulate attribute data in a dataset
abstract class to specify dataset behavior
Definition vtkDataSet.h:165
represent and manipulate fields of data
Internal methods and state for the IOSS reader.
void SetDisplacementMagnitude(double s)
bool GetEntityMesh(vtkUnstructuredGrid *grid, const std::vector< std::string > &blockNames, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle)
Adds geometry (points) and topology (cell) information to the grid for all the entity blocks or sets ...
void ResetCacheAccessCounts()
Cache related API.
vtkIOSSUtilities::DatabaseFormatType Format
bool ReadAssemblies(vtkPartitionedDataSetCollection *output, const DatabaseHandle &handle)
Fills up the vtkDataAssembly with ioss-assemblies, if present.
void ClearCache()
Cache related API.
bool HaveCreatedRegions(const std::vector< DatabaseHandle > &dbaseHandles)
Returns if the given database handles have regions already created.
DatabaseNamesType UnfilteredDatabaseNames
bool GetFields(vtkDataSetAttributes *dsa, vtkDataArraySelection *selection, Ioss::Region *region, Ioss::GroupingEntity *group_entity, const DatabaseHandle &handle, int timestep, bool read_ioss_ids, vtkIdTypeArray *ids_to_extract=nullptr, const std::string &cache_key_suffix=std::string())
Reads selected field arrays for the given entity block or set.
bool HaveRestartFiles() const
Get if there are restart files available.
std::map< DatabaseHandle, std::shared_ptr< Ioss::Region > > RegionMap
DatabaseNamesType GenerateSubset(const DatabaseNamesType &databases, vtkIOSSReader *self)
Generate a subset based the readers current settings for FileRange and FileStride.
std::map< std::string, std::vector< std::pair< int, double > > > DatabaseTimes
vtkSmartPointer< vtkPoints > GetGeometry(const std::string &blockname, const DatabaseHandle &handle)
Get with point coordinates aka geometry read from the block with the given name (blockname).
Ioss::Region * GetRegion(const DatabaseHandle &handle)
vtkSmartPointer< vtkAbstractArray > GetField(const std::string &fieldname, Ioss::Region *region, const Ioss::GroupingEntity *group_entity, const DatabaseHandle &handle, int timestep, vtkIdTypeArray *ids_to_extract=nullptr, const std::string &cache_key_suffix=std::string())
Reads a field with name fieldname from entity block or set with chosen name (blockname) and type (vtk...
bool GetGlobalFields(vtkFieldData *fd, const DatabaseHandle &handle, int timestep)
Read global fields.
std::string GetRawFileName(const DatabaseHandle &handle, bool shortname=false) const
Useful for printing error messages etc.
bool GetQAAndInformationRecords(vtkFieldData *fd, const DatabaseHandle &handle)
Read quality assurance and information data from the file.
bool GenerateEntityIdArray(vtkCellData *cd, vtkIdType numberOfCells, const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle)
Add "id" array to the dataset using the id for the grouping entity, if any.
void ClearCacheUnused()
Cache related API.
Ioss::Region * GetRegion(const std::string &dbasename, int fileid)
std::set< std::string > FileNames
bool ApplyDisplacements(vtkPointSet *grid, Ioss::Region *region, Ioss::GroupingEntity *group_entity, const DatabaseHandle &handle, int timestep, bool mergeExodusEntityBlocks=false)
Reads node block array with displacements and then transforms the points in the grid using those disp...
bool GetNodeFields(vtkDataSetAttributes *dsa, vtkDataArraySelection *selection, Ioss::Region *region, Ioss::GroupingEntity *group_entity, const DatabaseHandle &handle, int timestep, bool read_ioss_ids, bool mergeExodusEntityBlocks=false)
This reads node fields for an entity block or set.
bool GenerateFileId(vtkDataSetAttributes *cellData, vtkIdType numberOfCells, Ioss::GroupingEntity *group_entity, const DatabaseHandle &handle)
Adds 'file_id' array to indicate which file the dataset was read from.
void GenerateElementAndSideIds(vtkDataSet *dataset, Ioss::SideSet *sideSet, const DatabaseHandle &handle, const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type)
Add field-data arrays holding side-set specifications (i.e., (cell-id, side-id) tuples) for use by th...
bool GetTopology(vtkUnstructuredGrid *grid, const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle)
Fill up the grid with connectivity information for the entity block (or set) with the given name (blo...
void ReleaseHandles()
Releases any open file handles.
bool NeedToUpdateEntityAndFieldSelections(vtkIOSSReader *self, const std::vector< DatabaseHandle > &dbaseHandles)
Checks if the entity and field selections have changed.
bool BuildAssembly(Ioss::Region *region, vtkDataAssembly *assembly, int root, bool add_leaves)
Ioss::PropertyManager DatabaseProperties
bool GetMesh(vtkUnstructuredGrid *grid, const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle, bool remove_unused_points)
Adds geometry (points) and topology (cell) information to the grid for the entity block or set chosen...
bool UpdateTimeInformation(vtkIOSSReader *self)
Read Ioss databases to generate information about timesteps / times in the databases.
vtkIOSSReaderInternal(vtkIOSSReader *reader)
std::vector< DatabaseHandle > GetDatabaseHandles(int piece, int npieces, int timestep) const
Returns the list of fileids, if any to be read for a given "piece" for the chosen timestep.
int GetFileProcessor(const DatabaseHandle &handle) const
For spatially partitioned files, this returns the partition identifier for the file identified by the...
std::vector< double > TimestepValues
bool GetMesh(vtkStructuredGrid *grid, const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle)
Reads a structured block.
unsigned int GetDataSetIndexForEntity(const Ioss::GroupingEntity *entity) const
std::pair< vtkSmartPointer< vtkUnsignedCharArray >, vtkSmartPointer< vtkCellArray > > CombineTopologies(const std::vector< std::pair< int, vtkSmartPointer< vtkCellArray > > > &topologies)
Combine a vector cell types, cell arrays pairs into a single vtkUnsignedCharArray of cell types and a...
virtual ~vtkIOSSReaderInternal()=default
std::vector< vtkSmartPointer< vtkDataSet > > GetDataSets(const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle, int timestep, vtkIOSSReader *self)
Reads datasets (meshes and fields) for the given block.
void Reset()
Clear all regions, databases etc.
bool UpdateAssembly(vtkIOSSReader *self, int *tag)
Populates the vtkDataAssembly used for block/set selection.
vtkIOSSUtilities::DatabaseFormatType GetFormat() const
std::vector< int > GetFileIds(const std::string &dbasename, int myrank, int numRanks) const
vtkDataAssembly * GetAssembly() const
vtkSmartPointer< vtkAbstractArray > ConvertFieldForVTK(vtkAbstractArray *array)
Fields like "ids" have to be vtkIdTypeArray in VTK.
bool GenerateOutput(vtkPartitionedDataSetCollection *output, vtkIOSSReader *self)
Fills up the output data-structure based on the entity blocks/sets chosen and those available.
void ReleaseRegions()
Little more aggressive than ReleaseHandles but less intense than Reset, releases all IOSS regions and...
std::vector< vtkSmartPointer< vtkDataSet > > GetExodusDataSets(const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle, int timestep, vtkIOSSReader *self)
Called by GetDataSets to process each type of dataset.
bool UpdateEntityAndFieldSelections(vtkIOSSReader *self)
Populates various vtkDataArraySelection objects on the vtkIOSSReader with names for entity-blocks,...
std::map< std::string, DatabasePartitionInfo > DatabaseNamesType
bool GetGeometry(vtkUnstructuredGrid *grid, const std::string &blockname, const DatabaseHandle &handle)
Fill up grid with point coordinates aka geometry read from the block with the given name (blockname).
std::vector< vtkSmartPointer< vtkDataSet > > GetCGNSDataSets(const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle, int timestep, vtkIOSSReader *self)
Called by GetDataSets to process each type of dataset.
DatabaseNamesType DatabaseNames
std::set< std::string > Selectors
bool UpdateDatabaseNames(vtkIOSSReader *self)
Processes filenames to populate names for Ioss databases to read.
const std::vector< double > & GetTimeSteps() const
vtkIOSSUtilities::Cache Cache
std::map< std::pair< Ioss::EntityType, std::string >, unsigned int > DatasetIndexMap
std::array< std::set< vtkIOSSUtilities::EntityNameType >, vtkIOSSReader::NUMBER_OF_ENTITY_TYPES > EntityNames
std::vector< std::pair< int, vtkSmartPointer< vtkCellArray > > > GetTopology(const std::string &blockname, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle)
Get a vector of cell arrays and their cell type for the entity block (or set) with the given name (bl...
Ioss::Init::Initializer io
vtkSmartPointer< vtkDataSet > GetExodusEntityDataSet(const std::vector< std::string > &blockNames, vtkIOSSReader::EntityType vtk_entity_type, const DatabaseHandle &handle, int timestep, vtkIOSSReader *self)
Reads datasets (meshes and fields) for the given exodus entity.
bool GetGeometry(vtkStructuredGrid *grid, const Ioss::StructuredBlock *groupEntity)
GetGeometry for vtkStructuredGrid i.e.
vtkSmartPointer< vtkDataAssembly > Assembly
Reader for IOSS (Sierra IO System)
void RemoveAllSelections()
void Clear()
Clears the cache.
void ClearUnused()
Removes all cached entries not accessed since most recent call to ResetAccessCounts.
void ResetAccessCounts()
Call this to clear internal count for hits.
dynamic, self-adjusting array of vtkIdType
Composite dataset that groups datasets as a collection.
represent and manipulate point attribute data
concrete class for storing a set of points
Definition vtkPointSet.h:98
Hold a reference to a vtkObjectBase instance.
topologically regular array of data
record modification and/or execution time
dynamic, self-adjusting array of unsigned char
dataset represents arbitrary combinations of all possible cell types
bool operator==(const DatabasePartitionInfo &other) const
std::pair< std::string, int > DatabaseHandle
int vtkIdType
Definition vtkType.h:332