Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4VelocityTable class implementation << 27 // 26 // 28 // Author: Hisaya Kurashige, 17 August 2011 << 27 // $Id: G4VelocityTable.cc 69005 2013-04-15 09:26:47Z gcosmo $ 29 // ------------------------------------------- << 28 // >> 29 // >> 30 //--------------------------------------------------------------- >> 31 // >> 32 // G4VelocityTable.cc >> 33 // >> 34 // class description: >> 35 // This class keeps a table of velocity as a function of >> 36 // the ratio kinetic erngy and mass >> 37 // >> 38 //--------------------------------------------------------------- >> 39 // created 17.Aug. 2011 H.Kurashige >> 40 // 30 41 31 #include "G4VelocityTable.hh" 42 #include "G4VelocityTable.hh" 32 #include "G4PhysicalConstants.hh" 43 #include "G4PhysicalConstants.hh" 33 #include "G4StateManager.hh" 44 #include "G4StateManager.hh" 34 #include "G4ApplicationState.hh" 45 #include "G4ApplicationState.hh" 35 #include "G4Log.hh" << 36 #include "G4Exp.hh" << 37 46 38 #include "G4ios.hh" << 47 #include "G4ios.hh" 39 48 40 G4ThreadLocal G4VelocityTable* G4VelocityTable << 49 G4ThreadLocal G4VelocityTable* G4VelocityTable::theInstance = 0; 41 50 42 // ------------------------------------------- << 51 //////////////// 43 G4VelocityTable::G4VelocityTable() 52 G4VelocityTable::G4VelocityTable() >> 53 //////////////// >> 54 : edgeMin(0.), edgeMax(0.), numberOfNodes(0), >> 55 dBin(0.), baseBin(0.), >> 56 lastEnergy(-DBL_MAX), lastValue(0.), lastBin(0), >> 57 maxT( 1000.0 ), minT( 0.0001 ), NbinT( 500 ) 44 { 58 { 45 PrepareVelocityTable(); 59 PrepareVelocityTable(); 46 } << 60 } 47 61 48 // ------------------------------------------- << 62 ///////////////// 49 G4VelocityTable::~G4VelocityTable() 63 G4VelocityTable::~G4VelocityTable() >> 64 ///////////////// 50 { 65 { 51 dataVector.clear(); 66 dataVector.clear(); 52 binVector.clear(); 67 binVector.clear(); 53 } 68 } 54 69 55 // ------------------------------------------- << 70 >> 71 /////////////////// 56 void G4VelocityTable::PrepareVelocityTable() 72 void G4VelocityTable::PrepareVelocityTable() >> 73 /////////////////// 57 { 74 { 58 // const G4double g4log10 = std::log(10.); << 59 << 60 dataVector.clear(); 75 dataVector.clear(); 61 binVector.clear(); 76 binVector.clear(); 62 dBin = G4Log(maxT / minT) / NbinT; << 77 63 baseBin = G4Log(minT) / dBin; << 78 dBin = std::log10(maxT/minT)/NbinT; >> 79 baseBin = std::log10(minT)/dBin; 64 80 65 numberOfNodes = NbinT + 1; 81 numberOfNodes = NbinT + 1; 66 dataVector.reserve(numberOfNodes); 82 dataVector.reserve(numberOfNodes); 67 binVector.reserve(numberOfNodes); 83 binVector.reserve(numberOfNodes); 68 84 >> 85 const G4double g4log10 = std::log(10.); >> 86 69 binVector.push_back(minT); 87 binVector.push_back(minT); 70 dataVector.push_back(0.0); 88 dataVector.push_back(0.0); 71 89 72 for(std::size_t i = 1; i < numberOfNodes - 1 << 90 for (size_t i=1; i<numberOfNodes-1; i++){ 73 { << 91 binVector.push_back(std::exp(g4log10*(baseBin+i)*dBin)); 74 binVector.push_back(G4Exp((baseBin + i) * << 75 dataVector.push_back(0.0); 92 dataVector.push_back(0.0); 76 } 93 } 77 binVector.push_back(maxT); 94 binVector.push_back(maxT); 78 dataVector.push_back(0.0); 95 dataVector.push_back(0.0); 79 << 96 80 edgeMin = binVector[0]; 97 edgeMin = binVector[0]; 81 edgeMax = binVector[numberOfNodes - 1]; << 98 edgeMax = binVector[numberOfNodes-1]; 82 99 83 for(G4int i = 0; i <= NbinT; ++i) << 100 for (G4int i=0; i<=NbinT; i++){ 84 { << 101 G4double T = binVector[i]; 85 G4double T = binVector[i]; << 102 dataVector[i]= c_light*std::sqrt(T*(T+2.))/(T+1.0); 86 dataVector[i] = c_light * std::sqrt(T * (T << 87 } 103 } 88 104 89 return; 105 return; 90 } << 106 } 91 107 92 // ------------------------------------------- << 108 size_t G4VelocityTable::FindBinLocation(G4double theEnergy) const 93 std::size_t G4VelocityTable::FindBinLocation(G << 94 { 109 { 95 // For G4PhysicsLogVector, FindBinLocation i 110 // For G4PhysicsLogVector, FindBinLocation is implemented using 96 // a simple arithmetic calculation. 111 // a simple arithmetic calculation. 97 // 112 // 98 // Because this is a virtual function, it is 113 // Because this is a virtual function, it is accessed through a 99 // pointer to the G4PhysicsVector object for << 114 // pointer to the G4PhyiscsVector object for most usages. In this 100 // case, 'inline' will not be invoked. Howev << 115 // case, 'inline' will not be invoked. However, there is a possibility 101 // that the user access to the G4PhysicsLogV << 116 // that the user access to the G4PhysicsLogVector object directly and 102 // not through pointers or references. In th 117 // not through pointers or references. In this case, the 'inline' will 103 // be invoked. (See R.B.Murray, "C++ Strateg 118 // be invoked. (See R.B.Murray, "C++ Strategies and Tactics", Chap.6.6) 104 119 105 // const G4double g4log10 = G4Log(10.); << 120 return size_t( std::log10(theEnergy)/dBin - baseBin ); 106 return std::size_t(G4Log(theEnergy) / dBin - << 107 } 121 } 108 122 109 // ------------------------------------------- << 123 G4double G4VelocityTable::Value(G4double theEnergy) 110 G4double G4VelocityTable::Value(G4double theEn << 111 { 124 { 112 // Use cache for speed up - check if the val << 125 // Use cache for speed up - check if the value 'theEnergy' is same as the 113 // last call. If it is same, then use the la 126 // last call. If it is same, then use the last bin location. Also the 114 // value 'theEnergy' lies between the last e << 127 // value 'theEnergy' lies between the last energy and low edge of of the 115 // bin of last call, then the last bin locat << 128 // bin of last call, then the last bin location is used. 116 129 117 if(theEnergy == lastEnergy) << 130 if( theEnergy == lastEnergy ) { 118 { << 131 119 } << 132 } else if( theEnergy < lastEnergy 120 else if(theEnergy < lastEnergy && theEnergy << 133 && theEnergy >= binVector[lastBin]) { 121 { << 134 lastEnergy = theEnergy; 122 lastEnergy = theEnergy; << 135 lastValue = Interpolation(); 123 lastValue = Interpolation(); << 136 124 } << 137 } else if( theEnergy <= edgeMin ) { 125 else if(theEnergy <= edgeMin) << 138 lastBin = 0; 126 { << 139 lastEnergy = edgeMin; 127 lastBin = 0; << 140 lastValue = dataVector[0]; 128 lastEnergy = edgeMin; << 141 129 lastValue = dataVector[0]; << 142 } else if( theEnergy >= edgeMax ){ 130 } << 143 lastBin = numberOfNodes-1; 131 else if(theEnergy >= edgeMax) << 144 lastEnergy = edgeMax; 132 { << 145 lastValue = dataVector[lastBin]; 133 lastBin = numberOfNodes - 1; << 146 134 lastEnergy = edgeMax; << 147 } else { 135 lastValue = dataVector[lastBin]; << 148 lastBin = FindBinLocation(theEnergy); 136 } << 149 lastEnergy = theEnergy; 137 else << 150 lastValue = Interpolation(); 138 { << 151 139 lastBin = (std::size_t)(G4Log(theEnergy) / << 140 if(lastBin == numberOfNodes) << 141 { << 142 --lastBin; << 143 } // VI: fix possible precision lost << 144 lastEnergy = theEnergy; << 145 lastValue = Interpolation(); << 146 } 152 } 147 return lastValue; << 153 return lastValue; 148 } 154 } 149 155 150 // ------------------------------------------- << 156 /////////////////////////////////////////////////////// >> 157 // Static methods >> 158 >> 159 /////////////////// 151 G4VelocityTable* G4VelocityTable::GetVelocityT 160 G4VelocityTable* G4VelocityTable::GetVelocityTable() >> 161 /////////////////// 152 { 162 { 153 if(theInstance == nullptr) << 163 if (!theInstance) { theInstance = new G4VelocityTable(); } 154 { << 155 static G4ThreadLocalSingleton<G4VelocityTa << 156 theInstance = inst.Instance(); << 157 } << 158 return theInstance; 164 return theInstance; 159 } 165 } 160 166 161 // ------------------------------------------- << 167 /////////////////// 162 void G4VelocityTable:: << 168 void G4VelocityTable::SetVelocityTableProperties(G4double t_max, G4double t_min, G4int nbin) 163 SetVelocityTableProperties(G4double t_max, G4d << 169 /////////////////// 164 { << 170 { 165 if(theInstance == nullptr) << 171 if (theInstance == 0) { theInstance = new G4VelocityTable(); } 166 { << 167 GetVelocityTable(); << 168 } << 169 172 170 G4StateManager* stateManager = G4StateMan << 173 G4StateManager* stateManager = G4StateManager::GetStateManager(); 171 G4ApplicationState currentState = stateManag 174 G4ApplicationState currentState = stateManager->GetCurrentState(); 172 << 175 173 // check if state is outside event loop 176 // check if state is outside event loop 174 if(!(currentState == G4State_Idle || current << 177 if(!(currentState==G4State_Idle||currentState==G4State_PreInit)){ 175 { << 178 G4Exception("G4VelocityTable::SetVelocityTableProperties", 176 G4Exception("G4VelocityTable::SetVelocityT << 179 "Track101", JustWarning, 177 JustWarning, << 178 "Can modify only in PreInit or 180 "Can modify only in PreInit or Idle state : Method ignored."); 179 return; 181 return; 180 } 182 } 181 183 182 if(nbin > 100) << 184 if (nbin > 100 ) theInstance->NbinT = nbin; 183 theInstance->NbinT = nbin; << 185 if ((t_min < t_max)&&(t_min>0.)) { 184 if((t_min < t_max) && (t_min > 0.)) << 186 theInstance->minT = t_min; 185 { << 186 theInstance->minT = t_min; << 187 theInstance->maxT = t_max; 187 theInstance->maxT = t_max; 188 } << 188 } 189 theInstance->PrepareVelocityTable(); 189 theInstance->PrepareVelocityTable(); 190 } 190 } 191 191 192 // ------------------------------------------- << 192 /////////////////// 193 G4double G4VelocityTable::GetMaxTOfVelocityTab 193 G4double G4VelocityTable::GetMaxTOfVelocityTable() >> 194 /////////////////// 194 { 195 { 195 return GetVelocityTable()->maxT; << 196 return GetVelocityTable()->maxT; 196 } 197 } 197 198 198 // ------------------------------------------- << 199 /////////////////// 199 G4double G4VelocityTable::GetMinTOfVelocityTab << 200 G4double G4VelocityTable::GetMinTOfVelocityTable() >> 201 /////////////////// 200 { 202 { 201 return GetVelocityTable()->minT; << 203 return GetVelocityTable()->minT; 202 } 204 } 203 205 204 // ------------------------------------------- << 206 /////////////////// 205 G4int G4VelocityTable::GetNbinOfVelocityTable( << 207 G4int G4VelocityTable::GetNbinOfVelocityTable() >> 208 /////////////////// 206 { 209 { 207 return GetVelocityTable()->NbinT; << 210 return GetVelocityTable()->NbinT; 208 } 211 } 209 212