Skip to content

Commit 14b7a6c

Browse files
committed
Merge branch '366-remove-damask-result-load_ascii' into 'development'
drop support for deprecated ASCII-based geom format Closes #366 See merge request damask/DAMASK!959
2 parents 3697450 + 7b12602 commit 14b7a6c

File tree

2 files changed

+1
-115
lines changed

2 files changed

+1
-115
lines changed

python/damask/_geomgrid.py

+1-110
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import os
22
import copy
3-
import warnings
43
import multiprocessing as mp
54
from functools import partial
6-
import typing
7-
from typing import Optional, Union, TextIO, Sequence, Dict
5+
from typing import Optional, Union, Sequence, Dict
86
from pathlib import Path
97

108
import numpy as np
@@ -272,83 +270,6 @@ def load_SPPARKS(fname: Union[str, Path]) -> 'GeomGrid':
272270
return GeomGrid._load(fname,'Spin')
273271

274272

275-
@typing.no_type_check
276-
@staticmethod
277-
def load_ASCII(fname)-> 'GeomGrid':
278-
"""
279-
Load from geom file.
280-
281-
Storing geometry files in ASCII format is deprecated.
282-
This function will be removed in a future version of DAMASK.
283-
284-
Parameters
285-
----------
286-
fname : str, pathlib.Path, or file handle
287-
Geometry file to read.
288-
289-
Returns
290-
-------
291-
loaded : damask.GeomGrid
292-
Grid-based geometry from file.
293-
294-
"""
295-
warnings.warn('Support for ASCII-based geom format will be removed in DAMASK 3.0.0', DeprecationWarning,2)
296-
if isinstance(fname, (str, Path)):
297-
f = open(fname)
298-
elif isinstance(fname, TextIO):
299-
f = fname
300-
else:
301-
raise TypeError
302-
303-
f.seek(0)
304-
try:
305-
header_length_,keyword = f.readline().split()[:2]
306-
header_length = int(header_length_)
307-
except ValueError:
308-
header_length,keyword = (-1, 'invalid')
309-
if not keyword.startswith('head') or header_length < 3:
310-
raise TypeError('invalid or missing header length information')
311-
312-
comments = []
313-
content = f.readlines()
314-
for i,line in enumerate(content[:header_length]):
315-
items = line.split('#')[0].lower().strip().split()
316-
if (key := items[0] if items else '') == 'grid':
317-
cells = np.array([ int(dict(zip(items[1::2],items[2::2]))[i]) for i in ['a','b','c']])
318-
elif key == 'size':
319-
size = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
320-
elif key == 'origin':
321-
origin = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
322-
else:
323-
comments.append(line.strip())
324-
325-
material = np.empty(cells.prod()) # initialize as flat array
326-
i = 0
327-
for line in content[header_length:]:
328-
if len(items := line.split('#')[0].split()) == 3:
329-
if items[1].lower() == 'of':
330-
material_entry = np.ones(int(items[0]))*float(items[2])
331-
elif items[1].lower() == 'to':
332-
material_entry = np.linspace(int(items[0]),int(items[2]),
333-
abs(int(items[2])-int(items[0]))+1,dtype=float)
334-
else: material_entry = list(map(float, items))
335-
else: material_entry = list(map(float, items))
336-
material[i:i+len(material_entry)] = material_entry
337-
i += len(items)
338-
339-
if i != cells.prod():
340-
raise TypeError(f'mismatch between {cells.prod()} expected entries and {i} found')
341-
342-
if not np.any(np.mod(material,1) != 0.0): # no float present
343-
material = material.astype(np.int64) - (1 if material.min() > 0 else 0)
344-
345-
return GeomGrid(material = material.reshape(cells,order='F'),
346-
size = size,
347-
origin = origin,
348-
comments = comments,
349-
)
350-
351-
352273
@staticmethod
353274
def load_Neper(fname: Union[str, Path]) -> 'GeomGrid':
354275
"""
@@ -788,36 +709,6 @@ def save(self,
788709
v.save(fname,parallel=False,compress=compress)
789710

790711

791-
def save_ASCII(self,
792-
fname: Union[str, TextIO]):
793-
"""
794-
Save as geom file.
795-
796-
Storing geometry files in ASCII format is deprecated.
797-
This function will be removed in a future version of DAMASK.
798-
799-
Parameters
800-
----------
801-
fname : str or file handle
802-
Geometry file to write with extension '.geom'.
803-
compress : bool, optional
804-
Compress geometry using 'x of y' and 'a to b'.
805-
806-
"""
807-
warnings.warn('Support for ASCII-based geom format will be removed in DAMASK 3.0.0', DeprecationWarning,2)
808-
header = [f'{len(self.comments)+4} header'] + self.comments \
809-
+ ['grid a {} b {} c {}'.format(*self.cells),
810-
'size x {} y {} z {}'.format(*self.size),
811-
'origin x {} y {} z {}'.format(*self.origin),
812-
'homogenization 1',
813-
]
814-
815-
format_string = '%g' if self.material.dtype in [np.float32,np.float64] else \
816-
'%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.material)))))
817-
np.savetxt(fname,
818-
self.material.reshape([self.cells[0],np.prod(self.cells[1:])],order='F').T,
819-
header='\n'.join(header), fmt=format_string, comments='')
820-
821712

822713
def show(self,
823714
colormap: Union[Colormap, str] = 'cividis') -> None:

python/tests/test_GeomGrid.py

-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ def test_invalid_size(self,default):
8888
GeomGrid(default.material[1:,1:,1:],
8989
size=np.ones(2))
9090

91-
def test_save_load_ASCII(self,default,tmp_path):
92-
default.save_ASCII(tmp_path/'ASCII')
93-
default.material -= 1
94-
assert GeomGrid.load_ASCII(tmp_path/'ASCII') == default
95-
9691
def test_save_load_SPPARKS(self,res_path,tmp_path):
9792
v = VTK.load(res_path/'SPPARKS_dump.vti')
9893
v.set('material',v.get('Spin')).delete('Spin').save(tmp_path/'SPPARKS_dump.vti',parallel=False)

0 commit comments

Comments
 (0)