diff --git a/interfaces/cython/cantera/test/test_composite.py b/interfaces/cython/cantera/test/test_composite.py index ba7970d685..489a5620a1 100644 --- a/interfaces/cython/cantera/test/test_composite.py +++ b/interfaces/cython/cantera/test/test_composite.py @@ -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') diff --git a/test/general/test_containers.cpp b/test/general/test_containers.cpp index 88c754597d..791daad12d 100644 --- a/test/general/test_containers.cpp +++ b/test/general/test_containers.cpp @@ -385,6 +385,50 @@ TEST(AnyMap, dumpYamlString) generated["species"].getMapWhere("name", "OH")["thermo"]["data"].asVector()); } +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 words{"foo", "bar", "baz", "qux", "foobar"}; + std::vector> strings; + std::vector> booleans; + std::vector> 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>(), strings); + EXPECT_EQ(generated["booleans"].asVector>(), booleans); + EXPECT_EQ(generated["integers"].asVector>(), integers); +} + TEST(AnyMap, definedKeyOrdering) { AnyMap m = AnyMap::fromYamlString("{zero: 1, half: 2}"); diff --git a/test/general/test_units.cpp b/test/general/test_units.cpp index cb3779cf51..4759dfca17 100644 --- a/test/general/test_units.cpp +++ b/test/general/test_units.cpp @@ -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) { @@ -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," @@ -180,6 +187,24 @@ TEST(Units, to_anymap) { EXPECT_DOUBLE_EQ(m["density"].asVector()[1], 20.0 * 1e-6); } +TEST(Units, anymap_quantities) { + AnyMap m; + std::vector 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()); + m.applyUnits(); + EXPECT_TRUE(m["a"].is()); + auto converted = m["a"].asVector(); + EXPECT_DOUBLE_EQ(converted[0], 8.0); + EXPECT_DOUBLE_EQ(converted[1], 1.2); + EXPECT_FALSE(m["b"].is()); +} + TEST(Units, to_anymap_nested) { UnitSystem U1{"g", "cm", "mol"}; UnitSystem U2{"mg", "km"}; diff --git a/test/kinetics/kineticsFromYaml.cpp b/test/kinetics/kineticsFromYaml.cpp index 7db21dde4f..c6a89bbf3a 100644 --- a/test/kinetics/kineticsFromYaml.cpp +++ b/test/kinetics/kineticsFromYaml.cpp @@ -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; @@ -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(duplicate)); + compareReactions(); + duplicateReaction(1); + compareReactions(); +} + TEST_F(ReactionToYaml, chemicallyActivated) { soln = newSolution("chemically-activated-reaction.xml"); @@ -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); +} diff --git a/test/thermo/thermoToYaml.cpp b/test/thermo/thermoToYaml.cpp index fde4a64cd2..ebe348566b 100644 --- a/test/thermo/thermoToYaml.cpp +++ b/test/thermo/thermoToYaml.cpp @@ -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",