Geant4  10.00.p01
CCalMaterialFactory.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 //
27 // File: CCalMaterialFactory.cc
28 // Description: CCalMaterialFactory is a factory class to vuild G4Material
29 // from CCalMaterial and CCalAmaterial
31 #include <fstream>
32 #include <stdlib.h>
33 
34 #include "CCalMaterialFactory.hh"
35 #include "CCalutils.hh"
36 
37 #include "G4PhysicalConstants.hh"
38 #include "G4SystemOfUnits.hh"
39 #include "G4Material.hh"
40 
41 //#define ddebug
42 //#define debug
43 
46 
47 
51 
52 
54  const G4String& mixfile){
55  if ((matfile=="" || matfile==elementfile) &&
56  (mixfile=="" || mixfile==mixturefile))
57  return getInstance();
58  else if ((matfile != "" && elementfile != "" && matfile != elementfile) ||
59  (mixfile != "" && mixturefile != "" && mixfile != mixturefile)) {
60  G4cerr << "ERROR: Trying to get materials from " << matfile << " and "
61  << mixfile << " while previously were retrieved from "
62  << elementfile << " and " << mixturefile << "." << G4endl;
63  return 0;
64  } else {
65  if (elementfile == "")
66  elementfile=matfile;
67  if (mixturefile == "")
68  mixturefile=mixfile;
69  return getInstance();
70  }
71 }
72 
73 
75  return getInstance(matfile,matfile);
76 }
77 
78 
80  if (elementfile=="" || mixturefile=="") {
81  G4cerr << "ERROR: You haven't defined files to be used for materials in "
82  << "CCalMaterialFactory::getInstance(const G4String&,const G4String&)"
83  << G4endl;
84  return 0;
85  }
86 
87  if (instance==0) {
89  return instance;
90  }
91  else
92  return instance;
93 }
94 
95 
97  CCalMaterialTable::iterator ite;
98  for(ite = theCCalMaterials.begin(); ite != theCCalMaterials.end(); ite++ ){
99  delete *ite;
100  }
101  theCCalMaterials.clear();
102  CCalAMaterialTable::iterator itea;
103  for(itea = theCCalAMaterials.begin(); itea != theCCalAMaterials.end();
104  itea++ ){
105  delete *itea;
106  }
107  theCCalAMaterials.clear();
108 }
109 
110 
112  G4Material* theMat=findG4Material(mat);
113 
114  if (theMat) {
115 #ifdef ddebug
116  G4cout << "Material " << mat << " already defined. Returning previous "
117  << "instance." << G4endl;
118 #endif
119  return theMat;
120  } else {
121  CCalMaterial* CCalmat=findCCalMaterial(mat);
122  if (CCalmat){
123  G4Material* G4Mat = new G4Material(CCalmat->Name(),
124  CCalmat->Density()*g/cm3,
125  CCalmat->NElements());
126  for(G4int i=0; i<CCalmat->NElements(); i++) {
127  G4Element* elem = findElement(CCalmat->Element(i));
128  if (!elem) {
129  G4cerr << " Could not build material " << mat << "." << G4endl;
130  exit(-10);
131  }
132  G4Mat->AddElement(elem, CCalmat->Weight(i));
133  }
134 #ifdef ddebug
135  G4cout << "Material " << mat << " has been built successfully." << G4endl;
136 #endif
137  return G4Mat;
138  } else {
139  G4cerr << "ERROR: Material " << mat << " not found in CCal database!!!"
140  << G4endl;
141  return 0;
142  }
143  }
144 }
145 
146 
148  const G4ElementTable theElements = *(G4Element::GetElementTable());
149  for (unsigned int i=0; i<theElements.size(); i++)
150  if (theElements[i]->GetName()==mat){
151 #ifdef ddebug
152  G4cout << "Element " << mat << " found!" << G4endl;
153 #endif
154  return theElements[i];
155  }
156  return 0;
157 }
158 
159 
161  const G4String & symbol,
162  G4double Z, G4double A,
163  G4double density) {
164 
165  G4Element* theEl = new G4Element(name, symbol, Z, A*g/mole);
166  //Make it also as a material.
167  CCalAMaterial* theMat = new CCalAMaterial(name,A,density);
168  theCCalAMaterials.push_back(theMat);
169 
170 #ifdef ddebug
171  G4cout << "Element " << name << " created!" << G4endl;
172 #endif
173  return theEl;
174 }
175 
176 
179  G4int nconst,
180  G4String mats[],
181  G4double prop[],
182  MatDescription md){
183  addCCalMaterial(name, density, nconst, mats, prop, md);
184  return findMaterial(name);
185 }
186 
187 
189 
190  G4String path = getenv("CCAL_GLOBALPATH");
191  G4cout << " ==> Opening file " << matfile << " to read elements..." << G4endl;
192  std::ifstream is;
193  bool ok = openGeomFile(is, path, matfile);
194  if (!ok) {
195  G4cerr << "ERROR: Could not open file " << matfile << G4endl;
196  return;
197  }
198 
199  // Find *DO GMAT
200  findDO(is, G4String("GMAT"));
201 
202  readElements(is);
203 
204  is.close();
205 }
206 
207 
209 
210  G4String path = getenv("CCAL_GLOBALPATH");
211  G4cout << " ==> Opening file " << matfile << " to read materials..." << G4endl;
212  std::ifstream is;
213  bool ok = openGeomFile(is, path, matfile);
214  if (!ok) {
215  G4cerr << "ERROR: Could not open file " << matfile << G4endl;
216  return;
217  }
218 
219  // Find *DO GMIX
220  findDO(is, G4String("GMIX"));
221 
222  readMaterials(is);
223 
224  is.close();
225 }
226 
227 
228 //===========================================================================
229 // Protected & private methods ==============================================
230 
231 
233  const G4MaterialTable theG4Materials = *(G4Material::GetMaterialTable());
234  for (unsigned int i=0; i<theG4Materials.size(); i++) {
235  if (theG4Materials[i]->GetName()==mat){
236  return theG4Materials[i];
237  }
238  }
239  return 0;
240 }
241 
242 
244  const {
245  for (unsigned int i=0; i<theCCalMaterials.size(); i++)
246  if (theCCalMaterials[i]->Name()==mat){
247 #ifdef ddebug
248  G4cout << "CCalMaterial " << mat << " found!" << G4endl;
249 #endif
250  return theCCalMaterials[i];
251  }
252  return (CCalMaterial*) findCCalAMaterial(mat);
253 }
254 
255 
257  const {
258  for (unsigned int i=0; i<theCCalAMaterials.size(); i++)
259  if (theCCalAMaterials[i]->Name()==mat){
260 #ifdef ddebug
261  G4cout << "CCalMaterial " << mat << " found!" << G4endl;
262 #endif
263  return theCCalAMaterials[i];
264  }
265  return 0;
266 }
267 
268 
271  G4int nconst,
272  G4String mats[],
273  G4double prop[],
274  MatDescription md){
275  ptrCCalMaterial* matcol=0;
276  ptrCCalAMaterial* amatcol=0;
277 
278  if (md==byAtomic)
279  amatcol = new ptrCCalAMaterial[nconst];
280  else
281  matcol = new ptrCCalMaterial[nconst];
282 
283  for (G4int i=0; i<nconst; i++){
284  if (md==byAtomic) {
285  CCalAMaterial* amat = findCCalAMaterial(mats[i]);
286  if (amat)
287  amatcol[i]=amat;
288  else {
289  G4cerr << "ERROR: Trying to build" << name << " out of unknown "
290  << mats[i] << "." << G4endl
291  << "Skiping this material!" << G4endl;
292  delete[] amatcol;
293  return 0;
294  }
295  } //by Atomic fractions
296  else {
297  CCalMaterial* mat = findCCalMaterial(mats[i]);
298  if (mat)
299  matcol[i]=mat;
300  else {
301  G4cerr << "ERROR: Trying to build" <<name << " out of unknown "
302  << mats[i] << "." << G4endl
303  << "Skiping this material!" << G4endl;
304  delete[] matcol;
305  return 0;
306  }
307  }
308  } //for
309 
310  //Let's do the CCalMaterial!
311  if (md==byAtomic) {
312  CCalAMaterial* amaterial = new CCalAMaterial(name, density, nconst,
313  amatcol, prop);
314  delete[] amatcol;
315  theCCalAMaterials.push_back(amaterial);
316 #ifdef ddebug
317  G4cout << *amaterial << G4endl;
318 #endif
319  return amaterial;
320  } else {
322  if (md == byWeight)
324  else
326  CCalMaterial* material = new CCalMaterial(name, density, nconst,
327  matcol, prop, ft);
328  delete[] matcol;
329  theCCalMaterials.push_back(material);
330 #ifdef ddebug
331  G4cout << *material << G4endl;
332 #endif
333  return material;
334  }
335 }
336 
337 
338 void CCalMaterialFactory::readElements(std::ifstream& is){
340 
341  G4cout << " ==> Reading elements... " << G4endl;
342 #ifdef debug
343  G4cout << " Element \tsymbol\tA\tZ\tdensity\tX_0 abs_l"<< G4endl;
344 #endif
345  //There should come the list of materials. #. Defines a comment
346  //*DO defines the beguining of the Mixes block.
347 
348  readName(is,name);
349  while (name != "*ENDDO") {
350  //It should be an element definition
351  G4double A, Z, density;
352  is >> symbol >> A >> Z >> density >> jump;
353 #ifdef debug
354  G4cout << " " << name << " \t" << symbol << "\t"
355  << A << "\t" << Z << "\t" << density << G4endl;
356 #endif
357  addElement(name, symbol, Z, A, density);
358  readName(is,name);
359  };
360  G4cout << " " << G4Element::GetElementTable()->size()
361  << " elements read from file" << G4endl << G4endl;
362 }
363 
364 
365 void CCalMaterialFactory::readMaterials(std::ifstream& is){
366  G4String name, matname;
367 
368  G4cout << " ==> Reading materials... " << G4endl;
369 
370  //Take into account the special case of vacuum...
371 #ifdef debug
372  G4cout <<" \"Vacuum\"" << G4endl;
373 #endif
374  G4double density = universe_mean_density; //from PhysicalConstants.h
375  G4double pressure = 1.E-19*pascal;
376  G4double temperature = 0.1*kelvin;
377  new G4Material("Vacuum", /*Z=*/ 1., /*A=*/ 1.01*g/mole,
378  density, kStateGas, temperature, pressure);
379 
380  //There should come the list of materials. #. Defines a comment
381  //*ENDDO defines the block.
382  readName(is,name);
383  while (name != "*ENDDO") {
384  //It should be a material definition
385  matname=name;
386  G4int nElem;
387  G4double dens;
388  is >> nElem >> dens >> jump;
389 
390 #ifdef debug
391  G4cout <<" " << matname
392  << " made of " << nElem
393  << " elements. Density=" << dens
394  << G4endl;
395 #endif
396 
397  G4int absnelem = std::abs(nElem);
398 
399  G4String* mats = new G4String[absnelem];
400  G4double* weights = new G4double[absnelem];
401 
402  G4double prop;
403  for(int i=0; i<absnelem; i++) {
404  readName(is, name);
405  is >> prop >> jump;
406  mats[i]=name;
407  weights[i]=std::abs(prop);
408  } //for...
409  MatDescription md;
410  if (nElem>0 && prop<0)
411  md = byAtomic;
412  else if (nElem>0)
413  md = byWeight;
414  else
415  md = byVolume;
416 
417  addCCalMaterial(matname, dens, absnelem, mats, weights, md);
418  delete[] mats;
419  delete[] weights;
420 
421  readName(is,name);
422  }; //while
423 
424  G4cout << " " << theCCalMaterials.size() << " materials read from "
425  << mixturefile << G4endl << G4endl;
426 }
427 
428 
432 }
static G4String mixturefile
void readElements(const G4String &)
int NElements() const
Definition: CCalMaterial.hh:53
CCalMaterial * ptrCCalMaterial
G4String symbol
Definition: TRTMaterials.hh:40
double Weight(int i) const
Definition: CCalMaterial.hh:55
bool openGeomFile(std::ifstream &is, const G4String &pathname, const G4String &filename)
Definition: CCalutils.cc:116
CCalAMaterial * ptrCCalAMaterial
G4String name
Definition: TRTMaterials.hh:40
G4String Name() const
Definition: CCalMaterial.hh:51
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:564
std::vector< G4Material * > G4MaterialTable
static CCalMaterialFactory * instance
G4Element * addElement(const G4String &, const G4String &, G4double, G4double, G4double)
std::istream & jump(std::istream &)
Definition: CCalutils.cc:95
std::ifstream & findDO(std::ifstream &, const G4String &)
Definition: CCalutils.cc:72
int G4int
Definition: G4Types.hh:78
CCalAMaterialTable theCCalAMaterials
G4double density
Definition: TRTMaterials.hh:39
std::ifstream & readName(std::ifstream &, G4String &)
Definition: CCalutils.cc:53
G4GLOB_DLL std::ostream G4cout
G4Material * addMaterial(const G4String &nam, G4double density, G4int nconst, G4String mats[], G4double prop[], MatDescription md=byWeight)
G4Material * findMaterial(const G4String &) const
static const double cm3
Definition: G4SIunits.hh:108
double Density() const
Definition: CCalMaterial.hh:52
static G4String elementfile
CCalMaterial * addCCalMaterial(const G4String &nam, G4double density, G4int nconst, G4String mats[], G4double prop[], MatDescription md=byWeight)
static const G4double A[nN]
static const double kelvin
Definition: G4SIunits.hh:260
CCalAMaterial * findCCalAMaterial(const G4String &) const
#define pascal
Definition: G4SIunits.hh:213
CCalMaterialTable theCCalMaterials
G4Material * findG4Material(const G4String &) const
G4String Element(int i) const
Definition: CCalMaterial.hh:54
static const double g
Definition: G4SIunits.hh:162
void readMaterials(const G4String &)
static const double mole
Definition: G4SIunits.hh:265
#define G4endl
Definition: G4ios.hh:61
void AddElement(G4Element *element, G4int nAtoms)
Definition: G4Material.cc:345
static CCalMaterialFactory * getInstance()
std::vector< G4Element * > G4ElementTable
double G4double
Definition: G4Types.hh:76
static G4ElementTable * GetElementTable()
Definition: G4Element.cc:395
CCalMaterial * findCCalMaterial(const G4String &) const
G4Element * findElement(const G4String &) const
G4GLOB_DLL std::ostream G4cerr