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 = 10;
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.;
227 delete fpStepProcessor;
232 delete fpModelProcessor;
233 fpModelProcessor = 0;
238 if(fpTrackingManager)
240 delete fpTrackingManager;
241 fpTrackingManager = 0;
252 delete fpModelHandler;
268 fTrackContainer.
Clear();
285 delete fpStepProcessor;
289 delete fpModelProcessor;
318 if(fUsePreDefinedTimeSteps)
320 if(fpUserTimeSteps == 0)
324 <<
"You are asking to use user defined steps but you did not give any.";
325 G4Exception(
"G4Scheduler::FindUserPreDefinedTimeStep",
328 exceptionDescription);
359 fUserUpperTimeLimit = -1;
365 fInteractionStep =
true;
382 G4cout <<
"*** G4Scheduler starts processing " <<
G4endl;
384 G4cout <<
"___________________________________________"
385 "___________________________" <<
G4endl;
401 if (fpTrackingInteractivity) fpTrackingInteractivity->
Initialize();
410 G4bool trackFound =
false;
436 G4cout <<
"G4Scheduler: process time= "<< localtimer <<
G4endl;
460 G4cout <<
"*** G4Scheduler ends at time : "
462 G4cout <<
"___________________________________" <<
G4endl;
466 G4cout <<
"*** G4Scheduler did not start because no "
467 "track was found to be processed"<<
G4endl;
468 G4cout <<
"___________________________________" <<
G4endl;
475 if (fpUserTimeStepAction) fpUserTimeStepAction->
EndProcessing();
483 if (fpTrackingInteractivity) fpTrackingInteractivity->
Finalize();
490 std::set<double>::const_iterator up = fWatchedTimes.upper_bound(fGlobalTime);
491 if(up == fWatchedTimes.end())
return DBL_MAX;
518 fTmpGlobalTime = fGlobalTime;
522 G4double tmpGlobalTime = fGlobalTime;
524 double nextWatchedTime = -1;
530 fStopTime = min(fTrackContainer.
GetNextTime(), fEndTime);
534 fStopTime = min(nextWatchedTime, fEndTime);
540 if(nextWatchedTime > fEndTime && carryOn)
542 fStopTime = min(fTrackContainer.
GetNextTime(), fEndTime);
552 return fGlobalTime < fEndTime && (fMaxSteps == -1 ? true : fNbSteps < fMaxSteps)
553 && fContinue ==
true;
563 G4cout <<
"G4Scheduler has reached a stage: it might be"
564 " a transition or the end"
567 G4bool normalStop =
false;
569 if(fGlobalTime >= fStopTime)
571 G4cout <<
"== G4Scheduler: I stop because I reached the stop time : "
577 G4cout <<
"G4Scheduler: I stop because the current main list of tracks "
582 if(fMaxSteps == -1 ?
false : fNbSteps >= fMaxSteps)
584 G4cout <<
"G4Scheduler: I stop because I reached the maximum allowed "
585 "number of steps=" << fMaxSteps
589 if(fContinue && normalStop ==
false)
591 G4cout <<
"G4Scheduler: It might be that I stop because "
592 "I have been told so. You may check "
593 "member fContinue and usage of the method G4Scheduler::Stop()."
605 if(fpUserTimeStepAction) fpUserTimeStepAction->
NewStage();
607#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
608 MemStat mem_first, mem_second, mem_diff;
611#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
612 mem_first = MemoryUsage();
615 while (fGlobalTime < fStopTime
617 && (fMaxSteps == -1 ?
true : fNbSteps < fMaxSteps)
618 && fContinue ==
true)
625#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
626 mem_second = MemoryUsage();
627 mem_diff = mem_second-mem_first;
628 G4cout <<
"\t || MEM || After step " << fNbSteps <<
", diff is : "
635#if defined (DEBUG_MEM) && defined (DEBUG_MEM_STEPPING)
636 mem_second = MemoryUsage();
637 mem_diff = mem_second-mem_first;
638 G4cout <<
"\t || MEM || After stepping, diff is : " << mem_diff <<
G4endl;
643 G4cout <<
"*** G4Scheduler has finished processing a track list at time : "
651 fTimeStep = fMaxTimeStep;
656 fInteractionStep =
false;
657 fReachedUserTimeLimit =
false;
668 G4cout <<
"*** Start Of Step N°" << fNbSteps + 1 <<
" ***" <<
G4endl;
677#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
678 MemStat mem_first, mem_second, mem_diff;
681#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
682 mem_first = MemoryUsage();
687 if (fUsePreDefinedTimeSteps)
696 <<
" the chosen user time step is : "
708 fDefinedMinTimeStep);
711 else if(fUseDefaultTimeSteps)
713 fTSTimeStep = fDefinedMinTimeStep;
716#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
717 mem_second = MemoryUsage();
718 mem_diff = mem_second-mem_first;
719 G4cout <<
"|| MEM || After computing TS, diff is : " << mem_diff <<
G4endl;
736#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
737 mem_first = MemoryUsage();
750#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
751 mem_second = MemoryUsage();
752 mem_diff = mem_second-mem_first;
753 G4cout <<
"|| MEM || After IL, diff is : " << mem_diff <<
G4endl;
762 G4cout <<
"*** The minimum time returned by the processes is : "
770#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
771 mem_first = MemoryUsage();
774 if (fILTimeStep <= fTSTimeStep)
777 fInteractionStep =
true;
779 fTimeStep = fILTimeStep;
785 fInteractionStep =
false;
787 fTimeStep = fTSTimeStep;
791 if (fGlobalTime + fTimeStep > fStopTime)
794 fTimeStep = fStopTime - fGlobalTime;
796 fInteractionStep =
true;
804 if (fZeroTimeCount >= fMaxNZeroTimeStepsAllowed)
808 exceptionDescription <<
"Too many zero time steps were detected. ";
809 exceptionDescription <<
"The simulation is probably stuck. ";
811 <<
"The maximum number of zero time steps is currently : "
812 << fMaxNZeroTimeStepsAllowed;
813 exceptionDescription <<
".";
816 "SchedulerNullTimeSteps",
818 exceptionDescription);
826 fReachedUserTimeLimit =
827 ((fTimeStep <= fDefinedMinTimeStep) || ((fTimeStep > fDefinedMinTimeStep)
828 && fabs(fTimeStep - fDefinedMinTimeStep) < fTimeTolerance)) ?
834#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
835 mem_second = MemoryUsage();
836 mem_diff = mem_second-mem_first;
837 G4cout <<
"|| MEM || After LeadingTracks and UserPreTimeStepAction: "
841#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
842 mem_first = MemoryUsage();
846 fGlobalTime += fTimeStep;
851 if (fTSTimeStep > 0 || fILTimeStep <= fTSTimeStep)
854 fpStepProcessor->
DoIt(fTimeStep);
864#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
865 mem_second = MemoryUsage();
866 mem_diff = mem_second-mem_first;
867 G4cout <<
"|| MEM || After DoIT, diff is : " << mem_diff <<
G4endl;
870#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
871 mem_first = MemoryUsage();
878 fReachedUserTimeLimit,
880 fpUserTimeStepAction,
885 if (fpUserTimeStepAction)
890 fPreviousTimeStep = fTimeStep;
892#if defined (DEBUG_MEM) && defined (DEBUG_MEM_DETAILED_STEPPING)
893 mem_second = MemoryUsage();
894 mem_diff = mem_second-mem_first;
895 G4cout <<
"|| MEM || After computing reactions + UserPostTimeStepAction, "
896 "diff is : " << mem_diff <<
G4endl;
910 std::stringstream finalOutput;
912 finalOutput <<
"*** End of step N°" << fNbSteps
913 <<
"\t T_i= " <<
G4BestUnit(fGlobalTime-fTimeStep,
"Time")
915 <<
"\t T_f= " <<
G4BestUnit(fGlobalTime,
"Time")
916 <<
"\t " << interactionType
921 if(fReachedUserTimeLimit)
923 finalOutput <<
"It has also reached the user time limit" <<
G4endl;
925 finalOutput <<
"_______________________________________________________________"
929 G4cout << finalOutput.str();
942 if (fpUserTimeSteps == 0)
return fDefaultMinTimeStep;
943 if (fabs(fGlobalTime - fUserUpperTimeLimit) < fTimeTolerance)
944 return fDefinedMinTimeStep;
946 map<double, double>::const_iterator it_fpUserTimeSteps_i = fpUserTimeSteps
947 ->upper_bound(fGlobalTime);
948 map<double, double>::const_iterator it_fpUserTimeSteps_low = fpUserTimeSteps
949 ->lower_bound(fGlobalTime);
962 if (it_fpUserTimeSteps_i == fpUserTimeSteps->end())
964 it_fpUserTimeSteps_i--;
965 fUserUpperTimeLimit = fStopTime;
967 else if (fabs(fGlobalTime - it_fpUserTimeSteps_low->first) < fTimeTolerance)
972 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
973 map<double, double>::const_iterator tmp_it = it_fpUserTimeSteps_low;
975 if (tmp_it == fpUserTimeSteps->end())
977 fUserUpperTimeLimit = fStopTime;
981 fUserUpperTimeLimit = tmp_it->first;
984 else if (it_fpUserTimeSteps_i == it_fpUserTimeSteps_low)
987 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
993 if(it_fpUserTimeSteps_i != fpUserTimeSteps->begin()) it_fpUserTimeSteps_i--;
997 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
998 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
1001 return it_fpUserTimeSteps_i->second;
1009 if(fpUserTimeSteps == 0)
1012 exceptionDescription
1013 <<
"You are asking to use user defined steps but you did not give any.";
1014 G4Exception(
"G4Scheduler::FindUserPreDefinedTimeStep",
1017 exceptionDescription);
1020 map<double, double>::iterator fpUserTimeSteps_i =
1021 fpUserTimeSteps->upper_bound(fGlobalTime);
1022 map<double, double>::iterator fpUserTimeSteps_low = fpUserTimeSteps
1023 ->lower_bound(fGlobalTime);
1035 if(fpUserTimeSteps_i == fpUserTimeSteps->end())
1037 fpUserTimeSteps_i--;
1039 else if(fabs(fGlobalTime - fpUserTimeSteps_low->first) < fTimeTolerance)
1044 fpUserTimeSteps_i = fpUserTimeSteps_low;
1046 else if(fpUserTimeSteps_i == fpUserTimeSteps_low)
1049 fpUserTimeSteps_i--;
1053 fpUserTimeSteps_i = fpUserTimeSteps_low;
1056 fDefinedMinTimeStep = fpUserTimeSteps_i->second;
1066 exceptionDescription
1067 <<
"End tracking is called while G4Scheduler is still running."
1073 exceptionDescription);
1083 for (; it != end; ++it)
1095 for (; it != end; ++it)
1105 fpTrackingInteractivity = interactivity;
1106 if(fpTrackingManager)
1117 fInitialized =
false;
1148 switch(fITStepStatus)
1151 interactionType =
"eInteractionWithMedium";
1154 interactionType =
"eCollisionBetweenTracks";
1157 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 void RegisterModel(G4VITStepModel *, double)
virtual G4bool Notify(G4ApplicationState requestedState)
void FindUserPreDefinedTimeStep()
void ForceReinitialization()
void SetInteractivity(G4ITTrackingInteractivity *)
double GetNextWatchedTime() const
static G4Scheduler * Instance()
virtual size_t GetNTracks()
double GetLimitingTimeStep() const
void GetCollisionType(G4String &interactionType)
static void DeleteInstance()
virtual void UserPostTimeStepAction()
virtual void StartProcessing()
virtual void EndProcessing()
virtual void UserPreTimeStepAction()