Medusa  1.1
Coordinate Free Mehless Method implementation
mm::HDF Class Reference

#include <HDF_fwd.hpp>

Detailed Description

Simplified HDF5 I/O utilities.

This class is a wrapper around the HDF5 C library, for simplified reading and writing.

This class represents a HDF reader and writer: it knows which file (open or closed) and which group (open or closed) are the ones that are to be read from/written to. Any calls to read* or write* methods write to that group in that file. The file and group can be opened, closed or changed during the lifetime of the object, with user's responsibility being that they are open when writing to/reading from it.

In addition atomic() method can be used to perform open/read/close or open/write/close cycle in one line.

Raw C identifiers are exposed with getFileID() and getGroupID() methods and can be used to perform any action not directly supported by this class.

All write methods create the datasets or attributes required. They also have an overwrite parameter which is set to false by default. This means that any attempt to write to an existing object will fail an assertion to prevent accidental deletions of existing data. This can be disabled by setting overwrite = true, allowing one to overwrite existing data.

All open objects are closed upon destruction.

Note
To enable support for Eigen types, HDF_Eigen.hpp must be included.

Usage example:

// open new empty file and group '/' automatically
HDF hdf("test/testdata/filename.h5", HDF::DESTROY);
hdf.writeStringAttribute("text", "This is the content"); // written to group '/'
hdf.openGroup("group1");
std::vector<double> a = {1.2, -4.5, 4.5};
hdf.writeDoubleArray("a", a);
hdf.close();
hdf.reopen(); // reopen previously closed file and group
hdf.openGroup("/");
std::string s = hdf.readStringAttribute("text");
hdf.writeStringAttribute("text", "New content", true); // enable overwrite
hdf.setFilename("test/testdata/new_file.h5");
hdf.setGroupName("group_name"); // set current group name and filename, but do not open yet
std::vector<std::vector<double>> b(5, a); // written as dataset of size 5x3
hdf.atomic().writeFloat2DArray("b", b); // opens, writes and closes, the value is cast to float
Eigen::MatrixXd M(5, 3);
M << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15;
hdf.atomic().writeEigen("M", M);
auto M2 = hdf.atomic().readEigen("M"); // opens, reads, closes
std::cout << hdf << std::endl;
// Timer, DomainDiscretization and XML classes can also be written directly
Examples
test/end2end/poisson_explicit.cpp.

Definition at line 75 of file HDF_fwd.hpp.

Public Member Functions

 HDF ()
 Construct an empty HDF reader. More...
 
 HDF (std::string filename, Mode mode=APPEND)
 Construct a HDF reader and open filename with given mode. More...
 
 ~HDF ()
 Closes all opened objects, by using close(). More...
 
void open (const std::string &filename, const std::string &group_name="/", Mode mode=APPEND)
 Opens given file and group. More...
 
void openFile (const std::string &filename, Mode mode=APPEND)
 Opens a given file with a given mode. More...
 
void reopenFile (Mode mode=APPEND)
 Reopens the current file. More...
 
void openGroup (std::string group_name)
 Open given group, closing any previously opened group(). More...
 
void reopenGroup ()
 Reopens current group. More...
 
void reopen (Mode mode=APPEND)
 Reopens current file and group. More...
 
bool isFileOpen () const
 Returns true is currently specified file is open and false otherwise. More...
 
bool isGroupOpen () const
 Returns true is currently specified group is open and false otherwise. More...
 
void flush () const
 Flush opened file, if it is open. More...
 
void closeGroup () const
 Closes current group, if open. More...
 
void closeFile () const
 Closes current file and all objects associated with it (e.g. the group), if open. More...
 
void close () const
 Closes all open objects. Alias of closeFile(). More...
 
const std::string & filename () const
 Get current filename. More...
 
const std::string & fileName () const
 Alias of filename(). More...
 
void setFilename (const std::string &filename)
 Sets new filename without opening the new file. Any previously opened file is closed. More...
 
void setFileName (const std::string &filename)
 Alias of setFilename(). More...
 
const std::string & groupName () const
 Get current group name. More...
 
void setGroupName (const std::string &group_name)
 Set new group name without opening the new group. Any previously opened group is closed. More...
 
template<class T >
readAttribute (const std::string &attr_name) const
 Read attribute given by attr_name. More...
 
template<class T >
void writeAttribute (const std::string &attr_name, const T &value, const hid_t &type, bool overwrite=false) const
 Writes given attribute with given value and type to file. More...
 
template<typename T >
std::pair< std::vector< hsize_t >, std::vector< T > > readLinearArray (const std::string &dataset_name) const
 Read given dataset into linear dim-D array. More...
 
template<typename T >
std::vector< T > read1DArray (const std::string &dataset_name) const
 Read 1D dataset given by dataset_name. More...
 
template<typename T >
std::vector< std::vector< T > > read2DArray (const std::string &dataset_name) const
 Read given dataset into 2D array of vectors. More...
 
template<typename T >
std::vector< std::vector< std::vector< T > > > read3DArray (const std::string &dataset_name) const
 Read given dataset into 3D array of vectors. More...
 
template<int dim, typename T >
void writeLinearArray (const std::string &dataset_name, const T *value, const std::array< hsize_t, dim > &sizes, hid_t type, bool overwrite) const
 Writes a dim-dimensional dataset stored in 1D array in col-major fashion. More...
 
template<typename T , typename array_t >
void write1DArray (const std::string &dataset_name, const array_t &value, hid_t type, bool overwrite) const
 Writes given 1D array with given value and type to file. More...
 
template<typename T , class array_t >
void write2DArray (const std::string &dataset_name, const array_t &value, hid_t type, bool overwrite) const
 Writes given 2D array of given value and type to file. More...
 
template<typename T , class array_t >
void write3DArray (const std::string &dataset_name, const array_t &value, hid_t type, bool overwrite) const
 Writes given 3D array of given value and type to file. More...
 
int readIntAttribute (const std::string &attr_name) const
 Reads int attribute. More...
 
bool readBoolAttribute (const std::string &attr_name) const
 Reads bool attribute. More...
 
double readDoubleAttribute (const std::string &attr_name) const
 Reads double attribute. More...
 
float readFloatAttribute (const std::string &attr_name) const
 Reads float attribute. More...
 
std::string readStringAttribute (const std::string &attr_name) const
 Reads non-null-terminated string attribute. More...
 
std::string readNullTerminatedStringAttribute (const std::string &attr_name) const
 Reads null-terminated string attribute. More...
 
void writeIntAttribute (const std::string &attr_name, int value, bool overwrite=false) const
 Write int attribute. More...
 
void writeBoolAttribute (const std::string &attr_name, bool value, bool overwrite=false) const
 Write bool attribute. More...
 
void writeDoubleAttribute (const std::string &attr_name, double value, bool overwrite=false) const
 Write double attribute. More...
 
void writeFloatAttribute (const std::string &attr_name, float value, bool overwrite=false) const
 Write float attribute. More...
 
void writeStringAttribute (const std::string &attr_name, const std::string &value, bool overwrite=false) const
 Write string attribute. More...
 
std::vector< int > readIntArray (const std::string &dataset_name) const
 Read given dataset into vector of ints. More...
 
std::vector< double > readDoubleArray (const std::string &dataset_name) const
 Read given dataset into vector of doubles. More...
 
std::vector< float > readFloatArray (const std::string &dataset_name) const
 Read given dataset into vector of floats. More...
 
template<typename array_t >
void writeIntArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 1D array of ints. More...
 
template<typename array_t >
void writeDoubleArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 1D array of doubles. More...
 
template<typename array_t >
void writeFloatArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 1D array of floatss. More...
 
std::vector< std::vector< int > > readInt2DArray (const std::string &dataset_name) const
 Read given dataset into 2D vector of ints. More...
 
std::vector< std::vector< double > > readDouble2DArray (const std::string &dataset_name) const
 Read given dataset into 2D vector of doubles. More...
 
std::vector< std::vector< float > > readFloat2DArray (const std::string &dataset_name) const
 Read given dataset into 2D vector of floats. More...
 
template<typename array_t >
void writeInt2DArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 2D array of ints. More...
 
template<typename array_t >
void writeDouble2DArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 2D array of doubles. More...
 
template<typename array_t >
void writeFloat2DArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 2D array of floats. More...
 
std::vector< std::vector< std::vector< int > > > readInt3DArray (const std::string &dataset_name) const
 Read given dataset into 3D vector of ints. More...
 
std::vector< std::vector< std::vector< double > > > readDouble3DArray (const std::string &dataset_name) const
 Read given dataset into 3D vector of doubles. More...
 
std::vector< std::vector< std::vector< float > > > readFloat3DArray (const std::string &dataset_name) const
 Read given dataset into 3D vector of floats. More...
 
template<typename array_t >
void writeInt3DArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 3D array of ints. More...
 
template<typename array_t >
void writeDouble3DArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 3D array of doubles. More...
 
template<typename array_t >
void writeFloat3DArray (const std::string &dataset_name, const array_t &value, bool overwrite=false) const
 Write given value as a 3D array of floats. More...
 
template<typename scalar_t = double>
Eigen::Matrix< scalar_t, -1, -1, 0, -1, -1 > readEigen (const std::string &dataset_name) const
 Reads data as an Eigen matrix of type scalar_t. More...
 
template<typename Derived >
void writeEigen (const std::string &dataset_name, const Eigen::MatrixBase< Derived > &value, bool overwrite=false) const
 Write given Eigen expression as a 2D array of its type (either int, double or float). More...
 
template<typename SparseMatrixType >
void writeSparseMatrix (const std::string &name, SparseMatrixType &matrix, bool one_based=true, bool overwrite=false)
 Writes a sparse matrix. More...
 
template<typename domain_t >
void writeDomain (const std::string &name, const domain_t &domain, bool overwrite=false)
 Writes given domain discretization to file. More...
 
template<typename timer_t >
void writeTimer (const std::string &name, const timer_t &timer, bool overwrite=false)
 Writes given timer to file. More...
 
template<typename conf_t >
void writeXML (const std::string &name, const conf_t &conf, bool overwrite=false)
 Writes given XML object to file. More...
 
HDF atomic () const
 Allows for "atomic" read and write operations to HDF5 files. More...
 
hid_t getFileID () const
 Return raw HDF C file identifier. More...
 
hid_t getGroupID () const
 Return raw HDF C group identifier. More...
 
Members members () const
 Returns an object with categorized names of all members of current group. More...
 
std::vector< std::string > groups () const
 Returns names of all subgroups in current group in alphabetical order. More...
 
std::vector< std::string > datasets () const
 Returns names of all datasets in current group in alphabetical order. More...
 

Public Types

enum  Mode : unsigned { APPEND = 256, DESTROY = 128, READONLY = 64 }
 Possible file opening modes. More...
 

Classes

struct  Members
 Holds categorized names for all members of a group. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const HDF &hdf)
 Output basic info about HDF reader, such as current group and file. More...
 

Private Member Functions

template<typename T , class array_t >
void write2DArray (const std::string &, const array_t &, hid_t, bool, std::false_type) const
 Overload for static_assert for Eigen due to different size method. More...
 
template<typename T , class array_t >
void write2DArray (const std::string &, const array_t &, hid_t, bool, std::true_type) const
 Overload for static_assert for Eigen due to different size method. More...
 

Static Private Member Functions

static std::string str (Mode mode)
 Convert HDF::Mode enum to string. More...
 
static hid_t openFileHelper (const std::string &filename, Mode mode)
 Opens given file with given mode. More...
 
static herr_t memberIterateCallback (hid_t loc_id, const char *name, const H5L_info_t *, void *data)
 Callback required by HDF Literate function when getting a list of members. More...
 

Private Attributes

std::string filename_
 Current file name. More...
 
std::string group_name_
 Current group name. More...
 
hid_t file
 Currently open file identifier. More...
 
hid_t group
 Currently open group identifier. More...
 

Member Enumeration Documentation

◆ Mode

enum mm::HDF::Mode : unsigned

Possible file opening modes.

Enumerator
APPEND 

Appends to the existing contents, if any exist.

DESTROY 

Removes old contents, if any exist.

READONLY 

Read only open, the file must exist.

Definition at line 84 of file HDF_fwd.hpp.

Constructor & Destructor Documentation

◆ HDF() [1/2]

mm::HDF::HDF ( )
inline

Construct an empty HDF reader.

Definition at line 95 of file HDF_fwd.hpp.

◆ HDF() [2/2]

mm::HDF::HDF ( std::string  filename,
Mode  mode = APPEND 
)
inlineexplicit

Construct a HDF reader and open filename with given mode.

It also opens group / by default, so it is immediately ready for reading / writing.

Exceptions
Assertionfails if mode was READONLY and the file can not be read.

Definition at line 101 of file HDF_fwd.hpp.

◆ ~HDF()

mm::HDF::~HDF ( )
inline

Closes all opened objects, by using close().

See also
close

Definition at line 107 of file HDF_fwd.hpp.

Member Function Documentation

◆ atomic()

HDF mm::HDF::atomic ( ) const

Allows for "atomic" read and write operations to HDF5 files.

This means that read and write functions open the file and group, before reading/writing and close them afterwards. The filename and group name must be set before use.

See also
setFilename, setGroupName
Examples
test/end2end/poisson_explicit.cpp.

Definition at line 250 of file HDF.cpp.

◆ close()

void mm::HDF::close ( ) const
inline

Closes all open objects. Alias of closeFile().

Examples
test/end2end/poisson_explicit.cpp.

Definition at line 164 of file HDF_fwd.hpp.

◆ closeFile()

void mm::HDF::closeFile ( ) const

Closes current file and all objects associated with it (e.g. the group), if open.

Definition at line 243 of file HDF.cpp.

◆ closeGroup()

void mm::HDF::closeGroup ( ) const

Closes current group, if open.

Definition at line 236 of file HDF.cpp.

◆ datasets()

std::vector<std::string> mm::HDF::datasets ( ) const
inline

Returns names of all datasets in current group in alphabetical order.

See also
members

Definition at line 558 of file HDF_fwd.hpp.

◆ filename()

const std::string& mm::HDF::filename ( ) const
inline

Get current filename.

Definition at line 166 of file HDF_fwd.hpp.

◆ fileName()

const std::string& mm::HDF::fileName ( ) const
inline

Alias of filename().

Definition at line 168 of file HDF_fwd.hpp.

◆ flush()

void mm::HDF::flush ( ) const

Flush opened file, if it is open.

Definition at line 229 of file HDF.cpp.

◆ getFileID()

hid_t mm::HDF::getFileID ( ) const
inline

Return raw HDF C file identifier.

Definition at line 523 of file HDF_fwd.hpp.

◆ getGroupID()

hid_t mm::HDF::getGroupID ( ) const
inline

Return raw HDF C group identifier.

Definition at line 525 of file HDF_fwd.hpp.

◆ groupName()

const std::string& mm::HDF::groupName ( ) const
inline

Get current group name.

Definition at line 174 of file HDF_fwd.hpp.

◆ groups()

std::vector<std::string> mm::HDF::groups ( ) const
inline

Returns names of all subgroups in current group in alphabetical order.

See also
members

Definition at line 555 of file HDF_fwd.hpp.

◆ isFileOpen()

bool mm::HDF::isFileOpen ( ) const
inline

Returns true is currently specified file is open and false otherwise.

Definition at line 154 of file HDF_fwd.hpp.

◆ isGroupOpen()

bool mm::HDF::isGroupOpen ( ) const
inline

Returns true is currently specified group is open and false otherwise.

Definition at line 156 of file HDF_fwd.hpp.

◆ memberIterateCallback()

herr_t mm::HDF::memberIterateCallback ( hid_t  loc_id,
const char *  name,
const H5L_info_t *  ,
void *  data 
)
staticprivate

Callback required by HDF Literate function when getting a list of members.

Definition at line 277 of file HDF.cpp.

◆ members()

HDF::Members mm::HDF::members ( ) const

Returns an object with categorized names of all members of current group.

The names within each category are guaranteed to be in alphabetical order.

Definition at line 298 of file HDF.cpp.

◆ open()

void mm::HDF::open ( const std::string &  filename,
const std::string &  group_name = "/",
Mode  mode = APPEND 
)
inline

Opens given file and group.

Parameters
filenameName of the file to open.
group_nameName of the group to open.
modeSpecifies the mode to open the file in, see HDF::Mode.

Definition at line 120 of file HDF_fwd.hpp.

◆ openFile()

void mm::HDF::openFile ( const std::string &  filename,
Mode  mode = APPEND 
)
inline

Opens a given file with a given mode.

Any previously opened file is closed. No group is opened.

Definition at line 130 of file HDF_fwd.hpp.

◆ openFileHelper()

hid_t mm::HDF::openFileHelper ( const std::string &  filename,
HDF::Mode  mode 
)
staticprivate

Opens given file with given mode.

See also
open, openFile, openGroup

Definition at line 21 of file HDF.cpp.

◆ openGroup()

void mm::HDF::openGroup ( std::string  group_name)

Open given group, closing any previously opened group().

Parameters
group_nameName of the group to open. The name must be / separated and can be either absolute or relative. Absolute names such as /a/b/c open groups staring from / group and relative names such as a/b/c open groups staring from current group. The groups are created automatically if they do not exist.

Definition at line 48 of file HDF.cpp.

◆ read1DArray()

template<typename T >
std::vector< T > mm::HDF::read1DArray ( const std::string &  dataset_name) const

Read 1D dataset given by dataset_name.

Parameters
dataset_nameName of the dataset.
Returns
A vector containing all dataset elements.
Exceptions
Assertionfails if the dataset does not exists or cannot be read.
Warning
If the element type is not compatible with type T, the function will still attempt to read it the behavior is undefined. Most often the return value is garbage.

Definition at line 95 of file HDF.hpp.

◆ read2DArray()

template<typename T >
std::vector< std::vector< T > > mm::HDF::read2DArray ( const std::string &  dataset_name) const

Read given dataset into 2D array of vectors.

Parameters
dataset_nameName of the dataset.
Returns
A vector of vectors containing all dataset elements.
Exceptions
Assertionfails if the dataset does not exists or cannot be read.
Warning
If the element type is not compatible with type T, the function will still attempt to read it the behavior is undefined. Most often the return value is garbage.

Definition at line 105 of file HDF.hpp.

◆ read3DArray()

template<typename T >
std::vector< std::vector< std::vector< T > > > mm::HDF::read3DArray ( const std::string &  dataset_name) const

Read given dataset into 3D array of vectors.

Parameters
dataset_nameName of the dataset.
Returns
A vector of vectors containing all dataset elements.
Exceptions
Assertionfails if the dataset does not exists or cannot be read.
Warning
If the element type is not compatible with type T, the function will still attempt to read it the behavior is undefined. Most often the return value is garbage.

Definition at line 123 of file HDF.hpp.

◆ readAttribute()

template<class T >
T mm::HDF::readAttribute ( const std::string &  attr_name) const

Read attribute given by attr_name.

Template Parameters
TThe type to which to read the attribute.
Parameters
attr_nameName of the attribute
Returns
Attribute value, converted to type T.
Exceptions
Assertionfails if the attribute does not exists or cannot be read.
Warning
If the attribute type is not compatible with type T, the function will still attempt to read it the behavior is undefined. Most often the return value is garbage.
Note
This function is specialized for std::string.

Definition at line 16 of file HDF.hpp.

◆ readBoolAttribute()

bool mm::HDF::readBoolAttribute ( const std::string &  attr_name) const

Reads bool attribute.

See also
readAttribute

Definition at line 122 of file HDF.cpp.

◆ readDouble2DArray()

std::vector< std::vector< double > > mm::HDF::readDouble2DArray ( const std::string &  dataset_name) const

Read given dataset into 2D vector of doubles.

See also
read2DArray

Definition at line 206 of file HDF.cpp.

◆ readDouble3DArray()

std::vector< std::vector< std::vector< double > > > mm::HDF::readDouble3DArray ( const std::string &  dataset_name) const

Read given dataset into 3D vector of doubles.

See also
read2DArray

Definition at line 220 of file HDF.cpp.

◆ readDoubleArray()

std::vector< double > mm::HDF::readDoubleArray ( const std::string &  dataset_name) const

Read given dataset into vector of doubles.

See also
readArray

Definition at line 194 of file HDF.cpp.

◆ readDoubleAttribute()

double mm::HDF::readDoubleAttribute ( const std::string &  attr_name) const

Reads double attribute.

See also
readAttribute

Definition at line 126 of file HDF.cpp.

◆ readEigen()

template<typename scalar_t = double>
Eigen::Matrix<scalar_t, -1, -1, 0, -1, -1> mm::HDF::readEigen ( const std::string &  dataset_name) const

Reads data as an Eigen matrix of type scalar_t.

Template Parameters
scalar_tThe of the data to read. Set to double by default.
Parameters
dataset_nameName of the dataset to read from.
Warning
If the supplied data type does not match the actual type of the data, the behavior is undefined. Most likely, the returned matrix will have garbage contents.
Note
To use this function, the file HDF_Eigen.hpp must be included.

◆ readFloat2DArray()

std::vector< std::vector< float > > mm::HDF::readFloat2DArray ( const std::string &  dataset_name) const

Read given dataset into 2D vector of floats.

See also
read2DArray

Definition at line 210 of file HDF.cpp.

◆ readFloat3DArray()

std::vector< std::vector< std::vector< float > > > mm::HDF::readFloat3DArray ( const std::string &  dataset_name) const

Read given dataset into 3D vector of floats.

See also
read2DArray

Definition at line 225 of file HDF.cpp.

◆ readFloatArray()

std::vector< float > mm::HDF::readFloatArray ( const std::string &  dataset_name) const

Read given dataset into vector of floats.

See also
readArray

Definition at line 198 of file HDF.cpp.

◆ readFloatAttribute()

float mm::HDF::readFloatAttribute ( const std::string &  attr_name) const

Reads float attribute.

See also
readAttribute

Definition at line 130 of file HDF.cpp.

◆ readInt2DArray()

std::vector< std::vector< int > > mm::HDF::readInt2DArray ( const std::string &  dataset_name) const

Read given dataset into 2D vector of ints.

See also
read2DArray

Definition at line 202 of file HDF.cpp.

◆ readInt3DArray()

std::vector< std::vector< std::vector< int > > > mm::HDF::readInt3DArray ( const std::string &  dataset_name) const

Read given dataset into 3D vector of ints.

See also
read2DArray

Definition at line 215 of file HDF.cpp.

◆ readIntArray()

std::vector< int > mm::HDF::readIntArray ( const std::string &  dataset_name) const

Read given dataset into vector of ints.

See also
readArray

Definition at line 190 of file HDF.cpp.

◆ readIntAttribute()

int mm::HDF::readIntAttribute ( const std::string &  attr_name) const

Reads int attribute.

See also
readAttribute

Definition at line 118 of file HDF.cpp.

◆ readLinearArray()

template<typename T >
std::pair< std::vector< hsize_t >, std::vector< T > > mm::HDF::readLinearArray ( const std::string &  dataset_name) const

Read given dataset into linear dim-D array.

Parameters
dataset_nameName of the dataset.
Returns
A vector containing all dataset elements in col-major fashion.
Exceptions
Assertionfails if the dataset does not exists or cannot be read.
Warning
If the element type is not compatible with type T, the function will still attempt to read it the behavior is undefined. Most often the return value is garbage.

Definition at line 68 of file HDF.hpp.

◆ readNullTerminatedStringAttribute()

std::string mm::HDF::readNullTerminatedStringAttribute ( const std::string &  attr_name) const

Reads null-terminated string attribute.

See also
readAttribute

Definition at line 113 of file HDF.cpp.

◆ readStringAttribute()

std::string mm::HDF::readStringAttribute ( const std::string &  attr_name) const

Reads non-null-terminated string attribute.

Any null characters are read as part of the string.

See also
readAttribute

Definition at line 134 of file HDF.cpp.

◆ reopen()

void mm::HDF::reopen ( Mode  mode = APPEND)
inline

Reopens current file and group.

Examples
test/end2end/poisson_explicit.cpp.

Definition at line 152 of file HDF_fwd.hpp.

◆ reopenFile()

void mm::HDF::reopenFile ( Mode  mode = APPEND)
inline

Reopens the current file.

Definition at line 137 of file HDF_fwd.hpp.

◆ reopenGroup()

void mm::HDF::reopenGroup ( )
inline

Reopens current group.

Definition at line 148 of file HDF_fwd.hpp.

◆ setFilename()

void mm::HDF::setFilename ( const std::string &  filename)
inline

Sets new filename without opening the new file. Any previously opened file is closed.

Definition at line 170 of file HDF_fwd.hpp.

◆ setFileName()

void mm::HDF::setFileName ( const std::string &  filename)
inline

Alias of setFilename().

Definition at line 172 of file HDF_fwd.hpp.

◆ setGroupName()

void mm::HDF::setGroupName ( const std::string &  group_name)

Set new group name without opening the new group. Any previously opened group is closed.

Definition at line 262 of file HDF.cpp.

◆ str()

std::string mm::HDF::str ( HDF::Mode  mode)
staticprivate

Convert HDF::Mode enum to string.

Definition at line 12 of file HDF.cpp.

◆ write1DArray()

template<typename T , typename array_t >
void mm::HDF::write1DArray ( const std::string &  dataset_name,
const array_t &  value,
hid_t  type,
bool  overwrite 
) const

Writes given 1D array with given value and type to file.

Template Parameters
TType to which the elements of value are converted to.
array_tType of the given array. It must support operator[] and .size().
Parameters
dataset_nameName of the attribute.
valueValue to be written
typeHDF5 type of the elements to be written.
overwriteIf overwriting existing datasets is allowed. If an attribute with the same name is attempted to be written, it must have the same dimensions and type as current value. Otherwise the overwrite will fail.

Definition at line 190 of file HDF.hpp.

◆ write2DArray() [1/3]

template<typename T , class array_t >
void mm::HDF::write2DArray ( const std::string &  dataset_name,
const array_t &  value,
hid_t  type,
bool  overwrite,
std::false_type   
) const
private

Overload for static_assert for Eigen due to different size method.

Definition at line 199 of file HDF.hpp.

◆ write2DArray() [2/3]

template<typename T , class array_t >
void mm::HDF::write2DArray ( const std::string &  ,
const array_t &  ,
hid_t  ,
bool  ,
std::true_type   
) const
inlineprivate

Overload for static_assert for Eigen due to different size method.

Definition at line 309 of file HDF_fwd.hpp.

◆ write2DArray() [3/3]

template<typename T , class array_t >
void mm::HDF::write2DArray ( const std::string &  dataset_name,
const array_t &  value,
hid_t  type,
bool  overwrite 
) const
inline

Writes given 2D array of given value and type to file.

Template Parameters
TType of array_t elements
array_tType of the given 2D array. It must support .size(), [i].size() and [i][j] operations.
Parameters
dataset_nameName of the dataset.
valueValue to be written.
typeHDF5 type of the elements to be written.
overwriteIf overwriting existing datasets is allowed. If an attribute with the same name is attempted to be written, it must have the same dimensions and type as current value. Otherwise the overwrite will fail.

Definition at line 296 of file HDF_fwd.hpp.

◆ write3DArray()

template<typename T , class array_t >
void mm::HDF::write3DArray ( const std::string &  dataset_name,
const array_t &  value,
hid_t  type,
bool  overwrite 
) const

Writes given 3D array of given value and type to file.

Template Parameters
TType of array_t elements
array_tType of the given 2D array. It must support .size(), [i].size(), [i][j], [i][j].size() and [i][j][k] operations.
Parameters
dataset_nameName of the dataset.
valueValue to be written.
typeHDF5 type of the elements to be written.
overwriteIf overwriting existing datasets is allowed. If an attribute with the same name is attempted to be written, it must have the same dimensions and type as current value. Otherwise the overwrite will fail.

Definition at line 216 of file HDF.hpp.

◆ writeAttribute()

template<class T >
void mm::HDF::writeAttribute ( const std::string &  attr_name,
const T &  value,
const hid_t &  type,
bool  overwrite = false 
) const

Writes given attribute with given value and type to file.

Parameters
attr_nameName of the attribute.
valueValue to be written
typeHDF5 type of the attribute to be written.
overwriteIf overwriting existing attributes is allowed. If an attribute with the same name is attempted to be written, the existing attribute is deleted and a new one is created instead.

Definition at line 38 of file HDF.hpp.

◆ writeBoolAttribute()

void mm::HDF::writeBoolAttribute ( const std::string &  attr_name,
bool  value,
bool  overwrite = false 
) const

Write bool attribute.

See also
writeAttribute

Definition at line 142 of file HDF.cpp.

◆ writeDomain()

template<typename domain_t >
void mm::HDF::writeDomain ( const std::string &  name,
const domain_t &  domain,
bool  overwrite = false 
)

Writes given domain discretization to file.

A group with given name is created and 4 datasets and 1 attribute are stored inside:

  • N: int attribute storing number of nodes in the domain,
  • pos: N x dim double array of positions,
  • types: int array of length N representing domain types,
  • bmap: int array of length N mapping nodes to boundary nodes (see DomainDiscretization::bmap),
  • normals: double array of domain normals for boundary nodes. After the function exists, the same group is opened as before the call.
Parameters
nameName of the group in which the domain is stored.
domainDomain to write to file.
overwriteIf true, allows overwriting of existing entries.
See also
DomainDiscretization
Examples
test/end2end/poisson_explicit.cpp.

Definition at line 332 of file HDF.hpp.

◆ writeDouble2DArray()

template<typename array_t >
void mm::HDF::writeDouble2DArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 2D array of doubles.

See also
write2DArray

Definition at line 264 of file HDF.hpp.

◆ writeDouble3DArray()

template<typename array_t >
void mm::HDF::writeDouble3DArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 3D array of doubles.

See also
write3DArray

Definition at line 282 of file HDF.hpp.

◆ writeDoubleArray()

template<typename array_t >
void mm::HDF::writeDoubleArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 1D array of doubles.

See also
write1DArray
Examples
test/end2end/poisson_explicit.cpp.

Definition at line 246 of file HDF.hpp.

◆ writeDoubleAttribute()

void mm::HDF::writeDoubleAttribute ( const std::string &  attr_name,
double  value,
bool  overwrite = false 
) const

Write double attribute.

See also
writeAttribute

Definition at line 146 of file HDF.cpp.

◆ writeEigen()

template<typename Derived >
void mm::HDF::writeEigen ( const std::string &  dataset_name,
const Eigen::MatrixBase< Derived > &  value,
bool  overwrite = false 
) const

Write given Eigen expression as a 2D array of its type (either int, double or float).

Note
To use this function, the file HDF_Eigen.hpp must be included.

Definition at line 46 of file HDF_Eigen.hpp.

◆ writeFloat2DArray()

template<typename array_t >
void mm::HDF::writeFloat2DArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 2D array of floats.

See also
write2DArray

Definition at line 270 of file HDF.hpp.

◆ writeFloat3DArray()

template<typename array_t >
void mm::HDF::writeFloat3DArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 3D array of floats.

See also
write3DArray

Definition at line 288 of file HDF.hpp.

◆ writeFloatArray()

template<typename array_t >
void mm::HDF::writeFloatArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 1D array of floatss.

See also
write1DArray

Definition at line 252 of file HDF.hpp.

◆ writeFloatAttribute()

void mm::HDF::writeFloatAttribute ( const std::string &  attr_name,
float  value,
bool  overwrite = false 
) const

Write float attribute.

See also
writeAttribute

Definition at line 150 of file HDF.cpp.

◆ writeInt2DArray()

template<typename array_t >
void mm::HDF::writeInt2DArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 2D array of ints.

See also
write2DArray

Definition at line 258 of file HDF.hpp.

◆ writeInt3DArray()

template<typename array_t >
void mm::HDF::writeInt3DArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 3D array of ints.

See also
write3DArray

Definition at line 276 of file HDF.hpp.

◆ writeIntArray()

template<typename array_t >
void mm::HDF::writeIntArray ( const std::string &  dataset_name,
const array_t &  value,
bool  overwrite = false 
) const

Write given value as a 1D array of ints.

See also
write1DArray

Definition at line 240 of file HDF.hpp.

◆ writeIntAttribute()

void mm::HDF::writeIntAttribute ( const std::string &  attr_name,
int  value,
bool  overwrite = false 
) const

Write int attribute.

See also
writeAttribute

Definition at line 138 of file HDF.cpp.

◆ writeLinearArray()

template<int dim, typename T >
void mm::HDF::writeLinearArray ( const std::string &  dataset_name,
const T *  value,
const std::array< hsize_t, dim > &  sizes,
hid_t  type,
bool  overwrite 
) const

Writes a dim-dimensional dataset stored in 1D array in col-major fashion.

Template Parameters
dimDimension (HDF rank) of the dataset, 2 for matrices, 3 for tensors.
Parameters
dataset_nameName of the dataset.
valuePointer to values to be written, stored in a linear col-major fashion. The number of values should be equal to the product of sizes.
sizesSizes of the dataset along each dimension.
typeHDF5 type of the dataset.
overwriteSee write2DArray()

Definition at line 144 of file HDF.hpp.

◆ writeSparseMatrix()

template<typename SparseMatrixType >
void mm::HDF::writeSparseMatrix ( const std::string &  name,
SparseMatrixType &  matrix,
bool  one_based = true,
bool  overwrite = false 
)

Writes a sparse matrix.

Matrix matrix is written as a N x 3 matrix, where the columns represent vectors I, J, V, such that M(I(i), J(i)) = V(i). Matrix values are stored as doubles of native size.

Warning
By default, the indexes of the matrix elements are saved in 1-based format.
Parameters
nameAttribute name.
matrixA sparse matrix.
one_basedWhether to store the indices as one or zero based. One based indexes are ready to be read by Matlab's spconvert.
overwriteSee write2DArray().

Definition at line 316 of file HDF.hpp.

◆ writeStringAttribute()

void mm::HDF::writeStringAttribute ( const std::string &  attr_name,
const std::string &  value,
bool  overwrite = false 
) const

Write string attribute.

The string is written as an non-null-terminater array of bytes. It may contain null characters in the middle.

See also
writeAttribute

Definition at line 154 of file HDF.cpp.

◆ writeTimer()

template<typename timer_t >
void mm::HDF::writeTimer ( const std::string &  name,
const timer_t &  timer,
bool  overwrite = false 
)

Writes given timer to file.

A group with given name is created and durations (in seconds) between consecutive labels are writen as double attributes named label1-label2. Additionally, total time between first and last label (in seconds) is stored in the double attribute named total. After the function exists, the same group is opened as before the call. If timer has 0 labels, only the group is created. If timer has one label, attribute total with value 0.0 is written.

Parameters
nameName of the group in which the timer is stored.
timerTimer to write to file.
overwriteIf true, allows overwriting of existing entries.
See also
Timer

Definition at line 346 of file HDF.hpp.

◆ writeXML()

template<typename conf_t >
void mm::HDF::writeXML ( const std::string &  name,
const conf_t &  conf,
bool  overwrite = false 
)

Writes given XML object to file.

All attributes in the file, given by XML::getAll, are written in their dot-separated full-path format. They are written either as doubles (if whole value can be parsed as a double) or strings otherwise. After the function exists, the same group is opened as before the call.

Parameters
nameName of the group in which the XML attributes are stored.
confXML object.
overwriteIf true, allows overwriting of existing entries.
See also
XML

Definition at line 294 of file HDF.hpp.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const HDF hdf 
)
friend

Output basic info about HDF reader, such as current group and file.

Definition at line 561 of file HDF_fwd.hpp.

Member Data Documentation

◆ file

hid_t mm::HDF::file
private

Currently open file identifier.

Definition at line 79 of file HDF_fwd.hpp.

◆ filename_

std::string mm::HDF::filename_
private

Current file name.

Definition at line 76 of file HDF_fwd.hpp.

◆ group

hid_t mm::HDF::group
private

Currently open group identifier.

Definition at line 80 of file HDF_fwd.hpp.

◆ group_name_

std::string mm::HDF::group_name_
private

Current group name.

Definition at line 77 of file HDF_fwd.hpp.


The documentation for this class was generated from the following files:
mm::HDF::HDF
HDF()
Construct an empty HDF reader.
Definition: HDF_fwd.hpp:95
mm::HDF::DESTROY
@ DESTROY
Removes old contents, if any exist.
Definition: HDF_fwd.hpp:86