Geant4 10.7.0
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),
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 {
59 m_cost[i] = m_interval_sequence[i];
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
73 m_current_k_opt = 4;
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

Member Function Documentation

◆ GetEquationOfMotion()

G4EquationOfMotion * G4BulirschStoer::GetEquationOfMotion ( )
inline

◆ GetNumberOfVariables()

G4int G4BulirschStoer::GetNumberOfVariables ( ) const
inline

◆ reset()

void G4BulirschStoer::reset ( )

Definition at line 233 of file G4BulirschStoer.cc.

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

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 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 //
110 m_midpoint.SetSteps(m_interval_sequence[k]);
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 =
126 field_utils::relativeError(out, m_err, dt, m_eps_rel);
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 {
146 m_current_k_opt = std::min(m_k_max - 1, std::max(2, k));
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 {
166 m_current_k_opt = std::max( 2 , m_current_k_opt-1 );
167 new_h = h_opt[m_current_k_opt];
168 }
169 else if( (work[k] < KFAC2 * work[k-1]) && !m_last_step_rejected )
170 {
171 m_current_k_opt = std::min(m_k_max - 1, m_current_k_opt + 1);
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 {
196 m_current_k_opt = std::max(2, m_current_k_opt - 1);
197 }
198 if((work[k] < KFAC2 * work[m_current_k_opt]) && !m_last_step_rejected)
199 {
200 m_current_k_opt = std::min(m_k_max - 1 , k);
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 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

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