Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
31#include <algorithm>
32
34#include "G4StateManager.hh"
35
36// This macros change the references to fields that are now encapsulated
37// in the class G4VMPLData.
38#define G4MT_physicsVector \
39 ((G4VMPLsubInstanceManager.offset[g4vmplInstanceID]).physicsVector)
40
42
43// --------------------------------------------------------------------
45{
47}
48
49// --------------------------------------------------------------------
52{
54}
55
56// --------------------------------------------------------------------
58{
59 if(G4MT_physicsVector != nullptr)
60 {
61 for(auto & ptr : *G4MT_physicsVector) { delete ptr; }
62 delete G4MT_physicsVector;
63 G4MT_physicsVector = nullptr;
64 }
65}
66
67// --------------------------------------------------------------------
69 : G4VUserPhysicsList(right)
70{
72 G4MT_physicsVector = nullptr;
73}
74
75// --------------------------------------------------------------------
77 const G4VModularPhysicsList& right)
78{
79 if(this != &right)
80 {
89 ._fDisplayThreshold = static_cast<const G4VUserPhysicsList&>(right)
91 .offset[right.GetInstanceID()]
92 ._fDisplayThreshold;
94 ._fDisplayThreshold = static_cast<const G4VUserPhysicsList&>(right)
96 .offset[right.GetInstanceID()]
97 ._fIsPhysicsTableBuilt;
100
101 if(G4MT_physicsVector != nullptr)
102 {
103 for(auto & ptr : *G4MT_physicsVector) { delete ptr; }
104 delete G4MT_physicsVector;
105 G4MT_physicsVector = nullptr;
106 }
108 }
109 return *this;
110}
111
112// --------------------------------------------------------------------
114{
115 // create particles
116 for(auto itr = G4MT_physicsVector->cbegin();
117 itr != G4MT_physicsVector->cend(); ++itr)
118 {
119 (*itr)->ConstructParticle();
120 }
121}
122
123// --------------------------------------------------------------------
124// Andrea Dotti: May 6 2013
125// Current limitation being debugged: Construction of physics processes
126// needs to be sequential (there is at least one HAD processes creating
127// problems). This is not yet understood and needs to be debugged. We do not
128// want this part to be sequential (imagine when one has 100 threads)
129// TODO: Remove this lock
130#include "G4AutoLock.hh"
131namespace
132{
133 G4Mutex constructProcessMutex = G4MUTEX_INITIALIZER;
134}
135
136// --------------------------------------------------------------------
138{
139 G4AutoLock l(&constructProcessMutex); // Protection to be removed (A.Dotti)
141
142 for(auto itr = G4MT_physicsVector->cbegin();
143 itr != G4MT_physicsVector->cend(); ++itr)
144 {
145 (*itr)->ConstructProcess();
146 }
147}
148
149// --------------------------------------------------------------------
151{
153 G4ApplicationState currentState = stateManager->GetCurrentState();
154 if(!(currentState == G4State_PreInit))
155 {
156 G4Exception("G4VModularPhysicsList::RegisterPhysics", "Run0201",
158 "Geant4 kernel is not PreInit state : Method ignored.");
159 return;
160 }
161
162 G4String pName = fPhysics->GetPhysicsName();
163 G4int pType = fPhysics->GetPhysicsType();
164 // If physics_type is equal to 0,
165 // following duplication check is omitted
166 // This is TEMPORAL treatment.
167 if(pType == 0)
168 {
169 G4MT_physicsVector->push_back(fPhysics);
170#ifdef G4VERBOSE
171 if(verboseLevel > 1)
172 {
173 G4cout << "G4VModularPhysicsList::RegisterPhysics: " << pName
174 << " with type : " << pType << " is added" << G4endl;
175 }
176#endif
177 return;
178 }
179
180 // Check if physics with the physics_type same as one of given physics
181 auto itr = G4MT_physicsVector->cbegin();
182 for(; itr != G4MT_physicsVector->cend(); ++itr)
183 {
184 if(pType == (*itr)->GetPhysicsType())
185 break;
186 }
187 if(itr != G4MT_physicsVector->cend())
188 {
189#ifdef G4VERBOSE
190 if(verboseLevel > 0)
191 {
192 G4cout << "G4VModularPhysicsList::RegisterPhysics: "
193 << "a physics with given type already exists " << G4endl;
194 G4cout << " Type = " << pType << " : "
195 << " existing physics is " << (*itr)->GetPhysicsName() << G4endl;
196 G4cout << " New " << pName << " can not be registered " << G4endl;
197 }
198#endif
199 G4String comment = "Duplicate type for ";
200 comment += pName;
201 G4Exception("G4VModularPhysicsList::RegisterPhysics", "Run0202",
202 JustWarning, comment);
203 return;
204 }
205
206 // register
207 G4MT_physicsVector->push_back(fPhysics);
208}
209
210// --------------------------------------------------------------------
212{
214 G4ApplicationState currentState = stateManager->GetCurrentState();
215 if(!(currentState == G4State_PreInit))
216 {
217 G4Exception("G4VModularPhysicsList::ReplacePhysics", "Run0203", JustWarning,
218 "Geant4 kernel is not PreInit state : Method ignored.");
219 return;
220 }
221
222 G4String pName = fPhysics->GetPhysicsName();
223 G4int pType = fPhysics->GetPhysicsType();
224 // If physics_type is equal to 0,
225 // duplication check is omitted and just added.
226 // This is TEMPORAL treatment.
227 if(pType == 0)
228 {
229 // register
230 G4MT_physicsVector->push_back(fPhysics);
231#ifdef G4VERBOSE
232 if(verboseLevel > 0)
233 {
234 G4cout << "G4VModularPhysicsList::ReplacePhysics: " << pName
235 << " with type : " << pType << " is added" << G4endl;
236 }
237#endif
238 return;
239 }
240
241 // Check if physics with the physics_type same as one of given physics
242 auto itr = G4MT_physicsVector->begin();
243 for(; itr != G4MT_physicsVector->end(); ++itr)
244 {
245 if(pType == (*itr)->GetPhysicsType())
246 break;
247 }
248 if(itr == G4MT_physicsVector->end())
249 {
250 // register
251 G4MT_physicsVector->push_back(fPhysics);
252 }
253 else
254 {
255#ifdef G4VERBOSE
256 if(verboseLevel > 0)
257 {
258 G4cout << "G4VModularPhysicsList::ReplacePhysics: "
259 << (*itr)->GetPhysicsName() << " with type : " << pType
260 << " is replaced with " << pName << G4endl;
261 }
262#endif
263
264 // delete exsiting one
265 delete(*itr);
266 // replace with given one
267 (*itr) = fPhysics;
268 }
269 return;
270}
271
272// --------------------------------------------------------------------
274{
276 G4ApplicationState currentState = stateManager->GetCurrentState();
277 if(!(currentState == G4State_PreInit))
278 {
279 G4Exception("G4VModularPhysicsList::RemovePhysics", "Run0204", JustWarning,
280 "Geant4 kernel is not PreInit state : Method ignored.");
281 return;
282 }
283
284 for(auto itr = G4MT_physicsVector->cbegin();
285 itr != G4MT_physicsVector->cend();)
286 {
287 if(pType == (*itr)->GetPhysicsType())
288 {
289 G4String pName = (*itr)->GetPhysicsName();
290#ifdef G4VERBOSE
291 if(verboseLevel > 0)
292 {
293 G4cout << "G4VModularPhysicsList::RemovePhysics: " << pName
294 << " is removed" << G4endl;
295 }
296#endif
297 G4MT_physicsVector->erase(itr);
298 break;
299 }
300 else
301 {
302 ++itr;
303 }
304 }
305}
306
307// --------------------------------------------------------------------
309{
311 G4ApplicationState currentState = stateManager->GetCurrentState();
312 if(!(currentState == G4State_PreInit))
313 {
314 G4Exception("G4VModularPhysicsList::RemovePhysics", "Run0205", JustWarning,
315 "Geant4 kernel is not PreInit state : Method ignored.");
316 return;
317 }
318
319 for(auto itr = G4MT_physicsVector->cbegin();
320 itr != G4MT_physicsVector->cend();)
321 {
322 if(fPhysics == (*itr))
323 {
324 G4String pName = (*itr)->GetPhysicsName();
325#ifdef G4VERBOSE
326 if(verboseLevel > 0)
327 {
328 G4cout << "G4VModularPhysicsList::RemovePhysics: " << pName
329 << " is removed" << G4endl;
330 }
331#endif
332 G4MT_physicsVector->erase(itr);
333 break;
334 }
335 else
336 {
337 ++itr;
338 }
339 }
340}
341
342// --------------------------------------------------------------------
344{
346 G4ApplicationState currentState = stateManager->GetCurrentState();
347 if(!(currentState == G4State_PreInit))
348 {
349 G4Exception("G4VModularPhysicsList::RemovePhysics", "Run0206", JustWarning,
350 "Geant4 kernel is not PreInit state : Method ignored.");
351 return;
352 }
353
354 for(auto itr = G4MT_physicsVector->cbegin();
355 itr != G4MT_physicsVector->cend();)
356 {
357 G4String pName = (*itr)->GetPhysicsName();
358 if(name == pName)
359 {
360#ifdef G4VERBOSE
361 if(verboseLevel > 0)
362 {
363 G4cout << "G4VModularPhysicsList::RemovePhysics: " << pName
364 << " is removed" << G4endl;
365 }
366#endif
367 G4MT_physicsVector->erase(itr);
368 break;
369 }
370 else
371 {
372 ++itr;
373 }
374 }
375}
376
377// --------------------------------------------------------------------
379{
380 auto itr = G4MT_physicsVector->cbegin();
381 for(G4int i = 0; i < idx && itr != G4MT_physicsVector->cend(); ++i)
382 ++itr;
383 if(itr != G4MT_physicsVector->cend())
384 return (*itr);
385 else
386 return nullptr;
387}
388
389// --------------------------------------------------------------------
391 const G4String& name) const
392{
393 auto itr = G4MT_physicsVector->cbegin();
394 for(; itr != G4MT_physicsVector->cend(); ++itr)
395 {
396 if(name == (*itr)->GetPhysicsName())
397 break;
398 }
399 if(itr != G4MT_physicsVector->cend())
400 return (*itr);
401 else
402 return nullptr;
403}
404
405// --------------------------------------------------------------------
407 G4int pType) const
408{
409 auto itr = G4MT_physicsVector->cbegin();
410 for(; itr != G4MT_physicsVector->cend(); ++itr)
411 {
412 if(pType == (*itr)->GetPhysicsType())
413 break;
414 }
415 if(itr != G4MT_physicsVector->cend())
416 return (*itr);
417 else
418 return nullptr;
419}
420
421// --------------------------------------------------------------------
423{
424 verboseLevel = value;
425 // Loop over constructors
426 for(auto itr = G4MT_physicsVector->cbegin();
427 itr != G4MT_physicsVector->cend(); ++itr)
428 {
429 (*itr)->SetVerboseLevel(verboseLevel);
430 }
431}
432
433// --------------------------------------------------------------------
435{
436 // See https://jira-geant4.kek.jp/browse/DEV-284
437 std::for_each(
438 G4MT_physicsVector->cbegin(), G4MT_physicsVector->cend(),
439 [](G4PhysConstVector::value_type el) { el->TerminateWorker(); });
441}
G4ApplicationState
@ G4State_PreInit
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
std::mutex G4Mutex
Definition: G4Threading.hh:81
int G4int
Definition: G4Types.hh:85
#define G4MT_physicsVector
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
std::vector< G4VPhysicsConstructor * > G4PhysConstVectorData
G4PhysConstVectorData * physicsVector
virtual void TerminateWorker() override
virtual void ConstructParticle() override
void SetVerboseLevel(G4int value)
G4VModularPhysicsList & operator=(const G4VModularPhysicsList &)
virtual void ConstructProcess() override
void RegisterPhysics(G4VPhysicsConstructor *)
static G4RUN_DLL G4VMPLManager G4VMPLsubInstanceManager
void RemovePhysics(G4VPhysicsConstructor *)
const G4VPhysicsConstructor * GetPhysicsWithType(G4int physics_type) const
void ReplacePhysics(G4VPhysicsConstructor *)
const G4VPhysicsConstructor * GetPhysics(G4int index) const
const G4String & GetPhysicsName() const
G4RUN_DLL G4ThreadLocalStatic T * offset
G4int CreateSubInstance()
virtual void TerminateWorker()
G4bool fIsCheckedForRetrievePhysicsTable
static G4RUN_DLL G4VUPLManager subInstanceManager
static const G4VUPLManager & GetSubInstanceManager()