Cantera  3.2.0a4
Loading...
Searching...
No Matches
Solution.h
Go to the documentation of this file.
1//! @file Solution.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_SOLUTION_H
7#define CT_SOLUTION_H
8
10#include "cantera/base/AnyMap.h"
11
12namespace Cantera
13{
14
15class ThermoPhase;
16class Kinetics;
17class Transport;
18class ExternalHandle;
19
20//! @defgroup solnGroup Objects Representing Phases
21//! High-level interface to %Cantera's core objects.
22
23//! A container class for chemically-reacting solutions.
24/*!
25 * The Solution class collects all objects needed to describe a chemically-reacting
26 * solution. Instances can be created to represent any type of solution -- a mixture
27 * of gases, a liquid solution, or a solid solution, for example.
28 *
29 * Solution objects only define a small number of methods of their own, and are provided
30 * so that a single object can be used to access thermodynamic, kinetic, and transport
31 * properties of a solution:
32 * - ThermoPhase manager; accessed via thermo()
33 * - Kinetics manager; accessed via kinetics()
34 * - Transport manager; accessed via transport()
35 *
36 * The most common way to instantiate Solution objects is by using a phase definition,
37 * species and reactions defined in an input file:
38 * @code
39 * shared_ptr<Solution> sol = newSolution("gri30.yaml", "gri30");
40 * @endcode
41 * @ingroup solnGroup
42 */
43class Solution : public std::enable_shared_from_this<Solution>
44{
45protected:
46 Solution() = default;
47
48public:
49 virtual ~Solution() = default;
50 Solution(const Solution&) = delete;
51 Solution& operator=(const Solution&) = delete;
52
53 //! Create an empty Solution object
54 static shared_ptr<Solution> create() {
55 return shared_ptr<Solution>( new Solution );
56 }
57
58 //! Create a new Solution object with cloned ThermoPhase, Kinetics, and Transport
59 //! objects that use the same species definitions, thermodynamic parameters, and
60 //! reactions as this one.
61 //! @param adjacent For surface phases, an optional list of new adjacent phases
62 //! to link to the InterfaceKinetics object. Any adjacent phases not provided
63 //! will have their ThermoPhase model automatically cloned.
64 //! @param withKinetics Flag indicating whether to clone the Kinetics object
65 //! associated with this phase. If `false`, the cloned Solution will not include
66 //! a kinetics manager.
67 //! @param withTransport Flag indicating whether to clone the Transport object
68 //! associated with this phase. If `false`, the cloned Solution will not include
69 //! a transport property manager.
70 //! @since New in %Cantera 3.2.
71 shared_ptr<Solution> clone(const vector<shared_ptr<Solution>>& adjacent={},
72 bool withKinetics=true, bool withTransport=true) const;
73
74 //! Return the name of this Solution object
75 string name() const;
76
77 //! Set the name of this Solution object
78 void setName(const string& name);
79
80 //! Set the ThermoPhase object
81 virtual void setThermo(shared_ptr<ThermoPhase> thermo);
82
83 //! Set the Kinetics object
84 virtual void setKinetics(shared_ptr<Kinetics> kinetics);
85
86 //! Set the Transport object directly
87 virtual void setTransport(shared_ptr<Transport> transport);
88
89 //! Retrieve transport model name
90 //! @since New in %Cantera 3.2
91 string transportModel();
92
93 //! Set the Transport object by name
94 //! @param model name of transport model; if omitted, the default model is used
95 //! @since New in %Cantera 3.0
96 void setTransportModel(const string& model="default");
97
98 //! Accessor for the ThermoPhase pointer
99 shared_ptr<ThermoPhase> thermo() {
100 return m_thermo;
101 }
102
103 //! Accessor for the Kinetics pointer
104 shared_ptr<Kinetics> kinetics() {
105 return m_kinetics;
106 }
107
108 //! Accessor for the Transport pointer
109 shared_ptr<Transport> transport() {
110 return m_transport;
111 }
112
113 //! Add a phase adjacent to this phase. Usually this means a higher-dimensional
114 //! phase that participates in reactions in this phase.
115 void addAdjacent(shared_ptr<Solution> adjacent);
116
117 //! Get the Solution object for an adjacent phase by index
118 shared_ptr<Solution> adjacent(size_t i) {
119 return m_adjacent.at(i);
120 }
121
122 //! Get the Solution object for an adjacent phase by name
123 shared_ptr<Solution> adjacent(const string& name);
124
125 //! Get the number of adjacent phases
126 size_t nAdjacent() const {
127 return m_adjacent.size();
128 }
129
130 //! Get the name of an adjacent phase by index
131 string adjacentName(size_t i) const {
132 if (i >= m_adjacent.size()) {
133 throw IndexError("Solution::adjacentName", "m_adjacent",
134 i, m_adjacent.size());
135 }
136 return m_adjacent.at(i)->name();
137 }
138
139 AnyMap parameters(bool withInput=false) const;
140
141 //! Access input data associated with header definition
142 const AnyMap& header() const;
143 AnyMap& header();
144
145 //! Retrieve source used for object creation; usually an input file name
146 const string source() const;
147
148 //! Overwrite source (only required if object is not created using newSolution)
149 void setSource(const string& source);
150
151 //! Store a handle to a wrapper for this Solution object from an external
152 //! language interface (for example, a Python Solution object)
153 void holdExternalHandle(const string& name, shared_ptr<ExternalHandle> handle);
154
155 //! Get the handle for a wrapper for this Solution object from an external
156 //! language interface.
157 //! Returns a null pointer if the requested handle does not exist.
158 shared_ptr<ExternalHandle> getExternalHandle(const string& name) const;
159
160 //! Register a function to be called if any of the Solution's thermo, kinetics,
161 //! or transport objects is replaced.
162 //! @param id A unique ID corresponding to the object affected by the callback.
163 //! Typically, this is a pointer to an object that also holds a reference to the
164 //! Solution object.
165 //! @param callback The callback function to be called after any component of the
166 //! Solution is replaced.
167 //! When the callback becomes invalid (for example, the corresponding object is
168 //! being deleted, the removeChangedCallback() method must be invoked.
169 //! @since New in %Cantera 3.0
170 void registerChangedCallback(void* id, const function<void()>& callback);
171
172 //! Remove the callback function associated with the specified object.
173 //! @since New in %Cantera 3.0
174 void removeChangedCallback(void* id);
175
176protected:
177 shared_ptr<ThermoPhase> m_thermo; //!< ThermoPhase manager
178 shared_ptr<Kinetics> m_kinetics; //!< Kinetics manager
179 shared_ptr<Transport> m_transport; //!< Transport manager
180
181 //! Adjacent phases, for access by index
182 vector<shared_ptr<Solution>> m_adjacent;
183
184 //! Adjacent phases, for access by name
185 map<string, shared_ptr<Solution>> m_adjacentByName;
186
187 AnyMap m_header; //!< Additional input fields; usually from a YAML input file
188
189 //! Wrappers for this Solution object in extension languages, for evaluation
190 //! of user-defined reaction rates
191 map<string, shared_ptr<ExternalHandle>> m_externalHandles;
192
193 //! Callback functions that are invoked when the therm, kinetics, or transport
194 //! members of the Solution are replaced.
195 map<void*, function<void()>> m_changeCallbacks;
196};
197
198//! Create and initialize a new Solution from an input file
199/*!
200 * This constructor wraps newThermo(), newKinetics() and newTransport() routines
201 * for initialization.
202 *
203 * @param infile name of the input file
204 * @param name name of the phase in the file. If this is blank, the first phase
205 * in the file is used.
206 * @param transport name of the transport model. If blank, the transport model specified
207 * in the phase definition is used.
208 * @param adjacent vector containing names of adjacent phases that participate in this
209 * phases kinetics. If empty, adjacent phases will be instantiated based
210 * on the phase definition.
211 * @returns an initialized Solution object.
212 * @ingroup solnGroup
213 */
214shared_ptr<Solution> newSolution(const string& infile, const string& name,
215 const string& transport, const vector<string>& adjacent);
216
217//! Create and initialize a new Solution manager from an input file
218/*!
219 * This constructor wraps newThermo(), newKinetics() and newTransport() routines
220 * for initialization.
221 *
222 * @param infile name of the input file
223 * @param name name of the phase in the file.
224 * If this is blank, the first phase in the file is used.
225 * @param transport name of the transport model.
226 * @param adjacent vector containing adjacent Solution objects. If empty, adjacent
227 * phases will be instantiated based on the phase definition.
228 * @returns an initialized Solution object.
229 * @ingroup solnGroup
230 */
231shared_ptr<Solution> newSolution(const string& infile,
232 const string& name="",
233 const string& transport="default",
234 const vector<shared_ptr<Solution>>& adjacent={});
235
236//! Create and initialize a new Solution manager from AnyMap objects
237/*!
238 * This constructor wraps newThermo(), newKinetics() and newTransport() routines
239 * for initialization.
240 *
241 * @param phaseNode the node containing the phase definition (that is, thermo model,
242 * list of species, and initial state)
243 * @param rootNode the root node of the tree containing the phase definition, which
244 * will be used as the default location from which to read species definitions.
245 * @param transport name of the transport model.
246 * @param adjacent vector containing adjacent Solution objects. If empty, adjacent
247 * phases will be instantiated based on the phase definition.
248 * @param related vector of phases related to the same root Solution object. Used
249 * internally by newSolution() when creating complex interfaces where
250 * a phase may be adjacent to multiple other phases but should be
251 * instantiated only once.
252 * @returns an initialized Solution object.
253 * @ingroup solnGroup
254 */
255shared_ptr<Solution> newSolution(
256 const AnyMap& phaseNode, const AnyMap& rootNode=AnyMap(),
257 const string& transport="default",
258 const vector<shared_ptr<Solution>>& adjacent={},
259 const map<string, shared_ptr<Solution>>& related={});
260
261}
262
263#endif
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
An array index is out of range.
A container class for chemically-reacting solutions.
Definition Solution.h:44
static shared_ptr< Solution > create()
Create an empty Solution object.
Definition Solution.h:54
vector< shared_ptr< Solution > > m_adjacent
Adjacent phases, for access by index.
Definition Solution.h:182
string transportModel()
Retrieve transport model name.
Definition Solution.cpp:105
string adjacentName(size_t i) const
Get the name of an adjacent phase by index.
Definition Solution.h:131
void removeChangedCallback(void *id)
Remove the callback function associated with the specified object.
Definition Solution.cpp:237
map< string, shared_ptr< Solution > > m_adjacentByName
Adjacent phases, for access by name.
Definition Solution.h:185
void setSource(const string &source)
Overwrite source (only required if object is not created using newSolution)
Definition Solution.cpp:212
shared_ptr< Kinetics > kinetics()
Accessor for the Kinetics pointer.
Definition Solution.h:104
void setTransportModel(const string &model="default")
Set the Transport object by name.
Definition Solution.cpp:124
void addAdjacent(shared_ptr< Solution > adjacent)
Add a phase adjacent to this phase.
Definition Solution.cpp:135
void setName(const string &name)
Set the name of this Solution object.
Definition Solution.cpp:75
shared_ptr< Kinetics > m_kinetics
Kinetics manager.
Definition Solution.h:178
virtual void setThermo(shared_ptr< ThermoPhase > thermo)
Set the ThermoPhase object.
Definition Solution.cpp:84
map< string, shared_ptr< ExternalHandle > > m_externalHandles
Wrappers for this Solution object in extension languages, for evaluation of user-defined reaction rat...
Definition Solution.h:191
shared_ptr< ThermoPhase > m_thermo
ThermoPhase manager.
Definition Solution.h:177
void registerChangedCallback(void *id, const function< void()> &callback)
Register a function to be called if any of the Solution's thermo, kinetics, or transport objects is r...
Definition Solution.cpp:232
shared_ptr< Solution > clone(const vector< shared_ptr< Solution > > &adjacent={}, bool withKinetics=true, bool withTransport=true) const
Create a new Solution object with cloned ThermoPhase, Kinetics, and Transport objects that use the sa...
Definition Solution.cpp:25
AnyMap m_header
Additional input fields; usually from a YAML input file.
Definition Solution.h:187
size_t nAdjacent() const
Get the number of adjacent phases.
Definition Solution.h:126
void holdExternalHandle(const string &name, shared_ptr< ExternalHandle > handle)
Store a handle to a wrapper for this Solution object from an external language interface (for example...
Definition Solution.cpp:217
map< void *, function< void()> > m_changeCallbacks
Callback functions that are invoked when the therm, kinetics, or transport members of the Solution ar...
Definition Solution.h:195
const string source() const
Retrieve source used for object creation; usually an input file name.
Definition Solution.cpp:207
virtual void setTransport(shared_ptr< Transport > transport)
Set the Transport object directly.
Definition Solution.cpp:114
shared_ptr< ThermoPhase > thermo()
Accessor for the ThermoPhase pointer.
Definition Solution.h:99
shared_ptr< Transport > transport()
Accessor for the Transport pointer.
Definition Solution.h:109
const AnyMap & header() const
Access input data associated with header definition.
Definition Solution.cpp:197
shared_ptr< Solution > adjacent(size_t i)
Get the Solution object for an adjacent phase by index.
Definition Solution.h:118
shared_ptr< Transport > m_transport
Transport manager.
Definition Solution.h:179
virtual void setKinetics(shared_ptr< Kinetics > kinetics)
Set the Kinetics object.
Definition Solution.cpp:92
shared_ptr< ExternalHandle > getExternalHandle(const string &name) const
Get the handle for a wrapper for this Solution object from an external language interface.
Definition Solution.cpp:223
string name() const
Return the name of this Solution object.
Definition Solution.cpp:66
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
shared_ptr< Solution > newSolution(const string &infile, const string &name, const string &transport, const vector< shared_ptr< Solution > > &adjacent)
Create and initialize a new Solution manager from an input file.
Definition Solution.cpp:242
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595