Added charge density

This commit is contained in:
rbrugo
2020-04-09 18:40:16 +02:00
committed by Mateusz Pusz
parent 7803da0bdf
commit 1cf0074483
5 changed files with 91 additions and 0 deletions

View File

@@ -158,6 +158,13 @@ struct dim_energy_density : derived_dimension<Child, U, exp<E, 1>, exp<L, -3>> {
template<typename Child, Unit U, DimensionOf<dim_voltage> V, DimensionOf<dim_length> L>
struct dim_electric_field_strength : derived_dimension<Child, U, exp<V, 1>, exp<L, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_electric_charge> Q, DimensionOf<dim_length> L>
struct dim_charge_density : derived_dimension<Child, U, exp<Q, 1>, exp<L, -3>> {};
template<typename Child, Unit U, DimensionOf<dim_electric_charge> Q, DimensionOf<dim_length> L>
struct dim_surface_charge_density : derived_dimension<Child, U, exp<Q, 1>, exp<L, -2>> {};
} // namespace physical
template<typename T>
@@ -274,4 +281,10 @@ concept ThermalConductivity = physical::QuantityOf<T, physical::dim_thermal_cond
template<typename T>
concept ElectricFieldStrength = physical::QuantityOf<T, physical::dim_electric_field_strength>;
template<typename T>
concept ChargeDensity = physical::QuantityOf<T, physical::dim_charge_density>;
template<typename T>
concept SurfaceChargeDensity = physical::QuantityOf<T, physical::dim_surface_charge_density>;
} // namespace units

View File

@@ -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"

View File

@@ -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 <units/physical/dimensions.h>
#include <units/physical/si/electric_charge.h>
#include <units/physical/si/length.h>
#include <units/physical/si/prefixes.h>
#include <units/quantity.h>
namespace units::si {
struct coulomb_per_metre_cub : unit<coulomb_per_metre_cub> {};
struct coulomb_per_metre_sq : unit<coulomb_per_metre_sq> {};
struct dim_charge_density : physical::dim_charge_density<dim_charge_density, coulomb_per_metre_cub, dim_electric_charge, dim_length> {};
struct dim_surface_charge_density : physical::dim_surface_charge_density<dim_surface_charge_density, coulomb_per_metre_sq, dim_electric_charge, dim_length> {};
template<Unit U, Scalar Rep = double>
using charge_density = quantity<dim_charge_density, U, Rep>;
template<Unit U, Scalar Rep = double>
using surface_charge_density = quantity<dim_surface_charge_density, U, Rep>;
inline namespace literals {
// C/m³
constexpr auto operator"" q_C_per_m3(unsigned long long l) { return charge_density<coulomb_per_metre_cub, std::int64_t>(l); }
constexpr auto operator"" q_C_per_m3(long double l) { return charge_density<coulomb_per_metre_cub, long double>(l); }
// C/m²
constexpr auto operator"" q_C_per_m2(unsigned long long l) { return surface_charge_density<coulomb_per_metre_sq, std::int64_t>(l); }
constexpr auto operator"" q_C_per_m2(long double l) { return surface_charge_density<coulomb_per_metre_sq, long double>(l); }
} // namespace literals
} // namespace units::si

View File

@@ -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³");

View File

@@ -334,4 +334,16 @@ static_assert(1q_C * 10q_V_per_m * 3q_m == 30q_J);
static_assert(detail::unit_text<dim_electric_field_strength, volt_per_metre>() == "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<dim_charge_density, coulomb_per_metre_cub>() == basic_symbol_text("C/m³", "C/m^3"));
static_assert(detail::unit_text<dim_surface_charge_density, coulomb_per_metre_sq>() == basic_symbol_text("C/m²", "C/m^2"));
} // namespace