Difference between revisions of "Read Xdmf"

From XdmfWeb
Jump to navigationJump to search
(New page: == Reading XDMF Data == Image:TwoHex.jpg The following Xdmf XML file is a simple example of a Uniform Grid that contains two Hexahedron that share a face. There are values centered a...)
 
(→‎Reading XDMF Data: Placed code in source tags so it get syntax highlighting. Cleaned up indentation of the code.)
 
(2 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
all stored directly in the XML file.
 
all stored directly in the XML file.
  
<?xml version="1.0" ?>
+
<source lang="xml" line="1">
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
+
<?xml version="1.0" ?>
+
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
+
<Xdmf>
<Xdmf>
 
 
     <Domain>
 
     <Domain>
 
         <Grid Name="TestGrid">
 
         <Grid Name="TestGrid">
 
             <Topology Type="Hexahedron" NumberOfElements="2" >
 
             <Topology Type="Hexahedron" NumberOfElements="2" >
 
                 <DataItem Format="XML" DataType="Float"
 
                 <DataItem Format="XML" DataType="Float"
                    Dimensions="2 8">
+
                          Dimensions="2 8">
 
                     0 1 7 6  3 4 10 9
 
                     0 1 7 6  3 4 10 9
 
                     1 2 8 7  4 5 11 10
 
                     1 2 8 7  4 5 11 10
Line 22: Line 21:
 
             </Topology>
 
             </Topology>
 
             <Geometry Type="XYZ">
 
             <Geometry Type="XYZ">
                    <DataItem Format="XML" DataType="Float" Precision="8"
+
                <DataItem Format="XML" DataType="Float" Precision="8"
                            Dimensions="4 3 3">
+
                          Dimensions="4 3 3">
                      0.0  0.0  1.0
+
                    0.0  0.0  1.0
                      1.0  0.0  1.0
+
                    1.0  0.0  1.0
                      3.0  0.0  2.0
+
                    3.0  0.0  2.0
+
                   
                      0.0  1.0  1.0
+
                    0.0  1.0  1.0
                      1.0  1.0  1.0
+
                    1.0  1.0  1.0
                      3.0  2.0  2.0
+
                    3.0  2.0  2.0
+
                   
                      0.0  0.0  -1.0
+
                    0.0  0.0  -1.0
                      1.0  0.0  -1.0
+
                    1.0  0.0  -1.0
                      3.0  0.0  -2.0
+
                    3.0  0.0  -2.0
+
                   
                      0.0  1.0  -1.0
+
                    0.0  1.0  -1.0
                      1.0  1.0  -1.0
+
                    1.0  1.0  -1.0
                      3.0  2.0  -2.0
+
                    3.0  2.0  -2.0
                    </DataItem>
+
                </DataItem>
 
             </Geometry>
 
             </Geometry>
 
             <Attribute Name="NodeValues" Center="Node">
 
             <Attribute Name="NodeValues" Center="Node">
 
                 <DataItem Format="XML" DataType="Float" Precision="8"
 
                 <DataItem Format="XML" DataType="Float" Precision="8"
                        Dimensions="4 3" >
+
                          Dimensions="4 3" >
                        100 200 300
+
                    100 200 300
                        300 400 500
+
                    300 400 500
                        300 400 500
+
                    300 400 500
                        500 600 700
+
                    500 600 700
 
                 </DataItem>
 
                 </DataItem>
 
             </Attribute>
 
             </Attribute>
 
             <Attribute Name="CellValues" Center="Cell">
 
             <Attribute Name="CellValues" Center="Cell">
 
                 <DataItem Format="XML" DataType="Float" Precision="8"
 
                 <DataItem Format="XML" DataType="Float" Precision="8"
                        Dimensions="2" >
+
                          Dimensions="2" >
                        100 200
+
                    100 200
 
                 </DataItem>
 
                 </DataItem>
 
             </Attribute>
 
             </Attribute>
 
         </Grid>
 
         </Grid>
 
     </Domain>
 
     </Domain>
</Xdmf>
+
</Xdmf>
 +
</source>
  
 
The XML is stored in the file "MyGrid.xmf". The following Python program demonstrated parsing the XML and
 
The XML is stored in the file "MyGrid.xmf". The following Python program demonstrated parsing the XML and
 
retrieving the data values.
 
retrieving the data values.
  
from Xdmf import *
+
<source lang="python" line="1">
dom = XdmfDOM()
+
#! /usr/bin/env python
dom.Parse('MyGrid.xmf')
+
from Xdmf import *
# We now have a tree. Find the one and only Grid element
+
reader = XdmfReader.New()
ge = dom.FindElementByPath('/Xdmf/Domain/Grid')
+
dom = XdmfReader.read('MyGrid.xmf')
grid = XdmfGrid()
+
# We now have a tree. Find the one and only Grid element
grid.SetDOM(dom)
+
grid = dom.getUnstruturedGrid(0)
grid.SetElement(ge)
+
 
# Read Light Data
+
top = grid.GetTopology()
grid.UpdateInformation()
+
print 'Values = ', top.getValuesString()
# Read Heavy Data which includes Topology and Geometry
+
# Release values from data when done
grid.Update()
+
top.release()
+
 
top = grid.GetTopology()
+
geo = grid.GetGeometry()
conn = top.GetConnectivity()
+
print 'Geo Type = ', geo.getType().getName(), ' # Points = ', geo.getNumberPoints()
print 'Values = ', conn.GetValues()
+
print 'Points = ', geo.getValuesString()
+
geo.release()
geo = grid.GetGeometry()
+
# for each Attribute, print Light Data and Values
points = geo.GetPoints()
+
for i in range(grid.getNumberAttributes()):
print 'Geo Type = ', geo.GetGeometryTypeAsString(), ' # Points = ', geo.GetNumberOfPoints()
+
     attr = grid.getAttribute(i)
print 'Points = ', points.GetValues()
+
     print 'Attribute ', i, ' Name = ', attr.getName()
# for each Attribute, print Light Data and Values
 
for i in range(grid.GetNumberOfAttributes()) :
 
     attr = grid.GetAttribute(i)
 
     print 'Attribute ', i, ' Name = ', attr.Get('Name')
 
 
     # Attribute HeavyData is not Updated by default
 
     # Attribute HeavyData is not Updated by default
 
     # there could potentially be many causing huge IO
 
     # there could potentially be many causing huge IO
     attr.Update()
+
     attr.read()
     vals = attr.GetValues()
+
     print 'Values ', attr.getValuesString()
     print 'Values ', vals.GetValues()
+
     attr.release()
 +
</source>

Latest revision as of 17:59, 20 May 2016

Reading XDMF Data

TwoHex.jpg

The following Xdmf XML file is a simple example of a Uniform Grid that contains two Hexahedron that share a face. There are values centered at the nodes and at the cell centers. The values for geometry, connectivity, and scalars are all stored directly in the XML file.

<source lang="xml" line="1"> <?xml version="1.0" ?> <!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []> <Xdmf>

   <Domain>
       <Grid Name="TestGrid">
           <Topology Type="Hexahedron" NumberOfElements="2" >
               <DataItem Format="XML" DataType="Float"
                         Dimensions="2 8">
                   0 1 7 6   3 4 10 9
                   1 2 8 7   4 5 11 10
               </DataItem>
           </Topology>
           <Geometry Type="XYZ">
               <DataItem Format="XML" DataType="Float" Precision="8"
                         Dimensions="4 3 3">
                   0.0   0.0   1.0
                   1.0   0.0   1.0
                   3.0   0.0   2.0
                   
                   0.0   1.0   1.0
                   1.0   1.0   1.0
                   3.0   2.0   2.0
                   
                   0.0   0.0   -1.0
                   1.0   0.0   -1.0
                   3.0   0.0   -2.0
                   
                   0.0   1.0   -1.0
                   1.0   1.0   -1.0
                   3.0   2.0   -2.0
               </DataItem>
           </Geometry>
           <Attribute Name="NodeValues" Center="Node">
               <DataItem Format="XML" DataType="Float" Precision="8"
                         Dimensions="4 3" >
                   100 200 300
                   300 400 500
                   300 400 500
                   500 600 700
               </DataItem>
           </Attribute>
           <Attribute Name="CellValues" Center="Cell">
               <DataItem Format="XML" DataType="Float" Precision="8"
                         Dimensions="2" >
                   100 200
               </DataItem>
           </Attribute>
       </Grid>
   </Domain>

</Xdmf> </source>

The XML is stored in the file "MyGrid.xmf". The following Python program demonstrated parsing the XML and retrieving the data values.

<source lang="python" line="1">

  1. ! /usr/bin/env python

from Xdmf import * reader = XdmfReader.New() dom = XdmfReader.read('MyGrid.xmf')

  1. We now have a tree. Find the one and only Grid element

grid = dom.getUnstruturedGrid(0)

top = grid.GetTopology() print 'Values = ', top.getValuesString()

  1. Release values from data when done

top.release()

geo = grid.GetGeometry() print 'Geo Type = ', geo.getType().getName(), ' # Points = ', geo.getNumberPoints() print 'Points = ', geo.getValuesString() geo.release()

  1. for each Attribute, print Light Data and Values

for i in range(grid.getNumberAttributes()):

   attr = grid.getAttribute(i)
   print 'Attribute ', i, ' Name = ', attr.getName()
   # Attribute HeavyData is not Updated by default
   # there could potentially be many causing huge IO
   attr.read()
   print 'Values ', attr.getValuesString()
   attr.release()

</source>