refactor: examples refactored to benefit from the latest features

This commit is contained in:
Mateusz Pusz
2023-02-14 12:58:54 +01:00
parent 079e640614
commit e98215c347
9 changed files with 29 additions and 33 deletions

View File

@ -55,9 +55,9 @@ public:
[[nodiscard]] constexpr QuantityOf<isq::weight> auto filled_weight() const [[nodiscard]] constexpr QuantityOf<isq::weight> auto filled_weight() const
{ {
const QuantityOf<isq::volume> auto volume = isq::volume(base_ * height_); const auto volume = isq::volume(base_ * height_);
const QuantityOf<isq::mass> auto mass = density_ * volume; const QuantityOf<isq::mass> auto mass = density_ * volume;
return quantity_cast<isq::weight>(mass * g); return isq::weight(mass * g);
} }
[[nodiscard]] constexpr quantity<isq::height[m]> fill_level(const quantity<isq::mass[kg]>& measured_mass) const [[nodiscard]] constexpr quantity<isq::height[m]> fill_level(const quantity<isq::mass[kg]>& measured_mass) const

View File

@ -40,7 +40,7 @@ int main()
constexpr auto R = isq::resistance(4.7 * si::kilo<si::ohm>); constexpr auto R = isq::resistance(4.7 * si::kilo<si::ohm>);
for (auto t = 0 * ms; t <= 50 * ms; ++t) { for (auto t = 0 * ms; t <= 50 * ms; ++t) {
const QuantityOf<isq::voltage> auto Vt = V0 * mp_units::exp(-t / (R * C)); const QuantityOf<isq::voltage> auto Vt = V0 * exp(-t / (R * C));
std::cout << "at " << t << " voltage is "; std::cout << "at " << t << " voltage is ";

View File

@ -61,7 +61,7 @@ altitude terrain_level_alt(const task& t, const flight_point& pos)
distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt) distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt)
{ {
const auto dist_to_finish = t.get_length() - pos.dist; const auto dist_to_finish = t.get_length() - pos.dist;
return distance((ground_alt + s.min_agl_height - pos.alt) / return distance(quantity_cast<isq::distance>(ground_alt + s.min_agl_height - pos.alt) /
((ground_alt - t.get_finish().alt) / dist_to_finish - 1 / glide_ratio(g.polar[0]))); ((ground_alt - t.get_finish().alt) / dist_to_finish - 1 / glide_ratio(g.polar[0])));
} }
@ -151,7 +151,8 @@ void estimate(timestamp start_ts, const glider& g, const weather& w, const task&
pos = tow(start_ts, pos, at); pos = tow(start_ts, pos, at);
// estimate the altitude needed to reach the finish line from this place // estimate the altitude needed to reach the finish line from this place
const altitude final_glide_alt = t.get_finish().alt + height(t.get_length() / glide_ratio(g.polar[0])); const altitude final_glide_alt =
t.get_finish().alt + quantity_cast<isq::height>(t.get_length()) / glide_ratio(g.polar[0]);
// how much height we still need to gain in the thermalls to reach the destination? // how much height we still need to gain in the thermalls to reach the destination?
height height_to_gain = final_glide_alt - pos.alt; height height_to_gain = final_glide_alt - pos.alt;

View File

@ -157,18 +157,14 @@ distance spherical_distance(position<T> from, position<T> to)
// const auto central_angle = 2 * asin(sqrt(0.5 - cos(lat2_rad - lat1_rad) / 2 + cos(lat1_rad) * cos(lat2_rad) * (1 // const auto central_angle = 2 * asin(sqrt(0.5 - cos(lat2_rad - lat1_rad) / 2 + cos(lat1_rad) * cos(lat2_rad) * (1
// - cos(lon2_rad - lon1_rad)) / 2)); // - cos(lon2_rad - lon1_rad)) / 2));
// TODO can we improve the below return quantity_cast<isq::distance>(earth_radius * central_angle);
// return quantity_cast<isq::distance>(earth_radius * central_angle);
return earth_radius.number() * central_angle * isq::distance[earth_radius.unit];
} else { } else {
// the haversine formula // the haversine formula
const auto sin_lat = sin((lat2_rad - lat1_rad) / 2); const auto sin_lat = sin((lat2_rad - lat1_rad) / 2);
const auto sin_lon = sin((lon2_rad - lon1_rad) / 2); const auto sin_lon = sin((lon2_rad - lon1_rad) / 2);
const auto central_angle = 2 * asin(sqrt(sin_lat * sin_lat + cos(lat1_rad) * cos(lat2_rad) * sin_lon * sin_lon)); const auto central_angle = 2 * asin(sqrt(sin_lat * sin_lat + cos(lat1_rad) * cos(lat2_rad) * sin_lon * sin_lon));
// TODO can we improve the below return quantity_cast<isq::distance>(earth_radius * central_angle);
// return quantity_cast<isq::distance>(earth_radius * central_angle);
return earth_radius.number() * central_angle * isq::distance[earth_radius.unit];
} }
} }

View File

@ -56,7 +56,7 @@ namespace glide_computer {
// https://en.wikipedia.org/wiki/Flight_planning#Units_of_measurement // https://en.wikipedia.org/wiki/Flight_planning#Units_of_measurement
inline constexpr struct mean_sea_level : mp_units::absolute_point_origin<mp_units::isq::height> { inline constexpr struct mean_sea_level : mp_units::absolute_point_origin<mp_units::isq::height> {
} mean_sea_level; } mean_sea_level;
QUANTITY_SPEC(rate_of_climb_speed, mp_units::isq::height / mp_units::isq::time); QUANTITY_SPEC(rate_of_climb_speed, mp_units::isq::speed, mp_units::isq::height / mp_units::isq::time);
// length // length
using distance = mp_units::quantity<mp_units::isq::distance[mp_units::si::kilo<mp_units::si::metre>]>; using distance = mp_units::quantity<mp_units::isq::distance[mp_units::si::kilo<mp_units::si::metre>]>;
@ -196,8 +196,7 @@ constexpr height agl(altitude glider_alt, altitude terrain_level) { return glide
inline mp_units::quantity<mp_units::isq::length[mp_units::si::kilo<mp_units::si::metre>]> length_3d(distance dist, inline mp_units::quantity<mp_units::isq::length[mp_units::si::kilo<mp_units::si::metre>]> length_3d(distance dist,
height h) height h)
{ {
// TODO Should we be able to calculate this on quantity of different kinds? What to return? return hypot(dist, h);
return hypot(quantity_cast<mp_units::isq::length>(dist), quantity_cast<mp_units::isq::length>(h));
} }
distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt); distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt);

View File

@ -31,7 +31,7 @@ using namespace mp_units;
constexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::distance> auto d, QuantityOf<isq::duration> auto t) constexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::distance> auto d, QuantityOf<isq::duration> auto t)
{ {
return quantity_cast<isq::speed>(d / t); return d / t;
} }
int main() int main()
@ -40,10 +40,10 @@ int main()
using namespace mp_units::international::unit_symbols; using namespace mp_units::international::unit_symbols;
constexpr auto v1 = 110 * (km / h); constexpr auto v1 = 110 * (km / h);
constexpr auto v2 = 70. * mph; constexpr auto v2 = 70 * mph;
constexpr auto v3 = avg_speed(220 * km, 2 * h); constexpr auto v3 = avg_speed(220. * km, 2 * h);
constexpr auto v4 = avg_speed(140 * mi, 2 * h); constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * isq::duration[h]);
constexpr auto v5 = value_cast<m / s>(v3); constexpr auto v5 = v3[m / s];
constexpr auto v6 = value_cast<m / s>(v4); constexpr auto v6 = value_cast<m / s>(v4);
constexpr auto v7 = value_cast<int>(v6); constexpr auto v7 = value_cast<int>(v6);

View File

@ -132,12 +132,12 @@ void example()
using namespace mp_units::si::unit_symbols; using namespace mp_units::si::unit_symbols;
const auto a = isq::acceleration(measurement{9.8, 0.1} * (m / s2)); const auto a = isq::acceleration(measurement{9.8, 0.1} * (m / s2));
const auto t = isq::time(measurement{1.2, 0.1} * s); const auto t = measurement{1.2, 0.1} * s;
const QuantityOf<isq::velocity> auto v = a * t; const QuantityOf<isq::velocity> auto v = a * t;
std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n'; std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n';
const auto length = isq::length(measurement{123., 1.} * m); const auto length = measurement{123., 1.} * m;
std::cout << "10 * " << length << " = " << 10 * length << '\n'; std::cout << "10 * " << length << " = " << 10 * length << '\n';
} }

View File

@ -41,8 +41,7 @@ int main()
const auto lever = isq_angle::position_vector(20 * cm); const auto lever = isq_angle::position_vector(20 * cm);
const auto force = isq_angle::force(500 * N); const auto force = isq_angle::force(500 * N);
const auto angle = isq_angle::angular_measure(90. * deg); const auto angle = isq_angle::angular_measure(90. * deg);
const auto torque = const auto torque = isq_angle::torque(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle));
quantity_cast<isq_angle::torque>(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle));
std::cout << "Applying a perpendicular force of " << force << " to a " << lever << " long lever results in " std::cout << "Applying a perpendicular force of " << force << " to a " << lever << " long lever results in "
<< torque[N * m / rad] << " of torque.\n"; << torque[N * m / rad] << " of torque.\n";

View File

@ -40,17 +40,18 @@ using namespace mp_units;
QuantityOf<isq::mechanical_energy> auto total_energy(QuantityOf<isq::momentum> auto p, QuantityOf<isq::mass> auto m, QuantityOf<isq::mechanical_energy> auto total_energy(QuantityOf<isq::momentum> auto p, QuantityOf<isq::mass> auto m,
QuantityOf<isq::speed> auto c) QuantityOf<isq::speed> auto c)
{ {
return quantity_cast<isq::mechanical_energy>(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c)))); return isq::mechanical_energy(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c))));
} }
void si_example() void si_example()
{ {
using namespace mp_units::si::unit_symbols; using namespace mp_units::si::unit_symbols;
constexpr auto GeV = si::giga<si::electronvolt>; constexpr auto GeV = si::giga<si::electronvolt>;
constexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum; constexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum;
const QuantityOf<isq::momentum> auto p1 = isq::mechanical_energy(4. * GeV) / c; auto c2 = pow<2>(c);
const QuantityOf<isq::mass> auto m1 = isq::mechanical_energy(3. * GeV) / pow<2>(c);
const auto p1 = isq::momentum(4. * GeV / c);
const QuantityOf<isq::mass> auto m1 = 3. * GeV / c2;
const auto E = total_energy(p1, m1, c); const auto E = total_energy(p1, m1, c);
std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n"; std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n";
@ -60,8 +61,8 @@ void si_example()
<< "m = " << m1 << "\n" << "m = " << m1 << "\n"
<< "E = " << E << "\n"; << "E = " << E << "\n";
const auto p2 = p1[GeV / (si::metre / s)]; const auto p2 = p1[GeV / (m / s)];
const auto m2 = m1[si::giga<si::electronvolt> / pow<2>(si::metre / s)]; const auto m2 = m1[GeV / pow<2>(m / s)];
const auto E2 = total_energy(p2, m2, c)[GeV]; const auto E2 = total_energy(p2, m2, c)[GeV];
std::cout << "\n[in `GeV`]\n" std::cout << "\n[in `GeV`]\n"
@ -69,7 +70,7 @@ void si_example()
<< "m = " << m2 << "\n" << "m = " << m2 << "\n"
<< "E = " << E2 << "\n"; << "E = " << E2 << "\n";
const auto p3 = p1[kg * si::metre / s]; const auto p3 = p1[kg * m / s];
const auto m3 = m1[kg]; const auto m3 = m1[kg];
const auto E3 = total_energy(p3, m3, c)[J]; const auto E3 = total_energy(p3, m3, c)[J];
@ -87,9 +88,9 @@ void natural_example()
using namespace mp_units::natural; using namespace mp_units::natural;
using namespace mp_units::natural::unit_symbols; using namespace mp_units::natural::unit_symbols;
constexpr QuantityOf<isq::speed> auto c = 1. * speed_of_light_in_vacuum; constexpr auto c = 1. * speed_of_light_in_vacuum;
const QuantityOf<isq::momentum> auto p = 4. * momentum[GeV]; const auto p = 4. * momentum[GeV];
const QuantityOf<isq::mass> auto m = 3. * mass[GeV]; const auto m = 3. * mass[GeV];
const auto E = total_energy(p, m, c); const auto E = total_energy(p, m, c);
std::cout << "\n*** Natural units (c = " << c << ") ***\n" std::cout << "\n*** Natural units (c = " << c << ") ***\n"