Skip to content

Commit c8aea00

Browse files
committed
[Input/Test] Improve test coverage of YAML serialization
1 parent 96808a9 commit c8aea00

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

interfaces/cython/cantera/test/test_composite.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,31 @@ def test_yaml_surface(self):
545545
self.assertArrayNear(surf.forward_rate_constants,
546546
surf2.forward_rate_constants)
547547

548+
def test_yaml_eos(self):
549+
ice = ct.Solution('water.yaml', 'ice')
550+
ice.TP = 270, 2 * ct.one_atm
551+
ice.write_yaml('ice-generated.yaml', units={'length': 'mm', 'mass': 'g'})
552+
553+
ice2 = ct.Solution('ice-generated.yaml')
554+
self.assertNear(ice.density, ice2.density)
555+
self.assertNear(ice.entropy_mole, ice2.entropy_mole)
556+
557+
def test_yaml_inconsistent_species(self):
558+
gas = ct.Solution('h2o2.yaml')
559+
gas2 = ct.Solution('h2o2.yaml')
560+
gas2.name = 'modified'
561+
# modify the NASA coefficients for one species
562+
h2 = gas2.species('H2')
563+
nasa_coeffs = h2.thermo.coeffs
564+
nasa_coeffs[1] += 0.1
565+
nasa_coeffs[8] += 0.1
566+
h2.thermo = ct.NasaPoly2(h2.thermo.min_temp, h2.thermo.max_temp,
567+
h2.thermo.reference_pressure, nasa_coeffs)
568+
gas2.modify_species(gas2.species_index('H2'), h2)
569+
with self.assertRaisesRegex(ct.CanteraError, "different definitions"):
570+
gas.write_yaml('h2o2-error.yaml', phases=gas2)
571+
572+
548573
class TestSpeciesSerialization(utilities.CanteraTest):
549574
def test_species_simple(self):
550575
gas = ct.Solution('h2o2.yaml')

test/general/test_containers.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,50 @@ TEST(AnyMap, dumpYamlString)
385385
generated["species"].getMapWhere("name", "OH")["thermo"]["data"].asVector<vector_fp>());
386386
}
387387

388+
TEST(AnyMap, YamlFlowStyle)
389+
{
390+
AnyMap original;
391+
original["x"] = 3;
392+
original["y"] = true;
393+
original["z"] = AnyMap::fromYamlString("{zero: 1, half: 2}");
394+
original.setFlowStyle();
395+
std::string serialized = original.toYamlString();
396+
// The serialized version should contain two lines, and end with a newline.
397+
EXPECT_EQ(std::count(serialized.begin(), serialized.end(), '\n'), 2);
398+
AnyMap generated = AnyMap::fromYamlString(serialized);
399+
for (const auto& item : original) {
400+
EXPECT_TRUE(generated.hasKey(item.first));
401+
}
402+
}
403+
404+
TEST(AnyMap, nestedVectorsToYaml)
405+
{
406+
std::vector<std::string> words{"foo", "bar", "baz", "qux", "foobar"};
407+
std::vector<std::vector<std::string>> strings;
408+
std::vector<std::vector<bool>> booleans;
409+
std::vector<std::vector<long int>> integers;
410+
for (size_t i = 0; i < 3; i++) {
411+
strings.emplace_back();
412+
booleans.emplace_back();
413+
integers.emplace_back();
414+
for (size_t j = 0; j < 4; j++) {
415+
strings.back().push_back(words[(i + 3 * j) % words.size()]);
416+
booleans.back().push_back(i == j);
417+
integers.back().push_back(6*i + j);
418+
}
419+
}
420+
AnyMap original;
421+
original["strings"] = strings;
422+
original["booleans"] = booleans;
423+
original["integers"] = integers;
424+
std::string serialized = original.toYamlString();
425+
AnyMap generated = AnyMap::fromYamlString(serialized);
426+
427+
EXPECT_EQ(generated["strings"].asVector<std::vector<std::string>>(), strings);
428+
EXPECT_EQ(generated["booleans"].asVector<std::vector<bool>>(), booleans);
429+
EXPECT_EQ(generated["integers"].asVector<std::vector<long int>>(), integers);
430+
}
431+
388432
TEST(AnyMap, definedKeyOrdering)
389433
{
390434
AnyMap m = AnyMap::fromYamlString("{zero: 1, half: 2}");

test/general/test_units.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ TEST(Units, with_defaults1) {
5454
}
5555

5656
TEST(Units, with_defaults2) {
57-
UnitSystem U({"dyn/cm^2"});
57+
UnitSystem U({"dyn/cm^2", "K"});
5858
EXPECT_DOUBLE_EQ(U.convertTo(1.0, "Pa"), 0.1);
5959
EXPECT_DOUBLE_EQ(U.convertFrom(1.0, "Pa"), 10);
6060
EXPECT_DOUBLE_EQ(U.convertTo(1.0, "N/m^2"), 1.0);
61+
EXPECT_DOUBLE_EQ(U.convertTo(300.0, "K"), 300.0);
6162
}
6263

6364
TEST(Units, with_defaults_map) {
@@ -139,6 +140,12 @@ TEST(Units, activation_energies6) {
139140
EXPECT_DOUBLE_EQ(U.convertActivationEnergyTo(1, "eV"), 1.0);
140141
}
141142

143+
TEST(Units, activation_energies_bad) {
144+
UnitSystem U;
145+
EXPECT_THROW(U.convertActivationEnergyTo(1000, "kg"), CanteraError);
146+
EXPECT_THROW(U.convertActivationEnergyFrom(1000, "K^2"), CanteraError);
147+
}
148+
142149
TEST(Units, from_anymap) {
143150
AnyMap m = AnyMap::fromYamlString(
144151
"{p: 12 bar, v: 10, A: 1 cm^2, V: 1,"
@@ -180,6 +187,24 @@ TEST(Units, to_anymap) {
180187
EXPECT_DOUBLE_EQ(m["density"].asVector<double>()[1], 20.0 * 1e-6);
181188
}
182189

190+
TEST(Units, anymap_quantities) {
191+
AnyMap m;
192+
std::vector<AnyValue> values(2);
193+
values[0].setQuantity(8, "kg/m^3");
194+
values[1].setQuantity(12, "mg/cl");
195+
m["a"] = values;
196+
values.emplace_back("hello");
197+
m["b"] = values;
198+
m.applyUnits();
199+
EXPECT_TRUE(m["a"].is<vector_fp>());
200+
m.applyUnits();
201+
EXPECT_TRUE(m["a"].is<vector_fp>());
202+
auto converted = m["a"].asVector<double>();
203+
EXPECT_DOUBLE_EQ(converted[0], 8.0);
204+
EXPECT_DOUBLE_EQ(converted[1], 1.2);
205+
EXPECT_FALSE(m["b"].is<vector_fp>());
206+
}
207+
183208
TEST(Units, to_anymap_nested) {
184209
UnitSystem U1{"g", "cm", "mol"};
185210
UnitSystem U2{"mg", "km"};

test/kinetics/kineticsFromYaml.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cantera/kinetics/KineticsFactory.h"
77
#include "cantera/kinetics/ReactionFactory.h"
88
#include "cantera/thermo/ThermoFactory.h"
9+
#include "cantera/base/Array.h"
910

1011
using namespace Cantera;
1112

@@ -388,6 +389,17 @@ TEST_F(ReactionToYaml, TroeFalloff)
388389
compareReactions();
389390
}
390391

392+
TEST_F(ReactionToYaml, SriFalloff)
393+
{
394+
soln = newSolution("sri-falloff.xml");
395+
soln->thermo()->setState_TPY(1000, 2e5, "R1A: 0.1, R1B:0.2, H: 0.2, R2:0.5");
396+
duplicateReaction(0);
397+
EXPECT_TRUE(std::dynamic_pointer_cast<FalloffReaction>(duplicate));
398+
compareReactions();
399+
duplicateReaction(1);
400+
compareReactions();
401+
}
402+
391403
TEST_F(ReactionToYaml, chemicallyActivated)
392404
{
393405
soln = newSolution("chemically-activated-reaction.xml");
@@ -449,3 +461,26 @@ TEST_F(ReactionToYaml, electrochemical)
449461
compareReactions();
450462
compareReactions();
451463
}
464+
465+
TEST_F(ReactionToYaml, unconvertible1)
466+
{
467+
ElementaryReaction R({{"H2", 1}, {"OH", 1}},
468+
{{"H2O", 1}, {"H", 1}},
469+
Arrhenius(1e5, -1.0, 12.5));
470+
AnyMap params = R.parameters();
471+
UnitSystem U{"g", "cm", "mol"};
472+
params.setUnits(U);
473+
EXPECT_THROW(params.applyUnits(), CanteraError);
474+
}
475+
476+
TEST_F(ReactionToYaml, unconvertible2)
477+
{
478+
Array2D coeffs(2, 2, 1.0);
479+
ChebyshevReaction R({{"H2", 1}, {"OH", 1}},
480+
{{"H2O", 1}, {"H", 1}},
481+
ChebyshevRate(273, 3000, 1e2, 1e7, coeffs));
482+
UnitSystem U{"g", "cm", "mol"};
483+
AnyMap params = R.parameters();
484+
params.setUnits(U);
485+
EXPECT_THROW(params.applyUnits(), CanteraError);
486+
}

test/thermo/thermoToYaml.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ TEST_F(ThermoYamlRoundTrip, IdealMolalSolution)
411411
compareThermo(308, 1.1e5, "H2O(l): 0.95, H2S(aq): 0.01, CO2(aq): 0.04");
412412
}
413413

414+
TEST_F(ThermoYamlRoundTrip, IdealSolutionVpss)
415+
{
416+
roundtrip("thermo-models.yaml", "IdealSolnGas-liquid");
417+
compareThermo(320, 1.5e5, "Li(l):1.0");
418+
}
419+
414420
TEST_F(ThermoYamlRoundTrip, IonsFromNeutral)
415421
{
416422
roundtrip("thermo-models.yaml", "ions-from-neutral-molecule",

0 commit comments

Comments
 (0)