63 #define DEBUG_MEM_STEPPING
64 #define DEBUG_MEM_DETAILED_STEPPING
69 using namespace G4MemStat;
77 #define RED "\033[0;31m"
78 #define LIGHT_RED "\33[1;31m"
79 #define GREEN "\033[32;40m"
80 #define GREEN_ON_BLUE "\033[1;32;44m"
81 #define RESET_COLOR "\033[0m"
86 #define GREEN_ON_BLUE ""
87 #define RESET_COLOR ""
97 return std::numeric_limits<T>::has_infinity
98 && value == std::numeric_limits<T>::infinity();
104 if(fgScheduler == 0) fgScheduler =
new G4Scheduler();
115 G4cout <<
"G4Scheduler received G4State_Quit" <<
G4endl;
133 G4Scheduler::G4Scheduler() :
140 void G4Scheduler::Create()
142 fUseDefaultTimeSteps =
true;
143 fUserUpperTimeLimit = -1;
148 fpTrackingInteractivity = 0;
160 fMaxNZeroTimeStepsAllowed = 10;
166 fInteractionStep =
true;
167 fUsePreDefinedTimeSteps =
false;
172 fpModelProcessor = 0;
178 fInitialized =
false;
180 fpUserTimeStepAction = 0;
185 fWhyDoYouStop =
false;
186 fDefinedMinTimeStep = -1.;
187 fReachedUserTimeLimit =
false;
189 fTmpGlobalTime = -1.;
228 delete fpStepProcessor;
233 delete fpModelProcessor;
234 fpModelProcessor = 0;
239 if(fpTrackingManager)
241 delete fpTrackingManager;
242 fpTrackingManager = 0;
253 delete fpModelHandler;
269 fTrackContainer.
Clear();
286 delete fpStepProcessor;
290 delete fpModelProcessor;
319 if(fUsePreDefinedTimeSteps)
321 if(fpUserTimeSteps == 0)
325 <<
"You are asking to use user defined steps but you did not give any.";
326 G4Exception(
"G4Scheduler::FindUserPreDefinedTimeStep",
329 exceptionDescription);
360 fUserUpperTimeLimit = -1;
366 fInteractionStep =
true;
383 G4cout <<
"*** G4Scheduler starts processing " <<
G4endl;
385 G4cout <<
"___________________________________________"
386 "___________________________" <<
G4endl;
402 if (fpTrackingInteractivity) fpTrackingInteractivity->
Initialize();
411 G4bool trackFound =
false;
437 G4cout <<
"G4Scheduler: process time= "<< localtimer <<
G4endl;
461 G4cout <<
"*** G4Scheduler ends at time : "
463 G4cout <<
"___________________________________" <<
G4endl;
467 G4cout <<
"*** G4Scheduler did not start because no "
468 "track was found to be processed"<<
G4endl;
469 G4cout <<
"___________________________________" <<
G4endl;
476 if (fpUserTimeStepAction) fpUserTimeStepAction->
EndProcessing();
484 if (fpTrackingInteractivity) fpTrackingInteractivity->
Finalize();
491 std::set<double>::const_iterator up = fWatchedTimes.upper_bound(fGlobalTime);
492 if(up == fWatchedTimes.end())
return DBL_MAX;
519 fTmpGlobalTime = fGlobalTime;
523 G4double tmpGlobalTime = fGlobalTime;
525 double nextWatchedTime = -1;
535 fStopTime =
min(nextWatchedTime, fEndTime);
541 if(nextWatchedTime > fEndTime && carryOn)
553 return fGlobalTime < fEndTime && (fMaxSteps == -1 ?
true : fNbSteps < fMaxSteps)
554 && fContinue ==
true;
564 G4cout <<
"G4Scheduler has reached a stage: it might be"
565 " a transition or the end"
568 G4bool normalStop =
false;
570 if(fGlobalTime >= fStopTime)
572 G4cout <<
"== G4Scheduler: I stop because I reached the stop time : "
578 G4cout <<
"G4Scheduler: I stop because the current main list of tracks "
583 if(fMaxSteps == -1 ?
false : fNbSteps >= fMaxSteps)
585 G4cout <<
"G4Scheduler: I stop because I reached the maximum allowed "
586 "number of steps=" << fMaxSteps
590 if(fContinue && normalStop ==
false)
592 G4cout <<
"G4Scheduler: It might be that I stop because "
593 "I have been told so. You may check "
594 "member fContinue and usage of the method G4Scheduler::Stop()."
606 if(fpUserTimeStepAction) fpUserTimeStepAction->
NewStage();
608 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
609 MemStat mem_first, mem_second, mem_diff;
612 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
616 while (fGlobalTime < fStopTime
618 && (fMaxSteps == -1 ?
true : fNbSteps < fMaxSteps)
619 && fContinue ==
true)
626 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
628 mem_diff = mem_second-mem_first;
629 G4cout <<
"\t || MEM || After step " << fNbSteps <<
", diff is : "
636 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
638 mem_diff = mem_second-mem_first;
639 G4cout <<
"\t || MEM || After stepping, diff is : " << mem_diff <<
G4endl;
644 G4cout <<
"*** G4Scheduler has finished processing a track list at time : "
652 fTimeStep = fMaxTimeStep;
657 fInteractionStep =
false;
658 fReachedUserTimeLimit =
false;
669 G4cout <<
"*** Start Of Step N°" << fNbSteps + 1 <<
" ***" <<
G4endl;
678 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
679 MemStat mem_first, mem_second, mem_diff;
682 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
688 if (fUsePreDefinedTimeSteps)
697 <<
" the chosen user time step is : "
709 fDefinedMinTimeStep);
712 else if(fUseDefaultTimeSteps)
714 fTSTimeStep = fDefinedMinTimeStep;
717 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
719 mem_diff = mem_second-mem_first;
720 G4cout <<
"|| MEM || After computing TS, diff is : " << mem_diff <<
G4endl;
737 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
751 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
753 mem_diff = mem_second-mem_first;
754 G4cout <<
"|| MEM || After IL, diff is : " << mem_diff <<
G4endl;
763 G4cout <<
"*** The minimum time returned by the processes is : "
771 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
775 if (fILTimeStep <= fTSTimeStep)
778 fInteractionStep =
true;
780 fTimeStep = fILTimeStep;
786 fInteractionStep =
false;
788 fTimeStep = fTSTimeStep;
792 if (fGlobalTime + fTimeStep > fStopTime)
795 fTimeStep = fStopTime - fGlobalTime;
797 fInteractionStep =
true;
805 if (fZeroTimeCount >= fMaxNZeroTimeStepsAllowed)
809 exceptionDescription <<
"Too many zero time steps were detected. ";
810 exceptionDescription <<
"The simulation is probably stuck. ";
812 <<
"The maximum number of zero time steps is currently : "
813 << fMaxNZeroTimeStepsAllowed;
814 exceptionDescription <<
".";
817 "SchedulerNullTimeSteps",
819 exceptionDescription);
827 fReachedUserTimeLimit =
828 ((fTimeStep <= fDefinedMinTimeStep) || ((fTimeStep > fDefinedMinTimeStep)
829 && fabs(fTimeStep - fDefinedMinTimeStep) < fTimeTolerance)) ?
835 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
837 mem_diff = mem_second-mem_first;
838 G4cout <<
"|| MEM || After LeadingTracks and UserPreTimeStepAction: "
842 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
847 fGlobalTime += fTimeStep;
852 if (fTSTimeStep > 0 || fILTimeStep <= fTSTimeStep)
855 fpStepProcessor->
DoIt(fTimeStep);
865 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
867 mem_diff = mem_second-mem_first;
868 G4cout <<
"|| MEM || After DoIT, diff is : " << mem_diff <<
G4endl;
871 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
879 fReachedUserTimeLimit,
881 fpUserTimeStepAction,
886 if (fpUserTimeStepAction)
891 fPreviousTimeStep = fTimeStep;
893 #if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
895 mem_diff = mem_second-mem_first;
896 G4cout <<
"|| MEM || After computing reactions + UserPostTimeStepAction, "
897 "diff is : " << mem_diff <<
G4endl;
911 std::stringstream finalOutput;
913 finalOutput <<
"*** End of step N°" << fNbSteps
914 <<
"\t T_i= " <<
G4BestUnit(fGlobalTime-fTimeStep,
"Time")
916 <<
"\t T_f= " <<
G4BestUnit(fGlobalTime,
"Time")
917 <<
"\t " << interactionType
922 if(fReachedUserTimeLimit)
924 finalOutput <<
"It has also reached the user time limit" <<
G4endl;
926 finalOutput <<
"_______________________________________________________________"
930 G4cout << finalOutput.str();
943 if (fpUserTimeSteps == 0)
return fDefaultMinTimeStep;
944 if (fabs(fGlobalTime - fUserUpperTimeLimit) < fTimeTolerance)
945 return fDefinedMinTimeStep;
947 map<double, double>::const_iterator it_fpUserTimeSteps_i = fpUserTimeSteps
948 ->upper_bound(fGlobalTime);
949 map<double, double>::const_iterator it_fpUserTimeSteps_low = fpUserTimeSteps
950 ->lower_bound(fGlobalTime);
963 if (it_fpUserTimeSteps_i == fpUserTimeSteps->end())
965 it_fpUserTimeSteps_i--;
966 fUserUpperTimeLimit = fStopTime;
968 else if (fabs(fGlobalTime - it_fpUserTimeSteps_low->first) < fTimeTolerance)
973 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
974 map<double, double>::const_iterator tmp_it = it_fpUserTimeSteps_low;
976 if (tmp_it == fpUserTimeSteps->end())
978 fUserUpperTimeLimit = fStopTime;
982 fUserUpperTimeLimit = tmp_it->first;
985 else if (it_fpUserTimeSteps_i == it_fpUserTimeSteps_low)
988 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
994 if(it_fpUserTimeSteps_i != fpUserTimeSteps->begin()) it_fpUserTimeSteps_i--;
998 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
999 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
1002 return it_fpUserTimeSteps_i->second;
1010 if(fpUserTimeSteps == 0)
1013 exceptionDescription
1014 <<
"You are asking to use user defined steps but you did not give any.";
1015 G4Exception(
"G4Scheduler::FindUserPreDefinedTimeStep",
1018 exceptionDescription);
1021 map<double, double>::iterator fpUserTimeSteps_i =
1022 fpUserTimeSteps->upper_bound(fGlobalTime);
1023 map<double, double>::iterator fpUserTimeSteps_low = fpUserTimeSteps
1024 ->lower_bound(fGlobalTime);
1036 if(fpUserTimeSteps_i == fpUserTimeSteps->end())
1038 fpUserTimeSteps_i--;
1040 else if(fabs(fGlobalTime - fpUserTimeSteps_low->first) < fTimeTolerance)
1045 fpUserTimeSteps_i = fpUserTimeSteps_low;
1047 else if(fpUserTimeSteps_i == fpUserTimeSteps_low)
1050 fpUserTimeSteps_i--;
1054 fpUserTimeSteps_i = fpUserTimeSteps_low;
1057 fDefinedMinTimeStep = fpUserTimeSteps_i->second;
1067 exceptionDescription
1068 <<
"End tracking is called while G4Scheduler is still running."
1074 exceptionDescription);
1084 for (; it != end; ++it)
1096 for (; it != end; ++it)
1106 fpTrackingInteractivity = interactivity;
1107 if(fpTrackingManager)
1118 fInitialized =
false;
1149 switch(fITStepStatus)
1152 interactionType =
"eInteractionWithMedium";
1155 interactionType =
"eCollisionBetweenTracks";
1158 interactionType =
"eCollisionBetweenTracks";
void RegisterModel(G4VITStepModel *aModel, const G4double globalTime)
virtual void DefineTracks()
void MergeSecondariesWithMainList()
virtual size_t GetNTracks()
void PrepareLeadingTracks()
void FindUserPreDefinedTimeStep()
virtual G4bool Notify(G4ApplicationState requestedState)
G4TrackManyList * GetSecondariesList()
std::ostringstream G4ExceptionDescription
virtual void Initialize()
void SetTrackingManager(G4ITTrackingManager *trackingManager)
void SetTrackingManager(G4ITTrackingManager *trackMan)
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
static G4Scheduler * Instance()
static constexpr double picosecond
G4double CalculateMinTimeStep(G4double currentGlobalTime, G4double definedMinTimeStep)
static G4ITTypeManager * Instance()
static G4ITReactionSet * Instance()
virtual void UserPostTimeStepAction()
void EndTrackingWOKill(G4Track *)
G4GLOB_DLL std::ostream G4cout
const XML_Char int const XML_Char * value
static void DeleteInstance()
virtual void UserPreTimeStepAction()
void DoIt(double timeStep)
double GetLimitingTimeStep() const
void ForceReinitialization()
bool MergeNextTimeToMainList(double &time)
bool SecondaryListsNOTEmpty()
static void DeleteInstance()
virtual size_t GetNTracks()
double GetNextWatchedTime() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
bool DelayListsNOTEmpty()
virtual void Initialize()
G4double ComputeInteractionLength(double previousTimeStep)
void ComputeTrackReaction(G4ITStepStatus fITStepStatus, G4double fGlobalTime, G4double currentTimeStep, G4double previousTimeStep, G4bool reachedUserTimeLimit, G4double fTimeTolerance, G4UserTimeStepAction *fpUserTimeStepAction, G4int fVerbose)
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
virtual void RegisterModel(G4VITStepModel *, double)
static constexpr double microsecond
virtual void EndProcessing()
void SetModelHandler(G4ITModelHandler *)
G4TrackList * GetMainList(Key)
void SetInteractivity(G4ITTrackingInteractivity *)
void GetCollisionType(G4String &interactionType)
void ResetLeadingTracks()
const XML_Char XML_Content * model
bool GetComputeTimeStep()
void SetInteractivity(G4ITTrackingInteractivity *)
virtual void StartProcessing()