Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4INCLIFunction1D.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 //
26 // INCL++ intra-nuclear cascade model
27 // Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
28 // Davide Mancusi, CEA
29 // Alain Boudard, CEA
30 // Sylvie Leray, CEA
31 // Joseph Cugnon, University of Liege
32 //
33 #define INCLXX_IN_GEANT4_MODE 1
34 
35 #include "globals.hh"
36 
37 /** \file G4INCLIFunction1D.cc
38  * \brief Functor for 1-dimensional mathematical functions
39  *
40  * \date 16 July 2011
41  * \author Davide Mancusi
42  */
43 
44 #include <algorithm>
45 #include <cmath>
46 #include <cstdlib>
47 #include "G4INCLIFunction1D.hh"
48 #include "G4INCLLogger.hh"
50 
51 namespace G4INCL {
52 
53  const G4double IFunction1D::integrationCoefficients[] = {
54  2.*95.0/288.0,
55  317.0/240.0,
56  23.0/30.0,
57  793.0/720.0,
58  157.0/160.0,
59  157.0/160.0,
60  793.0/720.0,
61  23.0/30.0,
62  317.0/240.0,
63  };
64 
65  G4double IFunction1D::integrate(const G4double x0, const G4double x1, const G4double step) const {
66  G4double xi = std::max(x0, xMin);
67  G4double xa = std::min(x1, xMax);
68  G4double sign;
69 
70  if(x1 <= x0) {
71  sign = -1.0;
72  std::swap(xi, xa);
73  } else
74  sign = 1.0;
75 
76  const G4double interval = xa - xi;
77 
78  G4int nIntervals;
79  if(step<0.) {
80  nIntervals = 45;
81  } else {
82  nIntervals = G4int(interval/step);
83 
84  // Round up nIntervals to the closest multiple of 9
85  G4int remainder = nIntervals % 9;
86  if (remainder != 0)
87  nIntervals += 9 - remainder;
88 
89  nIntervals = std::max(nIntervals, 9);
90  }
91 
92  const G4double dx = interval/nIntervals;
93  G4double result = (operator()(xi) + operator()(xa)) * integrationCoefficients[0]/2;
94  for(G4int j = 1; j<nIntervals; ++j) {
95  const G4double x = xi + interval*G4double(j)/G4double(nIntervals);
96  const unsigned index = j%9;
97  result += operator()(x) * integrationCoefficients[index];
98  }
99 
100  return result*dx*sign;
101 
102  }
103 
105  class Primitive : public IFunction1D {
106  public:
107  Primitive(IFunction1D const * const f) :
108  IFunction1D(f->getXMinimum(), f->getXMaximum()),
109  theFunction(f)
110  {}
111 
112  G4double operator()(const G4double x) const {
113  return theFunction->integrate(xMin,x);
114  }
115  private:
116  IFunction1D const * const theFunction;
117  } *thePrimitive = new Primitive(this);
118 
119  return thePrimitive;
120  }
121 
123  class InverseCDF : public IFunction1D {
124  public:
125  InverseCDF(IFunction1D const * const f) :
126  IFunction1D(f->getXMinimum(), f->getXMaximum()),
127  theFunction(f),
128  normalisation(1./theFunction->integrate(xMin,xMax))
129  {}
130 
131  G4double operator()(const G4double x) const {
132  return std::min(1., normalisation * theFunction->integrate(xMin,x));
133  }
134  private:
135  IFunction1D const * const theFunction;
136  const G4double normalisation;
137  } *theInverseCDF = new InverseCDF(this);
138 
139  InverseInterpolationTable *theTable = new InverseInterpolationTable(*theInverseCDF, nNodes);
140  delete theInverseCDF;
141  return theTable;
142  }
143 
144 }
145 
Simple interpolation table for the inverse of a IFunction1D functor.
IFunction1D * primitive() const
Return a pointer to the (numerical) primitive to this function.
G4double xMin
Minimum value of the independent variable.
int G4int
Definition: G4Types.hh:78
virtual G4double operator()(const G4double x) const =0
Compute the value of the function.
G4double xMax
Maximum value of the independent variable.
InverseInterpolationTable * inverseCDFTable(const G4int nNodes=60) const
Return a pointer to the inverse of the CDF of this function.
void swap(shared_ptr< P > &, shared_ptr< P > &)
Definition: memory.h:1247
Class for interpolating the inverse of a 1-dimensional function.
virtual G4double integrate(const G4double x0, const G4double x1, const G4double step=-1.) const
Integrate the function between two values.
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
Functor for 1-dimensional mathematical functions.
virtual G4double getXMinimum() const
Return the minimum allowed value of the independent variable.
virtual G4double getXMaximum() const
Return the maximum allowed value of the independent variable.
double G4double
Definition: G4Types.hh:76
G4int sign(const T t)