#include #include #include #include "../c++/abstract/unit.hpp" #include "../c++/abstract/unit_print.hpp" #include "../c++/lbm_unit.hpp" #include "../c++/particle/schema.hpp" namespace { namespace sch { using namespace kel::lbm::sch; using T = Float64; } using kel::lbm::si_type::meter; using kel::lbm::si_type::second; using lbm_meter = kel::lbm::lbm_type::meter; template using Met = sch::UnitElement; template using Sec = sch::UnitElement; template using U = sch::Unit; template using Mul = typename kel::lbm::unit_multiplication::Schema; template using Div = typename kel::lbm::unit_division::Schema; /** * The unit algebra had no direct coverage: converter.cpp asserts one * conversion and args.cpp exercises unit_division once. Nothing pinned * multiplication, exponent cancellation, or the scaling comparison -- and the * scaling path could not even be instantiated, because unit_multiplication, * unit_division and the three print patterns all matched the two argument * form, which means scale 0 after the default argument is substituted. */ SAW_TEST("Unit / multiplication folds same base and same scale"){ static_assert(std::is_same_v>, U>>, U>>, "m * m must fold to m^2"); static_assert(std::is_same_v>, U>>, U>>, "km * km must fold to km^2, keeping the scale"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / different scales do not fold"){ // Same base quantity, different prefix: the factors must both survive // rather than silently combining, because nothing rescales the value. using Mixed = Mul>, U>>; static_assert(!std::is_same_v>>, "must not fold to m^2"); static_assert(!std::is_same_v>>, "must not fold to km^2"); static_assert(std::is_same_v, Met<1,3>>>, "both factors must survive, in order"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / division negates the exponent but keeps the scale"){ // (km)^-1 is scale 3 exponent -1. Negating the scale too would quietly // mean (mm)^-1. static_assert(std::is_same_v>, U>>, U>>, "km^2 / km must be km"); static_assert(std::is_same_v, U>>, U>>, "1 / km must be km^-1, not mm^-1"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / cancelling exponents drop out entirely"){ // m/m must be genuinely dimensionless, not m^0. static_assert(std::is_same_v>, U>>, sch::Pure>, "m / m must reduce to Pure"); static_assert(std::is_same_v>, U>>, sch::Pure>, "km / km must reduce to Pure"); // But only when the scales agree. static_assert(!std::is_same_v>, U>>, sch::Pure>, "km / m must not cancel"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / lattice and SI metres never fold"){ // This is what actually separates lattice from SI -- distinct base tag // types, not the scale. using LbmMet = sch::UnitElement; using Mixed = Mul>, U>; static_assert(!std::is_same_v>>, "must not fold"); // Canonical order, not encounter order: "meter_lbm" sorts before "meter_si". static_assert(std::is_same_v>>, "both survive, sorted"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / existing aliases are unchanged"){ // Regression pin: widening the patterns must not have altered any // composition that lbm_unit.hpp already produced. static_assert(std::is_same_v, U>>, "SiMeter"); static_assert(std::is_same_v, U, Sec<-1>>>, "SiVelocity"); static_assert( std::is_same_v, U, Sec<-1>>>, "SiKinematicViscosity"); static_assert( std::is_same_v, sch::SiSecond>, sch::SiVelocity>, "m / s must be the velocity alias exactly"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / a scaled unit prints with its prefix"){ std::stringstream plain, kilo, milli; kel::lbm::data>> p{2.0}; kel::lbm::data>> k{2.0}; kel::lbm::data>> m{2.0}; plain << p; kilo << k; milli << m; SAW_EXPECT(plain.str() == "2 [m_si]", "unscaled prints bare"); SAW_EXPECT(kilo.str() == "2 [km_si]", "kilo prints a k prefix"); SAW_EXPECT(milli.str() == "2 [mm_si]", "milli prints an m prefix"); // The whole point: a scaled unit must not render identically to an // unscaled one, or the output lies about the value. SAW_EXPECT(plain.str() != kilo.str(), "scaled must differ from unscaled"); } SAW_TEST("Unit / an off table scale prints explicitly"){ std::stringstream o; kel::lbm::data>> odd{2.0}; o << odd; // No SI letter exists for 10^4, so it must say so rather than drop it. SAW_EXPECT(o.str() == "2 [10^(4)*m_si]", "unknown scale prints as a power"); } } namespace { /** * The particle position is unit typed. These pin the two things that are easy * to break without noticing. */ SAW_TEST("Unit / a united wrapper is layout compatible with its base"){ // The whole device path depends on this and nothing else checks it: the // particle group crosses to the GPU through sycl_copy_helper, // which is a bulk h.copy of the element block and never recurses into the // element schema. A unit wrapper that changed the size would corrupt every // particle silently rather than failing to compile. using Raw = kel::lbm::data>; using United = kel::lbm::data>>; static_assert(sizeof(Raw) == sizeof(United), "A unit wrapper must not change the layout"); static_assert(std::is_trivially_copyable_v, "A unit wrapper must stay memcpy-able"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / particle position carries lattice metres"){ kel::lbm::data> rb; auto &pos = rb.template get<"position">(); auto &pos_old = rb.template get<"position_old">(); auto &acc = rb.template get<"acceleration">(); static_assert( std::is_same_v, kel::lbm::data>>>, "position must be a lattice length"); static_assert( std::is_same_v, std::remove_cvref_t>, "position_old must match position"); static_assert( std::is_same_v, kel::lbm::data>>>, "acceleration must be a lattice acceleration"); // Element access still reaches the raw component. pos.at({{0u}}).set(3.0); SAW_EXPECT(pos.at({{0u}}).get() == 3.0, "element access must still work"); // A difference of two lengths is a length; its dot product is an area. auto delta = pos - pos_old; static_assert(std::is_same_v>, "length minus length is a length"); auto area = kel::lbm::math::dot(delta, delta); static_assert( std::is_same_v, sch::UnitElement>>>, "dot of two lengths must be an area"); SAW_EXPECT(area.at({}).get() == 9.0, "and the value must be right"); } } namespace { /** * Commutativity at the type level. * * m * s and s * m are the same physical unit. If the reduction preserves * encounter order rather than imposing a canonical one, they become distinct * C++ types and the same quantity reached two different ways stops matching. */ SAW_TEST("Unit / multiplication is commutative in the type"){ using A = Mul>, U>>; using B = Mul>, U>>; static_assert(std::is_same_v, "m*s and s*m must be the same type"); SAW_EXPECT(true, "static assert carries this one"); } } namespace { /** * An alias written by hand must already be in canonical order, or it will not * match the same unit arrived at by arithmetic. * * Multiplying by a dimensionless unit runs the alias back through the * reduction and the sort, so if its factors were declared out of order the * round trip changes the type and this fails. */ template inline constexpr bool is_canonical = std::is_same_v, Alias>, Alias>; SAW_TEST("Unit / division is commutative in the type too"){ // a/b reached two ways must agree. using A = Div>, U>>, U>>; using B = Div>, U>>, U>>; static_assert(std::is_same_v, "operand order must not change the type"); SAW_EXPECT(true, "static assert carries this one"); } SAW_TEST("Unit / three factors sort canonically whatever the order"){ using Kv = sch::UnitElement; using Sl = sch::UnitElement; using One = Mul>, U>, U>; using Two = Mul, U>>, U>; using Three = Mul, U>, U>>; static_assert(std::is_same_v, "any permutation must agree"); static_assert(std::is_same_v, "any permutation must agree"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / every lbm_unit alias is already canonical"){ static_assert(is_canonical>, "SiMeter"); static_assert(is_canonical>, "LbmMeter"); static_assert(is_canonical>, "SiSecond"); static_assert(is_canonical>, "LbmSecond"); static_assert(is_canonical>, "SiVelocity"); static_assert(is_canonical>, "LbmVelocity"); static_assert(is_canonical>, "SiAcceleration"); static_assert(is_canonical>, "LbmAcceleration"); static_assert(is_canonical>, "SiKinematicViscosity"); static_assert(is_canonical>, "LbmKinematicViscosity"); SAW_EXPECT(true, "static asserts carry this one"); } } namespace { /** * Velocity is typed through the kinematics and unwrapped at the force law, * because force needs a mass base unit that does not exist yet. */ SAW_TEST("Unit / a length over a time is a velocity"){ using Vel = Div, sch::LbmSecond>; static_assert(std::is_same_v>, "m / s must be exactly the LbmVelocity alias"); // And a velocity times a time is back to a length. using Len = Mul, sch::LbmSecond>; static_assert(std::is_same_v>, "velocity * time must be a length again"); SAW_EXPECT(true, "static asserts carry this one"); } SAW_TEST("Unit / a normalised vector is Pure, not merely unannotated"){ kel::lbm::data>> v; v.at({{0u}}).set(3.0); v.at({{1u}}).set(4.0); v.at({{2u}}).set(0.0); auto dir = kel::lbm::math::normalize(v); static_assert( std::is_same_v>>>, "a direction must be Pure"); SAW_EXPECT(std::abs(dir.at({{0u}}).get() - 0.6) < 1e-12, "x component"); SAW_EXPECT(std::abs(dir.at({{1u}}).get() - 0.8) < 1e-12, "y component"); // The length keeps the unit it measured. auto len = kel::lbm::math::norm(v); static_assert(std::is_same_v>>>, "a length must stay a length"); SAW_EXPECT(std::abs(len.at({}).get() - 5.0) < 1e-12, "norm value"); } } namespace { /** * sqrt halves the exponents, so the root of an area is a length. This is what * lets `pos_rel / sqrt(distance_squared)` come out as a dimensionless * direction rather than an unlabelled vector. */ SAW_TEST("Unit / sqrt of an area is a length"){ using Area = Mul>, sch::LbmMeter>>; kel::lbm::data a; a.at({}).set(9.0); auto len = kel::lbm::math::sqrt(a); static_assert( std::is_same_v>>>, "sqrt(m^2) must be m"); SAW_EXPECT(std::abs(len.at({}).get() - 3.0) < 1e-12, "and the value must be right"); // A length divided by a length is a direction scale: dimensionless. using Ratio = Div>, sch::LbmMeter>>; static_assert(std::is_same_v>>, "m / m must be Pure"); SAW_EXPECT(true, "static asserts carry the rest"); } SAW_TEST("Unit / sqrt refuses an odd exponent"){ // m^3 has no expressible square root in this system. The static_assert in // math::sqrt is what stops it, so this test records the intent rather // than invoking it -- instantiating it would fail the build by design. using Cube = Mul>, sch::LbmMeter>>, sch::LbmMeter>>; static_assert(std::is_same_v, sch::UnitElement>>, "m*m*m must be m^3"); SAW_EXPECT(true, "sqrt of this would not compile, by design"); } }