[Paraview] storing tensors in hdf5?

Pratik Mallya pratik.mallya at gmail.com
Sat Apr 9 20:27:11 EDT 2011


Pratik Mallya wrote:
> Well, that is why i chose the subject title to be so :).
> In particular, I since I am using numpy (and h5py) to calculate the 
> tensor, I am storing the result (a 256X256X9 ndarray named Q) into h5 
> file by using a command of the form:
> g.create_dataset("Q", data=Q)
> Does the problem lie here? Am i supposed to use a different 
> format/function of h5py, or something?  Because I can see the .h5 file 
> using hdfview, and it seems perfectly all right.
>
>
Even after using the low-level h5py functions to create the hdf5 file i 
am still unable to read the tensor data. My question really is: how 
should i write my xdmf file so that it can read a 9 component tensor? I 
have a 256X256 2dsmesh grid, and the tensor in the h5 file is a dataset 
of dimension 256X256X9. Is this ok?
Any suggestions are welcome. I have been trying quite hard for a long 
time :-/:
the python code for creating the tensor data is here:

import numpy as np
import h5py
#L and N=vectors, S and T =scalars; all are of dimension 256X256(X3)



elem=range(4)
f= h5py.File("test.h5","r")# contains the data for calculating tensor
j=0
for i in f:
    elem[j]=(f.get(i)).value # gets the data
    j+=1
f.close()

L=elem[0]
N=elem[1]
S=elem[2]
T=elem[3]

dim=256
delta=np.eye(3)
dimen=(dim,dim,9)
x=np.zeros(dim*dim,np.float64)
y=np.zeros(dim*dim,np.float64)
Q=np.zeros((dim,dim,9),np.float64)
ndx=0
for j in range(0, dim):
    yt= j /255.
    for i in range(0,dim):
        temp=(3./2.)*S[i][j]*(np.outer(N[i][j],N[i][j])-(delta/3)) + 
(1./2.)* T[i][j] * (np.outer(L[i][j],L[i][j])-np.outer ( 
np.cross(N[i][j],L[i][j]) , np.cross(N[i][j],L[i][j]) ) )
        xt= i/255.
        x[ndx]=xt
        y[ndx]=yt
        temp.shape=(1,9)
        Q[i][j]=temp[0]
        ndx+=1
#Q= (3/2)*np.outer(N,N) + 
(1/2)*(np.outer(L,L)-np.outer(np.cross(N,L),np.cross(N,L)))

x.shape=(dim,dim)
y.shape=(dim,dim)

g=h5py.File("Q.h5","w")
g.create_dataset("X", data=x)
g.create_dataset("Y", data=y)
g.close()
file_id=h5py.h5f.open("Q.h5")
dataspace_id=h5py.h5s.create_simple(dimen,dimen)
dataset_id  =h5py.h5d.create(file_id,"Q",h5py.h5t.NATIVE_FLOAT, 
dataspace_id)
dataset_id.write(dataspace_id,h5py.h5s.ALL,Q)

file_id.close()



-- 
*Pratik Mallya*
http://en.wikipedia.org/wiki/User:Pratik.mallya


More information about the ParaView mailing list