Geant4-11
G4RKIntegrationDriver.icc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4RKIntegrationDriver inline implementation
27//
28// Created: D.Sorokin, 2017
29// --------------------------------------------------------------------
30
31#include "globals.hh"
32
33template <class T>
34G4RKIntegrationDriver<T>::G4RKIntegrationDriver(T* pStepper)
35{
36 RenewStepperAndAdjustImpl(pStepper);
37 fMaxStepBase = 250;
38 fMaxNoSteps = fMaxStepBase / pIntStepper->IntegratorOrder();
39}
40
41// Step failed; compute the size of retrial Step.
42//
43template <class T> G4double G4RKIntegrationDriver<T>::
44ShrinkStepSize(G4double h, G4double error) const
45{
46 if (error > errorConstraintShrink)
47 {
48 return max_stepping_decrease * h;
49 }
50 return GetSafety() * h * std::pow(error, GetPshrnk());
51}
52
53template <class T> G4double G4RKIntegrationDriver<T>::
54ShrinkStepSize2(G4double h, G4double error2) const
55{
56 if (error2 > errorConstraintShrink * errorConstraintShrink)
57 {
58 return max_stepping_decrease * h;
59 }
60 return GetSafety() * h * std::pow(error2, 0.5 * GetPshrnk());
61}
62
63// Compute size of next Step
64//
65template<class T> G4double G4RKIntegrationDriver<T>::
66GrowStepSize(G4double h, G4double error) const
67{
68 if (error < errorConstraintGrow)
69 {
70 return max_stepping_increase * h;
71 }
72 return GetSafety() * h * std::pow(error, GetPgrow());
73}
74
75template<class T> G4double G4RKIntegrationDriver<T>::
76GrowStepSize2(G4double h, G4double error2) const
77{
78 if (error2 < errorConstraintGrow * errorConstraintGrow)
79 {
80 return max_stepping_increase * h;
81 }
82 return GetSafety() * h * std::pow(error2, 0.5 * GetPgrow());
83}
84
85template <class T>
86G4double G4RKIntegrationDriver<T>::
87ComputeNewStepSize( G4double errMaxNorm, // max error (normalised)
88 G4double hstepCurrent ) // current step size
89{
90 if (errMaxNorm > 1)
91 {
92 return ShrinkStepSize(hstepCurrent, errMaxNorm);
93 }
94 else if (errMaxNorm >= 0)
95 {
96 return GrowStepSize(hstepCurrent, errMaxNorm);
97 }
98
99 G4Exception("G4RKIntegrationDriver::ComputeNewStepSize", "GeomField0003",
100 FatalException, "Error is negative!");
101
102 return max_stepping_increase * hstepCurrent;
103}
104
105template <class T>
106void G4RKIntegrationDriver<T>::
107GetDerivatives( const G4FieldTrack& track, G4double dydx[] ) const
108{
109 G4double y[G4FieldTrack::ncompSVEC];
110 track.DumpToArray(y);
111 pIntStepper->RightHandSide(y, dydx);
112}
113
114template <class T>
115void G4RKIntegrationDriver<T>::GetDerivatives(const G4FieldTrack& track,
116 G4double dydx[],
117 G4double field[]) const
118{
119 G4double y[G4FieldTrack::ncompSVEC];
120 track.DumpToArray(y);
121 pIntStepper->RightHandSide(y, dydx, field);
122}
123
124template <class T>
125void G4RKIntegrationDriver<T>::UpdateErrorConstraints()
126{
127 errorConstraintShrink = std::pow(
128 max_stepping_decrease / GetSafety(), 1. / GetPshrnk());
129
130 errorConstraintGrow = std::pow(
131 max_stepping_increase / GetSafety(), 1. / GetPgrow());
132}
133
134template <class T>
135inline G4double G4RKIntegrationDriver<T>::GetSafety() const
136{
137 return safety;
138}
139
140template <class T>
141inline G4double G4RKIntegrationDriver<T>::GetPshrnk() const
142{
143 return pshrnk;
144}
145
146template <class T>
147G4double G4RKIntegrationDriver<T>::GetPgrow() const
148{
149 return pgrow;
150}
151
152template <class T>
153void G4RKIntegrationDriver<T>::ReSetParameters(G4double new_safety)
154{
155 safety = new_safety;
156 pshrnk = -1.0 / pIntStepper->IntegratorOrder();
157 pgrow = -1.0 / (1.0 + pIntStepper->IntegratorOrder());
158 UpdateErrorConstraints();
159}
160
161template <class T>
162void G4RKIntegrationDriver<T>::SetSafety(G4double val)
163{
164 safety = val;
165 UpdateErrorConstraints();
166}
167
168template <class T> void G4RKIntegrationDriver<T>::
169RenewStepperAndAdjust(G4MagIntegratorStepper* stepper)
170{
171 T* ourStepper = dynamic_cast<T*>(stepper);
172 if ( ourStepper )
173 {
174 RenewStepperAndAdjustImpl( ourStepper );
175 }
176 else
177 {
178 G4Exception("G4RKIntegrationDriver::RenewStepperAndAdjust()",
179 "GeomField0002", FatalException,
180 "The type of the stepper provided is incorrect for this templated driver");
181 }
182}
183
184template <class T>
185void G4RKIntegrationDriver<T>::RenewStepperAndAdjustImpl(T* stepper)
186{
187 pIntStepper = stepper;
188 ReSetParameters();
189}
190
191template <class T>
192const T* G4RKIntegrationDriver<T>::GetStepper() const
193{
194 return pIntStepper;
195}
196
197template <class T>
198T* G4RKIntegrationDriver<T>::GetStepper()
199{
200 return pIntStepper;
201}
202
203template <class T>
204G4int G4RKIntegrationDriver<T>::GetMaxNoSteps() const
205{
206 return fMaxNoSteps;
207}
208
209template <class T>
210void G4RKIntegrationDriver<T>::SetMaxNoSteps(G4int val)
211{
212 fMaxNoSteps = val;
213}
214
215template <class T>
216G4EquationOfMotion* G4RKIntegrationDriver<T>::GetEquationOfMotion()
217{
218 return pIntStepper->GetEquationOfMotion();
219}
220
221template <class T>
222void G4RKIntegrationDriver<T>::SetEquationOfMotion(G4EquationOfMotion* equation)
223{
224 pIntStepper->SetEquationOfMotion(equation);
225}
226
227template <class T>
228void G4RKIntegrationDriver<T>::StreamInfo( std::ostream& os ) const
229{
230 os << "State of G4RKIntegrationDriver: " << std::endl;
231 os << " Max number of Steps = " << fMaxNoSteps << std::endl;
232 os << " Safety factor = " << safety << std::endl;
233 os << " Power - shrink = " << pshrnk << std::endl;
234 os << " Power - grow = " << pgrow << std::endl;
235 os << " threshold - shrink = " << errorConstraintShrink << std::endl;
236 os << " threshold - grow = " << errorConstraintGrow << std::endl;
237}