47 for (
auto idepth=0; idepth<max_depth+1; ++idepth )
49 ptrInterMedFT[ idepth ] =
new G4FieldTrack( zeroV, zeroV, 0., 0., 0., 0.);
63 for (
auto idepth=0; idepth<max_depth+1; ++idepth )
65 delete ptrInterMedFT[idepth];
120 G4bool& recalculatedEndPoint,
125 const char* MethodName=
"G4MultiLevelLocator::EstimateIntersectionPoint()";
127 G4bool found_approximate_intersection =
false;
128 G4bool there_is_no_intersection =
false;
130 G4FieldTrack CurrentA_PointVelocity = CurveStartPointVelocity;
131 G4FieldTrack CurrentB_PointVelocity = CurveEndPointVelocity;
133 G4bool validNormalAtE =
false;
136 G4FieldTrack ApproxIntersecPointV(CurveEndPointVelocity);
137 G4bool validIntersectP=
true;
139 G4bool last_AF_intersection =
false;
144 G4bool first_section =
true;
145 recalculatedEndPoint =
false;
146 G4bool restoredFullEndpoint =
false;
148 unsigned int substep_no = 0;
154 unsigned int trigger_substepno_print = 0;
155 const G4double tolerance = 1.0e-8 * CLHEP::mm;
156 unsigned int biggest_depth = 0;
163 endChangeB(
"EndPointB"), recApproxPoint(
"ApproxPoint"),
164 pointH_logger(
"Trial points 'E': position, normal");
165 unsigned int eventCount = 0;
172 recApproxPoint.clear();
173 pointH_logger.clear();
178 eventCount, CurrentA_PointVelocity );
180 eventCount, CurrentB_PointVelocity );
193 const G4int param_substeps = 5;
197 G4bool Second_half =
false;
205 unsigned int depth = 0;
213 substep_no, eventCount,
216 #if (G4DEBUG_FIELD>1)
218 if( (TrialPoint - StartPosition).mag2() <
sqr(tolerance))
221 tolerance, fNumCalls);
230 *ptrInterMedFT[0] = CurveEndPointVelocity;
231 for (
auto idepth=1; idepth<max_depth+1; ++idepth )
233 *ptrInterMedFT[idepth] = CurveStartPointVelocity;
239 for (
auto idepth=0; idepth<
max_depth; ++idepth )
241 fin_section_depth[idepth] =
true;
245 G4FieldTrack SubStart_PointVelocity = CurveStartPointVelocity;
249 unsigned int substep_no_p = 0;
250 G4bool sub_final_section =
false;
252 SubStart_PointVelocity = CurrentA_PointVelocity;
263 G4double distAB = (Point_B - Point_A).mag();
266 G4cerr <<
"ERROR> (Start) Point A coincides with or has gone past (end) point B"
267 <<
"MLL: iters = " << substep_no <<
G4endl;
269 G4cerr <<
" Difference = " << distAB - curv_lenAB
270 <<
" exceeds limit of relative dist (10*epsilon)= " << 10*
fiEpsilonStep
273 G4cerr <<
" Len A, B = " << lenA <<
" " << lenB <<
G4endl
274 <<
" Position A: " << Point_A <<
G4endl
275 <<
" Position B: " << Point_B <<
G4endl;
284 if( !validIntersectP ){
286 errmsg <<
"Assertion FAILURE - invalid (stale) Interection point. Substep: "
287 << substep_no <<
" call: " << fNumCalls <<
G4endl;
290 G4Exception(
"G4MultiLevelLocator::EstimateIntersectionPoint",
"GeomNav0004",
299 CurrentB_PointVelocity,
306 substep_no, eventCount, ApproxIntersecPointV ) );
308 G4double checkVsEnd= lenB - lenIntsc;
310 if( lenIntsc > lenB )
312 std::ostringstream errmsg;
313 errmsg.precision(17);
315 G4double ratioTol = std::fabs(ratio) / tolerance;
316 errmsg <<
"Intermediate F point is past end B point" <<
G4endl
317 <<
" l( intersection ) = " << lenIntsc <<
G4endl
318 <<
" l( endpoint ) = " << lenB <<
G4endl;
320 errmsg <<
" l_end - l_inters = " << checkVsEnd <<
G4endl
321 <<
" / l_end = " << ratio <<
G4endl
322 <<
" ratio / tolerance = " << ratioTol <<
G4endl;
335 G4ThreeVector ChordEF_Vector = CurrentF_Point - CurrentE_Point;
338 G4double MomDir_dot_Norm = NewMomentumDir.
dot( NormalAtEntry );
346 MomDir_dot_ABchord = (1.0 / ABchord_length)
347 * NewMomentumDir.
dot( ChordAB );
349 ChordEF_Vector, NewMomentumDir, NormalAtEntry, validNormalAtE );
350 G4cout <<
" dot( MomentumDir, ABchord_unit ) = "
351 << MomDir_dot_ABchord <<
G4endl;
355 ( MomDir_dot_Norm >= 0.0 )
356 || (! validNormalAtE );
361 found_approximate_intersection =
true;
365 IntersectedOrRecalculatedFT = ApproxIntersecPointV;
366 IntersectedOrRecalculatedFT.
SetPosition( CurrentE_Point );
375 CurrentE_Point, CurrentF_Point, MomentumDir,
376 last_AF_intersection, IP, NewSafety,
377 previousSafety, previousSftOrigin );
378 if ( goodCorrection )
380 IntersectedOrRecalculatedFT = ApproxIntersecPointV;
402 G4bool Intersects_FB =
false;
404 NewSafety, previousSafety,
408 last_AF_intersection = Intersects_AF;
413 CurrentB_PointVelocity = ApproxIntersecPointV;
414 CurrentE_Point = PointG;
416 validIntersectP =
true;
420 validNormalAtE = validNormalLast;
424 fin_section_depth[depth] =
false;
429 endChangeB.push_back(
431 substep_no, eventCount, CurrentB_PointVelocity) );
436 G4cout <<
"G4PiF::LI> Investigating intermediate point"
438 <<
" on way to full s="
458 NewSafety, previousSafety,
476 CurrentA_PointVelocity = ApproxIntersecPointV;
477 CurrentE_Point = PointH;
479 validIntersectP =
true;
483 validNormalAtE = validNormalH;
488 endChangeA.push_back(
490 substep_no, eventCount, CurrentA_PointVelocity) );
496 substep_no, eventCount, intersectH_pn );
501 validIntersectP =
false;
502 if( fin_section_depth[depth] )
512 if( ((Second_half)&&(depth==0)) || (first_section) )
514 there_is_no_intersection =
true;
520 substep_no_p = param_substeps+2;
525 sub_final_section =
true;
530 CurrentA_PointVelocity = CurrentB_PointVelocity;
531 CurrentB_PointVelocity = (depth==0) ? CurveEndPointVelocity
532 : *ptrInterMedFT[depth] ;
533 SubStart_PointVelocity = CurrentA_PointVelocity;
534 restoredFullEndpoint =
true;
536 validIntersectP =
false;
541 endChangeA.push_back(
544 substep_no, eventCount, CurrentA_PointVelocity) );
545 endChangeB.push_back(
548 substep_no, eventCount, CurrentB_PointVelocity) );
554 G4int errorEndPt = 0;
556 G4bool recalculatedB=
false;
557 if( driverReIntegrates )
561 CurrentB_PointVelocity,
566 CurrentB_PointVelocity = RevisedB_FT;
573 if ( (fin_section_depth[depth])
574 &&( first_section || ((Second_half)&&(depth==0)) ) )
576 recalculatedEndPoint =
true;
577 IntersectedOrRecalculatedFT = RevisedB_FT;
589 endChangeB.push_back(
591 substep_no, eventCount, RevisedB_FT ) );
602 std::ostringstream errmsg;
605 CurveStartPointVelocity, CurveEndPointVelocity,
607 CurrentA_PointVelocity, CurrentB_PointVelocity,
608 SubStart_PointVelocity, CurrentE_Point,
609 ApproxIntersecPointV, substep_no, substep_no_p, depth);
615 errmsg <<
G4endl <<
" * Location: " << MethodName
616 <<
"- After EndIf(Intersects_AF)" <<
G4endl;
617 errmsg <<
" * Bool flags: Recalculated = " << recalculatedB
618 <<
" Intersects_AF = " << Intersects_AF
619 <<
" Intersects_FB = " << Intersects_FB <<
G4endl;
620 errmsg <<
" * Number of calls to MLL:EIP= " << fNumCalls <<
G4endl;
623 if( restoredFullEndpoint )
625 fin_section_depth[depth] = restoredFullEndpoint;
626 restoredFullEndpoint =
false;
632 if( trigger_substepno_print == 0)
634 trigger_substepno_print= fWarnSteps - 20;
637 if( substep_no >= trigger_substepno_print )
639 G4cout <<
"Difficulty in converging in " << MethodName
640 <<
" Substep no = " << substep_no <<
G4endl;
641 if( substep_no == trigger_substepno_print )
644 printStatus( CurveStartPointVelocity, CurveEndPointVelocity,
645 -1.0, NewSafety, 0 );
649 G4cout <<
" ** For more information enable 'check mode' in G4MultiLevelLocator "
650 <<
"-- (it saves and can output change records) " <<
G4endl;
654 printStatus( CurrentA_PointVelocity, CurrentA_PointVelocity,
655 -1.0, NewSafety, substep_no-1 );
657 printStatus( CurrentA_PointVelocity, CurrentB_PointVelocity,
658 -1.0, NewSafety, substep_no );
664 }
while ( ( ! found_approximate_intersection )
665 && ( ! there_is_no_intersection )
667 && ( substep_no_p <= param_substeps) );
670 if( (!found_approximate_intersection) && (!there_is_no_intersection) )
682 if( (did_len < fraction_done*all_len)
683 && (depth<
max_depth) && (!sub_final_section) )
687 biggest_depth = std::max(depth, biggest_depth);
692 first_section =
false;
694 G4double Sub_len = (all_len-did_len)/(2.);
697 integrDriver->AccurateAdvance(midPoint, Sub_len,
fiEpsilonStep);
700 if( fullAdvance ) { ++fNumAdvanceFull; }
705 const G4double adequateFraction = (1.0-CLHEP::perThousand);
706 G4bool goodAdvance = (lenAchieved >= adequateFraction * Sub_len);
707 if ( goodAdvance ) { ++fNumAdvanceGood; }
712 G4cout <<
"MLL> AdvanceChordLimited not full at depth=" << depth
713 <<
" total length achieved = " << lenAchieved <<
" of "
714 << Sub_len <<
" fraction= ";
715 if (Sub_len != 0.0 ) {
G4cout << lenAchieved / Sub_len; }
716 else {
G4cout <<
"DivByZero"; }
717 G4cout <<
" Good-enough fraction = " << adequateFraction;
718 G4cout <<
" Number of call to mll = " << fNumCalls
719 <<
" iteration " << substep_no
720 <<
" inner = " << substep_no_p <<
G4endl;
722 G4cout <<
" State at start is = " << CurrentA_PointVelocity
724 <<
" at end (midpoint)= " << midPoint <<
G4endl;
728 ReportFieldValue( CurrentA_PointVelocity,
"start", equation );
729 ReportFieldValue( midPoint,
"midPoint", equation );
730 G4cout <<
" Original Start = "
731 << CurveStartPointVelocity <<
G4endl;
732 G4cout <<
" Original End = "
733 << CurveEndPointVelocity <<
G4endl;
734 G4cout <<
" Original TrialPoint = "
736 G4cout <<
" (this is STRICT mode) "
737 <<
" num Splits= " << numSplits;
742 *ptrInterMedFT[depth] = midPoint;
743 CurrentB_PointVelocity = midPoint;
748 endChangeB.push_back(
750 substep_no, eventCount, midPoint) );
755 SubStart_PointVelocity = CurrentA_PointVelocity;
765 NewSafety, previousSafety,
766 previousSftOrigin, distAB,
770 last_AF_intersection = Intersects_AB;
771 CurrentE_Point = PointGe;
772 fin_section_depth[depth] =
true;
774 validIntersectP =
true;
778 validNormalAtE = validNormalAB;
787 validIntersectP=
false;
791 G4bool unfinished = Second_half;
792 while ( unfinished && (depth>0) )
799 SubStart_PointVelocity = *ptrInterMedFT[depth];
800 CurrentA_PointVelocity = *ptrInterMedFT[depth];
801 CurrentB_PointVelocity = *ptrInterMedFT[depth-1];
807 substep_no, eventCount, CurrentA_PointVelocity);
808 endChangeA.push_back( chngPop_a );
810 substep_no, eventCount, CurrentB_PointVelocity);
811 endChangeB.push_back( chngPop_b );
817 G4int errorEndPt = -1;
818 G4bool recalculatedB=
false;
819 if( driverReIntegrates )
824 G4FieldTrack RevisedEndPointFT = CurrentB_PointVelocity;
827 CurrentB_PointVelocity,
832 CurrentB_PointVelocity = RevisedEndPointFT;
836 recalculatedEndPoint =
true;
837 IntersectedOrRecalculatedFT = RevisedEndPointFT;
850 endChangeB.push_back(
852 substep_no, eventCount, RevisedEndPointFT));
857 std::ostringstream errmsg;
859 CurveStartPointVelocity, CurveEndPointVelocity,
861 CurrentA_PointVelocity, CurrentB_PointVelocity,
862 SubStart_PointVelocity, CurrentE_Point,
863 ApproxIntersecPointV, substep_no, substep_no_p, depth);
864 errmsg <<
" * Location: " << MethodName <<
"- Second-Half" <<
G4endl;
865 errmsg <<
" * Recalculated = " << recalculatedB <<
G4endl;
875 previousSftOrigin, distAB,
879 last_AF_intersection = Intersects_AB;
880 CurrentE_Point = PointGi;
882 validIntersectP =
true;
887 validIntersectP =
false;
890 there_is_no_intersection =
true;
894 fin_section_depth[depth] =
true;
895 unfinished = !validIntersectP;
898 if( ! ( validIntersectP || there_is_no_intersection ) )
901 G4cout <<
"MLL - WARNING Potential FAILURE: Conditions not met!"
903 <<
" Depth = " << depth <<
G4endl
904 <<
" Num Substeps= " << substep_no <<
G4endl;
905 G4cout <<
" Found intersection= " << found_approximate_intersection
909 CurveStartPointVelocity, CurveEndPointVelocity,
910 substep_no, CurrentA_PointVelocity,
911 CurrentB_PointVelocity,
918 assert( validIntersectP || there_is_no_intersection
919 || found_approximate_intersection);
921 }
while ( ( ! found_approximate_intersection )
922 && ( ! there_is_no_intersection )
923 && ( substep_no <= fMaxSteps) );
925 if( substep_no > max_no_seen )
927 max_no_seen = substep_no;
929 if( max_no_seen > fWarnSteps )
931 trigger_substepno_print = max_no_seen-20;
936 if( !there_is_no_intersection && !found_approximate_intersection )
938 if( substep_no >= fMaxSteps)
942 recalculatedEndPoint =
true;
943 IntersectedOrRecalculatedFT = CurrentA_PointVelocity;
944 found_approximate_intersection =
false;
946 std::ostringstream message;
948 message <<
"Convergence is requiring too many substeps: "
949 << substep_no <<
" ( limit = "<< fMaxSteps <<
")"
951 <<
" Abandoning effort to intersect. " <<
G4endl <<
G4endl;
952 message <<
" Number of calls to MLL: " << fNumCalls;
953 message <<
" iteration = " << substep_no <<
G4endl <<
G4endl;
955 message.precision( 12 );
958 message <<
" Undertaken only length: " << done_len
959 <<
" out of " << full_len <<
" required." <<
G4endl
960 <<
" Remaining length = " << full_len - done_len;
962 message <<
" Start and end-point of requested Step:" <<
G4endl;
963 printStatus( CurveStartPointVelocity, CurveEndPointVelocity,
964 -1.0, NewSafety, 0, message, -1 );
965 message <<
" Start and end-point of current Sub-Step:" <<
G4endl;
966 printStatus( CurrentA_PointVelocity, CurrentA_PointVelocity,
967 -1.0, NewSafety, substep_no-1, message, -1 );
968 printStatus( CurrentA_PointVelocity, CurrentB_PointVelocity,
969 -1.0, NewSafety, substep_no, message, -1 );
973 else if( substep_no >= fWarnSteps)
975 std::ostringstream message;
976 message <<
"Many substeps while trying to locate intersection."
978 <<
" Undertaken length: "
980 <<
" - Needed: " << substep_no <<
" substeps." <<
G4endl
981 <<
" Warning number = " << fWarnSteps
982 <<
" and maximum substeps = " << fMaxSteps;
987 return (!there_is_no_intersection) && found_approximate_intersection;
994 G4cout <<
" Number of split level ('advances'): "
995 << fNumAdvanceTrials <<
G4endl;
996 G4cout <<
" Number of full advances: "
997 << fNumAdvanceGood <<
G4endl;
998 G4cout <<
" Number of good advances: "
999 << fNumAdvanceFull <<
G4endl;
1002void G4MultiLevelLocator::ReportFieldValue(
const G4FieldTrack& locationPV,
1003 const char* nameLoc,
1006 enum { maxNumFieldComp = 24 };
1011 G4double FieldVec[maxNumFieldComp];
1012 for (
auto i=0; i<maxNumFieldComp; ++i )
1017 G4cout <<
" B-field value (" << nameLoc <<
")= "
1018 << FieldVec[0] <<
" " << FieldVec[1] <<
" " << FieldVec[2];
1021 FieldVec[5] ).
mag2();
1024 G4cout <<
" Electric = " << FieldVec[3] <<
" "
1025 << FieldVec[4] <<
" "
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
CLHEP::Hep3Vector G4ThreeVector
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
double dot(const Hep3Vector &) const
G4FieldTrack ApproxCurvePointV(const G4FieldTrack &curveAPointVelocity, const G4FieldTrack &curveBPointVelocity, const G4ThreeVector ¤tEPoint, G4double epsStep)
G4VIntegrationDriver * GetIntegrationDriver()
void GetFieldValue(const G4double Point[4], G4double Field[]) const
const G4ThreeVector & GetMomentumDir() const
void SetMomentum(G4ThreeVector nMomDir)
G4double GetCurveLength() const
G4ThreeVector GetMomentumDirection() const
G4ThreeVector GetPosition() const
void SetPosition(G4ThreeVector nPos)
G4double GetRestMass() const
G4double GetLabTimeOfFlight() const
static std::ostream & ReportEndChanges(std::ostream &os, const G4LocatorChangeLogger &startA, const G4LocatorChangeLogger &endB)
static std::ostream & ReportEndChanges(std::ostream &os, const std::vector< G4LocatorChangeRecord > &startA, const std::vector< G4LocatorChangeRecord > &endB)
G4bool EstimateIntersectionPoint(const G4FieldTrack &curveStartPointTangent, const G4FieldTrack &curveEndPointTangent, const G4ThreeVector &trialPoint, G4FieldTrack &intersectPointTangent, G4bool &recalculatedEndPoint, G4double &fPreviousSafety, G4ThreeVector &fPreviousSftOrigin)
G4MultiLevelLocator(G4Navigator *theNavigator)
void SetMaxSteps(unsigned int valMax)
void SetWarnSteps(unsigned int valWarn)
virtual void LocateGlobalPointWithinVolume(const G4ThreeVector &position)
virtual G4bool DoesReIntegrate() const =0
G4double fiDeltaIntersection
G4Navigator * GetNavigatorFor()
G4ChordFinder * GetChordFinderFor()
G4ThreeVector GetSurfaceNormal(const G4ThreeVector &CurrentInt_Point, G4bool &validNormal)
void ReportTrialStep(G4int step_no, const G4ThreeVector &ChordAB_v, const G4ThreeVector &ChordEF_v, const G4ThreeVector &NewMomentumDir, const G4ThreeVector &NormalAtEntry, G4bool validNormal)
G4bool IntersectChord(const G4ThreeVector &StartPointA, const G4ThreeVector &EndPointB, G4double &NewSafety, G4double &PreviousSafety, G4ThreeVector &PreviousSftOrigin, G4double &LinearStepLength, G4ThreeVector &IntersectionPoint, G4bool *calledNavigator=nullptr)
G4bool CheckAndReEstimateEndpoint(const G4FieldTrack &CurrentStartA, const G4FieldTrack &EstimatedEndB, G4FieldTrack &RevisedEndPoint, G4int &errorCode)
void ReportProgress(std::ostream &oss, const G4FieldTrack &StartPointVel, const G4FieldTrack &EndPointVel, G4int substep_no, const G4FieldTrack &A_PtVel, const G4FieldTrack &B_PtVel, G4double safetyLast, G4int depth=-1)
G4double GetEpsilonStepFor()
void ReportImmediateHit(const char *MethodName, const G4ThreeVector &StartPosition, const G4ThreeVector &TrialPoint, G4double tolerance, unsigned long int numCalls)
G4bool GetAdjustementOfFoundIntersection()
void printStatus(const G4FieldTrack &startFT, const G4FieldTrack ¤tFT, G4double requestStep, G4double safety, G4int stepNum)
G4bool AdjustmentOfFoundIntersection(const G4ThreeVector &A, const G4ThreeVector &CurrentE_Point, const G4ThreeVector &CurrentF_Point, const G4ThreeVector &MomentumDir, const G4bool IntersectAF, G4ThreeVector &IntersectionPoint, G4double &NewSafety, G4double &fPrevSafety, G4ThreeVector &fPrevSftOrigin)
void ReportReversedPoints(std::ostringstream &ossMsg, const G4FieldTrack &StartPointVel, const G4FieldTrack &EndPointVel, G4double NewSafety, G4double epsStep, const G4FieldTrack &CurrentA_PointVelocity, const G4FieldTrack &CurrentB_PointVelocity, const G4FieldTrack &SubStart_PointVelocity, const G4ThreeVector &CurrentE_Point, const G4FieldTrack &ApproxIntersecPointV, G4int sbstp_no, G4int sbstp_no_p, G4int depth)