Warning, file /include/Geant4/G4MoleculeCounterManager.hh was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #ifndef G4MOLECULECOUNTERMANAGER_HH
0030 #define G4MOLECULECOUNTERMANAGER_HH 1
0031
0032 #include "G4Exception.hh"
0033 #include "G4Threading.hh"
0034 #include "G4Types.hh"
0035 #include "G4VMoleculeCounter.hh"
0036 #include "G4VMoleculeReactionCounter.hh"
0037 #include "G4ios.hh"
0038
0039 #include <functional>
0040 #include <map>
0041 #include <vector>
0042
0043 class G4Event;
0044 class G4Run;
0045 class G4Step;
0046 class G4StepPoint;
0047 class G4Track;
0048 class G4MoleculeCounterManagerMessenger;
0049
0050 class G4MoleculeCounterManager final
0051 {
0052 private:
0053 struct Private
0054 {
0055 explicit Private() = default;
0056 };
0057
0058 public:
0059 G4MoleculeCounterManager(Private);
0060 ~G4MoleculeCounterManager();
0061
0062 private:
0063 G4bool fInstancesRegistered{false};
0064 void RegisterInstance();
0065
0066 public:
0067 static G4MoleculeCounterManager* Instance();
0068 static G4MoleculeCounterManager* GetInstanceIfExists();
0069 static void DeleteInstance();
0070
0071 void Initialize();
0072
0073
0074
0075
0076 G4int RegisterCounter(std::unique_ptr<G4VMoleculeCounter>);
0077 G4int RegisterCounter(std::unique_ptr<G4VMoleculeReactionCounter>);
0078 void DeregisterAllCounters();
0079
0080 private:
0081 template<typename T>
0082 G4int RegisterCounter(std::map<G4int, T*>&, std::unique_ptr<T>, std::function<G4int()>);
0083 void InitializeMaster();
0084 void InitializeWorker();
0085
0086 public:
0087
0088 void BeginOfEventAction(const G4Event*);
0089 void BeginOfRunAction(const G4Run*);
0090 void EndOfEventAction(const G4Event*);
0091 void EndOfRunAction(const G4Run*);
0092
0093 void DumpMasterCounters() const;
0094 void DumpWorkerCounters() const;
0095
0096 void AbsorbWorkerManagerCounters(const G4MoleculeCounterManager* = nullptr);
0097
0098
0099
0100 public:
0101
0102 void AddMoleculeWithoutTrack(const G4MolecularConfiguration*, G4double, G4int = 1);
0103
0104 void RemoveMoleculeWithoutTrack(const G4MolecularConfiguration*, G4double, G4int = 1);
0105
0106 void AddMolecule(const G4Track*, G4double, G4int = 1);
0107 void RemoveMolecule(const G4Track*, G4double, G4int = 1);
0108
0109 void AddMolecule(const G4Track*, const G4StepPoint*, G4double, G4int = 1);
0110 void RemoveMolecule(const G4Track*, const G4StepPoint*, G4double, G4int = 1);
0111
0112 void ActivateCounterAtTimes(G4int, G4double, G4double, G4bool = true, G4bool = true);
0113
0114
0115
0116
0117
0118 void RecordReaction(const G4DNAMolecularReactionData*, G4double, G4int = 1);
0119
0120
0121
0122 void ActivateReactionCounterAtTimes(G4int, G4double, G4double, G4bool = true, G4bool = true);
0123
0124
0125
0126
0127 void ResetCounters();
0128
0129 void NotifyOfStep(const G4Step*);
0130 void NotifyOfFinalize();
0131
0132
0133 void BroadcastIgnoreMolecule(const G4MoleculeDefinition*);
0134 void BroadcastIgnoreReactant(const G4MolecularConfiguration*);
0135 void BroadcastRegisterAllMoleculesAndReactants();
0136
0137 private:
0138 static const G4MoleculeCounterManager* fpMasterInstance;
0139 static std::vector<const G4MoleculeCounterManager*> fWorkerInstances;
0140 G4ThreadLocalStatic std::unique_ptr<G4MoleculeCounterManager> fpInstance;
0141
0142 std::unique_ptr<G4MoleculeCounterManagerMessenger> fpMessenger;
0143
0144 G4int fVerbosity;
0145 G4bool fIsInitialized;
0146 G4bool fIsActive;
0147 static std::atomic<G4bool> fResetCountersBeforeEvent;
0148 static std::atomic<G4bool> fResetCountersBeforeRun;
0149 static std::atomic<G4bool> fResetMasterCounterWithWorkers;
0150 static std::atomic<G4bool> fAccumulateCounterIntoMaster;
0151
0152 std::map<G4int, G4VMoleculeCounter*> fCounters{};
0153 std::map<G4int, G4VMoleculeReactionCounter*> fReactionCounters{};
0154
0155 public:
0156 G4bool GetIsActive() const { return fIsActive; }
0157 void SetIsActive(G4bool flag) { fIsActive = flag; }
0158
0159 G4int GetVerbosity() const { return fVerbosity; }
0160 void SetVerbosity(G4int v) { fVerbosity = v; }
0161
0162 G4bool GetResetCountersBeforeEvent() const;
0163 void SetResetCountersBeforeEvent(G4bool = true);
0164
0165 G4bool GetResetCountersBeforeRun() const;
0166 void SetResetCountersBeforeRun(G4bool = true);
0167
0168 G4bool GetResetMasterCounterWithWorkers() const;
0169 void SetResetMasterCounterWithWorkers(G4bool = true);
0170
0171 G4bool GetAccumulateCounterIntoMaster() const;
0172 void SetAccumulateCounterIntoMaster(G4bool = true);
0173
0174 std::vector<const G4VMoleculeCounter*> GetMoleculeCounters() const;
0175 std::vector<const G4VMoleculeCounter*> GetMoleculeCounters(G4String) const;
0176 template<typename T = G4VMoleculeCounter>
0177 const T* GetMoleculeCounter(G4int) const;
0178 G4VMoleculeCounter* GetEditableMoleculeCounter(G4int) const;
0179
0180 std::vector<const G4VMoleculeReactionCounter*> GetMoleculeReactionCounters() const;
0181 std::vector<const G4VMoleculeReactionCounter*> GetMoleculeReactionCounters(G4String) const;
0182 G4VMoleculeReactionCounter* GetEditableMoleculeReactionCounter(G4int) const;
0183 template<typename T = G4VMoleculeReactionCounter>
0184 const T* GetMoleculeReactionCounter(G4int) const;
0185
0186 private:
0187 G4ThreadLocalStatic std::atomic<G4bool> fBeginOfEventTriggered;
0188 static std::atomic<G4bool> fBeginOfRunTriggered;
0189 };
0190
0191
0192
0193 template<typename T>
0194 G4int G4MoleculeCounterManager::RegisterCounter(std::map<G4int, T*>& map,
0195 std::unique_ptr<T> counter,
0196 std::function<G4int()> idProvider)
0197 {
0198
0199
0200
0201 if (fVerbosity > 0) {
0202 G4cout << "G4MoleculeCounterManager::RegisterCounter ("
0203 << (G4Threading::IsMasterThread() ? "master" : "worker") << ")" << G4endl;
0204 }
0205
0206 if (counter->GetManagedId() >= 0) {
0207 G4ExceptionDescription description;
0208 description << "Trying to add a counter whose id was already altered to be non-negative!\n";
0209 description << " Id: " << counter->GetManagedId() << "\n";
0210 description << "Name: " << counter->GetName();
0211 G4Exception("G4MoleculeCounterManager::RegisterCounter", "MOLMAN002", FatalErrorInArgument,
0212 description);
0213 }
0214
0215 T* sp = counter.release();
0216
0217 sp->SetManagedId(idProvider());
0218
0219 map.emplace(sp->GetManagedId(), sp);
0220
0221 return sp->GetManagedId();
0222 }
0223
0224
0225
0226 template<typename T>
0227 const T* G4MoleculeCounterManager::GetMoleculeCounter(G4int id) const
0228 {
0229 static_assert(std::is_base_of<G4VMoleculeCounter, T>::value,
0230 "T must be derived from G4VMoleculeCounter!");
0231 auto base_ptr = GetEditableMoleculeCounter(id);
0232 if (base_ptr == nullptr) return nullptr;
0233 const T* ptr = dynamic_cast<const T*>(base_ptr);
0234 return ptr;
0235 }
0236
0237 template<typename T>
0238 const T* G4MoleculeCounterManager::GetMoleculeReactionCounter(G4int id) const
0239 {
0240 static_assert(std::is_base_of<G4VMoleculeReactionCounter, T>::value,
0241 "T must be derived from G4VMoleculeReactionCounter!");
0242 auto base_ptr = GetEditableMoleculeReactionCounter(id);
0243 if (base_ptr == nullptr) return nullptr;
0244 const T* ptr = dynamic_cast<const T*>(base_ptr);
0245 return ptr;
0246 }
0247
0248
0249
0250 #endif