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.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 // G4tgbMaterialMgr implementation
 27 //
 28 // Author: P.Arce, CIEMAT (November 2007)
 29 // --------------------------------------------------------------------
 30 
 31 #include "G4tgbMaterialMgr.hh"
 32 #include "G4tgbMaterialMixtureByWeight.hh"
 33 #include "G4tgbMaterialMixtureByVolume.hh"
 34 #include "G4tgbMaterialMixtureByNoAtoms.hh"
 35 #include "G4tgbMaterialSimple.hh"
 36 
 37 #include "G4tgrMaterialFactory.hh"
 38 #include "G4tgrMaterialSimple.hh"
 39 #include "G4tgrMaterialMixture.hh"
 40 #include "G4tgrUtils.hh"
 41 #include "G4tgrMessenger.hh"
 42 #include "G4NistManager.hh"
 43 
 44 G4ThreadLocal G4tgbMaterialMgr* G4tgbMaterialMgr::theInstance = nullptr;
 45 
 46 // --------------------------------------------------------------------
 47 G4tgbMaterialMgr::G4tgbMaterialMgr()
 48 {
 49 }
 50 
 51 // --------------------------------------------------------------------
 52 G4tgbMaterialMgr* G4tgbMaterialMgr::GetInstance()
 53 {
 54   if(theInstance == nullptr)
 55   {
 56     theInstance = new G4tgbMaterialMgr;
 57     theInstance->CopyIsotopes();
 58     theInstance->CopyElements();
 59     theInstance->CopyMaterials();
 60   }
 61   return theInstance;
 62 }
 63 
 64 // --------------------------------------------------------------------
 65 G4tgbMaterialMgr::~G4tgbMaterialMgr()
 66 {
 67   for(auto isotcite = theG4tgbIsotopes.cbegin();
 68            isotcite != theG4tgbIsotopes.cend(); ++isotcite)
 69   {
 70     delete(*isotcite).second;
 71   }
 72   theG4tgbIsotopes.clear();
 73 
 74   for(auto elemcite = theG4tgbElements.cbegin();
 75            elemcite != theG4tgbElements.cend(); ++elemcite)
 76   {
 77     delete(*elemcite).second;
 78   }
 79   theG4tgbElements.clear();
 80 
 81   for(auto matcite = theG4tgbMaterials.cbegin();
 82            matcite != theG4tgbMaterials.cend(); ++matcite)
 83   {
 84     delete(*matcite).second;
 85   }
 86   theG4tgbMaterials.clear();
 87 
 88   delete theInstance;
 89 }
 90 
 91 // --------------------------------------------------------------------
 92 void G4tgbMaterialMgr::CopyIsotopes()
 93 {
 94   const G4mstgrisot tgrIsots =
 95     G4tgrMaterialFactory::GetInstance()->GetIsotopeList();
 96   for(auto cite = tgrIsots.cbegin(); cite != tgrIsots.cend(); ++cite)
 97   {
 98     G4tgrIsotope* tgr                = (*cite).second;
 99     G4tgbIsotope* tgb                = new G4tgbIsotope(tgr);
100     theG4tgbIsotopes[tgb->GetName()] = tgb;
101   }
102 }
103 
104 // --------------------------------------------------------------------
105 void G4tgbMaterialMgr::CopyElements()
106 {
107   const G4mstgrelem tgrElems =
108     G4tgrMaterialFactory::GetInstance()->GetElementList();
109   for(auto cite = tgrElems.cbegin(); cite != tgrElems.cend(); ++cite)
110   {
111     G4tgrElement* tgr                = (*cite).second;
112     G4tgbElement* tgb                = new G4tgbElement(tgr);
113     theG4tgbElements[tgb->GetName()] = tgb;
114   }
115 }
116 
117 // --------------------------------------------------------------------
118 void G4tgbMaterialMgr::CopyMaterials()
119 {
120   const G4mstgrmate tgrMates =
121     G4tgrMaterialFactory::GetInstance()->GetMaterialList();
122   for(auto cite = tgrMates.cbegin(); cite != tgrMates.cend(); ++cite)
123   {
124     G4tgrMaterial* tgr = (*cite).second;
125     G4tgbMaterial* tgb = nullptr;
126     if(tgr->GetType() == "MaterialSimple")
127     {
128       tgb = new G4tgbMaterialSimple(tgr);
129     }
130     else if(tgr->GetType() == "MaterialMixtureByWeight")
131     {
132       tgb = new G4tgbMaterialMixtureByWeight(tgr);
133     }
134     else if(tgr->GetType() == "MaterialMixtureByNoAtoms")
135     {
136       tgb = new G4tgbMaterialMixtureByNoAtoms(tgr);
137     }
138     else if(tgr->GetType() == "MaterialMixtureByVolume")
139     {
140       tgb = new G4tgbMaterialMixtureByVolume(tgr);
141     }
142     else
143     {
144       return;
145     }
146     theG4tgbMaterials[tgb->GetName()] = tgb;
147   }
148 }
149 
150 // --------------------------------------------------------------------
151 G4Isotope* G4tgbMaterialMgr::FindOrBuildG4Isotope(const G4String& name)
152 {
153   G4Isotope* g4isot = FindBuiltG4Isotope(name);
154   if(g4isot == nullptr)
155   {
156     G4tgbIsotope* tgbisot = FindG4tgbIsotope(name);
157     // FindG4tgbIsotope never returns nullptr, otherwise if not found, crashes
158     g4isot = tgbisot->BuildG4Isotope();
159     // Register it
160     G4String isotname       = g4isot->GetName();
161     theG4Isotopes[isotname] = g4isot;
162   }
163   else
164   {
165 #ifdef G4VERBOSE
166     if(G4tgrMessenger::GetVerboseLevel() >= 1)
167     {
168       G4cout << " G4tgbMaterialMgr::FindOrBuildG4Isotope() -"
169              << " G4Isotope already built: " << g4isot->GetName() << G4endl;
170     }
171 #endif
172   }
173 
174 #ifdef G4VERBOSE
175   if(G4tgrMessenger::GetVerboseLevel() >= 2)
176   {
177     G4cout << " G4tgbMaterialMgr::FindOrBuildG4Isotope() - Isotope: " << name
178            << G4endl;
179   }
180 #endif
181   return g4isot;
182 }
183 
184 // --------------------------------------------------------------------
185 G4Isotope* G4tgbMaterialMgr::FindBuiltG4Isotope(const G4String& name) const
186 {
187   G4Isotope* g4isot = nullptr;
188 
189   G4msg4isot::const_iterator cite = theG4Isotopes.find(name);
190   if(cite != theG4Isotopes.cend())
191   {
192     g4isot = (*cite).second;
193 #ifdef G4VERBOSE
194     if(G4tgrMessenger::GetVerboseLevel() >= 2)
195     {
196       G4cout << " G4tgbMaterialMgr::FindBuiltG4Isotope() - Isotope: " << name
197              << " = " << g4isot << G4endl;
198     }
199 #endif
200   }
201 
202   return g4isot;
203 }
204 
205 // --------------------------------------------------------------------
206 G4tgbIsotope* G4tgbMaterialMgr::FindG4tgbIsotope(const G4String& name,
207                                                  G4bool bMustExist) const
208 {
209   G4tgbIsotope* isot = nullptr;
210 
211   G4mstgbisot::const_iterator cite = theG4tgbIsotopes.find(name);
212   if(cite != theG4tgbIsotopes.cend())
213   {
214 #ifdef G4VERBOSE
215     if(G4tgrMessenger::GetVerboseLevel() >= 2)
216     {
217       G4cout << " G4tgbMaterialMgr::FindG4tgbIsotope() -"
218              << " G4tgbIsotope found: " << ((*cite).second)->GetName()
219              << G4endl;
220     }
221 #endif
222     isot = (*cite).second;
223   }
224   if((isot == nullptr) && bMustExist)
225   {
226     G4String ErrMessage = "Isotope " + name + " not found !";
227     G4Exception("G4tgbMaterialMgr::FindG4tgbIsotope()", "InvalidSetup",
228                 FatalException, ErrMessage);
229   }
230 
231   return isot;
232 }
233 
234 // --------------------------------------------------------------------
235 G4Element* G4tgbMaterialMgr::FindOrBuildG4Element(const G4String& name,
236                                                   G4bool bMustExist)
237 {
238   G4Element* g4elem = FindBuiltG4Element(name);
239   if(g4elem == nullptr)
240   {
241     G4tgbElement* tgbelem = FindG4tgbElement(name, false);
242     if(tgbelem == nullptr)
243     {
244       // If FindG4tgbElement returns nullptr, look for a G4NISTElement
245       G4cout << "  G4NistManager::Instance()->FindOrBuildElement( " << G4endl;
246       g4elem = G4NistManager::Instance()->FindOrBuildElement(name);
247     }
248     else
249     {
250       if(tgbelem->GetType() == "ElementSimple")
251       {
252         g4elem = tgbelem->BuildG4ElementSimple();
253       }
254       else if(tgbelem->GetType() == "ElementFromIsotopes")
255       {
256         g4elem = tgbelem->BuildG4ElementFromIsotopes();
257       }
258       else
259       {
260         G4String ErrMessage =
261           "Element type " + tgbelem->GetType() + " does not exist !";
262         G4Exception("G4tgbMaterialMgr::GetG4Element()", "InvalidSetup",
263                     FatalException, ErrMessage);
264       }
265     }
266     // Register it
267     if((g4elem != nullptr))
268     {
269       theG4Elements[g4elem->GetName()] = g4elem;
270 #ifdef G4VERBOSE
271       if(G4tgrMessenger::GetVerboseLevel() >= 2)
272       {
273         G4cout << " G4tgbMaterialMgr::FindOrBuildG4Element() - Element: "
274                << name << G4endl;
275       }
276 #endif
277     }
278     else
279     {
280       if(bMustExist)
281       {
282         G4String ErrMessage = "Element " + name + " not found !";
283         G4Exception("G4tgbMaterialMgr::FindOrBuildG4Element()", "InvalidSetup",
284                     FatalException, ErrMessage);
285       }
286 #ifdef G4VERBOSE
287       if(G4tgrMessenger::GetVerboseLevel() >= 2)
288       {
289         G4cout << " G4tgbMaterialMgr::FindOrBuildG4Element() - Element: "
290                << name << " not found  " << G4endl;
291       }
292 #endif
293     }
294   }
295   else
296   {
297 #ifdef G4VERBOSE
298     if(G4tgrMessenger::GetVerboseLevel() >= 1)
299     {
300       G4cout << " G4tgbMaterialMgr::GetG4Element() -"
301              << " G4Element already built: " << g4elem->GetName() << G4endl;
302     }
303 #endif
304   }
305 
306   return g4elem;
307 }
308 
309 // --------------------------------------------------------------------
310 G4Element* G4tgbMaterialMgr::FindBuiltG4Element(const G4String& name) const
311 {
312   G4Element* g4elem = nullptr;
313 
314   G4msg4elem::const_iterator cite = theG4Elements.find(name);
315   if(cite != theG4Elements.cend())
316   {
317     g4elem = (*cite).second;
318 #ifdef G4VERBOSE
319     if(G4tgrMessenger::GetVerboseLevel() >= 2)
320     {
321       G4cout << " G4tgbMaterialMgr::FindBuiltG4Element() - Element: " << name
322              << " = " << g4elem << G4endl;
323     }
324 #endif
325   }
326 
327   return g4elem;
328 }
329 
330 // --------------------------------------------------------------------
331 G4tgbElement* G4tgbMaterialMgr::FindG4tgbElement(const G4String& name,
332                                                  G4bool bMustExist) const
333 {
334   G4tgbElement* elem = nullptr;
335 
336   G4mstgbelem::const_iterator cite = theG4tgbElements.find(name);
337   if(cite != theG4tgbElements.cend())
338   {
339 #ifdef G4VERBOSE
340     if(G4tgrMessenger::GetVerboseLevel() >= 2)
341     {
342       G4cout << " G4tgbMaterialMgr::FindG4tgbElement() -"
343              << " G4tgbElement found: " << ((*cite).second)->GetName()
344              << G4endl;
345     }
346 #endif
347     elem = (*cite).second;
348   }
349   if((elem == nullptr) && bMustExist)
350   {
351     G4String ErrMessage = "Element " + name + "  not found !";
352     G4Exception("G4tgbMaterialMgr::FindG4tgbElement()", "InvalidSetup",
353                 FatalException, ErrMessage);
354   }
355 
356   return elem;
357 }
358 
359 // --------------------------------------------------------------------
360 G4Material* G4tgbMaterialMgr::FindOrBuildG4Material(const G4String& name,
361                                                     G4bool bMustExist)
362 {
363   G4Material* g4mate = FindBuiltG4Material(name);
364   if(g4mate == nullptr)
365   {
366     G4tgbMaterial* tgbmate = FindG4tgbMaterial(name, false);
367 
368     if(tgbmate == nullptr)
369     {
370       // if FindG4tgbMaterial() returns 0, look for a G4NISTMaterial
371       g4mate = G4NistManager::Instance()->FindOrBuildMaterial(name);
372     }
373     else
374     {
375       g4mate = tgbmate->BuildG4Material();
376 
377       if(tgbmate->GetTgrMate()->GetIonisationMeanExcitationEnergy() != -1.)
378       {
379         g4mate->GetIonisation()->SetMeanExcitationEnergy(
380           tgbmate->GetTgrMate()->GetIonisationMeanExcitationEnergy());
381       }
382     }
383 
384     // Register it
385     if(g4mate != nullptr)
386     {
387       theG4Materials[g4mate->GetName()] = g4mate;
388 #ifdef G4VERBOSE
389       if(G4tgrMessenger::GetVerboseLevel() >= 2)
390       {
391         G4cout << " G4tgbMaterialMgr::FindOrBuildG4Material() - Material: "
392                << name << G4endl;
393       }
394 #endif
395     }
396     else
397     {
398       if(bMustExist)
399       {
400         G4String ErrMessage = "Material " + name + "  not found !";
401         G4Exception("G4tgbMaterialMgr::FindOrBuildG4Material()", "InvalidSetup",
402                     FatalException, ErrMessage);
403       }
404 #ifdef G4VERBOSE
405       if(G4tgrMessenger::GetVerboseLevel() >= 2)
406       {
407         G4cout << " G4tgbMaterialMgr::FindOrBuildG4Material() - Element: "
408                << name << " not found  " << G4endl;
409       }
410 #endif
411     }
412   }
413   else
414   {
415 #ifdef G4VERBOSE
416     if(G4tgrMessenger::GetVerboseLevel() >= 1)
417     {
418       G4cout << " G4tgbMaterialMgr::FindOrBuildG4Material() -"
419              << " G4Material already built: " << g4mate->GetName() << G4endl;
420     }
421 #endif
422   }
423 
424   return g4mate;
425 }
426 
427 // --------------------------------------------------------------------
428 G4Material* G4tgbMaterialMgr::FindBuiltG4Material(const G4String& name) const
429 {
430   G4Material* g4mate = nullptr;
431   //---------- look for an existing G4Material
432   G4msg4mate::const_iterator cite = theG4Materials.find(name);
433   if(cite != theG4Materials.cend())
434   {
435     g4mate = (*cite).second;
436 #ifdef G4VERBOSE
437     if(G4tgrMessenger::GetVerboseLevel() >= 2)
438     {
439       G4cout << " G4tgbMaterialMgr::FindBuiltG4Material() - Material: " << name
440              << " = " << g4mate << G4endl;
441     }
442 #endif
443   }
444 
445   return g4mate;
446 }
447 
448 // -------------------------------------------------------------------------
449 G4tgbMaterial* G4tgbMaterialMgr::FindG4tgbMaterial(const G4String& name,
450                                                    G4bool bMustExist) const
451 {
452   G4tgbMaterial* mate = nullptr;
453   G4mstgbmate::const_iterator cite = theG4tgbMaterials.find(name);
454   if(cite != theG4tgbMaterials.cend())
455   {
456     mate = (*cite).second;
457 #ifdef G4VERBOSE
458     if(G4tgrMessenger::GetVerboseLevel() >= 2)
459     {
460       G4cout << " G4tgbMaterialMgr::FindG4tgbMaterial() -"
461              << " G4tgbMaterial found: " << ((*cite).second)->GetName()
462              << " type " << ((*cite).second)->GetName() << G4endl;
463     }
464 #endif
465   }
466 
467   if((mate == nullptr) && bMustExist)
468   {
469     G4String ErrMessage = "Material " + name + "  not found !";
470     G4Exception("G4tgbMaterialMgr::FindG4tgbMaterial()", "InvalidSetup",
471                 FatalException, ErrMessage);
472   }
473 
474   return mate;
475 }
476