Geant4-11
G4AdjointSimManager.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// G4AdjointCrossSurfChecker implementation
27//
28// --------------------------------------------------------------------
29// Class Name: G4AdjointCrossSurfChecker
30// Author: L. Desorgher, 2007-2009
31// Organisation: SpaceIT GmbH
32// Contract: ESA contract 21435/08/NL/AT
33// Customer: ESA/ESTEC
34// --------------------------------------------------------------------
35
37#include "G4Run.hh"
38#include "G4RunManager.hh"
39
40#include "G4UserEventAction.hh"
41#include "G4UserRunAction.hh"
46
51
53
55
56#include "G4ParticleTable.hh"
57#include "G4PhysicsLogVector.hh"
58
59// --------------------------------------------------------------------
60//
62
63// --------------------------------------------------------------------
64//
66{
67 // Create adjoint actions;
68 //----------------------
77
78 // Create messenger
79 //----------------
81
82 // Define user action and set this class instance as RunAction
83 //----------------
84 // DefineUserActions();
85 // G4RunManager* theRunManager = G4RunManager::GetRunManager();
86
87 // theRunManager->G4RunManager::SetUserAction(this);
88}
89
90// --------------------------------------------------------------------
91//
93{
100 delete theMessenger;
101}
102
103// --------------------------------------------------------------------
104//
106{
107 if(instance == nullptr)
109 return instance;
110}
111
112// --------------------------------------------------------------------
113//
115{
116 if(G4RunManager::GetRunManager()->GetRunManagerType() !=
118 return; // only for sequential mode
120 {
121 G4cout << "****************************************************************"
122 << std::endl;
123 G4cout << "*** Geant4 Reverse/Adjoint Monte Carlo mode ***"
124 << std::endl;
125 G4cout << "*** Author: L.Desorgher ***"
126 << std::endl;
127 G4cout << "*** Company: SpaceIT GmbH, Bern, Switzerland ***"
128 << std::endl;
129 G4cout << "*** Sponsored by: ESA/ESTEC contract contract 21435/08/NL/AT ***"
130 << std::endl;
131 G4cout << "****************************************************************"
132 << std::endl;
133 welcome_message = false;
134 }
135
136 // Switch to adjoint simulation mode
137 //---------------------------------------------------------
139
140 // Make the run
141 //------------
142 nb_evt_of_last_run = nb_evt;
145
146 // Back to Fwd Simulation Mode
147 //--------------------------------
149
150 /*
151 //Register the weight vector
152 //--------------------------
153 std::ofstream FileOutputElectronWeight("ElectronWeight.txt", std::ios::out);
154 FileOutputElectronWeight<<std::setiosflags(std::ios::scientific);
155 FileOutputElectronWeight<<std::setprecision(6);
156 G4bool aBool = electron_last_weight_vector->Store(FileOutputElectronWeight,
157 true); FileOutputElectronWeight.close();
158
159 std::ofstream FileOutputProtonWeight("ProtonWeight.txt", std::ios::out);
160 FileOutputProtonWeight<<std::setiosflags(std::ios::scientific);
161 FileOutputProtonWeight<<std::setprecision(6);
162 aBool = proton_last_weight_vector->Store(FileOutputProtonWeight, true);
163 FileOutputProtonWeight.close();
164
165 std::ofstream FileOutputGammaWeight("GammaWeight.txt", std::ios::out);
166 FileOutputGammaWeight<<std::setiosflags(std::ios::scientific);
167 FileOutputGammaWeight<<std::setprecision(6);
168 aBool = gamma_last_weight_vector->Store(FileOutputGammaWeight, true);
169 FileOutputGammaWeight.close();
170 */
171}
172
173// --------------------------------------------------------------------
174//
176{
177 G4RunManager* theRunManager = G4RunManager::GetRunManager();
178
181
182 // Replace the user action by the adjoint actions
183 //-------------------------------------------------
184
185 theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
186 theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
187 theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
188}
189
190// --------------------------------------------------------------------
191//
193{
194 // Replace the user defined actions by the adjoint actions
195 //---------------------------------------------------------
197
198 // Update the list of primaries
199 //-----------------------------
201 adjoint_sim_mode = true;
203}
204
205// --------------------------------------------------------------------
206//
208{
209 // Restore the user defined actions
210 //--------------------------------
212 adjoint_sim_mode = false;
213}
214
215// --------------------------------------------------------------------
216//
218{
219 G4RunManager* theRunManager = G4RunManager::GetRunManager();
220
223
224 // Replace the user action by the adjoint actions
225 //-------------------------------------------------
226 theRunManager->G4RunManager::SetUserAction(this);
227 theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
228 theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
231 else
233 theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
234 theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
235 theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
238 else
240}
241
242// --------------------------------------------------------------------
243//
245{
246 G4RunManager* theRunManager = G4RunManager::GetRunManager();
247
250
251 // Replace the user action by the adjoint actions
252 //-------------------------------------------------
253
254 theRunManager->G4RunManager::SetUserAction(theAdjointRunAction);
255 theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
256 theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
259 else
261}
262
263// --------------------------------------------------------------------
264//
266{
267 G4RunManager* theRunManager = G4RunManager::GetRunManager();
268
269 // Restore the user defined actions
270 //-------------------------------
271 theRunManager->G4RunManager::SetUserAction(fUserRunAction);
272 theRunManager->G4RunManager::SetUserAction(fUserEventAction);
273 theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
274 theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
275 theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
276 theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
277}
278
279// --------------------------------------------------------------------
280//
282{
283 G4RunManager* theRunManager = G4RunManager::GetRunManager();
284
285 // Restore the user defined actions
286 //-------------------------------
287
288 theRunManager->G4RunManager::SetUserAction(fUserEventAction);
289 theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
290 theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
291}
292
293// --------------------------------------------------------------------
294//
296{
297 G4RunManager* theRunManager = G4RunManager::GetRunManager();
298 // Restore the user defined actions
299 //-------------------------------
300 theRunManager->G4RunManager::SetUserAction(fUserRunAction);
301 theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
302 theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
303}
304
305// --------------------------------------------------------------------
306//
308{
309 G4RunManager* theRunManager = G4RunManager::GetRunManager();
311 const_cast<G4UserTrackingAction*>(theRunManager->GetUserTrackingAction());
313 const_cast<G4UserEventAction*>(theRunManager->GetUserEventAction());
315 const_cast<G4UserSteppingAction*>(theRunManager->GetUserSteppingAction());
318 theRunManager->GetUserPrimaryGeneratorAction());
320 const_cast<G4UserRunAction*>(theRunManager->GetUserRunAction());
322 const_cast<G4UserStackingAction*>(theRunManager->GetUserStackingAction());
324}
325
326// --------------------------------------------------------------------
327//
329{
331}
332
333// --------------------------------------------------------------------
334//
336 G4bool aBool) // could be removed
337{
338 adjoint_tracking_mode = aBool;
339
341 {
345 }
346 else
347 {
351 {
354 }
355 else
357 }
358}
359
360// --------------------------------------------------------------------
361//
363{
365}
366
367// --------------------------------------------------------------------
368//
369std::vector<G4ParticleDefinition*>*
371{
373}
374
375// --------------------------------------------------------------------
376//
378{
380 ->size();
381}
382
383// --------------------------------------------------------------------
384//
387{
389}
390
391// --------------------------------------------------------------------
392//
395{
397}
398
399// --------------------------------------------------------------------
400//
403{
405}
406
407// --------------------------------------------------------------------
408//
411{
413}
414
415// --------------------------------------------------------------------
416//
419{
421}
422
423// --------------------------------------------------------------------
424//
427{
429}
430
431// --------------------------------------------------------------------
432//
434{
436}
437
438// --------------------------------------------------------------------
439//
442{
445}
446
447// --------------------------------------------------------------------
448//
451{
453}
454
455// --------------------------------------------------------------------
456//
459{
462}
463
464// --------------------------------------------------------------------
465//
467{
469}
470
471// --------------------------------------------------------------------
472//
474{
480
482
483 last_fwd_part_name.erase(0, 4);
484
487 ->GetPDGEncoding();
488
489 std::vector<G4ParticleDefinition*>* aList =
492 std::size_t i = 0;
493 while(i < aList->size() && last_fwd_part_index < 0)
494 {
495 if((*aList)[i]->GetParticleName() == last_fwd_part_name)
497 ++i;
498 }
499
502 if(aPartDef->GetParticleType() == "adjoint_nucleus")
503 {
504 nb_nuc = double(aPartDef->GetBaryonNumber());
506 }
507
509
510 last_pos_vec.push_back(last_pos);
512 last_ekin_vec.push_back(last_ekin);
514 last_cos_th_vec.push_back(last_cos_th);
515 last_weight_vec.push_back(last_weight);
521}
522
523// --------------------------------------------------------------------
524//
527{
528 G4double area;
530 "ExternalSource", radius, pos, area);
531}
532
533// --------------------------------------------------------------------
534//
535G4bool
537 G4double radius, const G4String& volume_name)
538{
539 G4double area;
540 G4ThreeVector center;
543 "ExternalSource", radius, volume_name, center, area);
544}
545
546// --------------------------------------------------------------------
547//
549 const G4String& volume_name)
550{
551 G4double area;
553 "ExternalSource", volume_name, area);
554}
555
556// --------------------------------------------------------------------
557//
559{
561}
562
563// --------------------------------------------------------------------
564//
567{
568 G4double area;
570 "AdjointSource", radius, pos, area);
572 pos);
574 return aBool;
575}
576
577// --------------------------------------------------------------------
578//
579G4bool
581 G4double radius, const G4String& volume_name)
582{
583 G4double area;
584 G4ThreeVector center;
587 "AdjointSource", radius, volume_name, center, area);
589 center);
591 return aBool;
592}
593
594// --------------------------------------------------------------------
595//
597 const G4String& volume_name)
598{
599 G4double area;
600 G4bool aBool =
602 "AdjointSource", volume_name, area);
604 if(aBool)
605 {
608 }
609 return aBool;
610}
611
612// --------------------------------------------------------------------
613//
615{
617}
618
619// --------------------------------------------------------------------
620//
622{
624}
625
626// --------------------------------------------------------------------
627//
629 const G4String& particle_name)
630{
632}
633
634// --------------------------------------------------------------------
635//
637 const G4String& particle_name)
638{
640}
641
642// --------------------------------------------------------------------
643//
645 G4ParticleDefinition* fwdIon)
646{
648}
649
650// --------------------------------------------------------------------
651//
653{
655}
656
657// --------------------------------------------------------------------
658//
660{
661 theAdjointPrimaryWeight = aWeight;
663}
664
665// --------------------------------------------------------------------
666//
668{
669 theAdjointEventAction = anAction;
670}
671
672// --------------------------------------------------------------------
673//
675 G4UserSteppingAction* anAction)
676{
678}
679
680// --------------------------------------------------------------------
681//
683 G4UserStackingAction* anAction)
684{
686}
687
688
689// --------------------------------------------------------------------
690//
692{
693 theAdjointRunAction = anAction;
694}
695
696// --------------------------------------------------------------------
697//
699{
701}
702
703// --------------------------------------------------------------------
704//
706{
708}
709
710// --------------------------------------------------------------------
711//
713{
715}
716
717// --------------------------------------------------------------------
718//
720{
721 /*
722 if (!adjoint_sim_mode){
723 if(fUserRunAction) fUserRunAction->BeginOfRunAction(aRun);
724 }
725 else {
726 if (theAdjointRunAction) theAdjointRunAction->BeginOfRunAction(aRun);
727 }
728 */
730}
731
732// --------------------------------------------------------------------
733//
735{
737 {
740 }
741 else if(theAdjointRunAction)
743 /*
744 #ifdef G4MULTITHREADED
745 if (G4RunManager::GetRunManager()->GetRunManagerType() ==
746 G4RunManager::workerRM)
747 {
748 if (adjoint_sim_mode) BackToFwdSimulationMode();
749 }
750 #endif
751 */
752}
753
754// --------------------------------------------------------------------
755//
757{
759}
760
761// --------------------------------------------------------------------
762//
764{
766}
static const G4double pos
static const G4double Emax
static const G4double Emin
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cout
double z() const
double mag() const
G4bool AddaSphericalSurface(const G4String &SurfaceName, G4double radius, G4ThreeVector pos, G4double &area)
G4bool AddanExtSurfaceOfAvolume(const G4String &SurfaceName, const G4String &volume_name, G4double &area)
static G4AdjointCrossSurfChecker * GetInstance()
G4bool AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume(const G4String &SurfaceName, G4double radius, const G4String &volume_name, G4ThreeVector &center, G4double &area)
std::vector< G4ParticleDefinition * > * GetListOfPrimaryFwdParticles()
void ConsiderParticleAsPrimary(const G4String &particle_name)
void SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(const G4String &volume_name)
void SetSphericalAdjointPrimarySource(G4double radius, G4ThreeVector pos)
G4ParticleDefinition * GetLastGeneratedFwdPrimaryParticle()
void NeglectParticleAsPrimary(const G4String &particle_name)
void SetPrimaryIon(G4ParticleDefinition *adjointIon, G4ParticleDefinition *fwdIon)
void SetAdjointStackingAction(G4UserStackingAction *anAction)
G4UserRunAction * theAdjointRunAction
G4bool DefineAdjointSourceOnTheExtSurfaceOfAVolume(const G4String &volume_name)
G4AdjointSteppingAction * theAdjointSteppingAction
const G4String & GetPrimaryIonName()
G4bool DefineExtSourceOnTheExtSurfaceOfAVolume(const G4String &volume_name)
G4double GetEkinAtEndOfLastAdjointTrack(std::size_t i=0)
G4int ID_of_last_particle_that_reach_the_ext_source
void ResetUserPrimaryRunAndStackingActions()
static G4ThreadLocal G4AdjointSimManager * instance
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(std::size_t i=0)
G4bool GetDidAdjParticleReachTheExtSource()
std::vector< G4ParticleDefinition * > * GetListOfPrimaryFwdParticles()
void RunAdjointSimulation(G4int nb_evt)
G4double GetCosthAtEndOfLastAdjointTrack(std::size_t i=0)
std::vector< G4int > ID_of_last_particle_that_reach_the_ext_source_vec
std::vector< G4ThreeVector > last_pos_vec
G4UserTrackingAction * fUserTrackingAction
const G4String & GetFwdParticleNameAtEndOfLastAdjointTrack()
G4bool DefineSphericalExtSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
G4UserSteppingAction * fUserSteppingAction
void SetNbAdjointPrimaryGammasPerEvent(G4int)
void SetAdjointTrackingMode(G4bool aBool)
std::vector< G4int > last_fwd_part_index_vec
G4AdjointStackingAction * theAdjointStackingAction
void ConsiderParticleAsPrimary(const G4String &particle_name)
G4UserEventAction * fUserEventAction
std::size_t GetNbOfPrimaryFwdParticles()
void SetAdjointRunAction(G4UserRunAction *anAction)
void SetExtSourceEmax(G4double Emax)
virtual void BeginOfRunAction(const G4Run *aRun)
void SetAdjointSourceEmax(G4double Emax)
std::vector< G4int > last_fwd_part_PDGEncoding_vec
void RegisterAdjointPrimaryWeight(G4double aWeight)
void SetAdjointSourceEmin(G4double Emin)
virtual void EndOfRunAction(const G4Run *aRun)
G4double GetWeightAtEndOfLastAdjointTrack(std::size_t i=0)
void SetAdjointPrimaryRunAndStackingActions()
G4UserRunAction * fUserRunAction
void ResetDidOneAdjPartReachExtSourceDuringEvent()
void SetAdjointEventAction(G4UserEventAction *anAction)
std::vector< G4double > last_cos_th_vec
G4UserEventAction * theAdjointEventAction
void NeglectParticleAsPrimary(const G4String &particle_name)
G4bool DefineSphericalAdjointSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
G4AdjointSimMessenger * theMessenger
G4AdjointTrackingAction * theAdjointTrackingAction
void SetNbAdjointPrimaryElectronsPerEvent(G4int)
G4AdjointPrimaryGeneratorAction * theAdjointPrimaryGeneratorAction
static G4AdjointSimManager * GetInstance()
G4int GetFwdParticleIndexAtEndOfLastAdjointTrack(std::size_t i=0)
G4VUserPrimaryGeneratorAction * fUserPrimaryGeneratorAction
G4double GetEkinNucAtEndOfLastAdjointTrack(std::size_t i=0)
std::vector< G4double > last_weight_vec
void SetNbOfPrimaryFwdGammasPerEvent(G4int)
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack(std::size_t i=0)
G4UserStackingAction * fUserStackingAction
std::vector< G4double > last_ekin_nuc_vec
G4ParticleDefinition * GetLastGeneratedFwdPrimaryParticle()
void SetAdjointSteppingAction(G4UserSteppingAction *anAction)
G4bool DefineSphericalExtSource(G4double radius, G4ThreeVector pos)
G4ThreeVector GetPositionAtEndOfLastAdjointTrack(std::size_t i=0)
std::size_t GetNbOfAdointTracksReachingTheExternalSurface()
std::vector< G4double > last_ekin_vec
G4bool DefineSphericalAdjointSource(G4double radius, G4ThreeVector pos)
void SetPrimaryIon(G4ParticleDefinition *adjointIon, G4ParticleDefinition *fwdIon)
std::vector< G4ThreeVector > last_direction_vec
void SetUserFwdStackingAction(G4UserStackingAction *anAction)
void SetUserAdjointStackingAction(G4UserStackingAction *anAction)
G4ParticleDefinition * GetLastPartDef()
void SetUserForwardSteppingAction(G4UserSteppingAction *anAction)
void SetExtSourceEMax(G4double Emax)
void SetUserAdjointSteppingAction(G4UserSteppingAction *anAction)
void SetPrimWeight(G4double weight)
G4double GetEkinAtEndOfLastAdjointTrack(std::size_t i=0)
void SetListOfPrimaryFwdParticles(std::vector< G4ParticleDefinition * > *aListOfParticles)
G4double GetEkinNucAtEndOfLastAdjointTrack(std::size_t i=0)
G4ThreeVector GetPositionAtEndOfLastAdjointTrack(std::size_t i=0)
const G4String & GetFwdParticleNameAtEndOfLastAdjointTrack()
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(std::size_t i=0)
G4double GetWeightAtEndOfLastAdjointTrack(std::size_t i=0)
std::size_t GetNbOfAdointTracksReachingTheExternalSurface()
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack(std::size_t i=0)
G4int GetLastFwdParticleIndex(std::size_t i=0)
G4double GetCosthAtEndOfLastAdjointTrack(std::size_t i=0)
void SetUserForwardTrackingAction(G4UserTrackingAction *anAction)
const G4String & GetParticleType() const
const G4String & GetParticleName() const
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
static G4ParticleTable * GetParticleTable()
const G4UserTrackingAction * GetUserTrackingAction() const
const G4VUserPrimaryGeneratorAction * GetUserPrimaryGeneratorAction() const
const G4UserEventAction * GetUserEventAction() const
static G4RunManager * GetRunManager()
virtual void BeamOn(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
const G4UserStackingAction * GetUserStackingAction() const
const G4UserSteppingAction * GetUserSteppingAction() const
const G4UserRunAction * GetUserRunAction() const
Definition: G4Run.hh:49
virtual void EndOfRunAction(const G4Run *aRun)
virtual void BeginOfRunAction(const G4Run *aRun)
#define G4ThreadLocal
Definition: tls.hh:77