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

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

tag geant4.9.4 beta 1 + modifs locales

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.25 2010/02/18 17:38:24 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-01 $
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   const G4String matrixref = GenerateName(key, pvec);
198   xercesc::DOMElement* matrixElement = NewElement("matrix");
199   matrixElement->setAttributeNode(NewAttribute("name", matrixref));
200   matrixElement->setAttributeNode(NewAttribute("coldim", "2"));
201   std::ostringstream pvalues;
202   for (G4int i=0; i<pvec->Entries(); i++)
203   {
204     G4MPVEntry cval = pvec->GetEntry(i);
205     if (i!=0)  { pvalues << " "; }
206     pvalues << cval.GetPhotonEnergy() << " " << cval.GetProperty();
207   }
208   matrixElement->setAttributeNode(NewAttribute("values", pvalues.str()));
209
210   defineElement->appendChild(matrixElement);
211}
212
213void G4GDMLWriteMaterials::PropertyWrite(xercesc::DOMElement* matElement,
214                                         const G4Material* const mat)
215{
216   xercesc::DOMElement* propElement;
217   G4MaterialPropertiesTable* ptable = mat->GetMaterialPropertiesTable();
218   const std::map< G4String, G4MaterialPropertyVector*,
219                 std::less<G4String> >* pmap = ptable->GetPropertiesMap();
220   const std::map< G4String, G4double,
221                 std::less<G4String> >* cmap = ptable->GetPropertiesCMap();
222   std::map< G4String, G4MaterialPropertyVector*,
223                 std::less<G4String> >::const_iterator mpos;
224   std::map< G4String, G4double,
225                 std::less<G4String> >::const_iterator cpos;
226   for (mpos=pmap->begin(); mpos!=pmap->end(); mpos++)
227   {
228      propElement = NewElement("property");
229      propElement->setAttributeNode(NewAttribute("name", mpos->first));
230      propElement->setAttributeNode(NewAttribute("ref",
231                                    GenerateName(mpos->first, mpos->second)));
232      PropertyVectorWrite(mpos->first, mpos->second);
233      matElement->appendChild(propElement);
234   }
235   for (cpos=cmap->begin(); cpos!=cmap->end(); cpos++)
236   {
237      propElement = NewElement("property");
238      propElement->setAttributeNode(NewAttribute("name", cpos->first));
239      propElement->setAttributeNode(NewAttribute("ref", cpos->first));
240      xercesc::DOMElement* constElement = NewElement("constant");
241      constElement->setAttributeNode(NewAttribute("name", cpos->first));
242      constElement->setAttributeNode(NewAttribute("value", cpos->second));
243      defineElement->appendChild(constElement);
244      matElement->appendChild(propElement);
245   }
246}
247
248void G4GDMLWriteMaterials::MaterialsWrite(xercesc::DOMElement* element)
249{
250   G4cout << "G4GDML: Writing materials..." << G4endl;
251
252   materialsElement = NewElement("materials");
253   element->appendChild(materialsElement);
254
255   isotopeList.clear();
256   elementList.clear();
257   materialList.clear();
258}
259
260void G4GDMLWriteMaterials::AddIsotope(const G4Isotope* const isotopePtr)
261{
262   for (size_t i=0; i<isotopeList.size(); i++)   // Check if isotope is
263   {                                             // already in the list!
264     if (isotopeList[i] == isotopePtr)  { return; }
265   }
266   isotopeList.push_back(isotopePtr);
267   IsotopeWrite(isotopePtr);
268}
269
270void G4GDMLWriteMaterials::AddElement(const G4Element* const elementPtr)
271{
272   for (size_t i=0;i<elementList.size();i++)     // Check if element is
273   {                                             // already in the list!
274      if (elementList[i] == elementPtr) { return; }
275   }
276   elementList.push_back(elementPtr);
277   ElementWrite(elementPtr);
278}
279
280void G4GDMLWriteMaterials::AddMaterial(const G4Material* const materialPtr)
281{
282   for (size_t i=0;i<materialList.size();i++)    // Check if material is
283   {                                             // already in the list!
284      if (materialList[i] == materialPtr)  { return; }
285   }
286   materialList.push_back(materialPtr);
287   MaterialWrite(materialPtr);
288}
Note: See TracBrowser for help on using the repository browser.