51 G4int statisticsVerbose)
52 : fNoIntegrationVariables(numComponents),
53 fNoVars(
std::max( fNoIntegrationVariables, fMinNoVars )),
54 fStatisticsVerboseLevel(statisticsVerbose)
60 fMinimumStep = hminimum;
67 if( (fVerboseLevel > 0) || (fStatisticsVerboseLevel > 1) )
69 G4cout <<
"MagIntDriver version: Accur-Adv: "
70 <<
"invE_nS, QuickAdv-2sqrt with Statistics "
86 if( fStatisticsVerboseLevel > 1 )
110 G4int no_warnings = 0;
111 static G4int dbg = 1;
123 const G4int nvar = fNoVars;
133 std::ostringstream message;
134 message <<
"Proposed step is zero; hstep = " << hstep <<
" !";
135 G4Exception(
"G4OldMagIntDriver::AccurateAdvance()",
141 std::ostringstream message;
142 message <<
"Invalid run condition." <<
G4endl
143 <<
"Proposed step is negative; hstep = " << hstep <<
"." <<
G4endl
144 <<
"Requested step cannot be negative! Aborting event.";
145 G4Exception(
"G4OldMagIntDriver::AccurateAdvance()",
154 x1= startCurveLength;
157 if ( (hinitial > 0.0) && (hinitial < hstep)
158 && (hinitial > perMillion * hstep) )
169 for ( i=0; i<nvar; ++i) { y[i] = ystart[i]; }
173 G4cout <<
"IDriver::AccurAdv called. "
174 <<
" Input: hstep = " << hstep <<
" hinitial= " << hinitial <<
" , current: h = " << h <<
G4endl;
186 for (i=0; i<nvar; ++i) { ySubStepStart[i] = y[i]; }
198 if( h > fMinimumStep )
206 G4cout <<
"IntegrationDriver -- after OneGoodStep / requesting step = " << h <<
G4endl;
216 G4double dchord_step, dyerr, dyerr_len;
220 QuickAdvance( yFldTrk, dydx, h, dchord_step, dyerr_len );
227 if ( dyerr_len > fDyerr_max ) { fDyerr_max = dyerr_len; }
228 fDyerrPos_smTot += dyerr_len;
230 if (nstp<=1) { ++fNoInitialSmallSteps; }
235 if(fNoSmallSteps<2) {
PrintStatus(ySubStepStart, x1, y, x, h, -nstp); }
236 G4cout <<
"Another sub-min step, no " << fNoSmallSteps
237 <<
" of " << fNoTotalSteps <<
" this time " << nstp <<
G4endl;
239 G4cout <<
" dyerr= " << dyerr_len <<
" relative = " << dyerr_len / h
240 <<
" epsilon= " << eps <<
" hstep= " << hstep
241 <<
" h= " << h <<
" hmin= " << fMinimumStep <<
G4endl;
246 G4Exception(
"G4OldMagIntDriver::AccurateAdvance()",
248 "Integration Step became Zero!");
250 dyerr = dyerr_len / h;
263 static G4int nStpPr = 50;
264 if( (dbg>0) && (dbg<=2) && (nstp>nStpPr))
266 if( nstp==nStpPr ) {
G4cout <<
"***** Many steps ****" <<
G4endl; }
268 G4cout <<
"hdid=" << std::setw(12) << hdid <<
" "
269 <<
"hnext=" << std::setw(12) << hnext <<
" "
270 <<
"hstep=" << std::setw(12) << hstep <<
" (requested) "
272 PrintStatus( ystart, x1, y, x, h, (nstp==nStpPr) ? -nstp: nstp);
277 G4double endPointDist= (EndPos-StartPos).mag();
278 if ( endPointDist >= hdid*(1.+perMillion) )
284 if ( endPointDist >= hdid*(1.+perThousand) )
290 G4cerr <<
" Total steps: bad " << fNoBadSteps
291 <<
" current h= " << hdid <<
G4endl;
292 PrintStatus( ystart, x1, y, x, hstep, no_warnings?nstp:-nstp);
300 if( (h < eps * hstep) || (h < fSmallestFraction * startCurveLength) )
308 if(std::fabs(hnext) <=
Hmin())
312 if( (x < x2 * (1-eps) ) &&
313 (std::fabs(hstep) >
Hmin()) )
318 PrintStatus( ystart, x1, y, x, hstep, no_warnings?nstp:-nstp);
344 int prec=
G4cout.precision(12);
345 G4cout <<
"Warning: G4MagIntegratorDriver::AccurateAdvance"
347 <<
" Integration step 'h' became "
348 << h <<
" due to roundoff. " <<
G4endl
349 <<
" Calculated as difference of x2= "<< x2 <<
" and x=" << x
350 <<
" Forcing termination of advance." <<
G4endl;
356 }
while ( ((nstp++)<=fMaxNoSteps) && (x < x2) && (!lastStep) );
364 for (i=0; i<nvar; ++i) { yEnd[i] = y[i]; }
370 if(nstp > fMaxNoSteps)
384 if( dbg && no_warnings )
386 G4cerr <<
"G4MagIntegratorDriver exit status: no-steps " << nstp <<
G4endl;
402 const G4int maxNoWarnings = 10;
403 std::ostringstream message;
404 if( (noWarningsIssued < maxNoWarnings) || fVerboseLevel > 10 )
406 message <<
"The stepsize for the next iteration, " << hnext
407 <<
", is too small - in Step number " << nstp <<
"." <<
G4endl
408 <<
"The minimum for the driver is " <<
Hmin() <<
G4endl
409 <<
"Requested integr. length was " << hstep <<
" ." <<
G4endl
410 <<
"The size of this sub-step was " << h <<
" ." <<
G4endl
411 <<
"The integrations has already gone " << xDone;
415 message <<
"Too small 'next' step " << hnext
416 <<
", step-no: " << nstp <<
G4endl
417 <<
", this sub-step: " << h
418 <<
", req_tot_len: " << hstep
419 <<
", done: " << xDone <<
", min: " <<
Hmin();
421 G4Exception(
"G4OldMagIntDriver::WarnSmallStepSize()",
"GeomField1001",
433 std::ostringstream message;
434 message <<
"The number of steps used in the Integration driver"
435 <<
" (Runge-Kutta) is too many." <<
G4endl
436 <<
"Integration of the interval was not completed !" <<
G4endl
437 <<
"Only a " << (xCurrent-x1start)*100/(x2end-x1start)
438 <<
" % fraction of it was done.";
439 G4Exception(
"G4OldMagIntDriver::WarnTooManyStep()",
"GeomField1001",
452 G4bool isNewMax, prNewMax;
454 isNewMax = endPointDist > (1.0 + maxRelError) * h;
455 prNewMax = endPointDist > (1.0 + 1.05 * maxRelError) * h;
456 if( isNewMax ) { maxRelError= endPointDist / h - 1.0; }
459 && ( (dbg>1) || prNewMax || (endPointDist >= h*(1.+eps) ) ) )
462 std::ostringstream message;
463 if( (noWarnings++ < 10) || (dbg>2) )
465 message <<
"The integration produced an end-point which " <<
G4endl
466 <<
"is further from the start-point than the curve length."
469 message <<
" Distance of endpoints = " << endPointDist
470 <<
", curve length = " << h <<
G4endl
471 <<
" Difference (curveLen-endpDist)= " << (h - endPointDist)
472 <<
", relative = " << (h-endPointDist) / h
473 <<
", epsilon = " << eps;
474 G4Exception(
"G4OldMagIntDriver::WarnEndPointTooFar()",
"GeomField1001",
511 G4double inv_eps_vel_sq = 1.0 / (eps_rel_max*eps_rel_max);
517 const G4int max_trials=100;
521 G4bool hasSpin = (spin_mag2 > 0.0);
523 for (
G4int iter=0; iter<max_trials; ++iter)
525 pIntStepper-> Stepper(y,dydx,h,ytemp,yerr);
527 G4double eps_pos = eps_rel_max * std::max(h, fMinimumStep);
528 G4double inv_eps_pos_sq = 1.0 / (eps_pos*eps_pos);
532 errpos_sq =
sqr(yerr[0]) +
sqr(yerr[1]) +
sqr(yerr[2]) ;
533 errpos_sq *= inv_eps_pos_sq;
538 if( magvel_sq > 0.0 )
540 errvel_sq = sumerr_sq / magvel_sq;
544 std::ostringstream message;
545 message <<
"Found case of zero momentum." <<
G4endl
546 <<
"- iteration= " << iter <<
"; h= " << h;
549 errvel_sq = sumerr_sq;
551 errvel_sq *= inv_eps_vel_sq;
552 errmax_sq = std::max( errpos_sq, errvel_sq );
557 errspin_sq = (
sqr(yerr[9]) +
sqr(yerr[10]) +
sqr(yerr[11]) )
559 errspin_sq *= inv_eps_vel_sq;
560 errmax_sq = std::max( errmax_sq, errspin_sq );
563 if ( errmax_sq <= 1.0 ) {
break; }
568 if (htemp >= 0.1*h) { h = htemp; }
574 std::ostringstream message;
575 message <<
"Stepsize underflow in Stepper !" <<
G4endl
576 <<
"- Step's start x=" << x <<
" and end x= " << xnew
577 <<
" are equal !! " <<
G4endl
578 <<
" Due to step-size= " << h
579 <<
". Note that input step was " << htry;
587 if (errmax_sq > errcon*errcon)
597 for(
G4int k=0; k<fNoIntegrationVariables; ++k) { y[k] = ytemp[k]; }
613 G4Exception(
"G4OldMagIntDriver::QuickAdvance()",
"GeomField0001",
618 dchord_step = dyerr_pos_sq = hstep * hstep * dydx[0];
631 G4double dyerr_pos_sq, dyerr_mom_rel_sq;
635 G4double dyerr_mom_sq, vel_mag_sq, inv_vel_mag_sq;
646 pIntStepper-> Stepper(yarrin, dydx, hstep, yarrout, yerr_vec) ;
649 dchord_step= pIntStepper-> DistChord();
659 PrintStatus( yarrin, s_start, yarrout, s_start+hstep, hstep, 1);
665 vel_mag_sq = (
sqr(yarrout[3])+
sqr(yarrout[4])+
sqr(yarrout[5]) );
666 inv_vel_mag_sq = 1.0 / vel_mag_sq;
667 dyerr_pos_sq = (
sqr(yerr_vec[0])+
sqr(yerr_vec[1])+
sqr(yerr_vec[2]));
668 dyerr_mom_sq = (
sqr(yerr_vec[3])+
sqr(yerr_vec[4])+
sqr(yerr_vec[5]));
669 dyerr_mom_rel_sq = dyerr_mom_sq * inv_vel_mag_sq;
675#ifdef RETURN_A_NEW_STEP_LENGTH
677 dyerr_len = std::sqrt( dyerr_len_sq );
678 dyerr_len_sq /= eps ;
687 if( dyerr_pos_sq > ( dyerr_mom_rel_sq *
sqr(hstep) ) )
689 dyerr = std::sqrt(dyerr_pos_sq);
694 dyerr = std::sqrt(dyerr_mom_rel_sq) * hstep;
700 <<
"QuickAdvance" <<
G4endl
701 <<
" Input: hstep= " << hstep <<
G4endl
702 <<
" track= " << startTrack <<
G4endl
703 <<
" Output: track= " << y_posvel <<
G4endl
704 <<
" d_chord = " << dchord_step
705 <<
" dyerr = " << dyerr <<
G4endl;
713#ifdef QUICK_ADV_ARRAY_IN_AND_OUT
721 G4Exception(
"G4OldMagIntDriver::QuickAdvance()",
"GeomField0001",
723 dyerr = dchord_step = hstep * yarrin[0] * dydx[0];
724 yarrout[0]= yarrin[0];
740 if(errMaxNorm > 1.0 )
745 else if(errMaxNorm > 0.0 )
774 if (errMaxNorm > 1.0 )
789 if (errMaxNorm > errcon)
816 CurrentFT.
LoadFromArray( CurrentArr, fNoIntegrationVariables);
819 PrintStatus(StartFT, CurrentFT, requestStep, subStepNo );
829 G4int verboseLevel= fVerboseLevel;
839 G4double DotStartCurrentVeloc= StartUnitVelocity.
dot(CurrentUnitVelocity);
844 if( (subStepNo <= 1) || (verboseLevel > 3) )
846 subStepNo = - subStepNo;
848 G4cout << std::setw( 6) <<
" " << std::setw( 25)
849 <<
" G4OldMagIntDriver: Current Position and Direction" <<
" "
851 G4cout << std::setw( 5) <<
"Step#" <<
" "
852 << std::setw( 7) <<
"s-curve" <<
" "
853 << std::setw( 9) <<
"X(mm)" <<
" "
854 << std::setw( 9) <<
"Y(mm)" <<
" "
855 << std::setw( 9) <<
"Z(mm)" <<
" "
856 << std::setw( 8) <<
" N_x " <<
" "
857 << std::setw( 8) <<
" N_y " <<
" "
858 << std::setw( 8) <<
" N_z " <<
" "
859 << std::setw( 8) <<
" N^2-1 " <<
" "
860 << std::setw(10) <<
" N(0).N " <<
" "
861 << std::setw( 7) <<
"KinEner " <<
" "
862 << std::setw(12) <<
"Track-l" <<
" "
863 << std::setw(12) <<
"Step-len" <<
" "
864 << std::setw(12) <<
"Step-len" <<
" "
865 << std::setw( 9) <<
"ReqStep" <<
" "
869 if( (subStepNo <= 0) )
875 if( verboseLevel <= 3 )
879 subStepNo, subStepSize, DotStartCurrentVeloc );
882 G4cout.precision(oldPrec);
899 G4cout << std::setw( 5) << subStepNo <<
" ";
903 G4cout << std::setw( 5) <<
"Start" <<
" ";
906 G4cout << std::setw( 7) << curveLen;
907 G4cout << std::setw( 9) << Position.x() <<
" "
908 << std::setw( 9) << Position.y() <<
" "
909 << std::setw( 9) << Position.z() <<
" "
910 << std::setw( 8) << UnitVelocity.
x() <<
" "
911 << std::setw( 8) << UnitVelocity.
y() <<
" "
912 << std::setw( 8) << UnitVelocity.
z() <<
" ";
914 G4cout << std::setw( 8) << UnitVelocity.
mag2()-1.0 <<
" ";
916 G4cout << std::setw(10) << dotVeloc_StartCurr <<
" ";
917 G4cout.precision(oldprec);
919 G4cout << std::setw(12) << step_len <<
" ";
926 if( curveLen > oldCurveLength )
928 subStep_len= curveLen - oldCurveLength;
930 else if (subStepNo == oldSubStepNo)
932 subStep_len= oldSubStepLength;
934 oldCurveLength= curveLen;
935 oldSubStepLength= subStep_len;
937 G4cout << std::setw(12) << subStep_len <<
" ";
938 G4cout << std::setw(12) << subStepSize <<
" ";
939 if( requestStep != -1.0 )
941 G4cout << std::setw( 9) << requestStep <<
" ";
945 G4cout << std::setw( 9) <<
" InitialStep " <<
" ";
957 G4cout <<
"G4OldMagIntDriver Statistics of steps undertaken. " <<
G4endl;
958 G4cout <<
"G4OldMagIntDriver: Number of Steps: "
959 <<
" Total= " << fNoTotalSteps
960 <<
" Bad= " << fNoBadSteps
961 <<
" Small= " << fNoSmallSteps
962 <<
" Non-initial small= " << (fNoSmallSteps-fNoInitialSmallSteps)
964 G4cout.precision(oldPrec);
971 if( (newFraction > 1.e-16) && (newFraction < 1e-8) )
973 fSmallestFraction= newFraction;
977 std::ostringstream message;
978 message <<
"Smallest Fraction not changed. " <<
G4endl
979 <<
" Proposed value was " << newFraction <<
G4endl
980 <<
" Value must be between 1.e-8 and 1.e-16";
981 G4Exception(
"G4OldMagIntDriver::SetSmallestFraction()",
1028 pIntStepper = pItsStepper;
1034 os <<
"State of G4OldMagIntDriver: " << std::endl;
1035 os <<
" Max number of Steps = " << fMaxNoSteps
1036 <<
" (base # = " << fMaxStepBase <<
" )" << std::endl;
1037 os <<
" Safety factor = " << safety << std::endl;
1038 os <<
" Power - shrink = " << pshrnk << std::endl;
1039 os <<
" Power - grow = " << pgrow << std::endl;
1040 os <<
" threshold (errcon) = " << errcon << std::endl;
1042 os <<
" fMinimumStep = " << fMinimumStep << std::endl;
1043 os <<
" Smallest Fraction = " << fSmallestFraction << std::endl;
1045 os <<
" No Integrat Vars = " << fNoIntegrationVariables << std::endl;
1046 os <<
" Min No Vars = " << fMinNoVars << std::endl;
1047 os <<
" Num-Vars = " << fNoVars << std::endl;
1049 os <<
" verbose level = " << fVerboseLevel << std::endl;
1054 os <<
" Reintegrates = " << does << std::endl;
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
CLHEP::Hep3Vector G4ThreeVector
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
double dot(const Hep3Vector &) const
static void PrintStatus(const G4double *StartArr, G4double xstart, const G4double *CurrentArr, G4double xcurrent, G4double requestStep, unsigned int subStepNo, unsigned int noIntegrationVariables)
const G4ThreeVector & GetMomentumDir() const
G4double GetCurveLength() const
void SetCurveLength(G4double nCurve_s)
G4double GetKineticEnergy() const
G4ThreeVector GetPosition() const
void DumpToArray(G4double valArr[ncompSVEC]) const
void LoadFromArray(const G4double valArr[ncompSVEC], G4int noVarsIntegrated)
G4double GetSurfaceTolerance() const
static G4GeometryTolerance * GetInstance()
G4EquationOfMotion * GetEquationOfMotion()
void RightHandSide(const G4double y[], G4double dydx[]) const
void SetEquationOfMotion(G4EquationOfMotion *newEquation)
virtual G4int IntegratorOrder() const =0
void PrintStat_Aux(const G4FieldTrack &aFieldTrack, G4double requestStep, G4double actualStep, G4int subStepNo, G4double subStepSize, G4double dotVelocities)
virtual G4bool QuickAdvance(G4FieldTrack &y_val, const G4double dydx[], G4double hstep, G4double &dchord_step, G4double &dyerr) override
virtual G4bool DoesReIntegrate() const override
void PrintStatisticsReport()
G4double ComputeNewStepSize_WithinLimits(G4double errMaxNorm, G4double hstepCurrent)
void PrintStatus(const G4double *StartArr, G4double xstart, const G4double *CurrentArr, G4double xcurrent, G4double requestStep, G4int subStepNo)
G4double GetPgrow() const
virtual G4bool AccurateAdvance(G4FieldTrack &y_current, G4double hstep, G4double eps, G4double hinitial=0.0) override
void OneGoodStep(G4double ystart[], const G4double dydx[], G4double &x, G4double htry, G4double eps, G4double &hdid, G4double &hnext)
void WarnSmallStepSize(G4double hnext, G4double hstep, G4double h, G4double xDone, G4int noSteps)
void ReSetParameters(G4double new_safety=0.9)
virtual ~G4OldMagIntDriver() override
virtual void SetEquationOfMotion(G4EquationOfMotion *equation) override
G4double GetSafety() const
void SetSmallestFraction(G4double val)
void StreamInfo(std::ostream &os) const override
virtual G4double ComputeNewStepSize(G4double errMaxNorm, G4double hstepCurrent) override
virtual void GetDerivatives(const G4FieldTrack &y_curr, G4double dydx[]) const override
virtual G4EquationOfMotion * GetEquationOfMotion() override
virtual void RenewStepperAndAdjust(G4MagIntegratorStepper *pItsStepper) override
G4double GetPshrnk() const
virtual const G4MagIntegratorStepper * GetStepper() const override
G4OldMagIntDriver(G4double hminimum, G4MagIntegratorStepper *pItsStepper, G4int numberOfComponents=6, G4int statisticsVerbosity=0)
void WarnEndPointTooFar(G4double endPointDist, G4double hStepSize, G4double epsilonRelative, G4int debugFlag)
void WarnTooManyStep(G4double x1start, G4double x2end, G4double xCurrent)
static constexpr G4double max_stepping_decrease
static constexpr G4double max_stepping_increase