20#include <boost/algorithm/string.hpp>
26 bool withKinetics,
bool withTransport)
const
28 shared_ptr<Solution> out =
create();
31 vector<shared_ptr<ThermoPhase>> kinPhases;
32 map<string, shared_ptr<Solution>> adjacentByName;
34 adjacentByName[soln->name()] = soln;
35 if (
m_kinetics->phaseIndex(soln->name()) == -1) {
36 throw CanteraError(
"Solution::clone",
"Provided adjacent phase '{}'"
37 " not found in Kinetics object.", soln->name());
41 kinPhases.push_back(out->thermo());
42 for (
size_t i = 1; i <
m_kinetics->nPhases(); i++) {
44 if (adjacentByName.count(
name) != 0) {
45 kinPhases.push_back(adjacentByName[
name]->
thermo());
46 out->addAdjacent(adjacentByName[
name]);
47 adjacentByName.erase(
name);
49 auto soln =
m_kinetics->phase(i)->root()->clone({},
false,
false);
50 kinPhases.push_back(soln->thermo());
51 out->addAdjacent(soln);
54 out->setKinetics(
m_kinetics->clone(kinPhases));
59 out->setTransport(
m_transport->clone(out->thermo()));
71 "Requires associated 'ThermoPhase'");
80 "Requires associated 'ThermoPhase'");
86 m_thermo->setSolution(weak_from_this());
109 "The Transport object is not initialized.");
127 "Unable to set Transport model without valid ThermoPhase object.");
138 "Solution '{}' already contains an adjacent phase named '{}'.",
145 "Adjacent phases should have higher dimensionality than the reacting ",
146 "phase.\n'{}' is {}-dimensional while '{}' is {}-dimensional",
158 }
catch (std::exception&) {
159 throw CanteraError(
"Solution::adjacent",
"Solution '{}' does not have an "
160 "adjacent phase named '{}'", this->
name(), name);
164AnyMap Solution::parameters(
bool withInput)
const
172 out[
"transport"] = empty;
173 }
else if (
m_transport->transportModel() ==
"none") {
174 out[
"transport"] = empty;
181 if (input.hasKey(
"reactions")) {
183 input.erase(
"reactions");
186 if (input.hasKey(
"transport")) {
191 if (out[
"transport"] == empty) {
192 out.
erase(
"transport");
209 return source.empty() ?
"<unknown>" :
source.asString();
218 shared_ptr<ExternalHandle> handle)
228 return shared_ptr<ExternalHandle>();
244 const string &transport,
245 const vector<shared_ptr<Solution>> &adjacent)
248 size_t dot = infile.find_last_of(
".");
254 if (extension ==
"cti" || extension ==
"xml") {
256 "The CTI and XML formats are no longer supported.");
262 auto sol =
newSolution(phaseNode, rootNode, transport, adjacent);
263 sol->setSource(infile);
267shared_ptr<Solution>
newSolution(
const string& infile,
const string& name,
268 const string& transport,
const vector<string>& adjacent)
273 vector<shared_ptr<Solution>> adjPhases;
275 for (
auto& name : adjacent) {
276 const auto& adjNode = rootNode.
at(
"phases").
getMapWhere(
"name", name);
277 adjPhases.push_back(
newSolution(adjNode, rootNode));
279 return newSolution(phaseNode, rootNode, transport, adjPhases);
284 const string& transport,
285 const vector<shared_ptr<Solution>>& adjacent,
286 const map<
string, shared_ptr<Solution>>& related)
289 auto thermo =
newThermo(phaseNode, rootNode);
292 shared_ptr<Solution> sol;
293 switch (thermo->nDim()) {
300 sol->setSource(
"custom YAML");
301 sol->setThermo(thermo);
304 for (
auto& adj : adjacent) {
305 sol->addAdjacent(adj);
310 if (adjacent.empty() && phaseNode.
hasKey(
"adjacent-phases")) {
311 auto all_related = related;
312 for (
auto& phase : adjacent) {
313 all_related[phase->name()] = phase;
320 if (!all_related.count(name)) {
323 "default", {}, all_related);
324 all_related[name] = adj;
325 for (
size_t i = 0; i < adj->nAdjacent(); i++) {
326 all_related[adj->adjacent(i)->name()] = adj->adjacent(i);
329 sol->addAdjacent(all_related[name]);
332 auto& adjPhases = phaseNode[
"adjacent-phases"];
333 if (adjPhases.is<vector<string>>()) {
335 for (
auto& phase : adjPhases.as<vector<string>>()) {
336 addPhase(rootNode[
"phases"], rootNode, phase);
338 }
else if (adjPhases.is<vector<AnyMap>>()) {
342 for (
auto& item : adjPhases.asVector<
AnyMap>()) {
343 const string& source = item.begin()->first;
344 const auto& names = item.begin()->second.asVector<
string>();
345 const auto& slash = boost::ifind_last(source,
"/");
348 string fileName(source.begin(), slash.begin());
349 string node(slash.end(), source.end());
352 for (
auto& phase : names) {
353 addPhase(phaseSource[node], phaseSource, phase);
355 }
else if (rootNode.
hasKey(source)) {
357 for (
auto& phase : names) {
358 addPhase(rootNode[source], rootNode, phase);
362 "Could not find a phases section named '{}'.", source);
367 "Could not parse adjacent phase declaration of type '{}'",
368 adjPhases.type_str());
373 vector<shared_ptr<ThermoPhase>> phases;
374 phases.push_back(sol->thermo());
375 for (
size_t i = 0; i < sol->nAdjacent(); i++) {
376 phases.push_back(sol->adjacent(i)->thermo());
378 sol->setKinetics(
newKinetics(phases, phaseNode, rootNode, sol));
381 sol->setTransportModel(transport);
385 for (
const auto& [key, value] : rootNode.
ordered()) {
386 if (key ==
"phases") {
392 sol->header() = header;
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Header file defining class TransportFactory (see TransportFactory)
Headers for the Transport object, which is the virtual base class for all transport property evaluato...
const AnyValue & getMetadata(const string &key) const
Get a value from the metadata applicable to the AnyMap tree containing this node.
A map of string keys to values whose type can vary at runtime.
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
void setMetadata(const string &key, const AnyValue &value)
Set a metadata value that applies to this AnyMap and its children.
OrderedProxy ordered(bool withUnits=false) const
Return a proxy object that allows iteration in an order determined by the order of insertion,...
const string & getString(const string &key, const string &default_) const
If key exists, return it as a string, otherwise return default_.
void erase(const string &key)
Erase the value held by key.
static AnyMap fromYamlFile(const string &name, const string &parent_name="")
Create an AnyMap from a YAML file.
const AnyValue & at(const string &key) const
Get the value of the item stored in key.
void update(const AnyMap &other, bool keepExisting=true)
Add items from other to this AnyMap.
A wrapper for a variable whose type is determined at runtime.
AnyMap & getMapWhere(const string &key, const string &value, bool create=false)
Treating the value as vector<AnyMap>, return the item where the given key has the specified value.
Base class for exceptions thrown by Cantera classes.
static shared_ptr< Interface > create()
Create an empty Interface object.
static shared_ptr< Solution > create()
Create an empty Solution object.
vector< shared_ptr< Solution > > m_adjacent
Adjacent phases, for access by index.
string transportModel()
Retrieve transport model name.
void removeChangedCallback(void *id)
Remove the callback function associated with the specified object.
map< string, shared_ptr< Solution > > m_adjacentByName
Adjacent phases, for access by name.
void setSource(const string &source)
Overwrite source (only required if object is not created using newSolution)
shared_ptr< Kinetics > kinetics()
Accessor for the Kinetics pointer.
void setTransportModel(const string &model="default")
Set the Transport object by name.
void addAdjacent(shared_ptr< Solution > adjacent)
Add a phase adjacent to this phase.
void setName(const string &name)
Set the name of this Solution object.
shared_ptr< Kinetics > m_kinetics
Kinetics manager.
virtual void setThermo(shared_ptr< ThermoPhase > thermo)
Set the ThermoPhase object.
map< string, shared_ptr< ExternalHandle > > m_externalHandles
Wrappers for this Solution object in extension languages, for evaluation of user-defined reaction rat...
shared_ptr< ThermoPhase > m_thermo
ThermoPhase manager.
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...
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...
AnyMap m_header
Additional input fields; usually from a YAML input file.
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...
map< void *, function< void()> > m_changeCallbacks
Callback functions that are invoked when the therm, kinetics, or transport members of the Solution ar...
const string source() const
Retrieve source used for object creation; usually an input file name.
virtual void setTransport(shared_ptr< Transport > transport)
Set the Transport object directly.
shared_ptr< ThermoPhase > thermo()
Accessor for the ThermoPhase pointer.
shared_ptr< Transport > transport()
Accessor for the Transport pointer.
const AnyMap & header() const
Access input data associated with header definition.
shared_ptr< Solution > adjacent(size_t i)
Get the Solution object for an adjacent phase by index.
shared_ptr< Transport > m_transport
Transport manager.
virtual void setKinetics(shared_ptr< Kinetics > kinetics)
Set the Kinetics object.
shared_ptr< ExternalHandle > getExternalHandle(const string &name) const
Get the handle for a wrapper for this Solution object from an external language interface.
string name() const
Return the name of this Solution object.
string toLowerCopy(const string &input)
Convert to lower case.
shared_ptr< Kinetics > newKinetics(const string &model)
Create a new Kinetics instance.
double dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
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.
shared_ptr< ThermoPhase > newThermo(const AnyMap &phaseNode, const AnyMap &rootNode)
Create a new ThermoPhase object and initialize it.
shared_ptr< Transport > newTransport(shared_ptr< ThermoPhase > thermo, const string &model)
Create a new Transport instance.
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
Contains declarations for string manipulation functions within Cantera.