From eaaee2e3056daafd63cd79a97275de5b6da5d013 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Mon, 19 Dec 2022 20:44:18 +0100 Subject: [PATCH] refactor: `total_energy` example refactored for V2 --- example/total_energy.cpp | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 example/total_energy.cpp diff --git a/example/total_energy.cpp b/example/total_energy.cpp new file mode 100644 index 00000000..8fec060c --- /dev/null +++ b/example/total_energy.cpp @@ -0,0 +1,114 @@ +// 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 + +template + requires units::is_scalar +inline constexpr bool units::is_vector = true; + +namespace { + +using namespace units; + +quantity_of auto total_energy(weak_quantity_of auto p, + weak_quantity_of auto m, + weak_quantity_of auto c) +{ + return quantity_cast(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c)))); +} + +void si_example() +{ + using namespace units::si::unit_symbols; + constexpr auto GeV = si::giga; + + constexpr quantity_of auto c = 1. * si::si2019::speed_of_light_in_vacuum; + const weak_quantity_of auto p1 = 4. * isq::mechanical_energy[GeV] / c; + const weak_quantity_of auto m1 = 3. * isq::mechanical_energy[GeV] / pow<2>(c); + const auto E = total_energy(p1, m1, c); + + std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n"; + + std::cout << "\n[in `GeV` and `c`]\n" + << "p = " << p1 << "\n" + << "m = " << m1 << "\n" + << "E = " << E << "\n"; + + const auto p2 = p1[GeV / (si::metre / s)]; + const auto m2 = m1[si::giga / pow<2>(si::metre / s)]; + const auto E2 = total_energy(p2, m2, c)[GeV]; + + std::cout << "\n[in `GeV`]\n" + << "p = " << p2 << "\n" + << "m = " << m2 << "\n" + << "E = " << E2 << "\n"; + + const auto p3 = p1[kg * si::metre / s]; + const auto m3 = m1[kg]; + const auto E3 = total_energy(p3, m3, c)[J]; + + std::cout << "\n[in SI units]\n" + << "p = " << p3 << "\n" + << "m = " << m3 << "\n" + << "E = " << E3 << "\n"; + + std::cout << "\n[converted from SI units back to GeV]\n" + << "E = " << quantity_cast(E3) << "\n"; +} + +void natural_example() +{ + using namespace units::natural; + using namespace units::natural::unit_symbols; + + constexpr quantity_of auto c = 1. * speed_of_light_in_vacuum; + const quantity_of auto p = 4. * momentum[GeV]; + const quantity_of auto m = 3. * mass[GeV]; + const auto E = total_energy(p, m, c); + + std::cout << "\n*** Natural units (c = " << c << ") ***\n" + << "p = " << p << "\n" + << "m = " << m << "\n" + << "E = " << E << "\n"; +} + +} // namespace + +int main() +{ + try { + si_example(); + natural_example(); + } catch (const std::exception& ex) { + std::cerr << "Unhandled std exception caught: " << ex.what() << '\n'; + } catch (...) { + std::cerr << "Unhandled unknown exception caught\n"; + } +}