2 // ********************************************************************
3 // * License and Disclaimer *
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. *
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. *
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 // ********************************************************************
27 // $Id: G4PhysicsVector.icc 79149 2014-02-19 15:08:06Z gcosmo $
30 //---------------------------------------------------------------
31 // GEANT 4 class source file
33 // G4PhysicsVector.icc
36 // A physics vector which has values of energy-loss, cross-section,
37 // and other physics values of a particle in matter in a given
38 // range of the energy, momentum, etc.
39 // This class serves as the base class for a vector having various
40 // energy scale, for example like 'log', 'linear', 'free', etc.
42 //---------------------------------------------------------------
44 extern G4GLOB_DLL G4ThreadLocal G4Allocator<G4PhysicsVector> *fpPVAllocator;
46 //---------------------------------------------------------------
49 void* G4PhysicsVector::operator new(size_t)
51 if (!fpPVAllocator) fpPVAllocator = new G4Allocator<G4PhysicsVector>;
52 return (void*)fpPVAllocator->MallocSingle();
55 //---------------------------------------------------------------
58 void G4PhysicsVector::operator delete(void* aVector)
60 fpPVAllocator->FreeSingle((G4PhysicsVector*)aVector);
63 //---------------------------------------------------------------
66 G4double G4PhysicsVector::Value(G4double theEnergy) const
69 return Value(theEnergy, idx);
72 //---------------------------------------------------------------
75 G4double G4PhysicsVector::operator[](const size_t binNumber) const
77 return dataVector[binNumber];
80 //---------------------------------------------------------------
83 G4double G4PhysicsVector::operator()(const size_t binNumber) const
85 return dataVector[binNumber];
88 //---------------------------------------------------------------
91 G4double G4PhysicsVector::Energy(const size_t binNumber) const
93 return binVector[binNumber];
96 //---------------------------------------------------------------
99 G4double G4PhysicsVector::GetMaxEnergy() const
104 //---------------------------------------------------------------
107 size_t G4PhysicsVector::GetVectorLength() const
109 return numberOfNodes;
112 //---------------------------------------------------------------
115 G4double G4PhysicsVector::GetValue(G4double theEnergy, G4bool&) const
118 return Value(theEnergy, idx);
121 //------------------------------------------------
124 G4double G4PhysicsVector::LinearInterpolation(size_t idx, G4double e) const
126 // Linear interpolation is used to get the value. Before this method
127 // is called it is ensured that the energy is inside the bin
128 // 0 < idx < numberOfNodes-1
130 return dataVector[idx] +
131 ( dataVector[idx + 1]-dataVector[idx] ) * (e - binVector[idx])
132 /( binVector[idx + 1]-binVector[idx] );
135 //---------------------------------------------------------------
138 G4double G4PhysicsVector::SplineInterpolation(size_t idx, G4double e) const
140 // Spline interpolation is used to get the value. Before this method
141 // is called it is ensured that the energy is inside the bin
142 // 0 < idx < numberOfNodes-1
144 static const G4double onesixth = 1.0/6.0;
147 G4double x1 = binVector[idx];
148 G4double x2 = binVector[idx + 1];
149 G4double delta = x2 - x1;
151 G4double a = (x2 - e)/delta;
152 G4double b = (e - x1)/delta;
154 // Final evaluation of cubic spline polynomial for return
155 G4double y1 = dataVector[idx];
156 G4double y2 = dataVector[idx + 1];
158 G4double res = a*y1 + b*y2 +
159 ( (a*a*a - a)*secDerivative[idx] +
160 (b*b*b - b)*secDerivative[idx + 1] )*delta*delta*onesixth;
165 //---------------------------------------------------------------
168 G4double G4PhysicsVector::Interpolation(size_t idx, G4double e) const
171 if(useSpline) { res = SplineInterpolation(idx, e); }
172 else { res = LinearInterpolation(idx, e); }
176 //---------------------------------------------------------------
179 void G4PhysicsVector::PutValue(size_t binNumber, G4double theValue)
181 dataVector[binNumber] = theValue;
184 //---------------------------------------------------------------
187 G4bool G4PhysicsVector::IsFilledVectorExist() const
191 if(numberOfNodes > 0) { status=true; }
195 //---------------------------------------------------------------
198 G4PhysicsVectorType G4PhysicsVector::GetType() const
203 //---------------------------------------------------------------
205 // Flag useSpline is "true" only if second derivatives are filled
207 void G4PhysicsVector::SetSpline(G4bool val)
210 if(0 == secDerivative.size() && 0 < dataVector.size()) {
211 FillSecondDerivatives();
215 secDerivative.clear();
219 //---------------------------------------------------------------
222 void G4PhysicsVector::SetVerboseLevel(G4int value)
224 verboseLevel = value;
227 //---------------------------------------------------------------
230 G4int G4PhysicsVector::GetVerboseLevel(G4int)
235 //---------------------------------------------------------------
238 size_t G4PhysicsVector::FindBinLocation(G4double theEnergy) const
241 if(type == T_G4PhysicsLogVector) {
242 bin = size_t(G4Log(theEnergy)/dBin - baseBin);
243 if(bin + 2 > numberOfNodes) { bin = numberOfNodes - 2; }
244 else if(bin > 0 && theEnergy < binVector[bin]) { --bin; }
245 else if(bin + 2 < numberOfNodes && theEnergy > binVector[bin+1])
247 } else if(type == T_G4PhysicsLinearVector) {
248 bin = size_t( theEnergy/dBin - baseBin );
249 if(bin + 2 > numberOfNodes) { bin = numberOfNodes - 2; }
250 else if(bin > 0 && theEnergy < binVector[bin]) { --bin; }
251 else if(bin + 2 < numberOfNodes && theEnergy > binVector[bin+1])
256 size_t bin3 = numberOfNodes - 1;
257 while (bin != bin3 - 1) {
258 bin2 = bin + (bin3 - bin + 1)/2;
259 if (theEnergy > binVector[bin2]) { bin = bin2; }
260 else { bin3 = bin2; }
263 // Bin location proposed by K.Genser (FNAL)
264 // V.I. Usage of this algorithm provides identical results
265 // no CPU advantage is observed in EM tests
266 // if new validation information will be known this code may be used
267 G4PVDataVector::const_iterator it =
268 std::lower_bound(binVector.begin(), binVector.end(), theEnergy);
269 bin = it - binVector.begin() - 1;
275 //---------------------------------------------------------------
277 inline size_t G4PhysicsVector::FindBin(G4double e, size_t idx) const
280 if(e < binVector[1]) {
282 } else if(e >= binVector[numberOfNodes-2]) {
283 id = numberOfNodes - 2;
284 } else if(idx >= numberOfNodes || e < binVector[idx] || e > binVector[idx+1]) {
285 id = FindBinLocation(e);
290 //---------------------------------------------------------------