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

Last change on this file since 1228 was 1228, checked in by garnier, 14 years ago

update geant4.9.3 tag

File size: 10.9 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.24 2009/04/27 07:22:36 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-03 $
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::
44G4GDMLWriteMaterials() : G4GDMLWriteDefine()
45{
46}
47
48G4GDMLWriteMaterials::
49~G4GDMLWriteMaterials()
50{
51}
52
53void G4GDMLWriteMaterials::
54AtomWrite(xercesc::DOMElement* element,const G4double& a)
55{
56   xercesc::DOMElement* atomElement = NewElement("atom");
57   atomElement->setAttributeNode(NewAttribute("unit","g/mole"));
58   atomElement->setAttributeNode(NewAttribute("value",a*mole/g));
59   element->appendChild(atomElement);
60}
61
62void G4GDMLWriteMaterials::
63DWrite(xercesc::DOMElement* element,const G4double& d)
64{
65   xercesc::DOMElement* DElement = NewElement("D");
66   DElement->setAttributeNode(NewAttribute("unit","g/cm3"));
67   DElement->setAttributeNode(NewAttribute("value",d*cm3/g));
68   element->appendChild(DElement);
69}
70
71void G4GDMLWriteMaterials::
72PWrite(xercesc::DOMElement* element,const G4double& P)
73{
74   xercesc::DOMElement* PElement = NewElement("P");
75   PElement->setAttributeNode(NewAttribute("unit","pascal"));
76   PElement->setAttributeNode(NewAttribute("value",P/pascal));
77   element->appendChild(PElement);
78}
79
80void G4GDMLWriteMaterials::
81TWrite(xercesc::DOMElement* element,const G4double& T)
82{
83   xercesc::DOMElement* TElement = NewElement("T");
84   TElement->setAttributeNode(NewAttribute("unit","K"));
85   TElement->setAttributeNode(NewAttribute("value",T/kelvin));
86   element->appendChild(TElement);
87}
88
89void G4GDMLWriteMaterials::
90IsotopeWrite(const G4Isotope* const isotopePtr)
91{
92   const G4String name = GenerateName(isotopePtr->GetName(),isotopePtr);
93
94   xercesc::DOMElement* isotopeElement = NewElement("isotope");
95   isotopeElement->setAttributeNode(NewAttribute("name",name));
96   isotopeElement->setAttributeNode(NewAttribute("N",isotopePtr->GetN()));
97   isotopeElement->setAttributeNode(NewAttribute("Z",isotopePtr->GetZ()));
98   materialsElement->appendChild(isotopeElement);
99   AtomWrite(isotopeElement,isotopePtr->GetA());
100}
101
102void G4GDMLWriteMaterials::ElementWrite(const G4Element* const elementPtr)
103{
104   const G4String name = GenerateName(elementPtr->GetName(),elementPtr);
105
106   xercesc::DOMElement* elementElement = NewElement("element");
107   elementElement->setAttributeNode(NewAttribute("name",name));
108
109   const size_t NumberOfIsotopes = elementPtr->GetNumberOfIsotopes();
110
111   if (NumberOfIsotopes>0)
112   {
113      const G4double* RelativeAbundanceVector =
114            elementPtr->GetRelativeAbundanceVector();             
115      for (size_t i=0;i<NumberOfIsotopes;i++)
116      {
117         G4String fractionref = GenerateName(elementPtr->GetIsotope(i)->GetName(),
118                                             elementPtr->GetIsotope(i));
119         xercesc::DOMElement* fractionElement = NewElement("fraction");
120         fractionElement->setAttributeNode(NewAttribute("n",
121                                           RelativeAbundanceVector[i]));
122         fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
123         elementElement->appendChild(fractionElement);
124         AddIsotope(elementPtr->GetIsotope(i));
125      }
126   }
127   else
128   {
129      elementElement->setAttributeNode(NewAttribute("Z",elementPtr->GetZ()));
130      AtomWrite(elementElement,elementPtr->GetA());
131   }
132
133   materialsElement->appendChild(elementElement);
134     // Append the element AFTER all the possible components are appended!
135}
136
137void G4GDMLWriteMaterials::MaterialWrite(const G4Material* const materialPtr)
138{
139   G4String state_str("undefined");
140   const G4State state = materialPtr->GetState();
141   if (state==kStateSolid) { state_str = "solid"; } else
142   if (state==kStateLiquid) { state_str = "liquid"; } else
143   if (state==kStateGas) { state_str = "gas"; }
144
145   const G4String name = GenerateName(materialPtr->GetName(), materialPtr);
146
147   xercesc::DOMElement* materialElement = NewElement("material");
148   materialElement->setAttributeNode(NewAttribute("name",name));
149   materialElement->setAttributeNode(NewAttribute("state",state_str));
150
151   // Write any property attached to the material...
152   //
153   if (materialPtr->GetMaterialPropertiesTable())
154   {
155     PropertyWrite(materialElement, materialPtr);
156   }
157
158   if (materialPtr->GetTemperature() != STP_Temperature)
159     { TWrite(materialElement,materialPtr->GetTemperature()); }
160   if (materialPtr->GetPressure() != STP_Pressure)
161     { PWrite(materialElement,materialPtr->GetPressure()); }
162   DWrite(materialElement,materialPtr->GetDensity());
163 
164   const size_t NumberOfElements = materialPtr->GetNumberOfElements();
165
166   if (NumberOfElements>1)
167   {
168      const G4double* MassFractionVector = materialPtr->GetFractionVector();
169
170      for (size_t i=0;i<NumberOfElements;i++)
171      {
172         const G4String fractionref =
173                        GenerateName(materialPtr->GetElement(i)->GetName(),
174                                     materialPtr->GetElement(i));
175         xercesc::DOMElement* fractionElement = NewElement("fraction");
176         fractionElement->setAttributeNode(NewAttribute("n",
177                                           MassFractionVector[i]));
178         fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
179         materialElement->appendChild(fractionElement);
180         AddElement(materialPtr->GetElement(i));
181      }
182   }
183   else
184   {
185      materialElement->setAttributeNode(NewAttribute("Z",materialPtr->GetZ()));
186      AtomWrite(materialElement,materialPtr->GetA());
187   }
188
189   // Append the material AFTER all the possible components are appended!
190   //
191   materialsElement->appendChild(materialElement);
192}
193
194void G4GDMLWriteMaterials::PropertyVectorWrite(const G4String& key,
195                           const G4MaterialPropertyVector* const pvec)
196{
197   xercesc::DOMElement* matrixElement = NewElement("matrix");
198   matrixElement->setAttributeNode(NewAttribute("name", key));
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", mpos->first));
230      PropertyVectorWrite(mpos->first, mpos->second);
231      matElement->appendChild(propElement);
232   }
233   for (cpos=cmap->begin(); cpos!=cmap->end(); cpos++)
234   {
235      propElement = NewElement("property");
236      propElement->setAttributeNode(NewAttribute("name", cpos->first));
237      propElement->setAttributeNode(NewAttribute("ref", cpos->first));
238      xercesc::DOMElement* constElement = NewElement("constant");
239      constElement->setAttributeNode(NewAttribute("name", cpos->first));
240      constElement->setAttributeNode(NewAttribute("value", cpos->second));
241      defineElement->appendChild(constElement);
242      matElement->appendChild(propElement);
243   }
244}
245
246void G4GDMLWriteMaterials::MaterialsWrite(xercesc::DOMElement* element)
247{
248   G4cout << "G4GDML: Writing materials..." << G4endl;
249
250   materialsElement = NewElement("materials");
251   element->appendChild(materialsElement);
252
253   isotopeList.clear();
254   elementList.clear();
255   materialList.clear();
256}
257
258void G4GDMLWriteMaterials::AddIsotope(const G4Isotope* const isotopePtr)
259{
260   for (size_t i=0; i<isotopeList.size(); i++)   // Check if isotope is
261   {                                             // already in the list!
262     if (isotopeList[i] == isotopePtr)  { return; }
263   }
264   isotopeList.push_back(isotopePtr);
265   IsotopeWrite(isotopePtr);
266}
267
268void G4GDMLWriteMaterials::AddElement(const G4Element* const elementPtr)
269{
270   for (size_t i=0;i<elementList.size();i++)     // Check if element is
271   {                                             // already in the list!
272      if (elementList[i] == elementPtr) { return; }
273   }
274   elementList.push_back(elementPtr);
275   ElementWrite(elementPtr);
276}
277
278void G4GDMLWriteMaterials::AddMaterial(const G4Material* const materialPtr)
279{
280   for (size_t i=0;i<materialList.size();i++)    // Check if material is
281   {                                             // already in the list!
282      if (materialList[i] == materialPtr)  { return; }
283   }
284   materialList.push_back(materialPtr);
285   MaterialWrite(materialPtr);
286}
Note: See TracBrowser for help on using the repository browser.