Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4GDMLWriteMaterials.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: G4GDMLWriteMaterials.cc 70764 2013-06-05 12:54:37Z gcosmo $
28 //
29 // class G4GDMLWriteMaterials Implementation
30 //
31 // Original author: Zoltan Torzsok, November 2007
32 //
33 // --------------------------------------------------------------------
34 
35 #include <sstream>
36 
37 #include "G4GDMLWriteMaterials.hh"
38 
39 #include "G4PhysicalConstants.hh"
40 #include "G4SystemOfUnits.hh"
41 #include "G4Element.hh"
42 #include "G4Isotope.hh"
43 #include "G4Material.hh"
44 
46  : G4GDMLWriteDefine(), materialsElement(0)
47 {
48 }
49 
51 {
52 }
53 
55 AtomWrite(xercesc::DOMElement* element,const G4double& a)
56 {
57  xercesc::DOMElement* atomElement = NewElement("atom");
58  atomElement->setAttributeNode(NewAttribute("unit","g/mole"));
59  atomElement->setAttributeNode(NewAttribute("value",a*mole/g));
60  element->appendChild(atomElement);
61 }
62 
64 DWrite(xercesc::DOMElement* element,const G4double& d)
65 {
66  xercesc::DOMElement* DElement = NewElement("D");
67  DElement->setAttributeNode(NewAttribute("unit","g/cm3"));
68  DElement->setAttributeNode(NewAttribute("value",d*cm3/g));
69  element->appendChild(DElement);
70 }
71 
73 PWrite(xercesc::DOMElement* element,const G4double& P)
74 {
75  xercesc::DOMElement* PElement = NewElement("P");
76  PElement->setAttributeNode(NewAttribute("unit","pascal"));
77  PElement->setAttributeNode(NewAttribute("value",P/hep_pascal));
78  element->appendChild(PElement);
79 }
80 
82 TWrite(xercesc::DOMElement* element,const G4double& T)
83 {
84  xercesc::DOMElement* TElement = NewElement("T");
85  TElement->setAttributeNode(NewAttribute("unit","K"));
86  TElement->setAttributeNode(NewAttribute("value",T/kelvin));
87  element->appendChild(TElement);
88 }
89 
91 MEEWrite(xercesc::DOMElement* element,const G4double& MEE)
92 {
93  xercesc::DOMElement* PElement = NewElement("MEE");
94  PElement->setAttributeNode(NewAttribute("unit","eV"));
95  PElement->setAttributeNode(NewAttribute("value",MEE/electronvolt));
96  element->appendChild(PElement);
97 }
98 
100 IsotopeWrite(const G4Isotope* const isotopePtr)
101 {
102  const G4String name = GenerateName(isotopePtr->GetName(),isotopePtr);
103 
104  xercesc::DOMElement* isotopeElement = NewElement("isotope");
105  isotopeElement->setAttributeNode(NewAttribute("name",name));
106  isotopeElement->setAttributeNode(NewAttribute("N",isotopePtr->GetN()));
107  isotopeElement->setAttributeNode(NewAttribute("Z",isotopePtr->GetZ()));
108  materialsElement->appendChild(isotopeElement);
109  AtomWrite(isotopeElement,isotopePtr->GetA());
110 }
111 
112 void G4GDMLWriteMaterials::ElementWrite(const G4Element* const elementPtr)
113 {
114  const G4String name = GenerateName(elementPtr->GetName(),elementPtr);
115 
116  xercesc::DOMElement* elementElement = NewElement("element");
117  elementElement->setAttributeNode(NewAttribute("name",name));
118 
119  const size_t NumberOfIsotopes = elementPtr->GetNumberOfIsotopes();
120 
121  if (NumberOfIsotopes>0)
122  {
123  const G4double* RelativeAbundanceVector =
124  elementPtr->GetRelativeAbundanceVector();
125  for (size_t i=0;i<NumberOfIsotopes;i++)
126  {
127  G4String fractionref = GenerateName(elementPtr->GetIsotope(i)->GetName(),
128  elementPtr->GetIsotope(i));
129  xercesc::DOMElement* fractionElement = NewElement("fraction");
130  fractionElement->setAttributeNode(NewAttribute("n",
131  RelativeAbundanceVector[i]));
132  fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
133  elementElement->appendChild(fractionElement);
134  AddIsotope(elementPtr->GetIsotope(i));
135  }
136  }
137  else
138  {
139  elementElement->setAttributeNode(NewAttribute("Z",elementPtr->GetZ()));
140  AtomWrite(elementElement,elementPtr->GetA());
141  }
142 
143  materialsElement->appendChild(elementElement);
144  // Append the element AFTER all the possible components are appended!
145 }
146 
147 void G4GDMLWriteMaterials::MaterialWrite(const G4Material* const materialPtr)
148 {
149  G4String state_str("undefined");
150  const G4State state = materialPtr->GetState();
151  if (state==kStateSolid) { state_str = "solid"; } else
152  if (state==kStateLiquid) { state_str = "liquid"; } else
153  if (state==kStateGas) { state_str = "gas"; }
154 
155  const G4String name = GenerateName(materialPtr->GetName(), materialPtr);
156 
157  xercesc::DOMElement* materialElement = NewElement("material");
158  materialElement->setAttributeNode(NewAttribute("name",name));
159  materialElement->setAttributeNode(NewAttribute("state",state_str));
160 
161  // Write any property attached to the material...
162  //
163  if (materialPtr->GetMaterialPropertiesTable())
164  {
165  PropertyWrite(materialElement, materialPtr);
166  }
167 
168  if (materialPtr->GetTemperature() != STP_Temperature)
169  { TWrite(materialElement,materialPtr->GetTemperature()); }
170  if (materialPtr->GetPressure() != STP_Pressure)
171  { PWrite(materialElement,materialPtr->GetPressure()); }
172 
173  // Write Ionisation potential (mean excitation energy)
174  MEEWrite(materialElement,materialPtr->GetIonisation()->GetMeanExcitationEnergy());
175 
176  DWrite(materialElement,materialPtr->GetDensity());
177 
178  const size_t NumberOfElements = materialPtr->GetNumberOfElements();
179 
180  if ( (NumberOfElements>1)
181  || ( materialPtr->GetElement(0)
182  && materialPtr->GetElement(0)->GetNumberOfIsotopes()>1 ) )
183  {
184  const G4double* MassFractionVector = materialPtr->GetFractionVector();
185 
186  for (size_t i=0;i<NumberOfElements;i++)
187  {
188  const G4String fractionref =
189  GenerateName(materialPtr->GetElement(i)->GetName(),
190  materialPtr->GetElement(i));
191  xercesc::DOMElement* fractionElement = NewElement("fraction");
192  fractionElement->setAttributeNode(NewAttribute("n",
193  MassFractionVector[i]));
194  fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
195  materialElement->appendChild(fractionElement);
196  AddElement(materialPtr->GetElement(i));
197  }
198  }
199  else
200  {
201  materialElement->setAttributeNode(NewAttribute("Z",materialPtr->GetZ()));
202  AtomWrite(materialElement,materialPtr->GetA());
203  }
204 
205  // Append the material AFTER all the possible components are appended!
206  //
207  materialsElement->appendChild(materialElement);
208 }
209 
211  const G4PhysicsOrderedFreeVector* const pvec)
212 {
213  const G4String matrixref = GenerateName(key, pvec);
214  xercesc::DOMElement* matrixElement = NewElement("matrix");
215  matrixElement->setAttributeNode(NewAttribute("name", matrixref));
216  matrixElement->setAttributeNode(NewAttribute("coldim", "2"));
217  std::ostringstream pvalues;
218  for (size_t i=0; i<pvec->GetVectorLength(); i++)
219  {
220  if (i!=0) { pvalues << " "; }
221  pvalues << pvec->Energy(i) << " " << (*pvec)[i];
222  }
223  matrixElement->setAttributeNode(NewAttribute("values", pvalues.str()));
224 
225  defineElement->appendChild(matrixElement);
226 }
227 
228 void G4GDMLWriteMaterials::PropertyWrite(xercesc::DOMElement* matElement,
229  const G4Material* const mat)
230 {
231  xercesc::DOMElement* propElement;
233  const std::map< G4String, G4PhysicsOrderedFreeVector*,
234  std::less<G4String> >* pmap = ptable->GetPropertiesMap();
235  const std::map< G4String, G4double,
236  std::less<G4String> >* cmap = ptable->GetPropertiesCMap();
237  std::map< G4String, G4PhysicsOrderedFreeVector*,
238  std::less<G4String> >::const_iterator mpos;
239  std::map< G4String, G4double,
240  std::less<G4String> >::const_iterator cpos;
241  for (mpos=pmap->begin(); mpos!=pmap->end(); mpos++)
242  {
243  propElement = NewElement("property");
244  propElement->setAttributeNode(NewAttribute("name", mpos->first));
245  propElement->setAttributeNode(NewAttribute("ref",
246  GenerateName(mpos->first, mpos->second)));
247  if (mpos->second)
248  {
249  PropertyVectorWrite(mpos->first, mpos->second);
250  matElement->appendChild(propElement);
251  }
252  else
253  {
254  G4String warn_message = "Null pointer for material property -"
255  + mpos->first + "- of material -" + mat->GetName() + "- !";
256  G4Exception("G4GDMLWriteMaterials::PropertyWrite()", "NullPointer",
257  JustWarning, warn_message);
258  continue;
259  }
260  }
261  for (cpos=cmap->begin(); cpos!=cmap->end(); cpos++)
262  {
263  propElement = NewElement("property");
264  propElement->setAttributeNode(NewAttribute("name", cpos->first));
265  propElement->setAttributeNode(NewAttribute("ref", cpos->first));
266  xercesc::DOMElement* constElement = NewElement("constant");
267  constElement->setAttributeNode(NewAttribute("name", cpos->first));
268  constElement->setAttributeNode(NewAttribute("value", cpos->second));
269  defineElement->appendChild(constElement);
270  matElement->appendChild(propElement);
271  }
272 }
273 
274 void G4GDMLWriteMaterials::MaterialsWrite(xercesc::DOMElement* element)
275 {
276  G4cout << "G4GDML: Writing materials..." << G4endl;
277 
278  materialsElement = NewElement("materials");
279  element->appendChild(materialsElement);
280 
281  isotopeList.clear();
282  elementList.clear();
283  materialList.clear();
284 }
285 
286 void G4GDMLWriteMaterials::AddIsotope(const G4Isotope* const isotopePtr)
287 {
288  for (size_t i=0; i<isotopeList.size(); i++) // Check if isotope is
289  { // already in the list!
290  if (isotopeList[i] == isotopePtr) { return; }
291  }
292  isotopeList.push_back(isotopePtr);
293  IsotopeWrite(isotopePtr);
294 }
295 
296 void G4GDMLWriteMaterials::AddElement(const G4Element* const elementPtr)
297 {
298  for (size_t i=0;i<elementList.size();i++) // Check if element is
299  { // already in the list!
300  if (elementList[i] == elementPtr) { return; }
301  }
302  elementList.push_back(elementPtr);
303  ElementWrite(elementPtr);
304 }
305 
306 void G4GDMLWriteMaterials::AddMaterial(const G4Material* const materialPtr)
307 {
308  for (size_t i=0;i<materialList.size();i++) // Check if material is
309  { // already in the list!
310  if (materialList[i] == materialPtr) { return; }
311  }
312  materialList.push_back(materialPtr);
313  MaterialWrite(materialPtr);
314 }
G4double GetPressure() const
Definition: G4Material.hh:183
size_t GetNumberOfIsotopes() const
Definition: G4Element.hh:159
const XML_Char * name
Definition: expat.h:151
G4IonisParamMat * GetIonisation() const
Definition: G4Material.hh:226
G4double GetA() const
Definition: G4Isotope.hh:97
G4double GetZ() const
Definition: G4Material.cc:623
void MEEWrite(xercesc::DOMElement *, const G4double &)
const G4String & GetName() const
Definition: G4Isotope.hh:88
static constexpr double hep_pascal
Definition: G4SIunits.hh:235
void DWrite(xercesc::DOMElement *, const G4double &)
G4State
Definition: G4Material.hh:114
const G4String & GetName() const
Definition: G4Material.hh:178
xercesc::DOMElement * materialsElement
G4double GetZ() const
Definition: G4Element.hh:131
G4double GetDensity() const
Definition: G4Material.hh:180
void AddMaterial(const G4Material *const)
xercesc::DOMElement * defineElement
static constexpr double STP_Pressure
size_t GetVectorLength() const
const std::map< G4String, G4MaterialPropertyVector *, std::less< G4String > > * GetPropertiesMap() const
static constexpr double g
Definition: G4SIunits.hh:183
const G4Element * GetElement(G4int iel) const
Definition: G4Material.hh:202
xercesc::DOMElement * NewElement(const G4String &)
Definition: G4GDMLWrite.cc:160
G4double GetA() const
Definition: G4Element.hh:139
void TWrite(xercesc::DOMElement *, const G4double &)
const std::map< G4String, G4double, std::less< G4String > > * GetPropertiesCMap() const
static constexpr double electronvolt
Definition: G4SIunits.hh:206
static double P[]
static constexpr double STP_Temperature
G4int GetN() const
Definition: G4Isotope.hh:94
std::vector< const G4Element * > elementList
G4GLOB_DLL std::ostream G4cout
void PWrite(xercesc::DOMElement *, const G4double &)
G4double * GetRelativeAbundanceVector() const
Definition: G4Element.hh:167
G4String GenerateName(const G4String &, const void *const)
Definition: G4GDMLWrite.cc:123
G4double Energy(size_t index) const
static constexpr double kelvin
Definition: G4SIunits.hh:281
static constexpr double cm3
Definition: G4SIunits.hh:121
void IsotopeWrite(const G4Isotope *const)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void ElementWrite(const G4Element *const)
G4double GetA() const
Definition: G4Material.cc:636
G4int GetZ() const
Definition: G4Isotope.hh:91
void PropertyVectorWrite(const G4String &, const G4PhysicsOrderedFreeVector *const)
virtual void MaterialsWrite(xercesc::DOMElement *)
xercesc::DOMAttr * NewAttribute(const G4String &, const G4String &)
Definition: G4GDMLWrite.cc:136
void AddElement(const G4Element *const)
G4MaterialPropertiesTable * GetMaterialPropertiesTable() const
Definition: G4Material.hh:252
void AtomWrite(xercesc::DOMElement *, const G4double &)
const G4Isotope * GetIsotope(G4int iso) const
Definition: G4Element.hh:170
G4double GetMeanExcitationEnergy() const
G4double GetTemperature() const
Definition: G4Material.hh:182
#define G4endl
Definition: G4ios.hh:61
size_t GetNumberOfElements() const
Definition: G4Material.hh:186
G4State GetState() const
Definition: G4Material.hh:181
double G4double
Definition: G4Types.hh:76
const G4String & GetName() const
Definition: G4Element.hh:127
std::vector< const G4Material * > materialList
void AddIsotope(const G4Isotope *const)
std::vector< const G4Isotope * > isotopeList
const G4double * GetFractionVector() const
Definition: G4Material.hh:194
static constexpr double mole
Definition: G4SIunits.hh:286
void PropertyWrite(xercesc::DOMElement *, const G4Material *const)
void MaterialWrite(const G4Material *const)