Geant4  10.02.p02
G4PhysicsVector.icc
Go to the documentation of this file.
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: G4PhysicsVector.icc 93409 2015-10-21 13:26:27Z gcosmo $
28 //
29 //
30 //---------------------------------------------------------------
31 // GEANT 4 class source file
32 //
33 // G4PhysicsVector.icc
34 //
35 // Description:
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.
41 //
42 //---------------------------------------------------------------
43 
44 inline
45  G4double G4PhysicsVector::operator[](const size_t binNumber) const
46 {
47  return dataVector[binNumber];
48 }
49 
50 //---------------------------------------------------------------
51 
52 inline
53  G4double G4PhysicsVector::operator()(const size_t binNumber) const
54 {
55  return dataVector[binNumber];
56 }
57 
58 //---------------------------------------------------------------
59 
60 inline
61  G4double G4PhysicsVector::Energy(const size_t binNumber) const
62 {
63  return binVector[binNumber];
64 }
65 
66 //---------------------------------------------------------------
67 
68 inline
69  G4double G4PhysicsVector::GetMaxEnergy() const
70 {
71  return edgeMax;
72 }
73 
74 //---------------------------------------------------------------
75 
76 inline
77  size_t G4PhysicsVector::GetVectorLength() const
78 {
79  return numberOfNodes;
80 }
81 
82 //---------------------------------------------------------------
83 
84 inline
85  G4double G4PhysicsVector::GetValue(G4double theEnergy, G4bool&) const
86 {
87  size_t idx=0;
88  return Value(theEnergy, idx);
89 }
90 
91 //------------------------------------------------
92 
93 inline
94  G4double G4PhysicsVector::LinearInterpolation(size_t idx, G4double e) const
95 {
96  // Linear interpolation is used to get the value. Before this method
97  // is called it is ensured that the energy is inside the bin
98  // 0 < idx < numberOfNodes-1
99 
100  return dataVector[idx] +
101  ( dataVector[idx + 1]-dataVector[idx] ) * (e - binVector[idx])
102  /( binVector[idx + 1]-binVector[idx] );
103 }
104 
105 //---------------------------------------------------------------
106 
107 inline
108  G4double G4PhysicsVector::SplineInterpolation(size_t idx, G4double e) const
109 {
110  // Spline interpolation is used to get the value. Before this method
111  // is called it is ensured that the energy is inside the bin
112  // 0 < idx < numberOfNodes-1
113 
114  static const G4double onesixth = 1.0/6.0;
115 
116  // check bin value
117  G4double x1 = binVector[idx];
118  G4double x2 = binVector[idx + 1];
119  G4double delta = x2 - x1;
120 
121  G4double a = (x2 - e)/delta;
122  G4double b = (e - x1)/delta;
123 
124  // Final evaluation of cubic spline polynomial for return
125  G4double y1 = dataVector[idx];
126  G4double y2 = dataVector[idx + 1];
127 
128  G4double res = a*y1 + b*y2 +
129  ( (a*a*a - a)*secDerivative[idx] +
130  (b*b*b - b)*secDerivative[idx + 1] )*delta*delta*onesixth;
131 
132  return res;
133 }
134 
135 //---------------------------------------------------------------
136 
137 inline
138  G4double G4PhysicsVector::Interpolation(size_t idx, G4double e) const
139 {
140  G4double res;
141  if(useSpline) { res = SplineInterpolation(idx, e); }
142  else { res = LinearInterpolation(idx, e); }
143  return res;
144 }
145 
146 //---------------------------------------------------------------
147 
148 inline
149  void G4PhysicsVector::PutValue(size_t binNumber, G4double theValue)
150 {
151  dataVector[binNumber] = theValue;
152 }
153 
154 //---------------------------------------------------------------
155 
156 inline
157  G4bool G4PhysicsVector::IsFilledVectorExist() const
158 {
159  G4bool status=false;
160 
161  if(numberOfNodes > 0) { status=true; }
162  return status;
163 }
164 
165 //---------------------------------------------------------------
166 
167 inline
168  G4PhysicsVectorType G4PhysicsVector::GetType() const
169 {
170  return type;
171 }
172 
173 //---------------------------------------------------------------
174 
175 // Flag useSpline is "true" only if second derivatives are filled
176 inline
177  void G4PhysicsVector::SetSpline(G4bool val)
178 {
179  if(val) {
180  if(0 == secDerivative.size() && 0 < dataVector.size()) {
181  FillSecondDerivatives();
182  }
183  } else {
184  useSpline = false;
185  secDerivative.clear();
186  }
187 }
188 
189 //---------------------------------------------------------------
190 
191 inline
192 void G4PhysicsVector::SetVerboseLevel(G4int value)
193 {
194  verboseLevel = value;
195 }
196 
197 //---------------------------------------------------------------
198 
199 inline
200 G4int G4PhysicsVector::GetVerboseLevel(G4int)
201 {
202  return verboseLevel;
203 }
204 
205 //---------------------------------------------------------------
206 
207 inline
208 size_t G4PhysicsVector::FindBinLocation(G4double theEnergy) const
209 {
210  size_t bin;
211  if(type == T_G4PhysicsLogVector) {
212  bin = size_t(G4Log(theEnergy)/dBin - baseBin);
213  if(bin > 0 && theEnergy < binVector[bin]) { --bin; }
214  else if(theEnergy > binVector[bin+1]) { ++bin; }
215  } else if(type == T_G4PhysicsLinearVector) {
216  bin = size_t( theEnergy/dBin - baseBin );
217  if(bin > 0 && theEnergy < binVector[bin]) { --bin; }
218  else if(theEnergy > binVector[bin+1]) { ++bin; }
219  } else {
220  // Bin location proposed by K.Genser (FNAL)
221  bin = std::lower_bound(binVector.begin(), binVector.end(), theEnergy)
222  - binVector.begin() - 1;
223  }
224  return std::min(bin, numberOfNodes-2);
225 }
226 
227 //---------------------------------------------------------------
228 
229 inline size_t G4PhysicsVector::FindBin(G4double e, size_t idx) const
230 {
231  size_t id = idx;
232  if(e < binVector[1]) {
233  id = 0;
234  } else if(e >= binVector[numberOfNodes-2]) {
235  id = numberOfNodes - 2;
236  } else if(idx >= numberOfNodes || e < binVector[idx]
237  || e > binVector[idx+1]) {
238  id = FindBinLocation(e);
239  }
240  return id;
241 }
242 
243 //---------------------------------------------------------------
244 
245 inline
246  G4double G4PhysicsVector::Value(G4double theEnergy) const
247 {
248  size_t idx=0;
249  return Value(theEnergy, idx);
250 }
251 
252 //---------------------------------------------------------------