From 71bf44d11f5b8b6981dc3a32fb72cee0a8d454e2 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 20 Dec 2018 17:15:31 -0500 Subject: [PATCH] [Input] Parse YAML entries for Species objects --- include/cantera/thermo/Species.h | 7 +++++++ src/thermo/Species.cpp | 21 +++++++++++++++++++++ test/thermo/phaseConstructors.cpp | 23 +++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/include/cantera/thermo/Species.h b/include/cantera/thermo/Species.h index c0d028395..8c5eb8507 100644 --- a/include/cantera/thermo/Species.h +++ b/include/cantera/thermo/Species.h @@ -55,12 +55,19 @@ public: shared_ptr 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 newSpecies(const XML_Node& species_node); +//! Create a new Species object from an AnyMap specification +unique_ptr newSpecies(const AnyMap& node); + //! Generate Species objects for all `` nodes in an XML document. //! //! The `` nodes are assumed to be children of the `` node diff --git a/src/thermo/Species.cpp b/src/thermo/Species.cpp index 08ca73490..81240a095 100644 --- a/src/thermo/Species.cpp +++ b/src/thermo/Species.cpp @@ -10,6 +10,7 @@ #include "cantera/base/ctml.h" #include #include +#include namespace Cantera { @@ -81,6 +82,26 @@ shared_ptr newSpecies(const XML_Node& species_node) return s; } +unique_ptr newSpecies(const AnyMap& node) +{ + unique_ptr s(new Species(node.at("name").asString(), + node.at("composition").asMap())); + + if (node.hasKey("thermo")) { + s->thermo = newSpeciesThermo(node.at("thermo").as()); + } 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 > getSpecies(const XML_Node& node) { std::vector > all_species; diff --git a/test/thermo/phaseConstructors.cpp b/test/thermo/phaseConstructors.cpp index 1c7e84412..2349e25f5 100644 --- a/test/thermo/phaseConstructors.cpp +++ b/test/thermo/phaseConstructors.cpp @@ -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