-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_dimensional.hpp
562 lines (500 loc) · 17.8 KB
/
hpc_dimensional.hpp
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#pragma once
#include <hpc_matrix3x3.hpp>
#include <hpc_symmetric3x3.hpp>
#include <hpc_vector3.hpp>
namespace hpc {
template <
int LengthPower,
int MassPower,
int TimePower,
int CurrentPower,
int TemperaturePower,
int AmountPower,
int IntensityPower>
class dimension
{
public:
static constexpr int length_power = LengthPower;
static constexpr int mass_power = MassPower;
static constexpr int time_power = TimePower;
static constexpr int current_power = CurrentPower;
static constexpr int temperature_power = TemperaturePower;
static constexpr int amount_power = AmountPower;
static constexpr int intensity_power = IntensityPower;
};
template <class left, class right>
class multiply_dimensions
{
public:
using type = dimension<
left::length_power + right::length_power,
left::mass_power + right::mass_power,
left::time_power + right::time_power,
left::current_power + right::current_power,
left::temperature_power + right::temperature_power,
left::amount_power + right::amount_power,
left::intensity_power + right::intensity_power>;
};
template <class left, class right>
using multiply_dimensions_t = typename multiply_dimensions<left, right>::type;
template <class left, class right>
class divide_dimensions
{
public:
using type = dimension<
left::length_power - right::length_power,
left::mass_power - right::mass_power,
left::time_power - right::time_power,
left::current_power - right::current_power,
left::temperature_power - right::temperature_power,
left::amount_power - right::amount_power,
left::intensity_power - right::intensity_power>;
};
template <class left, class right>
using divide_dimensions_t = typename divide_dimensions<left, right>::type;
template <class dim, int factor>
class root_dimension
{
static_assert(dim::length_power % factor == 0, "length power not evenly divisible");
static_assert(dim::mass_power % factor == 0, "mass power not evenly divisible");
static_assert(dim::time_power % factor == 0, "time power not evenly divisible");
static_assert(dim::current_power % factor == 0, "current power not evenly divisible");
static_assert(dim::temperature_power % factor == 0, "temperature power not evenly divisible");
static_assert(dim::amount_power % factor == 0, "amount power not evenly divisible");
static_assert(dim::intensity_power % factor == 0, "intensity power not evenly divisible");
public:
using type = dimension<
dim::length_power / factor,
dim::mass_power / factor,
dim::time_power / factor,
dim::current_power / factor,
dim::temperature_power / factor,
dim::amount_power / factor,
dim::intensity_power / factor>;
};
template <class dim, int factor>
using root_dimension_t = typename root_dimension<dim, factor>::type;
template <class dim, int factor>
class raise_dimension
{
public:
using type = dimension<
dim::length_power * factor,
dim::mass_power * factor,
dim::time_power * factor,
dim::current_power * factor,
dim::temperature_power * factor,
dim::amount_power * factor,
dim::intensity_power * factor>;
};
template <class dim, int factor>
using raise_dimension_t = typename raise_dimension<dim, factor>::type;
using no_dimension = dimension<0, 0, 0, 0, 0, 0, 0>;
using length_dimension = dimension<1, 0, 0, 0, 0, 0, 0>;
using mass_dimension = dimension<0, 1, 0, 0, 0, 0, 0>;
using time_dimension = dimension<0, 0, 1, 0, 0, 0, 0>;
using current_dimension = dimension<0, 0, 0, 1, 0, 0, 0>;
using temperature_dimension = dimension<0, 0, 0, 0, 1, 0, 0>;
using amount_dimension = dimension<0, 0, 0, 0, 0, 1, 0>;
using intensity_dimension = dimension<0, 0, 0, 0, 0, 0, 1>;
using speed_dimension = divide_dimensions_t<length_dimension, time_dimension>;
using velocity_dimension = speed_dimension;
using acceleration_dimension = divide_dimensions_t<speed_dimension, time_dimension>;
using momentum_dimension = multiply_dimensions_t<mass_dimension, velocity_dimension>;
using force_dimension = multiply_dimensions_t<mass_dimension, acceleration_dimension>;
using area_dimension = multiply_dimensions_t<length_dimension, length_dimension>;
using stress_dimension = divide_dimensions_t<force_dimension, area_dimension>;
using pressure_dimension = stress_dimension;
using volume_dimension = multiply_dimensions_t<area_dimension, length_dimension>;
using basis_dimension = no_dimension;
using strain_dimension = divide_dimensions_t<length_dimension, length_dimension>;
using strain_rate_dimension = divide_dimensions_t<strain_dimension, time_dimension>;
using inverse_length_dimension = divide_dimensions_t<no_dimension, length_dimension>;
using inverse_area_dimension = divide_dimensions_t<no_dimension, area_dimension>;
using density_dimension = divide_dimensions_t<mass_dimension, volume_dimension>;
using energy_dimension = multiply_dimensions_t<force_dimension, length_dimension>;
using power_dimension = divide_dimensions_t<energy_dimension, time_dimension>;
using specific_energy_dimension = divide_dimensions_t<energy_dimension, mass_dimension>;
using specific_energy_rate_dimension = divide_dimensions_t<specific_energy_dimension, time_dimension>;
using velocity_gradient_dimension = divide_dimensions_t<velocity_dimension, length_dimension>;
using dynamic_viscosity_dimension = divide_dimensions_t<pressure_dimension, velocity_gradient_dimension>;
using kinematic_viscosity_dimension = divide_dimensions_t<area_dimension, time_dimension>;
using power_dimension = divide_dimensions_t<energy_dimension, time_dimension>;
using heat_flux_dimension = divide_dimensions_t<power_dimension, area_dimension>;
using stress_rate_dimension = divide_dimensions_t<stress_dimension, time_dimension>;
using pressure_rate_dimension = stress_rate_dimension;
using energy_density_dimension = divide_dimensions_t<energy_dimension, volume_dimension>;
using energy_density_rate_dimension = divide_dimensions_t<energy_density_dimension, time_dimension>;
using stress_gradient_dimension = divide_dimensions_t<stress_dimension, length_dimension>;
using pressure_gradient_dimension = stress_gradient_dimension;
using strain_rate_rate_dimension = divide_dimensions_t<strain_rate_dimension, time_dimension>;
#ifdef HPC_ENABLE_DIMENSIONAL_ANALYSIS
template <class T, class Dimension>
class quantity
{
T m_impl;
public:
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr quantity(T in) noexcept : m_impl(in)
{
}
HPC_ALWAYS_INLINE constexpr quantity() noexcept = default;
HPC_ALWAYS_INLINE constexpr quantity(quantity const& in) noexcept = default;
HPC_ALWAYS_INLINE quantity&
operator=(quantity const& in) noexcept = default;
template <class Dimension2>
HPC_ALWAYS_INLINE constexpr explicit quantity(quantity<T, Dimension2> const& in) noexcept : quantity(T(in))
{
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr explicit operator T() const noexcept
{
return m_impl;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr T
get() const noexcept
{
return m_impl;
}
};
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator+(quantity<T, D> left, quantity<T, D> right) noexcept
{
return quantity<T, D>(T(left) + T(right));
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, D>&
operator+=(quantity<T, D>& left, quantity<T, D> right) noexcept
{
left = left + right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator+(quantity<T, D> left, T right) noexcept
{
return quantity<T, D>(T(left) + right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, D>&
operator+=(quantity<T, D>& left, T right) noexcept
{
left = left + right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator+(T left, quantity<T, D> right) noexcept
{
return right + left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr quantity<T, D>
operator-(quantity<T, D> x) noexcept
{
return quantity<T, D>(-T(x));
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator-(quantity<T, D> left, quantity<T, D> right) noexcept
{
return quantity<T, D>(T(left) - T(right));
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, D>&
operator-=(quantity<T, D>& left, quantity<T, D> right) noexcept
{
left = left - right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator-(quantity<T, D> left, T right) noexcept
{
return quantity<T, D>(T(left) - right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, D>&
operator-=(quantity<T, D>& left, T right) noexcept
{
left = left - right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator-(T left, quantity<T, D> right) noexcept
{
return quantity<T, D>(left - T(right));
}
template <class T, class LD, class RD>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator*(quantity<T, LD> left, quantity<T, RD> right) noexcept
{
return quantity<T, multiply_dimensions_t<LD, RD>>(T(left) * T(right));
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, D>&
operator*=(quantity<T, D>& left, quantity<T, no_dimension> right) noexcept
{
left = left * right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator*(quantity<T, D> left, T right) noexcept
{
return quantity<T, D>(T(left) * right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, D>&
operator*=(quantity<T, D>& left, T right) noexcept
{
left = left * right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator*(T left, quantity<T, D> right) noexcept
{
return right * left;
}
template <class T, class LD, class RD>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator/(quantity<T, LD> left, quantity<T, RD> right) noexcept
{
return quantity<T, divide_dimensions_t<LD, RD>>(T(left) / T(right));
}
template <class T, class LD, class RD>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, LD>&
operator/=(quantity<T, LD>& left, quantity<T, RD> right) noexcept
{
left = left / right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator/(quantity<T, D> left, T right) noexcept
{
return quantity<T, D>(T(left) / right);
}
template <class T, class LD, class RD>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE quantity<T, LD>&
operator/=(quantity<T, LD>& left, T right) noexcept
{
left = left / right;
return left;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator/(T left, quantity<T, D> right) noexcept
{
return quantity<T, divide_dimensions_t<no_dimension, D>>(left / T(right));
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator==(quantity<T, D> left, quantity<T, D> right) noexcept
{
return T(left) == T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator==(quantity<T, D> left, T right) noexcept
{
return T(left) == right;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator==(T left, quantity<T, D> right) noexcept
{
return left == T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator!=(quantity<T, D> left, quantity<T, D> right) noexcept
{
return T(left) != T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator!=(quantity<T, D> left, T right) noexcept
{
return T(left) != right;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator!=(T left, quantity<T, D> right) noexcept
{
return left != T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>(quantity<T, D> left, quantity<T, D> right) noexcept
{
return T(left) > T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>(quantity<T, D> left, T right) noexcept
{
return T(left) > right;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>(T left, quantity<T, D> right) noexcept
{
return left > T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<(quantity<T, D> left, quantity<T, D> right) noexcept
{
return T(left) < T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<(quantity<T, D> left, T right) noexcept
{
return T(left) < right;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<(T left, quantity<T, D> right) noexcept
{
return left < T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>=(quantity<T, D> left, quantity<T, D> right) noexcept
{
return T(left) >= T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>=(quantity<T, D> left, T right) noexcept
{
return T(left) >= right;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>=(T left, quantity<T, D> right) noexcept
{
return left >= T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<=(quantity<T, D> left, quantity<T, D> right) noexcept
{
return T(left) <= T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<=(quantity<T, D> left, T right) noexcept
{
return T(left) <= right;
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<=(T left, quantity<T, D> right) noexcept
{
return left <= T(right);
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE auto
sqrt(quantity<T, D> x) noexcept
{
using std::sqrt;
return quantity<T, root_dimension_t<D, 2>>(sqrt(T(x)));
}
template <class T, class D>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE auto
cbrt(quantity<T, D> x) noexcept
{
using std::cbrt;
return quantity<T, root_dimension_t<D, 3>>(cbrt(T(x)));
}
#else
template <class T, class D>
using quantity = T;
#endif
template <class T>
using adimensional = quantity<T, no_dimension>;
template <class T>
using length = quantity<T, length_dimension>;
template <class T>
using mass = quantity<T, mass_dimension>;
template <class T>
using time = quantity<T, time_dimension>;
template <class T>
using temperature = quantity<T, temperature_dimension>;
template <class T>
using speed = quantity<T, speed_dimension>;
template <class T>
using speed_rate = quantity<T, acceleration_dimension>;
template <class T>
using area = quantity<T, area_dimension>;
template <class T>
using volume = quantity<T, volume_dimension>;
template <class T>
using position = vector3<length<T>>;
template <class T>
using displacement = vector3<length<T>>;
template <class T>
using velocity = vector3<speed<T>>;
template <class T>
using acceleration = vector3<quantity<T, acceleration_dimension>>;
template <class T>
using basis_value = quantity<T, basis_dimension>;
template <class T>
using basis_gradient = vector3<quantity<T, inverse_length_dimension>>;
template <class T>
using inverse_area = quantity<T, inverse_area_dimension>;
template <class T>
using strain = quantity<T, strain_dimension>;
template <class T>
using strain_rate = quantity<T, strain_rate_dimension>;
template <class T>
using strain_rate_rate = quantity<T, strain_rate_rate_dimension>;
template <class T>
using deformation_gradient = matrix3x3<quantity<T, strain_dimension>>;
template <class T>
using stress = matrix3x3<quantity<T, stress_dimension>>;
template <class T>
using stress_rate = matrix3x3<quantity<T, stress_rate_dimension>>;
template <class T>
using pressure = quantity<T, pressure_dimension>;
template <class T>
using symmetric_stress = symmetric3x3<pressure<T>>;
template <class T>
using symmetric_deformation = symmetric3x3<quantity<T, strain_dimension>>;
template <class T>
using momentum = vector3<quantity<T, momentum_dimension>>;
template <class T>
using force = vector3<quantity<T, force_dimension>>;
template <class T>
using energy = quantity<T, energy_dimension>;
template <class T>
using power = quantity<T, power_dimension>;
template <class T>
using specific_energy = quantity<T, specific_energy_dimension>;
template <class T>
using specific_energy_rate = quantity<T, specific_energy_rate_dimension>;
template <class T>
using density = quantity<T, density_dimension>;
template <class T>
using dynamic_viscosity = quantity<T, dynamic_viscosity_dimension>;
template <class T>
using kinematic_viscosity = quantity<T, kinematic_viscosity_dimension>;
template <class T>
using velocity_gradient = matrix3x3<quantity<T, velocity_gradient_dimension>>;
template <class T>
using symmetric_velocity_gradient = symmetric3x3<quantity<T, velocity_gradient_dimension>>;
template <class T>
using heat_flux = vector3<quantity<T, heat_flux_dimension>>;
template <class T>
using pressure_rate = quantity<T, pressure_rate_dimension>;
template <class T>
using energy_density = quantity<T, energy_density_dimension>;
template <class T>
using energy_density_rate = quantity<T, energy_density_rate_dimension>;
template <class T>
using pressure_gradient = vector3<quantity<T, pressure_gradient_dimension>>;
} // namespace hpc