16#include <boost/algorithm/string.hpp>
21KineticsFactory* KineticsFactory::s_factory = 0;
22std::mutex KineticsFactory::kinetics_mutex;
24KineticsFactory::KineticsFactory() {
25 reg(
"none", []() {
return new Kinetics(); });
28 reg(
"bulk", []() {
return new BulkKinetics(); });
32 reg(
"surface", []() {
return new InterfaceKinetics(); });
36 reg(
"edge", []() {
return new EdgeKinetics(); });
40KineticsFactory* KineticsFactory::factory() {
41 std::unique_lock<std::mutex> lock(kinetics_mutex);
43 s_factory =
new KineticsFactory;
49 std::unique_lock<std::mutex> lock(kinetics_mutex);
61 shared_ptr<Kinetics> kin(KineticsFactory::factory()->
newKinetics(model));
65shared_ptr<Kinetics>
newKinetics(
const vector<shared_ptr<ThermoPhase>>& phases,
68 shared_ptr<Solution> soln)
70 string kinType = phaseNode.
getString(
"kinetics",
"none");
71 kinType = KineticsFactory::factory()->canonicalize(kinType);
72 if (kinType ==
"none") {
75 for (
auto& phase : phases) {
76 nDim = std::min(phase->nDim(), nDim);
81 }
else if (nDim == 1) {
86 shared_ptr<Kinetics> kin(KineticsFactory::factory()->
newKinetics(kinType));
88 soln->setKinetics(kin);
90 for (
auto& phase : phases) {
91 kin->addThermo(phase);
98shared_ptr<Kinetics>
newKinetics(
const vector<shared_ptr<ThermoPhase>>& phases,
99 const string& filename)
101 string reaction_phase = phases.at(0)->name();
103 AnyMap& phaseNode = root[
"phases"].getMapWhere(
"name", reaction_phase);
110 phaseNode.
getBool(
"skip-undeclared-third-bodies",
false));
112 phaseNode.
getString(
"explicit-third-body-duplicates",
"warn"));
117 vector<string> sections, rules;
119 if (phaseNode.
hasKey(
"reactions")) {
120 const auto& reactionsNode = phaseNode.
at(
"reactions");
121 if (reactionsNode.is<
string>()) {
122 if (rootNode.
hasKey(
"reactions")) {
125 sections.push_back(
"reactions");
126 rules.push_back(reactionsNode.asString());
127 }
else if (reactionsNode.asString() !=
"none") {
129 "Phase entry implies existence of 'reactions' section "
130 "which does not exist in the current input file.");
132 }
else if (reactionsNode.is<vector<string>>()) {
134 for (
const auto& item : reactionsNode.as<vector<string>>()) {
135 sections.push_back(item);
136 rules.push_back(
"all");
138 }
else if (reactionsNode.is<vector<AnyMap>>()) {
141 for (
const auto& item : reactionsNode.as<vector<AnyMap>>()) {
142 sections.push_back(item.begin()->first);
143 rules.push_back(item.begin()->second.asString());
147 if (!phaseNode.
hasKey(
"kinetics")) {
150 }
else if (rootNode.
hasKey(
"reactions")) {
153 sections.push_back(
"reactions");
154 rules.push_back(
"all");
157 "Phase entry implies existence of 'reactions' section which "
158 "does not exist in the current input file. Add the field "
159 "'reactions: none' to the phase entry to specify a kinetics "
160 "model with no reactions.");
165 fmt::memory_buffer add_rxn_err;
166 for (
size_t i = 0; i < sections.size(); i++) {
167 if (rules[i] ==
"all") {
169 }
else if (rules[i] ==
"declared-species") {
171 }
else if (rules[i] ==
"none") {
175 "Unknown rule '{}' for adding species from the '{}' section.",
176 rules[i], sections[i]);
178 const auto& slash = boost::ifind_last(sections[i],
"/");
181 string fileName (sections[i].begin(), slash.begin());
182 string node(slash.end(), sections[i].end());
186 for (
const auto& R : reactions[node].asVector<
AnyMap>()) {
213 if (add_rxn_err.size()) {
214 throw CanteraError(
"addReactions", to_string(add_rxn_err));
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
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.
bool getBool(const string &key, bool default_) const
If key exists, return it as a bool, otherwise return default_.
const string & getString(const string &key, const string &default_) const
If key exists, return it as a string, otherwise return default_.
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.
const vector< T > & asVector(size_t nMin=npos, size_t nMax=npos) const
Return the held value, if it is a vector of type T.
Base class for exceptions thrown by Cantera classes.
const char * what() const override
Get a description of the error.
Kinetics * create(const string &name, Args... args)
Create an object using the object construction function corresponding to "name" and the provided cons...
void reg(const string &name, function< Kinetics *(Args...)> f)
Register a new object construction function.
void addAlias(const string &original, const string &alias)
Add an alias for an existing registered type.
void addDeprecatedAlias(const string &original, const string &alias)
Add a deprecated alias for an existing registered type.
void deleteFactory() override
Virtual abstract function that deletes the factory.
Kinetics * newKinetics(const string &model)
Return a new, empty kinetics manager.
Public interface for kinetics managers.
virtual void resizeReactions()
Finalize Kinetics object and associated objects.
void setExplicitThirdBodyDuplicateHandling(const string &flag)
Specify how to handle duplicate third body reactions where one reaction has an explicit third body an...
virtual string kineticsType() const
Identifies the Kinetics manager type.
virtual pair< size_t, size_t > checkDuplicates(bool throw_err=true) const
Check for unmarked duplicate reactions and unmatched marked duplicates.
virtual bool addReaction(shared_ptr< Reaction > r, bool resize=true)
Add a single reaction to the mechanism.
void skipUndeclaredSpecies(bool skip)
Determine behavior when adding a new reaction that contains species not defined in any of the phases ...
void skipUndeclaredThirdBodies(bool skip)
Determine behavior when adding a new reaction that contains third-body efficiencies for species not d...
void fmt_append(fmt::memory_buffer &b, const std::string &tmpl, Args... args)
Versions 6.2.0 and 6.2.1 of fmtlib do not include this define before they include windows....
string toLowerCopy(const string &input)
Convert to lower case.
shared_ptr< Kinetics > newKinetics(const string &model)
Create a new Kinetics instance.
void addReactions(Kinetics &kin, const AnyMap &phaseNode, const AnyMap &rootNode)
Add reactions to a Kinetics object.
Namespace for the Cantera kernel.
void loadExtensions(const AnyMap &node)
Load extensions providing user-defined models from the extensions section of the given node.
unique_ptr< Reaction > newReaction(const string &type)
Create a new empty Reaction object.
Contains declarations for string manipulation functions within Cantera.