Geant4  10.01
G4DNAMolecularMaterial.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: G4DNAMolecularMaterial.cc 84858 2014-10-21 16:08:22Z gcosmo $
27 //
29 #include "G4Material.hh"
30 #include <utility>
31 #include "G4StateManager.hh"
32 #include "G4Threading.hh"
33 #include "G4AutoLock.hh"
34 #include "G4StateManager.hh"
35 
36 using namespace std;
37 
39 //G4ThreadLocal G4DNAMolecularMaterial* G4DNAMolecularMaterial::fInstance(0);
41 
43  const G4Material* mat2) const
44 {
45  if (mat1 == 0 && mat2 == 0) return false; //(mat1 == mat2)
46  if (mat1 == 0) return true; // mat1 < mat2
47  if (mat2 == 0) return false; //mat2 < mat1
48 
49  const G4Material* baseMat1 = mat1->GetBaseMaterial();
50  const G4Material* baseMat2 = mat2->GetBaseMaterial();
51 
52  if ((baseMat1 || baseMat2) == 0) // None of the materials derives from a base material
53  {
54  return mat1 < mat2;
55  }
56  else if (baseMat1 && baseMat2) // Both materials derive from a base material
57  {
58  return baseMat1 < baseMat2;
59  }
60 
61  else if (baseMat1 && (baseMat2 == 0)) // Only the material 1 derives from a base material
62  {
63  return baseMat1 < mat2;
64  }
65  // only case baseMat1==0 && baseMat2 remains
66  return mat1 < baseMat2;
67 }
68 
70 {
71  if (!fInstance) new G4DNAMolecularMaterial();
72  return fInstance;
73 }
74 
76 {
77  if (fInstance)
78  {
79  delete fInstance;
80  fInstance = 0;
81  }
82 }
83 
85 {
86  fpCompFractionTable = 0;
87  fpCompDensityTable = 0;
88  fpCompNumMolPerVolTable = 0;
89  fIsInitialized = false;
90  fNMaterials = 0;
91  fInstance = this;
92 }
93 
95 {
96  if (fpCompFractionTable)
97  {
98  fpCompFractionTable->clear();
99  delete fpCompFractionTable;
100  fpCompFractionTable = 0;
101  }
102  if (fpCompDensityTable)
103  {
104  fpCompDensityTable->clear();
105  delete fpCompDensityTable;
106  fpCompDensityTable = 0;
107  }
108  if (fpCompNumMolPerVolTable)
109  {
110  fpCompNumMolPerVolTable->clear();
111  delete fpCompNumMolPerVolTable;
112  fpCompNumMolPerVolTable = 0;
113  }
114 
115  map<const G4Material*, std::vector<double>*, CompareMaterial>::iterator it;
116 
117  for (it = fAskedDensityTable.begin(); it != fAskedDensityTable.end(); it++)
118  {
119  if (it->second)
120  {
121  delete it->second;
122  it->second = 0;
123  }
124  }
125 
126  for (it = fAskedNumPerVolTable.begin(); it != fAskedNumPerVolTable.end();
127  it++)
128  {
129  if (it->second)
130  {
131  delete it->second;
132  it->second = 0;
133  }
134  }
135 }
136 
139 {
140  Create();
141  fInstance = this;
142 }
143 
145 {
146  if (requestedState == G4State_Idle && G4StateManager::GetStateManager()
147  ->GetPreviousState() == G4State_PreInit)
148  {
149  Initialize();
150  }
151  else if (requestedState == G4State_Quit)
152  {
153 // G4cout << "G4DNAMolecularMaterial::Notify ---> received G4State_Quit"
154 // << G4endl;
155  Clear();
156  //DeleteInstance();
157  }
158  return true;
159 }
160 
162  const G4DNAMolecularMaterial& /*rhs*/) :
164 {
165  Create();
166 }
167 
170 {
171  if (this == &rhs) return *this;
172  Create();
173  return *this;
174 }
175 
177 {
178 // G4cout << "Deleting G4DNAMolecularMaterial" << G4endl;
179  Clear();
180  fInstance = 0;
181  //assert(G4StateManager::GetStateManager()->DeregisterDependent(this) == true);
182 }
183 
185 {
186  G4AutoLock l(&aMutex);
187  if (fIsInitialized)
188  {
189  return;
190  }
191 
192  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
193 
194  fNMaterials = materialTable->size();
195  // This is to prevent segment fault if materials are created later on
196  // Actually this creation should not be done
197 
198  if (fpCompFractionTable == 0)
199  {
200  fpCompFractionTable = new vector<ComponentMap>(materialTable->size());
201  }
202 
203  G4Material* mat(0);
204 
205  for (size_t i = 0; i < fNMaterials; i++)
206  {
207  mat = materialTable->at(i);
208  SearchMolecularMaterial(mat, mat, 1);
209 
210  mat = 0;
211  }
212 
215 
216  fIsInitialized = true;
217 }
218 
220 {
222  {
223  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
224  fpCompDensityTable = new vector<ComponentMap>(
225  G4Material::GetMaterialTable()->size());
226 
227  G4Material* parentMat;
228  const G4Material* compMat(0);
229  double massFraction = -1;
230  double parentDensity = -1;
231 
232  for (size_t i = 0; i < fNMaterials; i++)
233  {
234  parentMat = materialTable->at(i);
235  ComponentMap& massFractionComp = (*fpCompFractionTable)[i];
236  ComponentMap& densityComp = (*fpCompDensityTable)[i];
237 
238  parentDensity = parentMat->GetDensity();
239 
240  for (ComponentMap::iterator it = massFractionComp.begin();
241  it != massFractionComp.end(); it++)
242  {
243  compMat = it->first;
244  massFraction = it->second;
245  densityComp[compMat] = massFraction * parentDensity;
246  compMat = 0;
247  massFraction = -1;
248  }
249  }
250  }
251  else
252  {
253  G4ExceptionDescription exceptionDescription;
254  exceptionDescription << "The pointer fpCompFractionTable is not initialized"
255  << G4endl;
256  G4Exception("G4DNAMolecularMaterial::InitializeDensity",
257  "G4DNAMolecularMaterial001", FatalException,
258  exceptionDescription);
259  }
260 }
261 
263 {
264  if (fpCompDensityTable)
265  {
266  fpCompNumMolPerVolTable = new vector<ComponentMap>(fNMaterials);
267 
268  const G4Material* compMat(0);
269 
270  for (size_t i = 0; i < fNMaterials; i++)
271  {
272  ComponentMap& massFractionComp = (*fpCompFractionTable)[i];
273  ComponentMap& densityComp = (*fpCompDensityTable)[i];
274  ComponentMap& numMolPerVol = (*fpCompNumMolPerVolTable)[i];
275 
276  for (ComponentMap::iterator it = massFractionComp.begin();
277  it != massFractionComp.end(); it++)
278  {
279  compMat = it->first;
280  numMolPerVol[compMat] = densityComp[compMat]
281  / compMat->GetMassOfMolecule();
282  compMat = 0;
283  }
284  }
285  }
286  else
287  {
288  G4ExceptionDescription exceptionDescription;
289  exceptionDescription << "The pointer fpCompDensityTable is not initialized"
290  << G4endl;
291  G4Exception("G4DNAMolecularMaterial::InitializeNumMolPerVol",
292  "G4DNAMolecularMaterial002", FatalException,
293  exceptionDescription);
294  }
295 }
296 
298  G4Material* molecularMaterial,
299  G4double fraction)
300 {
301  ComponentMap& matComponent =
302  (*fpCompFractionTable)[parentMaterial->GetIndex()];
303 
304  if (matComponent.empty())
305  {
306  matComponent[molecularMaterial] = fraction;
307  return;
308  }
309 
310  ComponentMap::iterator it = matComponent.find(molecularMaterial);
311 
312  if (it == matComponent.end())
313  {
314  matComponent[molecularMaterial] = fraction;
315  }
316  else
317  {
318  matComponent[molecularMaterial] = it->second + fraction;
319  }
320 }
321 
323  G4Material* material,
324  double currentFraction)
325 {
326  if (material->GetMassOfMolecule() != 0.0)
327  {
328  RecordMolecularMaterial(parentMaterial, material, currentFraction);
329  return;
330  }
331 
332  G4Material* compMat(0);
333  G4double fraction = -1;
334  std::map<G4Material*, G4double> matComponent = material->GetMatComponents();
335  std::map<G4Material*, G4double>::iterator it = matComponent.begin();
336 
337  for (; it != matComponent.end(); it++)
338  {
339  compMat = it->first;
340  fraction = it->second;
341  if (compMat->GetMassOfMolecule() == 0.0)
342  {
343  SearchMolecularMaterial(parentMaterial, compMat,
344  currentFraction * fraction);
345  }
346  else
347  {
348  RecordMolecularMaterial(parentMaterial, compMat,
349  currentFraction * fraction);
350  }
351 
352  compMat = 0;
353  fraction = -1;
354  }
355 }
356 
357 const std::vector<double>*
359 GetDensityTableFor(const G4Material* lookForMaterial) const
360 {
361  if (!fpCompDensityTable)
362  {
363  if (fIsInitialized)
364  {
365  G4ExceptionDescription exceptionDescription;
366  exceptionDescription
367  << "The pointer fpCompDensityTable is not initialized will the "
368  "singleton of G4DNAMolecularMaterial "
369  << "has already been initialized." << G4endl;
370  G4Exception("G4DNAMolecularMaterial::GetDensityTableFor",
371  "G4DNAMolecularMaterial003", FatalException,
372  exceptionDescription);
373  }
374 
375  if (G4StateManager::GetStateManager()->GetCurrentState() == G4State_Idle)
376  {
377  const_cast<G4DNAMolecularMaterial*>(this)->Initialize();
378  }
379  else
380  {
381  G4ExceptionDescription exceptionDescription;
382  exceptionDescription
383  << "The geant4 application is at the wrong state. State must be: "
384  "G4State_Idle."
385  << G4endl;
386  G4Exception("G4DNAMolecularMaterial::GetDensityTableFor",
387  "G4DNAMolecularMaterial_WRONG_STATE_APPLICATION",
388  FatalException, exceptionDescription);
389  }
390  }
391 
392  std::map<const G4Material*, std::vector<double>*, CompareMaterial>::
393  const_iterator it_askedDensityTable =
394  fAskedDensityTable.find(lookForMaterial);
395 
396  if (it_askedDensityTable != fAskedDensityTable.end())
397  {
398  return it_askedDensityTable->second;
399  }
400 
401  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
402 
403  std::vector<double>* output = new std::vector<double>(materialTable->size());
404 
405  ComponentMap::const_iterator it;
406 
407  G4bool materialWasNotFound = true;
408 
409  for (size_t i = 0; i < fNMaterials; i++)
410  {
411  ComponentMap& densityTable = (*fpCompDensityTable)[i];
412 
413  it = densityTable.find(lookForMaterial);
414 
415  if (it == densityTable.end())
416  {
417  (*output)[i] = 0.0;
418  }
419  else
420  {
421  materialWasNotFound = false;
422  (*output)[i] = it->second;
423  }
424  }
425 
426  if (materialWasNotFound)
427  {
428  PrintNotAMolecularMaterial("G4DNAMolecularMaterial::GetDensityTableFor",
429  lookForMaterial);
430  }
431 
432  fAskedDensityTable.insert(make_pair(lookForMaterial, output));
433 
434  return output;
435 }
436 
438  const G4Material* lookForMaterial) const
439 {
441  {
442  if (fIsInitialized)
443  {
444  G4ExceptionDescription exceptionDescription;
445  exceptionDescription
446  << "The pointer fpCompNumMolPerVolTable is not initialized whereas "
447  "the singleton of G4DNAMolecularMaterial "
448  << "has already been initialized." << G4endl;
449  G4Exception("G4DNAMolecularMaterial::GetNumMolPerVolTableFor",
450  "G4DNAMolecularMaterial005", FatalException,
451  exceptionDescription);
452  }
453 
454  if (G4StateManager::GetStateManager()->GetCurrentState() == G4State_Idle)
455  {
456  const_cast<G4DNAMolecularMaterial*>(this)->Initialize();
457  }
458  else
459  {
460  G4ExceptionDescription exceptionDescription;
461  exceptionDescription
462  << "The geant4 application is at the wrong state. State must be : "
463  "G4State_Idle."
464  << G4endl;
465  G4Exception("G4DNAMolecularMaterial::GetNumMolPerVolTableFor",
466  "G4DNAMolecularMaterial_WRONG_STATE_APPLICATION",
467  FatalException, exceptionDescription);
468  }
469  }
470 
471  std::map<const G4Material*, std::vector<double>*, CompareMaterial>::
472  const_iterator it_askedNumMolPerVolTable =
473  fAskedNumPerVolTable.find(lookForMaterial);
474  if (it_askedNumMolPerVolTable != fAskedNumPerVolTable.end())
475  {
476  return it_askedNumMolPerVolTable->second;
477  }
478 
479  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
480 
481  std::vector<double>* output = new std::vector<double>(materialTable->size());
482 
483  ComponentMap::const_iterator it;
484 
485  G4bool materialWasNotFound = true;
486 
487  for (size_t i = 0; i < fNMaterials; i++)
488  {
489  ComponentMap& densityTable = (*fpCompNumMolPerVolTable)[i];
490 
491  it = densityTable.find(lookForMaterial);
492 
493  if (it == densityTable.end())
494  {
495  (*output)[i] = 0.0;
496  }
497  else
498  {
499  materialWasNotFound = false;
500  (*output)[i] = it->second;
501  }
502  }
503 
504  if (materialWasNotFound)
505  {
507  "G4DNAMolecularMaterial::GetNumMolPerVolTableFor", lookForMaterial);
508  }
509 
510  fAskedNumPerVolTable.insert(make_pair(lookForMaterial, output));
511 
512  return output;
513 }
514 
516 PrintNotAMolecularMaterial(const char* methodName,
517  const G4Material* lookForMaterial) const
518 {
519  std::map<const G4Material*, bool, CompareMaterial>::iterator it =
520  fWarningPrinted.find(lookForMaterial);
521 
522  if (it == fWarningPrinted.end())
523  {
524  G4ExceptionDescription exceptionDescription;
525  exceptionDescription << "The material " << lookForMaterial->GetName()
526  << " is not defined as a molecular material."
527  << G4endl
528  << "Meaning: The elements should be added to the "
529  "material using atom count rather than mass fraction "
530  "(cf. G4Material)"
531  << G4endl
532  << "If you want to use DNA processes on liquid water, you should better use "
533  "the NistManager to create the water material."
534  << G4endl
535  << "Since this message is displayed, it means that the DNA models will not "
536  "be called."
537  << "Please note that this message will only appear once even if you are "
538  "using other methods of G4DNAMolecularMaterial."
539  << G4endl;
540 
541  G4Exception(methodName, "MATERIAL_NOT_DEFINE_USING_ATOM_COUNT", JustWarning,
542  exceptionDescription);
543  fWarningPrinted[lookForMaterial] = true;
544  }
545 }
const std::vector< double > * GetDensityTableFor(const G4Material *) const
std::map< const G4Material *, double, CompareMaterial > ComponentMap
void SearchMolecularMaterial(G4Material *parentMaterial, G4Material *material, double currentFraction)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
virtual G4bool Notify(G4ApplicationState requestedState)
size_t GetIndex() const
Definition: G4Material.hh:260
std::map< const G4Material *, std::vector< double > *, CompareMaterial > fAskedNumPerVolTable
const G4String & GetName() const
Definition: G4Material.hh:176
void RecordMolecularMaterial(G4Material *parentMaterial, G4Material *molecularMaterial, G4double fraction)
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:588
std::vector< G4Material * > G4MaterialTable
bool operator()(const G4Material *mat1, const G4Material *mat2) const
void PrintNotAMolecularMaterial(const char *methodName, const G4Material *lookForMaterial) const
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:163
std::vector< ComponentMap > * fpCompNumMolPerVolTable
std::map< const G4Material *, std::vector< double > *, CompareMaterial > fAskedDensityTable
static G4DNAMolecularMaterial * fInstance
static G4StateManager * GetStateManager()
const std::vector< double > * GetNumMolPerVolTableFor(const G4Material *) const
bool G4bool
Definition: G4Types.hh:79
G4DNAMolecularMaterial & operator=(const G4DNAMolecularMaterial &)
G4Mutex aMutex
std::map< const G4Material *, bool, CompareMaterial > fWarningPrinted
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:161
static G4DNAMolecularMaterial * Instance()
std::vector< ComponentMap > * fpCompFractionTable
G4double GetMassOfMolecule() const
Definition: G4Material.hh:240
const G4Material * GetBaseMaterial() const
Definition: G4Material.hh:231
std::vector< ComponentMap > * fpCompDensityTable
std::map< G4Material *, G4double > GetMatComponents() const
Definition: G4Material.hh:235
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4ApplicationState