Geant4  10.02.p01
G4FastPathHadronicCrossSection.cc
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 //
27 #include "G4ios.hh"
28 #include "G4DynamicParticle.hh"
29 #include "G4Material.hh"
31 #include <vector>
32 #if defined(WIN32)
33  //Needed for M_LN10
34  #define _USE_MATH_DEFINES // for C++
35  #include <math.h>
36 #endif
37 #include <cmath>
38 //#include <math.h>
39 #include <array>
40 
41 #ifdef FPDEBUG
42 #define DBG( msg ) G4cout<< msg <<G4endl;
43 #define DUMP() G4cout<< <<G4endl;
44 #else
45 #define DBG(msg)
46 #define DUMP()
47 #endif
48 
49 using namespace G4FastPathHadronicCrossSection;
50 
51 //Utility functions used to perform fast-path calculations.
52 //See later for details
53 namespace {
54  struct Point_t {
55  double e;
56  double xs;
57  };
58  int simplify_function(G4double tolerance,
59  std::vector<Point_t> & raw_data,
60  std::vector<Point_t> & simplified_data);
61  void RemoveBias( std::vector <Point_t> &,
62  std::vector <Point_t> &,
63  std::vector <Point_t> &);
64 }
65 
67  particle(part),material(mat),min_cutoff(min),physicsVector(nullptr)
68  {
69  DBG("Initializing a fastPathEntry");
70 #ifdef FPDEBUG
71  count = 0;
72  slowpath_sum=0.;
73  max_delta=0.;
74  min_delta=0.;
75  sum_delta=0.;
76  sum_delta_square=0.;
77 #endif
78 }
79 
81 {
82  DBG("Deleting fastPathEntry");
83  DBG("Dumping status for: "<<(particle?particle->GetParticleName():"PART_NONE")<<" "\
84  <<(material?material->GetName():"MAT_NONE")<<" min_cutoff:"<<min_cutoff<<" "\
85  <<" count:"<<count<<" slowpath_sum:"<<slowpath_sum<<" max_delta:"<<max_delta\
86  <<" min_delta"<<min_delta<<" sum_delta"<<sum_delta<<" sum_delta_squared:"<<sum_delta_square);
87  delete physicsVector;
88 }
89 
90 //namespace {
91 // static inline G4double exp10(G4double x) {
92 // return std::exp( M_LN10*x);
93 // }
94 //}
95 
97 {
98  //Check this method is called when G4CrossSectionDataStore is in the correct state:
99  // FastPath is enabled and we are indeed initializing
102  using std::log10;
103  std::vector<Point_t> data_in;
104  const fastPathParameters& params = xsds->GetFastPathParameters();
105  G4double xs;
106 
107  //G4double max_query = params.queryMax;
108  //G4int count = sampleCount;
109  //G4double tol = dpTol;
110 
111  //Shift so max and min are >= 1.
112  //Don't forget to shift back before computing XS
113  G4double min = params.sampleMin;
114  G4double max = params.sampleMax;
115  G4double shift = 0.0;
116  if(min < 1.0){
117  shift = 1.0 - min;
118  }
119  min += shift;
120  max += shift;
121 
122  G4double log_max = std::log10(params.sampleMax);
123  G4double log_min = std::log10(params.sampleMin);
124  G4double log_step = (log_max-log_min)/(1.0*params.sampleCount);
125 
126  G4double max_xs = 0.0;
127 
128  //Utility particle to calculate XS, with 0 kin energy by default
129  static const G4ThreeVector constDirection(0.,0.,1.);
130  G4DynamicParticle* probingParticle = new G4DynamicParticle( particle , constDirection , 0 );
131 
132  //add the cutoff energy
133  probingParticle->SetKineticEnergy(min_cutoff);
134  //Sample cross-section
135  xs = xsds->GetCrossSection(probingParticle,material);
136  data_in.push_back({min_cutoff,xs});
137 
138  G4double currEnergy = 0.0;
139  //log results
140  auto exp10 = [](G4double x){ return std::exp( M_LN10*x); };
141  for(G4double log_currEnergy = log_min; log_currEnergy < log_max; log_currEnergy += log_step){
142  currEnergy = exp10(log_currEnergy) - shift;
143  if (currEnergy < min_cutoff) continue;
144  probingParticle->SetKineticEnergy(currEnergy);
145  xs=xsds->GetCrossSection(probingParticle,material);
146  //G4cout << "PRUTH: energy value " << currEnergy << ", XS value " << xs << G4endl;
147  if (xs > max_xs) max_xs = xs;
148  data_in.push_back({currEnergy,xs});
149  } // --- end of loop i
150  probingParticle->SetKineticEnergy(max-shift);
151  xs = xsds->GetCrossSection(probingParticle,material);
152  data_in.push_back({max-shift,xs});
153 
154  G4double tol = max_xs * 0.01;
155  std::vector<Point_t> decimated_data;
156  simplify_function(tol, data_in, decimated_data);
157  std::vector<Point_t> debiased_data;
158  RemoveBias( data_in, decimated_data, debiased_data);
159  if ( physicsVector != nullptr ) delete physicsVector;
160  physicsVector = new XSParam(decimated_data.size());
161  G4int physicsVectorIndex = 0;
162  for(size_t i = 0; i < decimated_data.size(); i++){
163  physicsVector->PutValue(physicsVectorIndex++, decimated_data[i].e, decimated_data[i].xs);
164  }
165  //xsds->DumpFastPath(particle,material,G4cout);
166 }
167 
169  particle(pname),material(mat),fastPath(nullptr),
170  energy(-1.),crossSection(-1.)
171 {
172  DBG("Initializing cache entry");
173 #ifdef FPDEBUG
174  cacheHitCount = 0;
175  initCyclesFastPath=0;
176  invocationCountSlowPath=0;
177  totalCyclesSlowPath=0;
178  invocationCountFastPath=0;
179  totalCyclesFastPath=0;
180  invocationCountTriedOneLineCache=0;
181  invocationCountOneLineCache=0;
182 #endif
183 }
184 
186 {
187  DBG("Deleting cache entry");
188  DBG(particle<<" "<<material<<" ("<<(material?material->GetName():"MAT_NONE")<<") "<<" "\
189  <<"fast path pointer:"<<fastPath<<" stored:"<<energy<<" "<<crossSection<<" "\
190  <<cacheHitCount<<" "<<initCyclesFastPath<<" "<<invocationCountSlowPath<<" "\
191  <<totalCyclesSlowPath<<" "<<invocationCountFastPath<<" "<<totalCyclesFastPath<<" "\
192  <<invocationCountTriedOneLineCache<<" "<<invocationCountOneLineCache);
193 }
194 
195 #ifdef FPDEBUG
196 namespace {
197  static inline unsigned long long rdtsc() {
198  unsigned hi=0,lo=0;
199 #if defined(__GNUC__) &&( defined(__i386__)|| defined(__x86_64__) )
200  __asm__ __volatile__ ("rdtsc":"=a"(lo),"=d"(hi));
201 #endif
202  return ((unsigned long long)lo) | ((unsigned long long)hi<<32 );
203  }
204 }
206 {
207  tm.rdtsc_start=rdtsc();
208 }
210 {
211  tm.rdtsc_stop=rdtsc();
212 }
213 #endif
215 #ifdef FPDEBUG
216  methodCalled = 0;
217  hitOneLineCache=0;
218  fastPath=0;
219  slowPath=0;
220  sampleZandA = 0;
221 #endif
222 }
223 
224 namespace {
225 // Rob Fowler's simplify code
226 
227 // This is a curve simplification routine based on the Douglas-Peucker
228 // algorithm.
229 // Simplifying assumptions are that the input polyline is a piecewise
230 // function with the x values monotonically increasing, that the function
231 // reaches an asymptote at the right (high energy) end.
232 // Also, the correct error measure is the difference in y between the original
233 // curve and the result.
234 // In GEANT4 use, the assumption is that the calling program has identified
235 // low- and high-energy cutoffs and that the vector passed in is restricted
236 // to the region between the cutoffs.
237 
238 
239 // The raw_data vector comes in ordered left to right (small energy to large).
240 // The simplified_data vector is initially empty.
241 
242 //A.Dotti ( 16-July-2015): transform variable size C-array and use of size_t
243 // to remove compilation warnings
244 
245 int simplify_function(G4double tolerance,
246  std::vector<Point_t> & raw_data,
247  std::vector<Point_t> & simplified_data)
248 {
249 
250  int gap_left, gap_right; // indices of the current region
251 
252  G4double tolsq = tolerance*tolerance; // Alternative to working with absolute values.
253 
254  std::vector<int> working_stack;
255  //A stack of the points to the right of the current interval that
256  // are known to be selected.
257 
258  gap_right = raw_data.size() - 1; // index of the last element.
259 
260  gap_left = 0;
261 
262  DBG("First and last elements " << gap_left <<" " <<gap_right);
263 
264  simplified_data.push_back(raw_data[0]); //copy first element over.
265 
266  DBG("first point ( 0 "
267  <<simplified_data[0].e <<", "<<simplified_data[0].xs <<" )");
268 
269  working_stack.push_back(gap_right); // 0th element on the stack.
270 
271  while ( !working_stack.empty() )
272  { G4double a, slope, delta;
273  G4double deltasq_max= tolsq;
274  int i_max;
275 
276  gap_right = working_stack.back(); //get current TOS
277  i_max = gap_right;
278 
279 
280  if ( (gap_left +1) < gap_right ) // At least three points in the range.
281  {
282  // co-efficients for the left to right affine line segment
283  slope = (raw_data[gap_right].xs - raw_data[gap_left].xs) /
284  (raw_data[gap_right].e - raw_data[gap_left].e);
285  a = raw_data[gap_left].xs - slope * raw_data[gap_left].e;
286 
287  for ( int i = gap_left +1; i <gap_right; i++) {
288  delta = raw_data[i].xs - a - slope * raw_data[i].e;
289  if ( delta * delta > deltasq_max){
290  deltasq_max = delta * delta;
291  i_max = i;
292  }
293  }
294  } else {
295  DBG(" Less than 3 point interval at [ "<< gap_left <<", " <<gap_right<< " ]");
296  }
297 
298  if(i_max < gap_right) { // Found a new point, push it on the stack
299  working_stack.push_back(i_max);
300  DBG(" pushing point " << i_max);
301  gap_right = i_max;
302  }
303  else { // didn't find a new point betweek gap_left and gap_right.
304  simplified_data.push_back(raw_data[gap_right]);
305  DBG("inserting point ("
306  <<gap_right <<", "<<raw_data[gap_right].e <<", "
307  << raw_data[gap_right].xs <<" )");
308  gap_left = gap_right;
309  working_stack.pop_back();
310  gap_right = working_stack.back();
311  DBG(" new gap_right " << gap_right);
312  }
313 
314  }
315  DBG("Simplified curve size "<< simplified_data.size());
316  return (simplified_data.size());
317 }
318 
319 // Rob Fowler's debias code
320 
321 // This is a de-biasing routine applied after using a curve simplification
322 // routine based on the Douglas-Peucker
323 // algorithm.
324 // Simplifying assumptions are that the input polyline is a piecewise
325 // function with the x values monotonically increasing, and
326 // The right error measure is the difference in y between the original
327 // curve and the result.
328 
329 
330 
331 void RemoveBias(std::vector<Point_t> & original, std::vector<Point_t> & simplified,
332  std::vector<Point_t> & result){
333 
334 
335  const size_t originalSize = original.size();
336  const size_t simplifiedSize = simplified.size();
337 
338  //Create index mapping array
339  std::vector<G4int> xindex(simplifiedSize,0);
340  //G4int xindex[simplifiedSize];
341  G4int lastmatch = 0;
342  G4int j = 0;
343 
344  DBG(" original and simplified vector sizes " << originalSize <<" "<<simplifiedSize);
345 
346  for (size_t k = 0; k <simplifiedSize; k++) {
347  for (size_t i = lastmatch; i < originalSize; i++) {
348  if (original[i].e == simplified[k].e) {
349  xindex[j++] = i;
350  lastmatch = i;
351  }
352  }
353  }
354 
355  DBG("Matched " << j << " values of the simplified vector");
356  // Use short names here.
357  G4int m = simplifiedSize;
358 
359  std::vector<G4double> GArea(m-1,0);
360  //G4double GArea [m-1];
361  G4double GAreatotal = 0;
362 
363  //Area of original simplified curve
364  for(int i = 0; i < m-1; i++){
365  G4double GAreatemp = 0;
366 
367  for(j = xindex[i]; j< xindex[i+1]; j++){
368  G4double trap = (original[j+1].xs + original[j].xs) * (original[j+1].e - original[j].e)/2.0;
369  GAreatemp = GAreatemp + trap;
370  }
371  GArea[i] = GAreatemp;
372  GAreatotal = GAreatotal + GAreatemp;
373  }
374 
375  DBG(" Area under the original curve " << GAreatotal);
376 
377  //aleph Why is this not alpha?
378 
379 
380  std::vector<G4double> aleph(m-1,0);
381  //G4double aleph [m-1];
382  for(int i = 0; i< m-1; i++){
383  aleph[i] = (simplified[i+1].e - simplified[i].e)/2.0;
384  }
385 
386  //solve for f
387  std::vector<G4double> adjustedy(m-1,0);
388  //G4double adjustedy [m];
389  adjustedy[m-1] = simplified[m-1].xs;
390  for(int i = 2; i < m+1; i++) {
391  adjustedy[m-i] = (GArea[m-i]/aleph[m-i]) - adjustedy[m-i+1];
392  if (adjustedy[m-i] <0.0) {
393  adjustedy[m-i] = 0.0;
394  DBG(" Fixing negative cross section at index " << (m-i));
395  }
396  }
397 
398  //error and difference tracking
399  std::vector<G4double> difference(m,0.);
400  //G4double difference [m];
401  G4double maxdiff = 0;
402  G4double adjustedarea = 0;
403  G4double simplifiedarea = 0;
404  for(int i = 0; i < m-1; i++){
405  G4double trap;
406  trap = (adjustedy[i+1]+adjustedy[i])*(simplified[i+1].e-simplified[i].e)/2.0;
407  adjustedarea = adjustedarea+trap;
408  trap = (simplified[i+1].xs+simplified[i].xs)*(simplified[i+1].e-simplified[i].e)/2.0;
409  simplifiedarea = simplifiedarea + trap;
410  }
411 
412  DBG(" Area: Simplified curve = " <<simplifiedarea);
413  DBG(" Area: Debiased curve = " << adjustedarea);
414 
415  for(int i = 0; i <m; i++) {
416  difference[i] = simplified[i].xs-adjustedy[i];
417  }
418  for(int i = 0; i <m; i++){
419  if(std::fabs(difference[i]) > maxdiff) {
420  maxdiff = std::fabs(difference[i]);
421  }
422  }
423  // what is the significance of the loops above ?
424 
425  for(size_t i = 0; i < simplifiedSize; i++){
426  result.push_back( {simplified[i].e , adjustedy[i] } );
427  }
428 
429 }
430 
431 
432 }
const G4FastPathHadronicCrossSection::controlFlag & GetFastPathControlFlags() const
fastPathEntry(const G4ParticleDefinition *par, const G4Material *mat, G4double min_cutoff)
void Initialize(G4CrossSectionDataStore *)
void PutValue(size_t binNumber, G4double binValue, G4double dataValue)
CLHEP::Hep3Vector G4ThreeVector
#define DBG(msg)
static const G4float tolerance
const G4String & particle
XSParam * physicsVector
const G4String & GetName() const
Definition: G4Material.hh:178
~fastPathEntry()
const G4ParticleDefinition *const particle
G4double a
Definition: TRTMaterials.hh:39
G4double energy
const G4double min_cutoff
const G4Material *const material
int G4int
Definition: G4Types.hh:78
const G4Material *const material
const G4String & GetParticleName() const
const G4FastPathHadronicCrossSection::fastPathParameters & GetFastPathParameters() const
cycleCountEntry(const G4String &pname, const G4Material *mat)
G4double GetCrossSection(const G4DynamicParticle *, const G4Material *)
void SetKineticEnergy(G4double aEnergy)
fastPathEntry * fastPath
T max(const T t1, const T t2)
brief Return the largest of the two arguments
G4double energy(const ThreeVector &p, const G4double m)
const G4double x[NPOINTSGL]
G4double crossSection
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
static const double m
Definition: G4SIunits.hh:128
~cycleCountEntry()
double G4double
Definition: G4Types.hh:76