diff --git a/example/box_example.cpp b/example/box_example.cpp index 00923a81..79c93a4a 100644 --- a/example/box_example.cpp +++ b/example/box_example.cpp @@ -1,83 +1,87 @@ -#include #include +#include +#include #include #include -#include #include #include #include #include -#include #include +#include namespace { using namespace units::physical; -using namespace units::physical::si::literals; using m = si::metre; +using m2 = si::square_metre; +using m3 = si::cubic_metre; using kg = si::kilogram; using N = si::newton; -using m3 = si::cubic_metre; using kgpm3 = si::kilogram_per_metre_cub; inline constexpr auto g = si::si2019::standard_gravity<>; +inline constexpr si::density air_density(1.225); -} // namespace -struct Box { - static constexpr auto air_density = 1.225_q_kg_per_m3; +class Box { + si::area base_; + si::length height_; + si::density density_ = air_density; +public: + constexpr Box(const si::length& length, const si::length& width, si::length height) : base_(length * width), height_(std::move(height)) {} - si::length length; - si::length width; - si::length height; - - struct contents { - si::density density = air_density; - } contents; - - constexpr Box(const si::length& l, const si::length& w, const si::length& h) : length{l}, width{w}, height{h} {} - - constexpr si::force filled_weight() const + [[nodiscard]] constexpr si::force filled_weight() const { - const si::volume volume = length * width * height; - const si::mass mass = contents.density * volume; + const si::volume volume = base_ * height_; + const si::mass mass = density_ * volume; return mass * g; } - constexpr si::length fill_level(const si::mass& measured_mass) const + [[nodiscard]] constexpr si::length fill_level(const si::mass& measured_mass) const { - return height * (measured_mass * g) / filled_weight(); + return height_ * measured_mass * g / filled_weight(); } - constexpr si::volume spare_capacity(const si::mass& measured_mass) const + [[nodiscard]] constexpr si::volume spare_capacity(const si::mass& measured_mass) const { - return (height - fill_level(measured_mass)) * width * length; + return (height_ - fill_level(measured_mass)) * base_; } constexpr void set_contents_density(const si::density& density_in) { assert(density_in > air_density); - contents.density = density_in; + density_ = density_in; } }; -#include +} // namespace + int main() { - auto box = Box(1000.0_q_mm, 500.0_q_mm, 200.0_q_mm); + using namespace units; + using namespace si::literals; + + const si::length height(200.0_q_mm); + auto box = Box(1000.0_q_mm, 500.0_q_mm, height); box.set_contents_density(1000.0_q_kg_per_m3); - const auto fill_time = 200.0_q_s; // time since starting fill - const auto measured_mass = 20.0_q_kg; // measured mass at fill_time + const auto fill_time = 200.0_q_s; // time since starting fill + const auto measured_mass = 20.0_q_kg; // measured mass at fill_time + + const Length auto fill_level = box.fill_level(measured_mass); + const Dimensionless auto fill_percent = quantity_cast(fill_level / height); + const Volume auto spare_capacity = box.spare_capacity(measured_mass); + const auto input_flow_rate = measured_mass / fill_time; // unknown dimension + const Speed auto float_rise_rate = fill_level / fill_time; + const Time auto fill_time_left = (height / fill_level - 1) * fill_time; std::cout << "mp-units box example...\n"; - std::cout << "fill height at " << fill_time << " = " << box.fill_level(measured_mass) << " (" - << (box.fill_level(measured_mass) / box.height) * 100 << "% full)\n"; - std::cout << "spare_capacity at " << fill_time << " = " << box.spare_capacity(measured_mass) << '\n'; - std::cout << "input flow rate after " << fill_time << " = " << measured_mass / fill_time << '\n'; - std::cout << "float rise rate = " << box.fill_level(measured_mass) / fill_time << '\n'; - const auto fill_time_left = (box.height / box.fill_level(measured_mass) - 1) * fill_time; - std::cout << "box full E.T.A. at current flow rate = " << fill_time_left << '\n'; + std::cout << fmt::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent); + std::cout << fmt::format("spare_capacity at {} = {}\n", fill_time, spare_capacity); + std::cout << fmt::format("input flow rate after {} = {}\n", fill_time, input_flow_rate); + std::cout << fmt::format("float rise rate = {}\n", float_rise_rate); + std::cout << fmt::format("box full E.T.A. at current flow rate = {}\n", fill_time_left); }