Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4INCLStore.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 // INCL++ intra-nuclear cascade model
27 // Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
28 // Davide Mancusi, CEA
29 // Alain Boudard, CEA
30 // Sylvie Leray, CEA
31 // Joseph Cugnon, University of Liege
32 //
33 #define INCLXX_IN_GEANT4_MODE 1
34 
35 #include "globals.hh"
36 
37 #include "G4INCLStore.hh"
38 #include <fstream>
39 #include "G4INCLLogger.hh"
40 #include "G4INCLCluster.hh"
41 
42 namespace G4INCL {
43 
44  Store::Store(Config const * const config) :
45  loadedA(0),
46  loadedZ(0),
47  loadedStoppingTime(0.),
48  theConfig(config)
49  {
50  }
51 
53  theBook.reset();
54  clear();
55  }
56 
58  inside.push_back(p);
59 
60  if(particleAvatarConnections.find(p)==particleAvatarConnections.end()) {
61  IAvatarList *avatars = new IAvatarList;
62  particleAvatarConnections[p] = avatars;
63  }
64  }
65 
67  // Add the avatar to the avatar map
68  avatarList.push_back(a);
69 
70  ParticleList pList = a->getParticles();
71  for(ParticleIter i=pList.begin(), e=pList.end(); i!=e; ++i) {
72  addIncomingParticle((*i));
73  // Connect each particle to the avatar
74  connectAvatarToParticle(a, *i);
75  }
76  }
77 
79  for(IAvatarIter a=al.begin(), e=al.end(); a!=e; ++a)
81  }
82 
83  void Store::add(IAvatar *a) {
84  // Add the avatar to the avatar map
85  avatarList.push_back(a);
86 
87  ParticleList pList = a->getParticles();
88  for(ParticleIter i=pList.begin(), e=pList.end(); i!=e; ++i) {
89  // If one of the particles participating in this avatar hasn't been
90  // registered with the store, it's probably a symptom of a bug
91  // somewhere...
92 // assert(particleAvatarConnections.find(*i) != particleAvatarConnections.end());
93 
94  // Connect each particle to the avatar
95  connectAvatarToParticle(a, *i);
96  }
97 
98  }
99 
101  incoming.push_back(p);
102  }
103 
104  void Store::connectAvatarToParticle(IAvatar * const a, Particle * const p) {
105  std::map<Particle*, IAvatarList*>::const_iterator iter = particleAvatarConnections.find(p);
106  // If the particle is already connected to other avatars
107  if(iter!=particleAvatarConnections.end()) { // Add to the existing map entry
108  iter->second->push_back(a);
109  } else { // Create a new map entry
110  IAvatarList *avatars = new IAvatarList;
111  avatars->push_back(a);
112  particleAvatarConnections[p] = avatars;
113  }
114  }
115 
116  void Store::disconnectAvatarFromParticle(IAvatar * const a, Particle * const p) {
117  particleAvatarConnections.find(p)->second->remove(a);
118  }
119 
120  void Store::removeAvatar(IAvatar * const avatar) {
121  // Disconnect the avatar from particles
122  ParticleList particlesRelatedToAvatar = avatar->getParticles();
123  for(ParticleIter particleIter = particlesRelatedToAvatar.begin(), e = particlesRelatedToAvatar.end();
124  particleIter != e; ++particleIter) {
125  disconnectAvatarFromParticle(avatar, *particleIter);
126  }
127 
128 #ifdef INCL_AVATAR_SEARCH_INCLSort
129  // Remove the avatar iterator from the avatarIterList, if it is present.
130  std::list<IAvatarIter>::iterator it=binaryIterSearch(avatar);
131  if(it != avatarIterList.end())
132  avatarIterList.erase(it);
133 #endif
134 
135  // Remove the avatar itself
136  avatarList.remove(avatar);
137  }
138 
139  void Store::removeAndDeleteAvatar(IAvatar * const avatar) {
140  removeAvatar(avatar);
141  delete avatar;
142  }
143 
144  void Store::particleHasBeenUpdated(Particle * const particle) {
145  // must make a copy of this list, because calls to removeAvatar will modify
146  // the list itself
147  IAvatarList avatars = *(particleAvatarConnections.find(particle)->second);
148  std::for_each(avatars.begin(), avatars.end(), std::bind1st(std::mem_fun(&G4INCL::Store::removeAndDeleteAvatar), this));
149  }
150 
151 #ifdef INCL_AVATAR_SEARCH_INCLSort
152  std::list<IAvatarIter>::iterator Store::binaryIterSearch(IAvatar const * const avatar) {
153  std::list<IAvatarIter>::iterator it;
154  std::iterator_traits<std::list<IAvatarIter>::iterator>::difference_type count, step;
155  std::list<IAvatarIter>::iterator first = avatarIterList.begin();
156  std::list<IAvatarIter>::iterator last = avatarIterList.end();
157  const G4double avatarTime = avatar->getTime();
158  count = distance(first,last);
159  while (count>0)
160  {
161  it = first; step=count/2; advance(it,step);
162  if ((**it)->getTime()>avatarTime)
163  { first=++it; count-=step+1; }
164  else count=step;
165  }
166  if(first!=last && (**first)->getID()==avatar->getID())
167  return first;
168  else
169  return last;
170  }
171 #endif
172 
174  if(avatarList.empty()) return NULL;
175 
176 #ifdef INCL_AVATAR_SEARCH_FullSort
177 
178  /* Full sort algorithm.
179  *
180  * Simple, but guaranteed to work.
181  */
182  avatarList.sort(Store::avatarComparisonPredicate);
183  IAvatar *avatar = avatarList.front();
184 
185 #elif defined(INCL_AVATAR_SEARCH_INCLSort)
186 
187  /* Partial sort algorithm used by INCL4.6.
188  *
189  * It nevers sorts the whole avatar list, but rather starts from the last
190  * best avatar. It requires the avatarList to be updated by appending new
191  * avatars at the end.
192  */
193 
194  IAvatarIter best;
195  if(avatarIterList.empty())
196  best = avatarList.begin();
197  else
198  best = avatarIterList.back();
199  G4double bestTime = (*best)->getTime();
200  IAvatarIter a = best;
201 
202  for(++a; a!=avatarList.end(); ++a)
203  if((*a)->getTime() < bestTime) {
204  best = a;
205  bestTime = (*best)->getTime();
206  avatarIterList.push_back(best);
207  }
208  IAvatar *avatar = *best;
209 
210 #elif defined(INCL_AVATAR_SEARCH_MinElement)
211 
212  /* Algorithm provided by the C++ stdlib. */
213  IAvatar *avatar = *(std::min_element(avatarList.begin(), avatarList.end(),
215 
216 #else
217 #error Unrecognized INCL_AVATAR_SEARCH. Allowed values are: FullSort, INCLSort, MinElement.
218 #endif
219 
220  removeAvatar(avatar);
221  return avatar;
222  }
223 
225  for(ParticleIter particleIter = inside.begin(), particleEnd=inside.end();
226  particleIter != particleEnd; ++particleIter) {
227  (*particleIter)->propagate(step);
228  }
229  }
230 
233  // The particle will be destroyed when destroying the Store
234  inside.remove(p);
235  std::map<Particle*, IAvatarList*>::iterator mapItem = particleAvatarConnections.find(p);
236  delete mapItem->second;
237  particleAvatarConnections.erase(mapItem);
238  }
239 
242  // Have to destroy the particle here, the Store will forget about it
243  inside.remove(p);
244  delete p;
245  }
246 
247  void Store::particleHasEntered(Particle * const particle) {
248  removeFromIncoming(particle);
249  add(particle);
250  }
251 
253  for(IAvatarIter iter = avatarList.begin(), e = avatarList.end(); iter != e; ++iter) {
254  delete *iter;
255  }
256 
257  for(std::map<Particle*, IAvatarList*>::iterator iter = particleAvatarConnections.begin(),
258  e = particleAvatarConnections.end(); iter != e; ++iter) {
259  delete iter->second;
260  }
261 
262  particleAvatarConnections.clear();
263  avatarList.clear();
264 
265  }
266 
268  for(ParticleIter ip=inside.begin(), e=inside.end(); ip!=e; ++ip) {
269  particleAvatarConnections[*ip] = new IAvatarList;
270  }
271  }
272 
273  void Store::clear() {
274  clearAvatars();
275 
276  clearInside();
277  clearOutgoing();
278 
279  if( incoming.size() != 0 ) {
280  INCL_WARN("Incoming list is not empty when Store::clear() is called" << std::endl);
281  }
282  incoming.clear();
283 
284 #ifdef INCL_AVATAR_SEARCH_INCLSort
285  avatarIterList.clear();
286 #endif
287 
288  }
289 
291  for(ParticleIter iter=inside.begin(), e=inside.end(); iter!=e; ++iter) {
292  delete *iter;
293  }
294  inside.clear();
295  }
296 
298  for(ParticleIter iter=outgoing.begin(), e=outgoing.end(); iter!=e; ++iter) {
299  if((*iter)->isCluster()) {
300  Cluster *c = dynamic_cast<Cluster *>(*iter);
301 // assert(c);
302 #ifdef INCLXX_IN_GEANT4_MODE
303  if(!c)
304  continue;
305 #endif
306  c->deleteParticles();
307  }
308  delete (*iter);
309  }
310  outgoing.clear();
311  }
312 
313  void Store::loadParticles(std::string filename) {
314  clear();
315  G4int projectileA, projectileZ, A, Z;
316  G4double stoppingTime, cutNN;
317  G4int ID, type, isParticipant;
318  G4double x, y, z;
319  G4double px, py, pz, E, v;
320 
321  std::ifstream in(filename.c_str());
322  in >> projectileA >> projectileZ >> A >> Z >> stoppingTime >> cutNN;
323  loadedA = A;
324  loadedZ = Z;
325  loadedStoppingTime = stoppingTime;
326 
327  G4int readA = 0;
328  G4int readZ = 0;
329  while(1) {
330  in >> ID >> type >> isParticipant >> x >> y >> z >> px >> py >> pz >> E >> v;
331  if(!in.good()) break;
332  ParticleType t;
333  if(type == 1) {
334  t = Proton;
335  readZ++;
336  readA++;
337  }
338  else if(type == -1) {
339  t = Neutron;
340  readA++;
341  }
342  else {
343  INCL_FATAL("Unrecognized particle type while loading particles; type=" << type << std::endl);
344  t = UnknownParticle;
345  }
346 
347  Particle *p = new Particle(t, E, ThreeVector(px, py, pz),
348  ThreeVector(x, y, z));
349  p->setPotentialEnergy(v);
350  if(isParticipant == 1) {
351  p->makeParticipant();
352  theBook.incrementCascading();
353  }
354  add(p);
355  }
356 
357  in.close();
358  }
359 
361  std::stringstream ss;
362  G4int A = 0, Z = 0;
363  for(ParticleIter i=inside.begin(), e=inside.end(); i!=e; ++i) {
364  if((*i)->getType() == Proton) {
365  A++;
366  Z++;
367  }
368  if((*i)->getType() == Neutron) {
369  A++;
370  }
371  }
372  // Note: Projectile A and Z are set to 0 (we don't really know
373  // anything about them at this point).
374  ss << "0 0 " << A << " " << Z << " "
375  << "100.0" << " "
376  << "0.0" << std::endl;
377 
378  for(ParticleIter i=inside.begin(), e=inside.end(); i!=e; ++i) {
379  G4int ID = (*i)->getID();
380  G4int type = 0;
381  if((*i)->getType() == Proton) {
382  type = 1;
383  }
384  if((*i)->getType() == Neutron) {
385  type = -1;
386  }
387 
388  G4int isParticipant = 0;
389  if((*i)->isParticipant()) {
390  isParticipant = 1;
391  }
392 
393  G4double x = (*i)->getPosition().getX();
394  G4double y = (*i)->getPosition().getY();
395  G4double z = (*i)->getPosition().getZ();
396  G4double E = (*i)->getEnergy();
397  G4double px = (*i)->getMomentum().getX();
398  G4double py = (*i)->getMomentum().getY();
399  G4double pz = (*i)->getMomentum().getZ();
400  G4double V = (*i)->getPotentialEnergy();
401 
402  ss << ID << " " << type << " " << isParticipant << " "
403  << x << " " << y << " " << z << " "
404  << px << " " << py << " " << pz << " "
405  << E << " " << V << std::endl;
406  }
407 
408  return ss.str();
409  }
410 
411  void Store::writeParticles(std::string filename) {
412  std::ofstream out(filename.c_str());
414  out.close();
415  }
416 
417  std::string Store::printAvatars() {
418  std::stringstream ss;
419  for(IAvatarIter i = avatarList.begin(), e = avatarList.end(); i != e; ++i) {
420  ss << (*i)->toString() << std::endl;
421  }
422  return ss.str();
423  }
424 
426  for(IAvatarIter i = avatarList.begin(), e = avatarList.end(); i != e; ++i)
427  if((*i)->getType()==CollisionAvatarType) return true;
428  return false;
429  }
430 
431 }
std::string printParticleConfiguration()
Definition: G4INCLStore.cc:360
#define INCL_FATAL(x)
void writeParticles(std::string filename)
Definition: G4INCLStore.cc:411
void clearAvatars()
Definition: G4INCLStore.cc:252
static G4bool avatarComparisonPredicate(IAvatar *lhs, IAvatar *rhs)
Comparison predicate for avatars.
Definition: G4INCLStore.hh:337
std::string printAvatars()
Definition: G4INCLStore.cc:417
G4double z
Definition: TRTMaterials.hh:39
const char * p
Definition: xmltok.h:285
G4bool containsCollisions() const
Definition: G4INCLStore.cc:425
UnorderedVector< IAvatar * >::const_iterator IAvatarIter
IAvatar * findSmallestTime()
Definition: G4INCLStore.cc:173
void add(Particle *p)
Definition: G4INCLStore.cc:57
#define INCL_WARN(x)
void remove(const T &t)
Store(Config const *const config)
Definition: G4INCLStore.cc:44
int G4int
Definition: G4Types.hh:78
void addParticleEntryAvatars(IAvatarList const &al)
Add one ParticleEntry avatar.
Definition: G4INCLStore.cc:78
G4double getTime() const
UnorderedVector< Particle * > ParticleList
void particleHasBeenDestroyed(Particle *const)
Definition: G4INCLStore.cc:240
void initialiseParticleAvatarConnections()
Initialise the particleAvatarConnections map.
Definition: G4INCLStore.cc:267
bool G4bool
Definition: G4Types.hh:79
void particleHasBeenEjected(Particle *const)
Definition: G4INCLStore.cc:231
long getID() const
virtual ParticleList getParticles() const =0
void setPotentialEnergy(G4double v)
Set the particle potential energy.
UnorderedVector< IAvatar * > IAvatarList
void clearInside()
Definition: G4INCLStore.cc:290
void clearOutgoing()
Definition: G4INCLStore.cc:297
void incrementCascading()
Definition: G4INCLBook.hh:76
void addParticleEntryAvatar(IAvatar *a)
Add one ParticleEntry avatar.
Definition: G4INCLStore.cc:66
void particleHasBeenUpdated(Particle *const)
Definition: G4INCLStore.cc:144
void reset()
Definition: G4INCLBook.hh:51
void loadParticles(std::string filename)
Definition: G4INCLStore.cc:313
void addIncomingParticle(Particle *const p)
Definition: G4INCLStore.cc:100
virtual void makeParticipant()
double G4double
Definition: G4Types.hh:76
void timeStep(G4double step)
Definition: G4INCLStore.cc:224
ParticleList::const_iterator ParticleIter
void removeFromIncoming(Particle *const p)
Definition: G4INCLStore.hh:124
void particleHasEntered(Particle *const particle)
Move a particle from incoming to inside.
Definition: G4INCLStore.cc:247