simplified/using new functionality

This commit is contained in:
Martin Diehl 2021-04-03 13:11:20 +02:00
parent 27f2e3b26e
commit 6f3dc80079
3 changed files with 72 additions and 115 deletions

View File

@ -419,25 +419,22 @@ class Result:
un = 'Unit' if self.version_minor < 12 else 'unit' un = 'Unit' if self.version_minor < 12 else 'unit'
message = '' message = ''
with h5py.File(self.fname,'r') as f: with h5py.File(self.fname,'r') as f:
for i in self.iterate('increments'): for i in self.visible['increments']:
message += f'\n{i} ({self.times[self.increments.index(i)]}s)\n' message += f'\n{i} ({self.times[self.increments.index(i)]}s)\n'
for o,p in zip(['phases','homogenizations'],['fields','fields']): for o,p in zip(['phases','homogenizations'],['fields','fields']):
message += f' {o[:-1]}\n' message += f' {o[:-1]}\n'
for oo in self.iterate(o): for oo in self.visible[o]:
message += f' {oo}\n' message += f' {oo}\n'
for pp in self.iterate(p): for pp in self.visible[p]:
message += f' {pp}\n' message += f' {pp}\n'
group = '/'.join([i,o[:-1],oo,pp]) # o[:-1]: plural/singular issue for d in f['/'.join([i,o[:-1],oo,pp])].keys():
for d in f[group].keys(): dataset = f['/'.join([i,o[:-1],oo,pp,d])]
try: unit = f' / {dataset.attrs[un]}' if h5py3 else \
dataset = f['/'.join([group,d])] f' / {dataset.attrs[un].decode()}'
unit = f" / {dataset.attrs[un]}" if h5py3 else \ description = dataset.attrs[de] if h5py3 else \
f" / {dataset.attrs[un].decode()}" dataset.attrs[de].decode()
description = dataset.attrs[de] if h5py3 else \ message += f' {d}{unit}: {description}\n'
dataset.attrs[de].decode()
message += f' {d}{unit}: {description}\n'
except KeyError:
pass
return message return message
@ -445,7 +442,7 @@ class Result:
"""Return the location of all active datasets with given label.""" """Return the location of all active datasets with given label."""
path = [] path = []
with h5py.File(self.fname,'r') as f: with h5py.File(self.fname,'r') as f:
for i in self.iterate('increments'): for i in self.visible['increments']:
k = '/'.join([i,'geometry',label]) k = '/'.join([i,'geometry',label])
try: try:
f[k] f[k]
@ -453,8 +450,8 @@ class Result:
except KeyError: except KeyError:
pass pass
for o,p in zip(['phases','homogenizations'],['fields','fields']): for o,p in zip(['phases','homogenizations'],['fields','fields']):
for oo in self.iterate(o): for oo in self.visible[o]:
for pp in self.iterate(p): for pp in self.visible[p]:
k = '/'.join([i,o[:-1],oo,pp,label]) k = '/'.join([i,o[:-1],oo,pp,label])
try: try:
f[k] f[k]

View File

@ -399,7 +399,7 @@ def DREAM3D_cell_data_group(fname):
""" """
base_group = DREAM3D_base_group(fname) base_group = DREAM3D_base_group(fname)
with h5py.File(fname,'r') as f: with h5py.File(fname,'r') as f:
cells = tuple(f[os.path.join(base_group,'_SIMPL_GEOMETRY','DIMENSIONS')][()][::-1]) cells = tuple(f['/'.join((base_group,'_SIMPL_GEOMETRY','DIMENSIONS'))][()][::-1])
cell_data_group = f[base_group].visititems(lambda path,obj: path.split('/')[0] \ cell_data_group = f[base_group].visititems(lambda path,obj: path.split('/')[0] \
if isinstance(obj,h5py._hl.dataset.Dataset) and np.shape(obj)[:-1] == cells \ if isinstance(obj,h5py._hl.dataset.Dataset) and np.shape(obj)[:-1] == cells \
else None) else None)

View File

@ -59,39 +59,39 @@ class TestResult:
def test_view_all(self,default): def test_view_all(self,default):
default.view('increments',True) default.view('increments',True)
a = default.get_dataset_location('F') a = default.read('F')
default.view('increments','*') default.view('increments','*')
b = default.get_dataset_location('F') assert dict_equal(a,default.read('F'))
default.view('increments',default.increments_in_range(0,np.iinfo(int).max)) default.view('increments',default.increments_in_range(0,np.iinfo(int).max))
c = default.get_dataset_location('F') assert dict_equal(a,default.read('F'))
default.view('times',True) default.view('times',True)
d = default.get_dataset_location('F') assert dict_equal(a,default.read('F'))
default.view('times','*') default.view('times','*')
e = default.get_dataset_location('F') assert dict_equal(a,default.read('F'))
default.view('times',default.times_in_range(0.0,np.inf)) default.view('times',default.times_in_range(0.0,np.inf))
f = default.get_dataset_location('F') assert dict_equal(a,default.read('F'))
assert a == b == c == d == e ==f
@pytest.mark.parametrize('what',['increments','times','phases']) # ToDo: discuss homogenizations @pytest.mark.parametrize('what',['increments','times','phases']) # ToDo: discuss homogenizations
def test_view_none(self,default,what): def test_view_none(self,default,what):
default.view(what,False) default.view(what,False)
a = default.get_dataset_location('F') a = default.read('F')
default.view(what,[]) default.view(what,[])
b = default.get_dataset_location('F') b = default.read('F')
assert a == b == [] assert a == b == {}
@pytest.mark.parametrize('what',['increments','times','phases']) # ToDo: discuss homogenizations @pytest.mark.parametrize('what',['increments','times','phases']) # ToDo: discuss homogenizations
def test_view_more(self,default,what): def test_view_more(self,default,what):
default.view(what,False) default.view(what,False)
default.view_more(what,'*') default.view_more(what,'*')
a = default.get_dataset_location('F') a = default.read('F')
default.view(what,True) default.view(what,True)
b = default.get_dataset_location('F') b = default.read('F')
assert a == b assert dict_equal(a,b)
@pytest.mark.parametrize('what',['increments','times','phases']) # ToDo: discuss homogenizations @pytest.mark.parametrize('what',['increments','times','phases']) # ToDo: discuss homogenizations
def test_view_less(self,default,what): def test_view_less(self,default,what):
@ -110,10 +110,8 @@ class TestResult:
def test_add_absolute(self,default): def test_add_absolute(self,default):
default.add_absolute('F_e') default.add_absolute('F_e')
loc = {'F_e': default.get_dataset_location('F_e'), in_memory = np.abs(default.place('F_e'))
'|F_e|': default.get_dataset_location('|F_e|')} in_file = default.place('|F_e|')
in_memory = np.abs(default.read_dataset(loc['F_e'],0))
in_file = default.read_dataset(loc['|F_e|'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
@pytest.mark.parametrize('mode',['direct','function']) @pytest.mark.parametrize('mode',['direct','function'])
@ -129,56 +127,42 @@ class TestResult:
default.enable_user_function(f.my_func) default.enable_user_function(f.my_func)
default.add_calculation('x','my_func(#F#)','-','my notes') default.add_calculation('x','my_func(#F#)','-','my notes')
loc = {'F': default.get_dataset_location('F'), in_memory = 2.0*np.abs(default.place('F'))-1.0
'x': default.get_dataset_location('x')} in_file = default.place('x')
in_memory = 2.0*np.abs(default.read_dataset(loc['F'],0))-1.0
in_file = default.read_dataset(loc['x'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_stress_Cauchy(self,default): def test_add_stress_Cauchy(self,default):
default.add_stress_Cauchy('P','F') default.add_stress_Cauchy('P','F')
loc = {'F': default.get_dataset_location('F'), in_memory = mechanics.stress_Cauchy(default.place('P'), default.place('F'))
'P': default.get_dataset_location('P'), in_file = default.place('sigma')
'sigma':default.get_dataset_location('sigma')}
in_memory = mechanics.stress_Cauchy(default.read_dataset(loc['P'],0),
default.read_dataset(loc['F'],0))
in_file = default.read_dataset(loc['sigma'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_determinant(self,default): def test_add_determinant(self,default):
default.add_determinant('P') default.add_determinant('P')
loc = {'P': default.get_dataset_location('P'), in_memory = np.linalg.det(default.place('P'))
'det(P)':default.get_dataset_location('det(P)')} in_file = default.place('det(P)')
in_memory = np.linalg.det(default.read_dataset(loc['P'],0)).reshape(-1,1)
in_file = default.read_dataset(loc['det(P)'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_deviator(self,default): def test_add_deviator(self,default):
default.add_deviator('P') default.add_deviator('P')
loc = {'P' :default.get_dataset_location('P'), in_memory = tensor.deviatoric(default.place('P'))
's_P':default.get_dataset_location('s_P')} in_file = default.place('s_P')
in_memory = tensor.deviatoric(default.read_dataset(loc['P'],0))
in_file = default.read_dataset(loc['s_P'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
@pytest.mark.parametrize('eigenvalue,function',[('max',np.amax),('min',np.amin)]) @pytest.mark.parametrize('eigenvalue,function',[('max',np.amax),('min',np.amin)])
def test_add_eigenvalue(self,default,eigenvalue,function): def test_add_eigenvalue(self,default,eigenvalue,function):
default.add_stress_Cauchy('P','F') default.add_stress_Cauchy('P','F')
default.add_eigenvalue('sigma',eigenvalue) default.add_eigenvalue('sigma',eigenvalue)
loc = {'sigma' :default.get_dataset_location('sigma'), in_memory = function(tensor.eigenvalues(default.place('sigma')),axis=1)
'lambda':default.get_dataset_location(f'lambda_{eigenvalue}(sigma)')} in_file = default.place(f'lambda_{eigenvalue}(sigma)')
in_memory = function(tensor.eigenvalues(default.read_dataset(loc['sigma'],0)),axis=1,keepdims=True)
in_file = default.read_dataset(loc['lambda'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
@pytest.mark.parametrize('eigenvalue,idx',[('max',2),('mid',1),('min',0)]) @pytest.mark.parametrize('eigenvalue,idx',[('max',2),('mid',1),('min',0)])
def test_add_eigenvector(self,default,eigenvalue,idx): def test_add_eigenvector(self,default,eigenvalue,idx):
default.add_stress_Cauchy('P','F') default.add_stress_Cauchy('P','F')
default.add_eigenvector('sigma',eigenvalue) default.add_eigenvector('sigma',eigenvalue)
loc = {'sigma' :default.get_dataset_location('sigma'), in_memory = tensor.eigenvectors(default.place('sigma'))[:,idx]
'v(sigma)':default.get_dataset_location(f'v_{eigenvalue}(sigma)')} in_file = default.place(f'v_{eigenvalue}(sigma)')
in_memory = tensor.eigenvectors(default.read_dataset(loc['sigma'],0))[:,idx]
in_file = default.read_dataset(loc['v(sigma)'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
@pytest.mark.parametrize('d',[[1,0,0],[0,1,0],[0,0,1]]) @pytest.mark.parametrize('d',[[1,0,0],[0,1,0],[0,0,1]])
@ -186,7 +170,7 @@ class TestResult:
default.add_IPF_color(d,'O') default.add_IPF_color(d,'O')
loc = {'O': default.get_dataset_location('O'), loc = {'O': default.get_dataset_location('O'),
'color': default.get_dataset_location('IPFcolor_[{} {} {}]'.format(*d))} 'color': default.get_dataset_location('IPFcolor_[{} {} {}]'.format(*d))}
qu = default.read_dataset(loc['O']).view(np.double).squeeze() qu = default.read_dataset(loc['O'])
crystal_structure = default._get_attribute(default.get_dataset_location('O')[0],'lattice') crystal_structure = default._get_attribute(default.get_dataset_location('O')[0],'lattice')
c = Orientation(rotation=qu,lattice=crystal_structure) c = Orientation(rotation=qu,lattice=crystal_structure)
in_memory = np.uint8(c.IPF_color(np.array(d))*255) in_memory = np.uint8(c.IPF_color(np.array(d))*255)
@ -196,10 +180,8 @@ class TestResult:
def test_add_maximum_shear(self,default): def test_add_maximum_shear(self,default):
default.add_stress_Cauchy('P','F') default.add_stress_Cauchy('P','F')
default.add_maximum_shear('sigma') default.add_maximum_shear('sigma')
loc = {'sigma' :default.get_dataset_location('sigma'), in_memory = mechanics.maximum_shear(default.place('sigma'))
'max_shear(sigma)':default.get_dataset_location('max_shear(sigma)')} in_file = default.place('max_shear(sigma)')
in_memory = mechanics.maximum_shear(default.read_dataset(loc['sigma'],0)).reshape(-1,1)
in_file = default.read_dataset(loc['max_shear(sigma)'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_Mises_strain(self,default): def test_add_Mises_strain(self,default):
@ -208,19 +190,15 @@ class TestResult:
default.add_strain('F',t,m) default.add_strain('F',t,m)
label = f'epsilon_{t}^{m}(F)' label = f'epsilon_{t}^{m}(F)'
default.add_equivalent_Mises(label) default.add_equivalent_Mises(label)
loc = {label :default.get_dataset_location(label), in_memory = mechanics.equivalent_strain_Mises(default.place(label))
label+'_vM':default.get_dataset_location(label+'_vM')} in_file = default.place(label+'_vM')
in_memory = mechanics.equivalent_strain_Mises(default.read_dataset(loc[label],0)).reshape(-1,1)
in_file = default.read_dataset(loc[label+'_vM'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_Mises_stress(self,default): def test_add_Mises_stress(self,default):
default.add_stress_Cauchy('P','F') default.add_stress_Cauchy('P','F')
default.add_equivalent_Mises('sigma') default.add_equivalent_Mises('sigma')
loc = {'sigma' :default.get_dataset_location('sigma'), in_memory = mechanics.equivalent_stress_Mises(default.place('sigma'))
'sigma_vM':default.get_dataset_location('sigma_vM')} in_file = default.place('sigma_vM')
in_memory = mechanics.equivalent_stress_Mises(default.read_dataset(loc['sigma'],0)).reshape(-1,1)
in_file = default.read_dataset(loc['sigma_vM'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_Mises_invalid(self,default): def test_add_Mises_invalid(self,default):
@ -235,26 +213,20 @@ class TestResult:
default.add_calculation('sigma_x','#sigma#',unit='x') default.add_calculation('sigma_x','#sigma#',unit='x')
default.add_equivalent_Mises('sigma_y',kind='strain') default.add_equivalent_Mises('sigma_y',kind='strain')
default.add_equivalent_Mises('sigma_x',kind='stress') default.add_equivalent_Mises('sigma_x',kind='stress')
loc = {'y' :default.get_dataset_location('sigma_y_vM'), assert not np.allclose(default.place('sigma_y_vM'),default.place('sigma_x_vM'))
'x' :default.get_dataset_location('sigma_x_vM')}
assert not np.allclose(default.read_dataset(loc['y'],0),default.read_dataset(loc['x'],0))
def test_add_norm(self,default): @pytest.mark.parametrize('ord',[1,2])
default.add_norm('F',1) @pytest.mark.parametrize('dataset,axis',[('F',(1,2)),('xi_sl',(1,))])
loc = {'F': default.get_dataset_location('F'), def test_add_norm(self,default,ord,dataset,axis):
'|F|_1':default.get_dataset_location('|F|_1')} default.add_norm(dataset,ord)
in_memory = np.linalg.norm(default.read_dataset(loc['F'],0),ord=1,axis=(1,2),keepdims=True) in_memory = np.linalg.norm(default.place(dataset),ord=ord,axis=axis,keepdims=True)
in_file = default.read_dataset(loc['|F|_1'],0) in_file = default.place(f'|{dataset}|_{ord}')
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_stress_second_Piola_Kirchhoff(self,default): def test_add_stress_second_Piola_Kirchhoff(self,default):
default.add_stress_second_Piola_Kirchhoff('P','F') default.add_stress_second_Piola_Kirchhoff('P','F')
loc = {'F':default.get_dataset_location('F'), in_memory = mechanics.stress_second_Piola_Kirchhoff(default.place('P'),default.place('F'))
'P':default.get_dataset_location('P'), in_file = default.place('S')
'S':default.get_dataset_location('S')}
in_memory = mechanics.stress_second_Piola_Kirchhoff(default.read_dataset(loc['P'],0),
default.read_dataset(loc['F'],0))
in_file = default.read_dataset(loc['S'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
@pytest.mark.skip(reason='requires rework of lattice.f90') @pytest.mark.skip(reason='requires rework of lattice.f90')
@ -262,30 +234,24 @@ class TestResult:
def test_add_pole(self,default,polar): def test_add_pole(self,default,polar):
pole = np.array([1.,0.,0.]) pole = np.array([1.,0.,0.])
default.add_pole('O',pole,polar) default.add_pole('O',pole,polar)
loc = {'O': default.get_dataset_location('O'), rot = Rotation(default.place('O'))
'pole': default.get_dataset_location('p^{}_[1 0 0)'.format(u'' if polar else 'xy'))}
rot = Rotation(default.read_dataset(loc['O']).view(np.double))
rotated_pole = rot * np.broadcast_to(pole,rot.shape+(3,)) rotated_pole = rot * np.broadcast_to(pole,rot.shape+(3,))
xy = rotated_pole[:,0:2]/(1.+abs(pole[2])) xy = rotated_pole[:,0:2]/(1.+abs(pole[2]))
in_memory = xy if not polar else \ in_memory = xy if not polar else \
np.block([np.sqrt(xy[:,0:1]*xy[:,0:1]+xy[:,1:2]*xy[:,1:2]),np.arctan2(xy[:,1:2],xy[:,0:1])]) np.block([np.sqrt(xy[:,0:1]*xy[:,0:1]+xy[:,1:2]*xy[:,1:2]),np.arctan2(xy[:,1:2],xy[:,0:1])])
in_file = default.read_dataset(loc['pole']) in_file = default.place('p^{}_[1 0 0)'.format(u'' if polar else 'xy'))
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_rotation(self,default): def test_add_rotation(self,default):
default.add_rotation('F') default.add_rotation('F')
loc = {'F': default.get_dataset_location('F'), in_memory = mechanics.rotation(default.place('F')).as_matrix()
'R(F)': default.get_dataset_location('R(F)')} in_file = default.place('R(F)')
in_memory = mechanics.rotation(default.read_dataset(loc['F'],0)).as_matrix()
in_file = default.read_dataset(loc['R(F)'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_spherical(self,default): def test_add_spherical(self,default):
default.add_spherical('P') default.add_spherical('P')
loc = {'P': default.get_dataset_location('P'), in_memory = tensor.spherical(default.place('P'),False)
'p_P': default.get_dataset_location('p_P')} in_file = default.place('p_P')
in_memory = tensor.spherical(default.read_dataset(loc['P'],0),False).reshape(-1,1)
in_file = default.read_dataset(loc['p_P'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_strain(self,default): def test_add_strain(self,default):
@ -293,26 +259,20 @@ class TestResult:
m = np.random.random()*2.0 - 1.0 m = np.random.random()*2.0 - 1.0
default.add_strain('F',t,m) default.add_strain('F',t,m)
label = f'epsilon_{t}^{m}(F)' label = f'epsilon_{t}^{m}(F)'
loc = {'F': default.get_dataset_location('F'), in_memory = mechanics.strain(default.place('F'),t,m)
label: default.get_dataset_location(label)} in_file = default.place(label)
in_memory = mechanics.strain(default.read_dataset(loc['F'],0),t,m)
in_file = default.read_dataset(loc[label],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_stretch_right(self,default): def test_add_stretch_right(self,default):
default.add_stretch_tensor('F','U') default.add_stretch_tensor('F','U')
loc = {'F': default.get_dataset_location('F'), in_memory = mechanics.stretch_right(default.place('F'))
'U(F)': default.get_dataset_location('U(F)')} in_file = default.place('U(F)')
in_memory = mechanics.stretch_right(default.read_dataset(loc['F'],0))
in_file = default.read_dataset(loc['U(F)'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_stretch_left(self,default): def test_add_stretch_left(self,default):
default.add_stretch_tensor('F','V') default.add_stretch_tensor('F','V')
loc = {'F': default.get_dataset_location('F'), in_memory = mechanics.stretch_left(default.place('F'))
'V(F)': default.get_dataset_location('V(F)')} in_file = default.place('V(F)')
in_memory = mechanics.stretch_left(default.read_dataset(loc['F'],0))
in_file = default.read_dataset(loc['V(F)'],0)
assert np.allclose(in_memory,in_file) assert np.allclose(in_memory,in_file)
def test_add_invalid(self,default): def test_add_invalid(self,default):
@ -358,10 +318,10 @@ class TestResult:
@pytest.mark.parametrize('allowed',['off','on']) @pytest.mark.parametrize('allowed',['off','on'])
def test_rename(self,default,allowed): def test_rename(self,default,allowed):
if allowed == 'on': if allowed == 'on':
F = default.read_dataset(default.get_dataset_location('F')) F = default.place('F')
default.allow_modification() default.allow_modification()
default.rename('F','new_name') default.rename('F','new_name')
assert np.all(F == default.read_dataset(default.get_dataset_location('new_name'))) assert np.all(F == default.place('new_name'))
default.disallow_modification() default.disallow_modification()
with pytest.raises(PermissionError): with pytest.raises(PermissionError):