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

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

file release beta

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