Cantera  4.0.0a2
Loading...
Searching...
No Matches
SolutionArray.h
Go to the documentation of this file.
1//! @file SolutionArray.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_SOLUTIONARRAY_H
7#define CT_SOLUTIONARRAY_H
8
10#include "cantera/base/AnyMap.h"
11
12namespace Cantera
13{
14
15class Solution;
16class ThermoPhase;
17
18//! A container class holding arrays of state information.
19/*!
20 * SolutionArray objects provide a convenient interface for representing many
21 * thermodynamic states using the same Solution object. C++ SolutionArray objects are
22 * one-dimensional by design; while shape information for multi-dimensional arrays is
23 * stored, reshaping operations need to be implemented in high-level API's.
24 *
25 * The SolutionArray class implements the main interface for saving and restoring of
26 * %Cantera simulation data. SolutionArray objects can be serialized to and from YAML and
27 * HDF container files using the save() and restore() methods. In addition, there is
28 * limited support for CSV files.
29 * @since New in %Cantera 3.0.
30 * @ingroup solnGroup
31 */
33{
34private:
35 SolutionArray(const shared_ptr<Solution>& sol,
36 int size,
37 const AnyMap& meta);
38
39 SolutionArray(const SolutionArray& arr, const vector<int>& indices);
40
41public:
42 virtual ~SolutionArray();
43
44 /**
45 * Instantiate a new SolutionArray reference.
46 *
47 * @param sol Solution object defining phase definitions
48 * @param size Number of SolutionArray entries
49 * @param meta AnyMap holding SolutionArray meta data
50 */
51 static shared_ptr<SolutionArray> create(const shared_ptr<Solution>& sol,
52 int size=0,
53 const AnyMap& meta={})
54 {
55 return shared_ptr<SolutionArray>(new SolutionArray(sol, size, meta));
56 }
57
58 /**
59 * Share locations from an existing SolutionArray and return new reference.
60 *
61 * Both SolutionArray object share common data. The method is used for slicing
62 * of SolutionArrays from high-level API's. Note that meta data are not inherited.
63 * @param selected List of locations for shared entries
64 */
65 shared_ptr<SolutionArray> share(const vector<int>& selected)
66 {
67 return shared_ptr<SolutionArray>(new SolutionArray(*this, selected));
68 }
69
70 //! Reset all entries of the SolutionArray to the current Solution state
71 void reset();
72
73 //! Size of SolutionArray (number of entries).
74 int size() const {
75 return static_cast<int>(m_size);
76 }
77
78 //! Resize SolutionArray objects with a single dimension (default).
79 void resize(int size);
80
81 //! SolutionArray shape information used by high-level API's.
82 vector<long int> apiShape() const {
83 return m_apiShape;
84 }
85
86 //! Set SolutionArray shape information used by high-level API's.
87 //! The size of the SolutionArray is adjusted automatically.
88 void setApiShape(const vector<long int>& shape);
89
90 //! Number of SolutionArray dimensions used by high-level API's.
91 int apiNdim() const {
92 return static_cast<int>(m_apiShape.size());
93 }
94
95 /**
96 * Print a concise summary of a SolutionArray.
97 * @param keys List of components to be displayed; if empty, all components are
98 * considered.
99 * @param rows Maximum number of rendered rows.
100 * @param width Maximum width of rendered output.
101 */
102 string info(const vector<string>& keys, int rows=10, int width=80);
103
104 //! SolutionArray meta data.
106 return m_meta;
107 }
108
109 //! Set SolutionArray meta data.
110 void setMeta(const AnyMap& meta) {
111 m_meta = meta;
112 }
113
114 //! Retrieve associated Solution object
115 //! @since New in %Cantera 3.2
116 shared_ptr<Solution> phase() {
117 return m_sol;
118 }
119
120 //! Retrieve associated ThermoPhase object
121 shared_ptr<ThermoPhase> thermo();
122
123 //! Retrieve associated Transport model
124 //! @since New in %Cantera 3.2
125 string transportModel();
126
127 //! Retrieve list of component names
128 vector<string> componentNames() const;
129
130 /**
131 * Check whether SolutionArray contains a component.
132 * A component is a property defining state or auxiliary variable.
133 * @param name name of component
134 * @param checkAlias if `true` (default), check alias mapping
135 */
136 bool hasComponent(const string& name, bool checkAlias=true) const;
137
138 /**
139 * Retrieve a component of the SolutionArray by name.
140 * Returns an AnyValue containing an array with length size() with a type
141 * specific to the component; in most cases, the type is double, but may differ
142 * for auxiliary data.
143 */
144 AnyValue getComponent(const string& name) const;
145
146 /**
147 * Set a component of the SolutionArray by name.
148 * The passed AnyValue should containing an array with length size() with a type
149 * specific to the component; in most cases, the type is double, but may differ
150 * for auxiliary data.
151 *
152 * @param name Name of component (property defining auxiliary variable)
153 * @param data Component data
154 */
155 void setComponent(const string& name, const AnyValue& data);
156
157 /**
158 * Update the buffered location used to access SolutionArray entries.
159 */
160 void setLoc(int loc, bool restore=true);
161
162 /**
163 * Update state at given location to state of associated Solution object.
164 */
165 void updateState(int loc);
166
167 /**
168 * Retrieve the state vector for a given location.
169 * This is a read-only accessor that returns the state stored at @c loc; it does
170 * not modify the buffered location or the associated Solution object.
171 * @since Starting in %Cantera 4.0, this method is const/read-only.
172 */
173 vector<double> getState(int loc) const;
174
175 //! Set the state vector for a given location.
176 void setState(int loc, const vector<double>& state);
177
178 //! Normalize mass/mole fractions
179 void normalize();
180
181 /**
182 * Add auxiliary component to SolutionArray. Initialization requires a subsequent
183 * call of setComponent().
184 *
185 * @param name Name of component (property defining auxiliary variable)
186 * @param back If `true` (default), add name after components representing the
187 * state, otherwise add to front of list. Front and back components are
188 * populated left to right.
189 */
190 void addExtra(const string& name, bool back=true);
191
192 //! Check whether SolutionArray contains an extra component
193 bool hasExtra(const string& name) const {
194 return m_extra->count(name);
195 }
196
197 //! Retrieve list of extra component names
198 vector<string> listExtra(bool all=true) const;
199
200 /**
201 * Retrieve auxiliary data for a given location.
202 * This is a read-only accessor that returns the auxiliary data stored at @c loc;
203 * it does not modify the buffered location or the associated Solution object.
204 * @since Starting in %Cantera 4.0, this method is const/read-only.
205 */
206 AnyMap getAuxiliary(int loc) const;
207
208 //! Set auxiliary data for a given location.
209 void setAuxiliary(int loc, const AnyMap& data);
210
211 //! Append location entry at end of SolutionArray.
212 void append(const vector<double>& state, const AnyMap& extra);
213
214 /**
215 * Write header data to a HDF container file.
216 *
217 * @param fname Name of HDF container file
218 * @param name Identifier of group holding header information
219 * @param desc Custom comment describing dataset
220 * @param overwrite Force overwrite if file/group exists;
221 * optional (default=`false`)
222 */
223 static void writeHeader(const string& fname, const string& name, const string& desc,
224 bool overwrite=false);
225
226 /**
227 * Write header data to AnyMap. Used by YAML serialization.
228 *
229 * @param root Root node of AnyMap structure
230 * @param name Identifier of node holding header information
231 * @param desc Custom comment describing dataset
232 * @param overwrite Force overwrite if node exists; optional (default=`false`)
233 */
234 static void writeHeader(AnyMap& root, const string& name, const string& desc,
235 bool overwrite=false);
236
237 /**
238 * Write SolutionArray data to a CSV file.
239 *
240 * @param fname Name of CSV file
241 * @param overwrite Force overwrite if file exists; optional (default=`false`)
242 * @param basis Output mass (`"Y"`/`"mass"`) or mole (`"X"`/`"mole"`) fractions;
243 * if omitted (default=`""`), the native basis of the underlying ThermoPhase
244 * manager is used - see Phase::nativeState
245 */
246 void writeEntry(const string& fname, bool overwrite=false, const string& basis="");
247
248 /**
249 * Write SolutionArray data to a HDF container file.
250 *
251 * @param fname Name of HDF container file
252 * @param name Identifier of group holding header information
253 * @param sub Name identifier of subgroup holding SolutionArray data
254 * @param overwrite Force overwrite if subgroup exists; optional (default=`false`)
255 * @param compression Compression level; optional (default=0; HDF only)
256 */
257 void writeEntry(const string& fname, const string& name, const string& sub,
258 bool overwrite=false, int compression=0);
259
260 /**
261 * Write SolutionArray data to AnyMap. Used by YAML serialization.
262 *
263 * @param root Root node of AnyMap structure
264 * @param name Identifier of node holding header information and subgroup
265 * @param sub Name identifier of subgroup holding SolutionArray data
266 * @param overwrite Force overwrite if subgroup exists; optional (default=`false`)
267 */
268 void writeEntry(AnyMap& root, const string& name, const string& sub,
269 bool overwrite=false);
270
271 /**
272 * Save current SolutionArray contents to a data file.
273 *
274 * Data can be saved either in CSV format (extension `*.csv`), YAML container
275 * format (extension `*.yaml`/`*.yml`) or HDF container format (extension
276 * `*.h5`/`*.hdf5`/`*.hdf`). The output format is automatically inferred from the
277 * file extension.
278 *
279 * CSV files preserve state data and auxiliary data for a single SolutionArray in a
280 * comma-separated text format, container files may hold multiple SolutionArray
281 * entries in an internal hierarchical structure. While YAML is a human-readable
282 * text format, HDF is a binary format that supports compression and is recommended
283 * for large datasets.
284 *
285 * For container files (YAML and HDF), header information contains automatically
286 * generated time stamps, version information and an optional description.
287 * Container files also preserve SolutionArray metadata (example: SolutionArray
288 * objects generated by Sim1D hold simulation settings).
289 *
290 * @param fname Name of output file (CSV, YAML or HDF)
291 * @param name Identifier of location within the container file; this node/group
292 * contains header information and a subgroup holding actual SolutionArray data
293 * (YAML/HDF only)
294 * @param sub Name identifier for the subgroup holding the SolutionArray data and
295 * metadata objects. If omitted (`""`), the subgroup name defaults to `"data"`
296 * (YAML/HDF only)
297 * @param desc Custom comment describing dataset to be stored (YAML/HDF only)
298 * @param overwrite Force overwrite if file and/or data entry exists; optional
299 * (default=`false`)
300 * @param compression Compression level (0-9); (default=0; HDF only)
301 * @param basis Output mass (`"Y"`/`"mass"`) or mole (`"X"`/`"mole"`) fractions;
302 * if not specified (default=`""`), the native basis of the underlying
303 * ThermoPhase manager is used - see Phase::nativeState (CSV only)
304 */
305 void save(const string& fname, const string& name="", const string& sub="",
306 const string& desc="", bool overwrite=false, int compression=0,
307 const string& basis="");
308
309 /**
310 * Read header information from a HDF container file.
311 *
312 * @param fname Name of HDF container file
313 * @param name Identifier of group holding header information
314 */
315 static AnyMap readHeader(const string& fname, const string& name);
316
317 /**
318 * Read header information from AnyMap. Used by YAML serialization.
319 *
320 * @param root Root node of AnyMap structure
321 * @param name Identifier of node holding header information
322 */
323 static AnyMap readHeader(const AnyMap& root, const string& name);
324
325 /**
326 * Restore SolutionArray data from a HDF container file.
327 *
328 * @param fname Name of HDF container file
329 * @param name Identifier of group holding header information
330 * @param sub Name identifier of subgroup holding SolutionArray data
331 */
332 void readEntry(const string& fname, const string& name, const string& sub);
333
334 /**
335 * Restore SolutionArray data from AnyMap. Used by YAML serialization.
336 *
337 * @param root Root node of AnyMap structure
338 * @param name Identifier of node holding header information
339 * @param sub Name identifier of subgroup holding SolutionArray data
340 */
341 void readEntry(const AnyMap& root, const string& name, const string& sub);
342
343 /**
344 * Restore SolutionArray data and header information from a container file.
345 *
346 * This method retrieves data from a YAML or HDF files that were previously saved
347 * using the save() method.
348 *
349 * @param fname Name of container file (YAML or HDF)
350 * @param name Identifier of location within the container file; this node/group
351 * contains header information and a subgroup holding actual SolutionArray data
352 * @param sub Name identifier for the subgroup holding the SolutionArray data and
353 * metadata objects. If omitted (`""`), the subgroup name defaults to "data"
354 * @return AnyMap containing header information
355 */
356 AnyMap restore(const string& fname, const string& name, const string& sub="");
357
358protected:
359 //! Service function used to resize SolutionArray
360 void _resize(size_t size);
361
362 /**
363 * Initialize extra SolutionArray component.
364 *
365 * @param name Name of component (property defining auxiliary variable)
366 * @param value Default value; used to determine type of component
367 */
368 void _initExtra(const string& name, const AnyValue& value);
369
370 /**
371 * Resize extra SolutionArray component.
372 *
373 * @param name Name of component (property defining auxiliary variable)
374 * @param value Default value
375 */
376 void _resizeExtra(const string& name, const AnyValue& value=AnyValue());
377
378 /**
379 * Set extra SolutionArray component
380 *
381 * @param name Name of component (property defining auxiliary variable)
382 * @param data Value to be set
383 */
384 void _setExtra(const string& name, const AnyValue& data=AnyValue());
385
386 /**
387 * Identify storage mode of state data. The storage mode is a combination of
388 * properties defining state); valid modes include Phase::nativeState (`"native"`)
389 * or other property combinations defined by Phase::fullStates (three-letter
390 * acronyms, for example `"TDY"`, `"TPX"`).
391 */
392 string _detectMode(const set<string>& names, bool native=true);
393
394 //! Retrieve set containing list of properties defining state
395 set<string> _stateProperties(const string& mode, bool alias=false);
396
397 //! `true` if this object is a view (slice) into another SolutionArray's data
398 bool _isSliced() const { return m_shared; }
399
400 shared_ptr<Solution> m_sol; //!< Solution object associated with state data
401 size_t m_size; //!< Number of entries in SolutionArray
402 size_t m_dataSize; //!< Total size of unsliced data
403 size_t m_stride; //!< Stride between SolutionArray entries
404 AnyMap m_meta; //!< Metadata
405 size_t m_loc = npos; //!< Buffered location within data vector
406 vector<long int> m_apiShape; //!< Shape information used by high-level API's
407
408 shared_ptr<vector<double>> m_data; //!< Work vector holding states
409
410 //! Auxiliary (extra) components; size of first dimension has to match m_dataSize
411 shared_ptr<map<string, AnyValue>> m_extra;
412
413 //! Mapping of auxiliary component names, where the index is used as the
414 //! mapping key. Names with index >= zero are listed before state components, while
415 //! names with index < zero are added at end. The name with the most negative index
416 //! corresponds to the last entry (different from Python index convention).
417 shared_ptr<map<int, string>> m_order;
418
419 bool m_shared = false; //!< `true` if data are shared from another object
420 vector<int> m_active; //!< Vector of locations referencing active entries
421};
422
423//! Return mapping of component alias names to standardized component names.
424const map<string, string>& _componentAliasMap();
425
426}
427
428#endif
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
A wrapper for a variable whose type is determined at runtime.
Definition AnyMap.h:88
A container class holding arrays of state information.
size_t m_size
Number of entries in SolutionArray.
AnyMap m_meta
Metadata.
void append(const vector< double > &state, const AnyMap &extra)
Append location entry at end of SolutionArray.
string transportModel()
Retrieve associated Transport model.
size_t m_loc
Buffered location within data vector.
void setLoc(int loc, bool restore=true)
Update the buffered location used to access SolutionArray entries.
string _detectMode(const set< string > &names, bool native=true)
Identify storage mode of state data.
size_t m_dataSize
Total size of unsliced data.
void setMeta(const AnyMap &meta)
Set SolutionArray meta data.
static AnyMap readHeader(const string &fname, const string &name)
Read header information from a HDF container file.
size_t m_stride
Stride between SolutionArray entries.
shared_ptr< SolutionArray > share(const vector< int > &selected)
Share locations from an existing SolutionArray and return new reference.
void _setExtra(const string &name, const AnyValue &data=AnyValue())
Set extra SolutionArray component.
string info(const vector< string > &keys, int rows=10, int width=80)
Print a concise summary of a SolutionArray.
vector< string > listExtra(bool all=true) const
Retrieve list of extra component names.
void setApiShape(const vector< long int > &shape)
Set SolutionArray shape information used by high-level API's.
vector< double > getState(int loc) const
Retrieve the state vector for a given location.
void setAuxiliary(int loc, const AnyMap &data)
Set auxiliary data for a given location.
static void writeHeader(const string &fname, const string &name, const string &desc, bool overwrite=false)
Write header data to a HDF container file.
AnyMap restore(const string &fname, const string &name, const string &sub="")
Restore SolutionArray data and header information from a container file.
AnyValue getComponent(const string &name) const
Retrieve a component of the SolutionArray by name.
vector< int > m_active
Vector of locations referencing active entries.
set< string > _stateProperties(const string &mode, bool alias=false)
Retrieve set containing list of properties defining state.
void readEntry(const string &fname, const string &name, const string &sub)
Restore SolutionArray data from a HDF container file.
void _resize(size_t size)
Service function used to resize SolutionArray.
AnyMap getAuxiliary(int loc) const
Retrieve auxiliary data for a given location.
void _initExtra(const string &name, const AnyValue &value)
Initialize extra SolutionArray component.
vector< string > componentNames() const
Retrieve list of component names.
vector< long int > apiShape() const
SolutionArray shape information used by high-level API's.
shared_ptr< vector< double > > m_data
Work vector holding states.
shared_ptr< map< string, AnyValue > > m_extra
Auxiliary (extra) components; size of first dimension has to match m_dataSize.
bool m_shared
true if data are shared from another object
void setState(int loc, const vector< double > &state)
Set the state vector for a given location.
shared_ptr< map< int, string > > m_order
Mapping of auxiliary component names, where the index is used as the mapping key.
int apiNdim() const
Number of SolutionArray dimensions used by high-level API's.
void setComponent(const string &name, const AnyValue &data)
Set a component of the SolutionArray by name.
bool _isSliced() const
true if this object is a view (slice) into another SolutionArray's data
AnyMap & meta()
SolutionArray meta data.
void normalize()
Normalize mass/mole fractions.
void save(const string &fname, const string &name="", const string &sub="", const string &desc="", bool overwrite=false, int compression=0, const string &basis="")
Save current SolutionArray contents to a data file.
void reset()
Reset all entries of the SolutionArray to the current Solution state.
void resize(int size)
Resize SolutionArray objects with a single dimension (default).
void _resizeExtra(const string &name, const AnyValue &value=AnyValue())
Resize extra SolutionArray component.
shared_ptr< ThermoPhase > thermo()
Retrieve associated ThermoPhase object.
void addExtra(const string &name, bool back=true)
Add auxiliary component to SolutionArray.
shared_ptr< Solution > phase()
Retrieve associated Solution object.
bool hasExtra(const string &name) const
Check whether SolutionArray contains an extra component.
void updateState(int loc)
Update state at given location to state of associated Solution object.
static shared_ptr< SolutionArray > create(const shared_ptr< Solution > &sol, int size=0, const AnyMap &meta={})
Instantiate a new SolutionArray reference.
void writeEntry(const string &fname, bool overwrite=false, const string &basis="")
Write SolutionArray data to a CSV file.
int size() const
Size of SolutionArray (number of entries).
bool hasComponent(const string &name, bool checkAlias=true) const
Check whether SolutionArray contains a component.
vector< long int > m_apiShape
Shape information used by high-level API's.
shared_ptr< Solution > m_sol
Solution object associated with state data.
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:183
const map< string, string > & _componentAliasMap()
Return mapping of component alias names to standardized component names.