Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-13 08:27:56

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // The `molcounters` example(s) are provided as part of Geant4-DNA
0027 // and any report or published result obtained using it shall cite
0028 // the respective Geant4-DNA collaboration publications.
0029 //
0030 // Reports or results obtained using the spatially-aware `MoleculeCounter`
0031 // provided in this example, shall further cite:
0032 //
0033 // Velten & Tomé, Radiation Physics and Chemistry, 2023 (10.1016/j.radphyschem.2023.111194)
0034 //
0035 //
0036 // Author: Christian Velten (2025)
0037 //
0038 
0039 #include "ActionInitialization.hh"
0040 
0041 #include "ChemistrySteppingAction.hh"
0042 #include "ChemistryTrackingManager.hh"
0043 #include "EventAction.hh"
0044 #include "MoleculeCounter.hh"
0045 #include "PrimaryGeneratorAction.hh"
0046 #include "RunAction.hh"
0047 #include "StackingAction.hh"
0048 
0049 #include "G4DNAChemistryManager.hh"
0050 #include "G4H2O.hh"
0051 #include "G4MoleculeCounter.hh"
0052 #include "G4MoleculeCounterManager.hh"
0053 #include "G4MoleculeReactionCounter.hh"
0054 #include "G4Scheduler.hh"
0055 #include "G4UserTimeStepAction.hh"
0056 
0057 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0058 void ActionInitialization::BuildForMaster() const
0059 {
0060   SetUserAction(new RunAction);
0061   if (!fBuildMultipleAndCustomMoleculeCounters)
0062     BuildMoleculeCounters();
0063   else
0064     BuildMultipleAndCustomMoleculeCounters();
0065 }
0066 
0067 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0068 void ActionInitialization::Build() const
0069 {
0070   SetUserAction(new PrimaryGeneratorAction());
0071 
0072   SetUserAction(new RunAction);
0073   SetUserAction(new EventAction);
0074   SetUserAction(new StackingAction);
0075 
0076   if (G4DNAChemistryManager::IsActivated()) {
0077     G4Scheduler::Instance()->SetUserAction(new G4UserTimeStepAction);
0078 
0079     auto chemTrackingManager = new ChemistryTrackingManager();
0080     chemTrackingManager->SetUserAction(new ChemistrySteppingAction);
0081     G4Scheduler::Instance()->SetInteractivity(chemTrackingManager);
0082 
0083     if (!fBuildMultipleAndCustomMoleculeCounters)
0084       BuildMoleculeCounters();
0085     else
0086       BuildMultipleAndCustomMoleculeCounters();
0087   }
0088 }
0089 
0090 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0091 void ActionInitialization::BuildMoleculeCounters() const
0092 {
0093   G4MoleculeCounterManager::Instance()->SetResetCountersBeforeEvent(false);  // defaults to false
0094   G4MoleculeCounterManager::Instance()->SetResetCountersBeforeRun(true);  // defaults to false
0095 
0096   // Register molecule counters
0097   {
0098     // Basic molecule counter
0099     auto counter = std::make_unique<G4MoleculeCounter>("Molecules");
0100     counter->IgnoreMolecule(G4H2O::Definition());
0101     G4MoleculeCounterManager::Instance()->RegisterCounter(std::move(counter));
0102   }
0103   {
0104     // Basic reaction counter
0105     auto counter = std::make_unique<G4MoleculeReactionCounter>("Reactions");
0106     G4MoleculeCounterManager::Instance()->RegisterCounter(std::move(counter));
0107   }
0108 }
0109 
0110 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0111 void ActionInitialization::BuildMultipleAndCustomMoleculeCounters() const
0112 {
0113   G4MoleculeCounterManager::Instance()->SetResetCountersBeforeEvent(false);
0114   G4MoleculeCounterManager::Instance()->SetResetCountersBeforeRun(true);
0115 
0116   // Register molecule counters
0117 
0118   // Basic (built-in) Counters
0119   {
0120     // Basic molecule counter using a fixed time precision.
0121     // this will create many records {molecule -> {time -> conut}}
0122     auto counter = std::make_unique<G4MoleculeCounter>("BasicCounter");
0123     counter->IgnoreMolecule(G4H2O::Definition());
0124     counter->SetTimeComparer(G4MoleculeCounterTimeComparer::CreateWithFixedPrecision(25 * ps));
0125     G4MoleculeCounterManager::Instance()->RegisterCounter(std::move(counter));
0126   }
0127   {
0128     // Basic molecule counter using a fixed time precision [same as above].
0129     // However, we are activating it to only count molecules between 500 ps and 10 ns!
0130     auto counter = std::make_unique<G4MoleculeCounter>("BasicCounter_Restricted");
0131     counter->SetActiveLowerBound(500 * ps);
0132     counter->SetActiveUpperBound(10 * ns);  // add option to truncate after time?
0133     counter->IgnoreMolecule(G4H2O::Definition());
0134     counter->SetTimeComparer(G4MoleculeCounterTimeComparer::CreateWithFixedPrecision(25 * ps));
0135     G4MoleculeCounterManager::Instance()->RegisterCounter(std::move(counter));
0136   }
0137   {
0138     // Basic molecule counter using variable time precision without time restrition.
0139     // The precision is changed with respect to chemistry time.
0140     auto counter = std::make_unique<G4MoleculeCounter>("BasicCounter_VariablePrecision");
0141     counter->SetActiveLowerBound(500 * ps);
0142     counter->SetActiveUpperBound(10 * ns);  // add option to truncate after time?
0143     counter->IgnoreMolecule(G4H2O::Definition());
0144     counter->SetTimeComparer(G4MoleculeCounterTimeComparer::CreateWithVariablePrecision({
0145       {10 * ps, 5 * ps},
0146       {100 * ps, 50 * ps},
0147       {1000 * ps, 500 * ps},
0148       {10 * ns, 5 * ns},
0149       {1 * microsecond, 50 * ns},
0150     }));
0151     G4MoleculeCounterManager::Instance()->RegisterCounter(std::move(counter));
0152   }
0153 
0154   // Custom molecule counter, see: 10.1016/j.radphyschem.2023.111194
0155   {
0156     // Here we create a custom volume-aware molecule counter that uses variable time precision.
0157     // This counter records not just the molecules but also the encompassing volume.
0158     // Note:
0159     // - For this counter you must set `SetSensitiveToStepping(true)`.
0160     // - Other options (like SetNegativeCountsAreFatal) are optional but recommended
0161     // - SetIgnoreMoleculePosition can be set to true to override this counter's volume-awareness
0162     //   this would, in effect, make it the same as a BasicCounter `G4MoleculeCounter`
0163     auto counter = std::make_unique<MoleculeCounter>("MoleculeCounter");
0164     counter->SetVerbose(1);
0165     counter->IgnoreMolecule(G4H2O::Definition());
0166     counter->SetSensitiveToStepping(true);
0167     counter->SetIgnoreMoleculePosition(false);
0168     counter->SetNegativeCountsAreFatal(true);
0169     counter->SetTimeComparer(G4MoleculeCounterTimeComparer::CreateWithVariablePrecision({
0170       {10 * ps, 5 * ps},
0171       {100 * ps, 50 * ps},
0172       {1000 * ps, 500 * ps},
0173       {10 * ns, 5 * ns},
0174       {1 * microsecond, 50 * ns},
0175     }));
0176     G4MoleculeCounterManager::Instance()->RegisterCounter(std::move(counter));
0177   }
0178 
0179   // Molecule Reaction Counter with fixed time precision.
0180   // Set to be active from 0ps to 1us.
0181   {
0182     auto counter = std::make_unique<G4MoleculeReactionCounter>("Reactions");
0183     counter->SetActiveLowerBound(0 * ps);
0184     counter->SetActiveUpperBound(1 * microsecond);
0185     counter->SetTimeComparer(G4MoleculeCounterTimeComparer::CreateWithFixedPrecision(50 * ps));
0186     G4MoleculeCounterManager::Instance()->RegisterCounter(std::move(counter));
0187   }
0188 }
0189 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......