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