source: trunk/source/persistency/gdml/src/G4GDMLWriteMaterials.cc @ 1347

Last change on this file since 1347 was 1347, checked in by garnier, 13 years ago

geant4 tag 9.4

File size: 11.0 KB
Line 
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,v 1.26 2010/10/14 16:19:40 gcosmo Exp $
28// GEANT4 tag $Name: gdml-V09-03-09 $
29//
30// class G4GDMLWriteMaterials Implementation
31//
32// Original author: Zoltan Torzsok, November 2007
33//
34// --------------------------------------------------------------------
35
36#include <sstream>
37#include "G4GDMLWriteMaterials.hh"
38
39#include "G4Element.hh"
40#include "G4Isotope.hh"
41#include "G4Material.hh"
42
43G4GDMLWriteMaterials::G4GDMLWriteMaterials()
44  : G4GDMLWriteDefine(), materialsElement(0)
45{
46}
47
48G4GDMLWriteMaterials::~G4GDMLWriteMaterials()
49{
50}
51
52void G4GDMLWriteMaterials::
53AtomWrite(xercesc::DOMElement* element,const G4double& a)
54{
55   xercesc::DOMElement* atomElement = NewElement("atom");
56   atomElement->setAttributeNode(NewAttribute("unit","g/mole"));
57   atomElement->setAttributeNode(NewAttribute("value",a*mole/g));
58   element->appendChild(atomElement);
59}
60
61void G4GDMLWriteMaterials::
62DWrite(xercesc::DOMElement* element,const G4double& d)
63{
64   xercesc::DOMElement* DElement = NewElement("D");
65   DElement->setAttributeNode(NewAttribute("unit","g/cm3"));
66   DElement->setAttributeNode(NewAttribute("value",d*cm3/g));
67   element->appendChild(DElement);
68}
69
70void G4GDMLWriteMaterials::
71PWrite(xercesc::DOMElement* element,const G4double& P)
72{
73   xercesc::DOMElement* PElement = NewElement("P");
74   PElement->setAttributeNode(NewAttribute("unit","pascal"));
75   PElement->setAttributeNode(NewAttribute("value",P/pascal));
76   element->appendChild(PElement);
77}
78
79void G4GDMLWriteMaterials::
80TWrite(xercesc::DOMElement* element,const G4double& T)
81{
82   xercesc::DOMElement* TElement = NewElement("T");
83   TElement->setAttributeNode(NewAttribute("unit","K"));
84   TElement->setAttributeNode(NewAttribute("value",T/kelvin));
85   element->appendChild(TElement);
86}
87
88void G4GDMLWriteMaterials::
89IsotopeWrite(const G4Isotope* const isotopePtr)
90{
91   const G4String name = GenerateName(isotopePtr->GetName(),isotopePtr);
92
93   xercesc::DOMElement* isotopeElement = NewElement("isotope");
94   isotopeElement->setAttributeNode(NewAttribute("name",name));
95   isotopeElement->setAttributeNode(NewAttribute("N",isotopePtr->GetN()));
96   isotopeElement->setAttributeNode(NewAttribute("Z",isotopePtr->GetZ()));
97   materialsElement->appendChild(isotopeElement);
98   AtomWrite(isotopeElement,isotopePtr->GetA());
99}
100
101void G4GDMLWriteMaterials::ElementWrite(const G4Element* const elementPtr)
102{
103   const G4String name = GenerateName(elementPtr->GetName(),elementPtr);
104
105   xercesc::DOMElement* elementElement = NewElement("element");
106   elementElement->setAttributeNode(NewAttribute("name",name));
107
108   const size_t NumberOfIsotopes = elementPtr->GetNumberOfIsotopes();
109
110   if (NumberOfIsotopes>0)
111   {
112      const G4double* RelativeAbundanceVector =
113            elementPtr->GetRelativeAbundanceVector();             
114      for (size_t i=0;i<NumberOfIsotopes;i++)
115      {
116         G4String fractionref = GenerateName(elementPtr->GetIsotope(i)->GetName(),
117                                             elementPtr->GetIsotope(i));
118         xercesc::DOMElement* fractionElement = NewElement("fraction");
119         fractionElement->setAttributeNode(NewAttribute("n",
120                                           RelativeAbundanceVector[i]));
121         fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
122         elementElement->appendChild(fractionElement);
123         AddIsotope(elementPtr->GetIsotope(i));
124      }
125   }
126   else
127   {
128      elementElement->setAttributeNode(NewAttribute("Z",elementPtr->GetZ()));
129      AtomWrite(elementElement,elementPtr->GetA());
130   }
131
132   materialsElement->appendChild(elementElement);
133     // Append the element AFTER all the possible components are appended!
134}
135
136void G4GDMLWriteMaterials::MaterialWrite(const G4Material* const materialPtr)
137{
138   G4String state_str("undefined");
139   const G4State state = materialPtr->GetState();
140   if (state==kStateSolid) { state_str = "solid"; } else
141   if (state==kStateLiquid) { state_str = "liquid"; } else
142   if (state==kStateGas) { state_str = "gas"; }
143
144   const G4String name = GenerateName(materialPtr->GetName(), materialPtr);
145
146   xercesc::DOMElement* materialElement = NewElement("material");
147   materialElement->setAttributeNode(NewAttribute("name",name));
148   materialElement->setAttributeNode(NewAttribute("state",state_str));
149
150   // Write any property attached to the material...
151   //
152   if (materialPtr->GetMaterialPropertiesTable())
153   {
154     PropertyWrite(materialElement, materialPtr);
155   }
156
157   if (materialPtr->GetTemperature() != STP_Temperature)
158     { TWrite(materialElement,materialPtr->GetTemperature()); }
159   if (materialPtr->GetPressure() != STP_Pressure)
160     { PWrite(materialElement,materialPtr->GetPressure()); }
161   DWrite(materialElement,materialPtr->GetDensity());
162 
163   const size_t NumberOfElements = materialPtr->GetNumberOfElements();
164
165   if (NumberOfElements>1)
166   {
167      const G4double* MassFractionVector = materialPtr->GetFractionVector();
168
169      for (size_t i=0;i<NumberOfElements;i++)
170      {
171         const G4String fractionref =
172                        GenerateName(materialPtr->GetElement(i)->GetName(),
173                                     materialPtr->GetElement(i));
174         xercesc::DOMElement* fractionElement = NewElement("fraction");
175         fractionElement->setAttributeNode(NewAttribute("n",
176                                           MassFractionVector[i]));
177         fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
178         materialElement->appendChild(fractionElement);
179         AddElement(materialPtr->GetElement(i));
180      }
181   }
182   else
183   {
184      materialElement->setAttributeNode(NewAttribute("Z",materialPtr->GetZ()));
185      AtomWrite(materialElement,materialPtr->GetA());
186   }
187
188   // Append the material AFTER all the possible components are appended!
189   //
190   materialsElement->appendChild(materialElement);
191}
192
193void G4GDMLWriteMaterials::PropertyVectorWrite(const G4String& key,
194                           const G4MaterialPropertyVector* const pvec)
195{
196   const G4String matrixref = GenerateName(key, pvec);
197   xercesc::DOMElement* matrixElement = NewElement("matrix");
198   matrixElement->setAttributeNode(NewAttribute("name", matrixref));
199   matrixElement->setAttributeNode(NewAttribute("coldim", "2"));
200   std::ostringstream pvalues;
201   for (G4int i=0; i<pvec->Entries(); i++)
202   {
203     G4MPVEntry cval = pvec->GetEntry(i);
204     if (i!=0)  { pvalues << " "; }
205     pvalues << cval.GetPhotonEnergy() << " " << cval.GetProperty();
206   }
207   matrixElement->setAttributeNode(NewAttribute("values", pvalues.str()));
208
209   defineElement->appendChild(matrixElement);
210}
211
212void G4GDMLWriteMaterials::PropertyWrite(xercesc::DOMElement* matElement,
213                                         const G4Material* const mat)
214{
215   xercesc::DOMElement* propElement;
216   G4MaterialPropertiesTable* ptable = mat->GetMaterialPropertiesTable();
217   const std::map< G4String, G4MaterialPropertyVector*,
218                 std::less<G4String> >* pmap = ptable->GetPropertiesMap();
219   const std::map< G4String, G4double,
220                 std::less<G4String> >* cmap = ptable->GetPropertiesCMap();
221   std::map< G4String, G4MaterialPropertyVector*,
222                 std::less<G4String> >::const_iterator mpos;
223   std::map< G4String, G4double,
224                 std::less<G4String> >::const_iterator cpos;
225   for (mpos=pmap->begin(); mpos!=pmap->end(); mpos++)
226   {
227      propElement = NewElement("property");
228      propElement->setAttributeNode(NewAttribute("name", mpos->first));
229      propElement->setAttributeNode(NewAttribute("ref",
230                                    GenerateName(mpos->first, mpos->second)));
231      PropertyVectorWrite(mpos->first, mpos->second);
232      matElement->appendChild(propElement);
233   }
234   for (cpos=cmap->begin(); cpos!=cmap->end(); cpos++)
235   {
236      propElement = NewElement("property");
237      propElement->setAttributeNode(NewAttribute("name", cpos->first));
238      propElement->setAttributeNode(NewAttribute("ref", cpos->first));
239      xercesc::DOMElement* constElement = NewElement("constant");
240      constElement->setAttributeNode(NewAttribute("name", cpos->first));
241      constElement->setAttributeNode(NewAttribute("value", cpos->second));
242      defineElement->appendChild(constElement);
243      matElement->appendChild(propElement);
244   }
245}
246
247void G4GDMLWriteMaterials::MaterialsWrite(xercesc::DOMElement* element)
248{
249   G4cout << "G4GDML: Writing materials..." << G4endl;
250
251   materialsElement = NewElement("materials");
252   element->appendChild(materialsElement);
253
254   isotopeList.clear();
255   elementList.clear();
256   materialList.clear();
257}
258
259void G4GDMLWriteMaterials::AddIsotope(const G4Isotope* const isotopePtr)
260{
261   for (size_t i=0; i<isotopeList.size(); i++)   // Check if isotope is
262   {                                             // already in the list!
263     if (isotopeList[i] == isotopePtr)  { return; }
264   }
265   isotopeList.push_back(isotopePtr);
266   IsotopeWrite(isotopePtr);
267}
268
269void G4GDMLWriteMaterials::AddElement(const G4Element* const elementPtr)
270{
271   for (size_t i=0;i<elementList.size();i++)     // Check if element is
272   {                                             // already in the list!
273      if (elementList[i] == elementPtr) { return; }
274   }
275   elementList.push_back(elementPtr);
276   ElementWrite(elementPtr);
277}
278
279void G4GDMLWriteMaterials::AddMaterial(const G4Material* const materialPtr)
280{
281   for (size_t i=0;i<materialList.size();i++)    // Check if material is
282   {                                             // already in the list!
283      if (materialList[i] == materialPtr)  { return; }
284   }
285   materialList.push_back(materialPtr);
286   MaterialWrite(materialPtr);
287}
Note: See TracBrowser for help on using the repository browser.