Skip to content

Commit

Permalink
[Input/Test] Improve test coverage of YAML serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Apr 13, 2021
1 parent 96808a9 commit c8aea00
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
25 changes: 25 additions & 0 deletions interfaces/cython/cantera/test/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,31 @@ def test_yaml_surface(self):
self.assertArrayNear(surf.forward_rate_constants,
surf2.forward_rate_constants)

def test_yaml_eos(self):
ice = ct.Solution('water.yaml', 'ice')
ice.TP = 270, 2 * ct.one_atm
ice.write_yaml('ice-generated.yaml', units={'length': 'mm', 'mass': 'g'})

ice2 = ct.Solution('ice-generated.yaml')
self.assertNear(ice.density, ice2.density)
self.assertNear(ice.entropy_mole, ice2.entropy_mole)

def test_yaml_inconsistent_species(self):
gas = ct.Solution('h2o2.yaml')
gas2 = ct.Solution('h2o2.yaml')
gas2.name = 'modified'
# modify the NASA coefficients for one species
h2 = gas2.species('H2')
nasa_coeffs = h2.thermo.coeffs
nasa_coeffs[1] += 0.1
nasa_coeffs[8] += 0.1
h2.thermo = ct.NasaPoly2(h2.thermo.min_temp, h2.thermo.max_temp,
h2.thermo.reference_pressure, nasa_coeffs)
gas2.modify_species(gas2.species_index('H2'), h2)
with self.assertRaisesRegex(ct.CanteraError, "different definitions"):
gas.write_yaml('h2o2-error.yaml', phases=gas2)


class TestSpeciesSerialization(utilities.CanteraTest):
def test_species_simple(self):
gas = ct.Solution('h2o2.yaml')
Expand Down
44 changes: 44 additions & 0 deletions test/general/test_containers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,50 @@ TEST(AnyMap, dumpYamlString)
generated["species"].getMapWhere("name", "OH")["thermo"]["data"].asVector<vector_fp>());
}

TEST(AnyMap, YamlFlowStyle)
{
AnyMap original;
original["x"] = 3;
original["y"] = true;
original["z"] = AnyMap::fromYamlString("{zero: 1, half: 2}");
original.setFlowStyle();
std::string serialized = original.toYamlString();
// The serialized version should contain two lines, and end with a newline.
EXPECT_EQ(std::count(serialized.begin(), serialized.end(), '\n'), 2);
AnyMap generated = AnyMap::fromYamlString(serialized);
for (const auto& item : original) {
EXPECT_TRUE(generated.hasKey(item.first));
}
}

TEST(AnyMap, nestedVectorsToYaml)
{
std::vector<std::string> words{"foo", "bar", "baz", "qux", "foobar"};
std::vector<std::vector<std::string>> strings;
std::vector<std::vector<bool>> booleans;
std::vector<std::vector<long int>> integers;
for (size_t i = 0; i < 3; i++) {
strings.emplace_back();
booleans.emplace_back();
integers.emplace_back();
for (size_t j = 0; j < 4; j++) {
strings.back().push_back(words[(i + 3 * j) % words.size()]);
booleans.back().push_back(i == j);
integers.back().push_back(6*i + j);
}
}
AnyMap original;
original["strings"] = strings;
original["booleans"] = booleans;
original["integers"] = integers;
std::string serialized = original.toYamlString();
AnyMap generated = AnyMap::fromYamlString(serialized);

EXPECT_EQ(generated["strings"].asVector<std::vector<std::string>>(), strings);
EXPECT_EQ(generated["booleans"].asVector<std::vector<bool>>(), booleans);
EXPECT_EQ(generated["integers"].asVector<std::vector<long int>>(), integers);
}

TEST(AnyMap, definedKeyOrdering)
{
AnyMap m = AnyMap::fromYamlString("{zero: 1, half: 2}");
Expand Down
27 changes: 26 additions & 1 deletion test/general/test_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ TEST(Units, with_defaults1) {
}

TEST(Units, with_defaults2) {
UnitSystem U({"dyn/cm^2"});
UnitSystem U({"dyn/cm^2", "K"});
EXPECT_DOUBLE_EQ(U.convertTo(1.0, "Pa"), 0.1);
EXPECT_DOUBLE_EQ(U.convertFrom(1.0, "Pa"), 10);
EXPECT_DOUBLE_EQ(U.convertTo(1.0, "N/m^2"), 1.0);
EXPECT_DOUBLE_EQ(U.convertTo(300.0, "K"), 300.0);
}

TEST(Units, with_defaults_map) {
Expand Down Expand Up @@ -139,6 +140,12 @@ TEST(Units, activation_energies6) {
EXPECT_DOUBLE_EQ(U.convertActivationEnergyTo(1, "eV"), 1.0);
}

TEST(Units, activation_energies_bad) {
UnitSystem U;
EXPECT_THROW(U.convertActivationEnergyTo(1000, "kg"), CanteraError);
EXPECT_THROW(U.convertActivationEnergyFrom(1000, "K^2"), CanteraError);
}

TEST(Units, from_anymap) {
AnyMap m = AnyMap::fromYamlString(
"{p: 12 bar, v: 10, A: 1 cm^2, V: 1,"
Expand Down Expand Up @@ -180,6 +187,24 @@ TEST(Units, to_anymap) {
EXPECT_DOUBLE_EQ(m["density"].asVector<double>()[1], 20.0 * 1e-6);
}

TEST(Units, anymap_quantities) {
AnyMap m;
std::vector<AnyValue> values(2);
values[0].setQuantity(8, "kg/m^3");
values[1].setQuantity(12, "mg/cl");
m["a"] = values;
values.emplace_back("hello");
m["b"] = values;
m.applyUnits();
EXPECT_TRUE(m["a"].is<vector_fp>());
m.applyUnits();
EXPECT_TRUE(m["a"].is<vector_fp>());
auto converted = m["a"].asVector<double>();
EXPECT_DOUBLE_EQ(converted[0], 8.0);
EXPECT_DOUBLE_EQ(converted[1], 1.2);
EXPECT_FALSE(m["b"].is<vector_fp>());
}

TEST(Units, to_anymap_nested) {
UnitSystem U1{"g", "cm", "mol"};
UnitSystem U2{"mg", "km"};
Expand Down
35 changes: 35 additions & 0 deletions test/kinetics/kineticsFromYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "cantera/kinetics/KineticsFactory.h"
#include "cantera/kinetics/ReactionFactory.h"
#include "cantera/thermo/ThermoFactory.h"
#include "cantera/base/Array.h"

using namespace Cantera;

Expand Down Expand Up @@ -388,6 +389,17 @@ TEST_F(ReactionToYaml, TroeFalloff)
compareReactions();
}

TEST_F(ReactionToYaml, SriFalloff)
{
soln = newSolution("sri-falloff.xml");
soln->thermo()->setState_TPY(1000, 2e5, "R1A: 0.1, R1B:0.2, H: 0.2, R2:0.5");
duplicateReaction(0);
EXPECT_TRUE(std::dynamic_pointer_cast<FalloffReaction>(duplicate));
compareReactions();
duplicateReaction(1);
compareReactions();
}

TEST_F(ReactionToYaml, chemicallyActivated)
{
soln = newSolution("chemically-activated-reaction.xml");
Expand Down Expand Up @@ -449,3 +461,26 @@ TEST_F(ReactionToYaml, electrochemical)
compareReactions();
compareReactions();
}

TEST_F(ReactionToYaml, unconvertible1)
{
ElementaryReaction R({{"H2", 1}, {"OH", 1}},
{{"H2O", 1}, {"H", 1}},
Arrhenius(1e5, -1.0, 12.5));
AnyMap params = R.parameters();
UnitSystem U{"g", "cm", "mol"};
params.setUnits(U);
EXPECT_THROW(params.applyUnits(), CanteraError);
}

TEST_F(ReactionToYaml, unconvertible2)
{
Array2D coeffs(2, 2, 1.0);
ChebyshevReaction R({{"H2", 1}, {"OH", 1}},
{{"H2O", 1}, {"H", 1}},
ChebyshevRate(273, 3000, 1e2, 1e7, coeffs));
UnitSystem U{"g", "cm", "mol"};
AnyMap params = R.parameters();
params.setUnits(U);
EXPECT_THROW(params.applyUnits(), CanteraError);
}
6 changes: 6 additions & 0 deletions test/thermo/thermoToYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ TEST_F(ThermoYamlRoundTrip, IdealMolalSolution)
compareThermo(308, 1.1e5, "H2O(l): 0.95, H2S(aq): 0.01, CO2(aq): 0.04");
}

TEST_F(ThermoYamlRoundTrip, IdealSolutionVpss)
{
roundtrip("thermo-models.yaml", "IdealSolnGas-liquid");
compareThermo(320, 1.5e5, "Li(l):1.0");
}

TEST_F(ThermoYamlRoundTrip, IonsFromNeutral)
{
roundtrip("thermo-models.yaml", "ions-from-neutral-molecule",
Expand Down

0 comments on commit c8aea00

Please sign in to comment.