diff --git a/src/physics/units.cpp b/src/physics/units.cpp index 5faa427..b9acaf9 100644 --- a/src/physics/units.cpp +++ b/src/physics/units.cpp @@ -1,56 +1,184 @@ /*------------------------------------------------------------------------------- Copyright (c) 2015 F. Georget , Princeton University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -----------------------------------------------------------------------------*/ #include "units.hpp" +#include + namespace specmicp { namespace units { scalar_t convert_pressure(scalar_t pressure_si, LengthUnit length_unit) { scalar_t pressure; switch (length_unit) { case LengthUnit::meter: pressure = pressure_si; break; case LengthUnit::decimeter: pressure = 1e-2*pressure_si; break; case LengthUnit::centimeter: pressure = 1e-4*pressure_si; } return pressure; } +void parse_amount_unit(std::string unit, AmountUnit& amounts) +{ + + auto n = unit.find("/"); + amounts.type = AmountUnitType::Unknown; + amounts.factor_si = -1; + scalar_t factor; + if (n == std::string::npos) + { + unit.erase(std::remove(unit.begin(), unit.end(), ' '), + unit.end()); + factor = is_mass_unit(unit); + if (factor > 0) + { + amounts.type = AmountUnitType::Mass; + amounts.factor_si = factor; + return; + } + factor = is_mole_unit(unit); + if (factor > 0) + { + amounts.type = AmountUnitType::NumberOfMoles; + amounts.factor_si = factor; + return; + } + factor = is_volume_unit(unit); + if (factor > 0) + { + amounts.type = AmountUnitType::Volume; + amounts.factor_si = factor; + return; + } + } + else + { + // composite unit + std::string numer = unit.substr(0, n); + numer.erase(std::remove(numer.begin(), numer.end(), ' '), + numer.end()); + std::string denom = unit.substr(n+1, unit.size()-n); + denom.erase(std::remove(denom.begin(), denom.end(), ' '), + denom.end()); + scalar_t factor_numer = -1; + scalar_t factor_denom = -1; + + factor_denom = is_volume_unit(denom); + if (factor_denom > 0) + { + // concentration + factor_numer = is_mass_unit(numer); + if (factor_numer > 0) + { + amounts.type = AmountUnitType::MassConcentration; + amounts.factor_si = factor_numer / factor_denom; + return; + } + factor_numer = is_mole_unit(numer); + if (factor_numer > 0) + { + amounts.type = AmountUnitType::MoleConcentration; + amounts.factor_si = factor_numer / factor_denom; + return; + } + return; + } + factor_denom = is_mass_unit(denom); + if (factor_denom > 0) + { + factor_numer = is_mole_unit(numer); + if (factor_numer > 0) + { + amounts.type = AmountUnitType::Molality; + amounts.factor_si = factor_numer / factor_denom; + return; + } + } + } +} + +scalar_t is_mass_unit(const std::string& unit) +{ + scalar_t factor = -1; + if (unit == "kg" or unit == "kilogram") + factor = 1; + else if (unit == "g" or unit == "gram") + factor = 1e-3; + return factor; +} + +scalar_t is_length_unit(const std::string& unit) +{ + scalar_t factor = -1; + if (unit == "m" or unit == "meter") + factor = 1; + else if (unit == "dm" or unit == "decimeter") + factor = 0.1; + else if (unit == "cm" or unit == "centimeter") + factor = 0.01; + else if (unit == "mm" or unit == "millimeter") + factor = 1e-3; + return factor; +} + +scalar_t is_volume_unit(const std::string& unit) +{ + scalar_t factor = -1; + if (unit == "m^3" or unit == "cubicmeter") + factor = 1; + else if (unit == "dm^3" or unit == "cubicdecimeter") + factor = 1e-3; + else if (unit == "cm^3" or unit == "cubiccentimeter") + factor = 1e-6; + else if (unit == "mm^3" or unit == "cubicmillimeter") + factor = 1e-9; + return factor; +} + +scalar_t is_mole_unit(const std::string &unit) +{ + scalar_t factor = -1; + if (unit == "mol" or unit == "moles") + factor = 1; + else if (unit == "mmol" or unit == "millimoles") + factor = 1e-3; + return factor; +} } //end namespace units } //end namespace specmicp diff --git a/src/physics/units.hpp b/src/physics/units.hpp index 73d4bb6..a2bde3a 100644 --- a/src/physics/units.hpp +++ b/src/physics/units.hpp @@ -1,129 +1,172 @@ /*------------------------------------------------------------------------------- Copyright (c) 2014,2015 F. Georget , Princeton University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -----------------------------------------------------------------------------*/ #ifndef SPECMICP_UNITS_UNITS #define SPECMICP_UNITS_UNITS #include "../types.hpp" //! \file physics/units.hpp //! \brief units management and conversion namespace specmicp { //! \namespace specmicp::units //! \brief Units management namespace units { //! units used for length enum class LengthUnit { meter, decimeter, centimeter }; //! units used for mass enum class MassUnit { kilogram, gram }; //! \brief Simple struct which contains the unit information struct UnitsSet { MassUnit mass; //!< The unit to use for mass LengthUnit length; //!< The unit to use for length (also area and volume) //! \brief Defaults are the SI unit system UnitsSet(): mass(MassUnit::kilogram), length(LengthUnit::meter) {} }; //! \brief Base class that handles the units //! //! To be inherited by other classes that need units class UnitBaseClass { public: UnitBaseClass() {} UnitBaseClass(UnitsSet units): m_units(units) {} //! \brief Return the units UnitsSet get_units() {return m_units;} //! \brief Set the units void set_units(UnitsSet units) {m_units = units;} //! \brief Return the Mass unit MassUnit mass_unit() const {return m_units.mass;} //! \brief Return the Length unit LengthUnit length_unit() const {return m_units.length;} //! \brief Return the mass unit MassUnit& mass_unit() {return m_units.mass;} //! \brief Return the length unit LengthUnit& length_unit() {return m_units.length;} private: UnitsSet m_units; }; + +//! \brief the type of an 'amount' unit +enum class AmountUnitType +{ + Mass, + Volume, + NumberOfMoles, + MoleConcentration, + MassConcentration, + Molality, + Unknown +}; + +//! \brief Description of an amount unit +struct AmountUnit +{ + AmountUnitType type; //!< The unit type + scalar_t factor_si; //!< unit*factor_si = SI unit +}; + +//! \brief Find the unit and factor of 'unit' +//! +//! \param unit the unit to analyse +//! \param amounts +void parse_amount_unit(std::string unit, AmountUnit& amounts); + +//! \brief Check if 'unit' is a mass unit +//! +//! \return if it is a mass unit return the factor to transform it to its SI equivalent, else return a negative number +scalar_t is_mass_unit(const std::string& unit); +//! \brief Check if 'unit' is a length unit +//! +//! \return if it is a length unit return the factor to transform it to its SI equivalent, else return a negative number +scalar_t is_length_unit(const std::string& unit); +//! \brief Check if 'unit' is a volume unit +//! +//! \return if it is a volume unit return the factor to transform it to its SI equivalent, else return a negative number +scalar_t is_volume_unit(const std::string& unit); +//! \brief Check if 'unit' is a mole unit +//! +//! \return if it is a mole unit return the factor to transform it to its SI equivalent, else return a negative number +scalar_t is_mole_unit(const std::string& unit); + // Temperature // ----------- //! Convert a temperature from Celsius to Kelvin inline constexpr scalar_t celsius(scalar_t tc) {return tc + 273.15;} //! Convert a temperature from Kelvin to Celsius inline constexpr scalar_t to_celsius(scalar_t tk) { return tk - 273.15;} // Volume // ------ //! Convert liter to cubic meter inline constexpr scalar_t liter(scalar_t vl) {return 1e-3*vl;} //! convert cubic meter to liter inline constexpr scalar_t to_liter(scalar_t vcm) {return 1e3*vcm;} // Pressure // -------- scalar_t SPECMICP_DLL_PUBLIC convert_pressure(scalar_t pressure_si, LengthUnit length_unit); } // end namespace units } // end namespace specmicp #endif // SPECMICP_UNITS_UNITS diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 46fe7cc..b586b58 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,78 +1,89 @@ ################## Tests ######################################## include(CatchTest) set(PROJECT_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${PROJECT_TEST_DIR}) add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}) # make check is also valid # Catch test #=========== +#common + +set( COMMON_TEST_DIR common ) + +add_catch_test(NAME common SOURCES + ${COMMON_TEST_DIR}/test_common.cpp + ${COMMON_TEST_DIR}/units.cpp + + LINK_LIBRARIES specmicp_common_static +) + # MiCPSolver # ---------- set(MICPSOLVER_TEST_DIR micpsolver) add_catch_test(NAME micpsolver SOURCES ${MICPSOLVER_TEST_DIR}/test_micpsolver.cpp ${MICPSOLVER_TEST_DIR}/condition_number.cpp ${MICPSOLVER_TEST_DIR}/ncp_functions.cpp ${MICPSOLVER_TEST_DIR}/micpsolver.cpp ) # ODEInt # ---------- set(ODEINT_TEST_DIR odeint) add_catch_test(NAME odeint SOURCES ${ODEINT_TEST_DIR}/test_odeint.cpp ${ODEINT_TEST_DIR}/butchertableau.cpp ${ODEINT_TEST_DIR}/embeddedrungekutta.cpp ) # Database # -------- add_subdirectory(database) # Specmicp : Adim system # ---------------------- add_catch_test(NAME specmicp_adim SOURCES specmicp/adim/test_specmicp_adim.cpp specmicp/adim/adimensional_system.cpp specmicp/adim/adimensional_system_solver.cpp specmicp/adim/adimensional_system_conditions.cpp specmicp/adim/adimensional_system_problem_solver.cpp specmicp/adim/adimensional_system_thermocarbo.cpp specmicp/adim/adimensional_system_carboalu.cpp specmicp/adim/adimensional_system_pcfm.cpp specmicp/adim/adimensional_system_oxydoreduc.cpp LINK_LIBRARIES ${SPECMICP_STATIC_LIBS} ) # Specmicp : Adim Kinetics # ------------------------ set(ADIMKINETICS_TEST_DIR ${PROJECT_TEST_DIR}/specmicp/adim_kinetics) add_catch_test(NAME specmicp_adim_kinetics SOURCES ${ADIMKINETICS_TEST_DIR}/test_specmicp_adim_kinetics.cpp ${ADIMKINETICS_TEST_DIR}/kinetics_variables.cpp LINK_LIBRARIES ${SPECMICP_STATIC_LIBS} ) # Reactmicp # --------- add_subdirectory( reactmicp ) diff --git a/src/physics/units.cpp b/tests/common/test_common.cpp similarity index 77% copy from src/physics/units.cpp copy to tests/common/test_common.cpp index 5faa427..95ba1db 100644 --- a/src/physics/units.cpp +++ b/tests/common/test_common.cpp @@ -1,56 +1,35 @@ /*------------------------------------------------------------------------------- Copyright (c) 2015 F. Georget , Princeton University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -----------------------------------------------------------------------------*/ -#include "units.hpp" - -namespace specmicp { -namespace units { - -scalar_t convert_pressure(scalar_t pressure_si, LengthUnit length_unit) -{ - scalar_t pressure; - switch (length_unit) { - case LengthUnit::meter: - pressure = pressure_si; - break; - case LengthUnit::decimeter: - pressure = 1e-2*pressure_si; - break; - case LengthUnit::centimeter: - pressure = 1e-4*pressure_si; - } - return pressure; -} - - -} //end namespace units -} //end namespace specmicp +#define CATCH_CONFIG_MAIN +#include "catch.hpp" + diff --git a/tests/common/units.cpp b/tests/common/units.cpp new file mode 100644 index 0000000..dcfc49e --- /dev/null +++ b/tests/common/units.cpp @@ -0,0 +1,106 @@ +/*------------------------------------------------------------------------------- + +Copyright (c) 2015 F. Georget , Princeton University +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-----------------------------------------------------------------------------*/ + +#include "catch.hpp" + +#include "physics/units.hpp" +#include + +using namespace specmicp::units; + +bool inline check_unit(const std::string& unit, AmountUnitType type, double factor) +{ + AmountUnit tmp; + parse_amount_unit(unit, tmp); + return (tmp.type == type and std::abs(tmp.factor_si-factor) <= DBL_EPSILON); +} + +TEST_CASE("Find units", "[Units]") { + + SECTION("is_volume_unit") { + CHECK(is_volume_unit("m^3") == 1.0); + CHECK(is_volume_unit("dm^3") == 1e-3); + CHECK(is_volume_unit("cm^3") == 1e-6); + CHECK(is_volume_unit("mm^3") == 1e-9); + CHECK(is_volume_unit("m") < 0); + + } + + SECTION("is_length_unit") { + CHECK(is_length_unit("m") == 1.0); + CHECK(is_length_unit("dm") == 0.1); + CHECK(is_length_unit("cm") == 0.01); + CHECK(is_length_unit("mm") == 1e-3); + CHECK(is_length_unit("m^3") < 0); + } + + SECTION("is_mass_unit") { + CHECK(is_mass_unit("kg") == 1.0); + CHECK(is_mass_unit("g") == 1e-3); + CHECK(is_mass_unit("m") < 0); + } + + SECTION("is_mole_unit") { + CHECK(is_mole_unit("mol") == 1.0); + CHECK(is_mole_unit("mmol") == 1e-3); + CHECK(is_mole_unit("kg") < 0); + } + + + SECTION("Find units") { + + CHECK(check_unit("kg", AmountUnitType::Mass, 1.0)); + CHECK(check_unit("g", AmountUnitType::Mass, 1e-3)); + + CHECK(check_unit(" m^3 ", AmountUnitType::Volume, 1.0)); + CHECK(check_unit("dm^3", AmountUnitType::Volume, 1e-3)); + CHECK(check_unit("cm^3 ", AmountUnitType::Volume, 1e-6)); + CHECK(check_unit("mm^3", AmountUnitType::Volume, 1e-9)); + + + CHECK(check_unit("mol ", AmountUnitType::NumberOfMoles, 1.0)); + CHECK(check_unit("mmol", AmountUnitType::NumberOfMoles, 1e-3)); + + + CHECK(check_unit("mol/kg", AmountUnitType::Molality, 1.0)); + CHECK(check_unit("mol/g", AmountUnitType::Molality, 1e3)); + + CHECK(check_unit("mol /cm^3", AmountUnitType::MoleConcentration, 1e6)); + + CHECK(check_unit("kg/ m^3", AmountUnitType::MassConcentration, 1.0)); + + CHECK(check_unit("oups", AmountUnitType::Unknown, -1.0)); + CHECK(check_unit("oups/ bummer", AmountUnitType::Unknown, -1.0)); + + } + +}