G4GDMLWriteMaterials.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // class G4GDMLWriteMaterials Implementation
00030 //
00031 // Original author: Zoltan Torzsok, November 2007
00032 //
00033 // --------------------------------------------------------------------
00034 
00035 #include <sstream>
00036 
00037 #include "G4GDMLWriteMaterials.hh"
00038 
00039 #include "G4PhysicalConstants.hh"
00040 #include "G4SystemOfUnits.hh"
00041 #include "G4Element.hh"
00042 #include "G4Isotope.hh"
00043 #include "G4Material.hh"
00044 
00045 G4GDMLWriteMaterials::G4GDMLWriteMaterials()
00046   : G4GDMLWriteDefine(), materialsElement(0)
00047 {
00048 }
00049 
00050 G4GDMLWriteMaterials::~G4GDMLWriteMaterials()
00051 {
00052 }
00053 
00054 void G4GDMLWriteMaterials::
00055 AtomWrite(xercesc::DOMElement* element,const G4double& a)
00056 {
00057    xercesc::DOMElement* atomElement = NewElement("atom");
00058    atomElement->setAttributeNode(NewAttribute("unit","g/mole"));
00059    atomElement->setAttributeNode(NewAttribute("value",a*mole/g));
00060    element->appendChild(atomElement);
00061 }
00062 
00063 void G4GDMLWriteMaterials::
00064 DWrite(xercesc::DOMElement* element,const G4double& d)
00065 {
00066    xercesc::DOMElement* DElement = NewElement("D");
00067    DElement->setAttributeNode(NewAttribute("unit","g/cm3"));
00068    DElement->setAttributeNode(NewAttribute("value",d*cm3/g));
00069    element->appendChild(DElement);
00070 }
00071 
00072 void G4GDMLWriteMaterials::
00073 PWrite(xercesc::DOMElement* element,const G4double& P)
00074 {
00075    xercesc::DOMElement* PElement = NewElement("P");
00076    PElement->setAttributeNode(NewAttribute("unit","pascal"));
00077    PElement->setAttributeNode(NewAttribute("value",P/pascal));
00078    element->appendChild(PElement);
00079 }
00080 
00081 void G4GDMLWriteMaterials::
00082 TWrite(xercesc::DOMElement* element,const G4double& T)
00083 {
00084    xercesc::DOMElement* TElement = NewElement("T");
00085    TElement->setAttributeNode(NewAttribute("unit","K"));
00086    TElement->setAttributeNode(NewAttribute("value",T/kelvin));
00087    element->appendChild(TElement);
00088 }
00089 
00090 void G4GDMLWriteMaterials::
00091 MEEWrite(xercesc::DOMElement* element,const G4double& MEE)
00092 {
00093    xercesc::DOMElement* PElement = NewElement("MEE");
00094    PElement->setAttributeNode(NewAttribute("unit","eV"));
00095    PElement->setAttributeNode(NewAttribute("value",MEE/electronvolt));
00096    element->appendChild(PElement);
00097 }
00098 
00099 void G4GDMLWriteMaterials::
00100 IsotopeWrite(const G4Isotope* const isotopePtr)
00101 {
00102    const G4String name = GenerateName(isotopePtr->GetName(),isotopePtr);
00103 
00104    xercesc::DOMElement* isotopeElement = NewElement("isotope");
00105    isotopeElement->setAttributeNode(NewAttribute("name",name));
00106    isotopeElement->setAttributeNode(NewAttribute("N",isotopePtr->GetN()));
00107    isotopeElement->setAttributeNode(NewAttribute("Z",isotopePtr->GetZ()));
00108    materialsElement->appendChild(isotopeElement);
00109    AtomWrite(isotopeElement,isotopePtr->GetA());
00110 }
00111 
00112 void G4GDMLWriteMaterials::ElementWrite(const G4Element* const elementPtr)
00113 {
00114    const G4String name = GenerateName(elementPtr->GetName(),elementPtr);
00115 
00116    xercesc::DOMElement* elementElement = NewElement("element");
00117    elementElement->setAttributeNode(NewAttribute("name",name));
00118 
00119    const size_t NumberOfIsotopes = elementPtr->GetNumberOfIsotopes();
00120 
00121    if (NumberOfIsotopes>0)
00122    {
00123       const G4double* RelativeAbundanceVector =
00124             elementPtr->GetRelativeAbundanceVector();             
00125       for (size_t i=0;i<NumberOfIsotopes;i++)
00126       {
00127          G4String fractionref = GenerateName(elementPtr->GetIsotope(i)->GetName(),
00128                                              elementPtr->GetIsotope(i));
00129          xercesc::DOMElement* fractionElement = NewElement("fraction");
00130          fractionElement->setAttributeNode(NewAttribute("n",
00131                                            RelativeAbundanceVector[i]));
00132          fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
00133          elementElement->appendChild(fractionElement);
00134          AddIsotope(elementPtr->GetIsotope(i));
00135       }
00136    }
00137    else
00138    {
00139       elementElement->setAttributeNode(NewAttribute("Z",elementPtr->GetZ()));
00140       AtomWrite(elementElement,elementPtr->GetA());
00141    }
00142 
00143    materialsElement->appendChild(elementElement);
00144      // Append the element AFTER all the possible components are appended!
00145 }
00146 
00147 void G4GDMLWriteMaterials::MaterialWrite(const G4Material* const materialPtr)
00148 {
00149    G4String state_str("undefined");
00150    const G4State state = materialPtr->GetState();
00151    if (state==kStateSolid) { state_str = "solid"; } else
00152    if (state==kStateLiquid) { state_str = "liquid"; } else
00153    if (state==kStateGas) { state_str = "gas"; }
00154 
00155    const G4String name = GenerateName(materialPtr->GetName(), materialPtr);
00156 
00157    xercesc::DOMElement* materialElement = NewElement("material");
00158    materialElement->setAttributeNode(NewAttribute("name",name));
00159    materialElement->setAttributeNode(NewAttribute("state",state_str));
00160 
00161    // Write any property attached to the material...
00162    //
00163    if (materialPtr->GetMaterialPropertiesTable())
00164    {
00165      PropertyWrite(materialElement, materialPtr);
00166    }
00167 
00168    if (materialPtr->GetTemperature() != STP_Temperature)
00169      { TWrite(materialElement,materialPtr->GetTemperature()); }
00170    if (materialPtr->GetPressure() != STP_Pressure)
00171      { PWrite(materialElement,materialPtr->GetPressure()); }
00172 
00173    // Write Ionisation potential (mean excitation energy)
00174    MEEWrite(materialElement,materialPtr->GetIonisation()->GetMeanExcitationEnergy());
00175    
00176    DWrite(materialElement,materialPtr->GetDensity());
00177   
00178    const size_t NumberOfElements = materialPtr->GetNumberOfElements();
00179 
00180    if ( (NumberOfElements>1)
00181       || ( materialPtr->GetElement(0)
00182         && materialPtr->GetElement(0)->GetNumberOfIsotopes()>1 ) )
00183    {
00184       const G4double* MassFractionVector = materialPtr->GetFractionVector();
00185 
00186       for (size_t i=0;i<NumberOfElements;i++)
00187       {
00188          const G4String fractionref =
00189                         GenerateName(materialPtr->GetElement(i)->GetName(),
00190                                      materialPtr->GetElement(i));
00191          xercesc::DOMElement* fractionElement = NewElement("fraction");
00192          fractionElement->setAttributeNode(NewAttribute("n",
00193                                            MassFractionVector[i]));
00194          fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
00195          materialElement->appendChild(fractionElement);
00196          AddElement(materialPtr->GetElement(i));
00197       }
00198    }
00199    else
00200    {
00201       materialElement->setAttributeNode(NewAttribute("Z",materialPtr->GetZ()));
00202       AtomWrite(materialElement,materialPtr->GetA());
00203    }
00204 
00205    // Append the material AFTER all the possible components are appended!
00206    //
00207    materialsElement->appendChild(materialElement);
00208 }
00209 
00210 void G4GDMLWriteMaterials::PropertyVectorWrite(const G4String& key,
00211                            const G4PhysicsOrderedFreeVector* const pvec)
00212 {
00213    const G4String matrixref = GenerateName(key, pvec);
00214    xercesc::DOMElement* matrixElement = NewElement("matrix");
00215    matrixElement->setAttributeNode(NewAttribute("name", matrixref));
00216    matrixElement->setAttributeNode(NewAttribute("coldim", "2"));
00217    std::ostringstream pvalues;
00218    for (size_t i=0; i<pvec->GetVectorLength(); i++)
00219    {
00220        if (i!=0)  { pvalues << " "; }
00221        pvalues << pvec->Energy(i) << " " << (*pvec)[i];
00222    }
00223    matrixElement->setAttributeNode(NewAttribute("values", pvalues.str()));
00224 
00225    defineElement->appendChild(matrixElement);
00226 }
00227 
00228 void G4GDMLWriteMaterials::PropertyWrite(xercesc::DOMElement* matElement,
00229                                          const G4Material* const mat)
00230 {
00231    xercesc::DOMElement* propElement;
00232    G4MaterialPropertiesTable* ptable = mat->GetMaterialPropertiesTable();
00233    const std::map< G4String, G4PhysicsOrderedFreeVector*,
00234                  std::less<G4String> >* pmap = ptable->GetPropertiesMap();
00235    const std::map< G4String, G4double,
00236                  std::less<G4String> >* cmap = ptable->GetPropertiesCMap();
00237    std::map< G4String, G4PhysicsOrderedFreeVector*,
00238                  std::less<G4String> >::const_iterator mpos;
00239    std::map< G4String, G4double,
00240                  std::less<G4String> >::const_iterator cpos;
00241    for (mpos=pmap->begin(); mpos!=pmap->end(); mpos++)
00242    {
00243       propElement = NewElement("property");
00244       propElement->setAttributeNode(NewAttribute("name", mpos->first));
00245       propElement->setAttributeNode(NewAttribute("ref",
00246                                     GenerateName(mpos->first, mpos->second)));
00247       if (mpos->second)
00248       {
00249          PropertyVectorWrite(mpos->first, mpos->second);
00250          matElement->appendChild(propElement);
00251       }
00252       else
00253       {
00254          G4String warn_message = "Null pointer for material property -"
00255                   + mpos->first + "- of material -" + mat->GetName() + "- !";
00256          G4Exception("G4GDMLWriteMaterials::PropertyWrite()", "NullPointer",
00257                      JustWarning, warn_message);
00258          continue;
00259       }
00260    }
00261    for (cpos=cmap->begin(); cpos!=cmap->end(); cpos++)
00262    {
00263       propElement = NewElement("property");
00264       propElement->setAttributeNode(NewAttribute("name", cpos->first));
00265       propElement->setAttributeNode(NewAttribute("ref", cpos->first));
00266       xercesc::DOMElement* constElement = NewElement("constant");
00267       constElement->setAttributeNode(NewAttribute("name", cpos->first));
00268       constElement->setAttributeNode(NewAttribute("value", cpos->second));
00269       defineElement->appendChild(constElement);
00270       matElement->appendChild(propElement);
00271    }
00272 }
00273 
00274 void G4GDMLWriteMaterials::MaterialsWrite(xercesc::DOMElement* element)
00275 {
00276    G4cout << "G4GDML: Writing materials..." << G4endl;
00277 
00278    materialsElement = NewElement("materials");
00279    element->appendChild(materialsElement);
00280 
00281    isotopeList.clear();
00282    elementList.clear();
00283    materialList.clear();
00284 }
00285 
00286 void G4GDMLWriteMaterials::AddIsotope(const G4Isotope* const isotopePtr)
00287 {
00288    for (size_t i=0; i<isotopeList.size(); i++)   // Check if isotope is
00289    {                                             // already in the list!
00290      if (isotopeList[i] == isotopePtr)  { return; }
00291    }
00292    isotopeList.push_back(isotopePtr);
00293    IsotopeWrite(isotopePtr);
00294 }
00295 
00296 void G4GDMLWriteMaterials::AddElement(const G4Element* const elementPtr)
00297 {
00298    for (size_t i=0;i<elementList.size();i++)     // Check if element is
00299    {                                             // already in the list!
00300       if (elementList[i] == elementPtr) { return; }
00301    }
00302    elementList.push_back(elementPtr);
00303    ElementWrite(elementPtr);
00304 }
00305 
00306 void G4GDMLWriteMaterials::AddMaterial(const G4Material* const materialPtr)
00307 {
00308    for (size_t i=0;i<materialList.size();i++)    // Check if material is
00309    {                                             // already in the list!
00310       if (materialList[i] == materialPtr)  { return; }
00311    }
00312    materialList.push_back(materialPtr);
00313    MaterialWrite(materialPtr);
00314 }

Generated on Mon May 27 17:48:20 2013 for Geant4 by  doxygen 1.4.7