From aa84dcb9efe35a86b76d92171dcbb18a302d7a13 Mon Sep 17 00:00:00 2001 From: Mike Ford Date: Sun, 31 May 2020 00:37:19 +0100 Subject: [PATCH] added example and some more units to fps system --- example/CMakeLists.txt | 1 + example/foot_pound_second.cpp | 104 ++++++++++++++++++++++ src/include/units/physical/fps/length.h | 23 +++++ src/include/units/physical/fps/mass.h | 22 ++++- src/include/units/physical/fps/power.h | 6 ++ src/include/units/physical/fps/pressure.h | 7 +- src/include/units/physical/fps/speed.h | 15 +++- src/include/units/physical/fps/time.h | 2 + 8 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 example/foot_pound_second.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 7d0e2403..1074642c 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -36,6 +36,7 @@ add_example(clcpp_response) add_example(conversion_factor) add_example(kalman_filter-alpha_beta_filter_example2) add_example(experimental_angle) +add_example(foot_pound_second) conan_check_testing(linear_algebra) add_example(linear_algebra) diff --git a/example/foot_pound_second.cpp b/example/foot_pound_second.cpp new file mode 100644 index 00000000..e54e54e6 --- /dev/null +++ b/example/foot_pound_second.cpp @@ -0,0 +1,104 @@ +// 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. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +using namespace units::physical; + + +// Some basic specs for the warship +struct Ship { + fps::length length; + fps::length draft; + fps::length beam; + + fps::speed speed; + fps::mass mass; + + fps::length mainGuns; + fps::mass shellMass; + fps::speed shellSpeed; + fps::power power; + + +}; + +// Print 'a' in its current units and print its value cast to the units in each of Args +template +auto fmt_line(const Q a) { + return fmt::format("{:22}",a) + (fmt::format(",{:20}", units::quantity_cast(a)) + ...); +} + +// Print the ship details in the units as defined in the Ship struct, in other imperial units, and in SI +std::ostream& print_details(std::string_view description, const Ship& ship) { + using namespace units::physical::fps::literals; + const auto waterDensity = 62.4q_lb_per_ft3; + std::cout << fmt::format("{}\n", description); + std::cout << fmt::format("{:20} : {}\n", "length", fmt_line, si::length>(ship.length)) + << fmt::format("{:20} : {}\n", "draft", fmt_line, si::length>(ship.draft)) + << fmt::format("{:20} : {}\n", "beam", fmt_line, si::length>(ship.beam)) + << fmt::format("{:20} : {}\n", "mass", fmt_line, si::mass>(ship.mass)) + << fmt::format("{:20} : {}\n", "speed", fmt_line, si::speed>(ship.speed)) + << fmt::format("{:20} : {}\n", "power", fmt_line, si::power>(ship.power)) + << fmt::format("{:20} : {}\n", "main guns", fmt_line, si::length>(ship.mainGuns)) + << fmt::format("{:20} : {}\n", "fire shells weighing",fmt_line, si::mass>(ship.shellMass)) + << fmt::format("{:20} : {}\n", "fire shells at",fmt_line, si::speed>(ship.shellSpeed)) + << fmt::format("{:20} : {}\n", "volume underwater", fmt_line, si::volume>(ship.mass / waterDensity)); + return std::cout; +} + +int main() +{ + using namespace units::physical::si::literals; + using namespace units::physical::fps::literals; + + + // KMS Bismark, using the units the Germans would use, taken from Wiki + auto bismark = Ship{.length{251.q_m}, .draft{9.3q_m}, .beam{36q_m}, .speed{56q_km_per_h}, .mass{50.3q_t}, .mainGuns{380q_mm}, .shellMass{800q_kg}, .shellSpeed{820.q_m_per_s}, .power{110.45q_kW}}; + + // USS Iowa, using units from the foot-pound-second system + auto iowa = Ship{.length{860.q_ft}, .draft{37.q_ft + 2.q_in}, .beam{108.q_ft + 2.q_in}, .speed{33q_knot}, .mass{57'540q_lton}, .mainGuns{16q_in}, .shellMass{2700q_lb}, .shellSpeed{2690.q_ft_per_s}, .power{212'000q_hp}}; + + // HMS King George V, using units from the foot-pound-second system + auto kgv = Ship{.length{745.1q_ft}, .draft{33.q_ft + 7.5q_in}, .beam{103.2q_ft + 2.5q_in}, .speed{28.3q_knot}, .mass{42'245q_lton}, .mainGuns{14q_in}, .shellMass{1'590q_lb}, .shellSpeed{2483.q_ft_per_s}, .power{110'000q_hp}}; + + print_details("KMS Bismark, defined in appropriate units from the SI system", bismark) << "\n\n"; + print_details("USS Iowa, defined in appropriate units foot-pound-second system", iowa) << "\n\n"; + print_details("HMS King George V, defined in appropriate units foot-pound-second system", kgv); + +} diff --git a/src/include/units/physical/fps/length.h b/src/include/units/physical/fps/length.h index c7e5a54c..1c962c08 100644 --- a/src/include/units/physical/fps/length.h +++ b/src/include/units/physical/fps/length.h @@ -31,6 +31,16 @@ namespace units::physical::fps { // https://en.wikipedia.org/wiki/Foot_(unit) struct foot : named_scaled_unit, si::metre> {}; +struct yard : named_scaled_unit, foot> {}; + +struct inch : named_scaled_unit, foot> {}; + +struct mile : named_scaled_unit, foot> {}; + +struct nautical_mile : named_scaled_unit, yard> {}; + + + struct dim_length : physical::dim_length {}; template @@ -38,10 +48,23 @@ using length = quantity; inline namespace literals { +constexpr auto operator"" q_in(unsigned long long l) { return length(l); } +constexpr auto operator"" q_in(long double l) { return length(l); } + // ft constexpr auto operator"" q_ft(unsigned long long l) { return length(l); } constexpr auto operator"" q_ft(long double l) { return length(l); } +constexpr auto operator"" q_yd(unsigned long long l) { return length(l); } +constexpr auto operator"" q_yd(long double l) { return length(l); } + +constexpr auto operator"" q_mile(unsigned long long l) { return length(l); } +constexpr auto operator"" q_mile(long double l) { return length(l); } + +constexpr auto operator"" q_naut_mi(unsigned long long l) { return length(l); } +constexpr auto operator"" q_naut_mi(long double l) { return length(l); } + + } } // namespace units::physical::fps diff --git a/src/include/units/physical/fps/mass.h b/src/include/units/physical/fps/mass.h index 7d57d181..2fe1c738 100644 --- a/src/include/units/physical/fps/mass.h +++ b/src/include/units/physical/fps/mass.h @@ -36,12 +36,32 @@ struct dim_mass : physical::dim_mass {}; template using mass = quantity; +struct ounce : named_scaled_unit, pound>{}; + +struct short_ton : named_scaled_unit, pound>{}; + +struct long_ton : named_scaled_unit, pound>{}; + inline namespace literals { -// g + +constexpr auto operator"" q_oz(unsigned long long l) { return mass(l); } +constexpr auto operator"" q_oz(long double l) { return mass(l); } + + +// lb constexpr auto operator"" q_lb(unsigned long long l) { return mass(l); } constexpr auto operator"" q_lb(long double l) { return mass(l); } + +constexpr auto operator"" q_ston(unsigned long long l) { return mass(l); } +constexpr auto operator"" q_ston(long double l) { return mass(l); } + + +constexpr auto operator"" q_lton(unsigned long long l) { return mass(l); } +constexpr auto operator"" q_lton(long double l) { return mass(l); } + + } } // namespace units::physical::fps diff --git a/src/include/units/physical/fps/power.h b/src/include/units/physical/fps/power.h index 0110360a..40df542d 100644 --- a/src/include/units/physical/fps/power.h +++ b/src/include/units/physical/fps/power.h @@ -35,6 +35,8 @@ struct dim_power : physical::dim_power {}; +struct horse_power : named_scaled_unit, foot_pound_force_per_second> {}; + template using power = quantity; @@ -49,6 +51,10 @@ constexpr auto operator"" q_ft_pdl_per_s(long double l) { return power(l); } constexpr auto operator"" q_ft_lbf_per_s(long double l) { return power(l); } + +constexpr auto operator"" q_hp(unsigned long long l) { return power(l); } +constexpr auto operator"" q_hp(long double l) { return power(l); } + } // namespace literals } // namespace units::physical::fps diff --git a/src/include/units/physical/fps/pressure.h b/src/include/units/physical/fps/pressure.h index deb705e0..7fc7e5af 100644 --- a/src/include/units/physical/fps/pressure.h +++ b/src/include/units/physical/fps/pressure.h @@ -32,14 +32,15 @@ namespace units::physical::fps { struct poundal_per_foot_sq : unit {}; -struct pound_force_per_foot_sq : named_scaled_unit, poundal_per_foot_sq> {}; - - struct dim_pressure : physical::dim_pressure {}; template using pressure = quantity; +struct pound_force_per_foot_sq : named_scaled_unit, poundal_per_foot_sq> {}; + +// struct pound_force_per_foot_sq : named_deduced_unit {}; + struct pound_force_per_inch_sq : named_scaled_unit, pound_force_per_foot_sq> {}; inline namespace literals { diff --git a/src/include/units/physical/fps/speed.h b/src/include/units/physical/fps/speed.h index 222cc96c..d0091760 100644 --- a/src/include/units/physical/fps/speed.h +++ b/src/include/units/physical/fps/speed.h @@ -35,12 +35,25 @@ struct dim_speed : physical::dim_speed using speed = quantity; +struct mile_per_hour : deduced_unit{}; + +struct nautical_mile_per_hour : deduced_unit{}; + +struct knot : alias_unit {}; + + inline namespace literals { -// cmps constexpr auto operator"" q_ft_per_s(unsigned long long l) { return speed(l); } constexpr auto operator"" q_ft_per_s(long double l) { return speed(l); } +constexpr auto operator"" q_mph(unsigned long long l) { return speed(l); } +constexpr auto operator"" q_mph(long double l) { return speed(l); } + +constexpr auto operator"" q_knot(unsigned long long l) { return speed(l); } +constexpr auto operator"" q_knot(long double l) { return speed(l); } + + } // namespace literals } // namespace units::physical::fps diff --git a/src/include/units/physical/fps/time.h b/src/include/units/physical/fps/time.h index 869897e0..4862c736 100644 --- a/src/include/units/physical/fps/time.h +++ b/src/include/units/physical/fps/time.h @@ -29,6 +29,8 @@ namespace units::physical::fps { using si::second; +using si::minute; +using si::hour; using si::dim_time; using si::time;