-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathThermoFactory.cpp
354 lines (328 loc) · 14.9 KB
/
ThermoFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* @file ThermoFactory.cpp
* Definitions for the factory class that can create known ThermoPhase objects
* (see @ref thermoprops and class @link Cantera::ThermoFactory ThermoFactory@endlink).
*/
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.
#include "cantera/thermo/ThermoFactory.h"
#include "cantera/thermo/Species.h"
#include "cantera/thermo/speciesThermoTypes.h"
#include "cantera/thermo/SpeciesThermoFactory.h"
#include "cantera/thermo/PDSSFactory.h"
#include "cantera/thermo/MultiSpeciesThermo.h"
#include "cantera/thermo/IdealGasPhase.h"
#include "cantera/thermo/PlasmaPhase.h"
#include "cantera/thermo/IdealSolidSolnPhase.h"
#include "cantera/thermo/MargulesVPSSTP.h"
#include "cantera/thermo/RedlichKisterVPSSTP.h"
#include "cantera/thermo/PureFluidPhase.h"
#include "cantera/thermo/RedlichKwongMFTP.h"
#include "cantera/thermo/PengRobinson.h"
#include "cantera/thermo/SurfPhase.h"
#include "cantera/thermo/CoverageDependentSurfPhase.h"
#include "cantera/thermo/EdgePhase.h"
#include "cantera/thermo/MetalPhase.h"
#include "cantera/thermo/StoichSubstance.h"
#include "cantera/thermo/LatticeSolidPhase.h"
#include "cantera/thermo/LatticePhase.h"
#include "cantera/thermo/HMWSoln.h"
#include "cantera/thermo/DebyeHuckel.h"
#include "cantera/thermo/IdealMolalSoln.h"
#include "cantera/thermo/IdealSolnGasVPSS.h"
#include "cantera/thermo/WaterSSTP.h"
#include "cantera/thermo/BinarySolutionTabulatedThermo.h"
#include "cantera/base/stringUtils.h"
#include <boost/algorithm/string.hpp>
namespace Cantera
{
ThermoFactory* ThermoFactory::s_factory = 0;
std::mutex ThermoFactory::thermo_mutex;
ThermoFactory::ThermoFactory()
{
reg("none", []() { return new ThermoPhase(); });
addDeprecatedAlias("none", "ThermoPhase");
addDeprecatedAlias("none", "None");
reg("ideal-gas", []() { return new IdealGasPhase(); });
addDeprecatedAlias("ideal-gas", "IdealGas");
reg("plasma", []() { return new PlasmaPhase(); });
reg("ideal-surface", []() { return new SurfPhase(); });
addDeprecatedAlias("ideal-surface", "Surface");
addDeprecatedAlias("ideal-surface", "Surf");
reg("coverage-dependent-surface", []() { return new CoverageDependentSurfPhase(); });
reg("edge", []() { return new EdgePhase(); });
addDeprecatedAlias("edge", "Edge");
reg("electron-cloud", []() { return new MetalPhase(); });
addDeprecatedAlias("electron-cloud", "Metal");
reg("fixed-stoichiometry", []() { return new StoichSubstance(); });
addDeprecatedAlias("fixed-stoichiometry", "StoichSubstance");
reg("pure-fluid", []() { return new PureFluidPhase(); });
addDeprecatedAlias("pure-fluid", "PureFluid");
reg("compound-lattice", []() { return new LatticeSolidPhase(); });
addDeprecatedAlias("compound-lattice", "LatticeSolid");
reg("lattice", []() { return new LatticePhase(); });
addDeprecatedAlias("lattice", "Lattice");
reg("HMW-electrolyte", []() { return new HMWSoln(); });
addDeprecatedAlias("HMW-electrolyte", "HMW");
addDeprecatedAlias("HMW-electrolyte", "HMWSoln");
reg("ideal-condensed", []() { return new IdealSolidSolnPhase(); });
addDeprecatedAlias("ideal-condensed", "IdealSolidSolution");
addDeprecatedAlias("ideal-condensed", "IdealSolidSoln");
reg("Debye-Huckel", []() { return new DebyeHuckel(); });
addDeprecatedAlias("Debye-Huckel", "DebyeHuckel");
reg("ideal-molal-solution", []() { return new IdealMolalSoln(); });
addDeprecatedAlias("ideal-molal-solution", "IdealMolalSolution");
addDeprecatedAlias("ideal-molal-solution", "IdealMolalSoln");
reg("ideal-solution-VPSS", []() { return new IdealSolnGasVPSS(); });
reg("ideal-gas-VPSS", []() { return new IdealSolnGasVPSS(); });
addDeprecatedAlias("ideal-solution-VPSS", "IdealSolnVPSS");
addDeprecatedAlias("ideal-solution-VPSS", "IdealSolnGas");
addDeprecatedAlias("ideal-gas-VPSS", "IdealGasVPSS");
reg("Margules", []() { return new MargulesVPSSTP(); });
reg("Redlich-Kister", []() { return new RedlichKisterVPSSTP(); });
addDeprecatedAlias("Redlich-Kister", "RedlichKister");
reg("Redlich-Kwong", []() { return new RedlichKwongMFTP(); });
addDeprecatedAlias("Redlich-Kwong", "RedlichKwongMFTP");
addDeprecatedAlias("Redlich-Kwong", "RedlichKwong");
reg("liquid-water-IAPWS95", []() { return new WaterSSTP(); });
addDeprecatedAlias("liquid-water-IAPWS95", "PureLiquidWater");
addDeprecatedAlias("liquid-water-IAPWS95", "Water");
reg("binary-solution-tabulated", []() { return new BinarySolutionTabulatedThermo(); });
addDeprecatedAlias("binary-solution-tabulated", "BinarySolutionTabulatedThermo");
reg("Peng-Robinson", []() { return new PengRobinson(); });
}
ThermoFactory* ThermoFactory::factory()
{
std::unique_lock<std::mutex> lock(thermo_mutex);
if (!s_factory) {
s_factory = new ThermoFactory;
}
return s_factory;
}
void ThermoFactory::deleteFactory()
{
std::unique_lock<std::mutex> lock(thermo_mutex);
delete s_factory;
s_factory = 0;
}
shared_ptr<ThermoPhase> newThermoModel(const string& model)
{
shared_ptr<ThermoPhase> tptr(ThermoFactory::factory()->create(model));
return tptr;
}
shared_ptr<ThermoPhase> newThermo(const AnyMap& phaseNode, const AnyMap& rootNode)
{
if (!phaseNode.hasKey("kinetics") && phaseNode.hasKey("reactions")) {
throw InputFileError("newThermo", phaseNode["reactions"],
"Phase entry includes a 'reactions' field but does not "
"specify a kinetics model.");
}
string model = phaseNode["thermo"].asString();
shared_ptr<ThermoPhase> t = newThermoModel(model);
setupPhase(*t, phaseNode, rootNode);
return t;
}
shared_ptr<ThermoPhase> newThermo(const string& infile, const string& id)
{
size_t dot = infile.find_last_of(".");
string extension;
extension = toLowerCopy(infile.substr(dot+1));
string id_ = id;
if (id == "-") {
id_ = "";
}
if (extension == "cti" || extension == "xml") {
throw CanteraError("newThermo",
"The CTI and XML formats are no longer supported.");
}
AnyMap root = AnyMap::fromYamlFile(infile);
AnyMap& phase = root["phases"].getMapWhere("name", id_);
return newThermo(phase, root);
}
void addDefaultElements(ThermoPhase& thermo, const vector<string>& element_names) {
for (const auto& symbol : element_names) {
thermo.addElement(symbol);
}
}
void addElements(ThermoPhase& thermo, const vector<string>& element_names,
const AnyValue& elements, bool allow_default)
{
const auto& local_elements = elements.asMap("symbol");
for (const auto& symbol : element_names) {
if (local_elements.count(symbol)) {
auto& element = *local_elements.at(symbol);
double weight = element["atomic-weight"].asDouble();
long int number = element.getInt("atomic-number", 0);
double e298 = element.getDouble("entropy298", ENTROPY298_UNKNOWN);
thermo.addElement(symbol, weight, number, e298);
} else if (allow_default) {
thermo.addElement(symbol);
} else {
throw InputFileError("addElements", elements,
"Element '{}' not found", symbol);
}
}
}
void addSpecies(ThermoPhase& thermo, const AnyValue& names, const AnyValue& species)
{
if (names.is<vector<string>>()) {
// 'names' is a list of species names which should be found in 'species'
const auto& species_nodes = species.asMap("name");
for (const auto& name : names.asVector<string>()) {
if (species_nodes.count(name)) {
thermo.addSpecies(newSpecies(*species_nodes.at(name)));
} else {
throw InputFileError("addSpecies", names, species,
"Could not find a species named '{}'.", name);
}
}
} else if (names == "all") {
// The keyword 'all' means to add all species from this source
for (const auto& item : species.asVector<AnyMap>()) {
thermo.addSpecies(newSpecies(item));
}
} else {
throw InputFileError("addSpecies", names,
"Could not parse species declaration of type '{}'", names.type_str());
}
}
void setupPhase(ThermoPhase& thermo, const AnyMap& phaseNode, const AnyMap& rootNode)
{
thermo.setName(phaseNode["name"].asString());
if (phaseNode.hasKey("deprecated")) {
string msg = phaseNode["deprecated"].asString();
string filename = phaseNode.getString("__file__",
rootNode.getString("__file__", "unknown file"));
string method = fmt::format("{}/{}", filename, phaseNode["name"].asString());
warn_deprecated(method, phaseNode, msg);
}
// Add elements
if (phaseNode.hasKey("elements")) {
if (phaseNode.getBool("skip-undeclared-elements", false)) {
thermo.ignoreUndefinedElements();
} else {
thermo.throwUndefinedElements();
}
if (phaseNode["elements"].is<vector<string>>()) {
// 'elements' is a list of element symbols
if (rootNode.hasKey("elements")) {
addElements(thermo, phaseNode["elements"].asVector<string>(),
rootNode["elements"], true);
} else {
addDefaultElements(thermo, phaseNode["elements"].asVector<string>());
}
} else if (phaseNode["elements"].is<vector<AnyMap>>()) {
// Each item in 'elements' is a map with one item, where the key is
// a section in this file or another YAML file, and the value is a
// list of element symbols to read from that section
for (const auto& elemNode : phaseNode["elements"].asVector<AnyMap>()) {
const string& source = elemNode.begin()->first;
const auto& names = elemNode.begin()->second.asVector<string>();
const auto& slash = boost::ifind_last(source, "/");
if (slash) {
string fileName(source.begin(), slash.begin());
string node(slash.end(), source.end());
const AnyMap elements = AnyMap::fromYamlFile(fileName,
rootNode.getString("__file__", ""));
addElements(thermo, names, elements.at(node), false);
} else if (rootNode.hasKey(source)) {
addElements(thermo, names, rootNode.at(source), false);
} else if (source == "default") {
addDefaultElements(thermo, names);
} else {
throw InputFileError("setupPhase", elemNode,
"Could not find elements section named '{}'", source);
}
}
} else {
throw InputFileError("setupPhase", phaseNode["elements"],
"Could not parse elements declaration of type '{}'",
phaseNode["elements"].type_str());
}
} else {
// If no elements list is provided, just add elements as-needed from the
// default list.
thermo.addUndefinedElements();
}
// Add species
if (phaseNode.hasKey("species")) {
if (phaseNode["species"].is<vector<string>>()) {
// 'species' is a list of species names to be added from the current
// file's 'species' section
addSpecies(thermo, phaseNode["species"], rootNode["species"]);
} else if (phaseNode["species"].is<string>()) {
// 'species' is a keyword applicable to the current file's 'species'
// section
addSpecies(thermo, phaseNode["species"], rootNode["species"]);
} else if (phaseNode["species"].is<vector<AnyMap>>()) {
// Each item in 'species' is a map with one item, where the key is
// a section in this file or another YAML file, and the value is a
// list of species names to read from that section
for (const auto& speciesNode : phaseNode["species"].asVector<AnyMap>()) {
const string& source = speciesNode.begin()->first;
const auto& names = speciesNode.begin()->second;
const auto& slash = boost::ifind_last(source, "/");
if (slash) {
// source is a different input file
string fileName(source.begin(), slash.begin());
string node(slash.end(), source.end());
AnyMap species = AnyMap::fromYamlFile(fileName,
rootNode.getString("__file__", ""));
addSpecies(thermo, names, species[node]);
} else if (rootNode.hasKey(source)) {
// source is in the current file
addSpecies(thermo, names, rootNode[source]);
} else {
throw InputFileError("setupPhase", speciesNode,
"Could not find species section named '{}'", source);
}
}
} else {
throw InputFileError("setupPhase", phaseNode["species"],
"Could not parse species declaration of type '{}'",
phaseNode["species"].type_str());
}
} else if (rootNode.hasKey("species")) {
// By default, add all species from the 'species' section
addSpecies(thermo, AnyValue("all"), rootNode["species"]);
}
auto* vpssThermo = dynamic_cast<VPStandardStateTP*>(&thermo);
if (vpssThermo) {
for (size_t k = 0; k < thermo.nSpecies(); k++) {
unique_ptr<PDSS> pdss;
if (!thermo.species(k)->input.hasKey("equation-of-state")) {
throw InputFileError("setupPhase", thermo.species(k)->input,
"Species '{}' in use by a ThermoPhase model of type '{}'\n"
"must define an 'equation-of-state' field.",
thermo.speciesName(k), thermo.type());
}
// Use the first node which specifies a valid PDSS model
auto& eos = thermo.species(k)->input["equation-of-state"];
bool found = false;
for (auto& node : eos.asVector<AnyMap>()) {
string model = node["model"].asString();
if (PDSSFactory::factory()->exists(model)) {
pdss.reset(newPDSS(model));
pdss->setParameters(node);
found = true;
break;
}
}
if (!found) {
throw InputFileError("setupPhase", eos,
"Could not find an equation-of-state specification "
"which defines a known PDSS model.");
}
vpssThermo->installPDSS(k, std::move(pdss));
}
}
thermo.setParameters(phaseNode, rootNode);
thermo.initThermo();
if (phaseNode.hasKey("state")) {
auto node = phaseNode["state"].as<AnyMap>();
thermo.setState(node);
} else {
thermo.setState_TP(298.15, OneAtm);
}
}
}