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