source: Sophya/trunk/SophyaLib/BaseTools/sunitpcst.cc@ 4082

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

Petites ameliorations classes Units et PhysQty, documentation doxygen, ajout fichiers pqnumber.h et sunitpcst.h ds basetools.h, passage numero de version a 2.30, Reza 27/04/2012

File size: 9.2 KB
Line 
1#include "machdefs.h"
2#include "sunitpcst.h"
3
4namespace SOPHYA {
5
6/*!
7 \class Units
8 \ingroup BaseTools
9 This class represents physical dimension and units. Static methods return Units
10 objects corresponding to the usual units, such as meter or watt
11
12 \code
13 Units w=Units::watt();
14 cout << " Power unit (watt) : " << w.Name() << " (" << w << ")" << endl;
15 // define the speed SI unit (m/s) :
16 Units mos = Units::meter()/Units::second() ;
17 cout << " SI speed unit : " << mos << " -> "; mos.Print() ;
18 // mutiple of an existing unit :
19 Units mw=Units::watt().mega();
20 cout << " Megawatt: " << mw.Name() << " (" << mw << ") -> "; mw.Print() ;
21 \endcode
22*/
23
24//-------------------------------------------------------------------------
25//------------------------- Classe Units --------------------------
26//-------------------------------------------------------------------------
27
28/* --Methode-- */
29Units::Units()
30 : mLength_(0), mMass_(0), mTime_(0), mCurr_(0),
31 mTemp_(0), mSubst_(0), mLumInt_(0), mSI_Value_(0.)
32{
33}
34
35/* --Methode-- */
36/*!
37 The Units object is created by specifying its name and short name, and the integer powers
38 for the seven SI base physical quantities (length, mass, time \ldots), and the value of the
39 unit in term of the SI unit.
40*/
41Units::Units(const char* nom, const char* snom, int m, int kg, int s, r_8 val, int A, int K, int mol, int cd)
42 : mLength_(m), mMass_(kg), mTime_(s), mCurr_(A),
43 mTemp_(K), mSubst_(mol), mLumInt_(cd), mSI_Value_(val),
44 mName_(nom), mShortName_(snom)
45{
46}
47
48
49/* --Methode-- */
50Units::Units(Units const& un)
51{
52 Set(un);
53}
54
55/* --Methode-- */
56Units::Units(Units const& un, r_8 scale, const char* nom, const char* snom)
57{
58 Set(un);
59 mSI_Value_*=scale;
60 mName_=nom;
61 mShortName_=snom;
62}
63
64/* --Methode-- */
65Units::~Units()
66{
67}
68
69/* --Methode-- */
70Units& Units::Set(Units const& un)
71{
72 mLength_ = un.mLength_;
73 mMass_=un.mMass_;
74 mTime_=un.mTime_;
75 mCurr_=un.mCurr_;
76 mTemp_=un.mTemp_;
77 mSubst_=un.mSubst_;
78 mLumInt_=un.mLumInt_;
79 mSI_Value_=un.mSI_Value_;
80 mName_=un.mName_;
81 mShortName_=un.mShortName_;
82 return (*this);
83}
84
85/* --Methode-- */
86bool Units::isSameDimension(Units const& un) const
87{
88 if ( (mLength_ == un.mLength_) && (mMass_==un.mMass_) &&
89 (mTime_==un.mTime_) && (mCurr_==un.mCurr_) &&
90 (mTemp_==un.mTemp_) && (mSubst_==un.mSubst_) && (mLumInt_==un.mLumInt_) ) return true;
91 else return false;
92}
93
94/* --Methode-- */
95ostream& Units::Print(ostream& os, int lev) const
96{
97 if (lev < 2) {
98 if (lev==0) os << mShortName_;
99 else os << mName_;
100 return os;
101 }
102 os << mName_ << " (" << mShortName_ << ") SI_Value=" << mSI_Value_ << " [ ";
103 if (!mLength_.isZero()) os << "L" << '^' << mLength_ << ' ';
104 if (!(mMass_.isZero())) os << "M" << '^' << mMass_ << ' ';
105 if (!(mTime_.isZero())) os << "T" << '^' << mTime_ << ' ';
106 if (!mCurr_.isZero()) os << "I" << '^' << mCurr_ << ' ';
107 if (!mTemp_.isZero()) os << "K" << '^' << mTemp_ << ' ';
108 if (!mSubst_.isZero()) os << "Mol" << '^' << mSubst_ << ' ';
109 if (!mLumInt_.isZero()) os << "Lum" << '^' << mLumInt_ << ' ';
110 cout << " ]" << endl;
111 return os;
112}
113
114/* --Methode-- */
115Units Units::power(QNumber q) const
116{
117 Units ru;
118 ru.mLength_=mLength_*q;
119 ru.mMass_=mMass_*q;
120 ru.mTime_=mTime_*q;
121 ru.mCurr_=mCurr_*q;
122 ru.mTemp_=mTemp_*q;
123 ru.mSubst_=mSubst_*q;
124 ru.mLumInt_=mLumInt_*q;
125 ru.mSI_Value_=pow(mSI_Value_,(double)q);
126 ru.mName_=mName_ + '^' + q.ToString();
127 ru.mShortName_=mShortName_ + '^' + q.ToString();
128 return ru;
129}
130
131/* --Methode-- */
132Units Units::Multiply(Units const& u1, Units const& u2)
133{
134 Units ru;
135 ru.mLength_=u1.mLength_+u2.mLength_;
136 ru.mMass_=u1.mMass_+u2.mMass_;
137 ru.mTime_=u1.mTime_+u2.mTime_;
138 ru.mCurr_=u1.mCurr_+u2.mCurr_;
139 ru.mTemp_=u1.mTemp_+u2.mTemp_;
140 ru.mSubst_=u1.mSubst_+u2.mSubst_;
141 ru.mLumInt_=u1.mLumInt_+u2.mLumInt_;
142 ru.mSI_Value_=u1.mSI_Value_*u2.mSI_Value_;
143 ru.mName_=u1.mName_ + '.' + u2.mName_;
144 ru.mShortName_=u1.mShortName_ + '.' + u2.mShortName_;
145 return ru;
146}
147
148/* --Methode-- */
149Units Units::Divide(Units const& u1, Units const& u2)
150{
151 Units ru;
152 ru.mLength_=u1.mLength_-u2.mLength_;
153 ru.mMass_=u1.mMass_-u2.mMass_;
154 ru.mTime_=u1.mTime_-u2.mTime_;
155 ru.mCurr_=u1.mCurr_-u2.mCurr_;
156 ru.mTemp_=u1.mTemp_-u2.mTemp_;
157 ru.mSubst_=u1.mSubst_-u2.mSubst_;
158 ru.mLumInt_=u1.mLumInt_-u2.mLumInt_;
159 ru.mSI_Value_=u1.mSI_Value_/u2.mSI_Value_;
160 ru.mName_=u1.mName_ + '/' + u2.mName_;
161 ru.mShortName_=u1.mShortName_ + '/' + u2.mShortName_;
162 return ru;
163}
164
165
166//----------------------------------------------------------
167// Classe pour la gestion de persistance de Units
168// ObjFileIO<Units>
169//----------------------------------------------------------
170
171/* --Methode-- */
172DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
173void ObjFileIO<Units>::WriteSelf(POutPersist& s) const
174{
175 if (dobj == NULL)
176 throw NullPtrError("ObjFileIO<Units>::WriteSelf() dobj=NULL");
177 int_4 ver;
178 ver = 1;
179 s.Put(ver); // ecriture numero de version PPF
180 s << dobj->mLength_ << dobj->mMass_ << dobj->mTime_ << dobj->mCurr_;
181 s << dobj->mTemp_ << dobj->mSubst_ << dobj->mLumInt_;
182 s << dobj->mSI_Value_;
183 s << dobj->mName_ << dobj->mShortName_;
184}
185
186/* --Methode-- */
187DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
188void ObjFileIO<Units>::ReadSelf(PInPersist& s)
189{
190 int_4 ver;
191 s.Get(ver); // Lecture numero de version PPF
192
193 if (dobj == NULL) dobj = new Units();
194 s >> dobj->mLength_ >> dobj->mMass_ >> dobj->mTime_ >> dobj->mCurr_;
195 s >> dobj->mTemp_ >> dobj->mSubst_ >> dobj->mLumInt_;
196 s >> dobj->mSI_Value_;
197 s >> dobj->mName_ >> dobj->mShortName_;
198}
199
200/*!
201 \class PhysQty
202 \ingroup BaseTools
203 Class for representing physical quantities and constants. A physical quantity
204 has a unit and a value, and optionally a precision and/or a name. A number of
205 physical constants and standard quantities can be obtained as PhysQty objects
206 through static methods.
207
208 \sa SOPHYA::Units
209
210 \code
211 cout << " k_Boltzmann: " << PhysQty::k() << endl;
212 cout << " m_e: " << PhysQty::electron_mass() << endl;
213 // Conversion of a quantity to a different unit
214 PhysQty aj(Units::joule(), 25.);
215 PhysQty aev=aj.ConvertTo(Units::electronvolt().giga());
216 cout << " A : " << aj << " -> " << aev << endl;
217 \endcode
218*/
219
220//-------------------------------------------------------------------------
221//------------------------- Classe PhysQty --------------------------
222//-------------------------------------------------------------------------
223
224/* --Methode-- */
225//! Default constructor: zero valued dimensionless quantity
226PhysQty::PhysQty()
227 : unit_(), val_(0.), prec_(0.)
228{
229}
230
231/* --Methode-- */
232/*!
233 \brief standard constructor
234 The object is constructed through the specification of the unit \b u, the value \b val,
235 and optional relative precision \b prec (=delta val/val) and an optional name.
236*/
237PhysQty::PhysQty(Units const & u, r_8 val, r_8 prec, const char* nom)
238 : unit_(u), val_(val), prec_(prec), name_((nom==NULL)?"":nom)
239{
240}
241
242/* --Methode-- */
243//! Copy constructor
244PhysQty::PhysQty(PhysQty const & qty)
245 : unit_(qty.unit_), val_(qty.val_), prec_(qty.prec_), name_(qty.name_)
246{
247}
248
249/* --Methode-- */
250PhysQty::~PhysQty()
251{
252}
253
254/* --Methode-- */
255PhysQty& PhysQty::Set(PhysQty const& qty)
256{
257 unit_=qty.unit_;
258 val_=qty.val_;
259 prec_=qty.prec_;
260 name_=qty.name_;
261 return (*this);
262}
263
264/* --Methode-- */
265PhysQty PhysQty::ConvertTo(Units const & u) const
266{
267 if (! unit_.isSameDimension(u) )
268 throw ParmError("PhysQty::ConvertTo() - incompatible dimensions !");
269 return PhysQty(u, Value()*unit_.SIValue()/u.SIValue(), RelativePrecision(),Name().c_str());
270}
271
272/* --Methode-- */
273ostream& PhysQty::Print(ostream& os,int lev) const
274{
275 if (lev==0) {
276 os << val_ << ' ' << unit_.ShortName();
277 return os;
278 }
279 if (RelativePrecision()>0.)
280 os << name_ << "= " << val_ << " (+/- " << AbsolutePrecision() << ") " << unit_.Name();
281 else os << name_ << "= " << val_ << " " << unit_.Name();
282 if (lev > 1) { os << " Unit: "; unit_.Print(os, lev); }
283 return os;
284}
285
286//----------------------------------------------------------
287// Classe pour la gestion de persistance de PhysQty
288// ObjFileIO<PhysQty>
289//----------------------------------------------------------
290
291/* --Methode-- */
292DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
293void ObjFileIO<PhysQty>::WriteSelf(POutPersist& s) const
294{
295 if (dobj == NULL)
296 throw NullPtrError("ObjFileIO<PhysQty>::WriteSelf() dobj=NULL");
297 int_4 ver;
298 ver = 1;
299 s.Put(ver); // ecriture numero de version PPF
300 // ecriture de l'objet Units et des champs valeurs associes
301 s<< dobj->unit_ << dobj->val_ << dobj->prec_ << dobj->name_;
302}
303
304/* --Methode-- */
305DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
306void ObjFileIO<PhysQty>::ReadSelf(PInPersist& s)
307{
308 int_4 ver;
309 s.Get(ver); // Lecture numero de version PPF
310 if (dobj == NULL) dobj = new PhysQty();
311 // ecriture de l'objet Units et des champs valeurs associes
312 s >> dobj->unit_ >> dobj->val_ >> dobj->prec_ >> dobj->name_;
313}
314
315#ifdef __CXX_PRAGMA_TEMPLATES__
316#pragma define_template ObjFileIO<Units>
317#pragma define_template ObjFileIO<PhysQty>
318#endif
319
320#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
321template class ObjFileIO<Units>;
322template class ObjFileIO<PhysQty>;
323#endif
324
325} // FIN namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.