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

Last change on this file since 1340 was 1340, checked in by garnier, 14 years ago

update ti head

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