Commit c11e7987 authored by Kenneth Loenbaek's avatar Kenneth Loenbaek
Browse files

Added XXD2vtk

Added sim2pyvista, XXD2pyvista
parent 1aac7c20
from . import ellipsys2dict
from . import ellipsys2vtk
\ No newline at end of file
from . import ellipsys2vtk
from .ellipsys2pyvista import XXD2pyvista, sim2pyvista
\ No newline at end of file
import pyvista as pv
from .ellipsys2vtk import sim2vtk, XXD2vtk
def XXD2pyvista(filname, load_attr=True, blocks=None, load_ghost=False, is2D=None, mblock=None):
return pv.MultiBlock(XXD2vtk(filname, load_attr, blocks, load_ghost, is2D, mblock))
def sim2pyvista(filename_RST, filename_XXD=None, load_names=None, read_default=True, blocks=None, load_ghost=False,
is2D=None, vectors=None, mblock=None):
return pv.MultiBlock(sim2vtk(filename_RST, filename_XXD, load_names, read_default, blocks, load_ghost,
is2D, vectors, mblock))
\ No newline at end of file
from vtk.numpy_interface import dataset_adapter as dsa
import numpy as np
from vtk import (vtkMultiBlockDataSet, vtkPoints, vtkStructuredGrid,
vtkTransformFilter, vtkTransform, vtkArrayCalculator, vtkCompositeDataSet)
from .ellipsys2dict import XXD2dict, sim2dict, RST_set_default_args
def XXD2vtk():
pass
def XXD2vtk(filname, load_attr=True, blocks=None, load_ghost=False, is2D=None, mblock=None):
# Initializing multiblock
if mblock is None:
mblock = vtkMultiBlockDataSet()
XXD, bsize, nblock_out = XXD2dict(filname, {"attr"} if load_attr else None, True, blocks, load_ghost, False, is2D)
# Computing domain size and number of blocks
ppbs = bsize + 3 if load_ghost else bsize + 1
if blocks is None:
blocks = range(1, nblock_out + 1)
nblock = len(blocks)
# Setting the number of blocks
mblock.SetNumberOfBlocks(nblock)
# Setting block data
for ib, i_block in enumerate(blocks):
# Converting numpy array to vtk points
points = vtkPoints()
points.SetData(dsa.numpyTovtkDataArray(XXD["points"][ib].T, "Points"))
def sim2vtk(filename_RST, filename_XXD=None, load_names=None, read_default=True, blocks=None, load_ghost=False, is2D=None, vectors=None, mblock=None):
# Creating the Structured Grid for the block
sgrid = vtkStructuredGrid()
if is2D:
sgrid.SetExtent(0, ppbs - 1, 0, ppbs - 1, 0, 0)
else:
sgrid.SetExtent(0, ppbs - 1, 0, ppbs - 1, 0, ppbs - 1)
sgrid.SetPoints(points)
# Loading data
if "attr" in XXD:
sgrid.GetPointData().AddArray(dsa.numpyTovtkDataArray(XXD["attr"][ib], "attr"))
# Adding the sgrid to mblock
mblock.SetBlock(ib, sgrid)
# Setting block name
mblock.GetMetaData(ib).Set(vtkCompositeDataSet.NAME(), "Block %s" % i_block)
return mblock
def sim2vtk(filename_RST, filename_XXD=None, load_names=None, read_default=True, blocks=None, load_ghost=False,
is2D=None, vectors=None, mblock=None):
# Initializing multiblock
if mblock is None:
......@@ -20,8 +59,8 @@ def sim2vtk(filename_RST, filename_XXD=None, load_names=None, read_default=True,
sim, bsize, nblock_out = sim2dict(filename_RST, filename_XXD, load_names, read_default, blocks, load_ghost,
False, is2D, vectors)
# Computing domain size and number of blocks
ppbs = bsize+3 if load_ghost else bsize+1
if blocks is None:
blocks = range(1, nblock_out+1)
nblock = len(blocks)
......@@ -46,6 +85,8 @@ def sim2vtk(filename_RST, filename_XXD=None, load_names=None, read_default=True,
# Loading data
for name in sim.keys():
if "points" == name:
continue
sgrid.GetPointData().AddArray(dsa.numpyTovtkDataArray(sim[name][ib], name))
# Adding the sgrid to mblock
......@@ -54,4 +95,4 @@ def sim2vtk(filename_RST, filename_XXD=None, load_names=None, read_default=True,
# Setting block name
mblock.GetMetaData(ib).Set(vtkCompositeDataSet.NAME(), "Block %s"%i_block)
return mblock
\ No newline at end of file
return mblock
import ellipsys2vtk
import unittest
import numpy as np
import pyvista as pv
class Testsim2vtk(unittest.TestCase):
class Testsim2pyvista(unittest.TestCase):
def test_default(self):
simdict, bsize, nblock = ellipsys2vtk.ellipsys2dict.sim2dict("ADsim.RST.01")
sim = pv.MultiBlock(ellipsys2vtk.ellipsys2vtk.sim2vtk("ADsim.RST.01"))
sim = ellipsys2vtk.sim2pyvista("ADsim.RST.01")
for name in simdict.keys(): # Testing if names are in dataset
if name == "points":
continue
sim[0][name]
np.testing.assert_equal(sim[0]["points"].shape, simdict["points"][0].shape)
np.testing.assert_equal(sim[0]["Vel"].shape, simdict["Vel"][0].shape)
# Testing names
for iblock in range(1, nblock+1):
self.assertEqual("Block %d"%iblock, sim.get_block_name(iblock-1))
......@@ -25,9 +26,11 @@ class Testsim2vtk(unittest.TestCase):
vectors = dict(velocity=["u", "v", "w"])
simdict, bsize, nblock = ellipsys2vtk.ellipsys2dict.sim2dict(fname_RST, fname_XXD, load_names, read_default,
blocks, load_ghost, False, is2D, vectors)
sim = pv.MultiBlock(ellipsys2vtk.ellipsys2vtk.sim2vtk(fname_RST, fname_XXD, load_names, read_default,
blocks, load_ghost, is2D, vectors))
sim = ellipsys2vtk.sim2pyvista(fname_RST, fname_XXD, load_names, read_default, blocks, load_ghost,
is2D, vectors)
for name in simdict.keys(): # Testing if names are in dataset
if name == "points":
continue
sim[0][name]
np.testing.assert_equal(sim[0]["velocity"].shape, simdict["velocity"][0].shape)
# Testing names
......@@ -45,15 +48,59 @@ class Testsim2vtk(unittest.TestCase):
vectors = dict(velocity=["u", "v", "w"])
simdict, bsize, nblock = ellipsys2vtk.ellipsys2dict.sim2dict(fname_RST, fname_XXD, load_names, read_default,
blocks, load_ghost, False, is2D, vectors)
sim = pv.MultiBlock(ellipsys2vtk.ellipsys2vtk.sim2vtk(fname_RST, fname_XXD, load_names, read_default,
blocks, load_ghost, is2D, vectors))
sim = ellipsys2vtk.sim2pyvista(fname_RST, fname_XXD, load_names, read_default, blocks, load_ghost,
is2D, vectors)
for name in simdict.keys(): # Testing if names are in dataset
if name == "points":
continue
sim[0][name]
np.testing.assert_equal(sim[0]["velocity"].shape, simdict["velocity"][0].shape)
# Testing names
for ib, iblock in enumerate(blocks):
self.assertEqual("Block %d"%iblock, sim.get_block_name(ib))
class TestXXD2pyvista(unittest.TestCase):
def test_default(self):
XXDdict, bsize, nblock = ellipsys2vtk.ellipsys2dict.XXD2dict("ADsim.X3D", {"attr"})
XXD = ellipsys2vtk.XXD2pyvista("ADsim.X3D")
for name in XXDdict.keys(): # Testing if names are in dataset
if name == "points":
continue
XXD[0][name]
np.testing.assert_equal(XXD[0]["attr"].shape, XXDdict["attr"][0].shape)
# Testing names
for iblock in range(1, nblock+1):
self.assertEqual("Block %d"%iblock, XXD.get_block_name(iblock-1))
def test_all_inputs(self):
filename = "ADsim.X3D"
load_attr = False
blocks = [1, 2] + list(range(5, 8))
load_ghost = True
is2D = False
XXDdict, bsize, nblock = ellipsys2vtk.ellipsys2dict.XXD2dict(filename, blocks=blocks, load_ghost=load_ghost,
is2D=is2D)
XXD = ellipsys2vtk.XXD2pyvista(filename, load_attr, blocks, load_ghost, is2D)
np.testing.assert_equal(XXD[0].points.T, XXDdict["points"][0])
# Testing names
for ib, iblock in enumerate(blocks):
self.assertEqual("Block %d"%iblock, XXD.get_block_name(ib))
def test_all_inputs_2D(self):
filename = "cyl.X2D"
load_attr = False
blocks = [1, 2] + list(range(5, 8))
load_ghost = True
is2D = True
XXDdict, bsize, nblock = ellipsys2vtk.ellipsys2dict.XXD2dict(filename, blocks=blocks, load_ghost=load_ghost,
is2D=is2D)
XXD = ellipsys2vtk.XXD2pyvista(filename, load_attr, blocks, load_ghost, is2D)
np.testing.assert_equal(XXD[0].points.T, XXDdict["points"][0])
# Testing names
for ib, iblock in enumerate(blocks):
self.assertEqual("Block %d" % iblock, XXD.get_block_name(ib))
if __name__ == "__main__":
test = Testsim2vtk()
test.test_all_inputs()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment