VGM Version 5.3
Loading...
Searching...
No Matches
MaterialFactory.cxx
Go to the documentation of this file.
1// $Id$
2
3// -----------------------------------------------------------------------
4// The Geant4GM package of the Virtual Geometry Model
5// Copyright (C) 2007, Ivana Hrivnacova
6// All rights reserved.
7//
8// For the licensing terms see vgm/LICENSE.
9// Contact: ivana@ipno.in2p3.fr
10// -----------------------------------------------------------------------
11
12//
13// Class MaterialFactory
14// ------------------------
15// The interface to material factory.
16//
17// Author: Ivana Hrivnacova; IPN Orsay
18
20
21#include "ClhepVGM/Units.h"
22
24
33
34#include "G4Element.hh"
35#include "G4Material.hh"
36#include "G4NistManager.hh"
37#include "G4SystemOfUnits.hh"
38
39#include <cmath>
40
41const double Geant4GM::MaterialFactory::fgkTolerance = 1e-09;
42
43//_____________________________________________________________________________
45 : VGM::IMaterialFactory(),
46 BaseVGM::VMaterialFactory("Geant4_GM_Material_Factory"),
47 fVacuumElements()
48{
50}
51
52//_____________________________________________________________________________
54 : VGM::IMaterialFactory(rhs),
55 BaseVGM::VMaterialFactory(rhs),
56 fVacuumElements(rhs.fVacuumElements)
57
58{
60}
61
62//_____________________________________________________________________________
64{
65 //
66
67 // delete map singletons
68
71 // There is inconsistence in using the singleton maps
72 // via a factory which is not a singleton
73 // TO DO: to be improved later
74}
75
76//
77// private functions
78//
79
80//_____________________________________________________________________________
81void Geant4GM::MaterialFactory::ImportIsotope(G4Isotope* isotope)
82{
84
85 if (Debug() > 0) {
87 std::cout << "Importing isotope: ";
88 if (Debug() > 1) {
89 void* isotopePtr = (void*)isotope;
90 std::cout << isotopePtr;
91 }
92 std::cout << std::endl;
94 std::cout << *isotope << std::endl;
95 }
96
97 VGM::IIsotope* vgmIsotope = new Geant4GM::Isotope(isotope);
98 IsotopeStore().push_back(vgmIsotope);
99}
100
101//_____________________________________________________________________________
102void Geant4GM::MaterialFactory::ImportElement(G4Element* element)
103{
105
106 if (Debug() > 0) {
108 std::cout << "Importing element: ";
109 if (Debug() > 1) {
110 void* elementPtr = (void*)element;
111 std::cout << elementPtr;
112 }
113 std::cout << std::endl;
115 std::cout << *element << std::endl;
116 }
117
118 VGM::IElement* vgmElement = new Geant4GM::Element(element);
119 ElementStore().push_back(vgmElement);
120}
121
122//_____________________________________________________________________________
123void Geant4GM::MaterialFactory::ImportMaterial(G4Material* material)
124{
126
127 if (Debug() > 0) {
129 std::cout << "Importing material: ";
130 if (Debug() > 1) {
131 void* materialPtr = (void*)material;
132 std::cout << materialPtr;
133 }
134 std::cout << std::endl;
136 std::cout << *material << std::endl;
137 }
138
139 VGM::IMaterial* vgmMaterial = new Geant4GM::Material(material);
140 MaterialStore().push_back(vgmMaterial);
141}
142
143//_____________________________________________________________________________
144bool Geant4GM::MaterialFactory::CompareIsotopes(const G4Element* g4Element,
145 const VGM::IsotopeVector& isotopes,
146 const VGM::RelAbundanceVector& relAbundances)
147{
151
152 if (isotopes.size() != g4Element->GetNumberOfIsotopes()) return false;
153
154 for (G4int i = 0; i < G4int(g4Element->GetNumberOfIsotopes()); ++i) {
155
156 const G4Isotope* g4Isotope = g4Element->GetIsotope(i);
157 double g4RelAbundance = g4Element->GetRelativeAbundanceVector()[i];
158 G4bool match = false;
159
160 for (G4int j = 0; j < G4int(isotopes.size()); ++j) {
161 VGM::IIsotope* vgmIsotope = isotopes[j];
162 if (std::abs(vgmIsotope->Z() - g4Isotope->GetZ()) < fgkTolerance &&
163 std::abs(vgmIsotope->N() - g4Isotope->GetN()) < fgkTolerance &&
164 std::abs(vgmIsotope->A() - g4Isotope->GetA() / (g / mole)) <
165 fgkTolerance &&
166 std::abs(relAbundances[j] - g4RelAbundance) < fgkTolerance) {
167 match = true;
168 break;
169 }
170 }
171 if (!match) return false;
172 }
173
174 // All isotopes were matched
175 return true;
176}
177
178//
179// public functions
180//
181
182//_____________________________________________________________________________
184 const std::string& name, int z, int n, double a)
185{
186 // Create isotope if isotope with given name does not yet exist
187
188 G4Isotope* g4Isotope = G4Isotope::GetIsotope(name, false);
189
190 // Do not take the isotope if its properties do not match
191 if (g4Isotope &&
192 (std::abs(g4Isotope->GetZ() - z) >= fgkTolerance ||
193 std::abs(g4Isotope->GetN() - n) >= fgkTolerance ||
194 std::abs(g4Isotope->GetA() / (g / mole) - a) >= fgkTolerance)) {
195 g4Isotope = 0;
196 }
197
198 VGM::IIsotope* vgmIsotope;
199 if (g4Isotope) {
200 vgmIsotope = Geant4GM::IsotopeMap::Instance()->GetIsotope(g4Isotope);
201 }
202 else {
203 vgmIsotope = new Geant4GM::Isotope(name, z, n, a);
204 IsotopeStore().push_back(vgmIsotope);
205 }
206
207 return vgmIsotope;
208}
209
210//_____________________________________________________________________________
212 const std::string& name, const std::string& symbol, double z, double a)
213{
214 // Create element if such element with specified properties does not
215 // yet exist
216
217 G4Element* g4Element = G4Element::GetElement(name, false);
218
219 // Do not take the element if its properties do not match
220 if (g4Element &&
221 (std::abs(g4Element->GetZ() - z) >= fgkTolerance ||
222 std::abs(g4Element->GetA() / (g / mole) - a) >= fgkTolerance)) {
223 g4Element = 0;
224 }
225
226 VGM::IElement* vgmElement;
227 if (g4Element) {
228 vgmElement = Geant4GM::ElementMap::Instance()->GetElement(g4Element);
229 if (!vgmElement) {
230 vgmElement = new Geant4GM::Element(g4Element);
231 ElementStore().push_back(vgmElement);
232 }
233 }
234 else {
235 if (z < 1.0) {
236 // special case (vacuum)
237 // in Geant4 vacuum has element with z = 1.0, a = 1.01
238 vgmElement = new Geant4GM::Element(name, symbol, z = 1.0, a = 1.01);
239
240 fVacuumElements.insert(vgmElement);
241 }
242 else {
243 vgmElement = new Geant4GM::Element(name, symbol, z, a);
244 }
245
246 ElementStore().push_back(vgmElement);
247 }
248
249 // Now G4Element exists
250 g4Element = G4Element::GetElement(name, false);
251
252 // Import isotopes if they were created with the G4element
253 for (unsigned int i = 0; i < g4Element->GetNumberOfIsotopes(); ++i) {
254 G4Isotope* g4Isotope = const_cast<G4Isotope*>(g4Element->GetIsotope(i));
255 VGM::IIsotope* vgmIsotope =
257 if (!vgmIsotope) ImportIsotope(g4Isotope);
258 }
259
260 return vgmElement;
261}
262
263//_____________________________________________________________________________
265 const std::string& symbol, const VGM::IsotopeVector& isotopes,
266 const VGM::RelAbundanceVector& relAbundances)
267{
268 // Create element if such element with specified properties does not
269 // yet exist
270
271 G4Element* g4Element = G4Element::GetElement(name, false);
272
273 // Do not take the element if its properties do not match
274 if (g4Element && !CompareIsotopes(g4Element, isotopes, relAbundances)) {
275 g4Element = 0;
276 }
277
278 VGM::IElement* vgmElement;
279 if (g4Element)
280 vgmElement = Geant4GM::ElementMap::Instance()->GetElement(g4Element);
281 else {
282 vgmElement = new Geant4GM::Element(name, symbol, isotopes, relAbundances);
283 ElementStore().push_back(vgmElement);
284 }
285
286 return vgmElement;
287}
288
289//_____________________________________________________________________________
291{
292 // Create element using Nist manager,
293 // if such element with specified properties does not yet exist
294
295 G4NistManager* nistManager = G4NistManager::Instance();
296 G4Element* g4Element = nistManager->FindOrBuildElement(z, isotopes);
297 if (!g4Element) {
298 std::cerr << "No element with z=" << z << " defined." << std::endl;
299 return 0;
300 }
301
302 VGM::IElement* vgmElement =
304
305 if (!vgmElement) {
306
307 for (unsigned i = 0; i < g4Element->GetNumberOfIsotopes(); i++)
308 ImportIsotope(const_cast<G4Isotope*>(g4Element->GetIsotope(i)));
309
310 vgmElement = new Geant4GM::Element(g4Element);
311 ElementStore().push_back(vgmElement);
312 }
313 return vgmElement;
314}
315
316//_____________________________________________________________________________
318 const std::string& name, double density, VGM::IElement* element,
319 double /*radlen*/, double /*intlen*/)
320{
321 // Create material
322
323 bool isVacuum = false;
324 if (fVacuumElements.find(element) != fVacuumElements.end()) isVacuum = true;
325
326 VGM::IMaterial* vgmMaterial =
327 new Geant4GM::Material(name, density, element, isVacuum);
328
329 MaterialStore().push_back(vgmMaterial);
330 return vgmMaterial;
331}
332
333//_____________________________________________________________________________
335 const std::string& name, double density, VGM::IElement* element,
336 double /*radlen*/, double /*intlen*/, VGM::MaterialState state,
337 double temperature, double pressure)
338{
339 // Create material
340
341 bool isVacuum = false;
342 if (fVacuumElements.find(element) != fVacuumElements.end()) isVacuum = true;
343
344 VGM::IMaterial* vgmMaterial = new Geant4GM::Material(
345 name, density, element, state, temperature, pressure, isVacuum);
346
347 MaterialStore().push_back(vgmMaterial);
348 return vgmMaterial;
349}
350
351//_____________________________________________________________________________
353 const std::string& name, double density, const VGM::ElementVector& elements,
354 const VGM::MassFractionVector& fractions)
355{
356 // Create material
357
358 VGM::IMaterial* vgmMaterial =
359 new Geant4GM::Material(name, density, elements, fractions);
360
361 MaterialStore().push_back(vgmMaterial);
362 return vgmMaterial;
363}
364
365//_____________________________________________________________________________
367 const std::string& name, double density, const VGM::ElementVector& elements,
368 const VGM::MassFractionVector& fractions, VGM::MaterialState state,
369 double temperature, double pressure)
370{
371 // Create material
372
373 VGM::IMaterial* vgmMaterial = new Geant4GM::Material(
374 name, density, elements, fractions, state, temperature, pressure);
375
376 MaterialStore().push_back(vgmMaterial);
377 return vgmMaterial;
378}
379
380//_____________________________________________________________________________
382 const std::string& name, double density, const VGM::ElementVector& elements,
383 const VGM::AtomCountVector& atomCounts)
384{
385 // Create material
386
387 VGM::IMaterial* vgmMaterial =
388 new Geant4GM::Material(name, density, elements, atomCounts);
389
390 MaterialStore().push_back(vgmMaterial);
391 return vgmMaterial;
392}
393
394//_____________________________________________________________________________
396 const std::string& name, double density, const VGM::ElementVector& elements,
397 const VGM::AtomCountVector& atomCounts, VGM::MaterialState state,
398 double temperature, double pressure)
399{
400 // Create material
401
402 VGM::IMaterial* vgmMaterial = new Geant4GM::Material(
403 name, density, elements, atomCounts, state, temperature, pressure);
404
405 MaterialStore().push_back(vgmMaterial);
406 return vgmMaterial;
407}
408
409//_____________________________________________________________________________
411 int mediumId, VGM::IMaterial* material, int nofParameters, double* parameters)
412{
413 // Create medium
414
415 VGM::IMedium* vgmMedium =
416 new Geant4GM::Medium(name, mediumId, material, nofParameters, parameters);
417
418 MediumStore().push_back(vgmMedium);
419 return vgmMedium;
420}
421
422//_____________________________________________________________________________
424{
426
427 const G4IsotopeTable* isotopeTable = G4Isotope::GetIsotopeTable();
428
429 for (G4int i = 0; i < G4int(isotopeTable->size()); i++) {
430 G4Isotope* isotope = (*isotopeTable)[i];
431 ImportIsotope(isotope);
432 }
433
434 const G4ElementTable* elementTable = G4Element::GetElementTable();
435
436 for (G4int i = 0; i < G4int(elementTable->size()); i++) {
437 G4Element* element = (*elementTable)[i];
438 ImportElement(element);
439 }
440
441 const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
442
443 for (G4int j = 0; j < G4int(materialTable->size()); j++) {
444 G4Material* material = (*materialTable)[j];
445 ImportMaterial(material);
446 }
447
448 return true;
449}
static ElementMap * Instance()
G4Element * GetElement(VGM::IElement *iElement) const
VGM implementation for Geant4 element.
Definition Element.h:32
static IsotopeMap * Instance()
G4Isotope * GetIsotope(VGM::IIsotope *iIsotope) const
VGM implementation for Geant4 Isotope.
Definition Isotope.h:32
VGM material factory for Geant4.
virtual VGM::IIsotope * CreateIsotope(const std::string &name, int z, int n, double a)
Create a chemical isotope.
virtual bool Import()
Import native materials.
virtual VGM::IMaterial * CreateMaterial(const std::string &name, double density, VGM::IElement *element, double radlen, double intlen)
Create a material.
virtual VGM::IMedium * CreateMedium(const std::string &name, int mediumId, VGM::IMaterial *material, int nofParameters, double *parameters)
Create a tracking medium.
virtual VGM::IElement * CreateElement(const std::string &name, const std::string &symbol, double z, double a)
Create a chemical element.
static MaterialMap * Instance()
VGM implementation for Geant4 material.
Definition Material.h:32
The VGM implementation of interface to tracking medium.
Definition Medium.h:36
The VGM interface to elements.
Definition IElement.h:34
The VGM interface to elements.
Definition IIsotope.h:28
virtual int N() const =0
Return the effective number of nucleons.
virtual double A() const =0
Return the effective effective mass of a mole in g/mole.
virtual int Z() const =0
Return the effective atomic number.
The VGM interface to materials.
Definition IMaterial.h:44
The VGM interface to tracking medium.
Definition IMedium.h:31
BaseVGM utilities.
Definition utilities.h:23
void DebugInfo()
Debug printing.
Definition utilities.cxx:27
VGM interfaces.
Definition VMedium.h:28
std::vector< IMaterial * > MaterialStore
std::vector< double > RelAbundanceVector
Definition IElement.h:31
std::vector< int > AtomCountVector
Definition IMaterial.h:33
std::vector< double > MassFractionVector
Definition IMaterial.h:32
std::vector< IElement * > ElementVector
Definition IMaterial.h:31
std::vector< IIsotope * > IsotopeStore
std::vector< IIsotope * > IsotopeVector
Definition IElement.h:30
MaterialState
Definition IMaterial.h:36
std::vector< IElement * > ElementStore