more tests

This commit is contained in:
Martin Diehl 2020-07-31 16:50:01 +02:00
parent aadce1e554
commit 50d7842dbe
2 changed files with 29 additions and 8 deletions

View File

@ -509,7 +509,7 @@ class Result:
else: else:
return dataset return dataset
@property
def cell_coordinates(self): def cell_coordinates(self):
"""Return initial coordinates of the cell centers.""" """Return initial coordinates of the cell centers."""
if self.structured: if self.structured:
@ -518,6 +518,7 @@ class Result:
with h5py.File(self.fname,'r') as f: with h5py.File(self.fname,'r') as f:
return f['geometry/x_c'][()] return f['geometry/x_c'][()]
@property
def node_coordinates(self): def node_coordinates(self):
"""Return initial coordinates of the cell centers.""" """Return initial coordinates of the cell centers."""
if self.structured: if self.structured:

View File

@ -10,13 +10,14 @@ import h5py
import damask import damask
from damask import Result from damask import Result
from damask import mechanics from damask import mechanics
from damask import grid_filters
@pytest.fixture @pytest.fixture
def default(tmp_path,reference_dir): def default(tmp_path,reference_dir):
"""Small Result file in temp location for modification.""" """Small Result file in temp location for modification."""
fname = '12grains6x7x8_tensionY.hdf5' fname = '12grains6x7x8_tensionY.hdf5'
shutil.copy(os.path.join(reference_dir,fname),tmp_path) shutil.copy(reference_dir/fname,tmp_path)
f = Result(os.path.join(tmp_path,fname)) f = Result(tmp_path/fname)
f.pick('times',20.0) f.pick('times',20.0)
return f return f
@ -24,13 +25,13 @@ def default(tmp_path,reference_dir):
def single_phase(tmp_path,reference_dir): def single_phase(tmp_path,reference_dir):
"""Single phase Result file in temp location for modification.""" """Single phase Result file in temp location for modification."""
fname = '6grains6x7x8_single_phase_tensionY.hdf5' fname = '6grains6x7x8_single_phase_tensionY.hdf5'
shutil.copy(os.path.join(reference_dir,fname),tmp_path) shutil.copy(reference_dir/fname,tmp_path)
return Result(os.path.join(tmp_path,fname)) return Result(tmp_path/fname)
@pytest.fixture @pytest.fixture
def reference_dir(reference_dir_base): def reference_dir(reference_dir_base):
"""Directory containing reference results.""" """Directory containing reference results."""
return os.path.join(reference_dir_base,'Result') return reference_dir_base/'Result'
class TestResult: class TestResult:
@ -98,8 +99,17 @@ class TestResult:
in_file = default.read_dataset(loc['|Fe|'],0) in_file = default.read_dataset(loc['|Fe|'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_calculation(self,default): @pytest.mark.parametrize('mode',['direct','function'])
def test_add_calculation(self,default,mode):
def my_func(field):
return 2.0*np.abs(field)-1.0
if mode == 'direct':
default.add_calculation('x','2.0*np.abs(#F#)-1.0','-','my notes') default.add_calculation('x','2.0*np.abs(#F#)-1.0','-','my notes')
else:
default.enable_user_function(my_func)
default.add_calculation('x','my_func(#F#)','-','my notes')
loc = {'F': default.get_dataset_location('F'), loc = {'F': default.get_dataset_location('F'),
'x': default.get_dataset_location('x')} 'x': default.get_dataset_location('x')}
in_memory = 2.0*np.abs(default.read_dataset(loc['F'],0))-1.0 in_memory = 2.0*np.abs(default.read_dataset(loc['F'],0))-1.0
@ -312,6 +322,16 @@ class TestResult:
with pytest.raises(PermissionError): with pytest.raises(PermissionError):
default.rename('P','another_new_name') default.rename('P','another_new_name')
@pytest.mark.parametrize('mode',['cell','node'])
def test_coordinates(self,default,mode):
if mode == 'cell':
a = grid_filters.cell_coord0(default.grid,default.size,default.origin)
b = default.cell_coordinates.reshape(tuple(default.grid)+(3,),order='F')
elif mode == 'node':
a = grid_filters.node_coord0(default.grid,default.size,default.origin)
b = default.node_coordinates.reshape(tuple(default.grid+1)+(3,),order='F')
assert np.allclose(a,b)
@pytest.mark.parametrize('output',['F',[],['F','P']]) @pytest.mark.parametrize('output',['F',[],['F','P']])
def test_vtk(self,tmp_path,default,output): def test_vtk(self,tmp_path,default,output):
os.chdir(tmp_path) os.chdir(tmp_path)