Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 5, 2019
1 parent 7c61d9c commit b9f7a2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 5 additions & 1 deletion include/cantera/base/Units.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ class UnitSystem
{
public:
//! Create a unit system with the specified default units
UnitSystem(std::initializer_list<std::string> units={});
UnitSystem(std::initializer_list<std::string> units);

//! Default constructor for unit system (needed as VS2019 does not
//! recognize optional argument with default value)
UnitSystem() : UnitSystem({}) {}

//! Set the default units to convert from when explicit units are not
//! provided. Defaults can be set for mass, length, time, quantity, energy,
Expand Down
22 changes: 11 additions & 11 deletions test/general/test_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using namespace Cantera;

TEST(Units, convert_to_base_units) {
UnitSystem U({});
UnitSystem U;
EXPECT_DOUBLE_EQ(U.convert(1.0, "Pa", "kg/m/s^2"), 1.0);
EXPECT_DOUBLE_EQ(U.convert(1.0, "J", "kg*m^2/s^2"), 1.0);
EXPECT_DOUBLE_EQ(U.convert(1.0, "ohm", "kg*m^2/s^3/A^2"), 1.0);
Expand All @@ -14,7 +14,7 @@ TEST(Units, convert_to_base_units) {
}

TEST(Units, notation) {
UnitSystem U({});
UnitSystem U;
EXPECT_DOUBLE_EQ(U.convert(2.0, "m^2", "m*m"), 2.0);
EXPECT_DOUBLE_EQ(U.convert(3.0, "", "kg/kg"), 3.0);
EXPECT_DOUBLE_EQ(U.convert(1.0, "1/m^2", "m^-2"), 1.0);
Expand All @@ -24,7 +24,7 @@ TEST(Units, notation) {
}

TEST(Units, basic_conversions) {
UnitSystem U({});
UnitSystem U;
EXPECT_DOUBLE_EQ(U.convert(100, "cm", "m"), 1.0);
EXPECT_DOUBLE_EQ(U.convert(2, "kmol", "mol"), 2000);
EXPECT_DOUBLE_EQ(U.convert(1000, "cal", "J"), 4184);
Expand All @@ -33,7 +33,7 @@ TEST(Units, basic_conversions) {
}

TEST(Units, prefixes) {
UnitSystem U({});
UnitSystem U;
EXPECT_DOUBLE_EQ(U.convert(1.0, "MJ", "J"), 1e6);
EXPECT_DOUBLE_EQ(U.convert(1.0, "nm", "cm"), 1e-7);
EXPECT_DOUBLE_EQ(U.convert(1.0, "m^2", "cm^2"), 1e4);
Expand Down Expand Up @@ -63,7 +63,7 @@ TEST(Units, with_defaults_map) {
{"length", "cm"}, {"mass", "g"}, {"quantity", "mol"},
{"pressure", "atm"}, {"energy", "J"}
};
UnitSystem U({});
UnitSystem U;
U.setDefaults(defaults);
EXPECT_DOUBLE_EQ(U.convert(1.0, "m"), 0.01);
EXPECT_DOUBLE_EQ(U.convert(1.0, "kmol/m^3"), 1000);
Expand All @@ -76,7 +76,7 @@ TEST(Units, with_defaults_map) {
}

TEST(Units, bad_defaults) {
UnitSystem U({});
UnitSystem U;
std::map<std::string, std::string> bad_key{{"length", "m"}, {"joy", "MJ"}};
EXPECT_THROW(U.setDefaults(bad_key), CanteraError);
std::map<std::string, std::string> bad_value{{"length", "m"}, {"time", "J"}};
Expand All @@ -85,15 +85,15 @@ TEST(Units, bad_defaults) {


TEST(Units, activation_energies1) {
UnitSystem U({});
UnitSystem U;
EXPECT_DOUBLE_EQ(U.convertActivationEnergy(1000, "J/kmol", "J/mol"), 1.0);
EXPECT_DOUBLE_EQ(U.convertActivationEnergy(100, "K", "K"), 100);
EXPECT_DOUBLE_EQ(U.convertActivationEnergy(500, "K", "J/kmol"), 500 * GasConstant);
EXPECT_DOUBLE_EQ(U.convertActivationEnergy(3, "J/mol", "K"), 3000 / GasConstant);
}

TEST(Units, activation_energies2) {
UnitSystem U({});
UnitSystem U;
U.setDefaultActivationEnergy("cal/mol");
U.setDefaults({"cm", "g", "J"});
EXPECT_DOUBLE_EQ(U.convertActivationEnergy(1000, "cal/mol"), 1000);
Expand All @@ -109,14 +109,14 @@ TEST(Units, activation_energies3) {
}

TEST(Units, activation_energies4) {
UnitSystem U({});
UnitSystem U;
U.setDefaultActivationEnergy("K");
EXPECT_DOUBLE_EQ(U.convertActivationEnergy(2000, "K"), 2000);
EXPECT_DOUBLE_EQ(U.convertActivationEnergy(2000, "J/kmol"), 2000 * GasConstant);
}

TEST(Units, activation_energies5) {
UnitSystem U({});
UnitSystem U;
std::map<std::string, std::string> defaults{
{"quantity", "mol"}, {"energy", "cal"}, {"activation-energy", "K"}
};
Expand All @@ -126,7 +126,7 @@ TEST(Units, activation_energies5) {
}

TEST(Units, activation_energies6) {
UnitSystem U({});
UnitSystem U;
std::map<std::string, std::string> defaults{
{"activation-energy", "eV"}
};
Expand Down

0 comments on commit b9f7a2f

Please sign in to comment.