2020-03-31 14:34:06 +05:30
|
|
|
import os
|
2020-06-26 15:15:54 +05:30
|
|
|
import filecmp
|
|
|
|
import time
|
2020-03-31 14:34:06 +05:30
|
|
|
|
|
|
|
import pytest
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from damask import VTK
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def reference_dir(reference_dir_base):
|
|
|
|
"""Directory containing reference results."""
|
|
|
|
return os.path.join(reference_dir_base,'Result')
|
|
|
|
|
|
|
|
class TestVTK:
|
|
|
|
|
|
|
|
def test_rectilinearGrid(self,tmp_path):
|
|
|
|
grid = np.random.randint(5,10,3)*2
|
|
|
|
size = np.random.random(3) + 1.0
|
|
|
|
origin = np.random.random(3)
|
|
|
|
v = VTK.from_rectilinearGrid(grid,size,origin)
|
2020-06-03 14:33:31 +05:30
|
|
|
string = v.__repr__()
|
2020-06-26 15:15:54 +05:30
|
|
|
v.write(os.path.join(tmp_path,'rectilinearGrid'),False)
|
2020-06-03 14:33:31 +05:30
|
|
|
vtr = VTK.from_file(os.path.join(tmp_path,'rectilinearGrid.vtr'))
|
|
|
|
with open(os.path.join(tmp_path,'rectilinearGrid.vtk'),'w') as f:
|
|
|
|
f.write(string)
|
|
|
|
vtk = VTK.from_file(os.path.join(tmp_path,'rectilinearGrid.vtk'),'VTK_rectilinearGrid')
|
|
|
|
assert(string == vtr.__repr__() == vtk.__repr__())
|
2020-03-31 14:34:06 +05:30
|
|
|
|
|
|
|
def test_polyData(self,tmp_path):
|
2020-06-26 15:15:54 +05:30
|
|
|
points = np.random.rand(100,3)
|
2020-03-31 14:34:06 +05:30
|
|
|
v = VTK.from_polyData(points)
|
2020-06-03 14:33:31 +05:30
|
|
|
string = v.__repr__()
|
2020-06-26 15:15:54 +05:30
|
|
|
v.write(os.path.join(tmp_path,'polyData'),False)
|
2020-06-03 14:33:31 +05:30
|
|
|
vtp = VTK.from_file(os.path.join(tmp_path,'polyData.vtp'))
|
|
|
|
with open(os.path.join(tmp_path,'polyData.vtk'),'w') as f:
|
|
|
|
f.write(string)
|
|
|
|
vtk = VTK.from_file(os.path.join(tmp_path,'polyData.vtk'),'polyData')
|
|
|
|
assert(string == vtp.__repr__() == vtk.__repr__())
|
2020-03-31 14:34:06 +05:30
|
|
|
|
|
|
|
@pytest.mark.parametrize('cell_type,n',[
|
|
|
|
('VTK_hexahedron',8),
|
|
|
|
('TETRA',4),
|
|
|
|
('quad',4),
|
|
|
|
('VTK_TRIANGLE',3)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_unstructuredGrid(self,tmp_path,cell_type,n):
|
|
|
|
nodes = np.random.rand(n,3)
|
|
|
|
connectivity = np.random.choice(np.arange(n),n,False).reshape(-1,n)
|
|
|
|
v = VTK.from_unstructuredGrid(nodes,connectivity,cell_type)
|
2020-06-03 14:33:31 +05:30
|
|
|
string = v.__repr__()
|
2020-06-26 15:15:54 +05:30
|
|
|
v.write(os.path.join(tmp_path,'unstructuredGrid'),False)
|
2020-06-03 14:33:31 +05:30
|
|
|
vtu = VTK.from_file(os.path.join(tmp_path,'unstructuredGrid.vtu'))
|
|
|
|
with open(os.path.join(tmp_path,'unstructuredGrid.vtk'),'w') as f:
|
|
|
|
f.write(string)
|
|
|
|
vtk = VTK.from_file(os.path.join(tmp_path,'unstructuredGrid.vtk'),'unstructuredgrid')
|
|
|
|
assert(string == vtu.__repr__() == vtk.__repr__())
|
|
|
|
|
2020-06-26 15:15:54 +05:30
|
|
|
|
|
|
|
def test_parallel_out(self,tmp_path):
|
|
|
|
points = np.random.rand(102,3)
|
|
|
|
v = VTK.from_polyData(points)
|
|
|
|
fname_s = os.path.join(tmp_path,'single.vtp')
|
|
|
|
fname_p = os.path.join(tmp_path,'parallel.vtp')
|
|
|
|
v.write(fname_s,False)
|
|
|
|
v.write(fname_p,True)
|
|
|
|
for i in range(10):
|
|
|
|
if os.path.isfile(fname_p) and filecmp.cmp(fname_s,fname_p):
|
|
|
|
assert(True)
|
|
|
|
return
|
|
|
|
time.sleep(.5)
|
|
|
|
assert(False)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('name,dataset_type',[('this_file_does_not_exist.vtk', None),
|
2020-06-03 14:33:31 +05:30
|
|
|
('this_file_does_not_exist.vtk','vtk'),
|
|
|
|
('this_file_does_not_exist.vtx', None)])
|
|
|
|
def test_invalid_dataset_type(self,dataset_type,name):
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
VTK.from_file('this_file_does_not_exist.vtk',dataset_type)
|