[4056] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | //-----------------------------------------------------------
|
---|
| 3 | // Class BasePhysQty : Base class for units and Physical Constants
|
---|
| 4 | // SOPHYA class library - (C) UPS+LAL IN2P3/CNRS , CEA-Irfu
|
---|
| 5 | // R. Ansari UPS+LAL IN2P3/CNRS 2012
|
---|
| 6 | //-----------------------------------------------------------
|
---|
| 7 |
|
---|
| 8 | #ifndef SUNITPCST_H_SEEN
|
---|
| 9 | #define SUNITPCST_H_SEEN
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | #include "machdefs.h"
|
---|
| 13 | #include <math.h>
|
---|
| 14 | #include <string>
|
---|
| 15 | #include <vector>
|
---|
| 16 | #include <iostream>
|
---|
| 17 |
|
---|
| 18 | #include "pqnumber.h"
|
---|
| 19 | #include "objfio.h"
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | namespace SOPHYA {
|
---|
| 23 |
|
---|
| 24 | //--------------------------------------------------------------------------------
|
---|
| 25 | //-------------------------------- Units class ---------------------------------
|
---|
| 26 | //--------------------------------------------------------------------------------
|
---|
| 27 |
|
---|
| 28 | //! Class representing physical dimensions and units
|
---|
| 29 | class Units : public AnyDataObj {
|
---|
| 30 | // gestionnaire PPF
|
---|
| 31 | friend class ObjFileIO<Units>;
|
---|
| 32 |
|
---|
| 33 | public:
|
---|
[4062] | 34 | //! Default constructor - creates a dimensionless object = 1
|
---|
[4056] | 35 | Units();
|
---|
[4062] | 36 | //! Copy constructor
|
---|
[4056] | 37 | Units(Units const& un);
|
---|
[4062] | 38 | //! Base constructor
|
---|
[4056] | 39 | Units(const char* nom, const char* snom, int m, int kg, int s, r_8 val=1., int A=0, int K=0, int mol=0, int cd=0);
|
---|
[4062] | 40 | //! Constructor from an existing unit, but with a scaling factor and new name
|
---|
| 41 | Units(Units const& un, r_8 scale, const char* nom, const char* snom);
|
---|
| 42 |
|
---|
[4056] | 43 | virtual ~Units();
|
---|
| 44 |
|
---|
| 45 | //! Copy method
|
---|
| 46 | virtual Units& Set(Units const& un);
|
---|
| 47 | //! Copy (=) operator
|
---|
| 48 | inline Units& operator = (Units const& un) { return Set(un); }
|
---|
| 49 | //! return true if the two quantities have the same dimension
|
---|
| 50 | bool isSameDimension(Units const& un) const;
|
---|
| 51 | //! return the value in base SI units
|
---|
| 52 | inline r_8 SIValue() const { return mSI_Value_; }
|
---|
| 53 | //! return the unit name
|
---|
| 54 | inline string const& Name() const { return mName_; }
|
---|
| 55 | //! return the unit short name
|
---|
| 56 | inline string const& ShortName() const { return mShortName_; }
|
---|
| 57 |
|
---|
| 58 | //! Prints the object on \b cout (return the cout stream object)
|
---|
| 59 | inline ostream& Print(int lev=2) const
|
---|
| 60 | { return Print(cout,lev); }
|
---|
| 61 | //! Prints the object on stream \b os (return the os stream object)
|
---|
| 62 | virtual ostream& Print(ostream& os,int lev=2) const;
|
---|
| 63 |
|
---|
| 64 | //! Return a new Unit corresponding to a rational power of the initial Unit
|
---|
| 65 | virtual Units power(QNumber q) const;
|
---|
| 66 | //! Return a new Unit corresponding to an integer power of the initial Unit
|
---|
| 67 | inline Units power(int p) const
|
---|
| 68 | { return power(QNumber(p,1)); }
|
---|
| 69 |
|
---|
| 70 | //! Compute a new unit corresponding corresponding to the product of two units
|
---|
| 71 | static Units Multiply(Units const& u1, Units const& u2);
|
---|
| 72 | //! Compute a new unit corresponding corresponding to the ratio of two units
|
---|
| 73 | static Units Divide(Units const& u1, Units const& u2);
|
---|
| 74 |
|
---|
| 75 | // Unites multiples et sous-multiples
|
---|
| 76 | inline Units micro()
|
---|
| 77 | { return this->Multiple(1.e-6,"micro","mu"); }
|
---|
| 78 | Units milli()
|
---|
| 79 | { return this->Multiple(1.e-3,"milli","m"); }
|
---|
| 80 | Units centi()
|
---|
| 81 | { return this->Multiple(1.e-2,"centi","c"); }
|
---|
| 82 | Units deci()
|
---|
| 83 | { return this->Multiple(1.e-1,"deci","d"); }
|
---|
| 84 | Units deca()
|
---|
| 85 | { return this->Multiple(1.e1,"deca","da"); }
|
---|
| 86 | Units hecto()
|
---|
| 87 | { return this->Multiple(1.e2,"hecto","h"); }
|
---|
| 88 | Units kilo()
|
---|
| 89 | { return this->Multiple(1.e3,"kilo","k"); }
|
---|
| 90 | Units mega()
|
---|
| 91 | { return this->Multiple(1.e6,"mega","M"); }
|
---|
| 92 | Units giga()
|
---|
| 93 | { return this->Multiple(1.e9,"giga","G"); }
|
---|
| 94 |
|
---|
| 95 | // Les unites usuelles - Voir definition et convention sur
|
---|
[4062] | 96 | // http://www.bipm.org/fr/si/ et http://physics.nist.gov/cuu/Units/
|
---|
[4056] | 97 | inline static Units meter()
|
---|
| 98 | { return Units("meter","m",1,0,0); }
|
---|
| 99 | inline static Units kilometer()
|
---|
| 100 | { return Units("kilometer","km",1,0,0,1.e3); }
|
---|
| 101 | inline static Units centimeter()
|
---|
| 102 | { return Units("centimeter","cm",1,0,0,1.e-2); }
|
---|
| 103 | inline static Units millimeter()
|
---|
| 104 | { return Units("millimeter","mm",1,0,0,1.e-3); }
|
---|
| 105 |
|
---|
| 106 | inline static Units kilogram()
|
---|
| 107 | { return Units("kilogram","kg",0,1,0); }
|
---|
| 108 | inline static Units gram()
|
---|
| 109 | { return Units("gram","g",0,1,0,0.001); }
|
---|
| 110 |
|
---|
| 111 | inline static Units second()
|
---|
| 112 | { return Units("second","s",0,0,1); }
|
---|
| 113 | inline static Units hour()
|
---|
| 114 | { return Units("hour","h",0,0,1,3600.); }
|
---|
| 115 | inline static Units minute()
|
---|
| 116 | { return Units("minute","mn",0,0,1,60.); }
|
---|
| 117 |
|
---|
| 118 | inline static Units liter()
|
---|
| 119 | { return Units("liter","L",3,0,0); }
|
---|
| 120 |
|
---|
| 121 | inline static Units ampere()
|
---|
| 122 | { return Units("ampere","A",0,0,0,1.,1); }
|
---|
| 123 | inline static Units kelvin()
|
---|
| 124 | { return Units("kelvin","K",0,0,0,1.,0,1); }
|
---|
| 125 | inline static Units mole()
|
---|
| 126 | { return Units("mole","mol",0,0,0,1.,0,0,1); }
|
---|
| 127 | inline static Units candela()
|
---|
| 128 | { return Units("candela","cd",0,0,0,1.,0,0,0,1); }
|
---|
| 129 |
|
---|
| 130 | inline static Units newton()
|
---|
| 131 | { return Units("newton","N",1,1,-2); }
|
---|
| 132 |
|
---|
| 133 | inline static Units pascal()
|
---|
| 134 | { return Units("pascal","Pa",-1,1,-2); }
|
---|
| 135 |
|
---|
| 136 | inline static Units joule()
|
---|
| 137 | { return Units("joule","J",2,1,-2); }
|
---|
| 138 | inline static Units electronvolt()
|
---|
| 139 | { return Units("electronvolt","eV",2,1,-2,1.602176e-19); }
|
---|
| 140 |
|
---|
| 141 | inline static Units watt()
|
---|
| 142 | { return Units("watt","W",2,1,-3); }
|
---|
| 143 |
|
---|
| 144 | inline static Units hertz()
|
---|
| 145 | { return Units("hertz","Hz",0,0,-1); }
|
---|
| 146 |
|
---|
| 147 | inline static Units coulomb()
|
---|
| 148 | { return Units("coulomb","C",0,0,1,1.,1); }
|
---|
| 149 | inline static Units volt()
|
---|
| 150 | { return Units("volt","V",2,1,-3,1.,-1); }
|
---|
| 151 | inline static Units tesla()
|
---|
| 152 | { return Units("tesla","T",0,1,-2,1.,-1); }
|
---|
| 153 | inline static Units ohm()
|
---|
| 154 | { return Units("Ohm","Ohm",2,1,-3,1.,-2); }
|
---|
| 155 | inline static Units henry()
|
---|
| 156 | { return Units("henry","H",2,1,-2,1.,-2); }
|
---|
| 157 | inline static Units farad()
|
---|
| 158 | { return Units("farad","F",-2,-1,4,1.,2); }
|
---|
| 159 |
|
---|
| 160 | inline static Units radian()
|
---|
| 161 | { return Units("radian","rad",0,0,0,1); }
|
---|
[4062] | 162 | inline static Units degree()
|
---|
| 163 | { return Units("degree","deg",0,0,0,M_PI/180.); }
|
---|
[4056] | 164 | inline static Units steradian()
|
---|
| 165 | { return Units("steradian","sr",0,0,0,1); }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | protected:
|
---|
| 169 | inline void SetName(string const& nom, string const& snom)
|
---|
| 170 | { mName_=nom; mShortName_=snom; }
|
---|
| 171 | inline Units Multiple(r_8 fact, const char* pre, const char* spre)
|
---|
| 172 | {
|
---|
| 173 | Units ru(*this); ru.mSI_Value_*=fact;
|
---|
| 174 | ru.mName_=pre+mName_; ru.mShortName_=spre+mShortName_;
|
---|
| 175 | return ru;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | // International system of Units (SI) - http://physics.nist.gov/cuu/Units/bibliography.html
|
---|
| 179 | QNumber mLength_, mMass_, mTime_, mCurr_; // Dimensions : length, mass, time and current
|
---|
| 180 | QNumber mTemp_, mSubst_, mLumInt_; // Dimensions : Temperature, Amount of substance, LuminousIntensity
|
---|
| 181 | r_8 mSI_Value_; // Value in SI units (m, kg, s, A, Kelvin, mole, Candela)
|
---|
| 182 | string mName_, mShortName_; // Name and short name
|
---|
| 183 | };
|
---|
| 184 |
|
---|
| 185 | /*! operator << overloading - Prints Units in string format on \b os*/
|
---|
| 186 | inline ostream& operator << (ostream& s, Units const & un)
|
---|
| 187 | { un.Print(s,0); return(s); }
|
---|
| 188 |
|
---|
| 189 | /*! Multiply operator definition for two Units objects */
|
---|
| 190 | inline Units operator * (Units const & a, Units const & b)
|
---|
| 191 | { return Units::Multiply(a,b); }
|
---|
| 192 |
|
---|
| 193 | /*! Divide operator definition for two Units objects */
|
---|
| 194 | inline Units operator / (Units const & a, Units const & b)
|
---|
| 195 | { return Units::Divide(a,b); }
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | //! Writes the Units object in the POutPersist stream \b os
|
---|
| 199 | inline POutPersist& operator << (POutPersist& os, Units & obj)
|
---|
| 200 | { ObjFileIO<Units> fio(&obj); fio.Write(os); return(os); }
|
---|
| 201 | //! Reads the Units object from the PInPersist stream \b is
|
---|
| 202 | inline PInPersist& operator >> (PInPersist& is, Units & obj)
|
---|
| 203 | { ObjFileIO<Units> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
|
---|
| 204 |
|
---|
| 205 | //--------------------------------------------------------------------------------
|
---|
| 206 | //------------------------------- PhysQty class --------------------------------
|
---|
| 207 | //--------------------------------------------------------------------------------
|
---|
| 208 |
|
---|
| 209 | //! Class representing physical quantities and constants
|
---|
| 210 | class PhysQty : public AnyDataObj {
|
---|
| 211 | // gestionnaire PPF
|
---|
| 212 | friend class ObjFileIO<PhysQty>;
|
---|
| 213 |
|
---|
| 214 | public:
|
---|
| 215 | PhysQty();
|
---|
| 216 | PhysQty(Units const & u, r_8 val, r_8 prec=0., const char* nom=NULL);
|
---|
| 217 | PhysQty(PhysQty const & qty);
|
---|
| 218 | virtual ~PhysQty();
|
---|
| 219 |
|
---|
| 220 | //! Copy method
|
---|
| 221 | virtual PhysQty& Set(PhysQty const& qty);
|
---|
| 222 | //! Copy (=) operator
|
---|
| 223 | inline PhysQty& operator = (PhysQty const& qty) { return Set(qty); }
|
---|
| 224 |
|
---|
| 225 | //! Return a new physical quantity object after unit conversion
|
---|
| 226 | PhysQty ConvertTo(Units const & u) const ;
|
---|
| 227 |
|
---|
| 228 | //! Return the physical quantity value in SI units
|
---|
| 229 | inline r_8 SIValue() const { return val_*unit_.SIValue(); }
|
---|
[4062] | 230 | //! Return the associated unit
|
---|
[4056] | 231 | inline Units getUnit() {return unit_; }
|
---|
[4062] | 232 | //! Return the associated value
|
---|
[4056] | 233 | inline r_8 Value() const { return val_; }
|
---|
[4062] | 234 | //! Change the value of the physical quantity
|
---|
[4056] | 235 | inline void setValue(r_8 val) { val_=val; }
|
---|
[4062] | 236 | //! Return the relative precision
|
---|
[4056] | 237 | inline r_8 RelativePrecision() const { return prec_; }
|
---|
[4062] | 238 | //! Change the relative precision
|
---|
[4056] | 239 | inline void setRelativePrecision(r_8 prec) { prec_=prec; }
|
---|
[4062] | 240 | //! Return the absolute precision
|
---|
[4056] | 241 | inline r_8 AbsolutePrecision() const { return prec_*val_; }
|
---|
[4062] | 242 | //! Return the associoated name
|
---|
[4056] | 243 | inline string const& Name() const { return name_; }
|
---|
| 244 |
|
---|
| 245 | //! Prints the object on \b cout (return the cout stream object)
|
---|
| 246 | inline ostream& Print(int lev=0) const
|
---|
| 247 | { return Print(cout, lev); }
|
---|
| 248 | //! Prints the object on stream \b os (return the os stream object)
|
---|
| 249 | virtual ostream& Print(ostream& os,int lev=2) const;
|
---|
| 250 |
|
---|
| 251 | //---- Constantes physiques - source NIST http://physics.nist.gov/
|
---|
| 252 | //! Return the speed of light in m/s
|
---|
| 253 | inline static PhysQty SpeedofLight()
|
---|
| 254 | { return PhysQty(Units::meter()/Units::second(), 299792458., 0., "Speed of light (c)"); }
|
---|
| 255 | //! Return the speed of light in m/s
|
---|
| 256 | inline static PhysQty c()
|
---|
| 257 | { return PhysQty::SpeedofLight(); }
|
---|
| 258 |
|
---|
| 259 | //! Return the Newtonian constant of gravitation in m^3 kg^-1 s^-2
|
---|
| 260 | inline static PhysQty G_Newton()
|
---|
| 261 | { return PhysQty(Units::meter().power(3)*Units::kilogram().power(-1)*Units::second().power(-2),
|
---|
| 262 | 6.67384e-11, 1.2e-4, "Newtonian constant of gravitation (G)"); }
|
---|
| 263 | //! Return the Newtonian constant of gravitation in m^3 kg^-1 s^-2
|
---|
| 264 | inline static PhysQty G()
|
---|
| 265 | { return PhysQty::G_Newton(); }
|
---|
| 266 |
|
---|
| 267 | //! Return the Planck constant in joule.s
|
---|
| 268 | inline static PhysQty PlanckConstant()
|
---|
| 269 | { return PhysQty(Units::joule()*Units::second(), 6.62606957e-34, 4.4e-8, "Planck constant (h)"); }
|
---|
| 270 | //! Return the Planck constant in joule.s
|
---|
| 271 | inline static PhysQty h()
|
---|
| 272 | { return PhysQty::PlanckConstant(); }
|
---|
| 273 | //! Return the Planck constant divided by 2 pi (hbar = h/2pi) in joule.s
|
---|
| 274 | inline static PhysQty hbar()
|
---|
| 275 | { return PhysQty(Units::joule()*Units::second(), 6.62606957e-34/2./M_PI, 4.4e-8, "hbar (h/2pi)"); }
|
---|
| 276 |
|
---|
| 277 | //! Return the Boltzmann constant in joule/K
|
---|
| 278 | inline static PhysQty BoltzmannConstant()
|
---|
| 279 | { return PhysQty(Units::joule()/Units::kelvin(), 1.3806488e-23, 9.1e-7, "Boltzmann constant (k)"); }
|
---|
| 280 | //! Return the Boltzmann constant in joule/K
|
---|
| 281 | inline static PhysQty k()
|
---|
| 282 | { return PhysQty::BoltzmannConstant(); }
|
---|
| 283 | //! Return the molar gas constant (R) in joule/mol/K
|
---|
| 284 | inline static PhysQty MolarGasConstant()
|
---|
| 285 | { return PhysQty(Units::joule()/Units::mole()/Units::kelvin(), 8.3144621, 9.1e-7, "Molar gas constant (R)"); }
|
---|
| 286 | //! Return the molar gas constant (R) in joule/mol/K
|
---|
| 287 | inline static PhysQty R()
|
---|
| 288 | { return PhysQty::MolarGasConstant(); }
|
---|
| 289 |
|
---|
| 290 | //! Return the Avogadro number (units mol^-1)
|
---|
| 291 | inline static PhysQty N_Avogradro()
|
---|
| 292 | { return PhysQty(Units::mole().power(-1), 6.02214129e23, 4.4e-8, "Avogadro constant (N_A)"); }
|
---|
| 293 | //! Return the Avogadro number (units mol^-1)
|
---|
| 294 | inline static PhysQty N_A()
|
---|
| 295 | { return PhysQty::N_Avogradro(); }
|
---|
| 296 |
|
---|
| 297 | //! Return the electric constant in Farad/m
|
---|
| 298 | inline static PhysQty epsilon0()
|
---|
| 299 | { return PhysQty(Units::farad()/Units::meter(), 8.854187817e-12, 0., "Electric constant (epsilon0)"); }
|
---|
| 300 | //! Return the magnetic constant in N / A^2
|
---|
| 301 | inline static PhysQty mu0()
|
---|
| 302 | { return PhysQty(Units::newton()/Units::ampere().power(2), 4.*M_PI*1e-7, 0., "Magnetic constant (mu0)"); }
|
---|
| 303 |
|
---|
| 304 | //! Return the electron mass in Kg
|
---|
| 305 | inline static PhysQty electron_mass()
|
---|
| 306 | { return PhysQty(Units::kilogram(), 9.10938291e-31, 4.4e-8, "Electron mass (m_e)"); }
|
---|
| 307 | //! Return the proton mass in Kg
|
---|
| 308 | inline static PhysQty proton_mass()
|
---|
| 309 | { return PhysQty(Units::kilogram(), 1.672621777e-27, 4.4e-8, "Proton mass (m_p)"); }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | protected:
|
---|
| 313 | Units unit_; // Unite de la grandeur physique
|
---|
| 314 | r_8 val_; // Valeur de la grandeur physique dans l'unite unit_
|
---|
| 315 | r_8 prec_; // precision relative (1 sigma)
|
---|
| 316 | string name_; // Name of the quantity / constant
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | /*! operator << overloading - Prints PhysQty on \b os*/
|
---|
| 320 | inline ostream& operator << (ostream& s, PhysQty const & qty)
|
---|
| 321 | { qty.Print(s,0); return(s); }
|
---|
| 322 |
|
---|
| 323 | //! Writes the PhysQty object in the POutPersist stream \b os
|
---|
| 324 | inline POutPersist& operator << (POutPersist& os, PhysQty & obj)
|
---|
| 325 | { ObjFileIO<PhysQty> fio(&obj); fio.Write(os); return(os); }
|
---|
| 326 | //! Reads the PhysQty object from the PInPersist stream \b is
|
---|
| 327 | inline PInPersist& operator >> (PInPersist& is, PhysQty & obj)
|
---|
| 328 | { ObjFileIO<PhysQty> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | } // namespace SOPHYA
|
---|
| 332 |
|
---|
| 333 | #endif
|
---|