Geant4_10
G4INCLRootFinder.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 
46 #include "G4INCLRootFinder.hh"
47 #include "G4INCLGlobals.hh"
48 #include "G4INCLLogger.hh"
49 #include <utility>
50 #include <cmath>
51 
52 namespace G4INCL {
53 
54  namespace RootFinder {
55 
56  namespace {
57 
59  const G4double toleranceY = 1.e-4;
60 
62  const G4int maxIterations=50;
63 
74  std::pair<G4double,G4double> bracketRoot(RootFunctor const * const f, G4double x0) {
75  G4double y0 = (*f)(x0);
76 
77  const G4double scaleFactor = 1.5;
78 
79  G4double x1;
80  if(x0!=0.)
81  x1=scaleFactor*x0;
82  else
83  x1=1.;
84  G4double y1 = (*f)(x1);
85 
86  if(Math::sign(y0)!=Math::sign(y1))
87  return std::make_pair(x0,x1);
88 
89  const G4double scaleFactorMinus1 = 1./scaleFactor;
90  G4double oldx0, oldx1, oldy1;
91  G4int iterations=0;
92  do {
93  if(iterations > maxIterations) {
94  INCL_DEBUG("Could not bracket the root." << std::endl);
95  return std::make_pair((G4double) 1.,(G4double) -1.);
96  }
97 
98  oldx0=x0;
99  oldx1=x1;
100  oldy1=y1;
101 
102  x0 *= scaleFactorMinus1;
103  x1 *= scaleFactor;
104  y0 = (*f)(x0);
105  y1 = (*f)(x1);
106  iterations++;
107  } while(Math::sign(y0)==Math::sign(y1));
108 
109  if(Math::sign(y1)==Math::sign(oldy1))
110  return std::make_pair(x0,oldx0);
111  else
112  return std::make_pair(oldx1,x1);
113  }
114 
115  }
116 
117  Solution solve(RootFunctor const * const f, const G4double x0) {
118  // If we already have the solution, do nothing
119  const G4double y0 = (*f)(x0);
120  if( std::abs(y0) < toleranceY ) {
121  return Solution(x0,y0);
122  }
123 
124  // Bracket the root and set the initial values
125  std::pair<G4double,G4double> bracket = bracketRoot(f,x0);
126  G4double x1 = bracket.first;
127  G4double x2 = bracket.second;
128  // If x1>x2, it means that we could not bracket the root. Return false.
129  if(x1>x2) {
130  // Maybe zero is a good solution?
131  G4double y_at_zero = (*f)(0.);
132  if(std::abs(y_at_zero)<=toleranceY) {
133  f->cleanUp(true);
134  return Solution(0.,y_at_zero);
135  } else {
136  INCL_DEBUG("Root-finding algorithm could not bracket the root." << std::endl);
137  f->cleanUp(false);
138  return Solution();
139  }
140  }
141 
142  G4double y1 = (*f)(x1);
143  G4double y2 = (*f)(x2);
144  G4double x = x1;
145  G4double y = y1;
146 
147  /* ********************************
148  * Start of the false-position loop
149  * ********************************/
150 
151  // Keep track of the last updated interval end (-1=left, 1=right)
152  G4int lastUpdated = 0;
153 
154  for(G4int iterations=0; std::abs(y) > toleranceY; iterations++) {
155 
156  if(iterations > maxIterations) {
157  INCL_DEBUG("Root-finding algorithm did not converge." << std::endl);
158  f->cleanUp(false);
159  return Solution();
160  }
161 
162  // Estimate the root position by linear interpolation
163  x = (y1*x2-y2*x1)/(y1-y2);
164 
165  // Update the value of the function
166  y = (*f)(x);
167 
168  // Update the bracketing interval
169  if(Math::sign(y) == Math::sign(y1)) {
170  x1=x;
171  y1=y;
172  if(lastUpdated==-1) y2 *= 0.5;
173  lastUpdated = -1;
174  } else {
175  x2=x;
176  y2=y;
177  if(lastUpdated==1) y1 *= 0.5;
178  lastUpdated = 1;
179  }
180  }
181 
182  /* ******************************
183  * End of the false-position loop
184  * ******************************/
185 
186  f->cleanUp(true);
187  return Solution(x,y);
188  }
189 
190  } // namespace RootFinder
191 }
Double_t y2[nxs]
Definition: Style.C:21
Double_t y1[nxs]
Definition: Style.C:20
Double_t x2[nxs]
Definition: Style.C:19
virtual void cleanUp(const G4bool success) const =0
tuple x
Definition: test.py:50
int G4int
Definition: G4Types.hh:78
TFile f
Definition: plotHisto.C:6
Double_t y
Definition: plot.C:279
Double_t x1[nxs]
Definition: Style.C:18
Solution solve(RootFunctor const *const f, const G4double x0)
Numerically solve a one-dimensional equation.
double G4double
Definition: G4Types.hh:76
#define INCL_DEBUG(x)
G4int sign(const T t)
Static root-finder algorithm.