Cantera  3.1.0a1
Storage.h
Go to the documentation of this file.
1 //! @file Storage.h
2 
3 // This file is part of Cantera. See License.txt in the top-level directory or
4 // at https://cantera.org/license.txt for license and copyright information.
5 
6 #ifndef CT_STORAGE_H
7 #define CT_STORAGE_H
8 
9 #include "cantera/base/ct_defs.h"
11 
12 #if CT_USE_HDF5
13 
14 #ifdef _WIN32
15  // see https://github.com/microsoft/vcpkg/issues/24293
16  #define H5_BUILT_AS_DYNAMIC_LIB
17 #else
18  #define H5_BUILT_AS_STATIC_LIB
19 #endif
20 
21 namespace HighFive {
22  class File;
23 }
24 
25 #endif
26 
27 namespace Cantera
28 {
29 
30 /**
31  * A wrapper class handling storage to HDF. Acts as a thin wrapper for HighFive.
32  * The class implements methods that are intended to be called from SolutionArray.
33  *
34  * @since New in %Cantera 3.0.
35  * @warning This class is an experimental part of the %Cantera API and may be
36  * changed or removed without notice.
37  */
38 class Storage
39 {
40 public:
41  Storage(string fname, bool write);
42 
43  ~Storage();
44 
45  //! Set compression level (0..9)
46  //!
47  //! Compression is only applied to matrix-type data; note that compression may
48  //! increase file size for small data sets (compression requires setting of chunk
49  //! sizes, which involves considerable overhead for metadata).
50  void setCompressionLevel(int level);
51 
52  //! Check whether location `id` represents a group
53  bool hasGroup(const string& id) const;
54 
55  //! Check whether path location exists.
56  //! If the location does not exist, an exception is thrown unless the *permissive*
57  //! flag is set; in this case, the method attempts to create a new location if the
58  //! file is accessed in write mode.
59  //! @param id storage location within file
60  //! @param permissive if true, do not raise exceptions (default=false)
61  //! @returns boolean indicating whether id is pre-existing
62  bool checkGroup(const string& id, bool permissive=false);
63 
64  //! Delete group
65  //! @param id storage location within file
66  void deleteGroup(const string& id);
67 
68  //! Retrieve contents of file from a specified location
69  //! @param id storage location within file
70  //! @returns pair containing size and list of entry names of stored data set
71  pair<size_t, set<string>> contents(const string& id) const;
72 
73  //! Read attributes from a specified location
74  //! @param id storage location within file
75  //! @param attr name of attribute to be checked
76  bool hasAttribute(const string& id, const string& attr) const;
77 
78  //! Read attributes from a specified location
79  //! @param id storage location within file
80  //! @param recursive boolean indicating whether subgroups should be included
81  //! @returns AnyMap containing attributes
82  AnyMap readAttributes(const string& id, bool recursive) const;
83 
84  //! Write attributes to a specified location
85  //! @param id storage location within file
86  //! @param meta AnyMap containing attributes
87  void writeAttributes(const string& id, const AnyMap& meta);
88 
89  //! Read dataset from a specified location
90  //! @param id storage location within file
91  //! @param name name of vector/matrix entry
92  //! @param rows number of vector length or matrix rows
93  //! @param cols number of matrix columns, if applicable; if 0, a vector is
94  //! expected, if npos, the size is detected automatically; otherwise, an exact
95  //! number of columns needs to be matched.
96  //! @returns matrix or vector containing data; implemented for types
97  //! `vector<double>`, `vector<long int>`, `vector<string>`,
98  //! `vector<vector<double>>`, `vector<vector<long int>>` and
99  //! `vector<vector<string>>`
100  AnyValue readData(const string& id,
101  const string& name, size_t rows, size_t cols=npos) const;
102 
103  //! Write dataset to a specified location
104  //! @param id storage location within file
105  //! @param name name of matrix entry
106  //! @param data vector or matrix containing data; implemented for types
107  //! `vector<double>`, `vector<long int>`, `vector<string>`
108  //! `vector<vector<double>>`, `vector<vector<long int>>` and
109  //! `vector<vector<string>>`
110  void writeData(const string& id, const string& name, const AnyValue& data);
111 
112 private:
113 #if CT_USE_HDF5
114  //! ensure that HDF group is readable
115  bool checkGroupRead(const string& id) const;
116 
117  //! ensure that HDF group is writeable
118  bool checkGroupWrite(const string& id, bool permissive);
119 
120  unique_ptr<HighFive::File> m_file; //!< HDF container file
121  bool m_write; //!< HDF access mode
122  int m_compressionLevel=0; //!< HDF compression level
123 #endif
124 };
125 
126 }
127 
128 #endif
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:427
A wrapper for a variable whose type is determined at runtime.
Definition: AnyMap.h:86
A wrapper class handling storage to HDF.
Definition: Storage.h:39
pair< size_t, set< string > > contents(const string &id) const
Retrieve contents of file from a specified location.
Definition: Storage.cpp:609
bool hasGroup(const string &id) const
Check whether location id represents a group.
Definition: Storage.cpp:591
void writeAttributes(const string &id, const AnyMap &meta)
Write attributes to a specified location.
Definition: Storage.cpp:627
void deleteGroup(const string &id)
Delete group.
Definition: Storage.cpp:603
AnyMap readAttributes(const string &id, bool recursive) const
Read attributes from a specified location.
Definition: Storage.cpp:621
bool hasAttribute(const string &id, const string &attr) const
Read attributes from a specified location.
Definition: Storage.cpp:615
AnyValue readData(const string &id, const string &name, size_t rows, size_t cols=npos) const
Read dataset from a specified location.
Definition: Storage.cpp:633
bool checkGroup(const string &id, bool permissive=false)
Check whether path location exists.
Definition: Storage.cpp:597
void setCompressionLevel(int level)
Set compression level (0..9)
Definition: Storage.cpp:585
void writeData(const string &id, const string &name, const AnyValue &data)
Write dataset to a specified location.
Definition: Storage.cpp:640
This file contains definitions of constants, types and terms that are used in internal routines and a...
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:180
Contains declarations for string manipulation functions within Cantera.