From b51005043cb21518c070e0b4744d885bf7040b67 Mon Sep 17 00:00:00 2001 From: hrocho Date: Mon, 29 Jan 2018 18:54:24 +0100 Subject: [PATCH] mf --- structures.h | 6 ++++++ tests/test_structures.cpp | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/structures.h b/structures.h index 6332d5b..b6053a4 100644 --- a/structures.h +++ b/structures.h @@ -117,6 +117,12 @@ namespace catima{ */ double weight_fraction(int i) const {return (atoms[i].stn<1.0)?atoms[i].stn:atoms[i].stn*atoms[i].A/M();}; + /** + * return molar fraction of i-th element + * @return molar fraction + */ + double molar_fraction(int i) const {return (atoms[i].stn<1.0)?atoms[i].stn*M()/atoms[i].A:atoms[i].stn;}; + /** * @return number of components in Material */ diff --git a/tests/test_structures.cpp b/tests/test_structures.cpp index 951202a..58cbb9e 100644 --- a/tests/test_structures.cpp +++ b/tests/test_structures.cpp @@ -234,26 +234,35 @@ const lest::test specification[] = }, CASE("fraction calculation"){ catima::Material water1({ - {0, 1, 0.111894}, - {0, 8, 0.888106} + {0, 1, 0.111898}, + {0, 8, 0.888102} }); catima::Material water2({ {0, 1, 2}, {0, 8, 1} }); - EXPECT(water1.weight_fraction(0)==0.111894); - EXPECT(water2.weight_fraction(0)==approx(water1.weight_fraction(0)).R(1e-3)); - EXPECT(water1.weight_fraction(1)==0.888106); - EXPECT(water2.weight_fraction(1)==approx(water1.weight_fraction(1)).R(1e-3)); + EXPECT(water1.weight_fraction(0)==0.111898); + EXPECT(water2.weight_fraction(0)==approx(water1.weight_fraction(0)).R(1e-5)); + EXPECT(water1.weight_fraction(1)==0.888102); + EXPECT(water2.weight_fraction(1)==approx(water1.weight_fraction(1)).R(1e-5)); EXPECT(water2.M()==approx(18).epsilon(0.1)); EXPECT(water1.M()==approx(6.0,0.1)); EXPECT(water2.M()==approx(18,0.1)); + EXPECT(water1.molar_fraction(0)==approx(2.0/3.0).R(1e-5)); + EXPECT(water2.molar_fraction(0)==approx(2.0).R(1e-5)); + EXPECT(water1.molar_fraction(1)==approx(1.0/3.0).R(1e-5)); + EXPECT(water2.molar_fraction(1)==approx(1.0).R(1e-5)); + EXPECT(water1.molar_fraction(0)/water1.molar_fraction(1)==approx(2.0).R(1e-5)); + EXPECT(water2.molar_fraction(0)/water2.molar_fraction(1)==approx(2.0).R(1e-5)); + + catima::Material mat({12.0,6,1}); EXPECT(mat.M()==approx(12.0,0.001)); EXPECT(mat.weight_fraction(0)==approx(1.0).R(1e-6)); + } };