Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4FunctionSolver< T_Function > Class Template Reference

#include <G4FunctionSolver.hh>

Public Member Functions

 G4FunctionSolver (T_Function *ff, const G4int iterations, const G4double tol)
 G4FunctionSolver (const G4FunctionSolver &right)=delete
 ~G4FunctionSolver ()=default
G4FunctionSolveroperator= (const G4FunctionSolver &right)=delete
G4bool operator== (const G4FunctionSolver &right) const =delete
G4bool operator!= (const G4FunctionSolver &right) const =delete
void SetMaxIterations (const G4int iterations)
void SetTolerance (const G4double epsilon)
void SetIntervalLimits (const G4double Limit1, const G4double Limit2)
G4bool FindRoot (G4double &x)

Detailed Description

template<class T_Function>
class G4FunctionSolver< T_Function >

Definition at line 52 of file G4FunctionSolver.hh.

Constructor & Destructor Documentation

◆ G4FunctionSolver() [1/2]

template<class T_Function>
G4FunctionSolver< T_Function >::G4FunctionSolver ( T_Function * ff,
const G4int iterations,
const G4double tol )
inline

Definition at line 56 of file G4FunctionSolver.hh.

57 : maxIter(iterations), tolerance(tol), tF(ff) {};

Referenced by G4FunctionSolver(), operator!=(), operator=(), and operator==().

◆ G4FunctionSolver() [2/2]

template<class T_Function>
G4FunctionSolver< T_Function >::G4FunctionSolver ( const G4FunctionSolver< T_Function > & right)
delete

◆ ~G4FunctionSolver()

template<class T_Function>
G4FunctionSolver< T_Function >::~G4FunctionSolver ( )
default

Member Function Documentation

◆ FindRoot()

template<class T_Function>
G4bool G4FunctionSolver< T_Function >::FindRoot ( G4double & x)
inline

Definition at line 83 of file G4FunctionSolver.hh.

84 {
85 G4double a = aa;
86 G4double b = bb;
87
88 // check the interval before the start
89 x = std::min(std::max(x, a), b);
90
91 // check initial function
92 G4double fc = tF->Function(x);
93 if (0.0 == fc) { return true; }
94
95 // define accuracy in X
96 G4double epsX = tolerance*x;
97 // define accuracy in Y
98 G4double epsY = tolerance*fc;
99
100 // the interval is too small
101 if (std::abs(a - b) <= epsX)
102 {
103 x = 0.5*(a + b);
104 return true;
105 }
106
107 // check edges
108 G4double fa = tF->Function(a);
109 G4double fb = tF->Function(b);
110
111 // root should be inside interval
112 if (fa*fb >= 0.0)
113 {
114 x = (std::abs(fa) <= std::abs(fb)) ? a : b;
115 return (std::min(std::abs(fa), std::abs(fb)) < epsY);
116 }
117
118 // fa*fb < 0.0 - finding the root by iterative procedure,
119 // the loop is completed if function is below epsY
120 // or if x become close to edges with accuracy epsX
121 for (G4int i = 0; i < maxIter; ++i)
122 {
123 x = (a*fb - b*fa)/(fb - fa);
124 fc = tF->Function(x);
125 if (std::abs(fc) < epsY) { return true; }
126
127 G4double delta = std::min((x - a), (b - x));
128 if (delta < epsX) { return true; }
129 else if (fa*fc < 0.0) { b = x; fb = fc; }
130 else { a = x; fa = fc; }
131 }
132 // number of iterations exceed the limit
133 return false;
134 }

◆ operator!=()

template<class T_Function>
G4bool G4FunctionSolver< T_Function >::operator!= ( const G4FunctionSolver< T_Function > & right) const
delete

◆ operator=()

template<class T_Function>
G4FunctionSolver & G4FunctionSolver< T_Function >::operator= ( const G4FunctionSolver< T_Function > & right)
delete

◆ operator==()

template<class T_Function>
G4bool G4FunctionSolver< T_Function >::operator== ( const G4FunctionSolver< T_Function > & right) const
delete

◆ SetIntervalLimits()

template<class T_Function>
void G4FunctionSolver< T_Function >::SetIntervalLimits ( const G4double Limit1,
const G4double Limit2 )
inline

Definition at line 76 of file G4FunctionSolver.hh.

77 {
78 aa = std::min(Limit1, Limit2);
79 bb = std::max(Limit1, Limit2);
80 }

◆ SetMaxIterations()

template<class T_Function>
void G4FunctionSolver< T_Function >::SetMaxIterations ( const G4int iterations)
inline

Definition at line 70 of file G4FunctionSolver.hh.

71 { maxIter = iterations; }

◆ SetTolerance()

template<class T_Function>
void G4FunctionSolver< T_Function >::SetTolerance ( const G4double epsilon)
inline

Definition at line 73 of file G4FunctionSolver.hh.

74 { tolerance = epsilon; }

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