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
|
2021-04-02 11:37:22 +05:30
|
|
|
import numpy.ma as ma
|
2020-03-31 14:34:06 +05:30
|
|
|
|
|
|
|
from damask import VTK
|
2020-06-28 03:07:46 +05:30
|
|
|
from damask import grid_filters
|
2020-03-31 14:34:06 +05:30
|
|
|
|
|
|
|
@pytest.fixture
|
2020-11-30 01:20:41 +05:30
|
|
|
def ref_path(ref_path_base):
|
2020-03-31 14:34:06 +05:30
|
|
|
"""Directory containing reference results."""
|
2020-11-30 01:20:41 +05:30
|
|
|
return ref_path_base/'VTK'
|
2020-03-31 14:34:06 +05:30
|
|
|
|
2020-08-25 11:19:56 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def default():
|
|
|
|
"""Simple VTK."""
|
2020-12-04 02:28:24 +05:30
|
|
|
cells = np.array([5,6,7],int)
|
|
|
|
size = np.array([.6,1.,.5])
|
|
|
|
return VTK.from_rectilinear_grid(cells,size)
|
2020-08-25 11:19:56 +05:30
|
|
|
|
2020-03-31 14:34:06 +05:30
|
|
|
class TestVTK:
|
|
|
|
|
2020-08-25 11:19:56 +05:30
|
|
|
@pytest.fixture(autouse=True)
|
2020-11-15 16:19:52 +05:30
|
|
|
def _patch_execution_stamp(self, patch_execution_stamp):
|
2020-08-25 11:19:56 +05:30
|
|
|
print('patched damask.util.execution_stamp')
|
|
|
|
|
2020-03-31 14:34:06 +05:30
|
|
|
def test_rectilinearGrid(self,tmp_path):
|
2020-12-04 02:28:24 +05:30
|
|
|
cells = np.random.randint(5,10,3)*2
|
2020-03-31 14:34:06 +05:30
|
|
|
size = np.random.random(3) + 1.0
|
|
|
|
origin = np.random.random(3)
|
2020-12-04 02:28:24 +05:30
|
|
|
v = VTK.from_rectilinear_grid(cells,size,origin)
|
2020-06-03 14:33:31 +05:30
|
|
|
string = v.__repr__()
|
2020-09-15 10:28:06 +05:30
|
|
|
v.save(tmp_path/'rectilinearGrid',False)
|
|
|
|
vtr = VTK.load(tmp_path/'rectilinearGrid.vtr')
|
2020-06-28 03:07:46 +05:30
|
|
|
with open(tmp_path/'rectilinearGrid.vtk','w') as f:
|
2020-06-03 14:33:31 +05:30
|
|
|
f.write(string)
|
2020-09-15 10:28:06 +05:30
|
|
|
vtk = VTK.load(tmp_path/'rectilinearGrid.vtk','VTK_rectilinearGrid')
|
2020-06-03 14:33:31 +05:30
|
|
|
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-10-27 18:12:49 +05:30
|
|
|
v = VTK.from_poly_data(points)
|
2020-06-03 14:33:31 +05:30
|
|
|
string = v.__repr__()
|
2020-09-15 10:28:06 +05:30
|
|
|
v.save(tmp_path/'polyData',False)
|
|
|
|
vtp = VTK.load(tmp_path/'polyData.vtp')
|
2020-06-28 03:07:46 +05:30
|
|
|
with open(tmp_path/'polyData.vtk','w') as f:
|
2020-06-03 14:33:31 +05:30
|
|
|
f.write(string)
|
2020-09-15 10:28:06 +05:30
|
|
|
vtk = VTK.load(tmp_path/'polyData.vtk','polyData')
|
2020-06-03 14:33:31 +05:30
|
|
|
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)
|
2020-10-27 18:12:49 +05:30
|
|
|
v = VTK.from_unstructured_grid(nodes,connectivity,cell_type)
|
2020-06-03 14:33:31 +05:30
|
|
|
string = v.__repr__()
|
2020-09-15 10:28:06 +05:30
|
|
|
v.save(tmp_path/'unstructuredGrid',False)
|
|
|
|
vtu = VTK.load(tmp_path/'unstructuredGrid.vtu')
|
2020-06-28 03:07:46 +05:30
|
|
|
with open(tmp_path/'unstructuredGrid.vtk','w') as f:
|
2020-06-03 14:33:31 +05:30
|
|
|
f.write(string)
|
2020-09-15 10:28:06 +05:30
|
|
|
vtk = VTK.load(tmp_path/'unstructuredGrid.vtk','unstructuredgrid')
|
2020-06-03 14:33:31 +05:30
|
|
|
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)
|
2020-10-27 18:12:49 +05:30
|
|
|
v = VTK.from_poly_data(points)
|
2020-06-28 22:53:17 +05:30
|
|
|
fname_s = tmp_path/'single.vtp'
|
|
|
|
fname_p = tmp_path/'parallel.vtp'
|
2020-09-15 10:28:06 +05:30
|
|
|
v.save(fname_s,False)
|
|
|
|
v.save(fname_p,True)
|
2020-06-26 15:15:54 +05:30
|
|
|
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)
|
|
|
|
|
2020-11-14 22:24:47 +05:30
|
|
|
def test_compress(self,tmp_path):
|
|
|
|
points = np.random.rand(102,3)
|
|
|
|
v = VTK.from_poly_data(points)
|
2020-11-16 21:04:49 +05:30
|
|
|
fname_c = tmp_path/'compressed.vtp'
|
2020-11-14 22:24:47 +05:30
|
|
|
fname_p = tmp_path/'plain.vtp'
|
|
|
|
v.save(fname_c,parallel=False,compress=False)
|
|
|
|
v.save(fname_p,parallel=False,compress=True)
|
|
|
|
assert(VTK.load(fname_c).__repr__() == VTK.load(fname_p).__repr__())
|
|
|
|
|
2020-06-26 15:15:54 +05:30
|
|
|
|
2020-11-04 22:38:04 +05:30
|
|
|
@pytest.mark.parametrize('fname',['a','a.vtp','a.b','a.b.vtp'])
|
|
|
|
def test_filename_variations(self,tmp_path,fname):
|
|
|
|
points = np.random.rand(102,3)
|
|
|
|
v = VTK.from_poly_data(points)
|
|
|
|
v.save(tmp_path/fname)
|
|
|
|
|
2020-11-19 15:09:41 +05:30
|
|
|
@pytest.mark.parametrize('fname,dataset_type',[('a_file.vtk', None),
|
|
|
|
('a_file.vtk','vtk'),
|
|
|
|
('a_file.vtx', None)])
|
|
|
|
def test_invalid_dataset_type(self,tmp_path,fname,dataset_type):
|
|
|
|
open(tmp_path/fname,'a').close()
|
2020-08-25 11:19:56 +05:30
|
|
|
with pytest.raises(TypeError):
|
2020-11-19 15:09:41 +05:30
|
|
|
VTK.load(tmp_path/fname,dataset_type)
|
|
|
|
|
|
|
|
def test_file_not_found(self):
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
|
|
VTK.load('/dev/null')
|
2020-08-25 11:19:56 +05:30
|
|
|
|
2020-11-06 02:08:00 +05:30
|
|
|
def test_add_extension(self,tmp_path,default):
|
|
|
|
default.save(tmp_path/'default.txt',parallel=False)
|
|
|
|
assert os.path.isfile(tmp_path/'default.txt.vtr')
|
2020-08-25 11:19:56 +05:30
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_get(self,default):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
default.get('does_not_exist')
|
|
|
|
|
2020-08-25 20:47:49 +05:30
|
|
|
def test_invalid_add_shape(self,default):
|
2020-08-25 11:19:56 +05:30
|
|
|
with pytest.raises(ValueError):
|
2020-08-25 20:47:49 +05:30
|
|
|
default.add(np.ones(3),'valid')
|
|
|
|
|
|
|
|
def test_invalid_add_missing_label(self,default):
|
|
|
|
data = np.random.randint(9,size=np.prod(np.array(default.vtk_data.GetDimensions())-1))
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
default.add(data)
|
2020-08-25 11:19:56 +05:30
|
|
|
|
|
|
|
def test_invalid_add_type(self,default):
|
2020-06-03 14:33:31 +05:30
|
|
|
with pytest.raises(TypeError):
|
2020-08-25 20:47:49 +05:30
|
|
|
default.add('invalid_type','valid')
|
2020-06-28 03:07:46 +05:30
|
|
|
|
2021-04-25 00:54:26 +05:30
|
|
|
@pytest.mark.parametrize('data_type,shape',[(float,(3,)),
|
|
|
|
(float,(3,3)),
|
|
|
|
(float,(1,)),
|
|
|
|
(int,(4,)),
|
|
|
|
(str,(1,))])
|
|
|
|
@pytest.mark.parametrize('N_values',[5*6*7,6*7*8])
|
|
|
|
def test_add_get(self,default,data_type,shape,N_values):
|
|
|
|
data = np.squeeze(np.random.randint(0,100,(N_values,)+shape)).astype(data_type)
|
|
|
|
default.add(data,'data')
|
|
|
|
assert (np.squeeze(data.reshape(N_values,-1)) == default.get('data')).all()
|
|
|
|
|
|
|
|
|
2021-04-02 11:37:22 +05:30
|
|
|
def test_add_masked(self,default):
|
|
|
|
data = np.random.rand(5*6*7,3)
|
|
|
|
masked = ma.MaskedArray(data,mask=data<.4,fill_value=42.)
|
|
|
|
default.add(masked,'D')
|
|
|
|
result_masked = str(default)
|
|
|
|
default.add(np.where(masked.mask,masked.fill_value,masked),'D')
|
|
|
|
assert result_masked == str(default)
|
|
|
|
|
|
|
|
|
2020-08-25 11:19:56 +05:30
|
|
|
def test_comments(self,tmp_path,default):
|
|
|
|
default.add_comments(['this is a comment'])
|
2020-09-15 10:28:06 +05:30
|
|
|
default.save(tmp_path/'with_comments',parallel=False)
|
|
|
|
new = VTK.load(tmp_path/'with_comments.vtr')
|
2020-08-25 11:19:56 +05:30
|
|
|
assert new.get_comments() == ['this is a comment']
|
2020-06-28 03:07:46 +05:30
|
|
|
|
2020-11-30 01:20:41 +05:30
|
|
|
def test_compare_reference_polyData(self,update,ref_path,tmp_path):
|
2020-06-28 03:07:46 +05:30
|
|
|
points=np.dstack((np.linspace(0.,1.,10),np.linspace(0.,2.,10),np.linspace(-1.,1.,10))).squeeze()
|
2020-10-27 18:12:49 +05:30
|
|
|
polyData = VTK.from_poly_data(points)
|
2020-06-28 03:07:46 +05:30
|
|
|
polyData.add(points,'coordinates')
|
|
|
|
if update:
|
2020-11-30 01:20:41 +05:30
|
|
|
polyData.save(ref_path/'polyData')
|
2020-06-28 03:07:46 +05:30
|
|
|
else:
|
2020-11-30 01:20:41 +05:30
|
|
|
reference = VTK.load(ref_path/'polyData.vtp')
|
2020-08-25 11:19:56 +05:30
|
|
|
assert polyData.__repr__() == reference.__repr__() and \
|
|
|
|
np.allclose(polyData.get('coordinates'),points)
|
2020-06-28 03:07:46 +05:30
|
|
|
|
2020-11-30 01:20:41 +05:30
|
|
|
def test_compare_reference_rectilinearGrid(self,update,ref_path,tmp_path):
|
2020-12-04 02:28:24 +05:30
|
|
|
cells = np.array([5,6,7],int)
|
|
|
|
size = np.array([.6,1.,.5])
|
|
|
|
rectilinearGrid = VTK.from_rectilinear_grid(cells,size)
|
2020-12-04 03:30:49 +05:30
|
|
|
c = grid_filters.coordinates0_point(cells,size).reshape(-1,3,order='F')
|
|
|
|
n = grid_filters.coordinates0_node(cells,size).reshape(-1,3,order='F')
|
2020-06-28 03:07:46 +05:30
|
|
|
rectilinearGrid.add(c,'cell')
|
|
|
|
rectilinearGrid.add(n,'node')
|
|
|
|
if update:
|
2020-11-30 01:20:41 +05:30
|
|
|
rectilinearGrid.save(ref_path/'rectilinearGrid')
|
2020-06-28 03:07:46 +05:30
|
|
|
else:
|
2020-11-30 01:20:41 +05:30
|
|
|
reference = VTK.load(ref_path/'rectilinearGrid.vtr')
|
2020-08-25 11:19:56 +05:30
|
|
|
assert rectilinearGrid.__repr__() == reference.__repr__() and \
|
|
|
|
np.allclose(rectilinearGrid.get('cell'),c)
|