From e6b34b6b1ba015c9ccbf686f85a90f7d323a5879 Mon Sep 17 00:00:00 2001 From: rbrugo Date: Tue, 7 Apr 2020 03:53:40 +0200 Subject: [PATCH] Added current density --- src/include/units/physical/dimensions.h | 6 +++ src/include/units/physical/si.h | 37 ++++++++------- .../units/physical/si/current_density.h | 47 +++++++++++++++++++ test/unit_test/runtime/fmt_units_test.cpp | 24 +++------- test/unit_test/static/si_test.cpp | 36 ++++---------- 5 files changed, 90 insertions(+), 60 deletions(-) create mode 100644 src/include/units/physical/si/current_density.h diff --git a/src/include/units/physical/dimensions.h b/src/include/units/physical/dimensions.h index e7ed11a7..254c44a3 100644 --- a/src/include/units/physical/dimensions.h +++ b/src/include/units/physical/dimensions.h @@ -131,6 +131,9 @@ struct dim_catalytic_activity : derived_dimension, exp E, DimensionOf M> struct dim_absorbed_dose : derived_dimension, exp> {}; +template I, DimensionOf A> +struct dim_current_density : derived_dimension, exp> {}; + } // namespace physical template @@ -217,4 +220,7 @@ concept CatalyticActivity = physical::QuantityOf concept AbsorbedDose = physical::QuantityOf; +template +concept CurrentDensity = physical::QuantityOf; + } // namespace units diff --git a/src/include/units/physical/si.h b/src/include/units/physical/si.h index 80144863..385e510b 100644 --- a/src/include/units/physical/si.h +++ b/src/include/units/physical/si.h @@ -22,20 +22,23 @@ #pragma once -#include "units/physical/si/area.h" -#include "units/physical/si/constants.h" -#include "units/physical/si/frequency.h" -#include "units/physical/si/momentum.h" -#include "units/physical/si/power.h" -#include "units/physical/si/velocity.h" -#include "units/physical/si/volume.h" -#include "units/physical/si/surface_tension.h" -#include "units/physical/si/density.h" -#include "units/physical/si/resistance.h" -#include "units/physical/si/voltage.h" -#include "units/physical/si/magnetic_induction.h" -#include "units/physical/si/magnetic_flux.h" -#include "units/physical/si/inductance.h" -#include "units/physical/si/conductance.h" -#include "units/physical/si/catalytic_activity.h" -#include "units/physical/si/absorbed_dose.h" +#include "si/area.h" +#include "si/constants.h" +#include "si/frequency.h" +#include "si/momentum.h" +#include "si/power.h" +#include "si/velocity.h" +#include "si/volume.h" +#include "si/surface_tension.h" +#include "si/pressure.h" +#include "si/density.h" +#include "si/resistance.h" +#include "si/voltage.h" +#include "si/capacitance.h" +#include "si/magnetic_induction.h" +#include "si/magnetic_flux.h" +#include "si/inductance.h" +#include "si/conductance.h" +#include "si/catalytic_activity.h" +#include "si/absorbed_dose.h" +#include "si/current_density.h" diff --git a/src/include/units/physical/si/current_density.h b/src/include/units/physical/si/current_density.h new file mode 100644 index 00000000..6bde78b0 --- /dev/null +++ b/src/include/units/physical/si/current_density.h @@ -0,0 +1,47 @@ +// The MIT License (MIT) +// +// Copyright (c) 2018 Mateusz Pusz +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include +#include +#include +#include +#include + +namespace units::si { + +struct ampere_per_metre_sq : unit {}; + +struct dim_current_density : physical::dim_current_density {}; + +template +using current_density = quantity; + +inline namespace literals { + +constexpr auto operator"" q_A_per_m2(unsigned long long l) { return current_density(l); } +constexpr auto operator"" q_A_per_m2(long double l) { return current_density(l); } + +} // namespace literals + +} // namespace units::si diff --git a/test/unit_test/runtime/fmt_units_test.cpp b/test/unit_test/runtime/fmt_units_test.cpp index 3c1216a6..e512178b 100644 --- a/test/unit_test/runtime/fmt_units_test.cpp +++ b/test/unit_test/runtime/fmt_units_test.cpp @@ -20,14 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include "units/physical/si/area.h" -#include "units/physical/si/constants.h" -#include "units/physical/si/frequency.h" -#include "units/physical/si/momentum.h" -#include "units/physical/si/power.h" -#include "units/physical/si/velocity.h" -#include "units/physical/si/volume.h" -#include "units/physical/si/surface_tension.h" +#include "units/physical/si.h" #include "units/physical/us/length.h" #include "units/physical/imperial/length.h" #include "units/physical/international/length.h" @@ -36,15 +29,6 @@ #include "units/physical/international/velocity.h" #include "units/physical/iau/length.h" #include "units/physical/typographic/length.h" -#include "units/physical/si/density.h" -#include "units/physical/si/resistance.h" -#include "units/physical/si/voltage.h" -#include "units/physical/si/magnetic_induction.h" -#include "units/physical/si/magnetic_flux.h" -#include "units/physical/si/inductance.h" -#include "units/physical/si/conductance.h" -#include "units/physical/si/catalytic_activity.h" -#include "units/physical/si/absorbed_dose.h" #include "units/format.h" #include @@ -249,6 +233,12 @@ TEST_CASE("fmt::format on synthesized unit symbols", "[text][fmt]") CHECK(fmt::format("{}", 1q_in + 1q_yd) == "37 in"); } + SECTION("current density") + { + CHECK(fmt::format("{}", 1q_A_per_m2) == "1 A/m²"); + CHECK(fmt::format("{:%q %AQ}", 1q_A_per_m2) == "1 A/m^2"); + } + SECTION("incoherent units with powers") { CHECK(fmt::format("{}", 1q_mi * 1q_mi * 1q_mi) == "1 [15900351812136/3814697265625 × 10⁹] m³"); diff --git a/test/unit_test/static/si_test.cpp b/test/unit_test/static/si_test.cpp index 1d690718..2a7bb62c 100644 --- a/test/unit_test/static/si_test.cpp +++ b/test/unit_test/static/si_test.cpp @@ -20,32 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include namespace { @@ -317,4 +292,13 @@ static_assert(2q_m * 5q_N_per_m == 10q_N); static_assert(detail::unit_text() == "N/m"); +// current density + +static_assert(12q_A_per_m2 == 60q_A / 5q_m2); +static_assert(1q_A_per_m2 == 1q_S * 1q_V / 1q_m2); + +/* #warning FIX THIS */ +/* static_assert(detail::unit_text() == basic_symbol_text("A · m²", "A/m^2")); */ + + } // namespace