Geant4  10.01.p03
G4MoleculeCounter.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 // $Id: G4MoleculeCounter.cc 84858 2014-10-21 16:08:22Z gcosmo $
27 //
28 #include "G4MoleculeCounter.hh"
29 #include "G4MoleculeTable.hh"
30 #include "G4UIcommand.hh"
31 #include "G4UnitsTable.hh"
32 #include <iomanip>
33 
34 using namespace std;
35 
38 
40 {
41  fVerbose = 0;
42 }
43 
45 
47 {
48  if (!fpInstance) fpInstance = new G4MoleculeCounter();
49 
50  return fpInstance;
51 }
52 
54 {
55  if (!fpInstance) fpInstance = new G4MoleculeCounter();
56 
57  return fpInstance;
58 }
59 
61 {
62  if (fpInstance)
63  {
64  delete fpInstance;
65  fpInstance = 0;
66  }
67 }
68 
70 {
71  if(fpInstance) fpInstance->Initialize();
72 }
73 
75 {
77  ->GetModelIterator();
78  while ((mol_iterator)())
79  {
80  // G4cout << "G4MoleculeCounter::Initialize" << G4endl;
81  // G4cout << mol_iterator->value()->GetName() << G4endl;
82  fCounterMap[*(mol_iterator.value())]; // initialize the second map
83  }
84 }
85 
86 void G4MoleculeCounter::SetTimeSlice(double timeSlice)
87 {
89 }
90 
92 {
93  if (fpLastSearch.get() == 0)
94  {
95  fpLastSearch.reset(new Search());
96  }
97  else
98  {
99  if (fpLastSearch->fLowerBoundSet && fpLastSearch->fLastMoleculeSearched
100  ->first
101  == molecule) return true;
102  }
103 
104  CounterMapType::iterator mol_it = fCounterMap.find(molecule);
105  fpLastSearch->fLastMoleculeSearched = mol_it;
106 
107  if (mol_it != fCounterMap.end()) // TODO
108  {
109  fpLastSearch->fLowerBoundTime = fpLastSearch->fLastMoleculeSearched->second
110  .end();
111  fpLastSearch->fLowerBoundSet = true;
112  }
113  else
114  {
115  fpLastSearch->fLowerBoundSet = false;
116  }
117 
118  return false;
119 }
120 
122  bool sameTypeOfMolecule)
123 {
124  CounterMapType::iterator mol_it = fpLastSearch->fLastMoleculeSearched;
125  if (mol_it == fCounterMap.end()) return 0; // RETURN
126 
127  NbMoleculeAgainstTime& timeMap = mol_it->second;
128  if (timeMap.empty()) return 0;
129 
130  NbMoleculeAgainstTime::iterator end_time = timeMap.end();
131 
132  if (sameTypeOfMolecule == true)
133  {
134  //G4cout << "SAME MOLECULE" << G4endl;
135  if (fpLastSearch->fLowerBoundSet && fpLastSearch->fLowerBoundTime
136  != end_time)
137  {
138  //G4cout << fpLastSearch->fLowerBoundTime->first << G4endl;
139 // G4cout << "fpLastSearch->fLowerBoundTime != timeMap.end() " << time << G4endl;
140  if (fpLastSearch->fLowerBoundTime->first < time)
141  {
142  NbMoleculeAgainstTime::iterator upperToLast = fpLastSearch
143  ->fLowerBoundTime;
144  upperToLast++;
145 
146  if (upperToLast == end_time)
147  {
148  return fpLastSearch->fLowerBoundTime->second;
149  }
150 
151  if (upperToLast->first > time)
152  {
153  return fpLastSearch->fLowerBoundTime->second;
154  }
155  }
156  }
157  }
158  /*
159  else
160  {
161  G4cout << "\n" << G4endl;
162  G4cout << "Molecule has changed" << G4endl;
163  G4cout << "\n" << G4endl;
164  }
165  */
166  //G4cout << "Searching" << G4endl;
167  // With upper bound
168  NbMoleculeAgainstTime::iterator up_time_it = timeMap.upper_bound(time);
169 
170  if (up_time_it == end_time)
171  {
172  NbMoleculeAgainstTime::reverse_iterator last_time = timeMap.rbegin();
173 
174  if (last_time->first <= time)
175  {
176  //G4cout << "RETURN LAST : " << G4BestUnit(time, "Time") << G4endl;
177  return last_time->second;
178  }
179 
180 // G4cout << "RETURN 0 (1)" << G4endl;
181  return 0; // RETURN
182  }
183  if (up_time_it == timeMap.begin())
184  {
185 // G4cout << "RETURN 0 (2)" << G4endl;
186  return 0; // RETURN
187  }
188 
189  //G4cout << "Going back : " << up_time_it->first << "-->";
190 
191  up_time_it--;
192 
193 // G4cout << up_time_it->first << G4endl;
194 
195  fpLastSearch->fLowerBoundTime = up_time_it;
196  fpLastSearch->fLowerBoundSet = true;
197 
198 // G4cout << "returning : " << fpLastSearch->fLowerBoundTime->second << G4endl;
199 
200  return fpLastSearch->fLowerBoundTime->second;
201 }
202 
204  double time)
205 {
206  G4bool sameTypeOfMolecule = SearchTimeMap(molecule);
207  return SearchUpperBoundTime(time, sameTypeOfMolecule);
208  // NbMoleculeAgainstTime::iterator low_time_it = timeMap.lower_bound(time);
209 }
210 
212  G4double time)
213 {
214  if (fDontRegister[molecule.GetDefinition()]) return;
215 
216  if (fVerbose)
217  {
218  G4cout << "G4MoleculeCounter::AddAMoleculeAtTime : " << molecule.GetName()
219  << " at time : " << G4BestUnit(time, "Time") << G4endl;
220  }
221 
222  CounterMapType::iterator counterMap_i = fCounterMap.find(molecule);
223 
224  if (counterMap_i == fCounterMap.end())
225  {
226  // DEBUG
227  // if(fVerbose) G4cout << " !! ***** Map is empty " << G4endl;
228  fCounterMap[molecule][time] = 1;
229  }
230  else if (counterMap_i->second.empty())
231  {
232  // DEBUG
233  // if(fVerbose) G4cout << " !! ***** Map is empty " << G4endl;
234  counterMap_i->second[time] = 1;
235  }
236  else
237  {
238  NbMoleculeAgainstTime::iterator end = counterMap_i->second.end();
239  end--;
240 
241  // DEBUG
242  // if(fVerbose)
243  // G4cout<<"!! End Time = "<< G4BestUnit(end->first, "Time") <<G4endl;
244 
245  if (end->first <= time)
246  {
247  counterMap_i->second[time] = end->second + 1;
248  }
249  else
250  {
251  NbMoleculeAgainstTime::iterator it = counterMap_i->second.lower_bound(
252  time);
253 
254  while (it->first > time && it != counterMap_i->second.begin())
255  {
256  // DEBUG
257  // if(fVerbose)
258  // G4cout<<"!! ********** Is going back!!!!"<<G4endl;
259  it--;
260  }
261 
262  if (it == counterMap_i->second.begin() && it->first > time)
263  {
264  // DEBUG
265  // if(fVerbose)
266  // G4cout<<"!! ********** Illegal !!!!"<<G4endl;
267  return;
268  }
269 
270  // DEBUG
271  // if(fVerbose)
272  // {
273  // G4cout<<"!! PREVIOUS NB = "<< it->second <<G4endl;
274  // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it->first,"Time") <<G4endl;
275  // }
276  counterMap_i->second[time] = it->second + 1;
277  }
278  }
279 
280  // DEBUG
281  // if(fVerbose)
282  // G4cout<<"!! NB = "<< fCounterMap[molecule][time]<<G4endl;
283 }
284 
286  G4double time)
287 {
288  if (fDontRegister[molecule.GetDefinition()]) return;
289 
290  if (fVerbose)
291  {
292  G4cout << "G4MoleculeCounter::RemoveAMoleculeAtTime : "
293  << molecule.GetName() << " at time : " << G4BestUnit(time, "Time")
294  << G4endl;
295  }
296 
297  NbMoleculeAgainstTime& nbMolPerTime = fCounterMap[molecule];
298 
299  if (nbMolPerTime.empty())
300  {
301  molecule.PrintState();
302  G4String errMsg =
303  "You are trying to remove molecule " + molecule.GetName()
304  + " from the counter while this kind of molecules has not been registered yet";
305  G4Exception("G4MoleculeCounter::RemoveAMoleculeAtTime", "",
306  FatalErrorInArgument, errMsg);
307 
308  return;
309  }
310  else
311  {
312  NbMoleculeAgainstTime::iterator it;
313 
314  if (nbMolPerTime.size() == 1)
315  {
316  it = nbMolPerTime.begin();
317  // DEBUG
318  // if(fVerbose)
319  // G4cout << "!! fCounterMap[molecule].size() == 1" << G4endl;
320  }
321  else
322  {
323  it = nbMolPerTime.lower_bound(time);
324  }
325 
326  if (it == nbMolPerTime.end())
327  {
328  // DEBUG
329  // if(fVerbose)
330  // G4cout << " ********** NO ITERATOR !!!!!!!!! " << G4endl;
331  it--;
332 
333  if (time < it->first)
334  {
335  G4String errMsg = "There was no " + molecule.GetName()
336  + " record at the time or even before the time asked";
337  G4Exception("G4MoleculeCounter::RemoveAMoleculeAtTime", "",
338  FatalErrorInArgument, errMsg);
339  }
340  }
341 
342  // DEBUG
343  // if(fVerbose)
344  // {
346  // G4cout<<"!! Molecule = " << molecule.GetName() << G4endl;
347  // G4cout<<"!! At Time = "<< G4BestUnit(time,"Time") <<G4endl;
348  // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it->first,"Time")<<G4endl;
349  // G4cout<<"!! PREVIOUS Nb = "<< it->second <<G4endl;
350  // }
351 
352  // If valgrind problem on the line below, it means that the pointer "it"
353  // points nowhere
354  if (nbMolPerTime.value_comp()(*it, *nbMolPerTime.begin()))
355  {
356  // DEBUG
357  // if(fVerbose)
358  // G4cout<<"!! ***** In value_comp ... " << G4endl;
359  it++;
360  if (time < it->first)
361  {
362  G4String errMsg = "There was no " + molecule.GetName()
363  + " record at the time or even before the time asked";
364  G4Exception("G4MoleculeCounter::RemoveAMoleculeAtTime", "",
365  FatalErrorInArgument, errMsg);
366  }
367  }
368 
369  while (it->first - time > compDoubleWithPrecision::fPrecision
370  && it != nbMolPerTime.begin())
371  {
372  // DEBUG
373  // if(fVerbose)
374  // {
375  // G4cout<<"!! ***** Is going back!!!!"<<G4endl;
376  // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it-> first,"Time") <<G4endl;
377  // }
378  it--;
379  }
380 
381  if (it == nbMolPerTime.begin() && it->first > time)
382  {
383  // DEBUG
384  // if(fVerbose)
385  // G4cout<<"!! ********** Illegal !!!!"<<G4endl;
386  return;
387  }
388 
389  // DEBUG
390  // if(fVerbose)
391  // {
392  // G4cout<<"!! PREVIOUS NB = "<< (*it).second <<G4endl;
393  // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it->first,"Time")<<G4endl;
394  // }
395  nbMolPerTime[time] = it->second - 1;
396  }
397 
398  // DEBUG
399  // if(fVerbose)
400  // {
401  // G4cout<<"!! NB = "<< nbMolPerTime[time]<<G4endl;
402  // }
403 }
404 
406 {
407  if (fVerbose > 1)
408  {
409  G4cout << "Entering in G4MoleculeCounter::RecordMolecules" << G4endl;
410  }
411 
412  CounterMapType::iterator it;
413  RecordedMolecules output (new vector<G4Molecule>);
414 
415  for(it = fCounterMap.begin(); it != fCounterMap.end(); it++)
416  {
417  output->push_back(it->first);
418  }
419  return output;
420 }
421 
423 {
424  RecordedTimes output(new std::set<G4double>);
425 
426  //G4double time;
427 
428  CounterMapType::iterator it;
429  CounterMapType::const_iterator ite;
430 
431  NbMoleculeAgainstTime::iterator it2;
432  NbMoleculeAgainstTime::const_iterator ite2;
433 
434  // iterate on each molecule
435  for (it = fCounterMap.begin(), ite = fCounterMap.end(); it != ite; ++it)
436  {
437  // iterate on each time
438  for (it2 = (it->second).begin(), ite2 = (it->second).end(); it2 != ite2;
439  ++it2)
440  {
441  //time = it2->first;
442  output->insert(it2->first);
443  }
444  }
445 
446  return output;
447 }
int SearchUpperBoundTime(double time, bool sameTypeOfMolecule)
virtual void AddAMoleculeAtTime(const G4Molecule &, G4double)
G4MoleculeModelIterator GetModelIterator()
static G4MoleculeCounter * GetMoleculeCounter()
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
G4bool SearchTimeMap(const G4Molecule &molecule)
#define G4ThreadLocal
Definition: tls.hh:89
int GetNMoleculesAtTime(const G4Molecule &molecule, double time)
const G4String & GetName() const
Returns the name of the molecule.
Definition: G4Molecule.cc:303
RecordedMolecules GetRecordedMolecules()
G4GLOB_DLL std::ostream G4cout
static G4MoleculeCounter * Instance()
bool G4bool
Definition: G4Types.hh:79
#define FALSE
Definition: globals.hh:52
static G4MoleculeTable * Instance()
stdunique_ptr< std::set< G4double > > RecordedTimes
const G4MoleculeDefinition * GetDefinition() const
Get molecule definition.
Definition: G4Molecule.cc:455
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4ThreadLocal double fPrecision
void PrintState() const
Show the electronic state of the molecule.
Definition: G4Molecule.cc:323
static G4ThreadLocal G4MoleculeCounter * fpInstance
stdunique_ptr< std::vector< G4Molecule > > RecordedMolecules
std::map< G4double, G4int, compDoubleWithPrecision > NbMoleculeAgainstTime
static void InitializeInstance()
#define G4endl
Definition: G4ios.hh:61
RecordedTimes GetRecordedTimes()
Class Description The dynamic molecule holds all the data that change for a molecule It has a pointer...
Definition: G4Molecule.hh:93
double G4double
Definition: G4Types.hh:76
virtual void RemoveAMoleculeAtTime(const G4Molecule &, G4double)
void SetTimeSlice(double)
static void DeleteInstance()