Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4INCLInverseInterpolationTable.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 G4INCLInverseInterpolationTable.cc
40 * \brief Simple interpolation table for the inverse of a IFunction1D functor
41 *
42 * \date 17 July 2012
43 * \author Davide Mancusi
44 */
45
46// #include <cassert>
47#include <algorithm>
48#include <functional>
50
51namespace G4INCL {
52
54// assert(nNodes>2);
55
56 const G4double x0 = f.getXMinimum();
57 const G4double x1 = f.getXMaximum();
58
59 // Build the nodes
60 G4double last = f(x0);
61 InterpolationNode firstNode(last, x0, 0.);
62 nodes.push_back(firstNode);
63 G4int skippedNodes = 0;
64 for(unsigned i = 1; i < nNodes; i++) {
65 const G4double xi = x0 + i*(x1-x0)/((G4double)(nNodes-1));
66 // Make sure that the x vector is sorted (corresponding to a monotonous
67 // function)
68 const G4double value = f(xi);
69 if(value <= last) {
70 ++skippedNodes;
71 continue;
72 }
73 InterpolationNode node(value, xi, 0.);
74 nodes.push_back(node);
75 last = value;
76 }
77
78// assert(nNodes==nodes.size()+skippedNodes);
79
80 // Initialise the "derivative" values
81 initDerivatives();
82 setFunctionDomain();
83 }
84
85 InverseInterpolationTable::InverseInterpolationTable(std::vector<G4double> const &x, std::vector<G4double> const &y) {
86// assert(x.size()==y.size());
87 // Assert that the x vector is sorted (corresponding to a monotonous
88 // function
89// assert(std::adjacent_find(nodes.begin(), nodes.end(), std::greater<InterpolationNode>()) == nodes.end());
90
91 for(unsigned i = 0; i < x.size(); ++i)
92 nodes.push_back(InterpolationNode(x.at(i), y.at(i), 0.));
93
94 initDerivatives();
95 setFunctionDomain();
96 }
97
98 void InverseInterpolationTable::initDerivatives() {
99 for(unsigned i = 0; i < nodes.size()-1; i++) {
100 if((nodes.at(i+1).getX() - nodes.at(i).getX()) == 0.0) // Safeguard against division by zero
101 nodes[i].setYPrime(0.0);
102 else
103 nodes[i].setYPrime((nodes.at(i+1).getY() - nodes.at(i).getY())/(nodes.at(i+1).getX() - nodes.at(i).getX()));
104 }
105 nodes.back().setYPrime(nodes.at(nodes.size()-2).getYPrime()); // Duplicate the last value
106 }
107
108 void InverseInterpolationTable::setFunctionDomain() {
109 // Set the function domain
110 if(nodes.front()>nodes.back()) {
111 xMin = nodes.back().getX();
112 xMax = nodes.front().getX();
113 } else {
114 xMin = nodes.front().getX();
115 xMax = nodes.back().getX();
116 }
117 }
118
120 // Find the relevant interpolation bin
121 std::vector<InterpolationNode>::const_iterator iter =
122 std::lower_bound(nodes.begin(), nodes.end(), x);
123
124 if(iter==nodes.begin())
125 return nodes.front().getY();
126
127 if(iter==nodes.end())
128 return nodes.back().getY();
129
130 std::vector<InterpolationNode>::const_iterator previousIter = iter - 1;
131 const G4double dx = x - previousIter->getX();
132 return previousIter->getY() + previousIter->getYPrime()*dx;
133 }
134
136 std::string message;
137 for(std::vector<InterpolationNode>::const_iterator n=nodes.begin(); n!=nodes.end(); ++n)
138 message += n->print();
139 return message;
140 }
141
142}
Simple interpolation table for the inverse of a IFunction1D functor.
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66
virtual G4double getXMaximum() const
Return the maximum allowed value of the independent variable.
G4double xMin
Minimum value of the independent variable.
G4double xMax
Maximum value of the independent variable.
virtual G4double getXMinimum() const
Return the minimum allowed value of the independent variable.
InverseInterpolationTable(IFunction1D const &f, const unsigned int nNodes=30)
G4double operator()(const G4double x) const
Compute the value of the function.