xdmf export

This commit is contained in:
Jukka Aho
2015-06-20 14:28:03 +03:00
parent f124c55063
commit b0c3560751
3 changed files with 443 additions and 0 deletions
+419
View File
@@ -0,0 +1,419 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exporting data to Xdmf format\n",
"\n",
"Author(s): Jukka Aho <jukka.aho@kapsi.fi>\n",
"\n",
"Python example how to export data to Xdmf format using Python"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"sys.path.append(os.path.expanduser('~/opt/xdmf/lib/python'))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from Xdmf import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export mesh"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"grid = XdmfUnstructuredGrid.New()\n",
"grid.setTime(XdmfTime.New(123))\n",
"geometry = XdmfGeometry.New()\n",
"topology = XdmfTopology.New()\n",
"geometry.setType(XdmfGeometryType.XYZ())\n",
"topology.setType(XdmfTopologyType.Mixed())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 0., 1.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"# one linear tetra\n",
"nodes = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])\n",
"nodes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It needs to be in 1d list:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(nodes.flatten())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"geometry.insertAsFloat32(0, list(nodes.flatten()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Topology; connect nodes 1 - 4 to form tetra\n",
"\n",
"List is here:\n",
"\n",
"http://public.kiteware.com/pipermail/2013-July/028859.html"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"topology.insertAsInt32(0, [0x6, 1, 2, 3, 4]) # Not sure starts this from 0 or 1"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"grid.setGeometry(geometry)\n",
"grid.setTopology(topology)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export fields"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Give scalar value to cells (i.e. elements)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"temperature = XdmfAttribute.New()\n",
"temperature.setType(XdmfAttributeType.Scalar())\n",
"temperature.setCenter(XdmfAttributeCenter.Cell())\n",
"temperature.setName(\"Temperature field\")\n",
"temperature.insertAsInt32(0, [56])\n",
"grid.insert(temperature)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Give vector values to nodes"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"density = XdmfAttribute.New()\n",
"density.setType(XdmfAttributeType.Vector())\n",
"density.setCenter(XdmfAttributeCenter.Node())\n",
"density.setName(\"Density\")\n",
"density.insertAsFloat32(0, [1.3, 2.4, 4.5, 6.7])\n",
"grid.insert(density)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Write to file"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I think this is not mandatory but create temporal collection anyway:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"coll = XdmfGridCollection.New()\n",
"coll.setType(XdmfGridCollectionType.Temporal())\n",
"coll.insert(grid)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create domain and write to disk"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"domain = XdmfDomain.New()\n",
"domain.insert(coll)\n",
"writer = XdmfWriter.New(\"testdata.xmf\")\n",
"domain.accept(writer)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"testdata.h5 testdata.xmf\r\n"
]
}
],
"source": [
"!ls testdata*"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n",
"<Xdmf xmlns:xi=\"http://www.w3.org/2001/XInclude\" Version=\"2.1\">\r\n",
" <Domain>\r\n",
" <Grid CollectionType=\"Temporal\" GridType=\"Collection\" Name=\"Collection\">\r\n",
" <Geometry Type=\"None\"/>\r\n",
" <Topology Dimensions=\"0\" Type=\"NoTopology\"/>\r\n",
" <Grid Name=\"Grid\">\r\n",
" <Time Value=\"123\"/>\r\n",
" <Geometry Type=\"XYZ\">\r\n",
" <DataItem DataType=\"Float\" Dimensions=\"12\" Format=\"XML\" Precision=\"4\">0 0 0 1 0 0 0 1 0 0 0 1</DataItem>\r\n",
" </Geometry>\r\n",
" <Topology Dimensions=\"1\" Type=\"Mixed\">\r\n",
" <DataItem DataType=\"Int\" Dimensions=\"5\" Format=\"XML\" Precision=\"4\">6 1 2 3 4</DataItem>\r\n",
" </Topology>\r\n",
" <Attribute Center=\"Cell\" Name=\"Temperature field\" Type=\"Scalar\">\r\n",
" <DataItem DataType=\"Int\" Dimensions=\"1\" Format=\"XML\" Precision=\"4\">56</DataItem>\r\n",
" </Attribute>\r\n",
" <Attribute Center=\"Node\" Name=\"Density\" Type=\"Vector\">\r\n",
" <DataItem DataType=\"Float\" Dimensions=\"4\" Format=\"XML\" Precision=\"4\">1.3 2.4000001 4.5 6.6999998</DataItem>\r\n",
" </Attribute>\r\n",
" </Grid>\r\n",
" </Grid>\r\n",
" </Domain>\r\n",
"</Xdmf>\r\n"
]
}
],
"source": [
"!cat testdata.xmf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This time Xdmf decided to write results directly to the xml file. If the model is bigger the correct format is"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
" <Xdmf xmlns:xi=\"http://www.w3.org/2001/XInclude\" Version=\"2.1\">\n",
" <Domain>\n",
" <Grid CollectionType=\"Temporal\" GridType=\"Collection\" Name=\"Collection\">\n",
" <Geometry Type=\"None\"/>\n",
" <Topology Dimensions=\"0\" Type=\"NoTopology\"/>\n",
" <Grid Name=\"Grid\">\n",
" <Time Value=\"0\"/>\n",
" <Geometry Type=\"XYZ\">\n",
" <DataItem DataType=\"Float\" Dimensions=\"1260999\" Format=\"HDF\" Precision=\"4\">model.h5:Data16</DataItem>\n",
" </Geometry>\n",
" <Topology Dimensions=\"292001\" Type=\"Mixed\">\n",
" <DataItem DataType=\"Int\" Dimensions=\"3046831\" Format=\"HDF\" Precision=\"4\">model.h5:Data17</DataItem>\n",
" </Topology>\n",
" <Attribute Center=\"Cell\" Name=\"Temperature field\" Type=\"Scalar\">\n",
" <DataItem DataType=\"Int\" Dimensions=\"292001\" Format=\"HDF\" Precision=\"4\">model.h5:Data18</DataItem>\n",
" </Attribute>\n",
" <Attribute Center=\"Node\" Name=\"Density\" Type=\"Vector\">\n",
" <DataItem DataType=\"Float\" Dimensions=\"292001\" Format=\"HDF\" Precision=\"4\">model.h5:Data19</DataItem>\n",
" </Attribute>\n",
" </Grid>\n",
" </Grid>\n",
" </Domain>\n",
" </Xdmf>\n",
"\n",
"\n",
"Structure of h5 file\n",
"\n",
"\n",
" File(filename=model.h5, title='', mode='r+', root_uep='/', filters=Filters(complevel=0, shuffle=False, fletcher32=False, least_significant_digit=None)) \n",
" / (RootGroup) '' \n",
" /Data0 (EArray(1125135,)) ''\n",
" atom := Float32Atom(shape=(), dflt=0.0)\n",
" maindim := 0 \n",
" flavor := 'numpy' \n",
" byteorder := 'little' \n",
" chunkshape := (1000,) \n",
" /Data1 (EArray(2680009,)) ''\n",
" atom := Int32Atom(shape=(), dflt=0)\n",
" maindim := 0 \n",
" flavor := 'numpy' \n",
" byteorder := 'little' \n",
" chunkshape := (1000,) \n",
" ...\n",
"\n",
"\n",
" In [7]: d.root.Data0[:]\n",
" Out[7]:\n",
" array([ 302.10440063, 222.19999695, 20.38028908, ..., -230. ,\n",
" 0. , 7.33333015], dtype=float32)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
BIN
View File
Binary file not shown.
+24
View File
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Xdmf xmlns:xi="http://www.w3.org/2001/XInclude" Version="2.1">
<Domain>
<Grid CollectionType="Temporal" GridType="Collection" Name="Collection">
<Geometry Type="None"/>
<Topology Dimensions="0" Type="NoTopology"/>
<Grid Name="Grid">
<Time Value="123"/>
<Geometry Type="XYZ">
<DataItem DataType="Float" Dimensions="12" Format="XML" Precision="4">0 0 0 1 0 0 0 1 0 0 0 1</DataItem>
</Geometry>
<Topology Dimensions="1" Type="Mixed">
<DataItem DataType="Int" Dimensions="5" Format="XML" Precision="4">6 1 2 3 4</DataItem>
</Topology>
<Attribute Center="Cell" Name="Temperature field" Type="Scalar">
<DataItem DataType="Int" Dimensions="1" Format="XML" Precision="4">56</DataItem>
</Attribute>
<Attribute Center="Node" Name="Density" Type="Vector">
<DataItem DataType="Float" Dimensions="4" Format="XML" Precision="4">1.3 2.4000001 4.5 6.6999998</DataItem>
</Attribute>
</Grid>
</Grid>
</Domain>
</Xdmf>