Skip to content

Commit

Permalink
Conductance buildable from 1/resistance
Browse files Browse the repository at this point in the history
  • Loading branch information
bernedom committed Aug 3, 2019
1 parent 14e6844 commit 31f15c0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Unified usage of template arugments in unit_t
* electric conductance can be built from 1/Ohm (including all ratios)
* electric resistance can be built from 1/S (including all ratios)

## 1.1.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ All units that can be built from other units are also decayable to the respectiv
| Acceleration | a | m/s^2 | v / T | none | none |
| Electric charge | Q | C | I \* T | aC to EC | `*_coulomb_t` |
| Electric potential | U | V | P / I, E/Q | aV to EV | `*_volt_t` |
| Electric resistance | O* | Ohm (Ω) | U / I | aOhm to EOhm | `*_ohm_t` |
| Electric resistance | O* | Ohm (Ω) | U / I, 1/G | aOhm to EOhm | `*_ohm_t` |
| Electric conductance | G | S | I / U, 1/R | aS to ES | `*_siemens_t` |
| Electric capacity | C | F | Q / U | aF to EF | `*_farad_t` |
| Force | F | N | M \* a | aN to EN | `*_newton_t` |
Expand Down
9 changes: 4 additions & 5 deletions include/SI/electric_conductance.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "detail/unit.h"
#include "electric_current.h"
#include "electric_potential.h"
#include "electric_resistance.h"

namespace SI {

Expand Down Expand Up @@ -55,11 +54,11 @@ namespace detail {
BUILD_UNIT_FROM_DIVISON(electric_conductance_t, electric_current_t,
electric_potential_t)

/// Builds conductance from 1/resistance
/// Builds conductance from 1/resistance, to avoid include cycles the base
/// unit_t is used instead of the type alias electric_resistance_t
template <typename _Type, class _Ratio = std::ratio<1>>
constexpr auto
operator/(const _Type scalar,
const electric_resistance_t<_Type, _Ratio> &resistance) {
constexpr auto operator/(const _Type scalar,
const unit_t<'O', 1, _Type, _Ratio> &resistance) {
return electric_conductance_t<_Type, std::ratio<_Ratio::den, _Ratio::num>>{
scalar / resistance.raw_value()};
}
Expand Down
11 changes: 11 additions & 0 deletions include/SI/electric_resistance.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "electric_potential.h"

namespace SI {

/// @todo find a way to use Ohm as dimension symbol
template <typename _Type, class _Ratio = std::ratio<1>>
using electric_resistance_t = detail::unit_t<'O', 1, _Type, _Ratio>;
Expand Down Expand Up @@ -52,6 +53,16 @@ using exa_ohm_t = electric_resistance_t<_Type, std::exa>;
namespace detail {
BUILD_UNIT_FROM_DIVISON(electric_resistance_t, electric_potential_t,
electric_current_t)

/// Builds conductance from 1/conductance, to avoid include cycles the base
/// unit_t is used instead of the type alias electric_conductance_t
template <typename _Type, class _Ratio = std::ratio<1>>
constexpr auto
operator/(const _Type scalar,
const detail::unit_t<'G', 1, _Type, _Ratio> &conductance) {
return electric_resistance_t<_Type, std::ratio<_Ratio::den, _Ratio::num>>{
scalar / conductance.raw_value()};
}
} // namespace detail

inline namespace literals {
Expand Down
2 changes: 2 additions & 0 deletions include/SI/radioactivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace SI {

/// @todo check how to make this the same/an alias of hertz
/// @todo think of how this can be built from 1/T (probably goes along with
/// making it an alias of hertz)
template <typename _Type, class _Ratio = std::ratio<1>>
using radioactivity_t = detail::unit_t<'A', 1, _Type, _Ratio>;

Expand Down
2 changes: 1 addition & 1 deletion test/src/derived_unit_tests/electric_conductance_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ template <typename _ratio_lhs, typename _ratio_rhs> struct ratio_tuple {
} // namespace

TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a resistance value THEN "
"result is electric_conductance AND ratio is kept",
"result is electric_conductance AND ratio is inverse",
"[electric_conductance][operator/]",
(ratio_tuple<std::milli, std::kilo>),
(ratio_tuple<std::kilo, std::milli>),
Expand Down
43 changes: 43 additions & 0 deletions test/src/derived_unit_tests/electric_resistance_tests.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <catch.hpp>

#include <SI/electric_conductance.h>
#include <SI/electric_resistance.h>

using namespace SI::literals;
Expand Down Expand Up @@ -217,3 +218,45 @@ TEMPLATE_TEST_CASE("GIVEN a electric_potential value WHEN divided by a "
decltype(result),
const SI::electric_current_t<TestType, std::ratio<1>>>::value);
}

TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a conductance value THEN "
"result is an electric resistance value",
"[electric_resistance][operator/]", int64_t, long double) {

constexpr SI::electric_conductance_t<TestType, std::ratio<1>> conductance{5};
constexpr TestType scalar{10};

constexpr auto result = scalar / conductance;
constexpr SI::ohm_t<TestType> expected{2};
STATIC_REQUIRE(
std::is_same<decltype(result), const SI::ohm_t<TestType>>::value);
STATIC_REQUIRE(result == expected);
}

namespace {
/// helper struct to faciltate templated testcase
template <typename _ratio_lhs, typename _ratio_rhs> struct ratio_tuple {
using ratio_lhs = _ratio_lhs;
using ratio_rhs = _ratio_rhs;
};

} // namespace

TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a conductance value THEN "
"result is electric_resistance AND ratio is inverse",
"[electric_conductance][operator/]",
(ratio_tuple<std::milli, std::kilo>),
(ratio_tuple<std::kilo, std::milli>),
(ratio_tuple<std::ratio<1>, std::ratio<1>>)) {

constexpr auto conductance =
SI::electric_conductance_t<int64_t, typename TestType::ratio_lhs>{5};
constexpr int64_t scalar{10};

constexpr auto result = scalar / conductance;
constexpr SI::electric_resistance_t<int64_t, typename TestType::ratio_rhs>
expected{2};
STATIC_REQUIRE(std::ratio_equal<typename decltype(result)::ratio,
typename TestType::ratio_rhs>::value);
STATIC_REQUIRE(result == expected);
}

0 comments on commit 31f15c0

Please sign in to comment.