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

Last change on this file since 4059 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: 7.6 KB
Line 
1#include "machdefs.h"
2#include "sunitpcst.h"
3
4namespace SOPHYA {
5
6/*!
7 \class Units
8 \ingroup BaseTools
9 Class for representing physical units
10
11 \code
12 // Create ....
13 \endcode
14*/
15
16//-------------------------------------------------------------------------
17//------------------------- Classe Units --------------------------
18//-------------------------------------------------------------------------
19
20/* --Methode-- */
21Units::Units()
22 : mLength_(0), mMass_(0), mTime_(0), mCurr_(0),
23 mTemp_(0), mSubst_(0), mLumInt_(0), mSI_Value_(0.)
24{
25}
26
27/* --Methode-- */
28Units::Units(const char* nom, const char* snom, int m, int kg, int s, r_8 val, int A, int K, int mol, int cd)
29 : mLength_(m), mMass_(kg), mTime_(s), mCurr_(A),
30 mTemp_(K), mSubst_(mol), mLumInt_(cd), mSI_Value_(val),
31 mName_(nom), mShortName_(snom)
32{
33}
34
35
36/* --Methode-- */
37Units::Units(Units const& un)
38{
39 Set(un);
40}
41
42/* --Methode-- */
43Units::~Units()
44{
45}
46
47/* --Methode-- */
48Units& Units::Set(Units const& un)
49{
50 mLength_ = un.mLength_;
51 mMass_=un.mMass_;
52 mTime_=un.mTime_;
53 mCurr_=un.mCurr_;
54 mTemp_=un.mTemp_;
55 mSubst_=un.mSubst_;
56 mLumInt_=un.mLumInt_;
57 mSI_Value_=un.mSI_Value_;
58 mName_=un.mName_;
59 mShortName_=un.mShortName_;
60 return (*this);
61}
62
63/* --Methode-- */
64bool Units::isSameDimension(Units const& un) const
65{
66 if ( (mLength_ == un.mLength_) && (mMass_==un.mMass_) &&
67 (mTime_==un.mTime_) && (mCurr_==un.mCurr_) &&
68 (mTemp_==un.mTemp_) && (mSubst_==un.mSubst_) && (mLumInt_==un.mLumInt_) ) return true;
69 else return false;
70}
71
72/* --Methode-- */
73ostream& Units::Print(ostream& os, int lev) const
74{
75 if (lev < 2) {
76 if (lev==0) os << mShortName_;
77 else os << mName_;
78 return os;
79 }
80 os << mName_ << " (" << mShortName_ << ") SI_Value=" << mSI_Value_ << " [ ";
81 if (!mLength_.isZero()) os << "L" << '^' << mLength_ << ' ';
82 if (!(mMass_.isZero())) os << "M" << '^' << mMass_ << ' ';
83 if (!(mTime_.isZero())) os << "T" << '^' << mTime_ << ' ';
84 if (!mCurr_.isZero()) os << "I" << '^' << mCurr_ << ' ';
85 if (!mTemp_.isZero()) os << "K" << '^' << mTemp_ << ' ';
86 if (!mSubst_.isZero()) os << "Mol" << '^' << mSubst_ << ' ';
87 if (!mLumInt_.isZero()) os << "Lum" << '^' << mLumInt_ << ' ';
88 cout << " ]" << endl;
89 return os;
90}
91
92/* --Methode-- */
93Units Units::power(QNumber q) const
94{
95 Units ru;
96 ru.mLength_=mLength_*q;
97 ru.mMass_=mMass_*q;
98 ru.mTime_=mTime_*q;
99 ru.mCurr_=mCurr_*q;
100 ru.mTemp_=mTemp_*q;
101 ru.mSubst_=mSubst_*q;
102 ru.mLumInt_=mLumInt_*q;
103 ru.mSI_Value_=pow(mSI_Value_,(double)q);
104 ru.mName_=mName_ + '^' + q.ToString();
105 ru.mShortName_=mShortName_ + '^' + q.ToString();
106 return ru;
107}
108
109/* --Methode-- */
110Units Units::Multiply(Units const& u1, Units const& u2)
111{
112 Units ru;
113 ru.mLength_=u1.mLength_+u2.mLength_;
114 ru.mMass_=u1.mMass_+u2.mMass_;
115 ru.mTime_=u1.mTime_+u2.mTime_;
116 ru.mCurr_=u1.mCurr_+u2.mCurr_;
117 ru.mTemp_=u1.mTemp_+u2.mTemp_;
118 ru.mSubst_=u1.mSubst_+u2.mSubst_;
119 ru.mLumInt_=u1.mLumInt_+u2.mLumInt_;
120 ru.mSI_Value_=u1.mSI_Value_*u2.mSI_Value_;
121 ru.mName_=u1.mName_ + '.' + u2.mName_;
122 ru.mShortName_=u1.mShortName_ + '.' + u2.mShortName_;
123 return ru;
124}
125
126/* --Methode-- */
127Units Units::Divide(Units const& u1, Units const& u2)
128{
129 Units ru;
130 ru.mLength_=u1.mLength_-u2.mLength_;
131 ru.mMass_=u1.mMass_-u2.mMass_;
132 ru.mTime_=u1.mTime_-u2.mTime_;
133 ru.mCurr_=u1.mCurr_-u2.mCurr_;
134 ru.mTemp_=u1.mTemp_-u2.mTemp_;
135 ru.mSubst_=u1.mSubst_-u2.mSubst_;
136 ru.mLumInt_=u1.mLumInt_-u2.mLumInt_;
137 ru.mSI_Value_=u1.mSI_Value_/u2.mSI_Value_;
138 ru.mName_=u1.mName_ + '/' + u2.mName_;
139 ru.mShortName_=u1.mShortName_ + '/' + u2.mShortName_;
140 return ru;
141}
142
143
144//----------------------------------------------------------
145// Classe pour la gestion de persistance de Units
146// ObjFileIO<Units>
147//----------------------------------------------------------
148
149/* --Methode-- */
150DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
151void ObjFileIO<Units>::WriteSelf(POutPersist& s) const
152{
153 if (dobj == NULL)
154 throw NullPtrError("ObjFileIO<Units>::WriteSelf() dobj=NULL");
155 int_4 ver;
156 ver = 1;
157 s.Put(ver); // ecriture numero de version PPF
158 s << dobj->mLength_ << dobj->mMass_ << dobj->mTime_ << dobj->mCurr_;
159 s << dobj->mTemp_ << dobj->mSubst_ << dobj->mLumInt_;
160 s << dobj->mSI_Value_;
161 s << dobj->mName_ << dobj->mShortName_;
162}
163
164/* --Methode-- */
165DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
166void ObjFileIO<Units>::ReadSelf(PInPersist& s)
167{
168 int_4 ver;
169 s.Get(ver); // Lecture numero de version PPF
170
171 if (dobj == NULL) dobj = new Units();
172 s >> dobj->mLength_ >> dobj->mMass_ >> dobj->mTime_ >> dobj->mCurr_;
173 s >> dobj->mTemp_ >> dobj->mSubst_ >> dobj->mLumInt_;
174 s >> dobj->mSI_Value_;
175 s >> dobj->mName_ >> dobj->mShortName_;
176}
177
178/*!
179 \class PhysQty
180 \ingroup BaseTools
181 Class for representing physical quantities and constants
182
183 \code
184 // Create ....
185 \endcode
186*/
187
188//-------------------------------------------------------------------------
189//------------------------- Classe PhysQty --------------------------
190//-------------------------------------------------------------------------
191
192/* --Methode-- */
193PhysQty::PhysQty()
194 : unit_(), val_(0.), prec_(0.)
195{
196}
197
198/* --Methode-- */
199PhysQty::PhysQty(Units const & u, r_8 val, r_8 prec, const char* nom)
200 : unit_(u), val_(val), prec_(prec), name_((nom==NULL)?"":nom)
201{
202}
203
204/* --Methode-- */
205PhysQty::PhysQty(PhysQty const & qty)
206 : unit_(qty.unit_), val_(qty.val_), prec_(qty.prec_), name_(qty.name_)
207{
208}
209
210/* --Methode-- */
211PhysQty::~PhysQty()
212{
213}
214
215/* --Methode-- */
216PhysQty& PhysQty::Set(PhysQty const& qty)
217{
218 unit_=qty.unit_;
219 val_=qty.val_;
220 prec_=qty.prec_;
221 name_=qty.name_;
222 return (*this);
223}
224
225/* --Methode-- */
226PhysQty PhysQty::ConvertTo(Units const & u) const
227{
228 if (! unit_.isSameDimension(u) )
229 throw ParmError("PhysQty::ConvertTo() - incompatible dimensions !");
230 return PhysQty(u, Value()*unit_.SIValue()/u.SIValue(), RelativePrecision(),Name().c_str());
231}
232
233/* --Methode-- */
234ostream& PhysQty::Print(ostream& os,int lev) const
235{
236 if (lev==0) {
237 os << val_ << ' ' << unit_.ShortName();
238 return os;
239 }
240 if (RelativePrecision()>0.)
241 os << name_ << "= " << val_ << " (+/- " << AbsolutePrecision() << ") " << unit_.Name();
242 else os << name_ << "= " << val_ << " " << unit_.Name();
243 if (lev > 1) { os << " Unit: "; unit_.Print(os, lev); }
244 return os;
245}
246
247//----------------------------------------------------------
248// Classe pour la gestion de persistance de PhysQty
249// ObjFileIO<PhysQty>
250//----------------------------------------------------------
251
252/* --Methode-- */
253DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
254void ObjFileIO<PhysQty>::WriteSelf(POutPersist& s) const
255{
256 if (dobj == NULL)
257 throw NullPtrError("ObjFileIO<PhysQty>::WriteSelf() dobj=NULL");
258 int_4 ver;
259 ver = 1;
260 s.Put(ver); // ecriture numero de version PPF
261 // ecriture de l'objet Units et des champs valeurs associes
262 s<< dobj->unit_ << dobj->val_ << dobj->prec_ << dobj->name_;
263}
264
265/* --Methode-- */
266DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
267void ObjFileIO<PhysQty>::ReadSelf(PInPersist& s)
268{
269 int_4 ver;
270 s.Get(ver); // Lecture numero de version PPF
271 if (dobj == NULL) dobj = new PhysQty();
272 // ecriture de l'objet Units et des champs valeurs associes
273 s >> dobj->unit_ >> dobj->val_ >> dobj->prec_ >> dobj->name_;
274}
275
276#ifdef __CXX_PRAGMA_TEMPLATES__
277#pragma define_template ObjFileIO<Units>
278#pragma define_template ObjFileIO<PhysQty>
279#endif
280
281#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
282template class ObjFileIO<Units>;
283template class ObjFileIO<PhysQty>;
284#endif
285
286} // FIN namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.