support of all standard numpy types

+ tests
This commit is contained in:
Martin Diehl 2020-11-30 10:40:46 +01:00
parent 813ef927c4
commit 1c2cdbd5a1
4 changed files with 1667 additions and 12 deletions

@ -1 +1 @@
Subproject commit 751f96155294e449b31c5d4913fea7bc02ce81b1
Subproject commit 68cde52291ebb683ca6f610879f2ae28372597a7

View File

@ -1176,8 +1176,17 @@ class Result:
This works only for scalar, 3-vector and 3x3-tensor data.
Selection is not taken into account.
"""
if self.N_constituents != 1 or not self.structured:
raise NotImplementedError('XDMF only available for grid results with 1 constituent.')
if self.N_constituents != 1 or len(self.phases) != 1 or not self.structured:
raise TypeError('XDMF output requires homogeneous grid')
attribute_type_map = defaultdict(lambda:'Matrix', ( ((),'Scalar'), ((3,),'Vector'), ((3,3),'Tensor')) )
def number_type_map(dtype):
if dtype in np.sctypes['int']: return 'Int'
if dtype in np.sctypes['uint']: return 'UInt'
if dtype in np.sctypes['float']: return 'Float'
xdmf=ET.Element('Xdmf')
xdmf.attrib={'Version': '2.0',
@ -1227,8 +1236,6 @@ class Result:
delta.text="{} {} {}".format(*(self.size/self.grid))
type_map = defaultdict(lambda:'Matrix', ( ((),'Scalar'), ((3,),'Vector'), ((3,3),'Tensor')) )
with h5py.File(self.fname,'r') as f:
attributes.append(ET.SubElement(grid, 'Attribute'))
attributes[-1].attrib={'Name': 'u / m',
@ -1249,18 +1256,17 @@ class Result:
shape = f[name].shape[1:]
dtype = f[name].dtype
if dtype != np.float64: continue
prec = f[name].dtype.itemsize
if dtype not in np.sctypes['int']+np.sctypes['uint']+np.sctypes['float']: continue
unit = f[name].attrs['Unit'] if h5py3 else f[name].attrs['Unit'].decode()
attributes.append(ET.SubElement(grid, 'Attribute'))
attributes[-1].attrib={'Name': name.split('/',2)[2]+f' / {unit}',
'Center': 'Cell',
'AttributeType': type_map[shape]}
'AttributeType': attribute_type_map[shape]}
data_items.append(ET.SubElement(attributes[-1], 'DataItem'))
data_items[-1].attrib={'Format': 'HDF',
'NumberType': 'Float',
'Precision': f'{prec}',
'NumberType': number_type_map(dtype),
'Precision': f'{dtype.itemsize}',
'Dimensions': '{} {} {} {}'.format(*self.grid,1 if shape == () else
np.prod(shape))}
data_items[-1].text=f'{os.path.split(self.fname)[1]}:{name}'

File diff suppressed because it is too large Load Diff

View File

@ -373,6 +373,17 @@ class TestResult:
os.chdir(tmp_path)
single_phase.save_VTK(mode=mode)
def test_XDMF(self,tmp_path,single_phase):
def test_XDMF(self,tmp_path,single_phase,update,reference_dir):
for shape in [('scalar',()),('vector',(3,)),('tensor',(3,3)),('matrix',(12,))]:
for dtype in ['f4','f8','i1','i2','i4','i8','u1','u2','u4','u8']:
single_phase.add_calculation(f'{shape[0]}_{dtype}',f"np.ones(np.shape(#F#)[0:1]+{shape[1]},'{dtype}')")
fname = os.path.splitext(os.path.basename(single_phase.fname))[0]+'.xdmf'
os.chdir(tmp_path)
single_phase.save_XDMF()
if update:
shutil.copy(tmp_path/fname,reference_dir/fname)
assert sorted(open(tmp_path/fname).read()) == sorted(open(reference_dir/fname).read()) # XML is not ordered
def test_XDMF_invalid(self,default):
with pytest.raises(TypeError):
default.save_XDMF()