diff --git a/src/include/units/physical/dimensions.h b/src/include/units/physical/dimensions.h index 6298a5a9..9635a1e8 100644 --- a/src/include/units/physical/dimensions.h +++ b/src/include/units/physical/dimensions.h @@ -158,6 +158,13 @@ struct dim_energy_density : derived_dimension, exp> { template V, DimensionOf L> struct dim_electric_field_strength : derived_dimension, exp> {}; +template Q, DimensionOf L> +struct dim_charge_density : derived_dimension, exp> {}; + +template Q, DimensionOf L> +struct dim_surface_charge_density : derived_dimension, exp> {}; + + } // namespace physical template @@ -274,4 +281,10 @@ concept ThermalConductivity = physical::QuantityOf concept ElectricFieldStrength = physical::QuantityOf; +template +concept ChargeDensity = physical::QuantityOf; + +template +concept SurfaceChargeDensity = physical::QuantityOf; + } // namespace units diff --git a/src/include/units/physical/si.h b/src/include/units/physical/si.h index c88a0f07..58790a94 100644 --- a/src/include/units/physical/si.h +++ b/src/include/units/physical/si.h @@ -26,6 +26,7 @@ #include "si/area.h" #include "si/capacitance.h" #include "si/catalytic_activity.h" +#include "si/charge_density.h" #include "si/concentration.h" #include "si/conductance.h" #include "si/constants.h" diff --git a/src/include/units/physical/si/charge_density.h b/src/include/units/physical/si/charge_density.h new file mode 100644 index 00000000..db2f04fe --- /dev/null +++ b/src/include/units/physical/si/charge_density.h @@ -0,0 +1,57 @@ +// 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 coulomb_per_metre_cub : unit {}; +struct coulomb_per_metre_sq : unit {}; + +struct dim_charge_density : physical::dim_charge_density {}; +struct dim_surface_charge_density : physical::dim_surface_charge_density {}; + +template +using charge_density = quantity; + +template +using surface_charge_density = quantity; + +inline namespace literals { + +// C/m³ +constexpr auto operator"" q_C_per_m3(unsigned long long l) { return charge_density(l); } +constexpr auto operator"" q_C_per_m3(long double l) { return charge_density(l); } + +// C/m² +constexpr auto operator"" q_C_per_m2(unsigned long long l) { return surface_charge_density(l); } +constexpr auto operator"" q_C_per_m2(long double l) { return surface_charge_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 275768ff..86d70006 100644 --- a/test/unit_test/runtime/fmt_units_test.cpp +++ b/test/unit_test/runtime/fmt_units_test.cpp @@ -279,6 +279,14 @@ TEST_CASE("fmt::format on synthesized unit symbols", "[text][fmt]") CHECK(fmt::format("{}", 1q_V_per_m) == "1 V/m"); } + SECTION("charge density") + { + CHECK(fmt::format("{}", 1q_C_per_m3) == "1 C/m³"); + CHECK(fmt::format("{:%Q %Aq}", 1q_C_per_m3) == "1 C/m^3"); + CHECK(fmt::format("{}", 1q_C_per_m2) == "1 C/m²"); + CHECK(fmt::format("{:%Q %Aq}", 1q_C_per_m2) == "1 C/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 48f06ea8..6ded78b2 100644 --- a/test/unit_test/static/si_test.cpp +++ b/test/unit_test/static/si_test.cpp @@ -334,4 +334,16 @@ static_assert(1q_C * 10q_V_per_m * 3q_m == 30q_J); static_assert(detail::unit_text() == "V/m"); +// [surface] charge density + +static_assert(20.q_C / 40q_m3 == 0.5q_C_per_m3); +static_assert(10.q_C / 20q_m2 == 0.5q_C_per_m2); +static_assert(20.q_C_per_m3 == 10.q_C_per_m2 / 0.5q_m); +static_assert(1q_C / 1q_m / 1q_m == 1q_C / 1q_m2); +static_assert(1q_C_per_m2 == 1q_C_per_m3 * 1q_m); +static_assert(1q_V_per_m * 10q_C_per_m3 * 1q_m3 == 10q_N); + +static_assert(detail::unit_text() == basic_symbol_text("C/m³", "C/m^3")); +static_assert(detail::unit_text() == basic_symbol_text("C/m²", "C/m^2")); + } // namespace