Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 79184 2014-02-20 09:14:38Z gcosmo $
27 //
29 #include "G4Material.hh"
30 #include <utility>
31 #include "G4StateManager.hh"
32 #include "G4Threading.hh"
33 #include "G4AutoLock.hh"
34 
35 using namespace std;
36 
38 //G4ThreadLocal G4DNAMolecularMaterial* G4DNAMolecularMaterial::fInstance(0);
40 
41 
42 bool CompareMaterial::operator() (const G4Material* mat1, const G4Material* mat2) const
43 {
44  if(mat1==0 && mat2==0) return false; //(mat1 == mat2)
45  if(mat1==0) return true; // mat1 < mat2
46  if(mat2==0) return false; //mat2 < mat1
47 
48  const G4Material* baseMat1 = mat1->GetBaseMaterial();
49  const G4Material* baseMat2 = mat2->GetBaseMaterial();
50 
51  if((baseMat1 || baseMat2) == 0) // None of the materials derives from a base material
52  {
53  return mat1 < mat2;
54  }
55  else if(baseMat1 && baseMat2) // Both materials derive from a base material
56  {
57  return baseMat1 < baseMat2;
58  }
59 
60  else if(baseMat1 && (baseMat2 == 0)) // Only the material 1 derives from a base material
61  {
62  return baseMat1 < mat2;
63  }
64  // only case baseMat1==0 && baseMat2 remains
65  return mat1 < baseMat2;
66 }
67 
69 {
70  if(! fInstance) new G4DNAMolecularMaterial();
71  return fInstance;
72 }
73 
75 {
76  delete fInstance;
77  fInstance = 0;
78 }
79 
81 {
82  fpCompFractionTable = 0;
83  fpCompDensityTable = 0;
84  fpCompNumMolPerVolTable = 0;
85  fIsInitialized = false;
86  fNMaterials = 0;
87  fInstance = this;
88 }
89 
91 {
92  Create();
93  fInstance = this;
94 }
95 
97 {
98  if(requestedState == G4State_Idle) Initialize();
99  return true;
100 }
101 
103 {
104  Create();
105 }
106 
108 {
109  if(this == &rhs) return *this;
110  Create();
111  return *this;
112 }
113 
115 {
117  {
118  fpCompFractionTable->clear();
119  delete fpCompFractionTable;
121  }
123  {
124  fpCompDensityTable->clear();
125  delete fpCompDensityTable;
126  fpCompDensityTable = 0;
127  }
129  {
130  fpCompNumMolPerVolTable->clear();
133  }
134 
135  std::map<const G4Material*,std::vector<double>*,CompareMaterial>::iterator it;
136 
137  for(it= fAskedDensityTable.begin() ; it != fAskedDensityTable.end() ;it++)
138  {
139  if(it->second)
140  {
141  delete it->second;
142  it->second = 0;
143  }
144  }
145 
146  for(it= fAskedNumPerVolTable.begin() ; it != fAskedNumPerVolTable.end() ;it++)
147  {
148  if(it->second)
149  {
150  delete it->second;
151  it->second = 0;
152  }
153  }
154 }
155 
156 
158 {
159  G4AutoLock l(&aMutex);
160  if(fIsInitialized)
161  {
162  return;
163  }
164 
165  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
166 
167  fNMaterials = materialTable->size();
168  // This is to prevent segment fault if materials are created later on
169  // Actually this creation should not be done
170 
171  if(fpCompFractionTable==0)
172  {
173  fpCompFractionTable = new vector<ComponentMap>(materialTable->size());
174  }
175 
176  G4Material* mat(0);
177 
178  for(size_t i = 0 ; i < fNMaterials ; i++)
179  {
180  mat = materialTable->at(i);
181  SearchMolecularMaterial(mat,mat,1);
182 
183  mat = 0;
184  }
185 
188 
189  fIsInitialized = true;
190 }
191 
193 {
195  {
196  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
197  fpCompDensityTable = new vector<ComponentMap>(G4Material::GetMaterialTable()->size());
198 
199  G4Material* parentMat;
200  const G4Material* compMat(0);
201  double massFraction = -1;
202  double parentDensity = -1;
203 
204  for(size_t i = 0 ; i < fNMaterials ; i++)
205  {
206  parentMat = materialTable->at(i);
207  ComponentMap& massFractionComp = (*fpCompFractionTable)[i];
208  ComponentMap& densityComp = (*fpCompDensityTable)[i];
209 
210  parentDensity = parentMat->GetDensity();
211 
212  for(ComponentMap::iterator it = massFractionComp.begin() ; it!=massFractionComp.end() ; it++)
213  {
214  compMat = it->first;
215  massFraction = it->second;
216  densityComp[compMat] = massFraction*parentDensity;
217  compMat = 0;
218  massFraction = -1;
219  }
220  }
221  }
222  else
223  {
224  G4ExceptionDescription exceptionDescription;
225  exceptionDescription << "The pointer fpCompFractionTable is not initialized" << G4endl;
226  G4Exception("G4DNAMolecularMaterial::InitializeDensity","G4DNAMolecularMaterial001",
227  FatalException,exceptionDescription);
228  }
229 }
230 
232 {
234  {
235  fpCompNumMolPerVolTable = new vector<ComponentMap>(fNMaterials);
236 
237  const G4Material* compMat(0);
238 
239  for(size_t i = 0 ; i < fNMaterials ; i++)
240  {
241  ComponentMap& massFractionComp = (*fpCompFractionTable)[i];
242  ComponentMap& densityComp = (*fpCompDensityTable)[i];
243  ComponentMap& numMolPerVol = (*fpCompNumMolPerVolTable)[i];
244 
245  for(ComponentMap::iterator it = massFractionComp.begin() ; it!=massFractionComp.end() ; it++)
246  {
247  compMat = it->first;
248  numMolPerVol[compMat] = densityComp[compMat]/ compMat->GetMassOfMolecule();
249  compMat = 0;
250  }
251  }
252  }
253  else
254  {
255  G4ExceptionDescription exceptionDescription;
256  exceptionDescription << "The pointer fpCompDensityTable is not initialized" << G4endl;
257  G4Exception("G4DNAMolecularMaterial::InitializeNumMolPerVol","G4DNAMolecularMaterial002",
258  FatalException,exceptionDescription);
259  }
260 }
261 
262 void G4DNAMolecularMaterial::RecordMolecularMaterial(G4Material* parentMaterial, G4Material* molecularMaterial, G4double fraction)
263 {
264  ComponentMap& matComponent = (*fpCompFractionTable)[parentMaterial->GetIndex()];
265 
266  if(matComponent.empty())
267  {
268  matComponent[molecularMaterial] = fraction;
269  return;
270  }
271 
272  ComponentMap::iterator it = matComponent.find(molecularMaterial);
273 
274  if(it == matComponent.end())
275  {
276  matComponent[molecularMaterial] = fraction;
277  }
278  else
279  {
280  matComponent[molecularMaterial] = it->second + fraction;
281  }
282 }
283 
284 void G4DNAMolecularMaterial::SearchMolecularMaterial(G4Material* parentMaterial, G4Material* material, double currentFraction)
285 {
286  if(material->GetMassOfMolecule() != 0.0)
287  {
288  RecordMolecularMaterial(parentMaterial,material,currentFraction);
289  return;
290  }
291 
292  G4Material* compMat(0);
293  G4double fraction = -1;
294  std::map<G4Material*,G4double> matComponent = material->GetMatComponents();
295  std::map<G4Material*,G4double>::iterator it = matComponent.begin();
296 
297  for( ; it!=matComponent.end() ; it++)
298  {
299  compMat = it->first;
300  fraction = it->second;
301  if(compMat->GetMassOfMolecule() == 0.0)
302  {
303  SearchMolecularMaterial(parentMaterial,compMat,currentFraction*fraction);
304  }
305  else
306  {
307  RecordMolecularMaterial(parentMaterial,compMat,currentFraction*fraction);
308  }
309 
310  compMat = 0;
311  fraction = -1;
312  }
313 }
314 
315 
316 const std::vector<double>* G4DNAMolecularMaterial::GetDensityTableFor(const G4Material* lookForMaterial) const
317 {
318  if(!fpCompDensityTable)
319  {
320  if(fIsInitialized)
321  {
322  G4ExceptionDescription exceptionDescription;
323  exceptionDescription << "The pointer fpCompDensityTable is not initialized will the singleton of G4DNAMolecularMaterial "
324  << "has already been initialized."<< G4endl;
325  G4Exception("G4DNAMolecularMaterial::GetDensityTableFor","G4DNAMolecularMaterial003",
326  FatalException,exceptionDescription);
327  }
328 
329  if(G4StateManager::GetStateManager()->GetCurrentState() == G4State_Idle)
330  const_cast<G4DNAMolecularMaterial*>(this)->Initialize();
331  else
332  {
333  G4ExceptionDescription exceptionDescription;
334  exceptionDescription << "The geant4 application is at the wrong state. State must be: G4State_Idle."<< G4endl;
335  G4Exception("G4DNAMolecularMaterial::GetDensityTableFor",
336  "G4DNAMolecularMaterial_WRONG_STATE_APPLICATION",FatalException,exceptionDescription);
337  }
338  }
339 
340  std::map<const G4Material*,std::vector<double>*,CompareMaterial>::const_iterator it_askedDensityTable = fAskedDensityTable.find(lookForMaterial);
341  if(it_askedDensityTable != fAskedDensityTable.end())
342  {
343  return it_askedDensityTable->second;
344  }
345 
346  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
347 
348  std::vector<double>* output = new std::vector<double>(materialTable->size());
349 
350  ComponentMap::const_iterator it;
351 
352  G4bool materialWasNotFound = true;
353 
354  for(size_t i = 0 ; i < fNMaterials ; i++)
355  {
356  ComponentMap& densityTable = (*fpCompDensityTable)[i];
357 
358  it = densityTable.find(lookForMaterial);
359 
360  if(it==densityTable.end())
361  {
362  (*output)[i] = 0.0;
363  }
364  else
365  {
366  materialWasNotFound = false;
367  (*output)[i] = it->second;
368  }
369  }
370 
371  if(materialWasNotFound)
372  {
373  PrintNotAMolecularMaterial("G4DNAMolecularMaterial::GetDensityTableFor",lookForMaterial);
374  }
375 
376  fAskedDensityTable.insert(make_pair(lookForMaterial, output));
377 
378  return output;
379 }
380 
381 const std::vector<double>* G4DNAMolecularMaterial::GetNumMolPerVolTableFor(const G4Material* lookForMaterial) const
382 {
384  {
385  if(fIsInitialized)
386  {
387  G4ExceptionDescription exceptionDescription;
388  exceptionDescription << "The pointer fpCompNumMolPerVolTable is not initialized whereas the singleton of G4DNAMolecularMaterial "
389  << "has already been initialized."<< G4endl;
390  G4Exception("G4DNAMolecularMaterial::GetNumMolPerVolTableFor","G4DNAMolecularMaterial005",
391  FatalException,exceptionDescription);
392  }
393 
394  if(G4StateManager::GetStateManager()->GetCurrentState() == G4State_Idle)
395  {
396  const_cast<G4DNAMolecularMaterial*>(this)->Initialize();
397  }
398  else
399  {
400  G4ExceptionDescription exceptionDescription;
401  exceptionDescription << "The geant4 application is at the wrong state. State must be : G4State_Idle."<< G4endl;
402  G4Exception("G4DNAMolecularMaterial::GetNumMolPerVolTableFor",
403  "G4DNAMolecularMaterial_WRONG_STATE_APPLICATION",FatalException,exceptionDescription);
404  }
405  }
406 
407  std::map<const G4Material*,std::vector<double>*,CompareMaterial>::const_iterator it_askedNumMolPerVolTable = fAskedNumPerVolTable.find(lookForMaterial);
408  if(it_askedNumMolPerVolTable != fAskedNumPerVolTable.end())
409  {
410  return it_askedNumMolPerVolTable->second;
411  }
412 
413  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
414 
415  std::vector<double>* output = new std::vector<double>(materialTable->size());
416 
417  ComponentMap::const_iterator it;
418 
419  G4bool materialWasNotFound = true;
420 
421  for(size_t i = 0 ; i < fNMaterials ; i++)
422  {
423  ComponentMap& densityTable = (*fpCompNumMolPerVolTable)[i];
424 
425  it = densityTable.find(lookForMaterial);
426 
427  if(it==densityTable.end())
428  {
429  (*output)[i] = 0.0;
430  }
431  else
432  {
433  materialWasNotFound = false;
434  (*output)[i] = it->second;
435  }
436  }
437 
438  if(materialWasNotFound)
439  {
440  PrintNotAMolecularMaterial("G4DNAMolecularMaterial::GetNumMolPerVolTableFor",lookForMaterial);
441  }
442 
443  fAskedNumPerVolTable.insert(make_pair(lookForMaterial, output));
444 
445  return output;
446 }
447 
448 void G4DNAMolecularMaterial::PrintNotAMolecularMaterial(const char* methodName, const G4Material* lookForMaterial) const
449 {
450  std::map<const G4Material*,bool,CompareMaterial>::iterator it = fWarningPrinted.find(lookForMaterial);
451 
452  if(it == fWarningPrinted.end())
453  {
454  G4ExceptionDescription exceptionDescription;
455  exceptionDescription
456  << "The material " << lookForMaterial->GetName()
457  << " is not defined as a molecular material."<< G4endl
458  << "Meaning: The elements should be added to the material using atom count rather than mass fraction (cf. G4Material)"
459  << G4endl
460  << "If you want to use DNA processes on liquid water, you should better use the NistManager to create the water material."
461  << G4endl
462  << "Since this message is displayed, it means that the DNA models will not be called."
463  << "Please note that this message will only appear once even if you are using other methods of G4DNAMolecularMaterial."
464  << G4endl;
465 
466  G4Exception(methodName,"MATERIAL_NOT_DEFINE_USING_ATOM_COUNT",JustWarning,exceptionDescription);
467  fWarningPrinted[lookForMaterial] = true;
468  }
469 }
const std::vector< double > * GetDensityTableFor(const G4Material *) const
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
const G4String & GetName() const
Definition: G4Material.hh:176
void RecordMolecularMaterial(G4Material *parentMaterial, G4Material *molecularMaterial, G4double fraction)
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:564
std::vector< G4Material * > G4MaterialTable
std::map< const G4Material *, double, CompareMaterial > ComponentMap
bool operator()(const G4Material *mat1, const G4Material *mat2) const
void PrintNotAMolecularMaterial(const char *methodName, const G4Material *lookForMaterial) const
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:158
std::vector< ComponentMap > * fpCompNumMolPerVolTable
string material
Definition: eplot.py:19
static G4DNAMolecularMaterial * fInstance
static G4StateManager * GetStateManager()
std::map< const G4Material *, std::vector< double > *, CompareMaterial > fAskedNumPerVolTable
const std::vector< double > * GetNumMolPerVolTableFor(const G4Material *) const
std::map< const G4Material *, std::vector< double > *, CompareMaterial > fAskedDensityTable
bool G4bool
Definition: G4Types.hh:79
G4DNAMolecularMaterial & operator=(const G4DNAMolecularMaterial &)
G4Mutex aMutex
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
std::map< const G4Material *, bool, CompareMaterial > fWarningPrinted
G4int G4Mutex
Definition: G4Threading.hh:156
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