Geant4  10.01.p01
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 85244 2014-10-27 08:24:13Z 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 < fReactants->size() ; j++)
334 // {
335 // G4cout << GetMolecule(fReactants->at(j) )->GetName()
336 // <<" ("<< fReactants->at(j)->GetTrackID() << ")" << G4endl;
337 // }
338 // }
339  }
340 
341 #ifdef G4VERBOSE
342  // DEBUG
343  if (fVerbose)
344  {
345  G4cout << "G4MoleculeEncounterStepper::CalculateStep will finally return :"
346  << G4BestUnit(fSampledMinTimeStep, "Time") << G4endl;
347 
348  if(fVerbose > 1)
349  {
350  G4cout << "Selected reactants for trackA: " << moleculeA->GetName()
351  << " (" << trackA.GetTrackID() << ") are: ";
352 
353  vector<G4Track*>::iterator it;
354  for(it = fReactants->begin(); it != fReactants->end(); it++)
355  {
356  G4Track* trackB = *it;
357  G4cout << GetMolecule(trackB)->GetName() << " ("
358  << trackB->GetTrackID() << ") \t ";
359  }
360  G4cout << G4endl;
361  }
362  }
363 #endif
364  return fSampledMinTimeStep;
365 }
366 
368 #ifdef G4VERBOSE
369  const G4double R,
370 #endif
371  G4KDTreeResultHandle& results)
372 {
373  if (results == 0)
374  {
375 #ifdef G4VERBOSE
376  // DEBUG
377  if (fVerbose > 1)
378  {
379  G4cout << "No molecule " << utils.moleculeB->GetName()
380  << " found to react with " << utils.moleculeA->GetName()
381  << G4endl;
382  }
383 #endif
384  return;
385  }
386 
387  for (results->Rewind(); !results->End(); results->Next())
388  {
389 
390  G4IT* reactiveB = results->GetItem<G4IT>();
391 
392  if (reactiveB == 0)
393  {
394  // DEBUG
395  // G4cout<<"Continue 1"<<G4endl;
396  continue;
397  }
398 
399  G4Track *trackB = reactiveB->GetTrack();
400 
401  if (trackB == 0)
402  {
403  G4ExceptionDescription exceptionDescription;
404  exceptionDescription
405  <<"The reactant B found using the ITManager does not have a valid "
406  "track attached to it. If this is done on purpose, please do "
407  "not record this molecule in the ITManager."
408  << G4endl;
409  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
410  "MoleculeEncounterStepper001", FatalErrorInArgument,
411  exceptionDescription);
412  continue;
413  }
414 
415  if (trackB->GetTrackStatus() != fAlive)
416  {
417  G4ExceptionDescription exceptionDescription;
418  exceptionDescription
419  << "The track status of one of the nearby reactants is not fAlive"
420  << G4endl;
421  exceptionDescription << "The incomming trackID "
422  << "(trackA entering in G4DNAMoleculeEncounterStepper and "
423  << "for which you are looking reactant for) is : "
424  << utils.trackA.GetTrackID() << "("
425  << GetMolecule(utils.trackA)->GetName() << ")" << G4endl;
426  exceptionDescription << "And the trackID of the reactant (trackB) is: "
427  << trackB->GetTrackID() << "(" << GetMolecule(trackB)->GetName()
428  << ")" << G4endl;
429  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
430  "MoleculeEncounterStepper002", FatalErrorInArgument,
431  exceptionDescription);
432  continue;
433  }
434 
435  if (trackB == &utils.trackA)
436  {
437  // DEBUG
438  G4ExceptionDescription exceptionDescription;
439  exceptionDescription
440  << "A track is reacting with itself (which is impossible) ie trackA == trackB"
441  << G4endl;
442  exceptionDescription << "Molecule A (and B) is of type : "
443  << utils.moleculeA->GetName() << " with trackID : "
444  << utils.trackA.GetTrackID() << G4endl;
445 
446  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
447  "MoleculeEncounterStepper003", FatalErrorInArgument,
448  exceptionDescription);
449 
450  }
451 
452  if (fabs(trackB->GetGlobalTime() - utils.trackA.GetGlobalTime())
453  > utils.trackA.GetGlobalTime() * (1 - 1 / 100))
454  {
455  // DEBUG
456  G4ExceptionDescription exceptionDescription;
457  exceptionDescription
458  << "The interacting tracks are not synchronized in time" << G4endl;
459  exceptionDescription
460  << "trackB->GetGlobalTime() != trackA.GetGlobalTime()" << G4endl;
461 
462  exceptionDescription << "trackA : trackID : " << utils.trackA.GetTrackID()
463  << "\t Name :" << utils.moleculeA->GetName()
464  << "\t trackA->GetGlobalTime() = "
465  << G4BestUnit(utils.trackA.GetGlobalTime(), "Time") << G4endl;
466 
467  exceptionDescription << "trackB : trackID : " << trackB->GetTrackID()
468  << "\t Name :" << utils.moleculeB->GetName()
469  << "\t trackB->GetGlobalTime() = "
470  << G4BestUnit(trackB->GetGlobalTime(), "Time") << G4endl;
471 
472  G4Exception("G4DNAMoleculeEncounterStepper::RetrieveResults",
473  "MoleculeEncounterStepper004", FatalErrorInArgument,
474  exceptionDescription);
475  }
476 
477 #ifdef G4VERBOSE
478  if(fVerbose > 1)
479  {
480  G4double r2 = results->GetDistanceSqr();
481  G4cout << "\t ************************************************** " << G4endl;
482  G4cout <<"\t Reaction between "
483  << utils.moleculeA->GetName() << " (" << utils.trackA.GetTrackID() << ") "
484  << " & " << utils.moleculeB->GetName() << " (" << trackB->GetTrackID() << "), "
485  << "Interaction Range = "
486  << G4BestUnit(R, "Length")<<G4endl;
487  G4cout <<"\t Real distance between reactants = "
488  << G4BestUnit((utils.trackA.GetPosition() - trackB->GetPosition()).mag(), "Length")<<G4endl;
489  G4cout <<"\t Distance between reactants calculated by nearest neighbor algorithm = "
490  << G4BestUnit(sqrt(r2), "Length")<<G4endl;
491  // G4cout << " ***** " << G4endl;
492  }
493 #endif
494 
495  fReactants->push_back(trackB);
496  }
497 }
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)
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
const std::vector< const G4Molecule * > * CanReactWith(const G4Molecule *aMolecule) const
Given a molecule's type, it returns with which a reaction is allowed.
Before stepping all tracks G4Scheduler calls all the G4VITModel which may contain a G4VITTimeStepper ...
#define DBL_MAX
Definition: templates.hh:83
{ Class description: