62#define DEBUG_MEM_STEPPING
63#define DEBUG_MEM_DETAILED_STEPPING
76#define RED "\033[0;31m"
77#define LIGHT_RED "\33[1;31m"
78#define GREEN "\033[32;40m"
79#define GREEN_ON_BLUE "\033[1;32;44m"
80#define RESET_COLOR "\033[0m"
85#define GREEN_ON_BLUE ""
96 return std::numeric_limits<T>::has_infinity
97 && value == std::numeric_limits<T>::infinity();
103 if(fgScheduler == 0) fgScheduler =
new G4Scheduler();
114 G4cout <<
"G4Scheduler received G4State_Quit" <<
G4endl;
132G4Scheduler::G4Scheduler() :
139void G4Scheduler::Create()
141 fUseDefaultTimeSteps =
true;
142 fUserUpperTimeLimit = -1;
147 fpTrackingInteractivity = 0;
159 fMaxNZeroTimeStepsAllowed = 10000;
162 fTimeTolerance = 1 * picosecond;
163 fEndTime = 1 * microsecond;
165 fInteractionStep =
true;
166 fUsePreDefinedTimeSteps =
false;
168 fDefaultMinTimeStep = 1 * picosecond;
171 fpModelProcessor = 0;
177 fInitialized =
false;
179 fpUserTimeStepAction = 0;
184 fWhyDoYouStop =
false;
185 fDefinedMinTimeStep = -1.;
186 fReachedUserTimeLimit =
false;
188 fTmpGlobalTime = -1.;
196 fResetScavenger =
true;
230 delete fpStepProcessor;
235 delete fpModelProcessor;
236 fpModelProcessor = 0;
241 if(fpTrackingManager)
243 delete fpTrackingManager;
244 fpTrackingManager = 0;
255 delete fpModelHandler;
271 fTrackContainer.
Clear();
288 delete fpStepProcessor;
292 delete fpModelProcessor;
321 if(fUsePreDefinedTimeSteps)
323 if(fpUserTimeSteps == 0)
327 <<
"You are asking to use user defined steps but you did not give any.";
328 G4Exception(
"G4Scheduler::FindUserPreDefinedTimeStep",
331 exceptionDescription);
362 fUserUpperTimeLimit = -1;
368 fInteractionStep =
true;
385 G4cout <<
"*** G4Scheduler starts processing " <<
G4endl;
387 G4cout <<
"___________________________________________"
388 "___________________________" <<
G4endl;
404 if (fpTrackingInteractivity) fpTrackingInteractivity->
Initialize();
412 if(fResetScavenger) {
413 if (fpUserScavenger !=
nullptr) {
414 fpUserScavenger->Reset();
418 if (fpUserTimeStepAction)
424 G4bool trackFound =
false;
450 G4cout <<
"G4Scheduler: process time= "<< localtimer <<
G4endl;
474 G4cout <<
"*** G4Scheduler ends at time : "
476 G4cout <<
"___________________________________" <<
G4endl;
480 G4cout <<
"*** G4Scheduler did not start because no "
481 "track was found to be processed"<<
G4endl;
482 G4cout <<
"___________________________________" <<
G4endl;
489 if (fpUserTimeStepAction) fpUserTimeStepAction->
EndProcessing();
497 if (fpTrackingInteractivity) fpTrackingInteractivity->
Finalize();
504 std::set<G4double>::const_iterator up = fWatchedTimes.upper_bound(fGlobalTime);
505 if(up == fWatchedTimes.end())
return DBL_MAX;
532 fTmpGlobalTime = fGlobalTime;
536 G4double tmpGlobalTime = fGlobalTime;
544 fStopTime = min(fTrackContainer.
GetNextTime(), fEndTime);
548 fStopTime = min(nextWatchedTime, fEndTime);
554 if(nextWatchedTime > fEndTime && carryOn)
556 fStopTime = min(fTrackContainer.
GetNextTime(), fEndTime);
566 return fGlobalTime < fEndTime && (fMaxSteps == -1 ? true : fNbSteps < fMaxSteps)
567 && fContinue ==
true;
577 G4cout <<
"G4Scheduler has reached a stage: it might be"
578 " a transition or the end"
581 G4bool normalStop =
false;
583 if(fGlobalTime >= fStopTime)
585 G4cout <<
"== G4Scheduler: I stop because I reached the stop time : "
591 G4cout <<
"G4Scheduler: I stop because the current main list of tracks "
596 if(fMaxSteps == -1 ?
false : fNbSteps >= fMaxSteps)
598 G4cout <<
"G4Scheduler: I stop because I reached the maximum allowed "
599 "number of steps=" << fMaxSteps
603 if(fContinue && normalStop ==
false)
605 G4cout <<
"G4Scheduler: It might be that I stop because "
606 "I have been told so. You may check "
607 "member fContinue and usage of the method G4Scheduler::Stop()."
619 if(fpUserTimeStepAction) fpUserTimeStepAction->
NewStage();
621#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
622 MemStat mem_first, mem_second, mem_diff;
625#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
626 mem_first = MemoryUsage();
629 while (fGlobalTime < fStopTime
631 && (fMaxSteps == -1 ?
true : fNbSteps < fMaxSteps)
632 && fContinue ==
true)
639#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
640 mem_second = MemoryUsage();
641 mem_diff = mem_second-mem_first;
642 G4cout <<
"\t || MEM || After step " << fNbSteps <<
", diff is : "
649#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
650 mem_second = MemoryUsage();
651 mem_diff = mem_second-mem_first;
652 G4cout <<
"\t || MEM || After stepping, diff is : " << mem_diff <<
G4endl;
657 G4cout <<
"*** G4Scheduler has finished processing a track list at time : "
665 fTimeStep = fMaxTimeStep;
670 fInteractionStep =
false;
671 fReachedUserTimeLimit =
false;
682 G4cout <<
"*** Start Of Step N°" << fNbSteps + 1 <<
" ***" <<
G4endl;
691#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
692 MemStat mem_first, mem_second, mem_diff;
695#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
696 mem_first = MemoryUsage();
701 if (fUsePreDefinedTimeSteps)
710 <<
" the chosen user time step is : "
722 fDefinedMinTimeStep);
725 else if(fUseDefaultTimeSteps)
727 fTSTimeStep = fDefinedMinTimeStep;
730#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
731 mem_second = MemoryUsage();
732 mem_diff = mem_second-mem_first;
733 G4cout <<
"|| MEM || After computing TS, diff is : " << mem_diff <<
G4endl;
750#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
751 mem_first = MemoryUsage();
763#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
764 mem_second = MemoryUsage();
765 mem_diff = mem_second-mem_first;
766 G4cout <<
"|| MEM || After IL, diff is : " << mem_diff <<
G4endl;
775 G4cout <<
"*** The minimum time returned by the processes is : "
783#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
784 mem_first = MemoryUsage();
787 if (fILTimeStep <= fTSTimeStep)
790 fInteractionStep =
true;
792 fTimeStep = fILTimeStep;
798 fInteractionStep =
false;
800 fTimeStep = fTSTimeStep;
804 if (fGlobalTime + fTimeStep > fStopTime)
807 fTimeStep = fStopTime - fGlobalTime;
809 fInteractionStep =
true;
817 if (fZeroTimeCount >= fMaxNZeroTimeStepsAllowed)
821 exceptionDescription <<
"Too many zero time steps were detected. ";
822 exceptionDescription <<
"The simulation is probably stuck. ";
824 <<
"The maximum number of zero time steps is currently : "
825 << fMaxNZeroTimeStepsAllowed;
826 exceptionDescription <<
".";
829 "SchedulerNullTimeSteps",
831 exceptionDescription);
839 fReachedUserTimeLimit =
840 ((fTimeStep <= fDefinedMinTimeStep) || ((fTimeStep > fDefinedMinTimeStep)
841 && fabs(fTimeStep - fDefinedMinTimeStep) < fTimeTolerance)) ?
847#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
848 mem_second = MemoryUsage();
849 mem_diff = mem_second-mem_first;
850 G4cout <<
"|| MEM || After LeadingTracks and UserPreTimeStepAction: "
854#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
855 mem_first = MemoryUsage();
859 fGlobalTime += fTimeStep;
864 if (fTSTimeStep > 0 || fILTimeStep <= fTSTimeStep)
867 fpStepProcessor->
DoIt(fTimeStep);
877#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
878 mem_second = MemoryUsage();
879 mem_diff = mem_second-mem_first;
880 G4cout <<
"|| MEM || After DoIT, diff is : " << mem_diff <<
G4endl;
883#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
884 mem_first = MemoryUsage();
891 fReachedUserTimeLimit,
893 fpUserTimeStepAction,
898 if (fpUserTimeStepAction)
903 fPreviousTimeStep = fTimeStep;
905#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
906 mem_second = MemoryUsage();
907 mem_diff = mem_second-mem_first;
908 G4cout <<
"|| MEM || After computing reactions + UserPostTimeStepAction, "
909 "diff is : " << mem_diff <<
G4endl;
923 std::stringstream finalOutput;
925 finalOutput <<
"*** End of step N°" << fNbSteps
926 <<
"\t T_i= " <<
G4BestUnit(fGlobalTime-fTimeStep,
"Time")
928 <<
"\t T_f= " <<
G4BestUnit(fGlobalTime,
"Time")
929 <<
"\t " << interactionType
934 if(fReachedUserTimeLimit)
936 finalOutput <<
"It has also reached the user time limit" <<
G4endl;
938 finalOutput <<
"_______________________________________________________________"
942 G4cout << finalOutput.str();
955 if (fpUserTimeSteps == 0)
return fDefaultMinTimeStep;
956 if (fabs(fGlobalTime - fUserUpperTimeLimit) < fTimeTolerance)
957 return fDefinedMinTimeStep;
959 map<G4double, G4double>::const_iterator it_fpUserTimeSteps_i = fpUserTimeSteps
960 ->upper_bound(fGlobalTime);
961 map<G4double, G4double>::const_iterator it_fpUserTimeSteps_low = fpUserTimeSteps
962 ->lower_bound(fGlobalTime);
975 if (it_fpUserTimeSteps_i == fpUserTimeSteps->end())
977 it_fpUserTimeSteps_i--;
978 fUserUpperTimeLimit = fStopTime;
980 else if (fabs(fGlobalTime - it_fpUserTimeSteps_low->first) < fTimeTolerance)
985 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
986 map<G4double, G4double>::const_iterator tmp_it = it_fpUserTimeSteps_low;
988 if (tmp_it == fpUserTimeSteps->end())
990 fUserUpperTimeLimit = fStopTime;
994 fUserUpperTimeLimit = tmp_it->first;
997 else if (it_fpUserTimeSteps_i == it_fpUserTimeSteps_low)
1000 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
1006 if(it_fpUserTimeSteps_i != fpUserTimeSteps->begin()) it_fpUserTimeSteps_i--;
1010 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
1011 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
1014 return it_fpUserTimeSteps_i->second;
1022 if(fpUserTimeSteps == 0)
1025 exceptionDescription
1026 <<
"You are asking to use user defined steps but you did not give any.";
1027 G4Exception(
"G4Scheduler::FindUserPreDefinedTimeStep",
1030 exceptionDescription);
1033 map<G4double, G4double>::iterator fpUserTimeSteps_i =
1034 fpUserTimeSteps->upper_bound(fGlobalTime);
1035 map<G4double, G4double>::iterator fpUserTimeSteps_low = fpUserTimeSteps
1036 ->lower_bound(fGlobalTime);
1048 if(fpUserTimeSteps_i == fpUserTimeSteps->end())
1050 fpUserTimeSteps_i--;
1052 else if(fabs(fGlobalTime - fpUserTimeSteps_low->first) < fTimeTolerance)
1057 fpUserTimeSteps_i = fpUserTimeSteps_low;
1059 else if(fpUserTimeSteps_i == fpUserTimeSteps_low)
1062 fpUserTimeSteps_i--;
1066 fpUserTimeSteps_i = fpUserTimeSteps_low;
1069 fDefinedMinTimeStep = fpUserTimeSteps_i->second;
1079 exceptionDescription
1080 <<
"End tracking is called while G4Scheduler is still running."
1086 exceptionDescription);
1096 for (; it != end; ++it)
1108 for (; it != end; ++it)
1118 fpTrackingInteractivity = interactivity;
1119 if(fpTrackingManager)
1130 fInitialized =
false;
1161 switch(fITStepStatus)
1164 interactionType =
"eInteractionWithMedium";
1167 interactionType =
"eCollisionBetweenTracks";
1170 interactionType =
"eCollisionBetweenTracks";
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
@ eCollisionBetweenTracks
G4GLOB_DLL std::ostream G4cout
static void DeleteInstance()
virtual void DefineTracks()
void RegisterModel(G4VITStepModel *pModel, G4double globalTime)
void SetModelHandler(G4ITModelHandler *)
void SetTrackingManager(G4ITTrackingManager *trackingManager)
G4double CalculateMinTimeStep(G4double currentGlobalTime, G4double definedMinTimeStep)
void ComputeTrackReaction(G4ITStepStatus fITStepStatus, G4double fGlobalTime, G4double currentTimeStep, G4double previousTimeStep, G4bool reachedUserTimeLimit, G4double fTimeTolerance, G4UserTimeStepAction *fpUserTimeStepAction, G4int fVerbose)
bool GetComputeTimeStep() const
static G4ITReactionSet * Instance()
void ResetLeadingTracks()
void SetTrackingManager(G4ITTrackingManager *trackMan)
void PrepareLeadingTracks()
G4double ComputeInteractionLength(double previousTimeStep)
void DoIt(double timeStep)
virtual void Initialize()
virtual size_t GetNTracks()
G4TrackList * GetMainList(Key)
void MergeSecondariesWithMainList()
G4TrackManyList * GetSecondariesList()
bool SecondaryListsNOTEmpty()
bool MergeNextTimeToMainList(double &time)
bool DelayListsNOTEmpty()
virtual void Initialize()
void EndTrackingWOKill(G4Track *)
void SetInteractivity(G4ITTrackingInteractivity *)
static G4ITTypeManager * Instance()
virtual G4bool Notify(G4ApplicationState requestedState)
void FindUserPreDefinedTimeStep()
void ForceReinitialization()
virtual void RegisterModel(G4VITStepModel *, G4double)
G4double GetNextWatchedTime() const
void SetInteractivity(G4ITTrackingInteractivity *)
G4double GetLimitingTimeStep() const
static G4Scheduler * Instance()
virtual size_t GetNTracks()
void GetCollisionType(G4String &interactionType)
static void DeleteInstance()
virtual void UserPostTimeStepAction()
virtual void StartProcessing()
virtual void EndProcessing()
virtual void UserPreTimeStepAction()