Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4INCL::RootFinder Class Reference

#include <G4INCLRootFinder.hh>

Static Public Member Functions

static G4bool solve (RootFunctor const *const f, const G4double x0)
 Numerically solve a one-dimensional equation.
 
static std::pair< G4double, G4double > constgetSolution ()
 Get the solution of the last call to solve().
 

Protected Member Functions

 RootFinder ()
 
 ~RootFinder ()
 

Detailed Description

Definition at line 66 of file G4INCLRootFinder.hh.

Constructor & Destructor Documentation

◆ RootFinder()

G4INCL::RootFinder::RootFinder ( )
inlineprotected

Definition at line 110 of file G4INCLRootFinder.hh.

110{};

◆ ~RootFinder()

G4INCL::RootFinder::~RootFinder ( )
inlineprotected

Definition at line 111 of file G4INCLRootFinder.hh.

111{};

Member Function Documentation

◆ getSolution()

static std::pair< G4double, G4double > const & G4INCL::RootFinder::getSolution ( )
inlinestatic

Get the solution of the last call to solve().

Returns
the solution, as an (x,y) pair

Definition at line 85 of file G4INCLRootFinder.hh.

85{ return RootFinder::solution; }

Referenced by G4INCL::InteractionAvatar::enforceEnergyConservation().

◆ solve()

G4bool G4INCL::RootFinder::solve ( RootFunctor const *const  f,
const G4double  x0 
)
static

Numerically solve a one-dimensional equation.

Numerically solves the equation f(x)==0. This implementation uses the false-position method.

If a root is found, it can be retrieved using the getSolution() method,

Parameters
fpointer to a RootFunctor
x0initial value of the function argument
Returns
true if a root was found

Definition at line 60 of file G4INCLRootFinder.cc.

60 {
61 // If we already have the solution, do nothing
62 const G4double y0 = (*f)(x0);
63 if( std::abs(y0) < toleranceY ) {
64 solution = std::make_pair(x0,y0);
65 return true;
66 }
67
68 // Bracket the root and set the initial values
69 std::pair<G4double,G4double> bracket = bracketRoot(f,x0);
70 G4double x1 = bracket.first;
71 G4double x2 = bracket.second;
72 // If x1>x2, it means that we could not bracket the root. Return false.
73 if(x1>x2) {
74 // Maybe zero is a good solution?
75 G4double y_at_zero = (*f)(0.);
76 if(std::abs(y_at_zero)<=toleranceY) {
77 f->cleanUp(true);
78 solution = std::make_pair(0.,y_at_zero);
79 return true;
80 } else {
81 WARN("Root-finding algorithm could not bracket the root." << std::endl);
82 f->cleanUp(false);
83 return false;
84 }
85 }
86
87 G4double y1 = (*f)(x1);
88 G4double y2 = (*f)(x2);
89 G4double x = x1;
90 G4double y = y1;
91
92 /* ********************************
93 * Start of the false-position loop
94 * ********************************/
95
96 // Keep track of the last updated interval end (-1=left, 1=right)
97 G4int lastUpdated = 0;
98
99 for(G4int iterations=0; std::abs(y) > toleranceY; iterations++) {
100
101 if(iterations > maxIterations) {
102 WARN("Root-finding algorithm did not converge." << std::endl);
103 f->cleanUp(false);
104 return false;
105 }
106
107 // Estimate the root position by linear interpolation
108 x = (y1*x2-y2*x1)/(y1-y2);
109
110 // Update the value of the function
111 y = (*f)(x);
112
113 // Update the bracketing interval
114 if(Math::sign(y) == Math::sign(y1)) {
115 x1=x;
116 y1=y;
117 if(lastUpdated==-1) y2 *= 0.5;
118 lastUpdated = -1;
119 } else {
120 x2=x;
121 y2=y;
122 if(lastUpdated==1) y1 *= 0.5;
123 lastUpdated = 1;
124 }
125 }
126
127 /* ******************************
128 * End of the false-position loop
129 * ******************************/
130
131 solution = std::make_pair(x,y);
132 f->cleanUp(true);
133 return true;
134 }
#define WARN(x)
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66
G4int sign(T t)

Referenced by G4INCL::InteractionAvatar::enforceEnergyConservation().


The documentation for this class was generated from the following files: