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;
238 G4bool fin_section_depth[max_depth];
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;
261 G4cerr <<
"ERROR> (Start) Point A coincides with or has gone past (end) point B"
262 <<
"MLL: iters = " << substep_no <<
G4endl;
276 CurrentB_PointVelocity,
283 substep_no, eventCount, ApproxIntersecPointV ) );
286 G4double checkVsEnd= lenB - lenIntsc;
288 if( lenIntsc > lenB )
290 std::ostringstream errmsg;
291 errmsg.precision(17);
293 G4double ratioTol = std::fabs(ratio) / tolerance;
294 errmsg <<
"Intermediate F point is past end B point" <<
G4endl
295 <<
" l( intersection ) = " << lenIntsc <<
G4endl
296 <<
" l( endpoint ) = " << lenB <<
G4endl;
298 errmsg <<
" l_end - l_inters = " << checkVsEnd <<
G4endl
299 <<
" / l_end = " << ratio <<
G4endl
300 <<
" ratio / tolerance = " << ratioTol <<
G4endl;
313 G4ThreeVector ChordEF_Vector = CurrentF_Point - CurrentE_Point;
316 G4double MomDir_dot_Norm = NewMomentumDir.
dot( NormalAtEntry );
324 MomDir_dot_ABchord = (1.0 / ABchord_length)
325 * NewMomentumDir.
dot( ChordAB );
327 ChordEF_Vector, NewMomentumDir, NormalAtEntry, validNormalAtE );
328 G4cout <<
" dot( MomentumDir, ABchord_unit ) = "
329 << MomDir_dot_ABchord <<
G4endl;
333 ( MomDir_dot_Norm >= 0.0 )
334 || (! validNormalAtE );
339 found_approximate_intersection =
true;
343 IntersectedOrRecalculatedFT = ApproxIntersecPointV;
344 IntersectedOrRecalculatedFT.
SetPosition( CurrentE_Point );
353 CurrentE_Point, CurrentF_Point, MomentumDir,
354 last_AF_intersection, IP, NewSafety,
355 previousSafety, previousSftOrigin );
356 if ( goodCorrection )
358 IntersectedOrRecalculatedFT = ApproxIntersecPointV;
380 G4bool Intersects_FB =
false;
382 NewSafety, previousSafety,
386 last_AF_intersection = Intersects_AF;
391 CurrentB_PointVelocity = ApproxIntersecPointV;
392 CurrentE_Point = PointG;
394 validIntersectP =
true;
398 validNormalAtE = validNormalLast;
402 fin_section_depth[depth] =
false;
407 endChangeB.push_back(
409 substep_no, eventCount, CurrentB_PointVelocity) );
414 G4cout <<
"G4PiF::LI> Investigating intermediate point"
416 <<
" on way to full s="
436 NewSafety, previousSafety,
454 CurrentA_PointVelocity = ApproxIntersecPointV;
455 CurrentE_Point = PointH;
457 validIntersectP =
true;
461 validNormalAtE = validNormalH;
466 endChangeA.push_back(
468 substep_no, eventCount, CurrentA_PointVelocity) );
474 substep_no, eventCount, intersectH_pn );
479 if( fin_section_depth[depth] )
489 if( ((Second_half)&&(depth==0)) || (first_section) )
491 there_is_no_intersection =
true;
497 substep_no_p = param_substeps+2;
502 sub_final_section =
true;
507 CurrentA_PointVelocity = CurrentB_PointVelocity;
508 CurrentB_PointVelocity = (depth==0) ? CurveEndPointVelocity
509 : *ptrInterMedFT[depth] ;
510 SubStart_PointVelocity = CurrentA_PointVelocity;
511 restoredFullEndpoint =
true;
513 validIntersectP =
false;
518 endChangeA.push_back(
521 substep_no, eventCount, CurrentA_PointVelocity) );
522 endChangeB.push_back(
525 substep_no, eventCount, CurrentB_PointVelocity) );
531 G4int errorEndPt = 0;
533 G4bool recalculatedB=
false;
534 if( driverReIntegrates )
538 CurrentB_PointVelocity,
543 CurrentB_PointVelocity = RevisedB_FT;
550 if ( (fin_section_depth[depth])
551 &&( first_section || ((Second_half)&&(depth==0)) ) )
553 recalculatedEndPoint =
true;
554 IntersectedOrRecalculatedFT = RevisedB_FT;
571 endChangeB.push_back(
573 substep_no, eventCount, RevisedB_FT ) );
584 std::ostringstream errmsg;
586 CurveStartPointVelocity, CurveEndPointVelocity,
588 CurrentA_PointVelocity, CurrentB_PointVelocity,
589 SubStart_PointVelocity, CurrentE_Point,
590 ApproxIntersecPointV, substep_no, substep_no_p, depth);
591 errmsg <<
G4endl <<
" * Location: " << MethodName
592 <<
"- After EndIf(Intersects_AF)" <<
G4endl;
593 errmsg <<
" * Bool flags: Recalculated = " << recalculatedB
594 <<
" Intersects_AF = " << Intersects_AF
595 <<
" Intersects_FB = " << Intersects_FB <<
G4endl;
596 errmsg <<
" * Number of calls to MLL:EIP= " << fNumCalls <<
G4endl;
599 if( restoredFullEndpoint )
601 fin_section_depth[depth] = restoredFullEndpoint;
602 restoredFullEndpoint =
false;
608 if( trigger_substepno_print == 0)
610 trigger_substepno_print= fWarnSteps - 20;
613 if( substep_no >= trigger_substepno_print )
615 G4cout <<
"Difficulty in converging in " << MethodName
616 <<
" Substep no = " << substep_no <<
G4endl;
617 if( substep_no == trigger_substepno_print )
620 printStatus( CurveStartPointVelocity, CurveEndPointVelocity,
621 -1.0, NewSafety, 0 );
624 G4cout <<
"endPoints A (start) and B (end): combined changes of AB intervals" <<
G4endl;
628 printStatus( CurrentA_PointVelocity, CurrentA_PointVelocity,
629 -1.0, NewSafety, substep_no-1 );
631 printStatus( CurrentA_PointVelocity, CurrentB_PointVelocity,
632 -1.0, NewSafety, substep_no );
638 }
while ( ( ! found_approximate_intersection )
639 && ( ! there_is_no_intersection )
640 && ( substep_no_p <= param_substeps) );
643 if( (!found_approximate_intersection) && (!there_is_no_intersection) )
655 if( (did_len < fraction_done*all_len)
656 && (depth<max_depth) && (!sub_final_section) )
660 biggest_depth = std::max(depth, biggest_depth);
665 first_section =
false;
667 G4double Sub_len = (all_len-did_len)/(2.);
670 integrDriver->AccurateAdvance(midPoint, Sub_len,
fiEpsilonStep);
673 if( fullAdvance ) { ++fNumAdvanceFull; }
678 const G4double adequateFraction = (1.0-CLHEP::perThousand);
679 G4bool goodAdvance = (lenAchieved >= adequateFraction * Sub_len);
680 if ( goodAdvance ) { ++fNumAdvanceGood; }
685 G4cout <<
"MLL> AdvanceChordLimited not full at depth=" << depth
686 <<
" total length achieved = " << lenAchieved <<
" of "
687 << Sub_len <<
" fraction= ";
688 if (Sub_len != 0.0 ) {
G4cout << lenAchieved / Sub_len; }
689 else {
G4cout <<
"DivByZero"; }
690 G4cout <<
" Good-enough fraction = " << adequateFraction;
691 G4cout <<
" Number of call to mll = " << fNumCalls
692 <<
" iteration " << substep_no
693 <<
" inner = " << substep_no_p <<
G4endl;
695 G4cout <<
" State at start is = " << CurrentA_PointVelocity
697 <<
" at end (midpoint)= " << midPoint <<
G4endl;
701 ReportFieldValue( CurrentA_PointVelocity,
"start", equation );
702 ReportFieldValue( midPoint,
"midPoint", equation );
703 G4cout <<
" Original Start = "
704 << CurveStartPointVelocity <<
G4endl;
705 G4cout <<
" Original End = "
706 << CurveEndPointVelocity <<
G4endl;
707 G4cout <<
" Original TrialPoint = "
709 G4cout <<
" (this is STRICT mode) "
710 <<
" num Splits= " << numSplits;
715 *ptrInterMedFT[depth] = midPoint;
716 CurrentB_PointVelocity = midPoint;
721 endChangeB.push_back(
723 substep_no, eventCount, midPoint) );
728 SubStart_PointVelocity = CurrentA_PointVelocity;
738 NewSafety, previousSafety,
739 previousSftOrigin, distAB,
743 last_AF_intersection = Intersects_AB;
744 CurrentE_Point = PointGe;
745 fin_section_depth[depth] =
true;
747 validIntersectP =
true;
751 validNormalAtE = validNormalAB;
760 validIntersectP=
false;
764 unsigned int levelPops = 0;
766 G4bool unfinished = Second_half;
767 while ( unfinished && (depth>0) )
776 SubStart_PointVelocity = *ptrInterMedFT[depth];
777 CurrentA_PointVelocity = *ptrInterMedFT[depth];
778 CurrentB_PointVelocity = *ptrInterMedFT[depth-1];
784 substep_no, eventCount, CurrentA_PointVelocity);
785 endChangeA.push_back( chngPop_a );
787 substep_no, eventCount, CurrentB_PointVelocity);
788 endChangeB.push_back( chngPop_b );
794 G4int errorEndPt = -1;
795 G4bool recalculatedB=
false;
796 if( driverReIntegrates )
801 G4FieldTrack RevisedEndPointFT = CurrentB_PointVelocity;
804 CurrentB_PointVelocity,
809 CurrentB_PointVelocity = RevisedEndPointFT;
813 recalculatedEndPoint =
true;
814 IntersectedOrRecalculatedFT = RevisedEndPointFT;
827 endChangeB.push_back(
829 substep_no, eventCount, RevisedEndPointFT));
834 std::ostringstream errmsg;
836 CurveStartPointVelocity, CurveEndPointVelocity,
838 CurrentA_PointVelocity, CurrentB_PointVelocity,
839 SubStart_PointVelocity, CurrentE_Point,
840 ApproxIntersecPointV, substep_no, substep_no_p, depth);
841 errmsg <<
" * Location: " << MethodName <<
"- Second-Half" <<
G4endl;
842 errmsg <<
" * Recalculated = " << recalculatedB <<
G4endl;
852 previousSftOrigin, distAB,
856 last_AF_intersection = Intersects_AB;
857 CurrentE_Point = PointGi;
859 validIntersectP =
true;
864 validIntersectP =
false;
867 there_is_no_intersection =
true;
871 fin_section_depth[depth] =
true;
872 unfinished = !validIntersectP;
875 if( ! ( validIntersectP || there_is_no_intersection ) )
878 G4cout <<
"MLL - WARNING Potential FAILURE: Conditions not met!"
880 <<
" Depth = " << depth <<
G4endl
881 <<
" Levels popped = " << levelPops
882 <<
" Num Substeps= " << substep_no <<
G4endl;
883 G4cout <<
" Found intersection= " << found_approximate_intersection
887 CurveStartPointVelocity, CurveEndPointVelocity,
888 substep_no, CurrentA_PointVelocity,
889 CurrentB_PointVelocity,
896 assert( validIntersectP || there_is_no_intersection
897 || found_approximate_intersection);
899 }
while ( ( ! found_approximate_intersection )
900 && ( ! there_is_no_intersection )
901 && ( substep_no <= fMaxSteps) );
903 if( substep_no > max_no_seen )
905 max_no_seen = substep_no;
907 if( max_no_seen > fWarnSteps )
909 trigger_substepno_print = max_no_seen-20;
914 if( !there_is_no_intersection && !found_approximate_intersection )
916 if( substep_no >= fMaxSteps)
920 recalculatedEndPoint =
true;
921 IntersectedOrRecalculatedFT = CurrentA_PointVelocity;
922 found_approximate_intersection =
false;
924 std::ostringstream message;
926 message <<
"Convergence is requiring too many substeps: "
927 << substep_no <<
" ( limit = "<< fMaxSteps <<
")"
929 <<
" Abandoning effort to intersect. " <<
G4endl <<
G4endl;
930 message <<
" Number of calls to MLL: " << fNumCalls;
931 message <<
" iteration = " << substep_no <<
G4endl <<
G4endl;
933 message.precision( 12 );
936 message <<
" Undertaken only length: " << done_len
937 <<
" out of " << full_len <<
" required." <<
G4endl
938 <<
" Remaining length = " << full_len - done_len;
940 message <<
" Start and end-point of requested Step:" <<
G4endl;
941 printStatus( CurveStartPointVelocity, CurveEndPointVelocity,
942 -1.0, NewSafety, 0, message, -1 );
943 message <<
" Start and end-point of current Sub-Step:" <<
G4endl;
944 printStatus( CurrentA_PointVelocity, CurrentA_PointVelocity,
945 -1.0, NewSafety, substep_no-1, message, -1 );
946 printStatus( CurrentA_PointVelocity, CurrentB_PointVelocity,
947 -1.0, NewSafety, substep_no, message, -1 );
951 else if( substep_no >= fWarnSteps)
953 std::ostringstream message;
954 message <<
"Many substeps while trying to locate intersection."
956 <<
" Undertaken length: "
958 <<
" - Needed: " << substep_no <<
" substeps." <<
G4endl
959 <<
" Warning number = " << fWarnSteps
960 <<
" and maximum substeps = " << fMaxSteps;
965 return (!there_is_no_intersection) && found_approximate_intersection;
972 G4cout <<
" Number of split level ('advances'): "
973 << fNumAdvanceTrials <<
G4endl;
974 G4cout <<
" Number of full advances: "
975 << fNumAdvanceGood <<
G4endl;
976 G4cout <<
" Number of good advances: "
977 << fNumAdvanceFull <<
G4endl;
980void G4MultiLevelLocator::ReportFieldValue(
const G4FieldTrack& locationPV,
984 enum { maxNumFieldComp = 24 };
990 for (
auto i=0; i<maxNumFieldComp; ++i )
995 G4cout <<
" B-field value (" << nameLoc <<
")= "
996 << FieldVec[0] <<
" " << FieldVec[1] <<
" " << FieldVec[2];
999 FieldVec[5] ).
mag2();
1002 G4cout <<
" Electric = " << FieldVec[3] <<
" "
1003 << FieldVec[4] <<
" "
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
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)