Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mpusz/units
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Jun 16, 2020
2 parents 0dafb13 + c97a438 commit 4a46f44
Show file tree
Hide file tree
Showing 18 changed files with 1,082 additions and 6 deletions.
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
104 changes: 104 additions & 0 deletions example/foot_pound_second.cpp
Original file line number Diff line number Diff line change
@@ -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 <units/format.h>
#include <units/physical/fps/density.h>
#include <units/physical/fps/length.h>
#include <units/physical/fps/mass.h>
#include <units/physical/fps/power.h>
#include <units/physical/fps/speed.h>
#include <units/physical/fps/volume.h>
#include <units/physical/si/length.h>
#include <units/physical/si/mass.h>
#include <units/physical/si/power.h>
#include <units/physical/si/speed.h>
#include <units/physical/si/volume.h>

#include <units/concepts.h>
#include <units/quantity.h>

#include <iostream>

using namespace units::physical;


// Some basic specs for the warship
struct Ship {
fps::length<fps::foot> length;
fps::length<fps::foot> draft;
fps::length<fps::foot> beam;

fps::speed<fps::foot_per_second> speed;
fps::mass<fps::pound> mass;

fps::length<fps::inch> mainGuns;
fps::mass<fps::pound> shellMass;
fps::speed<fps::foot_per_second> shellSpeed;
fps::power<fps::foot_poundal_per_second> power;


};

// Print 'a' in its current units and print its value cast to the units in each of Args
template<class ...Args, units::Quantity Q>
auto fmt_line(const Q a) {
return fmt::format("{:22}",a) + (fmt::format(",{:20}", units::quantity_cast<Args>(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<fps::length<fps::yard>, si::length<si::metre>>(ship.length))
<< fmt::format("{:20} : {}\n", "draft", fmt_line<fps::length<fps::yard>, si::length<si::metre>>(ship.draft))
<< fmt::format("{:20} : {}\n", "beam", fmt_line<fps::length<fps::yard>, si::length<si::metre>>(ship.beam))
<< fmt::format("{:20} : {}\n", "mass", fmt_line<fps::mass<fps::long_ton>, si::mass<si::tonne>>(ship.mass))
<< fmt::format("{:20} : {}\n", "speed", fmt_line<fps::speed<fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
<< fmt::format("{:20} : {}\n", "power", fmt_line<fps::power<fps::horse_power>, si::power<si::kilowatt>>(ship.power))
<< fmt::format("{:20} : {}\n", "main guns", fmt_line<fps::length<fps::inch>, si::length<si::millimetre>>(ship.mainGuns))
<< fmt::format("{:20} : {}\n", "fire shells weighing",fmt_line<fps::mass<fps::long_ton>, si::mass<si::kilogram>>(ship.shellMass))
<< fmt::format("{:20} : {}\n", "fire shells at",fmt_line<fps::speed<fps::mile_per_hour>, si::speed<si::kilometre_per_hour>>(ship.shellSpeed))
<< fmt::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume<si::cubic_metre>, si::volume<si::litre>>(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);

}
45 changes: 45 additions & 0 deletions src/include/units/physical/fps/acceleration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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/fps/speed.h>
#include <units/quantity.h>

namespace units::physical::fps {

struct foot_per_second_sq : unit<foot_per_second_sq> {};
struct dim_acceleration : physical::dim_acceleration<dim_acceleration, foot_per_second_sq, dim_length, dim_time> {};

template<Unit U, Scalar Rep = double>
using acceleration = quantity<dim_acceleration, U, Rep>;

inline namespace literals {

// ft_per_s2
constexpr auto operator"" q_ft_per_s2(unsigned long long l) { return acceleration<foot_per_second_sq, std::int64_t>(l); }
constexpr auto operator"" q_ft_per_s2(long double l) { return acceleration<foot_per_second_sq, long double>(l); }

} // namespace literals

} // namespace units::physical::fps
47 changes: 47 additions & 0 deletions src/include/units/physical/fps/area.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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/fps/length.h>
// #include <units/physical/si/area.h>
#include <units/quantity.h>

namespace units::physical::fps {

struct square_foot : unit<square_foot> {};
struct dim_area : physical::dim_area<dim_area, square_foot, dim_length> {};


template<Unit U, Scalar Rep = double>
using area = quantity<dim_area, U, Rep>;

inline namespace literals {

// ft2
constexpr auto operator"" q_ft2(unsigned long long l) { return area<square_foot, std::int64_t>(l); }
constexpr auto operator"" q_ft2(long double l) { return area<square_foot, long double>(l); }

}

} // namespace units::physical::fps
46 changes: 46 additions & 0 deletions src/include/units/physical/fps/density.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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/fps/mass.h>
#include <units/physical/fps/length.h>
#include <units/quantity.h>

namespace units::physical::fps {

struct pound_per_foot_cub : unit<pound_per_foot_cub> {};

struct dim_density : physical::dim_density<dim_density, pound_per_foot_cub, dim_mass, dim_length> {};

template<Unit U, Scalar Rep = double>
using density = quantity<dim_density, U, Rep>;

inline namespace literals {

constexpr auto operator"" q_lb_per_ft3(unsigned long long l) { return density<pound_per_foot_cub, std::int64_t>(l); }
constexpr auto operator"" q_lb_per_ft3(long double l) { return density<pound_per_foot_cub, long double>(l); }

} // namespace literals

} // namespace units::physical::fps
57 changes: 57 additions & 0 deletions src/include/units/physical/fps/energy.h
Original file line number Diff line number Diff line change
@@ -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/fps/force.h>
#include <units/physical/si/prefixes.h>
#include <units/quantity.h>

namespace units::physical::fps {

// https://en.wikipedia.org/wiki/Foot-poundal
struct foot_poundal : unit<foot_poundal> {};

struct dim_energy : physical::dim_energy<dim_energy, foot_poundal, dim_force, dim_length> {};

// https://en.wikipedia.org/wiki/Foot-pound_(energy)
struct foot_pound_force : noble_deduced_unit<foot_pound_force, dim_energy, pound_force, foot> {};



template<Unit U, Scalar Rep = double>
using energy = quantity<dim_energy, U, Rep>;


inline namespace literals {

constexpr auto operator"" q_ft_pdl(unsigned long long l) { return energy<foot_poundal, std::int64_t>(l); }
constexpr auto operator"" q_ft_pdl(long double l) { return energy<foot_poundal, long double>(l); }

// foot_pound force
constexpr auto operator"" q_ft_lbf(unsigned long long l) { return energy<foot_pound_force, std::int64_t>(l); }
constexpr auto operator"" q_ft_lbf(long double l) { return energy<foot_pound_force, long double>(l); }

} // namespace literals

} // namespace units::physical::fps
58 changes: 58 additions & 0 deletions src/include/units/physical/fps/force.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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/fps/acceleration.h>
#include <units/physical/fps/mass.h>
#include <units/physical/si/prefixes.h>
#include <units/quantity.h>

namespace units::physical::fps {

// https://en.wikipedia.org/wiki/Poundal
struct poundal : named_unit<poundal, "pdl", no_prefix> {};

// https://en.wikipedia.org/wiki/Pound_(force)
struct pound_force : named_scaled_unit<pound_force, "lbf", no_prefix, ratio<32'174'049, 1'000'000>, poundal> {};



struct dim_force : physical::dim_force<dim_force, poundal, dim_mass, dim_acceleration> {};

template<Unit U, Scalar Rep = double>
using force = quantity<dim_force, U, Rep>;

inline namespace literals {

// poundal
constexpr auto operator"" q_pdl(unsigned long long l) { return force<poundal, std::int64_t>(l); }
constexpr auto operator"" q_pdl(long double l) { return force<poundal, long double>(l); }

// pound force
constexpr auto operator"" q_lbf(unsigned long long l) { return force<pound_force, std::int64_t>(l); }
constexpr auto operator"" q_lbf(long double l) { return force<pound_force, long double>(l); }

} // namespace literals

} // namespace units::physical::fps
Loading

0 comments on commit 4a46f44

Please sign in to comment.