root / ase / io / vtkxml.py @ 14
Historique | Voir | Annoter | Télécharger (5,21 ko)
| 1 |
import numpy as np |
|---|---|
| 2 |
#from Numeric import asarray as Numeric_asarray
|
| 3 |
|
| 4 |
#from ase.units import Bohr
|
| 5 |
#from ase.parallel import paropen
|
| 6 |
|
| 7 |
fast = False
|
| 8 |
|
| 9 |
# -------------------------------------------------------------------
|
| 10 |
|
| 11 |
from vtk import vtkStructuredPoints, vtkDoubleArray, vtkXMLImageDataWriter |
| 12 |
|
| 13 |
def write_vti(filename, atoms, data): |
| 14 |
|
| 15 |
#if isinstance(fileobj, str):
|
| 16 |
# fileobj = paropen(fileobj, 'w')
|
| 17 |
|
| 18 |
if isinstance(atoms, list): |
| 19 |
if len(atoms) > 1: |
| 20 |
raise ValueError('Can only write one configuration to a VTI file!') |
| 21 |
atoms = atoms[0]
|
| 22 |
|
| 23 |
if data is None: |
| 24 |
raise ValueError('VTK XML Image Data (VTI) format requires data!') |
| 25 |
|
| 26 |
data = np.asarray(data) |
| 27 |
|
| 28 |
if data.dtype == complex: |
| 29 |
data = np.abs(data) |
| 30 |
|
| 31 |
cell = atoms.get_cell() |
| 32 |
|
| 33 |
assert np.all(cell==np.diag(cell.diagonal())), 'Unit cell must be orthogonal' |
| 34 |
|
| 35 |
bbox = np.array(zip(np.zeros(3),cell.diagonal())).ravel() |
| 36 |
|
| 37 |
# Create a VTK grid of structured points
|
| 38 |
spts = vtkStructuredPoints() |
| 39 |
spts.SetWholeBoundingBox(bbox) |
| 40 |
spts.SetDimensions(data.shape) |
| 41 |
spts.SetSpacing(cell.diagonal() / data.shape) |
| 42 |
#spts.SetSpacing(paw.gd.h_c * Bohr)
|
| 43 |
|
| 44 |
#print 'paw.gd.h_c * Bohr=',paw.gd.h_c * Bohr
|
| 45 |
#print 'atoms.cell.diagonal() / data.shape=', cell.diagonal()/data.shape
|
| 46 |
#assert np.all(paw.gd.h_c * Bohr==cell.diagonal()/data.shape)
|
| 47 |
|
| 48 |
#s = paw.wfs.kpt_u[0].psit_nG[0].copy()
|
| 49 |
#data = paw.get_pseudo_wave_function(band=0, kpt=0, spin=0, pad=False)
|
| 50 |
#spts.point_data.scalars = data.swapaxes(0,2).flatten()
|
| 51 |
#spts.point_data.scalars.name = 'scalars'
|
| 52 |
|
| 53 |
# Allocate a VTK array of type double and copy data
|
| 54 |
da = vtkDoubleArray() |
| 55 |
da.SetName('scalars')
|
| 56 |
da.SetNumberOfComponents(1)
|
| 57 |
da.SetNumberOfTuples(np.prod(data.shape)) |
| 58 |
|
| 59 |
for i,d in enumerate(data.swapaxes(0,2).flatten()): |
| 60 |
da.SetTuple1(i,d) |
| 61 |
|
| 62 |
# Assign the VTK array as point data of the grid
|
| 63 |
spd = spts.GetPointData() # type(spd) is vtkPointData
|
| 64 |
spd.SetScalars(da) |
| 65 |
|
| 66 |
"""
|
| 67 |
from vtk.util.vtkImageImportFromArray import vtkImageImportFromArray
|
| 68 |
iia = vtkImageImportFromArray()
|
| 69 |
#iia.SetArray(Numeric_asarray(data.swapaxes(0,2).flatten()))
|
| 70 |
iia.SetArray(Numeric_asarray(data))
|
| 71 |
ida = iia.GetOutput()
|
| 72 |
ipd = ida.GetPointData()
|
| 73 |
ipd.SetName('scalars')
|
| 74 |
spd.SetScalars(ipd.GetScalars())
|
| 75 |
"""
|
| 76 |
|
| 77 |
# Save the ImageData dataset to a VTK XML file.
|
| 78 |
w = vtkXMLImageDataWriter() |
| 79 |
|
| 80 |
if fast:
|
| 81 |
w.SetDataModeToAppend() |
| 82 |
w.EncodeAppendedDataOff() |
| 83 |
else:
|
| 84 |
w.SetDataModeToAscii() |
| 85 |
|
| 86 |
w.SetFileName(filename) |
| 87 |
w.SetInput(spts) |
| 88 |
w.Write() |
| 89 |
|
| 90 |
# -------------------------------------------------------------------
|
| 91 |
|
| 92 |
from vtk import vtkStructuredGrid, vtkPoints, vtkXMLStructuredGridWriter |
| 93 |
|
| 94 |
def write_vts(filename, atoms, data=None): |
| 95 |
raise NotImplementedError |
| 96 |
|
| 97 |
# -------------------------------------------------------------------
|
| 98 |
|
| 99 |
from vtk import vtkUnstructuredGrid, vtkPoints, vtkXMLUnstructuredGridWriter |
| 100 |
|
| 101 |
def write_vtu(filename, atoms, data=None): |
| 102 |
|
| 103 |
#if isinstance(fileobj, str):
|
| 104 |
# fileobj = paropen(fileobj, 'w')
|
| 105 |
|
| 106 |
if isinstance(atoms, list): |
| 107 |
if len(atoms) > 1: |
| 108 |
raise ValueError('Can only write one configuration to a VTI file!') |
| 109 |
atoms = atoms[0]
|
| 110 |
|
| 111 |
"""
|
| 112 |
if data is None:
|
| 113 |
raise ValueError('VTK XML Unstructured Grid (VTI) format requires data!')
|
| 114 |
|
| 115 |
data = np.asarray(data)
|
| 116 |
|
| 117 |
if data.dtype == complex:
|
| 118 |
data = np.abs(data)
|
| 119 |
"""
|
| 120 |
|
| 121 |
cell = atoms.get_cell() |
| 122 |
|
| 123 |
assert np.all(cell==np.diag(cell.diagonal())), 'Unit cell must be orthogonal' #TODO bounding box with general unit cell?! |
| 124 |
|
| 125 |
bbox = np.array(zip(np.zeros(3),cell.diagonal())).ravel() |
| 126 |
|
| 127 |
# Create a VTK grid of structured points
|
| 128 |
ugd = vtkUnstructuredGrid() |
| 129 |
ugd.SetWholeBoundingBox(bbox) |
| 130 |
|
| 131 |
"""
|
| 132 |
# Allocate a VTK array of type double and copy data
|
| 133 |
da = vtkDoubleArray()
|
| 134 |
da.SetName('scalars')
|
| 135 |
da.SetNumberOfComponents(3)
|
| 136 |
da.SetNumberOfTuples(len(atoms))
|
| 137 |
|
| 138 |
for i,pos in enumerate(atoms.get_positions()):
|
| 139 |
da.SetTuple3(i,pos[0],pos[1],pos[2])
|
| 140 |
"""
|
| 141 |
p = vtkPoints() |
| 142 |
p.SetNumberOfPoints(len(atoms))
|
| 143 |
p.SetDataTypeToDouble() |
| 144 |
for i,pos in enumerate(atoms.get_positions()): |
| 145 |
p.InsertPoint(i,pos[0],pos[1],pos[2]) |
| 146 |
|
| 147 |
|
| 148 |
ugd.SetPoints(p) |
| 149 |
|
| 150 |
# Assign the VTK array as point data of the grid
|
| 151 |
#upd = ugd.GetPointData() # type(spd) is vtkPointData
|
| 152 |
#upd.SetScalars(da)
|
| 153 |
|
| 154 |
|
| 155 |
# Save the UnstructuredGrid dataset to a VTK XML file.
|
| 156 |
w = vtkXMLUnstructuredGridWriter() |
| 157 |
|
| 158 |
if fast:
|
| 159 |
w.SetDataModeToAppend() |
| 160 |
w.EncodeAppendedDataOff() |
| 161 |
else:
|
| 162 |
w.GetCompressor().SetCompressionLevel(0)
|
| 163 |
w.SetDataModeToAscii() |
| 164 |
|
| 165 |
w.SetFileName(filename) |
| 166 |
w.SetInput(ugd) |
| 167 |
w.Write() |
| 168 |
|
| 169 |
|
| 170 |
# -------------------------------------------------------------------
|
| 171 |
|
| 172 |
def read_vti(filename): |
| 173 |
raise NotImplementedError |
| 174 |
|
| 175 |
def read_vts(filename): |
| 176 |
raise NotImplementedError |
| 177 |
|
| 178 |
def read_vtu(filename): |
| 179 |
raise NotImplementedError |
| 180 |
|
| 181 |
# -------------------------------------------------------------------
|
| 182 |
|
| 183 |
from vtk import vtkXMLFileReadTester |
| 184 |
|
| 185 |
def probe_vtkxml(filename): |
| 186 |
"""something..."""
|
| 187 |
|
| 188 |
r = vtkXMLFileReadTester() |
| 189 |
r.SetFileName(filename) |
| 190 |
|
| 191 |
if r.TestReadFile():
|
| 192 |
return r.GetFileDataType()
|
| 193 |
else:
|
| 194 |
return None |
| 195 |
|