summaryrefslogtreecommitdiff
path: root/modules/core/tests/unit.cpp
blob: 1c187c0d98b0d0c7684ef02e20da34aab32e3845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <forstio/test/suite.hpp>

#include <sstream>
#include <type_traits>

#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 <int64_t E, int64_t S = 0>
using Met = sch::UnitElement<meter, E, S>;

template <int64_t E, int64_t S = 0>
using Sec = sch::UnitElement<second, E, S>;

template <typename... Ele> using U = sch::Unit<sch::T, Ele...>;

template <typename A, typename B>
using Mul = typename kel::lbm::unit_multiplication<A, B>::Schema;

template <typename A, typename B>
using Div = typename kel::lbm::unit_division<A, B>::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<Mul<U<Met<1>>, U<Met<1>>>, U<Met<2>>>,
		"m * m must fold to m^2");
	static_assert(std::is_same_v<Mul<U<Met<1,3>>, U<Met<1,3>>>, U<Met<2,3>>>,
		"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<Met<1,0>>, U<Met<1,3>>>;
	static_assert(!std::is_same_v<Mixed, U<Met<2,0>>>, "must not fold to m^2");
	static_assert(!std::is_same_v<Mixed, U<Met<2,3>>>, "must not fold to km^2");
	static_assert(std::is_same_v<Mixed, U<Met<1,0>, 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<Div<U<Met<2,3>>, U<Met<1,3>>>, U<Met<1,3>>>,
		"km^2 / km must be km");
	static_assert(std::is_same_v<Div<U<>, U<Met<1,3>>>, U<Met<-1,3>>>,
		"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<Div<U<Met<1>>, U<Met<1>>>, sch::Pure<sch::T>>,
		"m / m must reduce to Pure");
	static_assert(std::is_same_v<Div<U<Met<1,3>>, U<Met<1,3>>>, sch::Pure<sch::T>>,
		"km / km must reduce to Pure");
	// But only when the scales agree.
	static_assert(!std::is_same_v<Div<U<Met<1,3>>, U<Met<1,0>>>, sch::Pure<sch::T>>,
		"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<lbm_meter, 1>;
	using Mixed = Mul<U<Met<1>>, U<LbmMet>>;
	static_assert(!std::is_same_v<Mixed, U<Met<2>>>, "must not fold");
	// Canonical order, not encounter order: "meter_lbm" sorts before "meter_si".
	static_assert(std::is_same_v<Mixed, U<LbmMet, Met<1>>>, "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<sch::SiMeter<sch::T>, U<Met<1>>>, "SiMeter");
	static_assert(std::is_same_v<sch::SiVelocity<sch::T>, U<Met<1>, Sec<-1>>>,
		"SiVelocity");
	static_assert(
		std::is_same_v<sch::SiKinematicViscosity<sch::T>, U<Met<2>, Sec<-1>>>,
		"SiKinematicViscosity");
	static_assert(
		std::is_same_v<Div<sch::SiMeter<sch::T>, sch::SiSecond<sch::T>>,
					   sch::SiVelocity<sch::T>>,
		"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<U<Met<1>>> p{2.0};
	kel::lbm::data<U<Met<1,3>>> k{2.0};
	kel::lbm::data<U<Met<1,-3>>> 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<U<Met<1,4>>> 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<FixedArray>,
	// 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<sch::Vector<sch::T,3u>>;
	using United = kel::lbm::data<sch::LbmMeter<sch::Vector<sch::T,3u>>>;

	static_assert(sizeof(Raw) == sizeof(United),
		"A unit wrapper must not change the layout");
	static_assert(std::is_trivially_copyable_v<United>,
		"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<sch::ParticleRigidBody<sch::T,3u>> 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<std::remove_cvref_t<decltype(pos)>,
					   kel::lbm::data<sch::LbmMeter<sch::Vector<sch::T,3u>>>>,
		"position must be a lattice length");
	static_assert(
		std::is_same_v<std::remove_cvref_t<decltype(pos_old)>,
					   std::remove_cvref_t<decltype(pos)>>,
		"position_old must match position");
	static_assert(
		std::is_same_v<std::remove_cvref_t<decltype(acc)>,
					   kel::lbm::data<sch::LbmAcceleration<sch::Vector<sch::T,3u>>>>,
		"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<decltype(delta), std::remove_cvref_t<decltype(pos)>>,
		"length minus length is a length");

	auto area = kel::lbm::math::dot(delta, delta);
	static_assert(
		std::is_same_v<decltype(area),
					   kel::lbm::data<sch::Unit<sch::Scalar<sch::T>,
												sch::UnitElement<kel::lbm::lbm_type::meter,2>>>>,
		"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<Met<1>>, U<Sec<-1>>>;
	using B = Mul<U<Sec<-1>>, U<Met<1>>>;
	static_assert(std::is_same_v<A,B>, "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 <typename Alias>
inline constexpr bool is_canonical =
	std::is_same_v<Mul<U<>, Alias>, Alias>;

SAW_TEST("Unit / division is commutative in the type too"){
	// a/b reached two ways must agree.
	using A = Div<Mul<U<Met<1>>, U<Sec<1>>>, U<Sec<2>>>;
	using B = Div<Mul<U<Sec<1>>, U<Met<1>>>, U<Sec<2>>>;
	static_assert(std::is_same_v<A,B>, "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<kel::lbm::lbm_type::meter, 2>;
	using Sl = sch::UnitElement<kel::lbm::lbm_type::second, -1>;

	using One = Mul<Mul<U<Met<1>>, U<Kv>>, U<Sl>>;
	using Two = Mul<Mul<U<Sl>, U<Met<1>>>, U<Kv>>;
	using Three = Mul<Mul<U<Kv>, U<Sl>>, U<Met<1>>>;

	static_assert(std::is_same_v<One,Two>, "any permutation must agree");
	static_assert(std::is_same_v<Two,Three>, "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<sch::SiMeter<sch::T>>, "SiMeter");
	static_assert(is_canonical<sch::LbmMeter<sch::T>>, "LbmMeter");
	static_assert(is_canonical<sch::SiSecond<sch::T>>, "SiSecond");
	static_assert(is_canonical<sch::LbmSecond<sch::T>>, "LbmSecond");
	static_assert(is_canonical<sch::SiVelocity<sch::T>>, "SiVelocity");
	static_assert(is_canonical<sch::LbmVelocity<sch::T>>, "LbmVelocity");
	static_assert(is_canonical<sch::SiAcceleration<sch::T>>, "SiAcceleration");
	static_assert(is_canonical<sch::LbmAcceleration<sch::T>>, "LbmAcceleration");
	static_assert(is_canonical<sch::SiKinematicViscosity<sch::T>>, "SiKinematicViscosity");
	static_assert(is_canonical<sch::LbmKinematicViscosity<sch::T>>, "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::LbmMeter<sch::T>, sch::LbmSecond<sch::T>>;
	static_assert(std::is_same_v<Vel, sch::LbmVelocity<sch::T>>,
		"m / s must be exactly the LbmVelocity alias");

	// And a velocity times a time is back to a length.
	using Len = Mul<sch::LbmVelocity<sch::T>, sch::LbmSecond<sch::T>>;
	static_assert(std::is_same_v<Len, sch::LbmMeter<sch::T>>,
		"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<sch::LbmMeter<sch::Vector<sch::T,3u>>> 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<decltype(dir),
					   kel::lbm::data<sch::Pure<sch::Vector<sch::T,3u>>>>,
		"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<decltype(len), kel::lbm::data<sch::LbmMeter<sch::Scalar<sch::T>>>>,
		"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<sch::Scalar<sch::T>>, sch::LbmMeter<sch::Scalar<sch::T>>>;

	kel::lbm::data<Area> a;
	a.at({}).set(9.0);

	auto len = kel::lbm::math::sqrt(a);
	static_assert(
		std::is_same_v<decltype(len), kel::lbm::data<sch::LbmMeter<sch::Scalar<sch::T>>>>,
		"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<sch::Scalar<sch::T>>, sch::LbmMeter<sch::Scalar<sch::T>>>;
	static_assert(std::is_same_v<Ratio, sch::Pure<sch::Scalar<sch::T>>>,
		"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<Mul<sch::LbmMeter<sch::Scalar<sch::T>>,
						 sch::LbmMeter<sch::Scalar<sch::T>>>,
					 sch::LbmMeter<sch::Scalar<sch::T>>>;
	static_assert(std::is_same_v<Cube,
		kel::lbm::sch::Unit<sch::Scalar<sch::T>,
							sch::UnitElement<kel::lbm::lbm_type::meter,3>>>,
		"m*m*m must be m^3");
	SAW_EXPECT(true, "sqrt of this would not compile, by design");
}
}