Geant4-11
Functions | Variables
G4INCL::RootFinder::anonymous_namespace{G4INCLRootFinder.cc} Namespace Reference

Functions

std::pair< G4double, G4doublebracketRoot (RootFunctor const *const f, G4double x0)
 Bracket the root of the function f. More...
 

Variables

const G4int maxIterations =50
 Maximum number of iterations for convergence. More...
 
const G4double toleranceY = 1.e-4
 Tolerance on the y value. More...
 

Function Documentation

◆ bracketRoot()

std::pair< G4double, G4double > G4INCL::RootFinder::anonymous_namespace{G4INCLRootFinder.cc}::bracketRoot ( RootFunctor const *const  f,
G4double  x0 
)

Bracket the root of the function f.

Tries to find a bracketing value for the function root.

Parameters
fpointer to a RootFunctor
x0starting value
Returns
if the root could be bracketed, returns two values of x bracketing the root, as a pair. If the bracketing failed, returns a pair with first > second.

Definition at line 75 of file G4INCLRootFinder.cc.

75 {
76 G4double y0 = (*f)(x0);
77
78 const G4double scaleFactor = 1.5;
79
80 G4double x1;
81 if(x0!=0.)
82 x1=scaleFactor*x0;
83 else
84 x1=1.;
85 G4double y1 = (*f)(x1);
86
87 if(Math::sign(y0)!=Math::sign(y1))
88 return std::make_pair(x0,x1);
89
90 const G4double scaleFactorMinus1 = 1./scaleFactor;
91 G4double oldx0, oldx1, oldy1;
92 G4int iterations=0;
93 do {
94 if(iterations > maxIterations) {
95 INCL_DEBUG("Could not bracket the root." << '\n');
96 return std::make_pair((G4double) 1.,(G4double) -1.);
97 }
98
99 oldx0=x0;
100 oldx1=x1;
101 oldy1=y1;
102
103 x0 *= scaleFactorMinus1;
104 x1 *= scaleFactor;
105 y0 = (*f)(x0);
106 y1 = (*f)(x1);
107 iterations++;
108 } while(Math::sign(y0)==Math::sign(y1)); /* Loop checking, 10.07.2015, D.Mancusi */
109
110 if(Math::sign(y1)==Math::sign(oldy1))
111 return std::make_pair(x0,oldx0);
112 else
113 return std::make_pair(oldx1,x1);
114 }
#define INCL_DEBUG(x)
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
G4int sign(const T t)
const G4int maxIterations
Maximum number of iterations for convergence.

References INCL_DEBUG, maxIterations, and G4INCL::Math::sign().

Referenced by G4INCL::RootFinder::solve().

Variable Documentation

◆ maxIterations

const G4int G4INCL::RootFinder::anonymous_namespace{G4INCLRootFinder.cc}::maxIterations =50

◆ toleranceY

const G4double G4INCL::RootFinder::anonymous_namespace{G4INCLRootFinder.cc}::toleranceY = 1.e-4

Tolerance on the y value.

Definition at line 60 of file G4INCLRootFinder.cc.

Referenced by G4DNARevertProbability::SetTolerance(), and G4INCL::RootFinder::solve().