Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exrdmMaterial.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 //
28 //
30 //
31 #include "exrdmMaterial.hh"
32 #include "exrdmMaterialData.hh"
34 
35 #include "globals.hh"
36 #include "G4UnitsTable.hh"
37 #include "G4PhysicalConstants.hh"
38 #include "G4SystemOfUnits.hh"
39 #include "G4ios.hh"
40 #include <vector>
41 #include <iomanip>
43 //
45 {
46  fMaterial.clear();
47  fElement.clear();
48  fIsotope.clear();
49  // some default materials vacuum (0), air (1) and aluminium (2) defined here
50  // examples of vacuum
51  //
52  // G4double a,z;
53 
54  static G4bool bmat = false ;
55 
56  if (!bmat) {
57 
58  // vacuum
59  G4double density = universe_mean_density; //from PhysicalConstants.h
60  G4double pressure = 3.e-18*pascal;
61  G4double temperature = 2.73*kelvin;
62  AddMaterial("Vacuum", "H", density,"gas",temperature,pressure);
63  // air
64  density = 1.290*mg/cm3;
65  AddMaterial("Air", "N0.78-O0.22", density, "gas");
66 
67  // aluminium
68  density=2.700*g/cm3 ;
69  AddMaterial ("Aluminium", "Al", density,"");
70 
71  //silicon
72  density=2.3290*g/cm3 ;
73  AddMaterial ("Silicon", "Si", density,"");
74 
75  bmat = true;
76  }
77  // create commands for interactive definition of the geometry
78  fMaterialMessenger = new exrdmMaterialMessenger(this);
79 }
81 //
83 {
84  delete fMaterialMessenger;
85 }
87 //
89  G4double density, G4String state, G4double tem, G4double pres)
90 {
91  G4int isotope, Z;
92  size_t i;
93  for (i = 0; i<fMaterial.size(); i++) {
94  if (fMaterial[i]->GetName() == name) {
95  G4cerr <<" AddMaterial : material " <<name
96  <<" already exists." <<G4endl;
97  G4cerr <<"--> Command rejected." <<G4endl;
98  return;
99  }
100  }
101 
102  char *tokenPtr1 = NULL;
103  char *sname = NULL;
104  G4String s0, s1("0123456789");
105  G4String element, isotopename;
106  G4int ncomponents, natoms;
107  G4double fatoms = 0.;
108  size_t ls, id=0, ll, lr;
109  ncomponents = 0;
110 
111  sname = new char[strlen(formula)+1];
112  strcpy(sname,formula);
113  tokenPtr1 = strtok(sname,"-");
114 
115  while (tokenPtr1 != NULL) {
116  ncomponents++;
117  tokenPtr1 = strtok( NULL, "-");
118  }
119  delete[] sname;
120 
121  G4Material* aMaterial = 0;
122  G4cout << name <<" "<< formula << " " << density/(g/cm3) << " " << tem <<" "
123  <<pres << G4endl;
124 
125  if (state == "") {
126  aMaterial = new G4Material(name, density, ncomponents);
127  } else if (state == "solid" && tem > 0.) {
128  aMaterial = new G4Material(name, density, ncomponents,
129  kStateSolid, tem );
130  } else if (state == "gas" && pres > 0.) {
131  aMaterial = new G4Material(name, density, ncomponents,
132  kStateGas, tem, pres );
133  }
134  if (aMaterial == 0) {
135  G4cerr <<" AddMaterial : Name " <<name <<"." <<G4endl;
136  G4cerr <<"--> Command failed." <<G4endl;
137  return;
138  }
139 
140  sname=new char[strlen(formula)+1];
141  strcpy(sname,formula);
142  tokenPtr1 = strtok(sname,"-");
143 
144  while (tokenPtr1 != NULL) {
145  isotope = 0;
146  // G4cout << tokenPtr1 << G4endl;
147  s0 = G4String(tokenPtr1);
148  ls = s0.length();
149  ll = s0.find("(");
150  lr = s0.find(")");
151  if (ll == lr) {
152  id = s0.find_first_of(s1);
153  element = s0.substr(0,id);
154 
155  if (element.length() == 1) element.insert(0," ");
156  for (i = 0; i<110; i++) {
157  if (element == fELU[i]) break;
158  }
159  if (i == 110) {
160  for (i = 0; i<110; i++) {
161  if (element == fELL[i]) break;
162  }
163  if (i == 110) {
164  for (i = 0; i<110; i++) {
165  if (element == fEUU[i]) break;
166  }
167  }
168  }
169 
170  if (i == 110) {
171  G4cerr <<"AddMaterial : Invalid element in material formula."
172  <<element <<G4endl;
173  G4cerr <<"--> Command rejected." <<G4endl;
174 // delete aMaterial;
175 // fMaterial[NbMat] = NULL;
176  return;
177  }
178 
179  Z = i+1;
180  element = fELU[i];
181  if (id == std::string::npos) {
182  natoms = 1;
183  } else {
184  natoms = atoi((s0.substr(id,ls-id)).c_str());
185  }
186  if (natoms < 1) fatoms = atof((s0.substr(id,ls-id)).c_str());
187  // G4cout << " Elements = " << element << G4endl;
188  //G4cout << " Nb of atoms = " << natoms << G4endl;
189  } else {
190  element = s0.substr(0,ll);
191  isotope = atoi((s0.substr(ll+1,lr-ll)).c_str());
192  if (element.length() == 1) element.insert(0," ");
193  for (i = 0; i<110; i++) {
194  if (element == fELU[i]) break;
195  }
196  if (i == 110) {
197  for (i = 0; i<110; i++) {
198  if (element == fELL[i]) break;
199  }
200  if (i == 110) {
201  for (i = 0; i<110; i++) {
202  if (element == fEUU[i]) break;
203  }
204  }
205  }
206  if (i == 110) {
207  G4cerr <<"AddMaterial : Invalid element in material formula."
208  <<element <<G4endl;
209  G4cerr <<"--> Command rejected." <<G4endl;
210 // delete aMaterial;
211 // fMaterial[NbMat] = NULL;
212  return;
213  }
214 
215  Z = i+1;
216  element = fELU[i];
217  isotopename = element+s0.substr(ll+1,lr-ll-1);
218  if (lr == std::string::npos ) {
219  natoms = 1;
220  } else {
221  natoms = atoi((s0.substr(lr+1,ls-lr)).c_str());
222  }
223  if (natoms < 1) fatoms = atof((s0.substr(id,ls-id)).c_str());
224  if (fatoms == 0.) natoms = 1;
225  //
226  // G4cout << " Elements = " << element << G4endl;
227  // G4cout << " fIsotope Nb = " << isotope << G4endl;
228  // G4cout << " Nb of atoms = " << natoms << G4endl;
229  }
230  if (isotope != 0) {
231  if (G4Isotope::GetIsotope(isotopename) == NULL) {
232  G4Isotope* aIsotope = new G4Isotope(isotopename, Z, isotope, isotope*g/mole);
233  G4Element* aElement = new G4Element(isotopename, element, 1);
234  aElement->AddIsotope(aIsotope, 100.*perCent);
235  fIsotope.push_back(aIsotope);
236  if (natoms>0) {
237  aMaterial->AddElement(aElement, natoms);
238  } else {
239  aMaterial->AddElement(aElement, fatoms);
240  }
241  fElement.push_back(aElement);
242  } else {
243  if (natoms>0) {
244  aMaterial->AddElement( G4Element::GetElement(isotopename,false) , natoms);
245  } else {
246  aMaterial->AddElement( G4Element::GetElement(isotopename,false) , fatoms);
247  }
248  }
249  } else {
250  if ( G4Element::GetElement(element,false) == NULL) {
251  G4Element* aElement = new G4Element(element, element, Z, fA[Z-1]*g/mole);
252  if (natoms>0) {
253  aMaterial->AddElement(aElement, natoms);
254  } else {
255  aMaterial->AddElement(aElement, fatoms);
256  }
257  fElement.push_back(aElement);
258  } else {
259  if (natoms>0) {
260  aMaterial->AddElement( G4Element::GetElement(element,false) , natoms);
261  } else {
262  aMaterial->AddElement( G4Element::GetElement(element,false) , fatoms);
263  }
264  }
265  }
266  tokenPtr1 = strtok( NULL, "-");
267  // s0.empty();
268  //element.erase();
269  //
270  }
271 
272  delete[] sname;
273  fMaterial.push_back(aMaterial);
274  G4cout <<" fMaterial:" <<name <<" with formula: " <<formula <<" added! "
275  <<G4endl;
276  G4cout <<" Nb of fMaterial = " <<fMaterial.size() <<G4endl;
277  G4cout <<" Nb of fIsotope = " <<fIsotope.size() <<G4endl;
278  G4cout <<" Nb of fElement = " <<fElement.size() <<G4endl;
279 }
281 //
283 {
284  size_t i(j-1);
285  if (i > fMaterial.size()) {
286  G4cerr <<"DeleteMaterial : Invalid material index " <<j <<"." <<G4endl;
287  G4cerr <<"--> Command rejected." <<G4endl;
288  } else {
289  G4cerr <<"It seems there is no mechanism in G4 for deleting a material yet!"
290  <<G4endl;
291  G4cerr <<"--> Command rejected." <<G4endl;
292  }
293 }
295 //
297 {
298  G4cerr <<"It seems there is no mechanism in G4 for deleting a material yet!"
299  <<G4endl;
300  G4cerr <<"--> Command rejected." <<G4endl;
301 }
303 //
305 {
306  size_t i ;
307  for (i = 0; i < fMaterial.size(); i++) {
308  if (fMaterial[i]->GetName() == name) break;
309  }
310  G4int k = G4int(i);
311  if (i == fMaterial.size()) k = -1;
312  return k;
313 }
315 //
317 {
318  G4cout <<" There are" <<std::setw(3) <<fMaterial.size()
319  <<" materials defined." <<G4endl;
320  for (size_t i = 0; i< fMaterial.size(); i++)
321  G4cout <<" fMaterial Index " <<std::setw(3) <<i+1 <<" "
322  <<std::setw(14) <<fMaterial[i]->GetName()
323  <<" density: " <<std::setw(6) <<std::setprecision(3)
324  <<G4BestUnit(fMaterial[i]->GetDensity(),"Volumic Mass") <<G4endl;
325  G4cout <<G4endl;
326 
327 }