Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  std::pair<G4double,G4double> RootFinder::solution;
55 
56  const G4double RootFinder::toleranceY = 1.e-4;
57 
58  G4bool RootFinder::solve(RootFunctor const * const f, const G4double x0) {
59  // If we already have the solution, do nothing
60  const G4double y0 = (*f)(x0);
61  if( std::abs(y0) < toleranceY ) {
62  solution = std::make_pair(x0,y0);
63  return true;
64  }
65 
66  // Bracket the root and set the initial values
67  std::pair<G4double,G4double> bracket = bracketRoot(f,x0);
68  G4double x1 = bracket.first;
69  G4double x2 = bracket.second;
70  // If x1>x2, it means that we could not bracket the root. Return false.
71  if(x1>x2) {
72  // Maybe zero is a good solution?
73  G4double y_at_zero = (*f)(0.);
74  if(std::abs(y_at_zero)<=toleranceY) {
75  f->cleanUp(true);
76  solution = std::make_pair(0.,y_at_zero);
77  return true;
78  } else {
79  WARN("Root-finding algorithm could not bracket the root." << std::endl);
80  f->cleanUp(false);
81  return false;
82  }
83  }
84 
85  G4double y1 = (*f)(x1);
86  G4double y2 = (*f)(x2);
87  G4double x = x1;
88  G4double y = y1;
89 
90  /* ********************************
91  * Start of the false-position loop
92  * ********************************/
93 
94  // Keep track of the last updated interval end (-1=left, 1=right)
95  G4int lastUpdated = 0;
96 
97  for(G4int iterations=0; std::abs(y) > toleranceY; iterations++) {
98 
99  if(iterations > maxIterations) {
100  WARN("Root-finding algorithm did not converge." << std::endl);
101  f->cleanUp(false);
102  return false;
103  }
104 
105  // Estimate the root position by linear interpolation
106  x = (y1*x2-y2*x1)/(y1-y2);
107 
108  // Update the value of the function
109  y = (*f)(x);
110 
111  // Update the bracketing interval
112  if(Math::sign(y) == Math::sign(y1)) {
113  x1=x;
114  y1=y;
115  if(lastUpdated==-1) y2 *= 0.5;
116  lastUpdated = -1;
117  } else {
118  x2=x;
119  y2=y;
120  if(lastUpdated==1) y1 *= 0.5;
121  lastUpdated = 1;
122  }
123  }
124 
125  /* ******************************
126  * End of the false-position loop
127  * ******************************/
128 
129  solution = std::make_pair(x,y);
130  f->cleanUp(true);
131  return true;
132  }
133 
134  std::pair<G4double,G4double> RootFinder::bracketRoot(RootFunctor const * const f, G4double x0) {
135  G4double y0 = (*f)(x0);
136 
137  const G4double scaleFactor = 1.5;
138 
139  G4double x1;
140  if(x0!=0.)
141  x1=scaleFactor*x0;
142  else
143  x1=1.;
144  G4double y1 = (*f)(x1);
145 
146  if(Math::sign(y0)!=Math::sign(y1))
147  return std::make_pair(x0,x1);
148 
149  const G4double scaleFactorMinus1 = 1./scaleFactor;
150  G4double oldx0, oldx1, oldy1;
151  G4int iterations=0;
152  do {
153  if(iterations > maxIterations) {
154  DEBUG("Could not bracket the root." << std::endl);
155  return std::make_pair((G4double) 1.,(G4double) -1.);
156  }
157 
158  oldx0=x0;
159  oldx1=x1;
160  oldy1=y1;
161 
162  x0 *= scaleFactorMinus1;
163  x1 *= scaleFactor;
164  y0 = (*f)(x0);
165  y1 = (*f)(x1);
166  iterations++;
167  } while(Math::sign(y0)==Math::sign(y1));
168 
169  if(Math::sign(y1)==Math::sign(oldy1))
170  return std::make_pair(x0,oldx0);
171  else
172  return std::make_pair(oldx1,x1);
173  }
174 
175 }