Geant4  10.01.p03
G4DNAMoleculeEncounterStepper.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: G4DNAMoleculeEncounterStepper.cc 90769 2015-06-09 10:33:41Z gcosmo $
27 //
28 // Author: Mathieu Karamitros (kara@cenbg.in2p3.fr)
29 //
30 // WARNING : This class is released as a prototype.
31 // It might strongly evolve or even disapear in the next releases.
32 //
33 // History:
34 // -----------
35 // 10 Oct 2011 M.Karamitros created
36 //
37 // -------------------------------------------------------------------
38 
40 #include "G4VDNAReactionModel.hh"
42 #include "G4H2O.hh"
43 #include "CLHEP/Utility/memory.h"
44 #include "G4UnitsTable.hh"
45 #include "G4MoleculeFinder.hh"
46 
47 using namespace std;
48 using namespace CLHEP;
49 
50 //#define DEBUG_MEM
51 
52 #ifdef DEBUG_MEM
53 #include "G4MemStat.hh"
54 using namespace G4MemStat;
55 #endif
56 
58  const G4Molecule* mB) :
59  trackA(tA), moleculeB(mB)
60 {
61  moleculeA = GetMolecule(tA);
64  Constant = 8 * (DA + DB + 2 * sqrt(DA * DB));
65 }
66 
72 {
73  fVerbose = 0;
75 }
76 
78 {
79  if (this == &rhs) return *this;
80  fReactionModel = 0;
81  fVerbose = rhs.fVerbose;
84  return *this;
85 }
86 
88 {
89 }
90 
92  G4VITTimeStepComputer(right),
93  fMolecularReactionTable(
94  reference_cast<const G4DNAMolecularReactionTable*>(fpReactionTable))
95 {
96  fVerbose = right.fVerbose;
98  fReactionModel = 0;
100 }
101 
103 {
104  // DEBUG
105  // G4cout << "G4DNAMoleculeEncounterStepper::PrepareForAllProcessors" << G4endl;
107 
108 #if defined (DEBUG_MEM)
109  MemStat mem_first, mem_second, mem_diff;
110 #endif
111 
112 #if defined (DEBUG_MEM)
113  mem_first = MemoryUsage();
114 #endif
116 
117 #if defined (DEBUG_MEM)
118  mem_second = MemoryUsage();
119  mem_diff = mem_second-mem_first;
120  G4cout << "\t || MEM || G4DNAMoleculeEncounterStepper::Prepare || "
121  "After computing G4ITManager<G4Molecule>::Instance()->"
122  "UpdatePositionMap, diff is : " << mem_diff << G4endl;
123 #endif
124 }
125 
127 {
128 // if(fReactants) fReactants = 0 ;
129  if (fReactants) fReactants.reset();
132 }
133 
134 template<typename T>
135  inline bool IsInf(T value)
136  {
137  return std::numeric_limits<T>::has_infinity
138  && value == std::numeric_limits<T>::infinity();
139  }
140 
142  const G4double& userMinTimeStep)
143 {
144  // DEBUG
145 // G4cout << "G4MoleculeEncounterStepper::CalculateStep, time :"
146 // << G4ITTrackHolder::Instance()->GetGlobalTime() << G4endl;
147 
148  G4Molecule* moleculeA = GetMolecule(trackA);
150  fUserMinTimeStep = userMinTimeStep;
151 
152 #ifdef G4VERBOSE
153  if (fVerbose)
154  {
155  G4cout
156  << "_______________________________________________________________________"
157  << G4endl;
158  G4cout << "G4DNAMoleculeEncounterStepper::CalculateStep" << G4endl;
159  G4cout << "Check done for molecule : " << moleculeA->GetName()
160  << " (" << trackA.GetTrackID() << ") "
161  << G4endl;
162  }
163 #endif
164 
165  //__________________________________________________________________
166  // Retrieve general informations for making reactions
167  const vector<const G4Molecule*>* reactivesVector = fMolecularReactionTable
168  ->CanReactWith(moleculeA);
169 
170  if (!reactivesVector)
171  {
172 #ifdef G4VERBOSE
173  // DEBUG
174  if (fVerbose > 1)
175  {
176  G4cout << "!!!!!!!!!!!!!!!!!!!!" << G4endl;
177  G4cout << "!!! WARNING" << G4endl;
178  G4cout << "G4MoleculeEncounterStepper::CalculateStep will return infinity "
179  "for the reaction because the molecule "
180  << moleculeA->GetName()
181  << " does not have any reactants given in the reaction table."
182  << G4endl;
183  G4cout << "!!!!!!!!!!!!!!!!!!!!"<<G4endl;
184  }
185 #endif
186  return DBL_MAX;
187  }
188 
189  G4int nbReactives = reactivesVector->size();
190 
191  if (nbReactives == 0)
192  {
193 #ifdef G4VERBOSE
194  // DEBUG
195  if (fVerbose)
196  {
197  // TODO replace with the warning mode of G4Exception
198  G4cout << "!!!!!!!!!!!!!!!!!!!!" << G4endl;
199  G4cout << "!!! WARNING" << G4endl;
200  G4cout << "G4MoleculeEncounterStepper::CalculateStep will return infinity "
201  "for the reaction because the molecule "
202  << moleculeA->GetName()
203  << " does not have any reactants given in the reaction table."
204  << "This message can also result from a wrong implementation of the reaction table."
205  << G4endl;
206  G4cout << "!!!!!!!!!!!!!!!!!!!!"<<G4endl;
207  }
208 #endif
209  return DBL_MAX;
210  }
211  // DEBUG
212  // else
213  // {
214  // G4cout << "nb reactants : " << nbReactives << " pour mol "<< moleculeA -> GetName () << G4endl;
215  // for(int k=0 ; k < nbReactives ; k++)
216  // {
217  // G4cout << (*reactivesVector)[k]->GetName() << G4endl;
218  // }
219  // }
220 
221 // fReactants = new vector<G4Track*>();
222  fReactants.reset(new vector<G4Track*>());
223  fReactionModel->Initialise(moleculeA, trackA);
224 
225  //__________________________________________________________________
226  // Start looping on possible reactants
227  for (G4int i = 0; i < nbReactives; i++)
228  {
229  const G4Molecule* moleculeB = (*reactivesVector)[i];
230 
231  //______________________________________________________________
232  // Retrieve reaction range
234 
235  //G4cout << "Reaction range = " << G4BestUnit(R, "Length") << G4endl;
236 
237  //______________________________________________________________
238  // Use KdTree algorithm to find closest reactants
239  G4KDTreeResultHandle resultsNearest(
240  G4MoleculeFinder::Instance()->FindNearest(moleculeA, moleculeB));
241 
242  if (resultsNearest == 0) continue;
243 
244  G4double r2 = resultsNearest->GetDistanceSqr();
245  Utils utils(trackA, moleculeB);
246 
247  if (r2 <= R * R) // ==> Record in range
248  {
249  // Entering in this condition may due to the fact that molecules are very close
250  // to each other
251  // Therefore, if we only take the nearby reactant into account, it might have already
252  // reacted. Instead, we will take all possible reactants that satisfy the condition r<R
253 
254  if (fHasAlreadyReachedNullTime == false)
255  {
256  fReactants->clear();
258  }
259 
260  fSampledMinTimeStep = 0.;
261  G4KDTreeResultHandle resultsInRange(
262 // G4ITManager<G4Molecule>::Instance()
263  G4MoleculeFinder::Instance()->FindNearestInRange(moleculeA, moleculeB,
264  R));
265  CheckAndRecordResults(utils,
266 #ifdef G4VERBOSE
267  R,
268 #endif
269  resultsInRange);
270  }
271  else
272  {
273  G4double r = sqrt(r2);
274  G4double tempMinET = pow(r - R, 2) / utils.Constant;
275  // constant = 16 * (DA + DB + 2*sqrt(DA*DB))
276 
277  //G4cout << tempMinET << G4endl;
278 // G4cout << "fSampledMinTimeStep =" << fSampledMinTimeStep << G4endl;
279 // G4cout << "fUserMinTimeStep =" << fUserMinTimeStep
280 // << " isInf = "<< IsInf(fUserMinTimeStep) << G4endl;
281 
282  if (tempMinET <= fSampledMinTimeStep)
283  {
284  if (fUserMinTimeStep < DBL_MAX/*IsInf(fUserMinTimeStep) == false*/
285  && tempMinET <= fUserMinTimeStep) // ==> Record in range
286  {
288  {
289  fReactants->clear();
290  }
291 
293 
294  G4double range = R + sqrt(fUserMinTimeStep*utils.Constant);
295 
296  G4KDTreeResultHandle resultsInRange (
298 // G4ITManager<G4Molecule>::Instance()
299  -> FindNearestInRange(moleculeA, moleculeB,range));
300 
301  CheckAndRecordResults(utils,
302 #ifdef G4VERBOSE
303  range,
304 #endif
305  resultsInRange);
306  }
307  else // ==> Record nearest
308  {
309  if(tempMinET < fSampledMinTimeStep)
310  // to avoid cases where fSampledMinTimeStep == tempMinET
311  {
312  fSampledMinTimeStep = tempMinET;
313  fReactants->clear();
314  }
315 
316  CheckAndRecordResults(utils,
317 #ifdef G4VERBOSE
318  R,
319 #endif
320  resultsNearest);
321  }
322  }
323  }
324 
325  // DEBUG
326 // if(bool(fReactants))
327 // {
328 // G4cout << "Potential reactions :" << G4endl;
329 // G4cout << GetMolecule(trackA)->GetName()
330 // << " ("<< trackA.GetTrackID()<< ") " << " + ..." << G4endl;
331 // //<< " | " << trackB->GetTrackID() << G4endl;
332 //
333 // for(int j = 0 ; j < (int) fReactants->size() ; j++)
334 // {
335 // G4cout << GetMolecule(fReactants->at(j) )->GetName()
336 // <<" ("<< fReactants->at(j)->GetTrackID() << ")" << G4endl;
337 // }
338 // }
339 
340  }
341 
342 #ifdef G4VERBOSE
343  // DEBUG
344  if (fVerbose)
345  {
346  G4cout << "G4MoleculeEncounterStepper::CalculateStep will finally return :"
347  << G4BestUnit(fSampledMinTimeStep, "Time") << G4endl;
348 
349  if(fVerbose > 1)
350  {
351  G4cout << "Selected reactants for trackA: " << moleculeA->GetName()
352  << " (" << trackA.GetTrackID() << ") are: ";
353 
354  vector<G4Track*>::iterator it;
355  for(it = fReactants->begin(); it != fReactants->end(); it++)
356  {
357  G4Track* trackB = *it;
358  G4cout << GetMolecule(trackB)->GetName() << " ("
359  << trackB->GetTrackID() << ") \t ";
360  }
361  G4cout << G4endl;
362  }
363  }
364 #endif
365  return fSampledMinTimeStep;
366 }
367 
369 #ifdef G4VERBOSE
370  const G4double R,
371 #endif
372  G4KDTreeResultHandle& results)
373 {
374  if (results == 0)
375  {
376 #ifdef G4VERBOSE
377  // DEBUG
378  if (fVerbose > 1)
379  {
380  G4cout << "No molecule " << utils.moleculeB->GetName()
381  << " found to react with " << utils.moleculeA->GetName()
382  << G4endl;
383  }
384 #endif
385  return;
386  }
387 
388  for (results->Rewind(); !results->End(); results->Next())
389  {
390 
391  G4IT* reactiveB = results->GetItem<G4IT>();
392 
393  if (reactiveB == 0)
394  {
395  // DEBUG
396  // G4cout<<"Continue 1"<<G4endl;
397  continue;
398  }
399 
400  G4Track *trackB = reactiveB->GetTrack();
401 
402  if (trackB == 0)
403  {
404  G4ExceptionDescription exceptionDescription;
405  exceptionDescription
406  <<"The reactant B found using the ITManager does not have a valid "
407  "track attached to it. If this is done on purpose, please do "
408  "not record this molecule in the ITManager."
409  << G4endl;
410  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
411  "MoleculeEncounterStepper001", FatalErrorInArgument,
412  exceptionDescription);
413  continue;
414  }
415 
416  if (trackB->GetTrackStatus() != fAlive)
417  {
418  G4ExceptionDescription exceptionDescription;
419  exceptionDescription
420  << "The track status of one of the nearby reactants is not fAlive"
421  << G4endl;
422  exceptionDescription << "The incomming trackID "
423  << "(trackA entering in G4DNAMoleculeEncounterStepper and "
424  << "for which you are looking reactant for) is : "
425  << utils.trackA.GetTrackID() << "("
426  << GetMolecule(utils.trackA)->GetName() << ")" << G4endl;
427  exceptionDescription << "And the trackID of the reactant (trackB) is: "
428  << trackB->GetTrackID() << "(" << GetMolecule(trackB)->GetName()
429  << ")" << G4endl;
430  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
431  "MoleculeEncounterStepper002", FatalErrorInArgument,
432  exceptionDescription);
433  continue;
434  }
435 
436  if (trackB == &utils.trackA)
437  {
438  // DEBUG
439  G4ExceptionDescription exceptionDescription;
440  exceptionDescription
441  << "A track is reacting with itself (which is impossible) ie trackA == trackB"
442  << G4endl;
443  exceptionDescription << "Molecule A (and B) is of type : "
444  << utils.moleculeA->GetName() << " with trackID : "
445  << utils.trackA.GetTrackID() << G4endl;
446 
447  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
448  "MoleculeEncounterStepper003", FatalErrorInArgument,
449  exceptionDescription);
450 
451  }
452 
453  if (fabs(trackB->GetGlobalTime() - utils.trackA.GetGlobalTime())
454  > utils.trackA.GetGlobalTime() * (1 - 1 / 100))
455  {
456  // DEBUG
457  G4ExceptionDescription exceptionDescription;
458  exceptionDescription
459  << "The interacting tracks are not synchronized in time" << G4endl;
460  exceptionDescription
461  << "trackB->GetGlobalTime() != trackA.GetGlobalTime()" << G4endl;
462 
463  exceptionDescription << "trackA : trackID : " << utils.trackA.GetTrackID()
464  << "\t Name :" << utils.moleculeA->GetName()
465  << "\t trackA->GetGlobalTime() = "
466  << G4BestUnit(utils.trackA.GetGlobalTime(), "Time") << G4endl;
467 
468  exceptionDescription << "trackB : trackID : " << trackB->GetTrackID()
469  << "\t Name :" << utils.moleculeB->GetName()
470  << "\t trackB->GetGlobalTime() = "
471  << G4BestUnit(trackB->GetGlobalTime(), "Time") << G4endl;
472 
473  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
474  "MoleculeEncounterStepper004", FatalErrorInArgument,
475  exceptionDescription);
476  }
477 
478 #ifdef G4VERBOSE
479  if(fVerbose > 1)
480  {
481  G4double r2 = results->GetDistanceSqr();
482  G4cout << "\t ************************************************** " << G4endl;
483  G4cout <<"\t Reaction between "
484  << utils.moleculeA->GetName() << " (" << utils.trackA.GetTrackID() << ") "
485  << " & " << utils.moleculeB->GetName() << " (" << trackB->GetTrackID() << "), "
486  << "Interaction Range = "
487  << G4BestUnit(R, "Length")<<G4endl;
488  G4cout <<"\t Real distance between reactants = "
489  << G4BestUnit((utils.trackA.GetPosition() - trackB->GetPosition()).mag(), "Length")<<G4endl;
490  G4cout <<"\t Distance between reactants calculated by nearest neighbor algorithm = "
491  << G4BestUnit(sqrt(r2), "Length")<<G4endl;
492  // G4cout << " ***** " << G4endl;
493  }
494 #endif
495 
496  fReactants->push_back(trackB);
497  }
498 }
const G4ITReactionTable * fpReactionTable
void CheckAndRecordResults(const Utils &, G4KDTreeResultHandle &)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4TrackVectorHandle fReactants
virtual void Initialise(const G4Molecule *, const G4Track &)
This macro is defined in AddClone_def.
G4DNAMoleculeEncounterStepper & operator=(const G4DNAMoleculeEncounterStepper &)
const G4ThreeVector & GetPosition() const
G4TrackStatus GetTrackStatus() const
G4DNAMolecularReactionTable sorts out the G4DNAMolecularReactionData for bimolecular reaction...
ReturnType & reference_cast(OriginalType &source)
G4double GetDiffusionCoefficient() const
Returns the diffusion coefficient D.
Definition: G4Molecule.cc:465
bool IsInf(T value)
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
int G4int
Definition: G4Types.hh:78
MemStat MemoryUsage()
Definition: G4MemStat.cc:55
virtual G4double CalculateStep(const G4Track &, const G4double &)
const G4String & GetName() const
Returns the name of the molecule.
Definition: G4Molecule.cc:303
G4GLOB_DLL std::ostream G4cout
static G4ThreadLocal G4double fUserMinTimeStep
Utils(const G4Track &tA, const G4Molecule *mB)
const std::vector< const G4Molecule * > * CanReactWith(const G4Molecule *) const
Given a molecule's type, it returns with which a reaction is allowed.
G4int GetTrackID() const
G4double GetGlobalTime() const
G4Molecule * GetMolecule(const G4Track &track)
Definition: G4Molecule.cc:67
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual void UpdatePositionMap()
static G4ITFinder * Instance()
const G4DNAMolecularReactionTable *& fMolecularReactionTable
Given a molecule G4DNAMoleculeEncounterStepper will calculate for its possible reactants what will be...
G4Track * GetTrack()
Definition: G4IT.hh:240
virtual G4double GetReactionRadius(const G4Molecule *, const G4Molecule *)=0
#define G4endl
Definition: G4ios.hh:61
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
Before stepping all tracks G4Scheduler calls all the G4VITModel which may contain a G4VITTimeStepper ...
#define DBL_MAX
Definition: templates.hh:83
{ Class description: