Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4Scheduler.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
28//
29// History:
30// -----------
31// 10 Oct 2011 M.Karamitros created
32//
33// -------------------------------------------------------------------
34
35#include "G4ITGun.hh"
36#include "G4ITModelHandler.hh"
37#include "G4ITModelProcessor.hh"
38#include "G4ITStepProcessor.hh"
39#include "G4ITStepStatus.hh"
42#include "G4IosFlagsSaver.hh"
43#include "G4StateManager.hh"
44#include "G4SystemOfUnits.hh"
45#include "G4Timer.hh"
47#include "G4UnitsTable.hh"
49#include "G4VITStepModel.hh"
50
51#include <G4AllITFinder.hh>
52#include <G4Scheduler.hh>
54
55#include <sstream>
56
57#define DEBUG_MEM_STEPPING
58#define DEBUG_MEM_DETAILED_STEPPING
59
60#ifdef DEBUG_MEM
61# include "G4MemStat.hh"
62using namespace G4MemStat;
64#endif
65
66#ifdef USE_COLOR
67# define RED "\033[0;31m"
68# define LIGHT_RED "\33[1;31m"
69# define GREEN "\033[32;40m"
70# define GREEN_ON_BLUE "\033[1;32;44m"
71# define RESET_COLOR "\033[0m"
72#else
73# define RED ""
74# define LIGHT_RED ""
75# define GREEN ""
76# define GREEN_ON_BLUE ""
77# define RESET_COLOR ""
78#endif
79
80using namespace std;
81
82G4ThreadLocal G4Scheduler* G4Scheduler::fgScheduler(nullptr);
83
84template<typename T>
85inline G4bool IsInf(T value)
86{
87 return std::numeric_limits<T>::has_infinity && value == std::numeric_limits<T>::infinity();
88}
89//_________________________________________________________________________
90
92{
93 if (fgScheduler == nullptr) fgScheduler = new G4Scheduler();
94 return fgScheduler;
95}
96//_________________________________________________________________________
97
99{
100 if (requestedState == G4State_Quit) {
101 if (fVerbose >= 4) {
102 G4cout << "G4Scheduler received G4State_Quit" << G4endl;
103 }
104 Clear();
105 }
106 return true;
107}
108//_________________________________________________________________________
109
111{
112 delete fgScheduler;
113}
114//_________________________________________________________________________
115
116G4Scheduler::G4Scheduler() : fTrackContainer((G4ITTrackHolder&)*G4ITTrackHolder::Instance())
117{
118 Create();
119}
120
121void G4Scheduler::Create()
122{
123 fUseDefaultTimeSteps = true;
124 fUserUpperTimeLimit = -1;
125 fpGun = nullptr;
126 fContinue = true;
127 fpTrackingInteractivity = nullptr;
128 fITStepStatus = eUndefined;
129 fpUserTimeSteps = nullptr;
130 fTimeStep = DBL_MAX;
131 fTSTimeStep = DBL_MAX;
132 fILTimeStep = DBL_MAX;
133 fPreviousTimeStep = DBL_MAX;
134 fZeroTimeCount = 0;
135 fMaxNZeroTimeStepsAllowed = 10000;
136 fStartTime = 0;
137 fTimeTolerance = 1 * picosecond;
138 fEndTime = 1 * microsecond;
139 fGlobalTime = -1;
140 fInteractionStep = true;
141 fUsePreDefinedTimeSteps = false;
142
143 fDefaultMinTimeStep = 1 * picosecond;
144 fpStepProcessor = nullptr;
145 fpModelProcessor = nullptr;
146 fNbSteps = 0;
147 fMaxSteps = -1;
148 fRunning = false;
149 fInitialized = false;
150 fpUserTimeStepAction = nullptr;
151 fpModelHandler = new G4ITModelHandler();
152 fpTrackingManager = new G4ITTrackingManager();
153 fVerbose = 0;
154 fWhyDoYouStop = false;
155 fDefinedMinTimeStep = -1.;
156 fReachedUserTimeLimit = false;
157 fStopTime = -1.;
158 fTmpGlobalTime = -1.;
159 fpMessenger = new G4SchedulerMessenger(this);
160 fReactionSet = G4ITReactionSet::Instance();
161 fMaxTimeStep = DBL_MAX;
162 // hoang add
163 fResetScavenger = true; // true by default
165}
166
167//_________________________________________________________________________
168
170{
171 if (fpMessenger != nullptr) // is used as a flag to know whether the manager was cleared
172 {
173 Clear();
174 }
175 fgScheduler = nullptr;
176}
177
179{
180 if (fpMessenger != nullptr) {
181 delete fpMessenger;
182 fpMessenger = nullptr;
183 }
184 if (fpStepProcessor != nullptr) {
185 delete fpStepProcessor;
186 fpStepProcessor = nullptr;
187 }
188 if (fpModelProcessor != nullptr) {
189 delete fpModelProcessor;
190 fpModelProcessor = nullptr;
191 }
192
194 ClearList();
195 if (fpTrackingManager != nullptr) {
196 delete fpTrackingManager;
197 fpTrackingManager = nullptr;
198 }
199
200 if (fReactionSet != nullptr) {
201 delete fReactionSet;
202 fReactionSet = nullptr;
203 }
204
205 if (fpModelHandler != nullptr) {
206 delete fpModelHandler;
207 fpModelHandler = nullptr;
208 }
209}
210
211//_________________________________________________________________________
212
214{
215 fTrackContainer.Clear();
217}
218
219//_________________________________________________________________________
221{
222 fpModelHandler->RegisterModel(model, time);
223}
224
225//_________________________________________________________________________
226
228{
229 delete fpStepProcessor;
230 delete fpModelProcessor;
231
232 fpModelProcessor = new G4ITModelProcessor();
233 fpModelProcessor->SetModelHandler(fpModelHandler);
234 fpModelProcessor->SetTrackingManager(fpTrackingManager);
235 fpStepProcessor = new G4ITStepProcessor();
236 fpStepProcessor->SetTrackingManager(fpTrackingManager);
237 fpTrackingManager->SetInteractivity(fpTrackingInteractivity);
238 if (fUsePreDefinedTimeSteps) {
239 if (fpUserTimeSteps == nullptr) // Extra checking, is it necessary ?
240 {
241 G4ExceptionDescription exceptionDescription;
242 exceptionDescription << "You are asking to use user defined steps but you did not give any.";
243 G4Exception("G4Scheduler::FindUserPreDefinedTimeStep", "Scheduler004", FatalErrorInArgument,
244 exceptionDescription);
245 return; // makes coverity happy
246 }
247 }
248
249 fInitialized = true;
250}
251
252//_________________________________________________________________________
253
255{
256 fStartTime = 0;
257 fUserUpperTimeLimit = -1;
258 fTimeStep = DBL_MAX;
259 fTSTimeStep = DBL_MAX;
260 fILTimeStep = DBL_MAX;
261 fPreviousTimeStep = DBL_MAX;
262 fGlobalTime = -1;
263 fInteractionStep = true;
264 fITStepStatus = eUndefined;
265 fZeroTimeCount = 0;
266
267 fNbSteps = 0;
268 fContinue = true;
269 fReactionSet->CleanAllReaction();
270}
271//_________________________________________________________________________
272
274{
275#ifdef G4VERBOSE
276 if (fVerbose != 0) {
277 G4cout << "*** G4Scheduler starts processing " << G4endl;
278 if (fVerbose > 2)
279 G4cout << "___________________________________________"
280 "___________________________"
281 << G4endl;
282 }
283#endif
284
285 if (!fInitialized) {
286 Initialize();
287 }
288 fpModelProcessor->Initialize();
289 fpStepProcessor->Initialize();
290
291 if (fpGun != nullptr) fpGun->DefineTracks();
292
293 if (fpTrackingInteractivity != nullptr) fpTrackingInteractivity->Initialize();
294
295 // ___________________
296 fRunning = true;
297 Reset();
298
299 if (fResetScavenger) {
300 if (fpUserScavenger != nullptr) {
301 fpUserScavenger->Reset();
302 }
303 }
304
305 if (fpUserTimeStepAction != nullptr) {
306 fpUserTimeStepAction->StartProcessing();
307 }
308
309#ifdef G4VERBOSE
310 G4bool trackFound = false;
311 G4IosFlagsSaver iosfs(G4cout);
312 G4cout.precision(5);
313#endif
314
315 //===========================================================================
316 // By default, before the G4Scheduler is launched, the tracks are pushed to
317 // the delayed lists
318 //===========================================================================
319
320 if (fTrackContainer.DelayListsNOTEmpty()) {
321 fStartTime = fTrackContainer.GetNextTime();
322#ifdef G4VERBOSE
323 trackFound = true;
324 G4Timer localtimer;
325 if (fVerbose > 1) {
326 localtimer.Start();
327 }
328#endif
330#ifdef G4VERBOSE
331 if (fVerbose > 1) {
332 localtimer.Stop();
333 G4cout << "G4Scheduler: process time= " << localtimer << G4endl;
334 }
335#endif
336 }
337#ifdef G4VERBOSE
338 if (fVerbose != 0) {
339 if (trackFound) {
340 G4cout << "*** G4Scheduler ends at time : " << G4BestUnit(fGlobalTime, "Time") << G4endl;
341 G4cout << "___________________________________" << G4endl;
342 }
343 else {
344 G4cout << "*** G4Scheduler did not start because no "
345 "track was found to be processed"
346 << G4endl;
347 G4cout << "___________________________________" << G4endl;
348 }
349 }
350#endif
351
352 fRunning = false;
353
354 if (fpUserTimeStepAction != nullptr) {
355 fpUserTimeStepAction->EndProcessing();
356 }
357 // ___________________
358 EndTracking();
359 ClearList();
360 Reset();
361
362 if (fpTrackingInteractivity != nullptr) {
363 fpTrackingInteractivity->Finalize();
364 }
365}
366
367//_________________________________________________________________________
368
370{
371 auto up = fWatchedTimes.upper_bound(fGlobalTime);
372 if (up == fWatchedTimes.end()) {
373 return DBL_MAX;
374 }
375 return *up;
376}
377
378//_________________________________________________________________________
379
381{
382 fTmpGlobalTime = fGlobalTime;
383 fGlobalTime = fTrackContainer.GetNextTime();
384 G4double tmpGlobalTime = fGlobalTime;
385 G4double nextWatchedTime = -1;
386 G4bool carryOn = true;
387 while (fTrackContainer.MergeNextTimeToMainList(tmpGlobalTime) && carryOn) {
388 if (tmpGlobalTime != fGlobalTime) {
389 fGlobalTime = tmpGlobalTime;
390 };
391 fStopTime = min(fTrackContainer.GetNextTime(), fEndTime);
392 while ((nextWatchedTime = GetNextWatchedTime()) < fTrackContainer.GetNextTime()
393 && (carryOn = CanICarryOn()))
394 {
395 fStopTime = min(nextWatchedTime, fEndTime);
396 DoProcess();
397 }
398
399 carryOn = CanICarryOn();
400
401 if (nextWatchedTime > fEndTime && carryOn) {
402 fStopTime = min(fTrackContainer.GetNextTime(), fEndTime);
403 DoProcess();
404 }
405 }
406}
407
408//_________________________________________________________________________
409
411{
412 return fGlobalTime < fEndTime && (fMaxSteps == -1 ? true : fNbSteps < fMaxSteps) && fContinue;
413}
414
415//_________________________________________________________________________
416
418{
419#ifdef G4VERBOSE
420 if (fWhyDoYouStop) {
421 G4cout << "G4Scheduler has reached a stage: it might be"
422 " a transition or the end"
423 << G4endl;
424
425 G4bool normalStop = false;
426
427 if (fGlobalTime >= fStopTime) {
428 G4cout << "== G4Scheduler: I stop because I reached the stop time : "
429 << G4BestUnit(fStopTime, "Time") << " ==" << G4endl;
430 normalStop = true;
431 }
432 if (!fTrackContainer.MainListsNOTEmpty()) // is empty
433 {
434 G4cout << "G4Scheduler: I stop because the current main list of tracks "
435 "is empty"
436 << G4endl;
437 normalStop = true;
438 }
439 if (fMaxSteps == -1 ? false : fNbSteps >= fMaxSteps) {
440 G4cout << "G4Scheduler: I stop because I reached the maximum allowed "
441 "number of steps="
442 << fMaxSteps << G4endl;
443 normalStop = true;
444 }
445 if (fContinue && !normalStop) {
446 G4cout << "G4Scheduler: It might be that I stop because "
447 "I have been told so. You may check "
448 "member fContinue and usage of the method G4Scheduler::Stop()."
449 << G4endl;
450 }
451 }
452#endif
453}
454
455//_________________________________________________________________________
456
458{
459 if (fpUserTimeStepAction != nullptr) fpUserTimeStepAction->NewStage();
460
461#if defined(DEBUG_MEM) && defined(DEBUG_MEM_STEPPING)
462 MemStat mem_first, mem_second, mem_diff;
463#endif
464
465#if defined(DEBUG_MEM) && defined(DEBUG_MEM_STEPPING)
466 mem_first = MemoryUsage();
467#endif
468
469 while (fGlobalTime < fStopTime && fTrackContainer.MainListsNOTEmpty()
470 && (fMaxSteps == -1 ? true : fNbSteps < fMaxSteps) && fContinue)
471 {
472 Stepping();
473
474#if defined(DEBUG_MEM) && defined(DEBUG_MEM_STEPPING)
475 mem_second = MemoryUsage();
476 mem_diff = mem_second - mem_first;
477 G4cout << "\t || MEM || After step " << fNbSteps << ", diff is : " << mem_diff << G4endl;
478#endif
479 }
480
482
483#if defined(DEBUG_MEM) && defined(DEBUG_MEM_STEPPING)
484 mem_second = MemoryUsage();
485 mem_diff = mem_second - mem_first;
486 G4cout << "\t || MEM || After stepping, diff is : " << mem_diff << G4endl;
487#endif
488
489#ifdef G4VERBOSE
490 if (fVerbose > 2)
491 G4cout << "*** G4Scheduler has finished processing a track list at time : "
492 << G4BestUnit(fGlobalTime, "Time") << G4endl;
493#endif
494}
495//_________________________________________________________________________
496
498{
499 fTimeStep = fMaxTimeStep;
500
501 fTSTimeStep = DBL_MAX;
502 fILTimeStep = DBL_MAX;
503
504 //fInteractionStep = false;
505 fReachedUserTimeLimit = false;
506
507 fITStepStatus = eUndefined;
508
509 // Start of step
510#ifdef G4VERBOSE
511 if (fVerbose > 2) {
512# ifdef USE_COLOR
513 G4cout << LIGHT_RED;
514# endif
515 G4cout << "*** Start Of Step N°" << fNbSteps + 1 << " species number : " << GetNTracks()
516 << " ***" << G4endl;
517 G4cout << "Current Global time : " << G4BestUnit(fGlobalTime, "Time") << G4endl;
518# ifdef USE_COLOR
520# endif
521 }
522#endif
523
524#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
525 MemStat mem_first, mem_second, mem_diff;
526#endif
527
528#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
529 mem_first = MemoryUsage();
530#endif
531
532 fDefinedMinTimeStep = GetLimitingTimeStep();
533
534 if (fUsePreDefinedTimeSteps) {
535#ifdef G4VERBOSE
536 if (fVerbose > 2) {
537# ifdef USE_COLOR
538 G4cout << LIGHT_RED;
539# endif
540 G4cout << "*** At time : " << G4BestUnit(fGlobalTime, "Time")
541 << " the chosen user time step is : " << G4BestUnit(fDefinedMinTimeStep, "Time")
542 << " ***" << G4endl;
543# ifdef USE_COLOR
545# endif
546 }
547#endif
548 }
549
550 if (fpModelProcessor->GetComputeTimeStep()) // fComputeTimeStep)
551 {
552 fTSTimeStep = fpModelProcessor->CalculateMinTimeStep(fGlobalTime, fDefinedMinTimeStep);
553 // => at least N (N = nb of tracks) loops
554 }
555 else if (fUseDefaultTimeSteps) {
556 fTSTimeStep = fDefinedMinTimeStep;
557 }
558
559#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
560 mem_second = MemoryUsage();
561 mem_diff = mem_second - mem_first;
562 G4cout << "|| MEM || After computing TS, diff is : " << mem_diff << G4endl;
563#endif
564
565#ifdef G4VERBOSE
566 if (fVerbose > 2) {
567# ifdef USE_COLOR
568 G4cout << LIGHT_RED;
569# endif
570 G4cout << "*** Time stepper returned : " << G4BestUnit(fTSTimeStep, "Time") << " ***" << G4endl;
571# ifdef USE_COLOR
573# endif
574 }
575#endif
576
577#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
578 mem_first = MemoryUsage();
579#endif
580
581 // Call IL even if fTSTimeStep == 0
582 // if fILTimeStep == 0 give the priority to DoIt processes
583
584 fILTimeStep = fpStepProcessor->ComputeInteractionLength(fPreviousTimeStep);
585 // => at least N loops
586 // All process returns the physical step of interaction
587 // The transportation process calculates the corresponding
588 // time step
589
590#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
591 mem_second = MemoryUsage();
592 mem_diff = mem_second - mem_first;
593 G4cout << "|| MEM || After IL, diff is : " << mem_diff << G4endl;
594#endif
595
596#ifdef G4VERBOSE
597 if (fVerbose > 2) {
598# ifdef USE_COLOR
599 G4cout << LIGHT_RED;
600# endif
601 G4cout << "*** The minimum time returned by the processes is : "
602 << G4BestUnit(fILTimeStep, "Time") << " ***" << G4endl;
603# ifdef USE_COLOR
605# endif
606 }
607#endif
608
609#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
610 mem_first = MemoryUsage();
611#endif
612
613 if (fILTimeStep <= fTSTimeStep)
614 // Give the priority to the IL
615 {
616 fInteractionStep = true;
617 //fReactionSet->CleanAllReaction();
618 fTimeStep = fILTimeStep;
619 fITStepStatus = eInteractionWithMedium;
620 fpStepProcessor->PrepareLeadingTracks();
621 }
622 else {
623 fInteractionStep = false;
624 fpStepProcessor->ResetLeadingTracks();
625 fTimeStep = fTSTimeStep;
626 fITStepStatus = eCollisionBetweenTracks;
627 }
628
629 if (fGlobalTime + fTimeStep > fStopTime)
630 // This check is done at every time step
631 {
632 fTimeStep = fStopTime - fGlobalTime;
633 fITStepStatus = eInteractionWithMedium; // ie: transportation
634 fInteractionStep = true;
635 fReactionSet->CleanAllReaction();
636 fpStepProcessor->ResetLeadingTracks();
637 }
638
639 if (fTimeStep == 0) // < fTimeTolerance)
640 {
641 ++fZeroTimeCount;
642 if (fZeroTimeCount >= fMaxNZeroTimeStepsAllowed) {
643 G4ExceptionDescription exceptionDescription;
644
645 exceptionDescription << "Too many zero time steps were detected. ";
646 exceptionDescription << "The simulation is probably stuck. ";
647 exceptionDescription << "The maximum number of zero time steps is currently : "
648 << fMaxNZeroTimeStepsAllowed;
649 exceptionDescription << ".";
650
651 G4Exception("G4Scheduler::Stepping", "SchedulerNullTimeSteps", FatalErrorInArgument,
652 exceptionDescription);
653 }
654 }
655 else {
656 fZeroTimeCount = 0;
657 }
658
659 fReachedUserTimeLimit = (fTimeStep <= fDefinedMinTimeStep)
660 || ((fTimeStep > fDefinedMinTimeStep)
661 && fabs(fTimeStep - fDefinedMinTimeStep) < fTimeTolerance);
662
663 if (fpUserTimeStepAction != nullptr) fpUserTimeStepAction->UserPreTimeStepAction();
664 // TODO: pre/post
665
666#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
667 mem_second = MemoryUsage();
668 mem_diff = mem_second - mem_first;
669 G4cout << "|| MEM || After LeadingTracks and UserPreTimeStepAction: " << mem_diff << G4endl;
670#endif
671
672#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
673 mem_first = MemoryUsage();
674#endif
675
676 fGlobalTime += fTimeStep;
677
678 // if fTSTimeStep > 0 => still need to call the transportation process
679 // if fILTimeStep < fTSTimeStep => call only DoIt processes, no reactions
680 // if fILTimeStep == fTSTimeStep => give the priority to the DoIt processes
681 if (fTSTimeStep > 0 || fILTimeStep <= fTSTimeStep) {
682 fpStepProcessor->DoIt(fTimeStep);
683 }
684#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
685 mem_second = MemoryUsage();
686 mem_diff = mem_second - mem_first;
687 G4cout << "|| MEM || After DoIT, diff is : " << mem_diff << G4endl;
688#endif
689
690#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
691 mem_first = MemoryUsage();
692#endif
693
694 fpModelProcessor->ComputeTrackReaction(fITStepStatus, fGlobalTime, fTimeStep, fPreviousTimeStep,
695 fReachedUserTimeLimit, fTimeTolerance,
696 fpUserTimeStepAction, fVerbose);
697
698 ++fNbSteps;
699
700 if (fpUserTimeStepAction != nullptr) {
701 fpUserTimeStepAction->UserPostTimeStepAction();
702 }
703
704 fPreviousTimeStep = fTimeStep;
705
706#if defined(DEBUG_MEM) && defined(DEBUG_MEM_DETAILED_STEPPING)
707 mem_second = MemoryUsage();
708 mem_diff = mem_second - mem_first;
709 G4cout << "|| MEM || After computing reactions + UserPostTimeStepAction, "
710 "diff is : "
711 << mem_diff << G4endl;
712#endif
713
714 // End of step
715#ifdef G4VERBOSE
716 if (fVerbose >= 2) {
717# ifdef USE_COLOR
718 G4cout << LIGHT_RED;
719# endif
720
721 G4String interactionType;
722 GetCollisionType(interactionType);
723
724 std::stringstream finalOutput;
725
726 finalOutput << "*** End of step N°" << fNbSteps
727 << "\t T_i= " << G4BestUnit(fGlobalTime - fTimeStep, "Time")
728 << "\t dt= " << G4BestUnit(fTimeStep, "Time")
729 << "\t T_f= " << G4BestUnit(fGlobalTime, "Time") << "\t " << interactionType
730 << G4endl;
731
732 if (fVerbose > 2) {
733 if (fReachedUserTimeLimit) {
734 finalOutput << "It has also reached the user time limit" << G4endl;
735 }
736 finalOutput << "_______________________________________________________________"
737 "_______"
738 << G4endl;
739 }
740
741 G4cout << finalOutput.str();
742
743# ifdef USE_COLOR
745# endif
746 }
747#endif
748}
749//_________________________________________________________________________
750
752{
753 if (fpUserTimeSteps == nullptr) return fDefaultMinTimeStep;
754 if (fabs(fGlobalTime - fUserUpperTimeLimit) < fTimeTolerance) return fDefinedMinTimeStep;
755
756 auto it_fpUserTimeSteps_i = fpUserTimeSteps->upper_bound(fGlobalTime);
757 auto it_fpUserTimeSteps_low = fpUserTimeSteps->lower_bound(fGlobalTime);
758
759 if (it_fpUserTimeSteps_i == fpUserTimeSteps->end()) {
760 it_fpUserTimeSteps_i--;
761 fUserUpperTimeLimit = fStopTime;
762 }
763 else if (fabs(fGlobalTime - it_fpUserTimeSteps_low->first) < fTimeTolerance) {
764 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
765 auto tmp_it = it_fpUserTimeSteps_low;
766 ++tmp_it;
767 if (tmp_it == fpUserTimeSteps->end()) {
768 fUserUpperTimeLimit = fStopTime;
769 }
770 else {
771 fUserUpperTimeLimit = tmp_it->first;
772 }
773 }
774 else if (it_fpUserTimeSteps_i == it_fpUserTimeSteps_low) {
775 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
776 if (it_fpUserTimeSteps_i != fpUserTimeSteps->begin()) it_fpUserTimeSteps_i--;
777 }
778 else {
779 fUserUpperTimeLimit = it_fpUserTimeSteps_i->first;
780 it_fpUserTimeSteps_i = it_fpUserTimeSteps_low;
781 }
782 return it_fpUserTimeSteps_i->second;
783}
784
785//_________________________________________________________________________
786
788{
789 if (fpUserTimeSteps == nullptr) {
790 G4ExceptionDescription exceptionDescription;
791 exceptionDescription << "You are asking to use user defined steps but you did not give any.";
792 G4Exception("G4Scheduler::FindUserPreDefinedTimeStep", "Scheduler004", FatalErrorInArgument,
793 exceptionDescription);
794 return; // makes coverity happy
795 }
796 auto fpUserTimeSteps_i = fpUserTimeSteps->upper_bound(fGlobalTime);
797 auto fpUserTimeSteps_low = fpUserTimeSteps->lower_bound(fGlobalTime);
798
799 if (fpUserTimeSteps_i == fpUserTimeSteps->end()) {
800 fpUserTimeSteps_i--;
801 }
802 else if (fabs(fGlobalTime - fpUserTimeSteps_low->first) < fTimeTolerance) {
803 // Case : fGlobalTime = X picosecond
804 // and fpUserTimeSteps_low->first = X picosecond
805 // but the precision is not good enough
806 fpUserTimeSteps_i = fpUserTimeSteps_low;
807 }
808 else if (fpUserTimeSteps_i == fpUserTimeSteps_low) {
809 // "Normal" cases
810 fpUserTimeSteps_i--;
811 }
812 else {
813 fpUserTimeSteps_i = fpUserTimeSteps_low;
814 }
815
816 fDefinedMinTimeStep = fpUserTimeSteps_i->second;
817}
818
819//_________________________________________________________________________
820
822{
823 if (fRunning) {
824 G4ExceptionDescription exceptionDescription;
825 exceptionDescription << "End tracking is called while G4Scheduler is still running." << G4endl;
826
827 G4Exception("G4Scheduler::EndTracking", "Scheduler017", FatalErrorInArgument,
828 exceptionDescription);
829 }
830
831 while (fTrackContainer.DelayListsNOTEmpty()) {
832 auto nextTime = fTrackContainer.GetNextTime();
833 fTrackContainer.MergeNextTimeToMainList(nextTime);
834 }
835
836 fTrackContainer.MergeSecondariesWithMainList();
837
838 if (fTrackContainer.MainListsNOTEmpty()) {
839 G4TrackManyList* mainList = fTrackContainer.GetMainList();
840 G4TrackManyList::iterator it = mainList->begin();
841 G4TrackManyList::iterator end = mainList->end();
842 for (; it != end; ++it) {
843 fpTrackingManager->EndTrackingWOKill(*it);
844 }
845 }
846
847 if (fTrackContainer.SecondaryListsNOTEmpty()) // should be empty
848 {
849 G4TrackManyList* secondaries = fTrackContainer.GetSecondariesList();
850 G4TrackManyList::iterator it = secondaries->begin();
851 G4TrackManyList::iterator end = secondaries->end();
852
853 for (; it != end; ++it) {
854 fpTrackingManager->EndTrackingWOKill(*it);
855 }
856 }
857}
858
859//_________________________________________________________________________
861{
862 fpTrackingInteractivity = interactivity;
863 if (fpTrackingManager != nullptr) {
864 fpTrackingManager->SetInteractivity(fpTrackingInteractivity);
865 }
866}
867
868//_________________________________________________________________________
870{
871 fInitialized = false;
872 Initialize();
873}
874
876{
877 return fTrackContainer.GetNTracks();
878}
879//_________________________________________________________________________
880
882{
883 switch (fITStepStatus) {
885 interactionType = "eInteractionWithMedium";
886 break;
888 interactionType = "eCollisionBetweenTracks";
889 break;
890 default:
891 interactionType = "eCollisionBetweenTracks";
892 break;
893 }
894}
G4ApplicationState
@ G4State_Quit
@ FatalErrorInArgument
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
@ eInteractionWithMedium
@ eCollisionBetweenTracks
@ eUndefined
G4bool IsInf(T value)
#define LIGHT_RED
#define RESET_COLOR
#define G4BestUnit(a, b)
G4ManyFastLists< G4Track > G4TrackManyList
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
static void DeleteInstance()
static G4ITReactionSet * Instance()
void ReserveRessource()
Definition G4ITType.cc:76
static G4ITTypeManager * Instance()
Definition G4ITType.cc:57
void ReleaseRessource()
Definition G4ITType.cc:82
G4ManyFastLists_iterator< G4Track > iterator
void SynchronizeTracks()
G4bool CanICarryOn()
void EndTracking()
void FindUserPreDefinedTimeStep()
void ForceReinitialization()
G4bool Notify(G4ApplicationState requestedState) override
G4double GetNextWatchedTime() const
void Reset() override
void Stepping()
static G4Scheduler * Instance()
void Process() override
void ClearList()
void Initialize() override
virtual size_t GetNTracks()
G4Scheduler(const G4Scheduler &)=delete
void GetCollisionType(G4String &interactionType)
void SetInteractivity(G4ITTrackingInteractivity *) override
G4double GetLimitingTimeStep() const override
void DoProcess()
void PrintWhyDoYouStop()
~G4Scheduler() override
void RegisterModel(G4VITStepModel *, G4double) override
static void DeleteInstance()
void Stop()
void Start()
MemStat MemoryUsage()
Definition G4MemStat.cc:55
#define DBL_MAX
Definition templates.hh:62
#define G4ThreadLocal
Definition tls.hh:77