Geant4  10.01.p03
G4DNAMolecularReactionTable.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: G4DNAMolecularReactionTable.cc 90769 2015-06-09 10:33:41Z gcosmo $
27 //
28 // Author: Mathieu Karamitros (kara (AT) 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 
39 #include <iomanip>
40 
42 #include "G4PhysicalConstants.hh"
43 #include "G4SystemOfUnits.hh"
44 #include "G4UIcommand.hh"
45 #include "G4VDNAReactionModel.hh"
47 #include "G4MoleculeTable.hh"
48 
49 using namespace std;
50 
51 class IosFlagSaver
52 {
53 public:
54  explicit IosFlagSaver(std::ostream& _ios) :
55  ios(_ios), f(_ios.flags())
56  {
57  }
59  {
60  ios.flags(f);
61  }
62 
63 // IosFlagSaver(const IosFlagSaver &rhs) = delete;
64 // IosFlagSaver& operator= (const IosFlagSaver& rhs) = delete;
65 
66 private:
67  std::ostream& ios;
68  std::ios::fmtflags f;
69 };
70 
72 //G4ThreadLocal G4DNAMolecularReactionTable* G4DNAMolecularReactionTable::fInstance(0);
73 
75  fReactive1(),
76  fReactive2(),
77  fReactionRate(0.),
78  fReducedReactionRadius(0.),
79  fProducts(0)
80 {
81  ;
82 }
83 
85  const G4Molecule* reactive1,
86  const G4Molecule* reactive2) :
87  fProducts(0)
88 {
89  fReactionRate = reactionRate;
90  SetReactive1(reactive1);
91  SetReactive2(reactive2);
92 
93  G4double sumDiffCoeff(0.);
94 
95  if (*reactive1 == *reactive2)
96  {
97  sumDiffCoeff = reactive1->GetDiffusionCoefficient();
99  / (4 * pi * sumDiffCoeff * Avogadro);
100  }
101  else
102  {
103  sumDiffCoeff = reactive1->GetDiffusionCoefficient()
104  + reactive2->GetDiffusionCoefficient();
105  fReducedReactionRadius = fReactionRate / (4 * pi * sumDiffCoeff * Avogadro);
106  }
107 }
108 
110  const G4String& reactive1,
111  const G4String& reactive2) :
112  fProducts(0)
113 {
114  fReactionRate = reactionRate;
115  SetReactive1(reactive1);
116  SetReactive2(reactive2);
117 
118  G4double sumDiffCoeff(0.);
119 
120  if (*fReactive1 == *fReactive2)
121  {
122  sumDiffCoeff = fReactive1->GetDiffusionCoefficient();
124  / (4 * pi * sumDiffCoeff * Avogadro);
125  }
126  else
127  {
128  sumDiffCoeff = fReactive1->GetDiffusionCoefficient()
130  fReducedReactionRadius = fReactionRate / (4 * pi * sumDiffCoeff * Avogadro);
131  }
132 }
133 
135 {
136  if (fProducts)
137  {
138  fProducts->clear();
139  delete fProducts;
140  fProducts = 0;
141  }
142 }
143 
145 {
146 // fReactive1 = G4MoleculeHandleManager::Instance()->GetMoleculeHandle(reactive);
147  fReactive1 = reactive;
148 }
150 {
151 // fReactive2 = G4MoleculeHandleManager::Instance()->GetMoleculeHandle(reactive);
152  fReactive2 = reactive;
153 }
155  const G4Molecule* reactive2)
156 {
157 // fReactive1 = G4MoleculeHandleManager::Instance()->GetMoleculeHandle(reactive1);
158 // fReactive2 = G4MoleculeHandleManager::Instance()->GetMoleculeHandle(reactive2);
159  fReactive1 = reactive1;
160  fReactive2 = reactive2;
161 }
162 
164 {
165 // if(!fProducts) fProducts = new std::vector<G4MoleculeHandle>();
166  // fProducts->push_back(G4MoleculeHandleManager::Instance()->GetMoleculeHandle(molecule));
167  if (!fProducts) fProducts = new std::vector<const G4Molecule*>();
168  fProducts->push_back(molecule);
169 }
170 
172 {
174 }
176 {
178 }
180  const G4String& reactive2)
181 {
184 }
185 
187 {
188 // if(!fProducts) fProducts = new std::vector<G4MoleculeHandle>();
189  if (!fProducts) fProducts = new std::vector<const G4Molecule*>();
190  fProducts->push_back(G4MoleculeTable::Instance()->GetMoleculeModel(molecule));
191 }
192 //_____________________________________________________________________________________
194 {
195  if (!fInstance)
196  {
198  }
199  return fInstance;
200 }
201 
203 {
204  // DEBUG
205 // G4cout << "G4MolecularReactionTable::DeleteInstance" << G4endl;
206  if (fInstance) delete fInstance;
207  fInstance = 0;
208 }
209 //_____________________________________________________________________________________
212  fMoleculeHandleManager(G4MoleculeHandleManager::Instance())
213 {
214 // G4cout << "G4DNAMolecularReactionTable::G4DNAMolecularReactionTable()" << G4endl;
215  fVerbose = false;
216  return;
217 }
218 //_____________________________________________________________________________________
220 {
221  // DEBUG
222 // G4cout << "G4MolecularReactionTable::~G4MolecularReactionTable" << G4endl;
223 
224  /*
225  ReactionDataMap::iterator it1 = fReactionData.begin();
226 
227  std::map<const G4Molecule*,
228  const G4DNAMolecularReactionData*,
229  compMoleculeP>::iterator it2;
230 
231  for(;it1!=fReactionData.end();it1++)
232  {
233  for(it2 = it1->second.begin();it2 != it1->second.end();it2++)
234  {
235  const G4DNAMolecularReactionData* reactionData = it2->second;
236  if(reactionData)
237  {
238  const G4Molecule* reactive1 = reactionData->GetReactive1();
239  const G4Molecule* reactive2 = reactionData->GetReactive2();
240 
241  fReactionData[reactive1][reactive2] = 0;
242  fReactionData[reactive2][reactive1] = 0;
243 
244  delete reactionData;
245  }
246  }
247  }
248  */
249  fReactionDataMV.clear();
250  fReactionData.clear();
251  fReactivesMV.clear();
252 }
253 //_____________________________________________________________________________________
255 {
256  const G4Molecule* reactive1 = reactionData->GetReactive1();
257  const G4Molecule* reactive2 = reactionData->GetReactive2();
258 
259  fReactionData[reactive1][reactive2] = reactionData;
260  fReactivesMV[reactive1].push_back(reactive2);
261  fReactionDataMV[reactive1].push_back(reactionData);
262 
263  if (reactive1 != reactive2)
264  {
265  fReactionData[reactive2][reactive1] = reactionData;
266  fReactivesMV[reactive2].push_back(reactive1);
267  fReactionDataMV[reactive2].push_back(reactionData);
268  }
269 }
270 //_____________________________________________________________________________________
272  const G4Molecule* reactive1,
273  const G4Molecule* reactive2)
274 {
276  reactionRate, reactive1, reactive2);
277  SetReaction(reactionData);
278 }
279 //_____________________________________________________________________________________
281 {
282  // Print Reactions and Interaction radius for jump step = 3ps
283 
284  IosFlagSaver iosfs(G4cout);
285 
286  if (pReactionModel)
287  {
288  if (!(pReactionModel->GetReactionTable())) pReactionModel->SetReactionTable(
289  this);
290  }
291 
292  ReactivesMV::iterator itReactives;
293 
294  map<G4Molecule*, map<G4Molecule*, G4bool> > alreadyPrint;
295 
296  G4cout << "Number of chemical species involved in reactions = "
297  << fReactivesMV.size() << G4endl;
298 
299  G4int nbPrintable = fReactivesMV.size() * fReactivesMV.size();
300 
301  G4String *outputReaction = new G4String[nbPrintable];
302  G4String *outputReactionRate = new G4String[nbPrintable];
303  G4String *outputRange = new G4String[nbPrintable];
304  G4int n = 0;
305 
306  for (itReactives = fReactivesMV.begin(); itReactives != fReactivesMV.end();
307  itReactives++)
308  {
309  G4Molecule* moleculeA = (G4Molecule*) itReactives->first;
310  const vector<const G4Molecule*>* reactivesVector = CanReactWith(moleculeA);
311 
312  if (pReactionModel) pReactionModel->InitialiseToPrint(moleculeA);
313 
314  G4int nbReactants = fReactivesMV[itReactives->first].size();
315 
316  for (G4int iReact = 0; iReact < nbReactants; iReact++)
317  {
318 
319  G4Molecule* moleculeB = (G4Molecule*) (*reactivesVector)[iReact];
320 
321  const G4DNAMolecularReactionData* reactionData =
322  fReactionData[moleculeA][moleculeB];
323 
324  //-----------------------------------------------------------
325  // Name of the reaction
326  if (!alreadyPrint[moleculeA][moleculeB])
327  {
328  outputReaction[n] = moleculeA->GetName() + " + " + moleculeB->GetName();
329 
330  G4int nbProducts = reactionData->GetNbProducts();
331 
332  if (nbProducts)
333  {
334  outputReaction[n] += " -> " + reactionData->GetProduct(0)->GetName();
335 
336  for (G4int j = 1; j < nbProducts; j++)
337  {
338  outputReaction[n] += " + " + reactionData->GetProduct(j)->GetName();
339  }
340  }
341  else
342  {
343  outputReaction[n] += " -> No product";
344  }
345 
346  //-----------------------------------------------------------
347  // Interaction Rate
348  outputReactionRate[n] = G4UIcommand::ConvertToString(
349  reactionData->GetReactionRate() / (1e-3 * m3 / (mole * s)));
350 
351  //-----------------------------------------------------------
352  // Calculation of the Interaction Range
353  G4double interactionRange = -1;
354  if (pReactionModel) interactionRange =
355  pReactionModel->GetReactionRadius(iReact);
356 
357  if (interactionRange != -1)
358  {
359  outputRange[n] = G4UIcommand::ConvertToString(
360  interactionRange / nanometer);
361  }
362  else
363  {
364  outputRange[n] = "";
365  }
366 
367  alreadyPrint[moleculeB][moleculeA] = TRUE;
368  n++;
369  }
370  }
371  }
372  // G4cout<<"Number of possible reactions: "<< n << G4endl;
373 
375  // Tableau dynamique en fonction du nombre de caractere maximal dans
376  // chaque colonne
378 
379  G4int maxlengthOutputReaction = -1;
380  G4int maxlengthOutputReactionRate = -1;
381 
382  for (G4int i = 0; i < n; i++)
383  {
384  if (maxlengthOutputReaction < (G4int) outputReaction[i].length())
385  {
386  maxlengthOutputReaction = outputReaction[i].length();
387  }
388  if (maxlengthOutputReactionRate < (G4int) outputReactionRate[i].length())
389  {
390  maxlengthOutputReactionRate = outputReactionRate[i].length();
391  }
392  }
393 
394  maxlengthOutputReaction += 2;
395  maxlengthOutputReactionRate += 2;
396 
397  if (maxlengthOutputReaction < 10) maxlengthOutputReaction = 10;
398  if (maxlengthOutputReactionRate < 30) maxlengthOutputReactionRate = 30;
399 
400  G4String* title;
401 
402  if (pReactionModel) title = new G4String[3];
403  else title = new G4String[2];
404 
405  title[0] = "Reaction";
406  title[1] = "Reaction Rate [dm3/(mol*s)]";
407 
408  if (pReactionModel) title[2] =
409  "Interaction Range for chosen reaction model [nm]";
410 
411  G4cout << setfill(' ') << setw(maxlengthOutputReaction) << left << title[0]
412  << setw(maxlengthOutputReactionRate) << left << title[1];
413 
414  if (pReactionModel) G4cout << setw(2) << left << title[2];
415 
416  G4cout << G4endl;
417 
418  G4cout.fill('-');
419  if (pReactionModel) G4cout.width(
420  maxlengthOutputReaction + 2 + maxlengthOutputReactionRate + 2
421  + (G4int) title[2].length());
422  else G4cout.width(maxlengthOutputReaction + 2 + maxlengthOutputReactionRate);
423  G4cout << "-" << G4endl;
424  G4cout.fill(' ');
425 
426  for (G4int i = 0; i < n; i++)
427  {
428  G4cout << setw(maxlengthOutputReaction) << left << outputReaction[i]
429  << setw(maxlengthOutputReactionRate) << left
430  << outputReactionRate[i];
431 
432  if (pReactionModel) G4cout << setw(2) << left << outputRange[i];
433 
434  G4cout << G4endl;
435 
436  G4cout.fill('-');
437  if (pReactionModel) G4cout.width(
438  maxlengthOutputReaction + 2 + maxlengthOutputReactionRate + 2
439  + (G4int) title[2].length());
440  else G4cout.width(
441  maxlengthOutputReaction + 2 + maxlengthOutputReactionRate);
442  G4cout << "-" << G4endl;
443  G4cout.fill(' ');
444  }
445 
446  delete[] title;
447  delete[] outputReaction;
448  delete[] outputReactionRate;
449  delete[] outputRange;
450 }
451 //_____________________________________________________________________________________
452 // Get/Set methods
453 
456  const G4Molecule* reactive2) const
457 {
458  if (fReactionData.empty())
459  {
460  G4String errMsg = "No reaction table was implemented";
461  G4Exception("G4MolecularInteractionTable::GetReactionData", "",
462  FatalErrorInArgument, errMsg);
463  return 0;
464  }
465 
466  ReactionDataMap::const_iterator it1 = fReactionData.find(reactive1);
467 
468  if (it1 == fReactionData.end())
469  {
470  G4String errMsg =
471  "No reaction table was implemented for this molecule Definition : " + reactive1
472  ->GetName();
473 // G4cout << "--- G4MolecularInteractionTable::GetReactionData ---" << G4endl;
474 // G4cout << errMsg << G4endl;
475  G4Exception("G4MolecularInteractionTable::GetReactionData", "",
476  FatalErrorInArgument, errMsg);
477 // return 0;
478  }
479 
480  std::map<const G4Molecule*, const G4DNAMolecularReactionData*, compMoleculeP>::const_iterator it2 =
481  it1->second.find(reactive2);
482 
483  if (it2 == it1->second.end())
484  {
485  G4cout << "Nom : " << reactive2->GetName() << G4endl;
486  G4String errMsg = "No reaction table was implemented for this molecule : "
487  + reactive2 -> GetName();
488  G4Exception("G4MolecularInteractionTable::GetReactionData","",FatalErrorInArgument, errMsg);
489  }
490 
491  return (it2->second);
492 }
493 
494 const std::vector<const G4Molecule*>*
496 {
497  if (fReactivesMV.empty())
498  {
499  G4String errMsg = "No reaction table was implemented";
500  G4Exception("G4MolecularInteractionTable::CanReactWith", "",
501  FatalErrorInArgument, errMsg);
502  return 0;
503  }
504 
505  ReactivesMV::const_iterator itReactivesMap = fReactivesMV.find(aMolecule);
506 
507  if (itReactivesMap == fReactivesMV.end())
508  {
509 #ifdef G4VERBOSE
510  if (fVerbose)
511  {
512  G4String errMsg = "No reaction table was implemented for this molecule : "
513  + aMolecule->GetName();
514 // G4Exception("G4MolecularInteractionTable::CanReactWith","",FatalErrorInArgument, errMsg);
515  G4cout << "--- G4MolecularInteractionTable::GetReactionData ---" << G4endl;
516  G4cout << errMsg << G4endl;
517  }
518 #endif
519  return 0;
520  }
521  else
522  {
523  if(fVerbose)
524  {
525  G4cout<< " G4MolecularInteractionTable::CanReactWith :"<<G4endl;
526  G4cout<<"You are checking reactants for : " << aMolecule->GetName()<<G4endl;
527  G4cout<<" the number of reactants is : " << itReactivesMap->second.size()<<G4endl;
528 
529  std::vector<const G4Molecule*>::const_iterator itProductsVector =
530  itReactivesMap->second.begin();
531 
532  for(; itProductsVector != itReactivesMap->second.end(); itProductsVector++)
533  {
534  G4cout<<(*itProductsVector)->GetName()<<G4endl;
535  }
536  }
537  return &(itReactivesMap->second);
538  }
539  return 0;
540 }
541 
542 //_____________________________________________________________________________________
543 const std::map<const G4Molecule*, const G4DNAMolecularReactionData*,
544  compMoleculeP>*
546 {
547 
548  if (fReactionData.empty())
549  {
550  G4String errMsg = "No reaction table was implemented";
551  G4Exception("G4MolecularInteractionTable::CanInteractWith", "",
552  FatalErrorInArgument, errMsg);
553  return 0;
554  }
555 
556  ReactionDataMap::const_iterator itReactivesMap = fReactionData.find(molecule);
557 
558  if (itReactivesMap == fReactionData.end())
559  {
560  G4cout << "Nom : " << molecule->GetName() << G4endl;
561  G4String errMsg = "No reaction table was implemented for this molecule Definition : "
562  + molecule -> GetName();
563  G4Exception("G4MolecularInteractionTable::CanReactWith","",FatalErrorInArgument, errMsg);
564  }
565  else
566  {
567  if(fVerbose)
568  {
569  G4cout<< " G4MolecularInteractionTable::CanReactWith :"<<G4endl;
570  G4cout<<"You are checking reactants for : " << molecule->GetName()<<G4endl;
571  G4cout<<" the number of reactants is : " << itReactivesMap->second.size()<<G4endl;
572 
573  std::map<const G4Molecule*,
575  compMoleculeP>::const_iterator itProductsVector =
576  itReactivesMap->second.begin();
577 
578  for(; itProductsVector != itReactivesMap->second.end(); itProductsVector++)
579  {
580  G4cout<<itProductsVector->first->GetName()<<G4endl;
581  }
582  }
583  return &(itReactivesMap->second);
584  }
585 
586  return 0;
587 }
588 
589 const std::vector<const G4DNAMolecularReactionData*>*
591 {
592  if (fReactionDataMV.empty())
593  {
594  G4String errMsg = "No reaction table was implemented";
595  G4Exception("G4MolecularInteractionTable::CanInteractWith", "",
596  FatalErrorInArgument, errMsg);
597  return 0;
598  }
599  ReactionDataMV::const_iterator it = fReactionDataMV.find(molecule);
600 
601  if (it == fReactionDataMV.end())
602  {
603  G4cout << "Nom : " << molecule->GetName() << G4endl;
604  G4String errMsg = "No reaction table was implemented for this molecule Definition : "
605  + molecule -> GetName();
606  G4Exception("G4MolecularInteractionTable::GetReactionData","",FatalErrorInArgument, errMsg);
607  return 0; // coverity
608  }
609 
610  return &(it->second);
611 }
G4VDNAReactionModel is an interface used by the G4DNAMolecularReaction process.
void AddProduct(const G4Molecule *molecule)
Free interface to define reaction information.
void SetReactive1(const G4Molecule *reactive)
static const double m3
Definition: G4SIunits.hh:112
static const double nanometer
Definition: G4SIunits.hh:91
const G4double pi
G4DNAMolecularReactionTable sorts out the G4DNAMolecularReactionData for bimolecular reaction...
void SetReactive(const G4Molecule *reactive1, const G4Molecule *reactive2)
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:371
G4double GetDiffusionCoefficient() const
Returns the diffusion coefficient D.
Definition: G4Molecule.cc:465
int G4int
Definition: G4Types.hh:78
static G4DNAMolecularReactionTable * GetReactionTable()
static const double s
Definition: G4SIunits.hh:150
const G4String & GetName() const
Returns the name of the molecule.
Definition: G4Molecule.cc:303
const G4Molecule * GetProduct(G4int i) const
G4GLOB_DLL std::ostream G4cout
void SetReactionTable(const G4DNAMolecularReactionTable *)
void SetReactive2(const G4Molecule *reactive)
const G4Molecule * GetReactive2() const
virtual void InitialiseToPrint(const G4Molecule *)=0
static G4MoleculeTable * Instance()
void PrintTable(G4VDNAReactionModel *=0)
const std::vector< const G4Molecule * > * CanReactWith(const G4Molecule *) const
Given a molecule's type, it returns with which a reaction is allowed.
#define TRUE
Definition: globals.hh:55
const G4int n
IosFlagSaver(std::ostream &_ios)
static G4DNAMolecularReactionTable * fInstance
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4DNAMolecularReactionData contains the information relative to a given reaction (eg : °OH + °OH -> H...
void SetReaction(G4double observedReactionRate, const G4Molecule *reactive1, const G4Molecule *reactive2)
Define a reaction : First argument : reaction rate Second argument : reactant 1 Third argument : reac...
G4Molecule * GetMoleculeModel(const G4String &, bool mustExist=true)
std::vector< const G4Molecule * > * fProducts
const G4DNAMolecularReactionData * GetReactionData(const G4Molecule *, const G4Molecule *) const
virtual G4double GetReactionRadius(const G4Molecule *, const G4Molecule *)=0
const std::map< const G4Molecule *, const G4DNAMolecularReactionData *, compMoleculeP > * GetReativesNData(const G4Molecule *) const
static const double mole
Definition: G4SIunits.hh:265
#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
const G4DNAMolecularReactionTable * GetReactionTable()
double G4double
Definition: G4Types.hh:76
const G4Molecule * GetReactive1() const