Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4AdjointSimManager.hh
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/////////////////////////////////////////////////////////////////////////////////
28// Class Name: G4AdjointSimManager.hh
29// Author: L. Desorgher
30// Organisation: SpaceIT GmbH
31// Contract: ESA contract 21435/08/NL/AT
32// Customer: ESA/ESTEC
33/////////////////////////////////////////////////////////////////////////////////
34//
35// CHANGE HISTORY
36// --------------
37// ChangeHistory:
38// -15-01-2007 creation by L. Desorgher
39// -March 2008 Redesigned as a non RunManager. L. Desorgher
40// -01-11-2009 Add the possibility to use user defined run, event, tracking,
41// stepping, and stacking actions during the adjoint tracking phase. L.
42// Desorgher
43//
44//
45//
46//-------------------------------------------------------------
47// Documentation:
48// This class represents the Manager of an adjoint/reverse MC simulation.
49// An adjoint run is divided in a serie of alternative adjoint and forward
50//tracking of adjoint and normal particles.
51//
52// Reverse tracking phase:
53// -----------------------
54// An adjoint particle of a given type (adjoint_e-, adjoint_gamma,...) is
55//first
56// generated on the so called adjoint source with a random energy (1/E
57// distribution) and direction. The adjoint source is the external surface of
58// a user defined volume or of a user defined sphere. The adjoint source
59// should contain one or several sensitive volumes and should be small compared
60// to the entire geometry. The user can set the min and max energy of the
61// adjoint source. After its generation the adjoint primary particle is tracked
62// bacward in the geometry till a user defined external surface (spherical or
63// boundary of a volume) or is killed before if it reaches a user defined
64// upper energy limit that represents the maximum energy of the external
65// source. During the reverse tracking, reverse processes take place where
66// the adjoint particle being tracked can be either scattered or transformed
67// in another type of adjoint paticle. During the reverse tracking the
68// G4SimulationManager replaces the user defined Primary, Run, ... actions, by
69// its own actions.
70//
71// Forward tracking phase
72// -----------------------
73// When an adjoint particle reaches the external surface its weight,type,
74//position, and directions are registered and a normal primary particle with a
75//type
76// equivalent to the last generated primary adjoint is generated with the
77// same energy, position but opposite direction and is tracked normally in the
78// sensitive region as in a fwd MC simulation. During this forward tracking
79// phase the event, stacking, stepping, tracking actions defined by the user for
80// its general fwd application are used. By this clear separation between
81// adjoint and fwd tracking phases , the code of the user developed for a fwd
82// simulation should be only slightly modified to adapt it for an adjoint
83// simulation. Indeed the computation of the signal is done by the same actions
84// or classes that the one used in the fwd simulation mode.
85//
86// Modification to brought in a existing G4 application to use the ReverseMC
87//method
88// -------------------------------
89// In order to be able to use the ReverseMC method in his simulation, the
90//user should
91// modify its code as such: 1) Adapt its physics list to use
92// ReverseProcesses for adjoint particles. An example of such physics list is
93// provided in an extended example. 2) Create an instance of
94// G4AdjointSimManager somewhere in the main code. 3) Modify the analysis
95// part of the code to normalise the signal computed during the fwd phase to the
96// weight of the last adjoint particle that reaches the external surface. This
97// is done by using the following method of G4AdjointSimManager.
98//
99// G4int GetIDOfLastAdjParticleReachingExtSource()
100// G4ThreeVector GetPositionAtEndOfLastAdjointTrack(){ return
101// last_pos;}
102// G4ThreeVector GetDirectionAtEndOfLastAdjointTrack(){ return
103// last_direction;} G4double GetEkinAtEndOfLastAdjointTrack(){ return
104// last_ekin;} G4double GetEkinNucAtEndOfLastAdjointTrack(){ return
105// last_ekin_nuc;} G4double GetWeightAtEndOfLastAdjointTrack(){return
106// last_weight;}
107// G4double GetCosthAtEndOfLastAdjointTrack(){return last_cos_th;}
108// G4String GetFwdParticleNameAtEndOfLastAdjointTrack(){return
109// last_fwd_part_name;} G4int
110// GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(){return
111// last_fwd_part_PDGEncoding;} G4int
112// GetFwdParticleIndexAtEndOfLastAdjointTrack().
113//
114// In orther to have a code working for both forward and adjoint
115//simulation
116// mode, the extra code needed in user actions for the adjoint simulation
117// mode can be seperated to the code needed only for the normal forward
118// simulation by using the following method
119//
120// G4bool GetAdjointSimMode() that return true if an adjoint
121//simulation
122// is running and false if not!
123//
124// Example of modification in the analysis part of the code:
125// -------------------------------------------------------------
126// Let say that in the forward simulation a G4 application computes the
127// energy
128// deposited in a volume. The user wants to normalise its results for an
129// external isotropic source of e- with differential spectrum given by f(E). A
130// possible modification of the code where the deposited energy Edep during an
131// event is registered would be the following
132//
133// G4AdjointSimManager* theAdjSimManager =
134// G4AdjointSimManager::GetInstance();
135// if (theAdjSimManager->GetAdjointSimMode()) {
136// //code of the user that should be consider only for forwrad
137//simulation G4double normalised_edep = 0.; if
138//(theAdjSimManager->GetFwdParticleNameAtEndOfLastAdjointTrack() == "e-"){
139//G4double ekin_prim =
140// theAdjSimManager->GetEkinAtEndOfLastAdjointTrack(); G4double
141// weight_prim = theAdjSimManager->GetWeightAtEndOfLastAdjointTrack();
142// normalised_edep = weight_prim*f(ekin_prim);
143// }
144// //then follow the code where normalised_edep is printed, or
145//registered
146// or whatever ....
147// }
148//
149// else { //code of the user that should be consider only for forward
150// simulation
151// }
152// Note that in this example a normalisation to only primary e- with only
153//one
154// spectrum f(E) is considered. The example code could be easily adapted
155// for a normalisatin to several spectra and several type of primary particles
156// in the same simulation.
157//
158
159#ifndef G4AdjointSimManager_h
160#define G4AdjointSimManager_h 1
161#include "G4ThreeVector.hh"
162#include "G4UserRunAction.hh"
163#include "globals.hh"
164#include <vector>
165
171class G4AdjointRunAction;
174class G4AdjointEventAction;
180class G4Run;
181
183{
184 public:
186
187 public: // public methods
188 virtual void BeginOfRunAction(const G4Run* aRun);
189 virtual void EndOfRunAction(const G4Run* aRun);
190 void RunAdjointSimulation(G4int nb_evt);
191
192 inline G4int GetNbEvtOfLastRun() { return nb_evt_of_last_run; }
193
194 void SetAdjointTrackingMode(G4bool aBool);
195 G4bool
196 GetAdjointTrackingMode(); // true if an adjoint track is being processed
198 {
199 return adjoint_sim_mode;
200 } // true if an adjoint simulation is running
201
206 // to continue here
208 {
209 return ID_of_last_particle_that_reach_the_ext_source;
210 };
223
224 std::vector<G4ParticleDefinition*>* GetListOfPrimaryFwdParticles();
226
229 G4double radius, const G4String& volume_name);
231 void SetExtSourceEmax(G4double Emax);
232
233 // Definition of adjoint source
234 //----------------------------
235
238 G4double radius, const G4String& volume_name);
240 const G4String& volume_name);
243 inline G4double GetAdjointSourceArea() { return area_of_the_adjoint_source; }
244 void ConsiderParticleAsPrimary(const G4String& particle_name);
245 void NeglectParticleAsPrimary(const G4String& particle_name);
246 void SetPrimaryIon(G4ParticleDefinition* adjointIon,
247 G4ParticleDefinition* fwdIon);
249
250 inline void SetNormalisationMode(G4int n) { normalisation_mode = n; };
251 G4int GetNormalisationMode() { return normalisation_mode; };
252 G4double GetNumberNucleonsInIon() { return nb_nuc; };
253
254 // Definition of user actions for the adjoint tracking phase
255 //----------------------------
259 void SetAdjointRunAction(G4UserRunAction* anAction);
260
261 // Set methods for user run actions
262 //--------------------------------
264 {
265 use_user_StackingAction = aBool;
266 }
268 {
269 use_user_TrackingAction = aBool;
270 }
271
272 // Set nb of primary fwd gamma
273 //---------------------------
275
276 // Set nb of adjoint primaries for reverse splitting
277 //-------------------------------------------------
280
281 // Convergence test
282 //-----------------------
283 /*
284 void RegisterSignalForConvergenceTest(G4double aSignal);
285 void DefineExponentialPrimarySpectrumForConvergenceTest(G4ParticleDefinition*
286 aPartDef, G4double E0); void
287 DefinePowerLawPrimarySpectrumForConvergenceTest(G4ParticleDefinition*
288 aPartDef, G4double alpha);
289
290 */
291
292 private:
293 static G4ThreadLocal G4AdjointSimManager* instance;
294
295 private: // methods
296 void SetRestOfAdjointActions();
297 void SetAdjointPrimaryRunAndStackingActions();
298 void SetAdjointActions();
299 void ResetRestOfUserActions();
300 void ResetUserPrimaryRunAndStackingActions();
301 void ResetUserActions();
302 void DefineUserActions();
303
304 public:
307
308 private: // constructor and destructor
311
312 private: // attributes
313 // Messenger
314 //----------
315 G4AdjointSimMessenger* theMessenger;
316
317 // user defined actions for the normal fwd simulation. Taken from the
318 // G4RunManager
319 //-------------------------------------------------
320 bool user_action_already_defined;
321 G4UserRunAction* fUserRunAction;
322 G4UserEventAction* fUserEventAction;
323 G4VUserPrimaryGeneratorAction* fUserPrimaryGeneratorAction;
324 G4UserTrackingAction* fUserTrackingAction;
325 G4UserSteppingAction* fUserSteppingAction;
326 G4UserStackingAction* fUserStackingAction;
327 bool use_user_StackingAction; // only for fwd part of the adjoint simulation
328 bool use_user_TrackingAction;
329
330 // action for adjoint simulation
331 //-----------------------------
332 G4UserRunAction* theAdjointRunAction;
333 G4UserEventAction* theAdjointEventAction;
334 G4AdjointPrimaryGeneratorAction* theAdjointPrimaryGeneratorAction;
335 G4AdjointTrackingAction* theAdjointTrackingAction;
336 G4AdjointSteppingAction* theAdjointSteppingAction;
337 G4AdjointStackingAction* theAdjointStackingAction;
338
339 // adjoint mode
340 //-------------
341 G4bool adjoint_tracking_mode;
342 G4bool adjoint_sim_mode;
343
344 // adjoint particle information on the external surface
345 //-----------------------------
346 std::vector<G4ThreeVector> last_pos_vec;
347 std::vector<G4ThreeVector> last_direction_vec;
348 std::vector<G4double> last_ekin_vec;
349 std::vector<G4double> last_ekin_nuc_vec;
350 std::vector<G4double> last_cos_th_vec;
351 std::vector<G4double> last_weight_vec;
352 std::vector<G4int> last_fwd_part_PDGEncoding_vec;
353 std::vector<G4int> last_fwd_part_index_vec;
354 std::vector<G4int> ID_of_last_particle_that_reach_the_ext_source_vec;
355
356 G4ThreeVector last_pos;
357 G4ThreeVector last_direction;
358 G4double last_ekin,
359 last_ekin_nuc; // last_ekin_nuc=last_ekin/nuc, nuc is 1 if not a nucleus
360 G4double last_cos_th;
361 G4String last_fwd_part_name;
362 G4int last_fwd_part_PDGEncoding;
363 G4int last_fwd_part_index;
364 G4double last_weight;
365 G4int ID_of_last_particle_that_reach_the_ext_source;
366
367 G4int nb_evt_of_last_run;
368 G4int normalisation_mode;
369
370 // Adjoint source
371 //--------------
372 G4double area_of_the_adjoint_source;
373 G4double nb_nuc;
374 G4double theAdjointPrimaryWeight;
375
376 // Weight Analysis
377 //----------
378 /*G4PhysicsLogVector* electron_last_weight_vector;
379 G4PhysicsLogVector* proton_last_weight_vector;
380 G4PhysicsLogVector* gamma_last_weight_vector;*/
381
382 G4bool welcome_message;
383
384 /* For the future
385 //Convergence test
386 //----------------
387
388 G4double normalised_signal;
389 G4double error_signal;
390 G4bool convergence_test_is_used;
391 G4bool power_law_spectrum_for_convergence_test; // true PowerLaw, ;
392 G4ParticleDefinition* the_par_def_for_convergence_test;
393 */
394};
395
396#endif
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
void SetAdjointStackingAction(G4UserStackingAction *anAction)
G4double GetEkinNucAtEndOfLastAdjointTrack(size_t i=0)
G4bool DefineAdjointSourceOnTheExtSurfaceOfAVolume(const G4String &volume_name)
G4double GetCosthAtEndOfLastAdjointTrack(size_t i=0)
const G4String & GetPrimaryIonName()
G4bool DefineExtSourceOnTheExtSurfaceOfAVolume(const G4String &volume_name)
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack(size_t i=0)
G4bool GetDidAdjParticleReachTheExtSource()
std::vector< G4ParticleDefinition * > * GetListOfPrimaryFwdParticles()
void RunAdjointSimulation(G4int nb_evt)
G4double GetEkinAtEndOfLastAdjointTrack(size_t i=0)
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(size_t i=0)
const G4String & GetFwdParticleNameAtEndOfLastAdjointTrack()
G4bool DefineSphericalExtSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
void SetNbAdjointPrimaryGammasPerEvent(G4int)
G4double GetWeightAtEndOfLastAdjointTrack(size_t i=0)
void SetAdjointTrackingMode(G4bool aBool)
G4int GetIDOfLastAdjParticleReachingExtSource()
void ConsiderParticleAsPrimary(const G4String &particle_name)
G4ThreeVector GetPositionAtEndOfLastAdjointTrack(size_t i=0)
void SetAdjointRunAction(G4UserRunAction *anAction)
void SetExtSourceEmax(G4double Emax)
virtual void BeginOfRunAction(const G4Run *aRun)
void SetAdjointSourceEmax(G4double Emax)
void RegisterAdjointPrimaryWeight(G4double aWeight)
void SetAdjointSourceEmin(G4double Emin)
virtual void EndOfRunAction(const G4Run *aRun)
void ResetDidOneAdjPartReachExtSourceDuringEvent()
void SetAdjointEventAction(G4UserEventAction *anAction)
void UseUserStackingActionInFwdTrackingPhase(G4bool aBool)
void NeglectParticleAsPrimary(const G4String &particle_name)
G4bool DefineSphericalAdjointSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
void UseUserTrackingActionInFwdTrackingPhase(G4bool aBool)
void SetNbAdjointPrimaryElectronsPerEvent(G4int)
static G4AdjointSimManager * GetInstance()
void SetNbOfPrimaryFwdGammasPerEvent(G4int)
size_t GetNbOfAdointTracksReachingTheExternalSurface()
G4ParticleDefinition * GetLastGeneratedFwdPrimaryParticle()
void SetAdjointSteppingAction(G4UserSteppingAction *anAction)
void SetNormalisationMode(G4int n)
G4int GetFwdParticleIndexAtEndOfLastAdjointTrack(size_t i=0)
G4bool DefineSphericalExtSource(G4double radius, G4ThreeVector pos)
G4bool DefineSphericalAdjointSource(G4double radius, G4ThreeVector pos)
void SetPrimaryIon(G4ParticleDefinition *adjointIon, G4ParticleDefinition *fwdIon)
Definition: G4Run.hh:49
#define G4ThreadLocal
Definition: tls.hh:77