source: Sophya/trunk/SophyaLib/BaseTools/sunitpcst.h@ 4056

Last change on this file since 4056 was 4056, checked in by ansari, 13 years ago

Ajout fichiers pqnumber.h .cc et sunits.h .cc : classes PrimeNumbers , QNumber , Units , PhysQty - MAJ Makefile, sophyainit et numero de version SOPHYA a 2.222 V_Avr12, Reza 16/04/2012

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