source: trunk/source/global/management/src/G4UnitsTable.cc@ 833

Last change on this file since 833 was 833, checked in by garnier, 17 years ago

import all except CVS

File size: 18.1 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: G4UnitsTable.cc,v 1.36 2006/11/30 10:37:57 gcosmo Exp $
28// GEANT4 tag $Name: $
29//
30//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
31//
32// 17-05-98: first version, M.Maire
33// 05-08-98: angstrom,microbarn,picobarn,petaelectronvolt, M.Maire
34// 13-10-98: units and symbols printed in fixed length, M.Maire
35// 01-03-01: parsec, M.Maire
36// 06-03-01: migration to STL vectors, G.Cosmo
37// 06-05-02: BestUnit operator<< flux instead of G4cout (mma)
38// 12-08-05: cm2/g ("Surface/Mass") (mma)
39// 30-06-05: um for micrometer (mma)
40// 07-02-06: GeV/cm MeV/cm keV/cm eV/cm ("Energy/Length") (mma)
41// 15-02-06: g/cm2 ("Mass/Surface")
42// MeV*cm2/g ..etc.. ("Energy*Surface/Mass")
43// 18-08-06: remove symbol mum (mma)
44//
45//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
47
48
49#include "G4UnitsTable.hh"
50
51#include <iomanip>
52#include <sstream>
53
54G4UnitsTable G4UnitDefinition::theUnitsTable;
55
56//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57
58G4UnitDefinition::G4UnitDefinition(const G4String& name,
59 const G4String& symbol,
60 const G4String& category, G4double value)
61 : Name(name),SymbolName(symbol),Value(value)
62{
63 //
64 //does the Category objet already exist ?
65 size_t nbCat = theUnitsTable.size();
66 size_t i = 0;
67 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
68 if (i == nbCat)
69 { theUnitsTable.push_back( new G4UnitsCategory(category)); }
70 CategoryIndex = i;
71 //
72 //insert this Unit in the Unitstable
73 (theUnitsTable[CategoryIndex]->GetUnitsList()).push_back(this);
74
75 //update string max length for name and symbol
76 theUnitsTable[i]->UpdateNameMxLen((G4int)name.length());
77 theUnitsTable[i]->UpdateSymbMxLen((G4int)symbol.length());
78}
79
80//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
81
82G4UnitDefinition::~G4UnitDefinition()
83{
84}
85
86//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
87
88G4UnitDefinition::G4UnitDefinition(const G4UnitDefinition& right)
89{
90 *this = right;
91}
92
93//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
94
95G4UnitDefinition& G4UnitDefinition::operator=(const G4UnitDefinition& right)
96{
97 if (this != &right)
98 {
99 Name = right.Name;
100 SymbolName = right.SymbolName;
101 Value = right.Value;
102 CategoryIndex = right.CategoryIndex;
103 }
104 return *this;
105}
106
107//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
108
109G4int G4UnitDefinition::operator==(const G4UnitDefinition& right) const
110{
111 return (this == (G4UnitDefinition *) &right);
112}
113
114//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
115
116G4int G4UnitDefinition::operator!=(const G4UnitDefinition &right) const
117{
118 return (this != (G4UnitDefinition *) &right);
119}
120
121//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
122
123G4UnitsTable& G4UnitDefinition::GetUnitsTable()
124{
125 return theUnitsTable;
126}
127
128//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
129
130G4double G4UnitDefinition::GetValueOf(const G4String& str)
131{
132 if(theUnitsTable.size()==0) { BuildUnitsTable(); }
133 G4String name,symbol;
134 for (size_t i=0;i<theUnitsTable.size();i++)
135 {
136 G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList();
137 for (size_t j=0;j<units.size();j++)
138 {
139 name=units[j]->GetName(); symbol=units[j]->GetSymbol();
140 if(str==name||str==symbol)
141 { return units[j]->GetValue(); }
142 }
143 }
144 G4cout << "Warning from G4UnitDefinition::GetValueOf(" << str << ")."
145 << " The unit " << str << " does not exist in UnitsTable."
146 << " Return Value = 0." << G4endl;
147 return 0.;
148}
149
150//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
151
152G4String G4UnitDefinition::GetCategory(const G4String& str)
153{
154 if(theUnitsTable.size()==0) { BuildUnitsTable(); }
155 G4String name,symbol;
156 for (size_t i=0;i<theUnitsTable.size();i++)
157 {
158 G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList();
159 for (size_t j=0;j<units.size();j++)
160 {
161 name=units[j]->GetName(); symbol=units[j]->GetSymbol();
162 if(str==name||str==symbol)
163 { return theUnitsTable[i]->GetName(); }
164 }
165 }
166 G4cout << "Warning from G4UnitDefinition::GetCategory(" << str << ")."
167 << " The unit " << str << " does not exist in UnitsTable."
168 << " Return category = None" << G4endl;
169 name = "None";
170 return name;
171}
172
173//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
174
175void G4UnitDefinition::PrintDefinition()
176{
177 G4int nameL = theUnitsTable[CategoryIndex]->GetNameMxLen();
178 G4int symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen();
179 G4cout << std::setw(nameL) << Name << " ("
180 << std::setw(symbL) << SymbolName << ") = " << Value << G4endl;
181}
182
183//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
184
185void G4UnitDefinition::BuildUnitsTable()
186{
187 //Length
188 new G4UnitDefinition( "parsec","pc" ,"Length",parsec);
189 new G4UnitDefinition( "kilometer","km" ,"Length",kilometer);
190 new G4UnitDefinition( "meter","m" ,"Length",meter);
191 new G4UnitDefinition("centimeter","cm" ,"Length",centimeter);
192 new G4UnitDefinition("millimeter","mm" ,"Length",millimeter);
193 new G4UnitDefinition("micrometer","um" ,"Length",micrometer);
194 new G4UnitDefinition( "nanometer","nm" ,"Length",nanometer);
195 new G4UnitDefinition( "angstrom","Ang" ,"Length",angstrom);
196 new G4UnitDefinition( "fermi","fm" ,"Length",fermi);
197
198 //Surface
199 new G4UnitDefinition( "kilometer2","km2" ,"Surface",kilometer2);
200 new G4UnitDefinition( "meter2","m2" ,"Surface",meter2);
201 new G4UnitDefinition("centimeter2","cm2" ,"Surface",centimeter2);
202 new G4UnitDefinition("millimeter2","mm2" ,"Surface",millimeter2);
203 new G4UnitDefinition( "barn","barn" ,"Surface",barn);
204 new G4UnitDefinition( "millibarn","mbarn" ,"Surface",millibarn);
205 new G4UnitDefinition( "microbarn","mubarn" ,"Surface",microbarn);
206 new G4UnitDefinition( "nanobarn","nbarn" ,"Surface",nanobarn);
207 new G4UnitDefinition( "picobarn","pbarn" ,"Surface",picobarn);
208
209 //Volume
210 new G4UnitDefinition( "kilometer3","km3" ,"Volume",kilometer3);
211 new G4UnitDefinition( "meter3","m3" ,"Volume",meter3);
212 new G4UnitDefinition("centimeter3","cm3" ,"Volume",centimeter3);
213 new G4UnitDefinition("millimeter3","mm3" ,"Volume",millimeter3);
214
215 //Angle
216 new G4UnitDefinition( "radian","rad" ,"Angle",radian);
217 new G4UnitDefinition("milliradian","mrad" ,"Angle",milliradian);
218 new G4UnitDefinition( "steradian","sr" ,"Angle",steradian);
219 new G4UnitDefinition( "degree","deg" ,"Angle",degree);
220
221 //Time
222 new G4UnitDefinition( "second","s" ,"Time",second);
223 new G4UnitDefinition("millisecond","ms" ,"Time",millisecond);
224 new G4UnitDefinition("microsecond","mus" ,"Time",microsecond);
225 new G4UnitDefinition( "nanosecond","ns" ,"Time",nanosecond);
226 new G4UnitDefinition( "picosecond","ps" ,"Time",picosecond);
227
228 //Frequency
229 new G4UnitDefinition( "hertz","Hz" ,"Frequency",hertz);
230 new G4UnitDefinition("kilohertz","kHz" ,"Frequency",kilohertz);
231 new G4UnitDefinition("megahertz","MHz" ,"Frequency",megahertz);
232
233 //Electric charge
234 new G4UnitDefinition( "eplus","e+" ,"Electric charge",eplus);
235 new G4UnitDefinition("coulomb","C" ,"Electric charge",coulomb);
236
237 //Energy
238 new G4UnitDefinition( "electronvolt","eV" ,"Energy",electronvolt);
239 new G4UnitDefinition("kiloelectronvolt","keV","Energy",kiloelectronvolt);
240 new G4UnitDefinition("megaelectronvolt","MeV","Energy",megaelectronvolt);
241 new G4UnitDefinition("gigaelectronvolt","GeV","Energy",gigaelectronvolt);
242 new G4UnitDefinition("teraelectronvolt","TeV","Energy",teraelectronvolt);
243 new G4UnitDefinition("petaelectronvolt","PeV","Energy",petaelectronvolt);
244 new G4UnitDefinition( "joule","J" ,"Energy",joule);
245
246 // Energy/Length
247 new G4UnitDefinition( "GeV/cm", "GeV/cm","Energy/Length", GeV/cm);
248 new G4UnitDefinition( "MeV/cm", "MeV/cm","Energy/Length", MeV/cm);
249 new G4UnitDefinition( "keV/cm", "keV/cm","Energy/Length", keV/cm);
250 new G4UnitDefinition( "eV/cm", "eV/cm","Energy/Length", eV/cm);
251
252 //Mass
253 new G4UnitDefinition("milligram","mg","Mass",milligram);
254 new G4UnitDefinition( "gram","g" ,"Mass",gram);
255 new G4UnitDefinition( "kilogram","kg","Mass",kilogram);
256
257 //Volumic Mass
258 new G4UnitDefinition( "g/cm3", "g/cm3","Volumic Mass", g/cm3);
259 new G4UnitDefinition("mg/cm3","mg/cm3","Volumic Mass",mg/cm3);
260 new G4UnitDefinition("kg/m3", "kg/m3", "Volumic Mass",kg/m3);
261
262 // Mass/Surface
263 new G4UnitDefinition( "g/cm2", "g/cm2","Mass/Surface", g/cm2);
264 new G4UnitDefinition( "mg/cm2", "mg/cm2","Mass/Surface", mg/cm2);
265 new G4UnitDefinition( "kg/cm2", "kg/cm2","Mass/Surface", kg/cm2);
266
267 // Surface/Mass
268 new G4UnitDefinition( "cm2/g", "cm2/g","Surface/Mass", cm2/g);
269
270 // Energy.Surface/Mass
271 new G4UnitDefinition( "eV*cm2/g", " eV*cm2/g","Energy*Surface/Mass", eV*cm2/g);
272 new G4UnitDefinition("keV*cm2/g", "keV*cm2/g","Energy*Surface/Mass",keV*cm2/g);
273 new G4UnitDefinition("MeV*cm2/g", "MeV*cm2/g","Energy*Surface/Mass",MeV*cm2/g);
274 new G4UnitDefinition("GeV*cm2/g", "GeV*cm2/g","Energy*Surface/Mass",GeV*cm2/g);
275
276 //Power
277 new G4UnitDefinition("watt","W","Power",watt);
278
279 //Force
280 new G4UnitDefinition("newton","N","Force",newton);
281
282 //Pressure
283 new G4UnitDefinition( "pascal","Pa" ,"Pressure",pascal);
284 new G4UnitDefinition( "bar","bar","Pressure",bar);
285 new G4UnitDefinition("atmosphere","atm","Pressure",atmosphere);
286
287 //Electric current
288 new G4UnitDefinition( "ampere","A" ,"Electric current",ampere);
289 new G4UnitDefinition("milliampere","mA" ,"Electric current",milliampere);
290 new G4UnitDefinition("microampere","muA","Electric current",microampere);
291 new G4UnitDefinition( "nanoampere","nA" ,"Electric current",nanoampere);
292
293 //Electric potential
294 new G4UnitDefinition( "volt","V" ,"Electric potential",volt);
295 new G4UnitDefinition("kilovolt","kV","Electric potential",kilovolt);
296 new G4UnitDefinition("megavolt","MV","Electric potential",megavolt);
297
298 //Magnetic flux
299 new G4UnitDefinition("weber","Wb","Magnetic flux",weber);
300
301 //Magnetic flux density
302 new G4UnitDefinition( "tesla","T" ,"Magnetic flux density",tesla);
303 new G4UnitDefinition("kilogauss","kG","Magnetic flux density",kilogauss);
304 new G4UnitDefinition( "gauss","G" ,"Magnetic flux density",gauss);
305
306 //Temperature
307 new G4UnitDefinition("kelvin","K","Temperature",kelvin);
308
309 //Amount of substance
310 new G4UnitDefinition("mole","mol","Amount of substance",mole);
311
312 //Activity
313 new G4UnitDefinition("becquerel","Bq","Activity",becquerel);
314 new G4UnitDefinition( "curie","Ci","Activity",curie);
315
316 //Dose
317 new G4UnitDefinition("gray","Gy","Dose",gray);
318}
319
320//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
321
322void G4UnitDefinition::PrintUnitsTable()
323{
324 G4cout << "\n ----- The Table of Units ----- \n";
325 for(size_t i=0;i<theUnitsTable.size();i++)
326 {
327 theUnitsTable[i]->PrintCategory();
328 }
329}
330
331//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
332
333void G4UnitDefinition::ClearUnitsTable()
334{
335 for (size_t i=0;i<theUnitsTable.size();i++)
336 {
337 delete theUnitsTable[i];
338 }
339 theUnitsTable.clear();
340}
341
342//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
343
344G4UnitsCategory::G4UnitsCategory(const G4String& name)
345 : Name(name),UnitsList(),NameMxLen(0),SymbMxLen(0)
346{
347}
348
349//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
350
351G4UnitsCategory::~G4UnitsCategory()
352{
353 for(size_t i=0;i<UnitsList.size();i++)
354 {
355 delete UnitsList[i];
356 }
357 UnitsList.clear();
358}
359
360//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
361
362G4UnitsCategory::G4UnitsCategory(const G4UnitsCategory& right)
363{
364 *this = right;
365}
366
367//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
368
369G4UnitsCategory& G4UnitsCategory::operator=(const G4UnitsCategory& right)
370{
371 if (this != &right)
372 {
373 Name = right.Name;
374 UnitsList = right.UnitsList;
375 NameMxLen = right.NameMxLen;
376 SymbMxLen = right.SymbMxLen;
377 }
378 return *this;
379}
380
381//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
382
383G4int G4UnitsCategory::operator==(const G4UnitsCategory& right) const
384{
385 return (this == (G4UnitsCategory *) &right);
386}
387
388//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
389
390G4int G4UnitsCategory::operator!=(const G4UnitsCategory &right) const
391{
392 return (this != (G4UnitsCategory *) &right);
393}
394
395//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
396
397void G4UnitsCategory::PrintCategory()
398{
399 G4cout << "\n category: " << Name << G4endl;
400 for(size_t i=0;i<UnitsList.size();i++)
401 { UnitsList[i]->PrintDefinition(); }
402}
403
404//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
405
406G4BestUnit::G4BestUnit(G4double value, const G4String& category)
407 : nbOfVals(1)
408{
409 // find the category
410 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable();
411 size_t nbCat = theUnitsTable.size();
412 size_t i = 0;
413 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
414 if (i == nbCat)
415 {
416 G4cout << " G4BestUnit: the category " << category
417 << " does not exist !!" << G4endl;
418 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall",
419 FatalException, "Missing unit category !") ;
420 }
421 //
422 Value[0] = value;
423 Value[1] = 0.;
424 Value[2] = 0.;
425 IndexOfCategory = i;
426}
427
428//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
429
430G4BestUnit::G4BestUnit(const G4ThreeVector& value, const G4String& category)
431 : nbOfVals(3)
432{
433 // find the category
434 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable();
435 size_t nbCat = theUnitsTable.size();
436 size_t i = 0;
437 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
438 if (i == nbCat)
439 {
440 G4cerr << " G4BestUnit: the category " << category
441 << " does not exist." << G4endl;
442 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall",
443 FatalException, "Missing unit category !") ;
444 }
445 //
446 Value[0] = value.x();
447 Value[1] = value.y();
448 Value[2] = value.z();
449 IndexOfCategory = i;
450}
451//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
452
453G4BestUnit::~G4BestUnit()
454{}
455
456//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
457
458G4BestUnit::operator G4String () const
459{
460 std::ostringstream oss;
461 oss << *this;
462 return oss.str();
463}
464
465//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
466
467std::ostream& operator<<(std::ostream& flux, G4BestUnit a)
468{
469 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable();
470 G4UnitsContainer& List = theUnitsTable[a.IndexOfCategory]
471 ->GetUnitsList();
472 G4int len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
473
474 G4int ksup(-1), kinf(-1);
475 G4double umax(0.), umin(DBL_MAX);
476 G4double rsup(DBL_MAX), rinf(0.);
477
478 //for a ThreeVector, choose the best unit for the biggest value
479 G4double value = std::max(std::max(std::fabs(a.Value[0]),
480 std::fabs(a.Value[1])),
481 std::fabs(a.Value[2]));
482
483 for (size_t k=0; k<List.size(); k++)
484 {
485 G4double unit = List[k]->GetValue();
486 if (!(value!=DBL_MAX))
487 {if(unit>umax) {umax=unit; ksup=k;}}
488 else if (value<=DBL_MIN)
489 {if(unit<umin) {umin=unit; kinf=k;}}
490 else
491 {
492 G4double ratio = value/unit;
493 if ((ratio>=1.)&&(ratio<rsup)) {rsup=ratio; ksup=k;}
494 if ((ratio< 1.)&&(ratio>rinf)) {rinf=ratio; kinf=k;}
495 }
496 }
497
498 G4int index=ksup;
499 if(index==-1) { index=kinf; }
500 if(index==-1) { index=0; }
501
502 for (G4int j=0; j<a.nbOfVals; j++)
503 { flux << a.Value[j]/(List[index]->GetValue()) << " "; }
504
505 std::ios::fmtflags oldform = flux.flags();
506
507 flux.setf(std::ios::left,std::ios::adjustfield);
508 flux << std::setw(len) << List[index]->GetSymbol();
509 flux.flags(oldform);
510
511 return flux;
512}
513
514//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Note: See TracBrowser for help on using the repository browser.