Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/run/src/G4AdjointSimManager.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 
 36 #include "G4AdjointSimManager.hh"
 37 
 38 #include "G4AdjointCrossSurfChecker.hh"
 39 #include "G4AdjointPrimaryGeneratorAction.hh"
 40 #include "G4AdjointSimMessenger.hh"
 41 #include "G4AdjointStackingAction.hh"
 42 #include "G4AdjointSteppingAction.hh"
 43 #include "G4AdjointTrackingAction.hh"
 44 #include "G4ParticleTable.hh"
 45 #include "G4PhysicsLogVector.hh"
 46 #include "G4Run.hh"
 47 #include "G4RunManager.hh"
 48 #include "G4WorkerRunManager.hh"
 49 #include "G4UserEventAction.hh"
 50 #include "G4UserRunAction.hh"
 51 #include "G4UserStackingAction.hh"
 52 #include "G4UserSteppingAction.hh"
 53 #include "G4UserTrackingAction.hh"
 54 #include "G4VUserPrimaryGeneratorAction.hh"
 55 
 56 // --------------------------------------------------------------------
 57 //
 58 G4ThreadLocal G4AdjointSimManager* G4AdjointSimManager::instance = nullptr;
 59 
 60 // --------------------------------------------------------------------
 61 //
 62 G4AdjointSimManager::G4AdjointSimManager()
 63 {
 64   theAdjointPrimaryGeneratorAction = new G4AdjointPrimaryGeneratorAction();
 65   theAdjointSteppingAction = new G4AdjointSteppingAction();
 66   theAdjointTrackingAction = new G4AdjointTrackingAction(theAdjointSteppingAction);
 67   theAdjointStackingAction = new G4AdjointStackingAction(theAdjointTrackingAction);
 68   theAdjointTrackingAction->SetListOfPrimaryFwdParticles(
 69   theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles());
 70 
 71   theMessenger = new G4AdjointSimMessenger(this);
 72 }
 73 
 74 // --------------------------------------------------------------------
 75 //
 76 G4AdjointSimManager::~G4AdjointSimManager()
 77 {
 78   delete theAdjointRunAction;
 79   delete theAdjointPrimaryGeneratorAction;
 80   delete theAdjointSteppingAction;
 81   delete theAdjointEventAction;
 82   delete theAdjointTrackingAction;
 83   delete theAdjointStackingAction;
 84   delete theMessenger;
 85 }
 86 
 87 // --------------------------------------------------------------------
 88 //
 89 G4AdjointSimManager* G4AdjointSimManager::GetInstance()
 90 {
 91   if (instance == nullptr) instance = new G4AdjointSimManager;
 92   return instance;
 93 }
 94 
 95 // --------------------------------------------------------------------
 96 //
 97 void G4AdjointSimManager::RunAdjointSimulation(G4int nb_evt)
 98 {
 99 
100   if (welcome_message) {
101     G4cout << "****************************************************************" << std::endl;
102     G4cout << "*** Geant4 Reverse/Adjoint Monte Carlo mode                  ***" << std::endl;
103     G4cout << "*** Author:       L.Desorgher                                ***" << std::endl;
104     G4cout << "*** Company:      SpaceIT GmbH, Bern, Switzerland            ***" << std::endl;
105     G4cout << "*** Sponsored by: ESA/ESTEC contract contract 21435/08/NL/AT ***" << std::endl;
106     G4cout << "****************************************************************" << std::endl;
107     welcome_message = false;
108   }
109 
110   // Switch to adjoint simulation mode
111   //---------------------------------------------------------
112   SwitchToAdjointSimulationMode();
113 
114   // Make the run
115   //------------
116   nb_evt_of_last_run = nb_evt;
117   if (G4Threading::G4GetThreadId() < 0){
118     G4RunManager::GetRunManager()->BeamOn(
119     G4int(nb_evt * theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes()));
120   }
121 }
122 
123 // --------------------------------------------------------------------
124 //
125 void G4AdjointSimManager::SetRestOfAdjointActions()
126 { if (G4Threading::G4GetThreadId() == -1) return;
127   G4RunManager* theRunManager = G4RunManager::GetRunManager();
128 
129   if (!user_action_already_defined) DefineUserActions();
130 
131   // Replace the user action by the adjoint actions
132   //-------------------------------------------------
133 
134   theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
135   theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
136   theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
137 }
138 
139 // --------------------------------------------------------------------
140 //
141 void G4AdjointSimManager::SwitchToAdjointSimulationMode()
142 {
143   // Replace the user defined actions by the adjoint actions
144   //---------------------------------------------------------
145   SetAdjointActions();
146 
147   // Update the list of primaries
148   //-----------------------------
149   if (G4Threading::G4GetThreadId() != -1)  theAdjointPrimaryGeneratorAction->UpdateListOfPrimaryParticles();
150   adjoint_sim_mode = true;
151   ID_of_last_particle_that_reach_the_ext_source = 0;
152 
153 }
154 
155 // --------------------------------------------------------------------
156 //
157 void G4AdjointSimManager::BackToFwdSimulationMode()
158 {
159   // Restore the user defined actions
160   //--------------------------------
161   ResetUserActions();
162   adjoint_sim_mode = false;
163 }
164 
165 // --------------------------------------------------------------------
166 //
167 void G4AdjointSimManager::SetAdjointActions()
168 {
169   auto theRunManager = G4RunManager::GetRunManager();
170 
171 
172   if (!user_action_already_defined) DefineUserActions();
173 
174 
175   // Replace the user action by the adjoint actions
176   //-------------------------------------------------
177   theRunManager->G4RunManager::SetUserAction(this);
178   if (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM ||
179       G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::workerRM) {
180   theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
181   theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
182   if (use_user_StackingAction)
183     theAdjointStackingAction->SetUserFwdStackingAction(fUserStackingAction);
184   else
185     theAdjointStackingAction->SetUserFwdStackingAction(nullptr);
186   theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
187   theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
188   theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
189   if (use_user_TrackingAction)
190     theAdjointTrackingAction->SetUserForwardTrackingAction(fUserTrackingAction);
191   else
192     theAdjointTrackingAction->SetUserForwardTrackingAction(nullptr);
193 }
194 }
195 
196 // --------------------------------------------------------------------
197 //
198 void G4AdjointSimManager::SetAdjointPrimaryRunAndStackingActions()
199 {
200   G4RunManager* theRunManager = G4RunManager::GetRunManager();
201 
202   if (!user_action_already_defined) DefineUserActions();
203 
204   // Replace the user action by the adjoint actions
205   //-------------------------------------------------
206 
207   theRunManager->G4RunManager::SetUserAction(theAdjointRunAction);
208   if (G4Threading::G4GetThreadId() != -1) {
209   theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
210   theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
211   if (use_user_StackingAction)
212     theAdjointStackingAction->SetUserFwdStackingAction(fUserStackingAction);
213   else
214     theAdjointStackingAction->SetUserFwdStackingAction(nullptr);
215 }
216 }
217 
218 // --------------------------------------------------------------------
219 //
220 void G4AdjointSimManager::ResetUserActions()
221 {
222   G4RunManager* theRunManager = G4RunManager::GetRunManager();
223 
224   // Restore the user defined actions
225   //-------------------------------
226   theRunManager->G4RunManager::SetUserAction(fUserRunAction);
227   if (G4Threading::G4GetThreadId() != -1) {
228   theRunManager->G4RunManager::SetUserAction(fUserEventAction);
229   theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
230   theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
231   theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
232   theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
233   }
234 }
235 
236 // --------------------------------------------------------------------
237 //
238 void G4AdjointSimManager::ResetRestOfUserActions()
239 { 
240   if (G4Threading::G4GetThreadId() == -1) return;
241   G4RunManager* theRunManager = G4RunManager::GetRunManager();
242 
243   // Restore the user defined actions
244   //-------------------------------
245 
246   theRunManager->G4RunManager::SetUserAction(fUserEventAction);
247   theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
248   theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
249 }
250 
251 // --------------------------------------------------------------------
252 //
253 void G4AdjointSimManager::ResetUserPrimaryRunAndStackingActions()
254 {
255   G4RunManager* theRunManager = G4RunManager::GetRunManager();
256   // Restore the user defined actions
257   //-------------------------------
258   theRunManager->G4RunManager::SetUserAction(fUserRunAction);
259   if (G4Threading::G4GetThreadId() != -1) {
260   theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
261   theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
262   }
263 }
264 
265 // --------------------------------------------------------------------
266 //
267 void G4AdjointSimManager::DefineUserActions()
268 {
269   G4RunManager* theRunManager = G4RunManager::GetRunManager();
270   fUserRunAction =
271     const_cast<G4UserRunAction*>(theRunManager->GetUserRunAction());
272  if (G4Threading::G4GetThreadId() != -1) {
273   fUserTrackingAction = const_cast<G4UserTrackingAction*>(theRunManager->GetUserTrackingAction());
274   fUserEventAction = const_cast<G4UserEventAction*>(theRunManager->GetUserEventAction());
275   fUserSteppingAction = const_cast<G4UserSteppingAction*>(theRunManager->GetUserSteppingAction());
276   theAdjointSteppingAction->SetUserForwardSteppingAction(fUserSteppingAction);
277   fUserPrimaryGeneratorAction =
278     const_cast<G4VUserPrimaryGeneratorAction*>(theRunManager->GetUserPrimaryGeneratorAction());
279   fUserStackingAction = const_cast<G4UserStackingAction*>(theRunManager->GetUserStackingAction());
280   user_action_already_defined = true;
281  }
282 }
283 
284 // --------------------------------------------------------------------
285 //
286 G4bool G4AdjointSimManager::GetAdjointTrackingMode()
287 {
288   return theAdjointTrackingAction->GetIsAdjointTrackingMode();
289 }
290 
291 // --------------------------------------------------------------------
292 //
293 void G4AdjointSimManager::SetAdjointTrackingMode(G4bool aBool)  // could be removed
294 {
295   adjoint_tracking_mode = aBool;
296 
297   if (adjoint_tracking_mode) {
298     SetRestOfAdjointActions();
299     theAdjointStackingAction->SetAdjointMode(true);
300     theAdjointStackingAction->SetKillTracks(false);
301   }
302   else {
303     ResetRestOfUserActions();
304     theAdjointStackingAction->SetAdjointMode(false);
305     if (GetDidAdjParticleReachTheExtSource()) {
306       theAdjointStackingAction->SetKillTracks(false);
307       RegisterAtEndOfAdjointTrack();
308     }
309     else
310       theAdjointStackingAction->SetKillTracks(true);
311   }
312 }
313 
314 // --------------------------------------------------------------------
315 //
316 G4bool G4AdjointSimManager::GetDidAdjParticleReachTheExtSource()
317 {
318   return (GetNbOfAdointTracksReachingTheExternalSurface() > 0);
319 }
320 
321 // --------------------------------------------------------------------
322 //
323 std::vector<G4ParticleDefinition*>* G4AdjointSimManager::GetListOfPrimaryFwdParticles()
324 {
325   return theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
326 }
327 
328 // --------------------------------------------------------------------
329 //
330 std::size_t G4AdjointSimManager::GetNbOfPrimaryFwdParticles()
331 {
332   return theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles()->size();
333 }
334 
335 // --------------------------------------------------------------------
336 //
337 G4ThreeVector G4AdjointSimManager::GetPositionAtEndOfLastAdjointTrack(std::size_t i)
338 {
339   return theAdjointTrackingAction->GetPositionAtEndOfLastAdjointTrack(i);
340 }
341 
342 // --------------------------------------------------------------------
343 //
344 G4ThreeVector G4AdjointSimManager::GetDirectionAtEndOfLastAdjointTrack(std::size_t i)
345 {
346   return theAdjointTrackingAction->GetDirectionAtEndOfLastAdjointTrack(i);
347 }
348 
349 // --------------------------------------------------------------------
350 //
351 G4double G4AdjointSimManager::GetEkinAtEndOfLastAdjointTrack(std::size_t i)
352 {
353   return theAdjointTrackingAction->GetEkinAtEndOfLastAdjointTrack(i);
354 }
355 
356 // --------------------------------------------------------------------
357 //
358 G4double G4AdjointSimManager::GetEkinNucAtEndOfLastAdjointTrack(std::size_t i)
359 {
360   return theAdjointTrackingAction->GetEkinNucAtEndOfLastAdjointTrack(i);
361 }
362 
363 // --------------------------------------------------------------------
364 //
365 G4double G4AdjointSimManager::GetWeightAtEndOfLastAdjointTrack(std::size_t i)
366 {
367   return theAdjointTrackingAction->GetWeightAtEndOfLastAdjointTrack(i);
368 }
369 
370 // --------------------------------------------------------------------
371 //
372 G4double G4AdjointSimManager::GetCosthAtEndOfLastAdjointTrack(std::size_t i)
373 {
374   return theAdjointTrackingAction->GetCosthAtEndOfLastAdjointTrack(i);
375 }
376 
377 // --------------------------------------------------------------------
378 //
379 const G4String& G4AdjointSimManager::GetFwdParticleNameAtEndOfLastAdjointTrack()
380 {
381   return theAdjointTrackingAction->GetFwdParticleNameAtEndOfLastAdjointTrack();
382 }
383 
384 // --------------------------------------------------------------------
385 //
386 G4int G4AdjointSimManager::GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(std::size_t i)
387 {
388   return theAdjointTrackingAction->GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(i);
389 }
390 
391 // --------------------------------------------------------------------
392 //
393 G4int G4AdjointSimManager::GetFwdParticleIndexAtEndOfLastAdjointTrack(std::size_t i)
394 {
395   return theAdjointTrackingAction->GetLastFwdParticleIndex(i);
396 }
397 
398 // --------------------------------------------------------------------
399 //
400 std::size_t G4AdjointSimManager::GetNbOfAdointTracksReachingTheExternalSurface()
401 {
402   return theAdjointTrackingAction->GetNbOfAdointTracksReachingTheExternalSurface();
403 }
404 
405 // --------------------------------------------------------------------
406 //
407 void G4AdjointSimManager::ClearEndOfAdjointTrackInfoVectors()
408 {
409   theAdjointTrackingAction->ClearEndOfAdjointTrackInfoVectors();
410 }
411 
412 // --------------------------------------------------------------------
413 //
414 void G4AdjointSimManager::RegisterAtEndOfAdjointTrack()
415 {
416   last_pos = theAdjointSteppingAction->GetLastPosition();
417   last_direction = theAdjointSteppingAction->GetLastMomentum();
418   last_direction /= last_direction.mag();
419   last_cos_th = last_direction.z();
420   G4ParticleDefinition* aPartDef = theAdjointSteppingAction->GetLastPartDef();
421 
422   last_fwd_part_name = aPartDef->GetParticleName();
423 
424   last_fwd_part_name.erase(0, 4);
425 
426   last_fwd_part_PDGEncoding =
427     G4ParticleTable::GetParticleTable()->FindParticle(last_fwd_part_name)->GetPDGEncoding();
428 
429   std::vector<G4ParticleDefinition*>* aList =
430     theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
431   last_fwd_part_index = -1;
432   G4int i = 0;
433   while (i < (G4int)aList->size() && last_fwd_part_index < 0) {
434     if ((*aList)[i]->GetParticleName() == last_fwd_part_name) last_fwd_part_index = i;
435     ++i;
436   }
437 
438   last_ekin = theAdjointSteppingAction->GetLastEkin();
439   last_ekin_nuc = last_ekin;
440   if (aPartDef->GetParticleType() == "adjoint_nucleus") {
441     nb_nuc = static_cast<G4double>(aPartDef->GetBaryonNumber());
442     last_ekin_nuc /= nb_nuc;
443   }
444 
445   last_weight = theAdjointSteppingAction->GetLastWeight();
446 
447   last_pos_vec.push_back(last_pos);
448   last_direction_vec.push_back(last_direction);
449   last_ekin_vec.push_back(last_ekin);
450   last_ekin_nuc_vec.push_back(last_ekin_nuc);
451   last_cos_th_vec.push_back(last_cos_th);
452   last_weight_vec.push_back(last_weight);
453   last_fwd_part_PDGEncoding_vec.push_back(last_fwd_part_PDGEncoding);
454   last_fwd_part_index_vec.push_back(last_fwd_part_index);
455   ID_of_last_particle_that_reach_the_ext_source++;
456   ID_of_last_particle_that_reach_the_ext_source_vec.push_back(
457     ID_of_last_particle_that_reach_the_ext_source);
458 }
459 
460 // --------------------------------------------------------------------
461 //
462 G4bool G4AdjointSimManager::DefineSphericalExtSource(G4double radius, G4ThreeVector pos)
463 {
464   G4double area;
465   return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("ExternalSource", radius,
466                                                                         pos, area);
467 }
468 
469 // --------------------------------------------------------------------
470 //
471 G4bool G4AdjointSimManager::DefineSphericalExtSourceWithCentreAtTheCentreOfAVolume(
472   G4double radius, const G4String& volume_name)
473 {
474   G4double area;
475   G4ThreeVector center;
476   return G4AdjointCrossSurfChecker::GetInstance()
477     ->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume("ExternalSource", radius, volume_name,
478                                                          center, area);
479 }
480 
481 // --------------------------------------------------------------------
482 //
483 G4bool G4AdjointSimManager::DefineExtSourceOnTheExtSurfaceOfAVolume(const G4String& volume_name)
484 {
485   G4double area;
486   return G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume("ExternalSource",
487                                                                             volume_name, area);
488 }
489 
490 // --------------------------------------------------------------------
491 //
492 void G4AdjointSimManager::SetExtSourceEmax(G4double Emax)
493 {
494   theAdjointSteppingAction->SetExtSourceEMax(Emax);
495 }
496 
497 // --------------------------------------------------------------------
498 //
499 G4bool G4AdjointSimManager::DefineSphericalAdjointSource(G4double radius, G4ThreeVector pos)
500 {
501   G4double area;
502   G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("AdjointSource",
503                                                                                 radius, pos, area);
504   theAdjointPrimaryGeneratorAction->SetSphericalAdjointPrimarySource(radius, pos);
505   area_of_the_adjoint_source = area;
506   return aBool;
507 }
508 
509 // --------------------------------------------------------------------
510 //
511 G4bool G4AdjointSimManager::DefineSphericalAdjointSourceWithCentreAtTheCentreOfAVolume(
512   G4double radius, const G4String& volume_name)
513 {
514   G4double area;
515   G4ThreeVector center;
516   G4bool aBool =
517     G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume(
518       "AdjointSource", radius, volume_name, center, area);
519   theAdjointPrimaryGeneratorAction->SetSphericalAdjointPrimarySource(radius, center);
520   area_of_the_adjoint_source = area;
521   return aBool;
522 }
523 
524 // --------------------------------------------------------------------
525 //
526 G4bool G4AdjointSimManager::DefineAdjointSourceOnTheExtSurfaceOfAVolume(const G4String& volume_name)
527 {
528   G4double area;
529   G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume(
530     "AdjointSource", volume_name, area);
531   area_of_the_adjoint_source = area;
532   if (aBool) {
533     theAdjointPrimaryGeneratorAction->SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(volume_name);
534   }
535   return aBool;
536 }
537 
538 // --------------------------------------------------------------------
539 //
540 void G4AdjointSimManager::SetAdjointSourceEmin(G4double Emin)
541 {
542   theAdjointPrimaryGeneratorAction->SetEmin(Emin);
543 }
544 
545 // --------------------------------------------------------------------
546 //
547 void G4AdjointSimManager::SetAdjointSourceEmax(G4double Emax)
548 {
549   theAdjointPrimaryGeneratorAction->SetEmax(Emax);
550 }
551 
552 // --------------------------------------------------------------------
553 //
554 void G4AdjointSimManager::ConsiderParticleAsPrimary(const G4String& particle_name)
555 {
556   theAdjointPrimaryGeneratorAction->ConsiderParticleAsPrimary(particle_name);
557 }
558 
559 // --------------------------------------------------------------------
560 //
561 void G4AdjointSimManager::NeglectParticleAsPrimary(const G4String& particle_name)
562 {
563   theAdjointPrimaryGeneratorAction->NeglectParticleAsPrimary(particle_name);
564 }
565 
566 // --------------------------------------------------------------------
567 //
568 void G4AdjointSimManager::SetPrimaryIon(G4ParticleDefinition* adjointIon,
569                                         G4ParticleDefinition* fwdIon)
570 {
571   theAdjointPrimaryGeneratorAction->SetPrimaryIon(adjointIon, fwdIon);
572 }
573 
574 // --------------------------------------------------------------------
575 //
576 const G4String& G4AdjointSimManager::GetPrimaryIonName()
577 {
578   return theAdjointPrimaryGeneratorAction->GetPrimaryIonName();
579 }
580 
581 // --------------------------------------------------------------------
582 //
583 void G4AdjointSimManager::RegisterAdjointPrimaryWeight(G4double aWeight)
584 {
585   theAdjointPrimaryWeight = aWeight;
586   theAdjointSteppingAction->SetPrimWeight(aWeight);
587 }
588 
589 // --------------------------------------------------------------------
590 //
591 void G4AdjointSimManager::SetAdjointEventAction(G4UserEventAction* anAction)
592 {
593   theAdjointEventAction = anAction;
594 }
595 
596 // --------------------------------------------------------------------
597 //
598 void G4AdjointSimManager::SetAdjointSteppingAction(G4UserSteppingAction* anAction)
599 {
600   theAdjointSteppingAction->SetUserAdjointSteppingAction(anAction);
601 }
602 
603 // --------------------------------------------------------------------
604 //
605 void G4AdjointSimManager::SetAdjointStackingAction(G4UserStackingAction* anAction)
606 {
607   theAdjointStackingAction->SetUserAdjointStackingAction(anAction);
608 }
609 
610 // --------------------------------------------------------------------
611 //
612 void G4AdjointSimManager::SetAdjointRunAction(G4UserRunAction* anAction)
613 {
614   theAdjointRunAction = anAction;
615 }
616 
617 // --------------------------------------------------------------------
618 //
619 void G4AdjointSimManager::SetNbOfPrimaryFwdGammasPerEvent(G4int nb)
620 {
621   theAdjointPrimaryGeneratorAction->SetNbPrimaryFwdGammasPerEvent(nb);
622 }
623 
624 // --------------------------------------------------------------------
625 //
626 void G4AdjointSimManager::SetNbAdjointPrimaryGammasPerEvent(G4int nb)
627 {
628   theAdjointPrimaryGeneratorAction->SetNbAdjointPrimaryGammasPerEvent(nb);
629 }
630 
631 // --------------------------------------------------------------------
632 //
633 void G4AdjointSimManager::SetNbAdjointPrimaryElectronsPerEvent(G4int nb)
634 {
635   theAdjointPrimaryGeneratorAction->SetNbAdjointPrimaryElectronsPerEvent(nb);
636 }
637 
638 // --------------------------------------------------------------------
639 //
640 void G4AdjointSimManager::BeginOfRunAction(const G4Run* aRun)
641 {  if (!adjoint_sim_mode){
642     if(fUserRunAction  != nullptr) fUserRunAction->BeginOfRunAction(aRun);
643    }
644    else {
645     if (theAdjointRunAction  != nullptr) theAdjointRunAction->BeginOfRunAction(aRun);
646    }
647   
648   //fUserRunAction->BeginOfRunAction(aRun);
649 }
650 
651 // --------------------------------------------------------------------
652 //
653 void G4AdjointSimManager::EndOfRunAction(const G4Run* aRun)
654 {
655   if (!adjoint_sim_mode) {
656     if (fUserRunAction != nullptr) fUserRunAction->EndOfRunAction(aRun);
657   }
658   else if (theAdjointRunAction != nullptr)
659     theAdjointRunAction->EndOfRunAction(aRun);
660 
661   BackToFwdSimulationMode();
662 
663 }
664 
665 // --------------------------------------------------------------------
666 //
667 G4ParticleDefinition* G4AdjointSimManager::GetLastGeneratedFwdPrimaryParticle()
668 {
669   return theAdjointPrimaryGeneratorAction->GetLastGeneratedFwdPrimaryParticle();
670 }
671 
672 // --------------------------------------------------------------------
673 //
674 void G4AdjointSimManager::ResetDidOneAdjPartReachExtSourceDuringEvent()
675 {
676   theAdjointSteppingAction->ResetDidOneAdjPartReachExtSourceDuringEvent();
677 }
678