11#define CT_SKIP_DEPRECATION_WARNINGS
20#include <boost/algorithm/string.hpp>
25KineticsFactory* KineticsFactory::s_factory = 0;
26std::mutex KineticsFactory::kinetics_mutex;
28KineticsFactory::KineticsFactory() {
29 reg(
"none", []() {
return new Kinetics(); });
32 reg(
"bulk", []() {
return new BulkKinetics(); });
34 reg(
"gas", []() {
return new GasKinetics(); });
37 reg(
"surface", []() {
return new InterfaceKinetics(); });
41 reg(
"edge", []() {
return new EdgeKinetics(); });
45KineticsFactory* KineticsFactory::factory() {
46 std::unique_lock<std::mutex> lock(kinetics_mutex);
48 s_factory =
new KineticsFactory;
54 std::unique_lock<std::mutex> lock(kinetics_mutex);
67 "To be removed after Cantera 3.0; superseded by newKinetics.");
68 return KineticsFactory::factory()->newKinetics(model);
73 shared_ptr<Kinetics> kin(KineticsFactory::factory()->
newKinetics(model));
77shared_ptr<Kinetics>
newKinetics(
const vector<shared_ptr<ThermoPhase>>& phases,
80 shared_ptr<Solution> soln)
82 string kinType = phaseNode.
getString(
"kinetics",
"none");
83 kinType = KineticsFactory::factory()->canonicalize(kinType);
84 if (kinType ==
"none") {
87 for (
auto& phase : phases) {
88 nDim = std::min(phase->nDim(), nDim);
93 }
else if (nDim == 1) {
98 shared_ptr<Kinetics> kin(KineticsFactory::factory()->
newKinetics(kinType));
100 soln->setKinetics(kin);
102 for (
auto& phase : phases) {
103 kin->addThermo(phase);
110unique_ptr<Kinetics>
newKinetics(
const vector<ThermoPhase*>& phases,
114 warn_deprecated(
"newKinetics(vector<ThermoPhase*>&, AnyMap&, AnyMap&)",
115 "To be removed after Cantera 3.0; superseded by\nnewKinetics"
116 "(const vector<shared_ptr<ThermoPhase>>&, const AnyMap&, const AnyMap&).");
117 string kinType = phaseNode.
getString(
"kinetics",
"none");
118 kinType = KineticsFactory::factory()->canonicalize(kinType);
119 if (kinType ==
"none") {
122 for (
auto& phase : phases) {
123 nDim = std::min(phase->nDim(), nDim);
128 }
else if (nDim == 1) {
133 unique_ptr<Kinetics> kin(KineticsFactory::factory()->
newKinetics(kinType));
134 for (
auto& phase : phases) {
135 kin->addPhase(*phase);
142shared_ptr<Kinetics>
newKinetics(
const vector<shared_ptr<ThermoPhase>>& phases,
143 const string& filename,
144 const string& phase_name)
146 if (phase_name !=
"") {
147 warn_deprecated(
"newKinetics",
"After Cantera 3.0, the 'phase_name' argument "
148 "will be removed and an exception will be thrown if the reacting phase is "
149 "not the first phase in the 'phases' vector.");
151 string reaction_phase = (phase_name.empty()) ? phases.at(0)->name() : phase_name;
153 AnyMap& phaseNode = root[
"phases"].getMapWhere(
"name", reaction_phase);
157unique_ptr<Kinetics>
newKinetics(
const vector<ThermoPhase*>& phases,
158 const string& filename,
const string& phase_name)
160 warn_deprecated(
"newKinetics(vector<ThermoPhase*>&, const string&, const string&)",
161 "To be removed after Cantera 3.0; superseded by\nnewKinetics"
162 "(const vector<shared_ptr<ThermoPhase>>&, const string&, const string&).");
164 AnyMap& phaseNode = root[
"phases"].getMapWhere(
"name", phase_name);
171 phaseNode.
getBool(
"skip-undeclared-third-bodies",
false));
176 vector<string> sections, rules;
178 if (phaseNode.
hasKey(
"reactions")) {
179 const auto& reactionsNode = phaseNode.
at(
"reactions");
180 if (reactionsNode.is<
string>()) {
181 if (rootNode.
hasKey(
"reactions")) {
184 sections.push_back(
"reactions");
185 rules.push_back(reactionsNode.asString());
186 }
else if (reactionsNode.asString() !=
"none") {
188 "Phase entry implies existence of 'reactions' section "
189 "which does not exist in the current input file.");
191 }
else if (reactionsNode.is<vector<string>>()) {
193 for (
const auto& item : reactionsNode.as<vector<string>>()) {
194 sections.push_back(item);
195 rules.push_back(
"all");
197 }
else if (reactionsNode.is<vector<AnyMap>>()) {
200 for (
const auto& item : reactionsNode.as<vector<AnyMap>>()) {
201 sections.push_back(item.begin()->first);
202 rules.push_back(item.begin()->second.asString());
206 if (!phaseNode.
hasKey(
"kinetics")) {
209 }
else if (rootNode.
hasKey(
"reactions")) {
212 sections.push_back(
"reactions");
213 rules.push_back(
"all");
216 "Phase entry implies existence of 'reactions' section which "
217 "does not exist in the current input file. Add the field "
218 "'reactions: none' to the phase entry to specify a kinetics "
219 "model with no reactions.");
224 fmt::memory_buffer add_rxn_err;
225 for (
size_t i = 0; i < sections.size(); i++) {
226 if (rules[i] ==
"all") {
228 }
else if (rules[i] ==
"declared-species") {
230 }
else if (rules[i] ==
"none") {
234 "Unknown rule '{}' for adding species from the '{}' section.",
235 rules[i], sections[i]);
237 const auto& slash = boost::ifind_last(sections[i],
"/");
240 string fileName (sections[i].begin(), slash.begin());
241 string node(slash.end(), sections[i].end());
245 for (
const auto& R : reactions[node].asVector<
AnyMap>()) {
272 if (add_rxn_err.size()) {
273 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.
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, 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.
Kinetics * newKineticsMgr(const string &model)
Create a new kinetics manager.
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.
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
unique_ptr< Reaction > newReaction(const string &type)
Create a new empty Reaction object.
Contains declarations for string manipulation functions within Cantera.