ReFRACtor
unit.h
Go to the documentation of this file.
1 #ifndef UNIT_H
2 #define UNIT_H
3 #include "printable.h"
4 #include <boost/rational.hpp>
5 #include <boost/array.hpp>
6 #include <boost/operators.hpp>
7 
8 namespace FullPhysics {
9 /****************************************************************/
25 class Unit : public Printable<Unit>,
26  boost::equality_comparable<Unit>,
27  boost::multipliable<Unit>,
28  boost::multipliable<Unit, double>,
29  boost::dividable<Unit>,
30  boost::dividable<Unit, double> {
31 public:
32  enum {number_base_unit = 11};
33  Unit(const std::string& Name, double Conversion_to_si,
34  const boost::rational<int>& Length_power,
35  const boost::rational<int>& Mass_power,
36  const boost::rational<int>& Time_power,
37  const boost::rational<int>& Current_power,
38  const boost::rational<int>& Temperature_power,
39  const boost::rational<int>& Amount_power,
40  const boost::rational<int>& Luminous_intensity_power,
41  const boost::rational<int>& Solid_angle_power,
42  const boost::rational<int>& Angle_power,
43  const boost::rational<int>& Photon_count_power,
44  const boost::rational<int>& Sample_index);
45  Unit(const std::string& Name, const Unit& Dunit);
46  Unit(const std::string& Name_to_parse);
47  Unit();
48 
49 //-----------------------------------------------------------------------
51 //-----------------------------------------------------------------------
52 
53  const boost::array<boost::rational<int>, number_base_unit>& base_unit_powers() const
54  {return base_unit_powers_;}
55 
56 //-----------------------------------------------------------------------
58 //-----------------------------------------------------------------------
59 
60  double conversion_to_si() const {return conversion_to_si_;}
61 
62 //-----------------------------------------------------------------------
66 //-----------------------------------------------------------------------
67 
68  bool is_commensurate(const Unit& Units) const
69  { return base_unit_powers() == Units.base_unit_powers(); }
70 
71  static Unit parse(const std::string& S);
72 
73 //-----------------------------------------------------------------------
75 //-----------------------------------------------------------------------
76 
77  const std::string& name() const {return name_;}
78 
79 //-----------------------------------------------------------------------
81 //-----------------------------------------------------------------------
82 
83  void name(const std::string& V) {name_ = V;}
84 
85 //-----------------------------------------------------------------------
87 //-----------------------------------------------------------------------
88  Unit& operator*=(const Unit& Dunit);
89  Unit& operator*=(double Scale_factor);
90  Unit& operator/=(double Scale_factor);
91  Unit& operator/=(const Unit& Dunit);
92  bool operator==(const Unit& U) const;
93  void print(std::ostream& Os) const;
94 private:
95  boost::array<boost::rational<int>, number_base_unit> base_unit_powers_;
96  double conversion_to_si_;
97  std::string name_;
98 };
99 
100 Unit pow(const Unit& Dunit, const boost::rational<int>& Exponent);
101 double conversion(const Unit& Dunit_from, const Unit& Dunit_to);
102 inline Unit operator/(double Scale_factor, const Unit& Unit)
103 {
104  return Scale_factor * pow(Unit, -1);
105 }
106 
107 //-----------------------------------------------------------------------
111 //-----------------------------------------------------------------------
112  namespace units {
113  // base units
114  const Unit m("m", 1.0, 1,0,0,0,0,0,0,0,0,0,0);
115  const Unit kg("kg", 1.0, 0,1,0,0,0,0,0,0,0,0,0);
116  const Unit s("s", 1.0, 0,0,1,0,0,0,0,0,0,0,0);
117  const Unit K("K", 1.0, 0,0,0,1,0,0,0,0,0,0,0);
118  const Unit A("A", 1.0, 0,0,0,0,1,0,0,0,0,0,0);
119  const Unit mol("mol", 1.0, 0,0,0,0,0,1,0,0,0,0,0);
120  const Unit cd("cd", 1.0, 0,0,0,0,0,0,1,0,0,0,0);
121  const Unit sr("sr", 1.0, 0,0,0,0,0,0,0,1,0,0,0);
122  const Unit rad("rad", 1.0, 0,0,0,0,0,0,0,0,1,0,0);
123  const Unit ph("ph", 1.0, 0,0,0,0,0,0,0,0,0,1,0);
124  const Unit sample_index("sample_index",
125  1.0, 0,0,0,0,0,0,0,0,0,0,1);
126  const Unit dimensionless("dimensionless",
127  1.0, 0,0,0,0,0,0,0,0,0,0,0);
128 
129 //-----------------------------------------------------------------------
131 //-----------------------------------------------------------------------
132 
133  const double pi = 3.14159265358979323846;
134 
135  const Unit deg("deg", pi / 180.0 * rad);
136  const Unit arcsec("arcsec", deg / (60.0 * 60));
137 
138  // Some useful names
139  const Unit day("day", 24 * 60 * 60 * s);
140  const Unit year("year", 365.25 * 24 * 60 * 60 * s);
141  const Unit cm("cm", 1e-2 * m);
142  const Unit km("km", 1e3 * m);
143  const Unit nm("nm", 1e-9 * m);
144  const Unit micron("micron", 1e-6 * m);
145  const Unit g("g", 1e-3 * kg);
146  const Unit inv_cm("cm^-1", pow(cm, -1));
147  const Unit inv_sr("sr^-1", pow(sr, -1));
148  // Derived units
149  const Unit N("N", kg * m / (s*s));
150  const Unit Pa("Pa", N / (m * m));
151  const Unit J("J", N * m);
152  const Unit W("W", J / s);
153  const Unit uW("uW", 1e-6 * W);
154  const Unit mW("mW", 1e-3 * W);
155  const Unit kW("kW", 1e3 * W);
156  }
157 }
158 #endif
const boost::array< boost::rational< int >, number_base_unit > & base_unit_powers() const
Array of the powers of the base units (so m^2 would return (1,0,0,0,0,0,0,0))
Definition: unit.h:53
const Unit arcsec("arcsec", deg/(60.0 *60))
Unit & operator/=(double Scale_factor)
Definition: unit.cc:106
const Unit s("s", 1.0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0)
static Unit parse(const std::string &S)
Parse a string, and return a Unit that matches the given units.
Definition: unit.cc:350
void name(const std::string &V)
Set name of unit.
Definition: unit.h:83
Unit operator/(double Scale_factor, const Unit &Unit)
Definition: unit.h:102
const Unit cm("cm", 1e-2 *m)
const Unit mW("mW", 1e-3 *W)
bool operator==(const Unit &U) const
Test for equality.
Definition: unit.cc:133
const Unit dimensionless("dimensionless", 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
const Unit nm("nm", 1e-9 *m)
Unit()
Default constructor. This creates a dimensionless unit.
Definition: unit.cc:66
const Unit kW("kW", 1e3 *W)
const Unit Pa("Pa", N/(m *m))
double conversion(const Unit &Dunit_from, const Unit &Dunit_to)
Return conversion factor to go from one unit to another.
Definition: unit.cc:180
const Unit sr("sr", 1.0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
const double pi
Pi.
Definition: unit.h:133
const Unit J("J", N *m)
This is a Mixin for classes that can be printed.
Definition: printable.h:24
const Unit micron("micron", 1e-6 *m)
const Unit sample_index("sample_index", 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
const Unit A("A", 1.0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)
void print(std::ostream &Os) const
Print to a stream.
Definition: unit.cc:143
const Unit inv_cm("cm^-1", pow(cm, -1))
const Unit K("K", 1.0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
const Unit kg("kg", 1.0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
const Unit m("m", 1.0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Unit pow(const Unit &Dunit, const boost::rational< int > &Exponent)
Definition: unit.cc:158
const Unit inv_sr("sr^-1", pow(sr, -1))
double conversion_to_si() const
Conversion factor to go to SI units.
Definition: unit.h:60
const Unit day("day", 24 *60 *60 *s)
const Unit year("year", 365.25 *24 *60 *60 *s)
const Unit cd("cd", 1.0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)
const Unit g("g", 1e-3 *kg)
Libraries such as boost::units allow unit handling where we know the units at compile time...
Definition: unit.h:25
bool is_commensurate(const Unit &Units) const
Test if this set of units is commensurate with another set.
Definition: unit.h:68
const Unit N("N", kg *m/(s *s))
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
const Unit mol("mol", 1.0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)
const Unit uW("uW", 1e-6 *W)
const Unit ph("ph", 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)
const std::string & name() const
Name of unit. May be an empty string if a name wasn&#39;t assigned.
Definition: unit.h:77
const Unit km("km", 1e3 *m)
Unit & operator*=(const Unit &Dunit)
Basic math operators for units.
Definition: unit.cc:87
const Unit rad("rad", 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0)
const Unit deg("deg", pi/180.0 *rad)
const Unit W("W", J/s)

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11