2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
26// G4RKIntegrationDriver inline implementation
28// Created: D.Sorokin, 2017
29// --------------------------------------------------------------------
34G4RKIntegrationDriver<T>::G4RKIntegrationDriver(T* pStepper)
36 RenewStepperAndAdjustImpl(pStepper);
38 fMaxNoSteps = fMaxStepBase / pIntStepper->IntegratorOrder();
41// Step failed; compute the size of retrial Step.
43template <class T> G4double G4RKIntegrationDriver<T>::
44ShrinkStepSize(G4double h, G4double error) const
46 if (error > errorConstraintShrink)
48 return max_stepping_decrease * h;
50 return GetSafety() * h * std::pow(error, GetPshrnk());
53template <class T> G4double G4RKIntegrationDriver<T>::
54ShrinkStepSize2(G4double h, G4double error2) const
56 if (error2 > errorConstraintShrink * errorConstraintShrink)
58 return max_stepping_decrease * h;
60 return GetSafety() * h * std::pow(error2, 0.5 * GetPshrnk());
63// Compute size of next Step
65template<class T> G4double G4RKIntegrationDriver<T>::
66GrowStepSize(G4double h, G4double error) const
68 if (error < errorConstraintGrow)
70 return max_stepping_increase * h;
72 return GetSafety() * h * std::pow(error, GetPgrow());
75template<class T> G4double G4RKIntegrationDriver<T>::
76GrowStepSize2(G4double h, G4double error2) const
78 if (error2 < errorConstraintGrow * errorConstraintGrow)
80 return max_stepping_increase * h;
82 return GetSafety() * h * std::pow(error2, 0.5 * GetPgrow());
86G4double G4RKIntegrationDriver<T>::
87ComputeNewStepSize( G4double errMaxNorm, // max error (normalised)
88 G4double hstepCurrent ) // current step size
92 return ShrinkStepSize(hstepCurrent, errMaxNorm);
94 else if (errMaxNorm >= 0)
96 return GrowStepSize(hstepCurrent, errMaxNorm);
99 G4Exception("G4RKIntegrationDriver::ComputeNewStepSize", "GeomField0003",
100 FatalException, "Error is negative!");
102 return max_stepping_increase * hstepCurrent;
106void G4RKIntegrationDriver<T>::
107GetDerivatives( const G4FieldTrack& track, G4double dydx[] ) const
109 G4double y[G4FieldTrack::ncompSVEC];
110 track.DumpToArray(y);
111 pIntStepper->RightHandSide(y, dydx);
115void G4RKIntegrationDriver<T>::GetDerivatives(const G4FieldTrack& track,
117 G4double field[]) const
119 G4double y[G4FieldTrack::ncompSVEC];
120 track.DumpToArray(y);
121 pIntStepper->RightHandSide(y, dydx, field);
125void G4RKIntegrationDriver<T>::UpdateErrorConstraints()
127 errorConstraintShrink = std::pow(
128 max_stepping_decrease / GetSafety(), 1. / GetPshrnk());
130 errorConstraintGrow = std::pow(
131 max_stepping_increase / GetSafety(), 1. / GetPgrow());
135inline G4double G4RKIntegrationDriver<T>::GetSafety() const
141inline G4double G4RKIntegrationDriver<T>::GetPshrnk() const
147G4double G4RKIntegrationDriver<T>::GetPgrow() const
153void G4RKIntegrationDriver<T>::ReSetParameters(G4double new_safety)
156 pshrnk = -1.0 / pIntStepper->IntegratorOrder();
157 pgrow = -1.0 / (1.0 + pIntStepper->IntegratorOrder());
158 UpdateErrorConstraints();
162void G4RKIntegrationDriver<T>::SetSafety(G4double val)
165 UpdateErrorConstraints();
168template <class T> void G4RKIntegrationDriver<T>::
169RenewStepperAndAdjust(G4MagIntegratorStepper* stepper)
171 T* ourStepper = dynamic_cast<T*>(stepper);
174 RenewStepperAndAdjustImpl( ourStepper );
178 G4Exception("G4RKIntegrationDriver::RenewStepperAndAdjust()",
179 "GeomField0002", FatalException,
180 "The type of the stepper provided is incorrect for this templated driver");
185void G4RKIntegrationDriver<T>::RenewStepperAndAdjustImpl(T* stepper)
187 pIntStepper = stepper;
192const T* G4RKIntegrationDriver<T>::GetStepper() const
198T* G4RKIntegrationDriver<T>::GetStepper()
204G4int G4RKIntegrationDriver<T>::GetMaxNoSteps() const
210void G4RKIntegrationDriver<T>::SetMaxNoSteps(G4int val)
216G4EquationOfMotion* G4RKIntegrationDriver<T>::GetEquationOfMotion()
218 return pIntStepper->GetEquationOfMotion();
222void G4RKIntegrationDriver<T>::SetEquationOfMotion(G4EquationOfMotion* equation)
224 pIntStepper->SetEquationOfMotion(equation);
228void G4RKIntegrationDriver<T>::StreamInfo( std::ostream& os ) const
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;