Geant4-11
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
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)
 
G4EquationOfMotionGetEquationOfMotion ()
 
G4int GetNumberOfVariables () const
 
void reset ()
 
void set_max_dt (G4double max_dt)
 
void set_max_relative_error (G4double eps_rel)
 
void SetEquationOfMotion (G4EquationOfMotion *equation)
 
step_result try_step (const G4double in[], const G4double dxdt[], G4double &t, G4double out[], G4double &dt)
 

Private Member Functions

G4double calc_h_opt (G4double h, G4double error, size_t k) const
 
void extrapolate (size_t k, G4double xest[])
 
G4bool in_convergence_window (G4int k) const
 
G4bool set_k_opt (size_t k, G4double &dt)
 
G4bool should_reject (G4double error, G4int k) const
 

Private Attributes

G4int fnvar
 
G4double h_opt [m_k_max+1]
 
G4double m_coeff [m_k_max+1][m_k_max]
 
G4int m_cost [m_k_max+1]
 
G4int m_current_k_opt
 
G4double m_dt_last
 
G4double m_eps_rel
 
G4double m_err [G4FieldTrack::ncompSVEC]
 
G4bool m_first
 
G4int m_interval_sequence [m_k_max+1]
 
G4bool m_last_step_rejected
 
G4double m_max_dt
 
G4ModifiedMidpoint m_midpoint
 
G4double m_table [m_k_max][G4FieldTrack::ncompSVEC]
 
G4double work [m_k_max+1]
 

Static Private Attributes

static const G4int m_k_max = 8
 

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.

48 {
49 success,
50 fail
51 };

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

References m_coeff, m_cost, m_current_k_opt, m_interval_sequence, and m_k_max.

Member Function Documentation

◆ calc_h_opt()

G4double G4BulirschStoer::calc_h_opt ( G4double  h,
G4double  error,
size_t  k 
) const
private

Definition at line 259 of file G4BulirschStoer.cc.

260{
261 /* calculates the optimal step size for a given error and stage number */
262
263 const G4double expo = 1.0 / (2 * k + 1);
264 const G4double facmin = std::pow(STEPFAC3, expo);
266
267 G4double facminInv= 1.0 / facmin;
268 if (error == 0.0)
269 {
270 fac = facminInv;
271 }
272 else
273 {
274 fac = STEPFAC2 * std::pow(error * inv_STEPFAC1 , -expo);
275 fac = std::max(facmin * inv_STEPFAC4, std::min( facminInv, fac));
276 }
277
278 return h * fac;
279}
static const G4double fac
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
static PROLOG_HANDLER error
Definition: xmlrole.cc:127

References error, fac, anonymous_namespace{G4BulirschStoer.cc}::inv_STEPFAC1, anonymous_namespace{G4BulirschStoer.cc}::inv_STEPFAC4, G4INCL::Math::max(), G4INCL::Math::min(), anonymous_namespace{G4BulirschStoer.cc}::STEPFAC2, and anonymous_namespace{G4BulirschStoer.cc}::STEPFAC3.

Referenced by try_step().

◆ extrapolate()

void G4BulirschStoer::extrapolate ( size_t  k,
G4double  xest[] 
)
private

Definition at line 239 of file G4BulirschStoer.cc.

240{
241 /* polynomial extrapolation, see http://www.nr.com/webnotes/nr3web21.pdf
242 * uses the obtained intermediate results to extrapolate to dt->0 */
243
244 for(G4int j = k - 1 ; j > 0; --j)
245 {
246 for (G4int i = 0; i < fnvar; ++i)
247 {
248 m_table[j-1][i] = m_table[j][i] * (1. + m_coeff[k][j])
249 - m_table[j-1][i] * m_coeff[k][j];
250 }
251 }
252 for (G4int i = 0; i < fnvar; ++i)
253 {
254 xest[i] = m_table[0][i] * (1. + m_coeff[k][0]) - xest[i] * m_coeff[k][0];
255 }
256}
G4double m_table[m_k_max][G4FieldTrack::ncompSVEC]

References fnvar, m_coeff, and m_table.

Referenced by try_step().

◆ GetEquationOfMotion()

G4EquationOfMotion * G4BulirschStoer::GetEquationOfMotion ( )
inline

◆ GetNumberOfVariables()

G4int G4BulirschStoer::GetNumberOfVariables ( ) const
inline

◆ in_convergence_window()

G4bool G4BulirschStoer::in_convergence_window ( G4int  k) const
private

Definition at line 312 of file G4BulirschStoer.cc.

313{
314 if( (k == m_current_k_opt - 1) && !m_last_step_rejected )
315 {
316 return true; // decrease stepsize only if last step was not rejected
317 }
318 return (k == m_current_k_opt) || (k == m_current_k_opt + 1);
319}

References m_current_k_opt, and m_last_step_rejected.

◆ reset()

void G4BulirschStoer::reset ( )

Definition at line 233 of file G4BulirschStoer.cc.

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

References m_first, and m_last_step_rejected.

Referenced by try_step().

◆ set_k_opt()

G4bool G4BulirschStoer::set_k_opt ( size_t  k,
G4double dt 
)
private

Definition at line 282 of file G4BulirschStoer.cc.

283{
284 /* calculates the optimal stage number */
285
286 if(k == 1)
287 {
288 m_current_k_opt = 2;
289 return true;
290 }
291 if( (work[k-1] < KFAC1 * work[k]) || (k == m_k_max) ) // order decrease
292 {
293 m_current_k_opt = k - 1;
294 dt = h_opt[ m_current_k_opt ];
295 return true;
296 }
297 else if( (work[k] < KFAC2 * work[k-1])
298 || m_last_step_rejected || (k == m_k_max-1) )
299 { // same order - also do this if last step got rejected
300 m_current_k_opt = k;
302 return true;
303 }
304 else { // order increase - only if last step was not rejected
305 m_current_k_opt = k + 1;
307 / m_cost[m_current_k_opt - 1];
308 return true;
309 }
310}
G4double work[m_k_max+1]
G4double h_opt[m_k_max+1]

References h_opt, anonymous_namespace{G4BulirschStoer.cc}::KFAC1, anonymous_namespace{G4BulirschStoer.cc}::KFAC2, m_cost, m_current_k_opt, m_k_max, m_last_step_rejected, and work.

◆ 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

◆ should_reject()

G4bool G4BulirschStoer::should_reject ( G4double  error,
G4int  k 
) const
private

Definition at line 322 of file G4BulirschStoer.cc.

323{
324 if(k == m_current_k_opt - 1)
325 {
329 const G4double e2 = e*e;
330 // step will fail, criterion 17.3.17 in NR
331 return error * e2 * e2 > d * d; // was return error > dOld * dOld; (where dOld= d/e; )
332 }
333 else if(k == m_current_k_opt)
334 {
337 return error * e * e > d * d; // was return error > dOld * dOld; (where dOld= d/e; )
338 }
339 else
340 {
341 return error > 1.0;
342 }
343}
static const G4double e2[44]

References e2, error, m_current_k_opt, and m_interval_sequence.

Referenced by try_step().

◆ try_step()

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

Definition at line 84 of file G4BulirschStoer.cc.

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

References calc_h_opt(), G4ModifiedMidpoint::DoStep(), error, extrapolate(), fail, fnvar, h_opt, anonymous_namespace{G4BulirschStoer.cc}::KFAC2, m_cost, m_current_k_opt, m_dt_last, m_eps_rel, m_err, m_first, m_interval_sequence, m_k_max, m_last_step_rejected, m_max_dt, m_midpoint, m_table, G4INCL::Math::max(), G4INCL::Math::min(), field_utils::relativeError(), reset(), G4ModifiedMidpoint::SetSteps(), should_reject(), success, and work.

Field Documentation

◆ fnvar

G4int G4BulirschStoer::fnvar
private

Definition at line 85 of file G4BulirschStoer.hh.

Referenced by extrapolate(), and try_step().

◆ h_opt

G4double G4BulirschStoer::h_opt[m_k_max+1]
private

Definition at line 121 of file G4BulirschStoer.hh.

Referenced by set_k_opt(), and try_step().

◆ m_coeff

G4double G4BulirschStoer::m_coeff[m_k_max+1][m_k_max]
private

Definition at line 112 of file G4BulirschStoer.hh.

Referenced by extrapolate(), and G4BulirschStoer().

◆ m_cost

G4int G4BulirschStoer::m_cost[m_k_max+1]
private

Definition at line 115 of file G4BulirschStoer.hh.

Referenced by G4BulirschStoer(), set_k_opt(), and try_step().

◆ m_current_k_opt

G4int G4BulirschStoer::m_current_k_opt
private

◆ m_dt_last

G4double G4BulirschStoer::m_dt_last
private

Definition at line 96 of file G4BulirschStoer.hh.

Referenced by try_step().

◆ m_eps_rel

G4double G4BulirschStoer::m_eps_rel
private

Definition at line 88 of file G4BulirschStoer.hh.

Referenced by try_step().

◆ m_err

G4double G4BulirschStoer::m_err[G4FieldTrack::ncompSVEC]
private

Definition at line 105 of file G4BulirschStoer.hh.

Referenced by try_step().

◆ m_first

G4bool G4BulirschStoer::m_first
private

Definition at line 94 of file G4BulirschStoer.hh.

Referenced by reset(), and try_step().

◆ m_interval_sequence

G4int G4BulirschStoer::m_interval_sequence[m_k_max+1]
private

Definition at line 109 of file G4BulirschStoer.hh.

Referenced by G4BulirschStoer(), should_reject(), and try_step().

◆ m_k_max

const G4int G4BulirschStoer::m_k_max = 8
staticprivate

Definition at line 75 of file G4BulirschStoer.hh.

Referenced by G4BulirschStoer(), set_k_opt(), and try_step().

◆ m_last_step_rejected

G4bool G4BulirschStoer::m_last_step_rejected
private

Definition at line 93 of file G4BulirschStoer.hh.

Referenced by in_convergence_window(), reset(), set_k_opt(), and try_step().

◆ m_max_dt

G4double G4BulirschStoer::m_max_dt
private

Definition at line 100 of file G4BulirschStoer.hh.

Referenced by try_step().

◆ m_midpoint

G4ModifiedMidpoint G4BulirschStoer::m_midpoint
private

Definition at line 91 of file G4BulirschStoer.hh.

Referenced by try_step().

◆ m_table

G4double G4BulirschStoer::m_table[m_k_max][G4FieldTrack::ncompSVEC]
private

Definition at line 118 of file G4BulirschStoer.hh.

Referenced by extrapolate(), and try_step().

◆ work

G4double G4BulirschStoer::work[m_k_max+1]
private

Definition at line 124 of file G4BulirschStoer.hh.

Referenced by set_k_opt(), and try_step().


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