Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4BulirschStoer Class Reference

#include <G4BulirschStoer.hh>

Public Types

enum class  step_result { success , fail }
 

Public Member Functions

 G4BulirschStoer (G4EquationOfMotion *equation, G4int nvar, G4double eps_rel, G4double max_dt=DBL_MAX)
 
void set_max_dt (G4double max_dt)
 
void set_max_relative_error (G4double eps_rel)
 
step_result try_step (const G4double in[], const G4double dxdt[], G4double &t, G4double out[], G4double &dt)
 
void reset ()
 
void SetEquationOfMotion (G4EquationOfMotion *equation)
 
G4EquationOfMotionGetEquationOfMotion ()
 
G4int GetNumberOfVariables () const
 

Detailed Description

Definition at line 43 of file G4BulirschStoer.hh.

Member Enumeration Documentation

◆ step_result

enum class G4BulirschStoer::step_result
strong
Enumerator
success 
fail 

Definition at line 47 of file G4BulirschStoer.hh.

Constructor & Destructor Documentation

◆ G4BulirschStoer()

G4BulirschStoer::G4BulirschStoer ( G4EquationOfMotion * equation,
G4int nvar,
G4double eps_rel,
G4double max_dt = DBL_MAX )

Definition at line 47 of file G4BulirschStoer.cc.

49 : fnvar(nvar), m_eps_rel(eps_rel), m_midpoint(equation,nvar), m_max_dt(max_dt)
50{
51 /* initialize sequence of stage numbers and work */
52
53 for(G4int i = 0; i < m_k_max + 1; ++i)
54 {
55 m_interval_sequence[i] = 2 * (i + 1);
56 if (i == 0)
57 {
58 m_cost[i] = m_interval_sequence[i];
59 }
60 else
61 {
62 m_cost[i] = m_cost[i-1] + m_interval_sequence[i];
63 }
64 for(G4int k = 0; k < i; ++k)
65 {
66 const G4double r = static_cast<G4double>(m_interval_sequence[i])
67 / static_cast<G4double>(m_interval_sequence[k]);
68 m_coeff[i][k] = 1.0 / (r * r - 1.0); // coefficients for extrapolation
69 }
70
71 // crude estimate of optimal order
72 m_current_k_opt = 4;
73
74 // no calculation because log10 might not exist for value_type!
75
76 //const G4double logfact = -log10(std::max(eps_rel, 1.0e-12)) * 0.6 + 0.5;
77 //m_current_k_opt = std::max(1.,
78 // std::min(static_cast<G4double>(m_k_max-1), logfact));
79 }
80}
double G4double
Definition G4Types.hh:83
int G4int
Definition G4Types.hh:85

Member Function Documentation

◆ GetEquationOfMotion()

G4EquationOfMotion * G4BulirschStoer::GetEquationOfMotion ( )
inline

◆ GetNumberOfVariables()

G4int G4BulirschStoer::GetNumberOfVariables ( ) const
inline

◆ reset()

void G4BulirschStoer::reset ( )

Definition at line 232 of file G4BulirschStoer.cc.

233{
234 m_first = true;
235 m_last_step_rejected = false;
236}

Referenced by try_step().

◆ set_max_dt()

void G4BulirschStoer::set_max_dt ( G4double max_dt)
inline

◆ set_max_relative_error()

void G4BulirschStoer::set_max_relative_error ( G4double eps_rel)
inline

◆ SetEquationOfMotion()

void G4BulirschStoer::SetEquationOfMotion ( G4EquationOfMotion * equation)
inline

◆ try_step()

G4BulirschStoer::step_result G4BulirschStoer::try_step ( const G4double in[],
const G4double dxdt[],
G4double & t,
G4double out[],
G4double & dt )

Definition at line 83 of file G4BulirschStoer.cc.

85{
86 if(m_max_dt < dt)
87 {
88 // given step size is bigger then max_dt set limit and return fail
89 //
90 dt = m_max_dt;
91 return step_result::fail;
92 }
93
94 if (dt != m_dt_last)
95 {
96 reset(); // step size changed from outside -> reset
97 }
98
99 G4bool reject = true;
100
101 G4double new_h = dt;
102
103 /* m_current_k_opt is the estimated current optimal stage number */
104
105 for(G4int k = 0; k <= m_current_k_opt+1; ++k)
106 {
107 // the stage counts are stored in m_interval_sequence
108 //
109 m_midpoint.SetSteps(m_interval_sequence[k]);
110 if(k == 0)
111 {
112 m_midpoint.DoStep(in, dxdt, out, dt);
113 /* the first step, nothing more to do */
114 }
115 else
116 {
117 m_midpoint.DoStep(in, dxdt, m_table[k-1], dt);
118 extrapolate(k, out);
119 // get error estimate
120 for (G4int i = 0; i < fnvar; ++i)
121 {
122 m_err[i] = out[i] - m_table[0][i];
123 }
124 const G4double error =
125 field_utils::relativeError(out, m_err, dt, m_eps_rel);
126 h_opt[k] = calc_h_opt(dt, error, k);
127 work[k] = static_cast<G4double>(m_cost[k]) / h_opt[k];
128
129 if( (k == m_current_k_opt-1) || m_first) // convergence before k_opt ?
130 {
131 if(error < 1.0)
132 {
133 // convergence
134 reject = false;
135 if( (work[k] < KFAC2 * work[k-1]) || (m_current_k_opt <= 2) )
136 {
137 // leave order as is (except we were in first round)
138 m_current_k_opt = std::min(m_k_max - 1 , std::max(2 , k + 1));
139 new_h = h_opt[k];
140 new_h *= static_cast<G4double>(m_cost[k + 1])
141 / static_cast<G4double>(m_cost[k]);
142 }
143 else
144 {
145 m_current_k_opt = std::min(m_k_max - 1, std::max(2, k));
146 new_h = h_opt[k];
147 }
148 break;
149 }
150 if(should_reject(error , k) && !m_first)
151 {
152 reject = true;
153 new_h = h_opt[k];
154 break;
155 }
156 }
157 if(k == m_current_k_opt) // convergence at k_opt ?
158 {
159 if(error < 1.0)
160 {
161 // convergence
162 reject = false;
163 if(work[k-1] < KFAC2 * work[k])
164 {
165 m_current_k_opt = std::max( 2 , m_current_k_opt-1 );
166 new_h = h_opt[m_current_k_opt];
167 }
168 else if( (work[k] < KFAC2 * work[k-1]) && !m_last_step_rejected )
169 {
170 m_current_k_opt = std::min(m_k_max - 1, m_current_k_opt + 1);
171 new_h = h_opt[k];
172 new_h *= static_cast<G4double>(m_cost[m_current_k_opt])
173 / static_cast<G4double>(m_cost[k]);
174 }
175 else
176 {
177 new_h = h_opt[m_current_k_opt];
178 }
179 break;
180 }
181 if(should_reject(error, k))
182 {
183 reject = true;
184 new_h = h_opt[m_current_k_opt];
185 break;
186 }
187 }
188 if(k == m_current_k_opt + 1) // convergence at k_opt+1 ?
189 {
190 if(error < 1.0) // convergence
191 {
192 reject = false;
193 if(work[k-2] < KFAC2 * work[k-1])
194 {
195 m_current_k_opt = std::max(2, m_current_k_opt - 1);
196 }
197 if((work[k] < KFAC2 * work[m_current_k_opt]) && !m_last_step_rejected)
198 {
199 m_current_k_opt = std::min(m_k_max - 1 , k);
200 }
201 new_h = h_opt[m_current_k_opt];
202 }
203 else
204 {
205 reject = true;
206 new_h = h_opt[m_current_k_opt];
207 }
208 break;
209 }
210 }
211 }
212
213 if(!reject)
214 {
215 t += dt;
216 }
217
218 if(!m_last_step_rejected || new_h < dt)
219 {
220 // limit step size
221 new_h = std::min(m_max_dt, new_h);
222 m_dt_last = new_h;
223 dt = new_h;
224 }
225
226 m_last_step_rejected = reject;
227 m_first = false;
228
229 return reject ? step_result::fail : step_result::success;
230}
bool G4bool
Definition G4Types.hh:86
void SetSteps(G4int steps)
void DoStep(const G4double yIn[], const G4double dydxIn[], G4double yOut[], G4double hstep) const
G4double relativeError(const G4double y[], const G4double yerr[], G4double hstep, G4double errorTolerance)

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