Advanced Multi-Physics (AMP)
On-Line Documentation
Writer.h
Go to the documentation of this file.
1#ifndef included_AMP_Writer
2#define included_AMP_Writer
3
4#include "AMP/mesh/MeshID.h"
5#include "AMP/utils/AMP_MPI.h"
6#include "AMP/utils/Array.h"
7#include "AMP/utils/Database.h"
8
9#include <memory>
10#include <string>
11#include <vector>
12
13
14// Declare some classes
15namespace AMP::Mesh {
16class Mesh;
17class MeshIterator;
18struct MeshElementID;
19} // namespace AMP::Mesh
20namespace AMP::LinearAlgebra {
21class Vector;
22class Matrix;
23} // namespace AMP::LinearAlgebra
24
25class dummy;
26
27namespace AMP::IO {
28
29
36class Writer
37{
38public:
40 std::string type; // Writer type: Silo, HDF5, Ascii
41 std::string extension; // The primary file extension for the writer
42 bool registerMesh; // Does the writer support registering a mesh
43 bool registerVector; // Does the writer support registering a vector
44 bool registerVectorWithMesh; // Does the writer support registering a vector with a mesh
45 bool registerMatrix; // Does the writer support registering a matrix
46 bool enabled; // Is the current writer enabled
47 bool isNull; // Is the current writer a null writer
49 };
50 enum class VectorType : uint8_t { DOUBLE, SINGLE, INT };
51 enum class DecompositionType : uint8_t { SINGLE, MULTIPLE };
52
53
54public:
67 static std::shared_ptr<AMP::IO::Writer> buildWriter( std::string type,
68 AMP_MPI comm = AMP_COMM_WORLD );
69
75 static std::shared_ptr<AMP::IO::Writer> buildWriter( std::shared_ptr<AMP::Database> db );
76
77
78public:
80 virtual ~Writer() = default;
81
83 Writer( const Writer & ) = delete;
84
86 virtual WriterProperties getProperties() const = 0;
87
89 std::string getExtension() const;
90
103 virtual void setDecomposition( int decomposition );
104
106 virtual void readFile( const std::string &fname ) = 0;
107
109 virtual void writeFile( const std::string &fname, size_t iteration, double time = 0 ) = 0;
110
124 void registerMesh( std::shared_ptr<AMP::Mesh::Mesh> mesh,
125 int level = 1,
126 const std::string &path = std::string() );
127
148 virtual void registerVector( std::shared_ptr<AMP::LinearAlgebra::Vector> vec,
149 std::shared_ptr<AMP::Mesh::Mesh> mesh,
151 const std::string &name = "",
152 VectorType precision = VectorType::DOUBLE,
153 bool isStatic = false );
154
163 void registerVector( std::shared_ptr<AMP::LinearAlgebra::Vector> vec,
164 const std::string &name = "" );
165
174 void registerMatrix( std::shared_ptr<AMP::LinearAlgebra::Matrix> mat,
175 const std::string &name = "" );
176
177protected: // Internal structures
178 // Structure to hold id
179 struct GlobalID {
180 uint64_t objID; // Object id
181 uint32_t ownerRank; // Global rank of the processor that "owns" the data
182 GlobalID() : objID( 0 ), ownerRank( 0 ) {}
183 GlobalID( uint64_t obj, uint32_t rank ) : objID( obj ), ownerRank( rank ) {}
184 bool operator==( const GlobalID &rhs ) const
185 {
186 return objID == rhs.objID && ownerRank == rhs.ownerRank;
187 }
188 bool operator!=( const GlobalID &rhs ) const
189 {
190 return objID != rhs.objID || ownerRank != rhs.ownerRank;
191 }
192 bool operator<( const GlobalID &rhs ) const
193 {
194 if ( objID == rhs.objID )
195 return ownerRank < rhs.ownerRank;
196 return objID < rhs.objID;
197 }
198 bool operator<=( const GlobalID &rhs ) const
199 {
200 if ( objID == rhs.objID )
201 return ownerRank <= rhs.ownerRank;
202 return objID < rhs.objID;
203 }
204 bool operator>( const GlobalID &rhs ) const
205 {
206 if ( objID == rhs.objID )
207 return ownerRank > rhs.ownerRank;
208 return objID > rhs.objID;
209 }
210 bool operator>=( const GlobalID &rhs ) const
211 {
212 if ( objID == rhs.objID )
213 return ownerRank >= rhs.ownerRank;
214 return objID > rhs.objID;
215 }
216 };
217
218 // Structure to hold vector data
220 struct VectorData {
221 bool isStatic = false; // Is the variable static vs time
222 VectorType dataType = VectorType::DOUBLE; // Desired precision (if supported)
223 uint8_t numDOFs = 0; // Number of unknowns per point
224 GeomType type = GeomType::Nullity; // Types of variables
225 std::string name; // Vector name to store
226 std::shared_ptr<AMP::LinearAlgebra::Vector> vec; // AMP vector
227 VectorData() = default;
228 VectorData( std::shared_ptr<AMP::LinearAlgebra::Vector>, const std::string & );
229 };
230
231 // Structure to hold matrix data
232 struct MatrixData {
233 std::string name; // Matrix name to store
234 std::shared_ptr<AMP::LinearAlgebra::Matrix> mat; // AMP matrix
235 MatrixData() = default;
236 MatrixData( std::shared_ptr<AMP::LinearAlgebra::Matrix>, const std::string & );
237 };
238
239 // Structure used to hold data for a base mesh
241 GlobalID id; // Unique ID to identify the mesh
242 std::shared_ptr<AMP::Mesh::Mesh> mesh; // Pointer to the mesh
243 int rank; // Rank of the current processor on the mesh
244 int ownerRank; // Global rank of the processor that "owns" the mesh
245 std::string meshName; // Name of the mesh
246 std::string path; // Path to the mesh
247 std::string file; // File that will contain the mesh
248 std::vector<VectorData> vectors; // Vectors for each mesh
249 // Function to count the number of bytes needed to pack the data
250 size_t size() const;
251 // Function to pack the data to a byte array (note: some info may be lost)
252 void pack( char * ) const;
253 // Function to unpack the data from a byte array (note: some info may be lost)
254 static baseMeshData unpack( const char * );
255 // Constructor
256 baseMeshData() : rank( -1 ), ownerRank( -1 ) {}
257 };
258
259 // Structure used to hold data for a multimesh
261 GlobalID id; // Unique ID to identify the mesh
262 std::shared_ptr<AMP::Mesh::Mesh> mesh; // Pointer to the mesh
263 int ownerRank; // Global rank of the processor that "owns" the mesh
264 // (usually rank 0 on the mesh comm)
265 std::string name; // Name of the multimesh
266 std::vector<GlobalID> meshes; // Base mesh ids needed to construct the mesh data
267 std::vector<std::string> varName; // Vectors for each mesh
268 // Function to count the number of bytes needed to pack the data
269 size_t size() const;
270 // Function to pack the data to a byte array
271 void pack( char * ) const;
272 // Function to unpack the data from a byte array
273 static multiMeshData unpack( const char * );
274 // Constructor
276 };
277
278protected: // Protected member functions
279 // Default constructor
280 Writer() = default;
281
282 // Given a filename, strip the directory information and create the directories if needed
283 void createDirectories( const std::string &filename );
284
285 // Function to determine which base mesh ids to register a vector with
286 static std::vector<AMP::Mesh::MeshID> getMeshIDs( std::shared_ptr<AMP::Mesh::Mesh> mesh );
287
288 // Get the node coordinates and elements for a mesh
289 static void getNodeElemList( std::shared_ptr<const AMP::Mesh::Mesh> mesh,
290 const AMP::Mesh::MeshIterator &elements,
292 AMP::Array<int> &nodelist,
293 std::vector<AMP::Mesh::MeshElementID> &nodelist_ids );
294
295 // Register the mesh returning the ids of all registered base meshes
296 void registerMesh2( std::shared_ptr<AMP::Mesh::Mesh> mesh,
297 int level,
298 const std::string &path,
299 std::set<GlobalID> &base_ids );
300
301 // Function to syncronize multimesh data
302 std::tuple<std::vector<multiMeshData>, std::map<GlobalID, baseMeshData>>
303 syncMultiMeshData( int root = -1 ) const;
304
305 // Get id from a communicator
306 GlobalID getID( const AMP_MPI &comm ) const;
307
308 // Synchronize the vectors (call makeConsistent)
310
311 // Synchronize mesh data
312 template<class TYPE>
313 void syncData( std::vector<TYPE> &data, int root ) const;
314
315protected: // Internal data
316 // The comm of the writer
318
319 // The decomposition to use
321
322 // List of all meshes and their ids
323 std::map<GlobalID, baseMeshData> d_baseMeshes;
324 std::map<GlobalID, multiMeshData> d_multiMeshes;
325
326 // List of all independent vectors that have been registered
327 std::map<GlobalID, VectorData> d_vectors;
328
329 // List of all independent matrices that have been registered
330 std::map<GlobalID, MatrixData> d_matrices;
331
332 // List of all vectors that have been registered (work on removing)
333 std::vector<std::shared_ptr<AMP::LinearAlgebra::Vector>> d_vectorsMesh;
334};
335
336
337} // namespace AMP::IO
338
339#endif
#define AMP_COMM_WORLD
Definition AMP_MPI.h:32
Provides C++ wrapper around MPI routines.
Definition AMP_MPI.h:63
A class used to abstract away reading/writing files.
Definition Writer.h:37
virtual void registerVector(std::shared_ptr< AMP::LinearAlgebra::Vector > vec, std::shared_ptr< AMP::Mesh::Mesh > mesh, AMP::Mesh::GeomType type, const std::string &name="", VectorType precision=VectorType::DOUBLE, bool isStatic=false)
Function to register a vector.
std::map< GlobalID, baseMeshData > d_baseMeshes
Definition Writer.h:323
virtual void setDecomposition(int decomposition)
Function to set the file decomposition.
Writer()=default
std::map< GlobalID, MatrixData > d_matrices
Definition Writer.h:330
virtual void writeFile(const std::string &fname, size_t iteration, double time=0)=0
Function to write a file.
std::tuple< std::vector< multiMeshData >, std::map< GlobalID, baseMeshData > > syncMultiMeshData(int root=-1) const
static std::shared_ptr< AMP::IO::Writer > buildWriter(std::shared_ptr< AMP::Database > db)
Function to build a writer.
void registerMesh(std::shared_ptr< AMP::Mesh::Mesh > mesh, int level=1, const std::string &path=std::string())
Function to register a mesh.
void syncData(std::vector< TYPE > &data, int root) const
void registerVector(std::shared_ptr< AMP::LinearAlgebra::Vector > vec, const std::string &name="")
Function to register a vector.
GlobalID getID(const AMP_MPI &comm) const
static void getNodeElemList(std::shared_ptr< const AMP::Mesh::Mesh > mesh, const AMP::Mesh::MeshIterator &elements, AMP::Array< double > *x, AMP::Array< int > &nodelist, std::vector< AMP::Mesh::MeshElementID > &nodelist_ids)
Writer(const Writer &)=delete
Delete copy constructor.
std::map< GlobalID, multiMeshData > d_multiMeshes
Definition Writer.h:324
void createDirectories(const std::string &filename)
void registerMesh2(std::shared_ptr< AMP::Mesh::Mesh > mesh, int level, const std::string &path, std::set< GlobalID > &base_ids)
AMP_MPI d_comm
Definition Writer.h:317
std::string getExtension() const
Function to return the file extension.
DecompositionType d_decomposition
Definition Writer.h:320
virtual WriterProperties getProperties() const =0
Function to get the writer properties.
static std::shared_ptr< AMP::IO::Writer > buildWriter(std::string type, AMP_MPI comm=AMP_COMM_WORLD)
Function to build a writer.
std::map< GlobalID, VectorData > d_vectors
Definition Writer.h:327
std::vector< std::shared_ptr< AMP::LinearAlgebra::Vector > > d_vectorsMesh
Definition Writer.h:333
virtual ~Writer()=default
Default destructor.
void registerMatrix(std::shared_ptr< AMP::LinearAlgebra::Matrix > mat, const std::string &name="")
Function to register a matrix.
virtual void readFile(const std::string &fname)=0
Function to read a file.
static std::vector< AMP::Mesh::MeshID > getMeshIDs(std::shared_ptr< AMP::Mesh::Mesh > mesh)
A class used to iterate over elements in a Mesh.
std::string filename(const std::string &filename)
Return the filename (strip the path)
std::string path(const std::string &filename)
Return the path to the file.
GeomType
Enumeration for basic mesh-based quantities.
Definition MeshID.h:12
bool operator<=(const GlobalID &rhs) const
Definition Writer.h:198
bool operator<(const GlobalID &rhs) const
Definition Writer.h:192
bool operator!=(const GlobalID &rhs) const
Definition Writer.h:188
bool operator==(const GlobalID &rhs) const
Definition Writer.h:184
GlobalID(uint64_t obj, uint32_t rank)
Definition Writer.h:183
bool operator>(const GlobalID &rhs) const
Definition Writer.h:204
bool operator>=(const GlobalID &rhs) const
Definition Writer.h:210
std::shared_ptr< AMP::LinearAlgebra::Matrix > mat
Definition Writer.h:234
MatrixData(std::shared_ptr< AMP::LinearAlgebra::Matrix >, const std::string &)
VectorData(std::shared_ptr< AMP::LinearAlgebra::Vector >, const std::string &)
std::shared_ptr< AMP::LinearAlgebra::Vector > vec
Definition Writer.h:226
std::shared_ptr< AMP::Mesh::Mesh > mesh
Definition Writer.h:242
static baseMeshData unpack(const char *)
std::vector< VectorData > vectors
Definition Writer.h:248
std::shared_ptr< AMP::Mesh::Mesh > mesh
Definition Writer.h:262
std::vector< std::string > varName
Definition Writer.h:267
static multiMeshData unpack(const char *)
std::vector< GlobalID > meshes
Definition Writer.h:266



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:40.
Comments on this page