2020-03-31 14:34:06 +05:30
|
|
|
import os
|
2020-06-26 15:15:54 +05:30
|
|
|
import filecmp
|
|
|
|
import time
|
2022-02-22 18:30:36 +05:30
|
|
|
import string
|
2022-03-27 02:38:09 +05:30
|
|
|
import sys
|
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
|
2021-09-02 11:34:09 +05:30
|
|
|
import vtk
|
2020-03-31 14:34:06 +05:30
|
|
|
|
|
|
|
from damask import VTK
|
2022-02-16 03:08:02 +05:30
|
|
|
from damask import Table
|
2022-03-08 20:01:08 +05:30
|
|
|
from damask import Colormap
|
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])
|
2021-12-22 17:11:16 +05:30
|
|
|
return VTK.from_image_data(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')
|
|
|
|
|
2022-03-09 03:13:54 +05:30
|
|
|
@pytest.mark.parametrize('cmap',[Colormap.from_predefined('cividis'),'strain'])
|
2022-03-27 02:38:09 +05:30
|
|
|
@pytest.mark.skipif(sys.platform == 'win32', reason='DISPLAY has no effect on windows')
|
2022-03-08 20:01:08 +05:30
|
|
|
def test_show(sef,default,cmap,monkeypatch):
|
2022-01-16 12:48:06 +05:30
|
|
|
monkeypatch.delenv('DISPLAY',raising=False)
|
2022-03-08 20:01:08 +05:30
|
|
|
default.show(colormap=cmap)
|
2022-02-23 11:19:38 +05:30
|
|
|
|
|
|
|
def test_imageData(self,tmp_path):
|
|
|
|
cells = np.random.randint(5,10,3)
|
|
|
|
size = np.random.random(3) + 0.1
|
|
|
|
origin = np.random.random(3) - 0.5
|
|
|
|
v = VTK.from_image_data(cells,size,origin)
|
2022-02-25 05:45:28 +05:30
|
|
|
string = str(v)
|
2022-02-23 11:19:38 +05:30
|
|
|
string = v.as_ASCII()
|
|
|
|
v.save(tmp_path/'imageData',False)
|
|
|
|
vtr = VTK.load(tmp_path/'imageData.vti')
|
|
|
|
with open(tmp_path/'imageData.vtk','w') as f:
|
|
|
|
f.write(string)
|
|
|
|
vtk = VTK.load(tmp_path/'imageData.vtk','VTK_imageData')
|
|
|
|
assert (string == vtr.as_ASCII() == vtk.as_ASCII())
|
|
|
|
|
2020-03-31 14:34:06 +05:30
|
|
|
def test_rectilinearGrid(self,tmp_path):
|
2022-02-18 04:24:26 +05:30
|
|
|
grid = np.sort(np.random.random((3,10)))
|
|
|
|
v = VTK.from_rectilinear_grid(grid)
|
2022-02-25 05:45:28 +05:30
|
|
|
string = str(v)
|
2022-02-23 11:19:38 +05:30
|
|
|
string = v.as_ASCII()
|
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')
|
2022-02-23 11:19:38 +05:30
|
|
|
assert (string == vtr.as_ASCII() == vtk.as_ASCII())
|
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)
|
2022-02-25 05:45:28 +05:30
|
|
|
string = str(v)
|
2022-02-23 11:19:38 +05:30
|
|
|
string = v.as_ASCII()
|
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')
|
2022-02-23 11:19:38 +05:30
|
|
|
assert(string == vtp.as_ASCII() == vtk.as_ASCII())
|
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)
|
2022-02-25 05:45:28 +05:30
|
|
|
string = str(v)
|
2022-02-23 11:19:38 +05:30
|
|
|
string = v.as_ASCII()
|
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')
|
2022-02-23 11:19:38 +05:30
|
|
|
assert(string == vtu.as_ASCII() == vtk.as_ASCII())
|
2020-06-03 14:33:31 +05:30
|
|
|
|
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)
|
2022-02-23 11:19:38 +05:30
|
|
|
assert(VTK.load(fname_c).as_ASCII() == VTK.load(fname_p).as_ASCII())
|
2020-11-14 22:24:47 +05:30
|
|
|
|
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)
|
2021-12-22 17:11:16 +05:30
|
|
|
assert os.path.isfile(tmp_path/'default.txt.vti')
|
2020-08-25 11:19:56 +05:30
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_get(self,default):
|
2022-03-07 15:58:17 +05:30
|
|
|
with pytest.raises(KeyError):
|
2020-08-25 11:19:56 +05:30
|
|
|
default.get('does_not_exist')
|
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
def test_invalid_set_shape(self,default):
|
2020-08-25 11:19:56 +05:30
|
|
|
with pytest.raises(ValueError):
|
2022-05-12 03:49:10 +05:30
|
|
|
default.set('valid',np.ones(3))
|
2020-08-25 20:47:49 +05:30
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
def test_invalid_set_missing_label(self,default):
|
2020-08-25 20:47:49 +05:30
|
|
|
data = np.random.randint(9,size=np.prod(np.array(default.vtk_data.GetDimensions())-1))
|
|
|
|
with pytest.raises(ValueError):
|
2022-05-12 03:49:10 +05:30
|
|
|
default.set(data=data)
|
2020-08-25 11:19:56 +05:30
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
def test_invalid_set_type(self,default):
|
2020-06-03 14:33:31 +05:30
|
|
|
with pytest.raises(TypeError):
|
2022-05-12 03:49:10 +05:30
|
|
|
default.set(label='valid',data='invalid_type')
|
2022-03-12 06:37:18 +05:30
|
|
|
with pytest.raises(TypeError):
|
2022-05-12 03:49:10 +05:30
|
|
|
default.set(label='valid',table='invalid_type')
|
2022-03-12 06:37:18 +05:30
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
def test_invalid_set_dual(self,default):
|
2022-03-12 06:37:18 +05:30
|
|
|
with pytest.raises(KeyError):
|
2022-05-12 03:49:10 +05:30
|
|
|
default.set(label='valid',data=0,table=0)
|
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])
|
2022-05-12 03:49:10 +05:30
|
|
|
def test_set_get(self,default,data_type,shape,N_values):
|
2021-04-25 00:54:26 +05:30
|
|
|
data = np.squeeze(np.random.randint(0,100,(N_values,)+shape)).astype(data_type)
|
2022-05-12 03:49:10 +05:30
|
|
|
new = default.set('data',data)
|
2022-02-21 16:47:00 +05:30
|
|
|
assert (np.squeeze(data.reshape(N_values,-1)) == new.get('data')).all()
|
2021-04-25 00:54:26 +05:30
|
|
|
|
|
|
|
|
2022-02-16 03:08:02 +05:30
|
|
|
@pytest.mark.parametrize('shapes',[{'scalar':(1,),'vector':(3,),'tensor':(3,3)},
|
|
|
|
{'vector':(6,),'tensor':(3,3)},
|
|
|
|
{'tensor':(3,3),'scalar':(1,)}])
|
2022-05-12 03:49:10 +05:30
|
|
|
def test_set_table(self,default,shapes):
|
2022-02-16 03:08:02 +05:30
|
|
|
N = np.random.choice([default.N_points,default.N_cells])
|
2022-02-16 03:12:17 +05:30
|
|
|
d = dict()
|
2022-02-16 03:08:02 +05:30
|
|
|
for k,s in shapes.items():
|
2022-02-16 03:12:17 +05:30
|
|
|
d[k] = dict(shape = s,
|
|
|
|
data = np.random.random(N*np.prod(s)).reshape((N,-1)))
|
2022-05-12 03:49:10 +05:30
|
|
|
new = default.set(table=Table(shapes,np.column_stack([d[k]['data'] for k in shapes.keys()])))
|
2022-02-16 03:08:02 +05:30
|
|
|
for k,s in shapes.items():
|
2022-02-21 16:47:00 +05:30
|
|
|
assert np.allclose(np.squeeze(d[k]['data']),new.get(k),rtol=1e-7)
|
2022-02-16 03:08:02 +05:30
|
|
|
|
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
def test_set_masked(self,default):
|
2021-04-02 11:37:22 +05:30
|
|
|
data = np.random.rand(5*6*7,3)
|
|
|
|
masked = ma.MaskedArray(data,mask=data<.4,fill_value=42.)
|
2022-05-12 03:49:10 +05:30
|
|
|
mask_auto = default.set('D',masked)
|
|
|
|
mask_manual = default.set('D',np.where(masked.mask,masked.fill_value,masked))
|
2022-02-23 11:19:38 +05:30
|
|
|
assert mask_manual == mask_auto
|
2021-04-02 11:37:22 +05:30
|
|
|
|
|
|
|
|
2022-02-22 18:30:36 +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_labels(self,default,data_type,shape,N_values):
|
|
|
|
data = np.squeeze(np.random.randint(0,100,(N_values,)+shape)).astype(data_type)
|
|
|
|
ALPHABET = np.array(list(string.ascii_lowercase + ' '))
|
|
|
|
label = ''.join(np.random.choice(ALPHABET, size=10))
|
2022-05-12 03:49:10 +05:30
|
|
|
new = default.set(label,data)
|
2022-02-22 18:30:36 +05:30
|
|
|
if N_values == default.N_points: assert label in new.labels['Point Data']
|
|
|
|
if N_values == default.N_cells: assert label in new.labels['Cell Data']
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-25 11:19:56 +05:30
|
|
|
def test_comments(self,tmp_path,default):
|
2022-12-14 00:02:19 +05:30
|
|
|
default.comments += ['this is a comment']
|
2020-09-15 10:28:06 +05:30
|
|
|
default.save(tmp_path/'with_comments',parallel=False)
|
2021-12-22 17:11:16 +05:30
|
|
|
new = VTK.load(tmp_path/'with_comments.vti')
|
2022-02-14 19:49:09 +05:30
|
|
|
assert new.comments == ['this is a comment']
|
2020-06-28 03:07:46 +05:30
|
|
|
|
2021-09-02 11:34:09 +05:30
|
|
|
@pytest.mark.xfail(int(vtk.vtkVersion.GetVTKVersion().split('.')[0])<8, reason='missing METADATA')
|
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()
|
2022-05-12 03:49:10 +05:30
|
|
|
polyData = VTK.from_poly_data(points).set('coordinates',points)
|
2020-06-28 03:07:46 +05:30
|
|
|
if update:
|
2022-02-02 13:40:24 +05:30
|
|
|
polyData.save(ref_path/'polyData')
|
2020-06-28 03:07:46 +05:30
|
|
|
else:
|
2022-02-02 13:40:24 +05:30
|
|
|
reference = VTK.load(ref_path/'polyData.vtp')
|
2022-02-23 11:19:38 +05:30
|
|
|
assert polyData.as_ASCII() == reference.as_ASCII() and \
|
2022-02-02 13:40:24 +05:30
|
|
|
np.allclose(polyData.get('coordinates'),points)
|
2020-06-28 03:07:46 +05:30
|
|
|
|
2021-09-02 11:34:09 +05:30
|
|
|
@pytest.mark.xfail(int(vtk.vtkVersion.GetVTKVersion().split('.')[0])<8, reason='missing METADATA')
|
2020-11-30 01:20:41 +05:30
|
|
|
def test_compare_reference_rectilinearGrid(self,update,ref_path,tmp_path):
|
2022-02-18 04:24:26 +05:30
|
|
|
grid = [np.arange(4)**2.,
|
|
|
|
np.arange(5)**2.,
|
|
|
|
np.arange(6)**2.] # ParaView renders tetrahedral meshing unless using float coordinates!
|
|
|
|
coords = np.stack(np.meshgrid(*grid,indexing='ij'),axis=-1)
|
|
|
|
c = coords[:-1,:-1,:-1,:].reshape(-1,3,order='F')
|
|
|
|
n = coords[:,:,:,:].reshape(-1,3,order='F')
|
2022-02-21 16:47:00 +05:30
|
|
|
rectilinearGrid = VTK.from_rectilinear_grid(grid) \
|
2022-05-12 03:49:10 +05:30
|
|
|
.set('cell',np.ascontiguousarray(c)) \
|
|
|
|
.set('node',np.ascontiguousarray(n))
|
2020-06-28 03:07:46 +05:30
|
|
|
if update:
|
2022-02-02 13:40:24 +05:30
|
|
|
rectilinearGrid.save(ref_path/'rectilinearGrid')
|
2020-06-28 03:07:46 +05:30
|
|
|
else:
|
2022-02-02 13:40:24 +05:30
|
|
|
reference = VTK.load(ref_path/'rectilinearGrid.vtr')
|
2022-02-23 11:19:38 +05:30
|
|
|
assert rectilinearGrid.as_ASCII() == reference.as_ASCII() and \
|
2022-02-02 13:40:24 +05:30
|
|
|
np.allclose(rectilinearGrid.get('cell'),c)
|