Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4MaterialPropertiesTable.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 //
27 // $Id: G4MaterialPropertiesTable.cc 102806 2017-02-22 16:36:36Z gcosmo $
28 //
29 //
31 // G4MaterialPropertiesTable Implementation
33 //
34 // File: G4MaterialPropertiesTable.cc
35 // Version: 1.0
36 // Created: 1996-02-08
37 // Author: Juliet Armstrong
38 // Updated: 2005-05-12 add SetGROUPVEL(), courtesy of
39 // Horton-Smith (bug report #741), by P. Gumplinger
40 // 2002-11-05 add named material constants by P. Gumplinger
41 // 1999-11-05 Migration from G4RWTPtrHashDictionary to STL
42 // by John Allison
43 // 1997-03-26 by Peter Gumplinger
44 // > cosmetics (only)
45 // mail: gum@triumf.ca
46 //
48 
49 #include "globals.hh"
51 #include "G4PhysicalConstants.hh"
52 #include "G4Log.hh"
53 
55 // Constructors
57 
59 {
60 }
61 
63 // Destructor
65 
67 {
68  MPTiterator i;
69  for (i = MPT.begin(); i != MPT.end(); ++i)
70  {
71  delete (*i).second;
72  }
73  MPT.clear();
74  MPTC.clear();
75 }
76 
78 // Methods
80 
82 {
83  // Returns the constant material property corresponding to a key
84 
85  MPTCiterator j;
86  j = MPTC.find(G4String(key));
87  if ( j != MPTC.end() ) return j->second;
88  G4cout << "key: " << G4String(key) << G4endl;
89  G4Exception("G4MaterialPropertiesTable::GetConstProperty()","mat202",
90  FatalException, "Constant Material Property not found.");
91  return 0.;
92 }
93 
95 {
96  // Returns true if a const property 'key' exists
97 
98  MPTCiterator j;
99  j = MPTC.find(G4String(key));
100  if ( j != MPTC.end() ) return true;
101  return false;
102 }
103 
106 {
107  // Returns a Material Property Vector corresponding to a key
108 
109  //Important Note for MT. adotti 17 Feb 2016
110  //In previous implementation the following line was at the bottom of the
111  //function causing a rare race-condition.
112  //Moving this line here from the bottom solves the problem because:
113  //1- Map is accessed only via operator[] (to insert) and find() (to search),
114  // and these are thread safe if done on separate elements.
115  // See notes on data-races at:
116  // http://www.cplusplus.com/reference/map/map/operator%5B%5D/
117  // http://www.cplusplus.com/reference/map/map/find/
118  //2- So we have a data race if two threads access the same element (GROUPVEL)
119  // one in read and one in write mode. This was happening with the line
120  // at the bottom of the code, one thread in SetGROUPVEL(),
121  // and the other here
122  //3- SetGROUPVEL() is protected by a mutex that ensures that only
123  // one thread at the time will execute its code
124  //4- The if() statement guarantees that only if two threads are searching
125  // the same problematic key (GROUPVEL) the mutex will be used.
126  // Different keys do not lock (good for performances)
127  //5- As soon as a thread acquires the mutex in SetGROUPVEL it checks again
128  // if the map has GROUPVEL key, if so returns immediately.
129  // This "double check" allows to execute the heavy code to calculate
130  // group velocity only once even if two threads enter SetGROUPVEL together
131  if (G4String(key) == "GROUPVEL") return SetGROUPVEL();
132 
133  MPTiterator i;
134  i = MPT.find(G4String(key));
135  if ( i != MPT.end() ) return i->second;
136  return nullptr;
137 }
138 
140  G4double aPhotonEnergy,
141  G4double aPropertyValue)
142 {
143  // Allows to add an entry pair directly to the Material Property Vector
144  // given a key
145 
146  G4MaterialPropertyVector *targetVector=MPT [G4String(key)];
147  if (targetVector != nullptr)
148  {
149  targetVector->InsertValues(aPhotonEnergy, aPropertyValue);
150  }
151  else
152  {
153  G4Exception("G4MaterialPropertiesTable::AddEntry()", "mat203",
154  FatalException, "Material Property Vector not found.");
155  }
156 }
157 
159 {
160  MPTiterator i;
161  for (i = MPT.begin(); i != MPT.end(); ++i)
162  {
163  G4cout << (*i).first << G4endl;
164  if ( (*i).second != 0 )
165  {
166  (*i).second->DumpValues();
167  }
168  else
169  {
170  G4Exception("G4MaterialPropertiesTable::DumpTable()", "mat204",
171  JustWarning, "NULL Material Property Vector Pointer.");
172  }
173  }
174  MPTCiterator j;
175  for (j = MPTC.begin(); j != MPTC.end(); ++j)
176  {
177  G4cout << j->first << G4endl;
178  if ( j->second != 0 )
179  {
180  G4cout << j->second << G4endl;
181  }
182  else
183  {
184  G4Exception("G4MaterialPropertiesTable::DumpTable()", "mat202",
185  JustWarning, "No Material Constant Property.");
186  }
187  }
188 }
189 
190 #ifdef G4MULTITHREADED
191 #include "G4AutoLock.hh"
192 namespace {
193  G4Mutex materialPropertyTableMutex = G4MUTEX_INITIALIZER;
194 }
195 #endif
196 
197 G4MaterialPropertyVector* G4MaterialPropertiesTable::SetGROUPVEL()
198 {
199 #ifdef G4MULTITHREADED
200  G4AutoLock mptm(&materialPropertyTableMutex);
201 #endif
202 
203  // check if "GROUPVEL" already exists
204  MPTiterator itr;
205  itr = MPT.find("GROUPVEL");
206  if(itr != MPT.end()) return itr->second;
207 
208  // fetch RINDEX data, give up if unavailable
209  //
210  G4MaterialPropertyVector *rindex = this->GetProperty("RINDEX");
211  if (rindex==0) { return 0; }
212 
213  // RINDEX exists but has no entries, give up
214  //
215  if ( rindex->GetVectorLength() == 0 ) { return 0; }
216 
217  // add GROUPVEL vector
218  //
220 
221  // fill GROUPVEL vector using RINDEX values
222  // rindex built-in "iterator" was advanced to first entry above
223  //
224  G4double E0 = rindex->Energy(0);
225  G4double n0 = (*rindex)[0];
226 
227  if (E0 <= 0.)
228  {
229  G4Exception("G4MaterialPropertiesTable::SetGROUPVEL()", "mat205",
230  FatalException, "Optical Photon Energy <= 0");
231  }
232 
233  if ( rindex->GetVectorLength() >= 2 )
234  {
235  // good, we have at least two entries in RINDEX
236  // get next energy/value pair
237 
238  G4double E1 = rindex->Energy(1);
239  G4double n1 = (*rindex)[1];
240 
241  if (E1 <= 0.)
242  {
243  G4Exception("G4MaterialPropertiesTable::SetGROUPVEL()", "mat205",
244  FatalException, "Optical Photon Energy <= 0");
245  }
246 
247  G4double vg;
248 
249  // add entry at first photon energy
250  //
251  vg = c_light/(n0+(n1-n0)/G4Log(E1/E0));
252 
253  // allow only for 'normal dispersion' -> dn/d(logE) > 0
254  //
255  if((vg<0) || (vg>c_light/n0)) { vg = c_light/n0; }
256 
257  groupvel->InsertValues( E0, vg );
258 
259  // add entries at midpoints between remaining photon energies
260  //
261 
262  for (size_t i = 2; i < rindex->GetVectorLength(); i++)
263  {
264  vg = c_light/( 0.5*(n0+n1)+(n1-n0)/G4Log(E1/E0));
265 
266  // allow only for 'normal dispersion' -> dn/d(logE) > 0
267  //
268  if((vg<0) || (vg>c_light/(0.5*(n0+n1)))) { vg = c_light/(0.5*(n0+n1)); }
269  groupvel->InsertValues( 0.5*(E0+E1), vg );
270 
271  // get next energy/value pair, or exit loop
272  //
273  E0 = E1;
274  n0 = n1;
275  E1 = rindex->Energy(i);
276  n1 = (*rindex)[i];
277 
278  if (E1 <= 0.)
279  {
280  G4Exception("G4MaterialPropertiesTable::SetGROUPVEL()", "mat205",
281  FatalException, "Optical Photon Energy <= 0");
282  }
283  }
284 
285  // add entry at last photon energy
286  //
287  vg = c_light/(n1+(n1-n0)/G4Log(E1/E0));
288 
289  // allow only for 'normal dispersion' -> dn/d(logE) > 0
290  //
291  if((vg<0) || (vg>c_light/n1)) { vg = c_light/n1; }
292  groupvel->InsertValues( E1, vg );
293  }
294  else // only one entry in RINDEX -- weird!
295  {
296  groupvel->InsertValues( E0, c_light/n0 );
297  }
298 
299  this->AddProperty( "GROUPVEL", groupvel );
300 
301  return groupvel;
302 }
void AddEntry(const char *key, G4double aPhotonEnergy, G4double aPropertyValue)
void InsertValues(G4double energy, G4double value)
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
G4MaterialPropertyVector * AddProperty(const char *key, G4double *PhotonEnergies, G4double *PropertyValues, G4int NumEntries)
G4GLOB_DLL std::ostream G4cout
G4PhysicsOrderedFreeVector G4MaterialPropertyVector
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4double G4Log(G4double x)
Definition: G4Log.hh:230
G4bool ConstPropertyExists(const char *key) const
G4int G4Mutex
Definition: G4Threading.hh:173
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4MaterialPropertyVector * GetProperty(const char *key)
float c_light
Definition: hepunit.py:257
G4double GetConstProperty(const char *key) const