Geant4_10
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 70171 2013-05-24 13:34:18Z 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  fInstance = this;
87 }
88 
90 {
91  Create();
92  fInstance = this;
93 }
94 
96 {
97  if(requestedState == G4State_Idle) Initialize();
98  return true;
99 }
100 
102 {
103  Create();
104 }
105 
107 {
108  if(this == &rhs) return *this;
109  Create();
110  return *this;
111 }
112 
114 {
116  {
117  fpCompFractionTable->clear();
118  delete fpCompFractionTable;
120  }
122  {
123  fpCompDensityTable->clear();
124  delete fpCompDensityTable;
125  fpCompDensityTable = 0;
126  }
128  {
129  fpCompNumMolPerVolTable->clear();
132  }
133 
134  std::map<const G4Material*,std::vector<double>*,CompareMaterial>::iterator it;
135 
136  for(it= fAskedDensityTable.begin() ; it != fAskedDensityTable.end() ;it++)
137  {
138  if(it->second)
139  {
140  delete it->second;
141  it->second = 0;
142  }
143  }
144 
145  for(it= fAskedNumPerVolTable.begin() ; it != fAskedNumPerVolTable.end() ;it++)
146  {
147  if(it->second)
148  {
149  delete it->second;
150  it->second = 0;
151  }
152  }
153 }
154 
155 void G4DNAMolecularMaterial::RecordMolecularMaterial(G4Material* parentMaterial, G4Material* molecularMaterial, G4double fraction)
156 {
157  ComponentMap& matComponent = (*fpCompFractionTable)[parentMaterial->GetIndex()];
158 
159  if(matComponent.empty())
160  {
161  matComponent[molecularMaterial] = fraction;
162  return;
163  }
164 
165  ComponentMap::iterator it = matComponent.find(molecularMaterial);
166 
167  if(it == matComponent.end())
168  {
169  matComponent[molecularMaterial] = fraction;
170  }
171  else
172  {
173  matComponent[molecularMaterial] = it->second + fraction;
174  }
175 }
176 
177 void G4DNAMolecularMaterial::SearchMolecularMaterial(G4Material* parentMaterial, G4Material* material, double currentFraction)
178 {
179  if(material->GetMassOfMolecule() != 0.0)
180  {
181  RecordMolecularMaterial(parentMaterial,material,currentFraction);
182  return;
183  }
184 
185  G4Material* compMat(0);
186  G4double fraction = -1;
187  std::map<G4Material*,G4double> matComponent = material->GetMatComponents();
188  std::map<G4Material*,G4double>::iterator it = matComponent.begin();
189 
190  for( ; it!=matComponent.end() ; it++)
191  {
192  compMat = it->first;
193  fraction = it->second;
194  if(compMat->GetMassOfMolecule() == 0.0)
195  {
196  SearchMolecularMaterial(parentMaterial,compMat,currentFraction*fraction);
197  }
198  else
199  {
200  RecordMolecularMaterial(parentMaterial,compMat,currentFraction*fraction);
201  }
202 
203  compMat = 0;
204  fraction = -1;
205  }
206 }
207 
209 {
211  {
212  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
213  fpCompDensityTable = new vector<ComponentMap>(G4Material::GetMaterialTable()->size());
214 
215  G4Material* parentMat;
216  const G4Material* compMat(0);
217  double massFraction = -1;
218  double parentDensity = -1;
219 
220  for(int i = 0 ; i < int(materialTable->size()) ; i++)
221  {
222  parentMat = materialTable->at(i);
223  ComponentMap& massFractionComp = (*fpCompFractionTable)[i];
224  ComponentMap& densityComp = (*fpCompDensityTable)[i];
225 
226  parentDensity = parentMat->GetDensity();
227 
228  for(ComponentMap::iterator it = massFractionComp.begin() ; it!=massFractionComp.end() ; it++)
229  {
230  compMat = it->first;
231  massFraction = it->second;
232  densityComp[compMat] = massFraction*parentDensity;
233  compMat = 0;
234  massFraction = -1;
235  }
236  }
237  }
238  else
239  {
240  G4ExceptionDescription exceptionDescription;
241  exceptionDescription << "The pointer fpCompFractionTable is not initialized" << G4endl;
242  G4Exception("G4DNAMolecularMaterial::InitializeDensity","G4DNAMolecularMaterial001",
243  FatalException,exceptionDescription);
244  }
245 }
246 
248 {
250  {
251  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
252  fpCompNumMolPerVolTable = new vector<ComponentMap>(G4Material::GetMaterialTable()->size());
253 
254  const G4Material* compMat(0);
255 
256  for(int i = 0 ; i < int(materialTable->size()) ; i++)
257  {
258  ComponentMap& massFractionComp = (*fpCompFractionTable)[i];
259  ComponentMap& densityComp = (*fpCompDensityTable)[i];
260  ComponentMap& numMolPerVol = (*fpCompNumMolPerVolTable)[i];
261 
262  for(ComponentMap::iterator it = massFractionComp.begin() ; it!=massFractionComp.end() ; it++)
263  {
264  compMat = it->first;
265  numMolPerVol[compMat] = densityComp[compMat]/ compMat->GetMassOfMolecule();
266  compMat = 0;
267  }
268  }
269  }
270  else
271  {
272  G4ExceptionDescription exceptionDescription;
273  exceptionDescription << "The pointer fpCompDensityTable is not initialized" << G4endl;
274  G4Exception("G4DNAMolecularMaterial::InitializeNumMolPerVol","G4DNAMolecularMaterial002",
275  FatalException,exceptionDescription);
276  }
277 }
278 
280 {
281  G4AutoLock l(&aMutex);
282  if(fIsInitialized)
283  {
284  return;
285  }
286 
287  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
288 
289  if(fpCompFractionTable==0)
290  {
291  fpCompFractionTable = new vector<ComponentMap>(materialTable->size());
292  }
293 
294  G4Material* mat(0);
295 
296  for(int i = 0 ; i < int(materialTable->size()) ; i++)
297  {
298  mat = materialTable->at(i);
299  SearchMolecularMaterial(mat,mat,1);
300 
301  mat = 0;
302  }
303 
306 
307  fIsInitialized = true;
308 }
309 
310 const std::vector<double>* G4DNAMolecularMaterial::GetDensityTableFor(const G4Material* lookForMaterial) const
311 {
312  if(!fpCompDensityTable)
313  {
314  if(fIsInitialized)
315  {
316  G4ExceptionDescription exceptionDescription;
317  exceptionDescription << "The pointer fpCompDensityTable is not initialized will the singleton of G4DNAMolecularMaterial "
318  << "has already been initialized."<< G4endl;
319  G4Exception("G4DNAMolecularMaterial::GetDensityTableFor","G4DNAMolecularMaterial003",
320  FatalException,exceptionDescription);
321  }
322 
323  if(G4StateManager::GetStateManager()->GetCurrentState() == G4State_Idle)
324  const_cast<G4DNAMolecularMaterial*>(this)->Initialize();
325  else
326  {
327  G4ExceptionDescription exceptionDescription;
328  exceptionDescription << "The geant4 application is at the wrong state. State must be: G4State_Idle."<< G4endl;
329  G4Exception("G4DNAMolecularMaterial::GetDensityTableFor",
330  "G4DNAMolecularMaterial_WRONG_STATE_APPLICATION",FatalException,exceptionDescription);
331  }
332  }
333 
334  std::map<const G4Material*,std::vector<double>*,CompareMaterial>::const_iterator it_askedDensityTable = fAskedDensityTable.find(lookForMaterial);
335  if(it_askedDensityTable != fAskedDensityTable.end())
336  {
337  return it_askedDensityTable->second;
338  }
339 
340  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
341 
342  std::vector<double>* output = new std::vector<double>(materialTable->size());
343 
344  ComponentMap::const_iterator it;
345 
346  G4bool materialWasNotFound = true;
347 
348  for(int i = 0 ; i < int(materialTable->size()) ; i++)
349  {
350  ComponentMap& densityTable = (*fpCompDensityTable)[i];
351 
352  it = densityTable.find(lookForMaterial);
353 
354  if(it==densityTable.end())
355  {
356  (*output)[i] = 0.0;
357  }
358  else
359  {
360  materialWasNotFound = false;
361  (*output)[i] = it->second;
362  }
363  }
364 
365  if(materialWasNotFound)
366  {
367  PrintNotAMolecularMaterial("G4DNAMolecularMaterial::GetDensityTableFor",lookForMaterial);
368  }
369 
370  fAskedDensityTable.insert(make_pair(lookForMaterial, output));
371 
372  return output;
373 }
374 
375 const std::vector<double>* G4DNAMolecularMaterial::GetNumMolPerVolTableFor(const G4Material* lookForMaterial) const
376 {
378  {
379  if(fIsInitialized)
380  {
381  G4ExceptionDescription exceptionDescription;
382  exceptionDescription << "The pointer fpCompNumMolPerVolTable is not initialized whereas the singleton of G4DNAMolecularMaterial "
383  << "has already been initialized."<< G4endl;
384  G4Exception("G4DNAMolecularMaterial::GetNumMolPerVolTableFor","G4DNAMolecularMaterial005",
385  FatalException,exceptionDescription);
386  }
387 
388  if(G4StateManager::GetStateManager()->GetCurrentState() == G4State_Idle)
389  {
390  const_cast<G4DNAMolecularMaterial*>(this)->Initialize();
391  }
392  else
393  {
394  G4ExceptionDescription exceptionDescription;
395  exceptionDescription << "The geant4 application is at the wrong state. State must be : G4State_Idle."<< G4endl;
396  G4Exception("G4DNAMolecularMaterial::GetNumMolPerVolTableFor",
397  "G4DNAMolecularMaterial_WRONG_STATE_APPLICATION",FatalException,exceptionDescription);
398  }
399  }
400 
401  std::map<const G4Material*,std::vector<double>*,CompareMaterial>::const_iterator it_askedNumMolPerVolTable = fAskedNumPerVolTable.find(lookForMaterial);
402  if(it_askedNumMolPerVolTable != fAskedNumPerVolTable.end())
403  {
404  return it_askedNumMolPerVolTable->second;
405  }
406 
407  const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
408 
409  std::vector<double>* output = new std::vector<double>(materialTable->size());
410 
411  ComponentMap::const_iterator it;
412 
413  G4bool materialWasNotFound = true;
414 
415  for(int i = 0 ; i < int(materialTable->size()) ; i++)
416  {
417  ComponentMap& densityTable = (*fpCompNumMolPerVolTable)[i];
418 
419  it = densityTable.find(lookForMaterial);
420 
421  if(it==densityTable.end())
422  {
423  (*output)[i] = 0.0;
424  }
425  else
426  {
427  materialWasNotFound = false;
428  (*output)[i] = it->second;
429  }
430  }
431 
432  if(materialWasNotFound)
433  {
434  PrintNotAMolecularMaterial("G4DNAMolecularMaterial::GetNumMolPerVolTableFor",lookForMaterial);
435  }
436 
437  fAskedNumPerVolTable.insert(make_pair(lookForMaterial, output));
438 
439  return output;
440 }
441 
442 void G4DNAMolecularMaterial::PrintNotAMolecularMaterial(const char* methodName, const G4Material* lookForMaterial) const
443 {
444  std::map<const G4Material*,bool,CompareMaterial>::iterator it = fWarningPrinted.find(lookForMaterial);
445 
446  if(it == fWarningPrinted.end())
447  {
448  G4ExceptionDescription exceptionDescription;
449  exceptionDescription
450  << "The material " << lookForMaterial->GetName()
451  << " is not defined as a molecular material."<< G4endl
452  << "Meaning: The elements should be added to the material using atom count rather than mass fraction (cf. G4Material)"
453  << G4endl
454  << "If you want to use DNA processes on liquid water, you should better use the NistManager to create the water material."
455  << G4endl
456  << "Since this message is displayed, it means that the DNA models will not be called."
457  << "Please note that this message will only appear once even if you are using other methods of G4DNAMolecularMaterial."
458  << G4endl;
459 
460  G4Exception(methodName,"MATERIAL_NOT_DEFINE_USING_ATOM_COUNT",JustWarning,exceptionDescription);
461  fWarningPrinted[lookForMaterial] = true;
462  }
463 }
const std::vector< double > * GetDensityTableFor(const G4Material *) const
void SearchMolecularMaterial(G4Material *parentMaterial, G4Material *material, double currentFraction)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
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
Float_t mat
Definition: plot.C:40
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