Loading [MathJax]/jax/output/HTML-CSS/config.js
Cantera  3.2.0a1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Species.cpp
Go to the documentation of this file.
1//! @file Species.cpp
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
14#include "cantera/base/global.h"
15#include <iostream>
16#include <limits>
17
18using namespace std;
19
20namespace Cantera {
21
22Species::Species(const string& name_, const Composition& comp_,
23 double charge_, double size_)
24 : name(name_)
25 , composition(comp_)
26 , charge(charge_)
27 , size(size_)
28{
29}
30
32 if (m_molecularWeight == Undef) {
33 double weight = 0.0;
34 const auto& elements = elementWeights();
35 for (const auto& [eName, stoich] : composition) {
36 auto search = elements.find(eName);
37 if (search != elements.end()) {
38 if (search->second < 0) {
39 throw CanteraError("setMolecularWeight",
40 "element '{}' has no stable isotopes", eName);
41 }
42 weight += search->second * stoich;
43 }
44 }
45 setMolecularWeight(weight);
46 }
47 return m_molecularWeight;
48}
49
50void Species::setMolecularWeight(double weight) {
51 if (m_molecularWeight != Undef) {
52 double maxWeight = max(weight, m_molecularWeight);
53 double weight_cmp = fabs(weight - m_molecularWeight) / maxWeight;
54 if (weight_cmp > 1.0e-9) {
56 "Species::setMolecularWeight",
57 "Molecular weight of species '{}' is changing from {} to {}.",
58 this->name,
60 weight
61 );
62 }
63 }
64
65 m_molecularWeight = weight;
66}
67
68AnyMap Species::parameters(const ThermoPhase* phase, bool withInput) const
69{
70 AnyMap speciesNode;
71 speciesNode["name"] = name;
72 speciesNode["composition"] = composition;
73 speciesNode["composition"].setFlowStyle();
74
75 if (charge != 0) {
76 speciesNode["charge"] = charge;
77 } else {
78 speciesNode.exclude("charge");
79 }
80
81 if (size != 1) {
82 speciesNode["size"] = size;
83 } else {
84 speciesNode.exclude("size");
85 }
86
87 if (thermo) {
88 AnyMap thermoNode = thermo->parameters(withInput);
89 if (thermoNode.size()) {
90 speciesNode["thermo"] = std::move(thermoNode);
91 }
92 }
93 if (transport) {
94 speciesNode["transport"] = transport->parameters(withInput);
95 }
96 if (phase) {
97 phase->getSpeciesParameters(name, speciesNode);
98 }
99 if (withInput && input.hasKey("equation-of-state")) {
100 auto& eosIn = input["equation-of-state"].asVector<AnyMap>();
101 for (const auto& eos : eosIn) {
102 auto& out = speciesNode["equation-of-state"].getMapWhere(
103 "model", eos["model"].asString(), true);
104 out.update(eos);
105 }
106 }
107 if (withInput) {
108 speciesNode.update(input);
109 }
110 return speciesNode;
111}
112
113unique_ptr<Species> newSpecies(const AnyMap& node)
114{
115 auto s = make_unique<Species>(node["name"].asString(),
116 node["composition"].asMap<double>());
117
118 if (node.hasKey("thermo")) {
119 s->thermo = newSpeciesThermo(node["thermo"].as<AnyMap>());
120 } else {
121 s->thermo = make_shared<SpeciesThermoInterpType>();
122 }
123
124 s->size = node.getDouble("sites", 1.0);
125 if (s->composition.find("E") != s->composition.end()) {
126 s->charge = -s->composition["E"];
127 }
128
129 if (node.hasKey("transport")) {
130 s->transport = newTransportData(node["transport"].as<AnyMap>());
131 s->transport->validate(*s);
132 }
133
134 // Store input parameters in the "input" map, unless they are stored in a
135 // child object
136 const static set<string> known_keys{
137 "thermo", "transport"
138 };
139 s->input.setUnits(node.units());
140 for (const auto& [key, child] : node) {
141 if (known_keys.count(key) == 0) {
142 s->input[key] = child;
143 }
144 }
145 s->input.applyUnits();
146 s->input.copyMetadata(node);
147
148 return s;
149}
150
151vector<shared_ptr<Species>> getSpecies(const AnyValue& items)
152{
153 vector<shared_ptr<Species>> all_species;
154 for (const auto& node : items.asVector<AnyMap>()) {
155 all_species.emplace_back(newSpecies(node));
156 }
157 return all_species;
158}
159
160}
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:432
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:641
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:88
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:50
double molecularWeight()
The molecular weight [amu] of the species.
Definition Species.cpp:31
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:151
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:113
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.