00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include <iomanip>
00050 #include <sstream>
00051
00052 #include "G4UnitsTable.hh"
00053 #include "G4SystemOfUnits.hh"
00054
00055 G4UnitsTable G4UnitDefinition::theUnitsTable;
00056
00057
00058
00059 G4UnitDefinition::G4UnitDefinition(const G4String& name,
00060 const G4String& symbol,
00061 const G4String& category, G4double value)
00062 : Name(name),SymbolName(symbol),Value(value)
00063 {
00064
00065
00066 size_t nbCat = theUnitsTable.size();
00067 size_t i = 0;
00068 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
00069 if (i == nbCat)
00070 { theUnitsTable.push_back( new G4UnitsCategory(category)); }
00071 CategoryIndex = i;
00072
00073
00074
00075 (theUnitsTable[CategoryIndex]->GetUnitsList()).push_back(this);
00076
00077
00078
00079 theUnitsTable[i]->UpdateNameMxLen((G4int)name.length());
00080 theUnitsTable[i]->UpdateSymbMxLen((G4int)symbol.length());
00081 }
00082
00083
00084
00085 G4UnitDefinition::~G4UnitDefinition()
00086 {
00087 }
00088
00089
00090
00091 G4UnitDefinition::G4UnitDefinition(const G4UnitDefinition& right)
00092 {
00093 *this = right;
00094 }
00095
00096
00097
00098 G4UnitDefinition& G4UnitDefinition::operator=(const G4UnitDefinition& right)
00099 {
00100 if (this != &right)
00101 {
00102 Name = right.Name;
00103 SymbolName = right.SymbolName;
00104 Value = right.Value;
00105 CategoryIndex = right.CategoryIndex;
00106 }
00107 return *this;
00108 }
00109
00110
00111
00112 G4int G4UnitDefinition::operator==(const G4UnitDefinition& right) const
00113 {
00114 return (this == (G4UnitDefinition *) &right);
00115 }
00116
00117
00118
00119 G4int G4UnitDefinition::operator!=(const G4UnitDefinition &right) const
00120 {
00121 return (this != (G4UnitDefinition *) &right);
00122 }
00123
00124
00125
00126 G4UnitsTable& G4UnitDefinition::GetUnitsTable()
00127 {
00128 if(theUnitsTable.size()==0) { BuildUnitsTable(); }
00129 return theUnitsTable;
00130 }
00131
00132
00133
00134 G4double G4UnitDefinition::GetValueOf(const G4String& str)
00135 {
00136 G4String name,symbol;
00137 for (size_t i=0;i<(GetUnitsTable()).size();i++)
00138 {
00139 G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList();
00140 for (size_t j=0;j<units.size();j++)
00141 {
00142 name=units[j]->GetName(); symbol=units[j]->GetSymbol();
00143 if(str==name||str==symbol)
00144 { return units[j]->GetValue(); }
00145 }
00146 }
00147 std::ostringstream message;
00148 message << "The unit '" << str << "' does not exist in the Units Table.";
00149 G4Exception("G4UnitDefinition::GetValueOf()", "InvalidUnit",
00150 JustWarning, message, "Returning Value = 0.");
00151 return 0.;
00152 }
00153
00154
00155
00156 G4String G4UnitDefinition::GetCategory(const G4String& str)
00157 {
00158 G4String name,symbol;
00159 for (size_t i=0;i<(GetUnitsTable()).size();i++)
00160 {
00161 G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList();
00162 for (size_t j=0;j<units.size();j++)
00163 {
00164 name=units[j]->GetName(); symbol=units[j]->GetSymbol();
00165 if(str==name||str==symbol)
00166 { return theUnitsTable[i]->GetName(); }
00167 }
00168 }
00169 std::ostringstream message;
00170 message << "The unit '" << str << "' does not exist in the Units Table.";
00171 G4Exception("G4UnitDefinition::GetCategory()", "InvalidUnit",
00172 JustWarning, message, "Returning Value = 0.");
00173 name = "None";
00174 return name;
00175 }
00176
00177
00178
00179 void G4UnitDefinition::PrintDefinition()
00180 {
00181 G4int nameL = theUnitsTable[CategoryIndex]->GetNameMxLen();
00182 G4int symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen();
00183 G4cout << std::setw(nameL) << Name << " ("
00184 << std::setw(symbL) << SymbolName << ") = " << Value << G4endl;
00185 }
00186
00187
00188
00189 void G4UnitDefinition::BuildUnitsTable()
00190 {
00191
00192 new G4UnitDefinition( "parsec","pc" ,"Length",parsec);
00193 new G4UnitDefinition( "kilometer","km" ,"Length",kilometer);
00194 new G4UnitDefinition( "meter","m" ,"Length",meter);
00195 new G4UnitDefinition("centimeter","cm" ,"Length",centimeter);
00196 new G4UnitDefinition("millimeter","mm" ,"Length",millimeter);
00197 new G4UnitDefinition("micrometer","um" ,"Length",micrometer);
00198 new G4UnitDefinition( "nanometer","nm" ,"Length",nanometer);
00199 new G4UnitDefinition( "angstrom","Ang" ,"Length",angstrom);
00200 new G4UnitDefinition( "fermi","fm" ,"Length",fermi);
00201
00202
00203 new G4UnitDefinition( "kilometer2","km2" ,"Surface",kilometer2);
00204 new G4UnitDefinition( "meter2","m2" ,"Surface",meter2);
00205 new G4UnitDefinition("centimeter2","cm2" ,"Surface",centimeter2);
00206 new G4UnitDefinition("millimeter2","mm2" ,"Surface",millimeter2);
00207 new G4UnitDefinition( "barn","barn" ,"Surface",barn);
00208 new G4UnitDefinition( "millibarn","mbarn" ,"Surface",millibarn);
00209 new G4UnitDefinition( "microbarn","mubarn" ,"Surface",microbarn);
00210 new G4UnitDefinition( "nanobarn","nbarn" ,"Surface",nanobarn);
00211 new G4UnitDefinition( "picobarn","pbarn" ,"Surface",picobarn);
00212
00213
00214 new G4UnitDefinition( "kilometer3","km3" ,"Volume",kilometer3);
00215 new G4UnitDefinition( "meter3","m3" ,"Volume",meter3);
00216 new G4UnitDefinition("centimeter3","cm3" ,"Volume",centimeter3);
00217 new G4UnitDefinition("millimeter3","mm3" ,"Volume",millimeter3);
00218
00219
00220 new G4UnitDefinition( "radian","rad" ,"Angle",radian);
00221 new G4UnitDefinition("milliradian","mrad" ,"Angle",milliradian);
00222 new G4UnitDefinition( "degree","deg" ,"Angle",degree);
00223
00224
00225 new G4UnitDefinition( "steradian","sr" ,"Solid angle",steradian);
00226 new G4UnitDefinition("millisteradian","msr" ,"Solid angle",steradian*0.001);
00227
00228
00229 new G4UnitDefinition( "second","s" ,"Time",second);
00230 new G4UnitDefinition("millisecond","ms" ,"Time",millisecond);
00231 new G4UnitDefinition("microsecond","mus" ,"Time",microsecond);
00232 new G4UnitDefinition( "nanosecond","ns" ,"Time",nanosecond);
00233 new G4UnitDefinition( "picosecond","ps" ,"Time",picosecond);
00234
00235
00236 new G4UnitDefinition( "hertz","Hz" ,"Frequency",hertz);
00237 new G4UnitDefinition("kilohertz","kHz" ,"Frequency",kilohertz);
00238 new G4UnitDefinition("megahertz","MHz" ,"Frequency",megahertz);
00239
00240
00241 new G4UnitDefinition( "eplus","e+" ,"Electric charge",eplus);
00242 new G4UnitDefinition("coulomb","C" ,"Electric charge",coulomb);
00243
00244
00245 new G4UnitDefinition( "electronvolt","eV" ,"Energy",electronvolt);
00246 new G4UnitDefinition("kiloelectronvolt","keV","Energy",kiloelectronvolt);
00247 new G4UnitDefinition("megaelectronvolt","MeV","Energy",megaelectronvolt);
00248 new G4UnitDefinition("gigaelectronvolt","GeV","Energy",gigaelectronvolt);
00249 new G4UnitDefinition("teraelectronvolt","TeV","Energy",teraelectronvolt);
00250 new G4UnitDefinition("petaelectronvolt","PeV","Energy",petaelectronvolt);
00251 new G4UnitDefinition( "joule","J" ,"Energy",joule);
00252
00253
00254 new G4UnitDefinition( "GeV/cm", "GeV/cm","Energy/Length", GeV/cm);
00255 new G4UnitDefinition( "MeV/cm", "MeV/cm","Energy/Length", MeV/cm);
00256 new G4UnitDefinition( "keV/cm", "keV/cm","Energy/Length", keV/cm);
00257 new G4UnitDefinition( "eV/cm", "eV/cm","Energy/Length", eV/cm);
00258
00259
00260 new G4UnitDefinition("milligram","mg","Mass",milligram);
00261 new G4UnitDefinition( "gram","g" ,"Mass",gram);
00262 new G4UnitDefinition( "kilogram","kg","Mass",kilogram);
00263
00264
00265 new G4UnitDefinition( "g/cm3", "g/cm3","Volumic Mass", g/cm3);
00266 new G4UnitDefinition("mg/cm3","mg/cm3","Volumic Mass",mg/cm3);
00267 new G4UnitDefinition("kg/m3", "kg/m3", "Volumic Mass",kg/m3);
00268
00269
00270 new G4UnitDefinition( "g/cm2", "g/cm2","Mass/Surface", g/cm2);
00271 new G4UnitDefinition( "mg/cm2", "mg/cm2","Mass/Surface", mg/cm2);
00272 new G4UnitDefinition( "kg/cm2", "kg/cm2","Mass/Surface", kg/cm2);
00273
00274
00275 new G4UnitDefinition( "cm2/g", "cm2/g","Surface/Mass", cm2/g);
00276
00277
00278 new G4UnitDefinition( "eV*cm2/g", " eV*cm2/g","Energy*Surface/Mass", eV*cm2/g);
00279 new G4UnitDefinition("keV*cm2/g", "keV*cm2/g","Energy*Surface/Mass",keV*cm2/g);
00280 new G4UnitDefinition("MeV*cm2/g", "MeV*cm2/g","Energy*Surface/Mass",MeV*cm2/g);
00281 new G4UnitDefinition("GeV*cm2/g", "GeV*cm2/g","Energy*Surface/Mass",GeV*cm2/g);
00282
00283
00284 new G4UnitDefinition("watt","W","Power",watt);
00285
00286
00287 new G4UnitDefinition("newton","N","Force",newton);
00288
00289
00290 new G4UnitDefinition( "pascal","Pa" ,"Pressure",pascal);
00291 new G4UnitDefinition( "bar","bar","Pressure",bar);
00292 new G4UnitDefinition("atmosphere","atm","Pressure",atmosphere);
00293
00294
00295 new G4UnitDefinition( "ampere","A" ,"Electric current",ampere);
00296 new G4UnitDefinition("milliampere","mA" ,"Electric current",milliampere);
00297 new G4UnitDefinition("microampere","muA","Electric current",microampere);
00298 new G4UnitDefinition( "nanoampere","nA" ,"Electric current",nanoampere);
00299
00300
00301 new G4UnitDefinition( "volt","V" ,"Electric potential",volt);
00302 new G4UnitDefinition("kilovolt","kV","Electric potential",kilovolt);
00303 new G4UnitDefinition("megavolt","MV","Electric potential",megavolt);
00304
00305
00306 new G4UnitDefinition( "volt/m","V/m","Electric field",volt/m);
00307
00308
00309 new G4UnitDefinition("weber","Wb","Magnetic flux",weber);
00310
00311
00312 new G4UnitDefinition( "tesla","T" ,"Magnetic flux density",tesla);
00313 new G4UnitDefinition("kilogauss","kG","Magnetic flux density",kilogauss);
00314 new G4UnitDefinition( "gauss","G" ,"Magnetic flux density",gauss);
00315
00316
00317 new G4UnitDefinition("kelvin","K","Temperature",kelvin);
00318
00319
00320 new G4UnitDefinition("mole","mol","Amount of substance",mole);
00321
00322
00323 new G4UnitDefinition("becquerel","Bq","Activity",becquerel);
00324 new G4UnitDefinition( "curie","Ci","Activity",curie);
00325
00326
00327 new G4UnitDefinition("gray","Gy","Dose",gray);
00328 }
00329
00330
00331
00332 void G4UnitDefinition::PrintUnitsTable()
00333 {
00334 G4cout << "\n ----- The Table of Units ----- \n";
00335 for(size_t i=0;i<theUnitsTable.size();i++)
00336 {
00337 theUnitsTable[i]->PrintCategory();
00338 }
00339 }
00340
00341
00342
00343 void G4UnitDefinition::ClearUnitsTable()
00344 {
00345 for (size_t i=0;i<theUnitsTable.size();i++)
00346 {
00347 delete theUnitsTable[i];
00348 }
00349 theUnitsTable.clear();
00350 }
00351
00352
00353
00354 G4UnitsCategory::G4UnitsCategory(const G4String& name)
00355 : Name(name),UnitsList(),NameMxLen(0),SymbMxLen(0)
00356 {
00357 }
00358
00359
00360
00361 G4UnitsCategory::~G4UnitsCategory()
00362 {
00363 for(size_t i=0;i<UnitsList.size();i++)
00364 {
00365 delete UnitsList[i];
00366 }
00367 UnitsList.clear();
00368 }
00369
00370
00371
00372 G4UnitsCategory::G4UnitsCategory(const G4UnitsCategory& right)
00373 {
00374 *this = right;
00375 }
00376
00377
00378
00379 G4UnitsCategory& G4UnitsCategory::operator=(const G4UnitsCategory& right)
00380 {
00381 if (this != &right)
00382 {
00383 Name = right.Name;
00384 UnitsList = right.UnitsList;
00385 NameMxLen = right.NameMxLen;
00386 SymbMxLen = right.SymbMxLen;
00387 }
00388 return *this;
00389 }
00390
00391
00392
00393 G4int G4UnitsCategory::operator==(const G4UnitsCategory& right) const
00394 {
00395 return (this == (G4UnitsCategory *) &right);
00396 }
00397
00398
00399
00400 G4int G4UnitsCategory::operator!=(const G4UnitsCategory &right) const
00401 {
00402 return (this != (G4UnitsCategory *) &right);
00403 }
00404
00405
00406
00407 void G4UnitsCategory::PrintCategory()
00408 {
00409 G4cout << "\n category: " << Name << G4endl;
00410 for(size_t i=0;i<UnitsList.size();i++)
00411 { UnitsList[i]->PrintDefinition(); }
00412 }
00413
00414
00415
00416 G4BestUnit::G4BestUnit(G4double value, const G4String& category)
00417 : nbOfVals(1)
00418 {
00419
00420 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable();
00421 size_t nbCat = theUnitsTable.size();
00422 size_t i = 0;
00423 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
00424 if (i == nbCat)
00425 {
00426 G4cout << " G4BestUnit: the category " << category
00427 << " does not exist !!" << G4endl;
00428 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall",
00429 FatalException, "Missing unit category !") ;
00430 }
00431
00432 Value[0] = value;
00433 Value[1] = 0.;
00434 Value[2] = 0.;
00435 IndexOfCategory = i;
00436 }
00437
00438
00439
00440 G4BestUnit::G4BestUnit(const G4ThreeVector& value, const G4String& category)
00441 : nbOfVals(3)
00442 {
00443
00444 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable();
00445 size_t nbCat = theUnitsTable.size();
00446 size_t i = 0;
00447 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
00448 if (i == nbCat)
00449 {
00450 G4cerr << " G4BestUnit: the category " << category
00451 << " does not exist." << G4endl;
00452 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall",
00453 FatalException, "Missing unit category !") ;
00454 }
00455
00456 Value[0] = value.x();
00457 Value[1] = value.y();
00458 Value[2] = value.z();
00459 IndexOfCategory = i;
00460 }
00461
00462
00463 G4BestUnit::~G4BestUnit()
00464 {}
00465
00466
00467
00468 G4BestUnit::operator G4String () const
00469 {
00470 std::ostringstream oss;
00471 oss << *this;
00472 return oss.str();
00473 }
00474
00475
00476
00477 std::ostream& operator<<(std::ostream& flux, G4BestUnit a)
00478 {
00479 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable();
00480 G4UnitsContainer& List = theUnitsTable[a.IndexOfCategory]
00481 ->GetUnitsList();
00482 G4int len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
00483
00484 G4int ksup(-1), kinf(-1);
00485 G4double umax(0.), umin(DBL_MAX);
00486 G4double rsup(DBL_MAX), rinf(0.);
00487
00488
00489 G4double value = std::max(std::max(std::fabs(a.Value[0]),
00490 std::fabs(a.Value[1])),
00491 std::fabs(a.Value[2]));
00492
00493 for (size_t k=0; k<List.size(); k++)
00494 {
00495 G4double unit = List[k]->GetValue();
00496 if (!(value!=DBL_MAX))
00497 {if(unit>umax) {umax=unit; ksup=k;}}
00498 else if (value<=DBL_MIN)
00499 {if(unit<umin) {umin=unit; kinf=k;}}
00500 else
00501 {
00502 G4double ratio = value/unit;
00503 if ((ratio>=1.)&&(ratio<rsup)) {rsup=ratio; ksup=k;}
00504 if ((ratio< 1.)&&(ratio>rinf)) {rinf=ratio; kinf=k;}
00505 }
00506 }
00507
00508 G4int index=ksup;
00509 if(index==-1) { index=kinf; }
00510 if(index==-1) { index=0; }
00511
00512 for (G4int j=0; j<a.nbOfVals; j++)
00513 { flux << a.Value[j]/(List[index]->GetValue()) << " "; }
00514
00515 std::ios::fmtflags oldform = flux.flags();
00516
00517 flux.setf(std::ios::left,std::ios::adjustfield);
00518 flux << std::setw(len) << List[index]->GetSymbol();
00519 flux.flags(oldform);
00520
00521 return flux;
00522 }
00523
00524