Write Xdmf
From XdmfWeb
Jump to navigationJump to search
Writing Xdmf
Xdmf can be generated in many different manners. Using the low level HDF5 library and print statements is certainly one of them. Utilizing the XDMF API, however, provides some convenient advantages. Suppose we wanted to generate an XDMF dataset of a co-rectilinear mesh with scalar values at each node
from Xdmf import * d = XdmfDOM() root = XdmfRoot() root.SetDOM(d) root.SetVersion(2.2) # Change the Version number because we can root.Build() # Information i = XdmfInformation() # Arbitrary Name=Value Facility i.SetName("Time") i.SetValue("0.0123") root.Insert(i) # XML DOM is used as the keeper of the structure # Insert() creates an XML node and inserts it under # the parent # Domain dm = XdmfDomain() root.Insert(dm) # Grid g = XdmfGrid() g.SetName("Structured Grid") # Topology t = g.GetTopology() t.SetTopologyType(XDMF_3DCORECTMESH) t.GetShapeDesc().SetShapeFromString('10 20 30') # Geometry geo = g.GetGeometry() geo.SetGeometryType(XDMF_GEOMETRY_ORIGIN_DXDYDZ) geo.SetOrigin(1, 2, 3) geo.SetDxDyDz(0.1, 0.2, 0.3) dm.Insert(g) # Attribute attr = XdmfAttribute() attr.SetName("Pressure") attr.SetAttributeCenter(XDMF_ATTRIBUTE_CENTER_NODE); attr.SetAttributeType(XDMF_ATTRIBUTE_TYPE_SCALAR); p = attr.GetValues() p.SetNumberOfElements(10 * 20 * 30) p.Generate(0.0, 1.0, 0, p.GetNumberOfElements() - 1) g.Insert(attr) # Update XML and Write Values to DataItems root.Build() # DataItems > 100 values are heavy print d.Serialize() # prints to stdout d.Write('SMesh.xmf') # write to file
Would result in the Light Data XML to be written to the file Smesh.xmf and the Heavy data to be written to Xdmf.h5.
<?xml version="1.0" ?> <!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []> <Xdmf xmlns:xi="http://www.w3.org/2003/XInclude" Version="2.2"> <Information Name="Time" Value="0.0123"/> <Domain> <Grid Name="Structured Grid" GridType="Uniform"> <Topology TopologyType="3DCORECTMesh" NumberOfElements="10 20 30 "/> <Geometry GeometryType="ORIGIN_DXDYDZ"> <DataItem Dimensions="3 " NumberType="Float" Precision="4" Format="XML"> 1 2 3 </DataItem> <DataItem Dimensions="3 " NumberType="Float" Precision="4" Format="XML"> 0.1 0.2 0.3 </DataItem> </Geometry> <Attribute Name="Pressure" AttributeType="Scalar" Center="Cell"> <DataItem Dimensions="6000 " NumberType="Float" Precision="4" Format="HDF">Xdmf.h5:/Data</DataItem> </Attribute> </Grid> </Domain> </Xdmf>
~