Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4BulirschStoer.cc
이 파일의 문서화 페이지로 가기
1 // ********************************************************************
2 // * License and Disclaimer *
3 // * *
4 // * The Geant4 software is copyright of the Copyright Holders of *
5 // * the Geant4 Collaboration. It is provided under the terms and *
6 // * conditions of the Geant4 Software License, included in the file *
7 // * LICENSE and available at http://cern.ch/geant4/license . These *
8 // * include a list of copyright holders. *
9 // * *
10 // * Neither the authors of this software system, nor their employing *
11 // * institutes,nor the agencies providing financial support for this *
12 // * work make any representation or warranty, express or implied, *
13 // * regarding this software system or assume any liability for its *
14 // * use. Please see the license in the file LICENSE and URL above *
15 // * for the full disclaimer and the limitation of liability. *
16 // * *
17 // * This code implementation is the result of the scientific and *
18 // * technical work of the GEANT4 collaboration. *
19 // * By using, copying, modifying or distributing the software (or *
20 // * any work based on the software) you agree to acknowledge its *
21 // * use in resulting scientific publications, and indicate your *
22 // * acceptance of all terms of the Geant4 Software license. *
23 // ********************************************************************
24 //
25 //
26 // $Id: $
27 //
28 // G4BulirschStoer class implementation
29 // Based on bulirsch_stoer.hpp from boost
30 //
31 // Author: Dmitry Sorokin - GSoC 2016
32 //
34 
35 #include "G4BulirschStoer.hh"
36 
37 #include "G4FieldUtils.hh"
38 
39 namespace
40 {
41  const G4double STEPFAC1 = 0.65;
42  const G4double STEPFAC2 = 0.94;
43  const G4double STEPFAC3 = 0.02;
44  const G4double STEPFAC4 = 4.0;
45  const G4double KFAC1 = 0.8;
46  const G4double KFAC2 = 0.9;
47 } // namespace
48 
50  G4int nvar, G4double eps_rel, G4double max_dt)
51  : fnvar(nvar), m_eps_rel(eps_rel), m_midpoint(equation,nvar),
52  m_last_step_rejected(false), m_first(true), m_dt_last(0.0), m_max_dt(max_dt)
53 {
54  /* initialize sequence of stage numbers and work */
55 
56  for(G4int i = 0; i < m_k_max + 1; ++i)
57  {
58  m_interval_sequence[i] = 2 * (i + 1);
59  if (i == 0)
60  {
62  }
63  else
64  {
65  m_cost[i] = m_cost[i-1] + m_interval_sequence[i];
66  }
67  for(G4int k = 0; k < i; ++k)
68  {
69  const G4double r = static_cast<G4double>(m_interval_sequence[i])
70  / static_cast<G4double>(m_interval_sequence[k]);
71  m_coeff[i][k] = 1.0 / (r * r - 1.0); // coefficients for extrapolation
72  }
73 
74  // crude estimate of optimal order
75  m_current_k_opt = 4;
76 
77  // no calculation because log10 might not exist for value_type!
78 
79  //const G4double logfact = -log10(std::max(eps_rel, 1.0e-12)) * 0.6 + 0.5;
80  //m_current_k_opt = std::max(1.,
81  // std::min(static_cast<G4double>(m_k_max-1), logfact));
82  }
83 }
84 
87  G4double& t, G4double out[], G4double& dt)
88 {
89  if(m_max_dt < dt)
90  {
91  // given step size is bigger then max_dt set limit and return fail
92  //
93  dt = m_max_dt;
94  return step_result::fail;
95  }
96 
97  if (dt != m_dt_last)
98  {
99  reset(); // step size changed from outside -> reset
100  }
101 
102  G4bool reject = true;
103 
104  G4double new_h = dt;
105 
106  /* m_current_k_opt is the estimated current optimal stage number */
107 
108  for(G4int k = 0; k <= m_current_k_opt+1; ++k)
109  {
110  // the stage counts are stored in m_interval_sequence
111  //
113  if(k == 0)
114  {
115  m_midpoint.DoStep(in, dxdt, out, dt);
116  /* the first step, nothing more to do */
117  }
118  else
119  {
120  m_midpoint.DoStep(in, dxdt, m_table[k-1], dt);
121  extrapolate(k, out);
122  // get error estimate
123  for (G4int i = 0; i < fnvar; ++i)
124  {
125  m_err[i] = out[i] - m_table[0][i];
126  }
127  const G4double error =
129  h_opt[k] = calc_h_opt(dt, error, k);
130  work[k] = static_cast<G4double>(m_cost[k]) / h_opt[k];
131 
132  if( (k == m_current_k_opt-1) || m_first) // convergence before k_opt ?
133  {
134  if(error < 1.0)
135  {
136  // convergence
137  reject = false;
138  if( (work[k] < KFAC2 * work[k-1]) || (m_current_k_opt <= 2) )
139  {
140  // leave order as is (except we were in first round)
141  m_current_k_opt = std::min(m_k_max - 1 , std::max(2 , k + 1));
142  new_h = h_opt[k];
143  new_h *= static_cast<G4double>(m_cost[k + 1])
144  / static_cast<G4double>(m_cost[k]);
145  }
146  else
147  {
149  new_h = h_opt[k];
150  }
151  break;
152  }
153  else if(should_reject(error , k) && !m_first)
154  {
155  reject = true;
156  new_h = h_opt[k];
157  break;
158  }
159  }
160  if(k == m_current_k_opt) // convergence at k_opt ?
161  {
162  if(error < 1.0)
163  {
164  // convergence
165  reject = false;
166  if(work[k-1] < KFAC2 * work[k])
167  {
169  new_h = h_opt[m_current_k_opt];
170  }
171  else if( (work[k] < KFAC2 * work[k-1]) && !m_last_step_rejected )
172  {
174  new_h = h_opt[k];
175  new_h *= static_cast<G4double>(m_cost[m_current_k_opt])
176  / static_cast<G4double>(m_cost[k]);
177  }
178  else
179  {
180  new_h = h_opt[m_current_k_opt];
181  }
182  break;
183  }
184  else if(should_reject(error, k))
185  {
186  reject = true;
187  new_h = h_opt[m_current_k_opt];
188  break;
189  }
190  }
191  if(k == m_current_k_opt + 1) // convergence at k_opt+1 ?
192  {
193  if(error < 1.0) // convergence
194  {
195  reject = false;
196  if(work[k-2] < KFAC2 * work[k-1])
197  {
199  }
200  if((work[k] < KFAC2 * work[m_current_k_opt]) && !m_last_step_rejected)
201  {
202  m_current_k_opt = std::min(m_k_max - 1 , k);
203  }
204  new_h = h_opt[m_current_k_opt];
205  }
206  else
207  {
208  reject = true;
209  new_h = h_opt[m_current_k_opt];
210  }
211  break;
212  }
213  }
214  }
215 
216  if(!reject)
217  {
218  t += dt;
219  }
220 
221  if(!m_last_step_rejected || new_h < dt)
222  {
223  // limit step size
224  new_h = std::min(m_max_dt, new_h);
225  m_dt_last = new_h;
226  dt = new_h;
227  }
228 
229  m_last_step_rejected = reject;
230  m_first = false;
231 
232  return reject ? step_result::fail : step_result::success;
233 }
234 
236 {
237  m_first = true;
238  m_last_step_rejected = false;
239 }
240 
241 void G4BulirschStoer::extrapolate(size_t k , G4double xest[])
242 {
243  /* polynomial extrapolation, see http://www.nr.com/webnotes/nr3web21.pdf
244  * uses the obtained intermediate results to extrapolate to dt->0 */
245 
246  for(G4int j = k - 1 ; j > 0; --j)
247  {
248  for (G4int i = 0; i < fnvar; ++i)
249  {
250  m_table[j-1][i] = m_table[j][i] * (1. + m_coeff[k][j])
251  - m_table[j-1][i] * m_coeff[k][j];
252  }
253  }
254  for (G4int i = 0; i < fnvar; ++i)
255  {
256  xest[i] = m_table[0][i] * (1. + m_coeff[k][0]) - xest[i] * m_coeff[k][0];
257  }
258 }
259 
260 G4double
262 {
263  /* calculates the optimal step size for a given error and stage number */
264 
265  G4double expo = 1.0 / (2 * k + 1);
266  G4double facmin = std::pow(STEPFAC3, expo);
267  G4double fac;
268 
269  if (error == 0.0)
270  {
271  fac = 1.0 / facmin;
272  }
273  else
274  {
275  fac = STEPFAC2 / std::pow(error / STEPFAC1 , expo);
276  fac = std::max(facmin / STEPFAC4, std::min(1.0 / facmin, fac));
277  }
278 
279  return h * fac;
280 }
281 
282 //why is not used!!??
284 {
285  /* calculates the optimal stage number */
286 
287  if(k == 1)
288  {
289  m_current_k_opt = 2;
290  return true;
291  }
292  if( (work[k-1] < KFAC1 * work[k]) || (k == m_k_max) ) // order decrease
293  {
294  m_current_k_opt = k - 1;
295  dt = h_opt[ m_current_k_opt ];
296  return true;
297  }
298  else if( (work[k] < KFAC2 * work[k-1])
299  || m_last_step_rejected || (k == m_k_max-1) )
300  { // same order - also do this if last step got rejected
301  m_current_k_opt = k;
302  dt = h_opt[m_current_k_opt];
303  return true;
304  }
305  else { // order increase - only if last step was not rejected
306  m_current_k_opt = k + 1;
308  / m_cost[m_current_k_opt - 1];
309  return true;
310  }
311 }
312 
314 {
315  if( (k == m_current_k_opt - 1) && !m_last_step_rejected )
316  {
317  return true; // decrease stepsize only if last step was not rejected
318  }
319  return (k == m_current_k_opt) || (k == m_current_k_opt + 1);
320 }
321 
322 
324 {
325  if(k == m_current_k_opt - 1)
326  {
330  * m_interval_sequence[0]);
331  // step will fail, criterion 17.3.17 in NR
332  return error > d * d;
333  }
334  else if(k == m_current_k_opt)
335  {
338  return error > d * d;
339  }
340  else
341  {
342  return error > 1.0;
343  }
344 }
T max(const T t1, const T t2)
brief Return the largest of the two arguments
G4double h_opt[m_k_max+1]
G4bool m_last_step_rejected
G4BulirschStoer(G4EquationOfMotion *equation, G4int nvar, G4double eps_rel, G4double max_dt=DBL_MAX)
G4double m_table[m_k_max][G4FieldTrack::ncompSVEC]
G4double relativeError(const G4double y[], const G4double yerr[], const G4double hstep, const G4double errorTolerance=1)
Definition: G4FieldUtils.cc:38
G4double work[m_k_max+1]
G4bool set_k_opt(size_t k, G4double &dt)
G4ModifiedMidpoint m_midpoint
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
void DoStep(const G4double yIn[], const G4double dydxIn[], G4double yOut[], G4double hstep) const
G4int m_cost[m_k_max+1]
G4bool should_reject(G4double error, G4int k) const
G4int m_interval_sequence[m_k_max+1]
G4double m_err[G4FieldTrack::ncompSVEC]
Float_t d
G4double calc_h_opt(G4double h, G4double error, size_t k) const
G4bool in_convergence_window(G4int k) const
step_result try_step(const G4double in[], const G4double dxdt[], G4double &t, G4double out[], G4double &dt)
int G4int
Definition: G4Types.hh:78
ifstream in
Definition: comparison.C:7
G4double m_coeff[m_k_max+1][m_k_max]
static const G4double fac
static const G4int m_k_max
void extrapolate(size_t k, G4double xest[])
static PROLOG_HANDLER error
Definition: xmlrole.cc:112
void SetSteps(G4int steps)
T min(const T t1, const T t2)
brief Return the smallest of the two arguments