cphot 0.1
A C++ tool for computing photometry from spectra.
rquantities.hpp
Go to the documentation of this file.
1 /**
2  * @defgroup RQUANTITY unit module
3  * @brief This module defines usual units and convenient ways to convert between them.
4  *
5  * a `Quantity` is defined as a `double` with a `Unit` attached.
6  * A unit combines the following 8 dimensions to some powers:
7  * -# MassDim mass
8  * -# LengthDim length
9  * -# TimeDim time
10  * -# AngleDim angular
11  * -# CurrentDim current (electric)
12  * -# LumDim luninosity
13  * -# SubDim Substance (chemical)
14  * -# TempDim temperature
15  *
16  * for example,
17  * * a frequency is defined as `QUANTITY_TYPE(0, 0, -1, 0, 0, 0, 0, 0, QFrequency)`
18  * * an energy as `QUANTITY_TYPE(1, 2, -2, 0, 0, 0, 0, 0, QEnergy)`
19  *
20  * Below are listed the base definitions:
21  *
22  * * `QUANTITY_TYPE(0, 0, 0, 0, 0, 0, 0, 0, Number)`
23  * * `QUANTITY_TYPE(1, 0, 0, 0, 0, 0, 0, 0, QMass)`
24  * * `QUANTITY_TYPE(0, 1, 0, 0, 0, 0, 0, 0, QLength)`
25  * * `QUANTITY_TYPE(0, 2, 0, 0, 0, 0, 0, 0, QArea)`
26  * * `QUANTITY_TYPE(0, 3, 0, 0, 0, 0, 0, 0, QVolume)`
27  * * `QUANTITY_TYPE(0, 0, 1, 0, 0, 0, 0, 0, QTime)`
28  * * `QUANTITY_TYPE(0, 1, -1, 0, 0, 0, 0, 0, QSpeed)`
29  * * `QUANTITY_TYPE(0, 1, -2, 0, 0, 0, 0, 0, QAcceleration)`
30  * * `QUANTITY_TYPE(0, 1, -3, 0, 0, 0, 0, 0, QJerk)`
31  * * `QUANTITY_TYPE(0, 0, -1, 0, 0, 0, 0, 0, QFrequency)`
32  * * `QUANTITY_TYPE(1, 1, -2, 0, 0, 0, 0, 0, QForce)`
33  * * `QUANTITY_TYPE(1, -1, -2, 0, 0, 0, 0, 0, QPressure)`
34  * * `QUANTITY_TYPE(0, 0, 0, 0, 1, 0, 0, 0, QCurrent)`
35  * * `QUANTITY_TYPE(0, 0, 0, 0, 0, 1, 0, 0, QLuminosity)`
36  * * `QUANTITY_TYPE(0, 0, 0, 0, 0, 0, 1, 0, QSubstance)`
37  * * `QUANTITY_TYPE(0, 0, 0, 0, 0, 0, 0, 1, QTemperature)`
38  * * `QUANTITY_TYPE(1, 2, -2, 0, 0, 0, 0, 0, QEnergy)`
39  * * `QUANTITY_TYPE(1, 2, -3, 0, 0, 0, 0, 0, QFlux)`
40  * * `QUANTITY_TYPE(1, -1, -3, 0, 0, 0, 0, 0, QSpectralFluxDensity)`
41  * * `QUANTITY_TYPE(0, 0, 0, 1, 0, 0, 0, 0, Angle)`
42  * * `QUANTITY_TYPE(0, 0, 0, 2, 0, 0, 0, 0, AngleArea)`
43  *
44  * We predefined units as `constexpr` which are optimized at compilation time.
45  *
46  * \note All units only apply to double values. (eventually will extend to vectors etc)
47  */
48 #pragma once
49 #include <ratio>
50 #include <ostream>
51 #include <string.h>
52 #include <string_view>
53 #include <map>
54 
55 // The class SHOULD NOT BE INSTANTIATED directly by itself, rather use the quantity types defined below.
56 /**
57  * @ingroup RQUANTITY
58  * @brief prototype template container class, that just holds a double value.
59  *
60  * \warning This class is not intended to be used directly, but rather as a base class for the quantity types.
61  */
62 template<typename MassDim, typename LengthDim, typename TimeDim, typename AngleDim, typename CurrentDim, typename LumDim, typename SubDim, typename TempDim>
63 class RQuantity
64 {
65 public:
66  double value;
67 
68 public:
69  constexpr RQuantity() : value(0.0) {}
70  constexpr RQuantity(double val) : value(val) {}
71  constexpr RQuantity(long double val) : value(static_cast<double>(val)) {}
72 
73  // The intrinsic operations for a quantity with a unit is addition and subtraction
74  constexpr RQuantity const& operator+=(const RQuantity& rhs)
75  {
76  value += rhs.value;
77  return *this;
78  }
79  constexpr RQuantity const& operator-=(const RQuantity& rhs)
80  {
81  value -= rhs.value;
82  return *this;
83  }
84 
85  // Returns the value of the quantity in multiples of the specified unit
86  constexpr double Convert(const RQuantity& rhs) const
87  {
88  return value / rhs.value;
89  }
90 
91  constexpr double to(const RQuantity& rhs) const {
92  return value / rhs.value;
93  }
94 
95  // returns the raw value of the quantity (should not be used)
96  constexpr double getValue() const
97  {
98  return value;
99  }
100 };
101 
102 
103 
104 // Predefined (physical unit) quantity types:
105 // ------------------------------------------
106 #define QUANTITY_TYPE(_Mdim, _Ldim, _Tdim, _Adim, _CDim, _LumDim, _SubDim, TempDim, name) \
107  typedef RQuantity<std::ratio<_Mdim>, std::ratio<_Ldim>, std::ratio<_Tdim>, std::ratio<_Adim>, std::ratio<_CDim>, std::ratio<_LumDim>, std::ratio<_SubDim>, std::ratio<TempDim>> name;
108 
109 //MassDim, LengthDim, TimeDim, AngleDim, CurrentDim, LumDim, SubDim, TempDim
110 // Replacement of "double" type
111 
112 QUANTITY_TYPE(0, 0, 0, 0, 0, 0, 0, 0, Number);
113 
114 // Physical quantity types
115 QUANTITY_TYPE(1, 0, 0, 0, 0, 0, 0, 0, QMass);
116 QUANTITY_TYPE(0, 1, 0, 0, 0, 0, 0, 0, QLength);
117 QUANTITY_TYPE(0, 2, 0, 0, 0, 0, 0, 0, QArea);
118 QUANTITY_TYPE(0, 3, 0, 0, 0, 0, 0, 0, QVolume);
119 QUANTITY_TYPE(0, 0, 1, 0, 0, 0, 0, 0, QTime);
120 QUANTITY_TYPE(0, 1, -1, 0, 0, 0, 0, 0, QSpeed);
121 QUANTITY_TYPE(0, 1, -2, 0, 0, 0, 0, 0, QAcceleration);
122 QUANTITY_TYPE(0, 1, -3, 0, 0, 0, 0, 0, QJerk);
123 QUANTITY_TYPE(0, 0, -1, 0, 0, 0, 0, 0, QFrequency);
124 QUANTITY_TYPE(1, 1, -2, 0, 0, 0, 0, 0, QForce);
125 QUANTITY_TYPE(1, -1, -2, 0, 0, 0, 0, 0, QPressure);
126 QUANTITY_TYPE(0, 0, 0, 0, 1, 0, 0, 0, QCurrent);
127 QUANTITY_TYPE(0, 0, 0, 0, 0, 1, 0, 0, QLuminosity);
128 QUANTITY_TYPE(0, 0, 0, 0, 0, 0, 1, 0, QSubstance);
129 QUANTITY_TYPE(0, 0, 0, 0, 0, 0, 0, 1, QTemperature);
130 QUANTITY_TYPE(1, 2, -2, 0, 0, 0, 0, 0, QEnergy);
131 QUANTITY_TYPE(1, 2, -3, 0, 0, 0, 0, 0, QFlux);
132 QUANTITY_TYPE(1, -1, -3, 0, 0, 0, 0, 0, QSpectralFluxDensity);
133 
134 // Angle type:
135 QUANTITY_TYPE(0, 0, 0, 1, 0, 0, 0, 0, Angle);
136 QUANTITY_TYPE(0, 0, 0, 2, 0, 0, 0, 0, AngleArea);
137 
138 
139 // Standard arithmetic operators:
140 // ------------------------------
141 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
144 {
146 }
147 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
150 {
152 }
153 template <typename M1, typename l1, typename T1, typename A1, typename C1, typename L1, typename S1, typename D1,
154  typename M2, typename l2, typename T2, typename A2, typename C2, typename L2, typename S2, typename D2>
155 constexpr RQuantity<std::ratio_add<M1, M2>, std::ratio_add<l1, l2>,
156  std::ratio_add<T1, T2>, std::ratio_add<A1, A2>,
157  std::ratio_add<C1, C2>, std::ratio_add<L1, L2>,
158  std::ratio_add<S1, S2>, std::ratio_add<D1, D2>
159  >
162 {
163  return RQuantity<std::ratio_add<M1, M2>, std::ratio_add<l1, l2>,
164  std::ratio_add<T1, T2>, std::ratio_add<A1, A2>,
165  std::ratio_add<C1, C2>, std::ratio_add<L1, L2>,
166  std::ratio_add<S1, S2>, std::ratio_add<D1, D2>>
167  (lhs.getValue()*rhs.getValue());
168 }
169 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
171  operator*(const double& lhs, const RQuantity<M, l, T, A, C, L, S, D>& rhs)
172 {
174 }
175 template <typename M1, typename l1, typename T1, typename A1, typename C1, typename L1, typename S1, typename D1,
176  typename M2, typename l2, typename T2, typename A2, typename C2, typename L2, typename S2, typename D2>
177 constexpr RQuantity<std::ratio_subtract<M1, M2>, std::ratio_subtract<l1, l2>,
178  std::ratio_subtract<T1, T2>, std::ratio_subtract<A1, A2>,
179  std::ratio_subtract<C1, C2>, std::ratio_subtract<L1, L2>,
180  std::ratio_subtract<S1, S2>, std::ratio_subtract<D1, D2>>
182 {
183  return RQuantity<std::ratio_subtract<M1, M2>, std::ratio_subtract<l1, l2>,
184  std::ratio_subtract<T1, T2>, std::ratio_subtract<A1, A2>,
185  std::ratio_subtract<C1, C2>, std::ratio_subtract<L1, L2>,
186  std::ratio_subtract<S1, S2>, std::ratio_subtract<D1, D2>>
187  (lhs.getValue() / rhs.getValue());
188 }
189 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
191  std::ratio_subtract<std::ratio<0>, l>,
192  std::ratio_subtract<std::ratio<0>, T>,
193  std::ratio_subtract<std::ratio<0>, A>,
194  std::ratio_subtract<std::ratio<0>, C>,
195  std::ratio_subtract<std::ratio<0>, L>,
196  std::ratio_subtract<std::ratio<0>, S>,
197  std::ratio_subtract<std::ratio<0>, D>>
199 {
201  std::ratio_subtract<std::ratio<0>, l>,
202  std::ratio_subtract<std::ratio<0>, T>,
203  std::ratio_subtract<std::ratio<0>, A>,
204  std::ratio_subtract<std::ratio<0>, C>,
205  std::ratio_subtract<std::ratio<0>, L>,
206  std::ratio_subtract<std::ratio<0>, S>,
207  std::ratio_subtract<std::ratio<0>, D>>
208  (x / rhs.getValue());
209 }
210 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
213 {
215 }
216 
217 
218 // Comparison operators for quantities:
219 // ------------------------------------
220 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
222 {
223  return (lhs.getValue() == rhs.getValue());
224 }
225 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
227 {
228  return (lhs.getValue() != rhs.getValue());
229 }
230 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
232 {
233  return (lhs.getValue() <= rhs.getValue());
234 }
235 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
237 {
238  return (lhs.getValue() >= rhs.getValue());
239 }
240 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
242 {
243  return (lhs.getValue()<rhs.getValue());
244 }
245 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
247 {
248  return (lhs.getValue()>rhs.getValue());
249 }
250 
251 
252 // Predefined units:
253 // -----------------
254 
255 // Predefined mass units:
256 constexpr QMass kg(1.0); // SI base unit
257 constexpr QMass gram = 0.001 * kg;
258 constexpr QMass tonne = 1000 * kg;
259 constexpr QMass ounce = 0.028349523125 * kg;
260 constexpr QMass pound = 16 * ounce;
261 constexpr QMass stone = 14 * pound;
262 constexpr QMass carat = 200 * 1e-3 * gram;
263 
264 // Predefined length-derived units
265 constexpr QLength metre(1.0); // SI base unit
266 constexpr QLength meter = metre;
267 constexpr QLength decimetre = metre / 10;
268 constexpr QLength centimetre = metre / 100;
269 constexpr QLength millimetre = metre / 1000;
270 constexpr QLength micrometre = millimetre / 1000;
271 constexpr QLength nanometre = micrometre / 1000;
272 constexpr QLength nanometer = nanometre;
273 constexpr QLength nm = nanometer;
274 constexpr QLength angstrom = micrometre / 10000;
275 constexpr QLength kilometre = 1000 * metre;
276 constexpr QLength inch = 2.54 * centimetre;
277 constexpr QLength foot = 12 * inch;
278 constexpr QLength yard = 3 * foot;
279 constexpr QLength mile = 5280 * foot;
280 
281 constexpr QArea kilometre2 = kilometre*kilometre;
282 constexpr QArea metre2 = metre*metre;
283 constexpr QArea decimetre2 = decimetre*decimetre;
285 constexpr QArea millimetre2 = millimetre * millimetre;
286 constexpr QArea inch2 = inch*inch;
287 constexpr QArea foot2 = foot*foot;
288 constexpr QArea mile2 = mile*mile;
289 
290 constexpr QVolume kilometre3 = kilometre2*kilometre;
291 constexpr QVolume metre3 = metre2*metre;
292 constexpr QVolume decimetre3 = decimetre2*decimetre;
293 constexpr QVolume litre = decimetre3;
294 constexpr QVolume centimetre3 = centimetre2*centimetre;
295 constexpr QVolume millimetre3 = millimetre2 * millimetre;
296 constexpr QVolume inch3 = inch2*inch;
297 constexpr QVolume foot3 = foot2*foot;
298 constexpr QVolume mile3 = mile2*mile;
299 
300 // Predefined time-derived units:
301 constexpr QTime second(1.0); // SI base unit
302 constexpr QTime minute = 60 * second;
303 constexpr QTime hour = 60 * minute;
304 constexpr QTime day = 24 * hour;
305 
306 constexpr QFrequency Hz(1.0);
307 
308 // Predefined mixed units:
309 constexpr QAcceleration G = 9.80665 * metre / (second*second);
310 
311 constexpr QForce newton(1.0);
312 constexpr QForce poundforce = pound*G;
313 constexpr QForce kilopond = kg*G;
314 
315 constexpr QPressure Pascal(1.0);
316 constexpr QPressure bar = 100000 * Pascal;
317 constexpr QPressure psi = pound*G / inch2;
318 
319 
320 // current units
321 constexpr QCurrent ampere(1.0); // SI Base
322 
323 // constexpr QEnergy coulomb = ampere * second;
324 
325 // energy
326 constexpr QEnergy joule = newton * metre;
327 constexpr QEnergy electron_volt = 1.60217653e-19 * joule;
328 constexpr QEnergy eV = electron_volt;
329 constexpr QEnergy thermochemical_calorie = 4.184 * joule;
330 constexpr QEnergy cal = thermochemical_calorie;
331 
332 constexpr QFlux watt = joule / second;
333 
334 // luminosity units
335 constexpr QLuminosity candela(1.0); // SI Base
336 
337 //substance units
338 constexpr QSubstance mole(1.0); // SI Base
339 
340 // temperature units
341 constexpr QTemperature degK(1.0); // SI Base
342 constexpr QTemperature kelvin = 1. * degK;
343 
344 
345 // Astronomy units
346 constexpr QTime julian_year = 365.25 * day;
347 constexpr QLength parsec = 30856775814914 * kilometre;
348 constexpr QLength astronomical_unit = 149597870691 * meter;
349 constexpr QLength au = astronomical_unit;
350 constexpr QSpeed speed_of_light = 299792458 * meter / second;
351 constexpr QSpeed c = speed_of_light;
352 constexpr QLength light_year = speed_of_light * julian_year;
353 constexpr QLength ly = light_year;
354 constexpr QMass msun = 1.98892e30 * kg;
355 constexpr QMass Msun = msun;
356 constexpr QLength rsun = 6.955e8 * meter;
357 constexpr QLength Rsun = rsun;
358 constexpr QEnergy erg = 1e-7 * joule;
359 constexpr QFlux lsun = 3.839e26 * watt;
360 
361 constexpr QSpectralFluxDensity flam = erg / second / centimetre2 / angstrom;
362 // constexpr QSpectralFluxDensity jansky = 1e-26 * watt / (meter * meter) / Hz;
363 constexpr QSpectralFluxDensity jansky = 1e-23 * erg / second/ centimetre2 / angstrom;
364 constexpr QSpectralFluxDensity Jy = jansky;
365 
366 
367 
368 constexpr QLength operator"" _pc(long double x) { return static_cast<double>(x)*parsec; }
369 constexpr QLength operator"" _pc(unsigned long long int x) { return static_cast<double>(x)*parsec; }
370 
371 // Physical unit literals:
372 // -----------------------
373 
374 // literals for length units
375 constexpr QLength operator"" _AA(long double x) { return static_cast<double>(x)*angstrom; }
376 constexpr QLength operator"" _nm(long double x) { return static_cast<double>(x)*nanometre; }
377 constexpr QLength operator"" _mm(long double x) { return static_cast<double>(x)*millimetre; }
378 constexpr QLength operator"" _cm(long double x) { return static_cast<double>(x)*centimetre; }
379 constexpr QLength operator"" _m(long double x) { return static_cast<double>(x)*metre; }
380 constexpr QLength operator"" _km(long double x) { return static_cast<double>(x)*kilometre; }
381 constexpr QLength operator"" _mi(long double x) { return static_cast<double>(x)*mile; }
382 constexpr QLength operator"" _yd(long double x) { return static_cast<double>(x)*yard; }
383 constexpr QLength operator"" _ft(long double x) { return static_cast<double>(x)*foot; }
384 constexpr QLength operator"" _in(long double x) { return static_cast<double>(x)*inch; }
385 constexpr QLength operator"" _mm(unsigned long long int x) { return static_cast<double>(x)*millimetre; }
386 constexpr QLength operator"" _cm(unsigned long long int x) { return static_cast<double>(x)*centimetre; }
387 constexpr QLength operator"" _m(unsigned long long int x) { return static_cast<double>(x)*metre; }
388 constexpr QLength operator"" _km(unsigned long long int x) { return static_cast<double>(x)*kilometre; }
389 constexpr QLength operator"" _mi(unsigned long long int x) { return static_cast<double>(x)*mile; }
390 constexpr QLength operator"" _yd(unsigned long long int x) { return static_cast<double>(x)*yard; }
391 constexpr QLength operator"" _ft(unsigned long long int x) { return static_cast<double>(x)*foot; }
392 constexpr QLength operator"" _in(unsigned long long int x) { return static_cast<double>(x)*inch; }
393 
394 // literals for speed units
395 constexpr QSpeed operator"" _mps(long double x) { return QSpeed(x); };
396 constexpr QSpeed operator"" _miph(long double x) { return static_cast<double>(x)*mile / hour; };
397 constexpr QSpeed operator"" _kmph(long double x) { return static_cast<double>(x)*kilometre / hour; };
398 constexpr QSpeed operator"" _mps(unsigned long long int x)
399  { return QSpeed(static_cast<long double>(x)); };
400 constexpr QSpeed operator"" _miph(unsigned long long int x)
401  { return static_cast<double>(x)*mile / hour; };
402 constexpr QSpeed operator"" _kmph(unsigned long long int x)
403  { return static_cast<double>(x)*kilometre / hour; };
404 
405 // literal for frequency unit
406 constexpr QFrequency operator"" _Hz(long double x) { return QFrequency(x); };
407 constexpr QFrequency operator"" _Hz(unsigned long long int x)
408  { return QFrequency(static_cast<long double>(x)); };
409 
410 // literals for time units
411 constexpr QTime operator"" _s(long double x) { return QTime(x); };
412 constexpr QTime operator"" _min(long double x) { return static_cast<double>(x)*minute; };
413 constexpr QTime operator"" _h(long double x) { return static_cast<double>(x)*hour; };
414 constexpr QTime operator"" _day(long double x) { return static_cast<double>(x)*day; };
415 constexpr QTime operator"" _s(unsigned long long int x) { return QTime(static_cast<double>(x)); };
416 constexpr QTime operator"" _min(unsigned long long int x) { return static_cast<double>(x)*minute; };
417 constexpr QTime operator"" _h(unsigned long long int x) { return static_cast<double>(x)*hour; };
418 constexpr QTime operator"" _day(unsigned long long int x) { return static_cast<double>(x)*day; };
419 
420 // literals for mass units
421 constexpr QMass operator"" _kg(long double x) { return QMass(x); };
422 constexpr QMass operator"" _g(long double x) { return static_cast<double>(x)*gram; };
423 constexpr QMass operator"" _t(long double x) { return static_cast<double>(x)*tonne; };
424 constexpr QMass operator"" _oz(long double x) { return static_cast<double>(x)*ounce; };
425 constexpr QMass operator"" _lb(long double x) { return static_cast<double>(x)*pound; };
426 constexpr QMass operator"" _st(long double x) { return static_cast<double>(x)*stone; };
427 constexpr QMass operator"" _kg(unsigned long long int x) { return QMass(static_cast<double>(x)); };
428 constexpr QMass operator"" _g(unsigned long long int x) { return static_cast<double>(x)*gram; };
429 constexpr QMass operator"" _t(unsigned long long int x) { return static_cast<double>(x)*tonne; };
430 constexpr QMass operator"" _oz(unsigned long long int x) { return static_cast<double>(x)*ounce; };
431 constexpr QMass operator"" _lb(unsigned long long int x) { return static_cast<double>(x)*pound; };
432 constexpr QMass operator"" _st(unsigned long long int x) { return static_cast<double>(x)*stone; };
433 
434 // literals for acceleration units
435 constexpr QAcceleration operator"" _mps2(long double x) { return QAcceleration(x); };
436 constexpr QAcceleration operator"" _mps2(unsigned long long int x)
437  { return QAcceleration(static_cast<double>(x)); };
438 constexpr QAcceleration operator"" _G(long double x) { return static_cast<double>(x)*G; };
439 constexpr QAcceleration operator"" _G(unsigned long long int x) { return static_cast<double>(x)*G; }
440 
441 // literals for force units
442 constexpr QForce operator"" _N(long double x) { return QForce(x); };
443 constexpr QForce operator"" _N(unsigned long long int x) { return QForce(static_cast<double>(x)); };
444 constexpr QForce operator"" _lbf(long double x) { return static_cast<double>(x)*poundforce; };
445 constexpr QForce operator"" _lbf(unsigned long long int x) { return static_cast<double>(x)*poundforce; };
446 constexpr QForce operator"" _kp(long double x) { return static_cast<double>(x)*kilopond; };
447 constexpr QForce operator"" _kp(unsigned long long int x) { return static_cast<double>(x)*kilopond; };
448 
449 // literals for pressure units
450 constexpr QPressure operator"" _Pa(long double x) { return QPressure(x); };
451 constexpr QPressure operator"" _Pa(unsigned long long int x)
452  { return QPressure(static_cast<double>(x)); };
453 constexpr QPressure operator"" _bar(long double x) { return static_cast<double>(x)*bar; };
454 constexpr QPressure operator"" _bar(unsigned long long int x) { return static_cast<double>(x)*bar; };
455 constexpr QPressure operator"" _psi(long double x) { return static_cast<double>(x)*psi; };
456 constexpr QPressure operator"" _psi(unsigned long long int x) { return static_cast<double>(x)*psi; };
457 
458 
459 // Angular unit literals:
460 // ----------------------
461 constexpr long double operator"" _pi(long double x)
462  { return static_cast<double>(x) * 3.1415926535897932384626433832795; }
463 constexpr long double operator"" _pi(unsigned long long int x)
464  { return static_cast<double>(x) * 3.1415926535897932384626433832795; }
465 
466 // Predefined angle units:
467 constexpr Angle radian(1.0);
468 constexpr Angle degree = static_cast<double>(2_pi / 360.0) * radian;
469 constexpr Angle arcminute = static_cast<double>(1./60.) * degree;
470 constexpr Angle arcsecond = static_cast<double>(1./60.) * arcminute;
471 constexpr AngleArea steradian = radian * radian;
472 
473 // literals for angle units
474 constexpr Angle operator"" _rad(long double x) { return Angle(x); };
475 constexpr Angle operator"" _rad(unsigned long long int x) { return Angle(static_cast<double>(x)); };
476 constexpr Angle operator"" _deg(long double x) { return static_cast<double>(x)*degree; };
477 constexpr Angle operator"" _deg(unsigned long long int x) { return static_cast<double>(x)*degree; };
478 constexpr Angle operator"" _arcmin(long double x) { return Angle(x); };
479 constexpr Angle operator"" _arcmin(unsigned long long int x) { return Angle(static_cast<double>(x)); };
480 constexpr Angle operator"" _arcsecond(long double x) { return Angle(x); };
481 constexpr Angle operator"" _arcsecond(unsigned long long int x) { return Angle(static_cast<double>(x)); };
482 constexpr AngleArea operator"" _steradian(long double x) { return AngleArea(x); };
483 constexpr AngleArea operator"" _steradian(unsigned long long int x) { return AngleArea(static_cast<double>(x)); };
484 
485 
486 // Conversion macro, which utilizes the string literals
487 #define ConvertTo(_x, _y) (_x).Convert(1.0_##_y)
488 
489 
490 
491 // Typesafe mathematical operations:
492 // ---------------------------------
493 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
495  std::ratio_divide<l, std::ratio<2>>,
496  std::ratio_divide<T, std::ratio<2>>,
497  std::ratio_divide<A, std::ratio<2>>,
498  std::ratio_divide<C, std::ratio<2>>,
499  std::ratio_divide<L, std::ratio<2>>,
500  std::ratio_divide<S, std::ratio<2>>,
501  std::ratio_divide<D, std::ratio<2>>>
503 {
505  std::ratio_divide<l, std::ratio<2>>,
506  std::ratio_divide<T, std::ratio<2>>,
507  std::ratio_divide<A, std::ratio<2>>,
508  std::ratio_divide<C, std::ratio<2>>,
509  std::ratio_divide<L, std::ratio<2>>,
510  std::ratio_divide<S, std::ratio<2>>,
511  std::ratio_divide<D, std::ratio<2>>>
512  (sqrt(num.getValue()));
513 }
514 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
516  std::ratio_multiply<l, std::ratio<2>>,
517  std::ratio_multiply<T, std::ratio<2>>,
518  std::ratio_multiply<A, std::ratio<2>>,
519  std::ratio_multiply<C, std::ratio<2>>,
520  std::ratio_multiply<L, std::ratio<2>>,
521  std::ratio_multiply<S, std::ratio<2>>,
522  std::ratio_multiply<D, std::ratio<2>>>
524 {
526  std::ratio_multiply<l, std::ratio<2>>,
527  std::ratio_multiply<T, std::ratio<2>>,
528  std::ratio_multiply<A, std::ratio<2>>,
529  std::ratio_multiply<C, std::ratio<2>>,
530  std::ratio_multiply<L, std::ratio<2>>,
531  std::ratio_multiply<S, std::ratio<2>>,
532  std::ratio_multiply<D, std::ratio<2>>>
533  (pow(num.getValue(), 2));
534 }
535 
536 // Typesafe trigonometric operations
537 inline double sin(const Angle &num)
538 {
539  return sin(num.getValue());
540 }
541 inline double cos(const Angle &num)
542 {
543  return cos(num.getValue());
544 }
545 inline double tan(const Angle &num)
546 {
547  return tan(num.getValue());
548 }
549 
550 /**
551  * Nice representation
552  */
553 template <typename M, typename l, typename T, typename A, typename C, typename L, typename S, typename D>
554 std::ostream &operator<<(std::ostream &os, RQuantity<M, l, T, A, C, L, S, D>& rhs) {
555  os << rhs.getValue();
556  return os;
557 }
558 
559 std::ostream &operator<<(std::ostream &os, QLength rhs){
560  os << rhs.getValue();
561  return os;
562 }
563 
564 std::ostream &operator<<(std::ostream &os, QMass rhs){
565  os << rhs.getValue();
566  return os;
567 }
568 std::ostream &operator<<(std::ostream &os, QLuminosity rhs){
569  os << rhs.getValue();
570  return os;
571 }
572 std::ostream &operator<<(std::ostream &os, QFlux rhs){
573  os << rhs.getValue();
574  return os;
575 }
576 
577 
578 /**
579  * @ingroup RQuantity
580  * @brief Convenient shortcuts
581  *
582  */
583 namespace units {
584  /**
585  * @ingroup RQuantity
586  * @brief str aliases to length units
587  */
588  const std::map<std::string, QLength> length_mapper {
589  {"Angstrom", angstrom},
590  {"AA", angstrom},
591  {"angstrom", angstrom},
592  {"Nanometer", nm},
593  {"nanometer", nm},
594  {"nm", nm},
595  {"meter", meter},
596  {"metre", meter},
597  {"m", meter},
598  {"cm", centimetre},
599  {"centimeter", centimetre},
600  {"mm", millimetre},
601  {"millimeter", millimetre},
602  {"km", kilometre},
603  {"kilometer", kilometre},
604  {"micrometer", micrometre},
605  {"micron", micrometre},
606  {"um", micrometre},
607  {"pc", parsec},
608  {"parsec", parsec}
609  };
610 
611  /**
612  * @ingroup RQuantity
613  * @brief str aliases to spectral flux density units
614  */
615  const std::map<std::string, QSpectralFluxDensity> spectralflux_mapper {
616  {"flam", flam},
617  {"erg/second/centimetre2/angstrom", flam},
618  {"erg/second/centimetre**2/angstrom", flam},
619  {"erg/second/centimetre^2/angstrom", flam},
620  {"Jy", Jy},
621  {"jansky", Jy},
622  };
623 
624  /**
625  * @ingroup RQuantity
626  * @brief find length units from a string
627  * @return QLength unit
628  */
629  QLength parse_length(const std::string& str) {
630  return length_mapper.at(str);
631  }
632 
633  /**
634  * @ingroup RQuantity
635  * @brief find length units from a string
636  * @return QLength unit
637  */
638  QSpectralFluxDensity parse_spectralflux(const std::string& str) {
639  return spectralflux_mapper.at(str);
640  }
641 } // namespace units
decimetre2
constexpr QArea decimetre2
Definition: rquantities.hpp:283
millimetre3
constexpr QVolume millimetre3
Definition: rquantities.hpp:295
julian_year
constexpr QTime julian_year
Definition: rquantities.hpp:346
Qsqrt
constexpr RQuantity< std::ratio_divide< M, std::ratio< 2 > >, std::ratio_divide< l, std::ratio< 2 > >, std::ratio_divide< T, std::ratio< 2 > >, std::ratio_divide< A, std::ratio< 2 > >, std::ratio_divide< C, std::ratio< 2 > >, std::ratio_divide< L, std::ratio< 2 > >, std::ratio_divide< S, std::ratio< 2 > >, std::ratio_divide< D, std::ratio< 2 > > > Qsqrt(const RQuantity< M, l, T, A, C, L, S, D > &num)
Definition: rquantities.hpp:502
candela
constexpr QLuminosity candela(1.0)
hour
constexpr QTime hour
Definition: rquantities.hpp:303
Hz
constexpr QFrequency Hz(1.0)
pound
constexpr QMass pound
Definition: rquantities.hpp:260
ampere
constexpr QCurrent ampere(1.0)
G
constexpr QAcceleration G
Definition: rquantities.hpp:309
RQuantity
prototype template container class, that just holds a double value.
Definition: rquantities.hpp:63
operator<=
constexpr bool operator<=(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:231
tan
double tan(const Angle &num)
Definition: rquantities.hpp:545
operator+
constexpr RQuantity< M, l, T, A, C, L, S, D > operator+(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:143
mile2
constexpr QArea mile2
Definition: rquantities.hpp:288
eV
constexpr QEnergy eV
Definition: rquantities.hpp:328
Pascal
constexpr QPressure Pascal(1.0)
units::length_mapper
const std::map< std::string, QLength > length_mapper
str aliases to length units
Definition: rquantities.hpp:588
minute
constexpr QTime minute
Definition: rquantities.hpp:302
decimetre
constexpr QLength decimetre
Definition: rquantities.hpp:267
inch
constexpr QLength inch
Definition: rquantities.hpp:276
decimetre3
constexpr QVolume decimetre3
Definition: rquantities.hpp:292
RQuantity::RQuantity
constexpr RQuantity()
Definition: rquantities.hpp:69
QUANTITY_TYPE
#define QUANTITY_TYPE(_Mdim, _Ldim, _Tdim, _Adim, _CDim, _LumDim, _SubDim, TempDim, name)
Definition: rquantities.hpp:106
mile3
constexpr QVolume mile3
Definition: rquantities.hpp:298
arcminute
constexpr Angle arcminute
Definition: rquantities.hpp:469
operator*
constexpr RQuantity< std::ratio_add< M1, M2 >, std::ratio_add< l1, l2 >, std::ratio_add< T1, T2 >, std::ratio_add< A1, A2 >, std::ratio_add< C1, C2 >, std::ratio_add< L1, L2 >, std::ratio_add< S1, S2 >, std::ratio_add< D1, D2 > > operator*(const RQuantity< M1, l1, T1, A1, C1, L1, S1, D1 > &lhs, const RQuantity< M2, l2, T2, A2, C2, L2, S2, D2 > &rhs)
Definition: rquantities.hpp:160
operator==
constexpr bool operator==(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:221
operator>
constexpr bool operator>(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:246
degree
constexpr Angle degree
Definition: rquantities.hpp:468
metre3
constexpr QVolume metre3
Definition: rquantities.hpp:291
poundforce
constexpr QForce poundforce
Definition: rquantities.hpp:312
nanometre
constexpr QLength nanometre
Definition: rquantities.hpp:271
sin
double sin(const Angle &num)
Definition: rquantities.hpp:537
RQuantity::value
double value
Definition: rquantities.hpp:66
operator/
constexpr RQuantity< std::ratio_subtract< M1, M2 >, std::ratio_subtract< l1, l2 >, std::ratio_subtract< T1, T2 >, std::ratio_subtract< A1, A2 >, std::ratio_subtract< C1, C2 >, std::ratio_subtract< L1, L2 >, std::ratio_subtract< S1, S2 >, std::ratio_subtract< D1, D2 > > operator/(const RQuantity< M1, l1, T1, A1, C1, L1, S1, D1 > &lhs, const RQuantity< M2, l2, T2, A2, C2, L2, S2, D2 > &rhs)
Definition: rquantities.hpp:181
units::parse_spectralflux
QSpectralFluxDensity parse_spectralflux(const std::string &str)
find length units from a string
Definition: rquantities.hpp:638
RQuantity::RQuantity
constexpr RQuantity(long double val)
Definition: rquantities.hpp:71
litre
constexpr QVolume litre
Definition: rquantities.hpp:293
centimetre3
constexpr QVolume centimetre3
Definition: rquantities.hpp:294
metre
constexpr QLength metre(1.0)
lsun
constexpr QFlux lsun
Definition: rquantities.hpp:359
RQuantity::getValue
constexpr double getValue() const
Definition: rquantities.hpp:96
erg
constexpr QEnergy erg
Definition: rquantities.hpp:358
parsec
constexpr QLength parsec
Definition: rquantities.hpp:347
bar
constexpr QPressure bar
Definition: rquantities.hpp:316
units::spectralflux_mapper
const std::map< std::string, QSpectralFluxDensity > spectralflux_mapper
str aliases to spectral flux density units
Definition: rquantities.hpp:615
joule
constexpr QEnergy joule
Definition: rquantities.hpp:326
au
constexpr QLength au
Definition: rquantities.hpp:349
angstrom
constexpr QLength angstrom
Definition: rquantities.hpp:274
foot
constexpr QLength foot
Definition: rquantities.hpp:277
carat
constexpr QMass carat
Definition: rquantities.hpp:262
c
constexpr QSpeed c
Definition: rquantities.hpp:351
kelvin
constexpr QTemperature kelvin
Definition: rquantities.hpp:342
speed_of_light
constexpr QSpeed speed_of_light
Definition: rquantities.hpp:350
day
constexpr QTime day
Definition: rquantities.hpp:304
mole
constexpr QSubstance mole(1.0)
inch3
constexpr QVolume inch3
Definition: rquantities.hpp:296
steradian
constexpr AngleArea steradian
Definition: rquantities.hpp:471
cos
double cos(const Angle &num)
Definition: rquantities.hpp:541
foot3
constexpr QVolume foot3
Definition: rquantities.hpp:297
millimetre
constexpr QLength millimetre
Definition: rquantities.hpp:269
inch2
constexpr QArea inch2
Definition: rquantities.hpp:286
radian
constexpr Angle radian(1.0)
Rsun
constexpr QLength Rsun
Definition: rquantities.hpp:357
metre2
constexpr QArea metre2
Definition: rquantities.hpp:282
Jy
constexpr QSpectralFluxDensity Jy
Definition: rquantities.hpp:364
meter
constexpr QLength meter
Definition: rquantities.hpp:266
electron_volt
constexpr QEnergy electron_volt
Definition: rquantities.hpp:327
operator-
constexpr RQuantity< M, l, T, A, C, L, S, D > operator-(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:149
rsun
constexpr QLength rsun
Definition: rquantities.hpp:356
units
Convenient shortcuts.
Definition: rquantities.hpp:583
operator>=
constexpr bool operator>=(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:236
Msun
constexpr QMass Msun
Definition: rquantities.hpp:355
msun
constexpr QMass msun
Definition: rquantities.hpp:354
stone
constexpr QMass stone
Definition: rquantities.hpp:261
operator!=
constexpr bool operator!=(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:226
kilometre
constexpr QLength kilometre
Definition: rquantities.hpp:275
nanometer
constexpr QLength nanometer
Definition: rquantities.hpp:272
degK
constexpr QTemperature degK(1.0)
RQuantity::operator+=
constexpr RQuantity const & operator+=(const RQuantity &rhs)
Definition: rquantities.hpp:74
yard
constexpr QLength yard
Definition: rquantities.hpp:278
operator<
constexpr bool operator<(const RQuantity< M, l, T, A, C, L, S, D > &lhs, const RQuantity< M, l, T, A, C, L, S, D > &rhs)
Definition: rquantities.hpp:241
RQuantity::to
constexpr double to(const RQuantity &rhs) const
Definition: rquantities.hpp:91
nm
constexpr QLength nm
Definition: rquantities.hpp:273
RQuantity::Convert
constexpr double Convert(const RQuantity &rhs) const
Definition: rquantities.hpp:86
Qsquare
constexpr RQuantity< std::ratio_multiply< M, std::ratio< 2 > >, std::ratio_multiply< l, std::ratio< 2 > >, std::ratio_multiply< T, std::ratio< 2 > >, std::ratio_multiply< A, std::ratio< 2 > >, std::ratio_multiply< C, std::ratio< 2 > >, std::ratio_multiply< L, std::ratio< 2 > >, std::ratio_multiply< S, std::ratio< 2 > >, std::ratio_multiply< D, std::ratio< 2 > > > Qsquare(const RQuantity< M, l, T, A, C, L, S, D > &num)
Definition: rquantities.hpp:523
light_year
constexpr QLength light_year
Definition: rquantities.hpp:352
watt
constexpr QFlux watt
Definition: rquantities.hpp:332
newton
constexpr QForce newton(1.0)
tonne
constexpr QMass tonne
Definition: rquantities.hpp:258
astronomical_unit
constexpr QLength astronomical_unit
Definition: rquantities.hpp:348
RQuantity::operator-=
constexpr RQuantity const & operator-=(const RQuantity &rhs)
Definition: rquantities.hpp:79
kilometre2
constexpr QArea kilometre2
Definition: rquantities.hpp:281
kilometre3
constexpr QVolume kilometre3
Definition: rquantities.hpp:290
flam
constexpr QSpectralFluxDensity flam
Definition: rquantities.hpp:361
micrometre
constexpr QLength micrometre
Definition: rquantities.hpp:270
kg
constexpr QMass kg(1.0)
mile
constexpr QLength mile
Definition: rquantities.hpp:279
ounce
constexpr QMass ounce
Definition: rquantities.hpp:259
centimetre
constexpr QLength centimetre
Definition: rquantities.hpp:268
thermochemical_calorie
constexpr QEnergy thermochemical_calorie
Definition: rquantities.hpp:329
psi
constexpr QPressure psi
Definition: rquantities.hpp:317
RQuantity::RQuantity
constexpr RQuantity(double val)
Definition: rquantities.hpp:70
units::parse_length
QLength parse_length(const std::string &str)
find length units from a string
Definition: rquantities.hpp:629
arcsecond
constexpr Angle arcsecond
Definition: rquantities.hpp:470
millimetre2
constexpr QArea millimetre2
Definition: rquantities.hpp:285
second
constexpr QTime second(1.0)
cal
constexpr QEnergy cal
Definition: rquantities.hpp:330
centimetre2
constexpr QArea centimetre2
Definition: rquantities.hpp:284
kilopond
constexpr QForce kilopond
Definition: rquantities.hpp:313
jansky
constexpr QSpectralFluxDensity jansky
Definition: rquantities.hpp:363
gram
constexpr QMass gram
Definition: rquantities.hpp:257
operator<<
std::ostream & operator<<(std::ostream &os, RQuantity< M, l, T, A, C, L, S, D > &rhs)
Nice representation.
Definition: rquantities.hpp:554
foot2
constexpr QArea foot2
Definition: rquantities.hpp:287
ly
constexpr QLength ly
Definition: rquantities.hpp:353