[Input] Parse YAML entries for Species objects

This commit is contained in:
Ray Speth 2018-12-20 17:15:31 -05:00
parent 84fa231ea2
commit 71bf44d11f
3 changed files with 51 additions and 0 deletions

View file

@ -55,12 +55,19 @@ public:
shared_ptr<SpeciesThermoInterpType> thermo;
//! Extra data used for specific models
//! @deprecated Superseded by #input. To be removed after Cantera 2.5.
AnyMap extra;
//! Input parameters used to define a species, e.g. from a YAML input file.
AnyMap input;
};
//! Create a new Species object from a 'species' XML_Node.
shared_ptr<Species> newSpecies(const XML_Node& species_node);
//! Create a new Species object from an AnyMap specification
unique_ptr<Species> newSpecies(const AnyMap& node);
//! Generate Species objects for all `<species>` nodes in an XML document.
//!
//! The `<species>` nodes are assumed to be children of the `<speciesData>` node

View file

@ -10,6 +10,7 @@
#include "cantera/base/ctml.h"
#include <iostream>
#include <limits>
#include <set>
namespace Cantera {
@ -81,6 +82,26 @@ shared_ptr<Species> newSpecies(const XML_Node& species_node)
return s;
}
unique_ptr<Species> newSpecies(const AnyMap& node)
{
unique_ptr<Species> s(new Species(node.at("name").asString(),
node.at("composition").asMap<double>()));
if (node.hasKey("thermo")) {
s->thermo = newSpeciesThermo(node.at("thermo").as<AnyMap>());
} else {
s->thermo.reset(new SpeciesThermoInterpType());
}
s->size = node.getDouble("sites", 1.0);
if (s->composition.find("E") != s->composition.end()) {
s->charge = -s->composition["E"];
}
s->input = node;
return s;
}
std::vector<shared_ptr<Species> > getSpecies(const XML_Node& node)
{
std::vector<shared_ptr<Species> > all_species;

View file

@ -750,4 +750,27 @@ TEST(PDSS_SSVol, fromScratch)
EXPECT_NEAR(p.entropy_mole(), 49848.48843237689, 2e-8);
}
TEST(Species, fromYaml)
{
AnyMap spec = AnyMap::fromYamlString(
"name: NO2\n"
"composition: {N: 1, O: 2}\n"
"units: {length: cm, quantity: mol}\n"
"molar-volume: 0.536\n"
"thermo:\n"
" model: NASA7\n"
" temperature-ranges: [200, 1000, 6000]\n"
" data:\n"
" - [3.944031200E+00, -1.585429000E-03, 1.665781200E-05, -2.047542600E-08,\n"
" 7.835056400E-12, 2.896617900E+03, 6.311991700E+00]\n"
" - [4.884754200E+00, 2.172395600E-03, -8.280690600E-07, 1.574751000E-10,\n"
" -1.051089500E-14, 2.316498300E+03, -1.174169500E-01]\n");
auto S = newSpecies(spec);
EXPECT_DOUBLE_EQ(S->thermo->minTemp(), 200);
EXPECT_EQ(S->composition.at("N"), 1);
// Check that units directive gets propagated to `input`
EXPECT_DOUBLE_EQ(S->input.convert("molar-volume", "m^3/kmol"), 0.000536);
}
} // namespace Cantera