2022-11-11 10:33:24 -10:00
|
|
|
// 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.
|
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
#include <mp-units/compat_macros.h>
|
2024-06-10 22:04:41 +02:00
|
|
|
#include <mp-units/ext/format.h>
|
2024-01-06 08:51:00 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
|
|
|
#include <numbers>
|
2024-04-24 20:53:54 +02:00
|
|
|
#include <string>
|
2024-01-06 08:51:00 +01:00
|
|
|
#ifdef MP_UNITS_MODULES
|
|
|
|
import mp_units;
|
|
|
|
#else
|
2023-05-25 12:47:10 +02:00
|
|
|
#include <mp-units/format.h>
|
2023-06-21 11:29:22 +02:00
|
|
|
#include <mp-units/math.h>
|
2024-04-25 16:30:52 +02:00
|
|
|
#include <mp-units/systems/isq.h>
|
|
|
|
#include <mp-units/systems/si.h>
|
2024-01-06 08:51:00 +01:00
|
|
|
#endif
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-09-13 09:01:03 +02:00
|
|
|
// allows standard gravity (acceleration) and weight (force) to be expressed with scalar representation
|
|
|
|
// types instead of requiring the usage of Linear Algebra library for this simple example
|
2022-12-20 17:26:55 +01:00
|
|
|
template<class T>
|
2022-12-29 20:18:48 +01:00
|
|
|
requires mp_units::is_scalar<T>
|
|
|
|
inline constexpr bool mp_units::is_vector<T> = true;
|
2022-12-20 17:26:55 +01:00
|
|
|
|
2022-11-11 10:33:24 -10:00
|
|
|
namespace {
|
|
|
|
|
2022-12-29 20:18:48 +01:00
|
|
|
using namespace mp_units;
|
|
|
|
using namespace mp_units::si::unit_symbols;
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-06-21 11:29:22 +02:00
|
|
|
// add a custom quantity type of kind isq::length
|
|
|
|
QUANTITY_SPEC(horizontal_length, isq::length);
|
|
|
|
|
|
|
|
// add a custom derived quantity type of kind isq::area
|
|
|
|
// with a constrained quantity equation
|
|
|
|
QUANTITY_SPEC(horizontal_area, isq::area, horizontal_length* isq::width);
|
|
|
|
|
2023-02-08 21:47:48 -08:00
|
|
|
inline constexpr auto g = 1 * si::standard_gravity;
|
2023-09-29 21:40:24 -06:00
|
|
|
inline constexpr auto air_density = isq::mass_density(1.225 * kg / m3);
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-03-06 14:49:38 +01:00
|
|
|
class StorageTank {
|
2023-07-10 16:54:25 +02:00
|
|
|
quantity<horizontal_area[m2]> base_;
|
2022-12-16 18:15:48 +01:00
|
|
|
quantity<isq::height[m]> height_;
|
2022-11-11 10:33:24 -10:00
|
|
|
quantity<isq::mass_density[kg / m3]> density_ = air_density;
|
|
|
|
public:
|
2023-07-10 16:54:25 +02:00
|
|
|
constexpr StorageTank(const quantity<horizontal_area[m2]>& base, const quantity<isq::height[m]>& height) :
|
2023-03-06 14:49:38 +01:00
|
|
|
base_(base), height_(height)
|
2022-11-11 10:33:24 -10:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-06 14:49:38 +01:00
|
|
|
constexpr void set_contents_density(const quantity<isq::mass_density[kg / m3]>& density)
|
|
|
|
{
|
|
|
|
assert(density > air_density);
|
|
|
|
density_ = density;
|
|
|
|
}
|
|
|
|
|
2023-02-02 14:56:29 +01:00
|
|
|
[[nodiscard]] constexpr QuantityOf<isq::weight> auto filled_weight() const
|
2022-11-11 10:33:24 -10:00
|
|
|
{
|
2023-03-06 14:49:38 +01:00
|
|
|
const auto volume = isq::volume(base_ * height_); // TODO check if we can remove that cast
|
2023-02-08 21:47:48 -08:00
|
|
|
const QuantityOf<isq::mass> auto mass = density_ * volume;
|
2023-02-14 12:58:54 +01:00
|
|
|
return isq::weight(mass * g);
|
2022-11-11 10:33:24 -10:00
|
|
|
}
|
|
|
|
|
2022-12-20 17:26:55 +01:00
|
|
|
[[nodiscard]] constexpr quantity<isq::height[m]> fill_level(const quantity<isq::mass[kg]>& measured_mass) const
|
2022-11-11 10:33:24 -10:00
|
|
|
{
|
|
|
|
return height_ * measured_mass * g / filled_weight();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr quantity<isq::volume[m3]> spare_capacity(const quantity<isq::mass[kg]>& measured_mass) const
|
|
|
|
{
|
|
|
|
return (height_ - fill_level(measured_mass)) * base_;
|
|
|
|
}
|
2023-03-06 14:49:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CylindricalStorageTank : public StorageTank {
|
|
|
|
public:
|
|
|
|
constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius, const quantity<isq::height[m]>& height) :
|
2023-06-21 11:29:22 +02:00
|
|
|
StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)), height)
|
2023-03-06 14:49:38 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-03-06 14:49:38 +01:00
|
|
|
class RectangularStorageTank : public StorageTank {
|
|
|
|
public:
|
2023-06-21 11:29:22 +02:00
|
|
|
constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length, const quantity<isq::width[m]>& width,
|
2023-03-06 14:49:38 +01:00
|
|
|
const quantity<isq::height[m]>& height) :
|
|
|
|
StorageTank(length * width, height)
|
2022-11-11 10:33:24 -10:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2023-09-13 09:01:03 +02:00
|
|
|
const quantity height = isq::height(200 * mm);
|
2023-06-21 11:29:22 +02:00
|
|
|
auto tank = RectangularStorageTank(horizontal_length(1'000 * mm), isq::width(500 * mm), height);
|
2023-09-29 21:40:24 -06:00
|
|
|
tank.set_contents_density(1'000 * kg / m3);
|
2023-03-06 14:49:38 +01:00
|
|
|
|
2023-09-13 09:01:03 +02:00
|
|
|
const auto duration = std::chrono::seconds{200};
|
|
|
|
const quantity fill_time = value_cast<int>(quantity{duration}); // time since starting fill
|
|
|
|
const quantity measured_mass = 20. * kg; // measured mass at fill_time
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-09-13 09:01:03 +02:00
|
|
|
const quantity fill_level = tank.fill_level(measured_mass);
|
|
|
|
const quantity spare_capacity = tank.spare_capacity(measured_mass);
|
|
|
|
const quantity filled_weight = tank.filled_weight();
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-02-08 21:47:48 -08:00
|
|
|
const QuantityOf<isq::mass_change_rate> auto input_flow_rate = measured_mass / fill_time;
|
|
|
|
const QuantityOf<isq::speed> auto float_rise_rate = fill_level / fill_time;
|
2023-07-10 16:53:49 +02:00
|
|
|
const QuantityOf<isq::time> auto fill_time_left = (height / fill_level - 1 * one) * fill_time;
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-09-13 09:01:03 +02:00
|
|
|
const quantity fill_ratio = fill_level / height;
|
2022-11-11 10:33:24 -10:00
|
|
|
|
2023-06-21 18:05:21 +02:00
|
|
|
std::cout << MP_UNITS_STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level,
|
2023-08-23 16:46:15 +02:00
|
|
|
fill_ratio.in(percent));
|
|
|
|
std::cout << MP_UNITS_STD_FMT::format("fill weight at {} = {} ({})\n", fill_time, filled_weight, filled_weight.in(N));
|
2023-06-21 18:05:21 +02:00
|
|
|
std::cout << MP_UNITS_STD_FMT::format("spare capacity at {} = {}\n", fill_time, spare_capacity);
|
|
|
|
std::cout << MP_UNITS_STD_FMT::format("input flow rate = {}\n", input_flow_rate);
|
|
|
|
std::cout << MP_UNITS_STD_FMT::format("float rise rate = {}\n", float_rise_rate);
|
2023-08-23 16:46:15 +02:00
|
|
|
std::cout << MP_UNITS_STD_FMT::format("tank full E.T.A. at current flow rate = {}\n", fill_time_left.in(s));
|
2022-11-11 10:33:24 -10:00
|
|
|
}
|