Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4VModularPhysicsList.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// G4VModularPhysicsList implementation
27//
28// Original author: H.Kurashige (Kobe University), 12 November 2000
29// --------------------------------------------------------------------
30
32
33#include "G4StateManager.hh"
34
35#include <algorithm>
36
37// This macros change the references to fields that are now encapsulated
38// in the class G4VMPLData.
39#define G4MT_physicsVector ((G4VMPLsubInstanceManager.offset[g4vmplInstanceID]).physicsVector)
40
42
43// --------------------------------------------------------------------
48
49// --------------------------------------------------------------------
54
55// --------------------------------------------------------------------
57{
58 if (G4MT_physicsVector != nullptr) {
59 for (auto& ptr : *G4MT_physicsVector) {
60 delete ptr;
61 }
62 delete G4MT_physicsVector;
63 G4MT_physicsVector = nullptr;
64 }
65}
66
67// --------------------------------------------------------------------
74
75// --------------------------------------------------------------------
77{
78 if (this != &right) {
86 (this->subInstanceManager.offset[this->g4vuplInstanceID])._fDisplayThreshold =
87 static_cast<const G4VUserPhysicsList&>(right)
88 .GetSubInstanceManager()
89 .offset[right.GetInstanceID()]
90 ._fDisplayThreshold;
91 (this->subInstanceManager.offset[this->g4vuplInstanceID])._fDisplayThreshold =
92 static_cast<G4int>(static_cast<const G4VUserPhysicsList&>(right)
94 .offset[right.GetInstanceID()]
95 ._fIsPhysicsTableBuilt);
98
99 if (G4MT_physicsVector != nullptr) {
100 for (auto& ptr : *G4MT_physicsVector) {
101 delete ptr;
102 }
103 delete G4MT_physicsVector;
104 G4MT_physicsVector = nullptr;
105 }
106 g4vmplInstanceID = G4VMPLsubInstanceManager.CreateSubInstance();
107 }
108 return *this;
109}
110
111// --------------------------------------------------------------------
113{
114 // create particles
115 for (auto itr = G4MT_physicsVector->cbegin(); itr != G4MT_physicsVector->cend(); ++itr) {
116 (*itr)->ConstructParticle();
117 }
118}
119
120// --------------------------------------------------------------------
121// Andrea Dotti: May 6 2013
122// Current limitation being debugged: Construction of physics processes
123// needs to be sequential (there is at least one HAD processes creating
124// problems). This is not yet understood and needs to be debugged. We do not
125// want this part to be sequential (imagine when one has 100 threads)
126// TODO: Remove this lock
127#include "G4AutoLock.hh"
128namespace
129{
130G4Mutex constructProcessMutex = G4MUTEX_INITIALIZER;
131}
132
133// --------------------------------------------------------------------
135{
136 G4AutoLock l(&constructProcessMutex); // Protection to be removed (A.Dotti)
138
139 for (auto itr = G4MT_physicsVector->cbegin(); itr != G4MT_physicsVector->cend(); ++itr) {
140 (*itr)->ConstructProcess();
141 }
142}
143
144// --------------------------------------------------------------------
146{
148 G4ApplicationState currentState = stateManager->GetCurrentState();
149 if (!(currentState == G4State_PreInit)) {
150 G4Exception("G4VModularPhysicsList::RegisterPhysics", "Run0201", JustWarning,
151 "Geant4 kernel is not PreInit state : Method ignored.");
152 return;
153 }
154
155 G4String pName = fPhysics->GetPhysicsName();
156 G4int pType = fPhysics->GetPhysicsType();
157 // If physics_type is equal to 0,
158 // following duplication check is omitted
159 // This is TEMPORAL treatment.
160 if (pType == 0) {
161 G4MT_physicsVector->push_back(fPhysics);
162#ifdef G4VERBOSE
163 if (verboseLevel > 1) {
164 G4cout << "G4VModularPhysicsList::RegisterPhysics: " << pName << " with type : " << pType
165 << " is added" << G4endl;
166 }
167#endif
168 return;
169 }
170
171 // Check if physics with the physics_type same as one of given physics
172 auto itr = G4MT_physicsVector->cbegin();
173 for (; itr != G4MT_physicsVector->cend(); ++itr) {
174 if (pType == (*itr)->GetPhysicsType()) break;
175 }
176 if (itr != G4MT_physicsVector->cend()) {
177#ifdef G4VERBOSE
178 if (verboseLevel > 0) {
179 G4cout << "G4VModularPhysicsList::RegisterPhysics: "
180 << "a physics with given type already exists " << G4endl;
181 G4cout << " Type = " << pType << " : "
182 << " existing physics is " << (*itr)->GetPhysicsName() << G4endl;
183 G4cout << " New " << pName << " can not be registered " << G4endl;
184 }
185#endif
186 G4String comment = "Duplicate type for ";
187 comment += pName;
188 G4Exception("G4VModularPhysicsList::RegisterPhysics", "Run0202", JustWarning, comment);
189 return;
190 }
191
192 // register
193 G4MT_physicsVector->push_back(fPhysics);
194}
195
196// --------------------------------------------------------------------
198{
200 G4ApplicationState currentState = stateManager->GetCurrentState();
201 if (!(currentState == G4State_PreInit)) {
202 G4Exception("G4VModularPhysicsList::ReplacePhysics", "Run0203", JustWarning,
203 "Geant4 kernel is not PreInit state : Method ignored.");
204 return;
205 }
206
207 G4String pName = fPhysics->GetPhysicsName();
208 G4int pType = fPhysics->GetPhysicsType();
209 // If physics_type is equal to 0,
210 // duplication check is omitted and just added.
211 // This is TEMPORAL treatment.
212 if (pType == 0) {
213 // register
214 G4MT_physicsVector->push_back(fPhysics);
215#ifdef G4VERBOSE
216 if (verboseLevel > 0) {
217 G4cout << "G4VModularPhysicsList::ReplacePhysics: " << pName << " with type : " << pType
218 << " is added" << G4endl;
219 }
220#endif
221 return;
222 }
223
224 // Check if physics with the physics_type same as one of given physics
225 G4String pName0;
226 auto itr = G4MT_physicsVector->begin();
227 for (; itr != G4MT_physicsVector->end(); ++itr) {
228 if (pType == (*itr)->GetPhysicsType()) {
229 pName0 = (*itr)->GetPhysicsName();
230 break;
231 }
232 }
233 if (itr == G4MT_physicsVector->end()) {
234 // register new at the end of the vector
235 G4MT_physicsVector->push_back(fPhysics);
236 }
237 else {
238#ifdef G4VERBOSE
239 // it is not needed to print on replacement if the same physics configuration
240 // replaces the previous one - the previous will be silently deleted
241 if (verboseLevel > 0 && pName != pName0) {
242 G4cout << "G4VModularPhysicsList::ReplacePhysics: " << (*itr)->GetPhysicsName()
243 << " with type : " << pType << " is replaced with " << pName << G4endl;
244 }
245#endif
246
247 // delete exsiting one
248 delete (*itr);
249 // replace with given one
250 (*itr) = fPhysics;
251 }
252 return;
253}
254
255// --------------------------------------------------------------------
257{
259 G4ApplicationState currentState = stateManager->GetCurrentState();
260 if (!(currentState == G4State_PreInit)) {
261 G4Exception("G4VModularPhysicsList::RemovePhysics", "Run0204", JustWarning,
262 "Geant4 kernel is not PreInit state : Method ignored.");
263 return;
264 }
265
266 for (auto itr = G4MT_physicsVector->cbegin(); itr != G4MT_physicsVector->cend();) {
267 if (pType == (*itr)->GetPhysicsType()) {
268 G4String pName = (*itr)->GetPhysicsName();
269#ifdef G4VERBOSE
270 if (verboseLevel > 0) {
271 G4cout << "G4VModularPhysicsList::RemovePhysics: " << pName << " is removed" << G4endl;
272 }
273#endif
274 G4MT_physicsVector->erase(itr);
275 break;
276 }
277
278 ++itr;
279 }
280}
281
282// --------------------------------------------------------------------
284{
286 G4ApplicationState currentState = stateManager->GetCurrentState();
287 if (!(currentState == G4State_PreInit)) {
288 G4Exception("G4VModularPhysicsList::RemovePhysics", "Run0205", JustWarning,
289 "Geant4 kernel is not PreInit state : Method ignored.");
290 return;
291 }
292
293 for (auto itr = G4MT_physicsVector->cbegin(); itr != G4MT_physicsVector->cend();) {
294 if (fPhysics == (*itr)) {
295 G4String pName = (*itr)->GetPhysicsName();
296#ifdef G4VERBOSE
297 if (verboseLevel > 0) {
298 G4cout << "G4VModularPhysicsList::RemovePhysics: " << pName << " is removed" << G4endl;
299 }
300#endif
301 G4MT_physicsVector->erase(itr);
302 break;
303 }
304
305 ++itr;
306 }
307}
308
309// --------------------------------------------------------------------
311{
313 G4ApplicationState currentState = stateManager->GetCurrentState();
314 if (!(currentState == G4State_PreInit)) {
315 G4Exception("G4VModularPhysicsList::RemovePhysics", "Run0206", JustWarning,
316 "Geant4 kernel is not PreInit state : Method ignored.");
317 return;
318 }
319
320 for (auto itr = G4MT_physicsVector->cbegin(); itr != G4MT_physicsVector->cend();) {
321 G4String pName = (*itr)->GetPhysicsName();
322 if (name == pName) {
323#ifdef G4VERBOSE
324 if (verboseLevel > 0) {
325 G4cout << "G4VModularPhysicsList::RemovePhysics: " << pName << " is removed" << G4endl;
326 }
327#endif
328 G4MT_physicsVector->erase(itr);
329 break;
330 }
331
332 ++itr;
333 }
334}
335
336// --------------------------------------------------------------------
338{
339 auto itr = G4MT_physicsVector->cbegin();
340 for (G4int i = 0; i < idx && itr != G4MT_physicsVector->cend(); ++i)
341 ++itr;
342 if (itr != G4MT_physicsVector->cend()) return (*itr);
343 return nullptr;
344}
345
346// --------------------------------------------------------------------
348{
349 auto itr = G4MT_physicsVector->cbegin();
350 for (; itr != G4MT_physicsVector->cend(); ++itr) {
351 if (name == (*itr)->GetPhysicsName()) break;
352 }
353 if (itr != G4MT_physicsVector->cend()) return (*itr);
354 return nullptr;
355}
356
357// --------------------------------------------------------------------
359{
360 auto itr = G4MT_physicsVector->cbegin();
361 for (; itr != G4MT_physicsVector->cend(); ++itr) {
362 if (pType == (*itr)->GetPhysicsType()) break;
363 }
364 if (itr != G4MT_physicsVector->cend()) return (*itr);
365 return nullptr;
366}
367
368// --------------------------------------------------------------------
370{
371 verboseLevel = value;
372 // Loop over constructors
373 for (auto itr = G4MT_physicsVector->cbegin(); itr != G4MT_physicsVector->cend(); ++itr) {
374 (*itr)->SetVerboseLevel(verboseLevel);
375 }
376}
377
378// --------------------------------------------------------------------
380{
381 // See https://jira-geant4.kek.jp/browse/DEV-284
382 std::for_each(G4MT_physicsVector->cbegin(), G4MT_physicsVector->cend(),
383 [](G4PhysConstVector::value_type el) { el->TerminateWorker(); });
385}
G4ApplicationState
@ G4State_PreInit
G4TemplateAutoLock< G4Mutex > G4AutoLock
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
G4ThreadLocal T * G4GeomSplitter< T >::offset
#define G4MUTEX_INITIALIZER
std::mutex G4Mutex
int G4int
Definition G4Types.hh:85
#define G4MT_physicsVector
G4VUPLSplitter< G4VMPLData > G4VMPLManager
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
std::vector< G4VPhysicsConstructor * > G4PhysConstVectorData
G4PhysConstVectorData * physicsVector
void SetVerboseLevel(G4int value)
G4VModularPhysicsList & operator=(const G4VModularPhysicsList &)
void RegisterPhysics(G4VPhysicsConstructor *)
static G4RUN_DLL G4VMPLManager G4VMPLsubInstanceManager
void RemovePhysics(G4VPhysicsConstructor *)
const G4VPhysicsConstructor * GetPhysicsWithType(G4int physics_type) const
void ReplacePhysics(G4VPhysicsConstructor *)
static const G4VMPLManager & GetSubInstanceManager()
const G4VPhysicsConstructor * GetPhysics(G4int index) const
const G4String & GetPhysicsName() const
virtual void TerminateWorker()
static G4RUN_DLL G4VUPLManager subInstanceManager