From 4d0d6b78a8fb217277dc01c8408649149cdf58c1 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Mon, 6 Mar 2023 14:49:38 +0100 Subject: [PATCH] refactor: `box_example` refactored to `storage_tank_example` --- example/CMakeLists.txt | 2 +- ...x_example.cpp => storage_tank_example.cpp} | 59 ++++++++++++------- 2 files changed, 39 insertions(+), 22 deletions(-) rename example/{box_example.cpp => storage_tank_example.cpp} (65%) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 7ff834cf..65f7f7fd 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -34,7 +34,7 @@ function(add_example target) endfunction() add_example(avg_speed mp-units::core-io mp-units::si mp-units::cgs mp-units::usc) -add_example(box_example mp-units::core-fmt mp-units::si) +add_example(storage_tank_example mp-units::core-fmt mp-units::si) add_example(capacitor_time_curve mp-units::core-io mp-units::si mp-units::utility) add_example( clcpp_response diff --git a/example/box_example.cpp b/example/storage_tank_example.cpp similarity index 65% rename from example/box_example.cpp rename to example/storage_tank_example.cpp index dbadd79c..21eb1a1d 100644 --- a/example/box_example.cpp +++ b/example/storage_tank_example.cpp @@ -42,20 +42,25 @@ using namespace mp_units::si::unit_symbols; inline constexpr auto g = 1 * si::standard_gravity; inline constexpr auto air_density = isq::mass_density(1.225 * (kg / m3)); -class Box { +class StorageTank { quantity base_; quantity height_; quantity density_ = air_density; public: - constexpr Box(const quantity& length, const quantity& width, - quantity height) : - base_(length * width), height_(std::move(height)) + constexpr StorageTank(const quantity& base, const quantity& height) : + base_(base), height_(height) { } + constexpr void set_contents_density(const quantity& density) + { + assert(density > air_density); + density_ = density; + } + [[nodiscard]] constexpr QuantityOf auto filled_weight() const { - const auto volume = isq::volume(base_ * height_); + const auto volume = isq::volume(base_ * height_); // TODO check if we can remove that cast const QuantityOf auto mass = density_ * volume; return isq::weight(mass * g); } @@ -69,11 +74,23 @@ public: { return (height_ - fill_level(measured_mass)) * base_; } +}; - constexpr void set_contents_density(const quantity& density_in) + +class CylindricalStorageTank : public StorageTank { +public: + constexpr CylindricalStorageTank(const quantity& radius, const quantity& height) : + StorageTank(std::numbers::pi * radius * radius, height) + { + } +}; + +class RectangularStorageTank : public StorageTank { +public: + constexpr RectangularStorageTank(const quantity& length, const quantity& width, + const quantity& height) : + StorageTank(length * width, height) { - assert(density_in > air_density); - density_ = density_in; } }; @@ -85,27 +102,27 @@ int main() using namespace mp_units; using namespace mp_units::si::unit_symbols; - const quantity height = 200. * mm; - auto box = Box(isq::length(1000. * mm), isq::width(500. * mm), height); - box.set_contents_density(1000.0 * isq::mass_density[kg / m3]); + const auto height = isq::height(200 * mm); + auto tank = RectangularStorageTank(isq::length(1000 * mm), isq::width(500 * mm), height); + tank.set_contents_density(1000 * isq::mass_density[kg / m3]); - const auto fill_time = 200. * s; // time since starting fill - const auto measured_mass = 20.0 * kg; // measured mass at fill_time - const auto fill_level = box.fill_level(measured_mass); - const auto spare_capacity = box.spare_capacity(measured_mass); - const auto filled_weight = box.filled_weight(); + const auto fill_time = 200 * s; // time since starting fill + const auto measured_mass = 20. * kg; // measured mass at fill_time + + const auto fill_level = tank.fill_level(measured_mass); + const auto spare_capacity = tank.spare_capacity(measured_mass); + const auto filled_weight = tank.filled_weight(); const QuantityOf auto input_flow_rate = measured_mass / fill_time; const QuantityOf auto float_rise_rate = fill_level / fill_time; const QuantityOf auto fill_time_left = (height / fill_level - 1) * fill_time; - const auto fill_percent = (fill_level / height)[percent]; + const auto fill_ratio = fill_level / height; - std::cout << "mp-units box example...\n"; - std::cout << STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent); + std::cout << STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_ratio[percent]); std::cout << STD_FMT::format("fill weight at {} = {} ({})\n", fill_time, filled_weight, filled_weight[N]); std::cout << STD_FMT::format("spare capacity at {} = {}\n", fill_time, spare_capacity); - std::cout << STD_FMT::format("input flow rate after {} = {}\n", fill_time, input_flow_rate); + std::cout << STD_FMT::format("input flow rate = {}\n", input_flow_rate); std::cout << STD_FMT::format("float rise rate = {}\n", float_rise_rate); - std::cout << STD_FMT::format("box full E.T.A. at current flow rate = {}\n", fill_time_left); + std::cout << STD_FMT::format("tank full E.T.A. at current flow rate = {}\n", fill_time_left[s]); }