Cantera  3.1.0b1
Loading...
Searching...
No Matches
Species.cpp
1// This file is part of Cantera. See License.txt in the top-level directory or
2// at https://cantera.org/license.txt for license and copyright information.
3
12#include "cantera/base/global.h"
13#include <iostream>
14#include <limits>
15
16using namespace std;
17
18namespace Cantera {
19
20Species::Species(const string& name_, const Composition& comp_,
21 double charge_, double size_)
22 : name(name_)
23 , composition(comp_)
24 , charge(charge_)
25 , size(size_)
26{
27}
28
30 if (m_molecularWeight == Undef) {
31 double weight = 0.0;
32 const auto& elements = elementWeights();
33 for (const auto& [eName, stoich] : composition) {
34 auto search = elements.find(eName);
35 if (search != elements.end()) {
36 if (search->second < 0) {
37 throw CanteraError("setMolecularWeight",
38 "element '{}' has no stable isotopes", eName);
39 }
40 weight += search->second * stoich;
41 }
42 }
43 setMolecularWeight(weight);
44 }
45 return m_molecularWeight;
46}
47
48void Species::setMolecularWeight(double weight) {
49 if (m_molecularWeight != Undef) {
50 double maxWeight = max(weight, m_molecularWeight);
51 double weight_cmp = fabs(weight - m_molecularWeight) / maxWeight;
52 if (weight_cmp > 1.0e-9) {
54 "Species::setMolecularWeight",
55 "Molecular weight of species '{}' is changing from {} to {}.",
56 this->name,
58 weight
59 );
60 }
61 }
62
63 m_molecularWeight = weight;
64}
65
66AnyMap Species::parameters(const ThermoPhase* phase, bool withInput) const
67{
68 AnyMap speciesNode;
69 speciesNode["name"] = name;
70 speciesNode["composition"] = composition;
71 speciesNode["composition"].setFlowStyle();
72
73 if (charge != 0) {
74 speciesNode["charge"] = charge;
75 } else {
76 speciesNode.exclude("charge");
77 }
78
79 if (size != 1) {
80 speciesNode["size"] = size;
81 } else {
82 speciesNode.exclude("size");
83 }
84
85 if (thermo) {
86 AnyMap thermoNode = thermo->parameters(withInput);
87 if (thermoNode.size()) {
88 speciesNode["thermo"] = std::move(thermoNode);
89 }
90 }
91 if (transport) {
92 speciesNode["transport"] = transport->parameters(withInput);
93 }
94 if (phase) {
95 phase->getSpeciesParameters(name, speciesNode);
96 }
97 if (withInput && input.hasKey("equation-of-state")) {
98 auto& eosIn = input["equation-of-state"].asVector<AnyMap>();
99 for (const auto& eos : eosIn) {
100 auto& out = speciesNode["equation-of-state"].getMapWhere(
101 "model", eos["model"].asString(), true);
102 out.update(eos);
103 }
104 }
105 if (withInput) {
106 speciesNode.update(input);
107 }
108 return speciesNode;
109}
110
111unique_ptr<Species> newSpecies(const AnyMap& node)
112{
113 auto s = make_unique<Species>(node["name"].asString(),
114 node["composition"].asMap<double>());
115
116 if (node.hasKey("thermo")) {
117 s->thermo = newSpeciesThermo(node["thermo"].as<AnyMap>());
118 } else {
119 s->thermo = make_shared<SpeciesThermoInterpType>();
120 }
121
122 s->size = node.getDouble("sites", 1.0);
123 if (s->composition.find("E") != s->composition.end()) {
124 s->charge = -s->composition["E"];
125 }
126
127 if (node.hasKey("transport")) {
128 s->transport = newTransportData(node["transport"].as<AnyMap>());
129 s->transport->validate(*s);
130 }
131
132 // Store input parameters in the "input" map, unless they are stored in a
133 // child object
134 const static set<string> known_keys{
135 "thermo", "transport"
136 };
137 s->input.setUnits(node.units());
138 for (const auto& [key, child] : node) {
139 if (known_keys.count(key) == 0) {
140 s->input[key] = child;
141 }
142 }
143 s->input.applyUnits();
144 s->input.copyMetadata(node);
145
146 return s;
147}
148
149vector<shared_ptr<Species>> getSpecies(const AnyValue& items)
150{
151 vector<shared_ptr<Species>> all_species;
152 for (const auto& node : items.asVector<AnyMap>()) {
153 all_species.emplace_back(newSpecies(node));
154 }
155 return all_species;
156}
157
158}
Contains the getElementWeight function and the definitions of element constraint types.
Header for factory functions to build instances of classes that manage the standard-state thermodynam...
Pure Virtual Base class for individual species reference state thermodynamic managers and text for th...
Declaration for class Cantera::Species.
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.
Definition AnyMap.h:431
void exclude(const string &key)
Mark key as excluded from this map.
Definition AnyMap.cpp:1502
double getDouble(const string &key, double default_) const
If key exists, return it as a double, otherwise return default_.
Definition AnyMap.cpp:1580
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1477
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition AnyMap.h:640
void setFlowStyle(bool flow=true)
Use "flow" style when outputting this AnyMap to YAML.
Definition AnyMap.cpp:1794
void update(const AnyMap &other, bool keepExisting=true)
Add items from other to this AnyMap.
Definition AnyMap.cpp:1493
A wrapper for a variable whose type is determined at runtime.
Definition AnyMap.h:87
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.
Definition AnyMap.inl.h:109
Base class for exceptions thrown by Cantera classes.
double m_molecularWeight
The molecular weight of the species, in atomic mass units.
Definition Species.h:89
Composition composition
The elemental composition of the species.
Definition Species.h:45
string name
The name of the species.
Definition Species.h:41
void setMolecularWeight(double weight)
Set the molecular weight of the species.
Definition Species.cpp:48
double molecularWeight()
The molecular weight [amu] of the species.
Definition Species.cpp:29
double charge
The electrical charge on the species, in units of the elementary charge.
Definition Species.h:48
double size
The effective size of the species.
Definition Species.h:52
shared_ptr< SpeciesThermoInterpType > thermo
Thermodynamic data for the species.
Definition Species.h:80
AnyMap input
Input parameters used to define a species, for example from a YAML input file.
Definition Species.h:83
Base class for a phase with thermodynamic properties.
virtual void getSpeciesParameters(const string &name, AnyMap &speciesNode) const
Get phase-specific parameters of a Species object such that an identical one could be reconstructed a...
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
void warn_user(const string &method, const string &msg, const Args &... args)
Print a user warning raised from method as CanteraWarning.
Definition global.h:263
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const double Undef
Fairly random number to be used to initialize variables against to see if they are subsequently defin...
Definition ct_defs.h:164
vector< shared_ptr< Species > > getSpecies(const AnyValue &items)
Generate Species objects for each item (an AnyMap) in items.
Definition Species.cpp:149
unique_ptr< SpeciesThermoInterpType > newSpeciesThermo(const AnyMap &node)
Create a new SpeciesThermoInterpType object using the specified parameters.
unique_ptr< Species > newSpecies(const AnyMap &node)
Create a new Species object from an AnyMap specification.
Definition Species.cpp:111
const map< string, double > & elementWeights()
Get a map with the element and isotope symbols and names as keys and weights as values.
Definition Elements.cpp:246
unique_ptr< TransportData > newTransportData(const AnyMap &node)
Create a new TransportData object from an AnyMap specification.
map< string, double > Composition
Map from string names to doubles.
Definition ct_defs.h:177
Contains declarations for string manipulation functions within Cantera.