From 6a0bb8be8eb11900e9fc629aff5ad07dd3f99d36 Mon Sep 17 00:00:00 2001 From: Test User Date: Wed, 30 Oct 2019 19:15:00 +0100 Subject: [PATCH 01/26] [skip ci] updated version information after successful test of v2.0.3-1023-g368d4deb --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 442097b29..f46b055b6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v2.0.3-957-gccbcc0d0 +v2.0.3-1023-g368d4deb From 1b7a9fd9e912cb5ca236937995d6f6cb53b62111 Mon Sep 17 00:00:00 2001 From: Franz Roters Date: Thu, 31 Oct 2019 17:20:17 +0100 Subject: [PATCH 02/26] [skip ci] just one slip system family --- examples/ConfigFiles/Phase_Dislotwin_Tungsten.config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ConfigFiles/Phase_Dislotwin_Tungsten.config b/examples/ConfigFiles/Phase_Dislotwin_Tungsten.config index b780f5c59..30c04cb9a 100644 --- a/examples/ConfigFiles/Phase_Dislotwin_Tungsten.config +++ b/examples/ConfigFiles/Phase_Dislotwin_Tungsten.config @@ -14,7 +14,7 @@ SolidSolutionStrength 1.5e8 # Strength due to elements in solid solution ### Dislocation glide parameters ### #per family -Nslip 12 0 +Nslip 12 slipburgers 2.72e-10 # Burgers vector of slip system [m] rhoedge0 1.0e12 # Initial edge dislocation density [m/m**3] rhoedgedip0 1.0 # Initial edged dipole dislocation density [m/m**3] From 2e834cc3c10260dbd500d83b56c64876191cbff9 Mon Sep 17 00:00:00 2001 From: Test User Date: Sat, 2 Nov 2019 22:45:34 +0100 Subject: [PATCH 03/26] [skip ci] updated version information after successful test of v2.0.3-1073-g6f3cb071 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index f46b055b6..a5ac7d281 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v2.0.3-1023-g368d4deb +v2.0.3-1073-g6f3cb071 From 7a7eea47b54463ef0530149241972325d3f164a3 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 21 Nov 2019 19:46:05 +0100 Subject: [PATCH 04/26] correct handling of arrays all strains measures except for logarithmic had wrong off-diagonal components --- python/damask/mechanics.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/damask/mechanics.py b/python/damask/mechanics.py index 436fbe091..476682380 100644 --- a/python/damask/mechanics.py +++ b/python/damask/mechanics.py @@ -48,10 +48,10 @@ def strain_tensor(F,t,m): if m > 0.0: eps = 1.0/(2.0*abs(m)) * (+ np.matmul(n,np.einsum('ij,ikj->ijk',w**m,n)) - - np.broadcast_to(np.ones(3),[F_.shape[0],3])) + - np.broadcast_to(np.eye(3),[F_.shape[0],3,3])) elif m < 0.0: eps = 1.0/(2.0*abs(m)) * (- np.matmul(n,np.einsum('ij,ikj->ijk',w**m,n)) - + np.broadcast_to(np.ones(3),[F_.shape[0],3])) + + np.broadcast_to(np.eye(3),[F_.shape[0],3,3])) else: eps = np.matmul(n,np.einsum('ij,ikj->ijk',0.5*np.log(w),n)) @@ -190,7 +190,7 @@ def rotational_part(x): Tensor of which the rotational part is computed. """ - return __polar_decomposition(x,'R') + return __polar_decomposition(x,'R')[0] def left_stretch(x): @@ -203,7 +203,7 @@ def left_stretch(x): Tensor of which the left stretch is computed. """ - return __polar_decomposition(x,'V') + return __polar_decomposition(x,'V')[0] def right_stretch(x): @@ -216,7 +216,7 @@ def right_stretch(x): Tensor of which the right stretch is computed. """ - return __polar_decomposition(x,'U') + return __polar_decomposition(x,'U')[0] def __polar_decomposition(x,requested): @@ -227,7 +227,7 @@ def __polar_decomposition(x,requested): ---------- x : numpy.array of shape (:,3,3) or (3,3) Tensor of which the singular values are computed. - requested : list of str + requested : iterable of str Requested outputs: ‘R’ for the rotation tensor, ‘V’ for left stretch tensor and ‘U’ for right stretch tensor. From a5ae82fe900ec3942451868c65c7db19acec9c04 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 21 Nov 2019 19:47:27 +0100 Subject: [PATCH 05/26] handle deprecation warning in python 3.8 --- python/damask/asciitable.py | 2 +- python/damask/test/test.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/python/damask/asciitable.py b/python/damask/asciitable.py index 59e285d6a..59982cb18 100644 --- a/python/damask/asciitable.py +++ b/python/damask/asciitable.py @@ -2,7 +2,7 @@ import os import sys import re import shlex -from collections import Iterable +from collections.abc import Iterable import numpy as np diff --git a/python/damask/test/test.py b/python/damask/test/test.py index e7f2da14a..3e43c1f01 100644 --- a/python/damask/test/test.py +++ b/python/damask/test/test.py @@ -1,10 +1,8 @@ -# -*- coding: UTF-8 no BOM -*- - import os,sys,shutil import logging,logging.config import damask import numpy as np -from collections import Iterable +from collections.abc import Iterable from optparse import OptionParser class Test(): From cf88c1f907519186ccf8d434b4aa888dd5a5918c Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 21 Nov 2019 19:49:46 +0100 Subject: [PATCH 06/26] correct syntax for integer comparison --- python/damask/solver/marc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/damask/solver/marc.py b/python/damask/solver/marc.py index 5e728545d..971b2226b 100644 --- a/python/damask/solver/marc.py +++ b/python/damask/solver/marc.py @@ -79,9 +79,9 @@ class Marc(Solver): exitnumber = -1 fid_out = open(outFile,'r') for line in fid_out: - if (string.find(line,'tress iteration') is not -1): + if (string.find(line,'tress iteration') != -1): print(line) - elif (string.find(line,'Exit number') is not -1): + elif (string.find(line,'Exit number') != -1): substr = line[string.find(line,'Exit number'):len(line)] exitnumber = int(substr[12:16]) From 77e410d7d141714d35e6d330051c87eeb66a48ed Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 22 Nov 2019 12:16:19 +0100 Subject: [PATCH 07/26] follow prospector rules --- python/damask/asciitable.py | 44 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/python/damask/asciitable.py b/python/damask/asciitable.py index 59982cb18..74bb97173 100644 --- a/python/damask/asciitable.py +++ b/python/damask/asciitable.py @@ -15,7 +15,7 @@ except NameError: # ------------------------------------------------------------------ class ASCIItable(): - """Read and write to ASCII tables""" + """Read and write to ASCII tables.""" tmpext = '_tmp' # filename extension for in-place access @@ -27,6 +27,7 @@ class ASCIItable(): labeled = True, # assume table has labels readonly = False, # no reading from file ): + """Read and write to ASCII tables.""" self.__IO__ = {'output': [], 'buffered': buffered, 'labeled': labeled, # header contains labels @@ -72,7 +73,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def _removeCRLF(self, string): - """Delete any carriage return and line feed from string""" + """Delete any carriage return and line feed from string.""" try: return string.replace('\n','').replace('\r','') except AttributeError: @@ -82,7 +83,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def _quote(self, what): - """Quote empty or white space-containing output""" + """Quote empty or white space-containing output.""" return '{quote}{content}{quote}'.format( quote = ('"' if str(what)=='' or re.search(r"\s",str(what)) else ''), content = what) @@ -103,7 +104,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def output_write(self, what): - """Aggregate a single row (string) or list of (possibly containing further lists of) rows into output""" + """Aggregate a single row (string) or list of (possibly containing further lists of) rows into output.""" if isinstance(what, (str, unicode)): self.__IO__['output'] += [what] else: @@ -143,7 +144,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def head_read(self): """ - Get column labels + Get column labels. by either reading the first row or, if keyword "head[*]" is present, the last line of the header @@ -154,7 +155,7 @@ class ASCIItable(): pass firstline = self.__IO__['in'].readline().strip() - m = re.search('(\d+)\s+head', firstline.lower()) # search for "head" keyword + m = re.search(r'(\d+)\s+head', firstline.lower()) # search for "head" keyword if m: # proper ASCIItable format @@ -194,7 +195,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def head_write(self, header = True): - """Write current header information (info + labels)""" + """Write current header information (info + labels).""" head = ['{}\theader'.format(len(self.info)+self.__IO__['labeled'])] if header else [] head.append(self.info) if self.__IO__['labeled']: @@ -205,7 +206,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def head_getGeom(self): - """Interpret geom header""" + """Interpret geom header.""" identifiers = { 'grid': ['a','b','c'], 'size': ['x','y','z'], @@ -247,7 +248,7 @@ class ASCIItable(): def labels_append(self, what, reset = False): - """Add item or list to existing set of labels (and switch on labeling)""" + """Add item or list to existing set of labels (and switch on labeling).""" if isinstance(what, (str, unicode)): self.tags += [self._removeCRLF(what)] else: @@ -261,7 +262,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def labels_clear(self): - """Delete existing labels and switch to no labeling""" + """Delete existing labels and switch to no labeling.""" self.tags = [] self.__IO__['labeled'] = False @@ -392,7 +393,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def info_append(self, what): - """Add item or list to existing set of infos""" + """Add item or list to existing set of infos.""" if isinstance(what, (str, unicode)): self.info += [self._removeCRLF(what)] else: @@ -403,7 +404,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def info_clear(self): - """Delete any info block""" + """Delete any info block.""" self.info = [] # ------------------------------------------------------------------ @@ -416,7 +417,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def data_skipLines(self, count): - """Wind forward by count number of lines""" + """Wind forward by count number of lines.""" for i in range(count): alive = self.data_read() @@ -426,7 +427,7 @@ class ASCIItable(): def data_read(self, advance = True, respectLabels = True): - """Read next line (possibly buffered) and parse it into data array""" + """Read next line (possibly buffered) and parse it into data array.""" self.line = self.__IO__['readBuffer'].pop(0) if len(self.__IO__['readBuffer']) > 0 \ else self.__IO__['in'].readline().strip() # take buffered content or get next data row from file @@ -446,9 +447,8 @@ class ASCIItable(): # ------------------------------------------------------------------ def data_readArray(self, labels = []): - """Read whole data of all (given) labels as numpy array""" - try: self.data_rewind() # try to wind back to start of data - except: pass # assume/hope we are at data start already... + """Read whole data of all (given) labels as numpy array.""" + self.data_rewind() if labels is None or labels == []: use = None # use all columns (and keep labels intact) @@ -480,7 +480,7 @@ class ASCIItable(): # ------------------------------------------------------------------ def data_write(self, delimiter = '\t'): - """Write current data array and report alive output back""" + """Write current data array and report alive output back.""" if len(self.data) == 0: return True if isinstance(self.data[0],list): @@ -492,16 +492,16 @@ class ASCIItable(): def data_writeArray(self, fmt = None, delimiter = '\t'): - """Write whole numpy array data""" + """Write whole numpy array data.""" for row in self.data: try: output = [fmt % value for value in row] if fmt else list(map(repr,row)) - except: + except Exception: output = [fmt % row] if fmt else [repr(row)] try: self.__IO__['out'].write(delimiter.join(output) + '\n') - except: + except Exception: pass # ------------------------------------------------------------------ @@ -545,7 +545,7 @@ class ASCIItable(): grid, type = 'i', strict = False): - """Read microstructure data (from .geom format)""" + """Read microstructure data (from .geom format).""" def datatype(item): return int(item) if type.lower() == 'i' else float(item) From dac63f7d92b0e495643378cd5d8d6a0ea3ea715f Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 22 Nov 2019 12:54:55 +0100 Subject: [PATCH 08/26] subfolders not needed --- python/damask/__init__.py | 2 +- python/damask/{test => }/test.py | 0 python/damask/test/__init__.py | 3 --- 3 files changed, 1 insertion(+), 4 deletions(-) rename python/damask/{test => }/test.py (100%) delete mode 100644 python/damask/test/__init__.py diff --git a/python/damask/__init__.py b/python/damask/__init__.py index f876d1417..f432ef056 100644 --- a/python/damask/__init__.py +++ b/python/damask/__init__.py @@ -2,7 +2,7 @@ import os with open(os.path.join(os.path.dirname(__file__),'VERSION')) as f: - version = f.readline()[1:-1] + version = f.readline()[1:-1] name = 'damask' diff --git a/python/damask/test/test.py b/python/damask/test.py similarity index 100% rename from python/damask/test/test.py rename to python/damask/test.py diff --git a/python/damask/test/__init__.py b/python/damask/test/__init__.py deleted file mode 100644 index a8d5d5034..000000000 --- a/python/damask/test/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -"""Test functionality.""" - -from .test import Test # noqa From 6e0b2a4fab5465bca24bb8f367a6c439fd29cbcd Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 22 Nov 2019 13:16:08 +0100 Subject: [PATCH 09/26] following prospector rules --- python/damask/test.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/python/damask/test.py b/python/damask/test.py index 3e43c1f01..b1729548d 100644 --- a/python/damask/test.py +++ b/python/damask/test.py @@ -15,7 +15,7 @@ class Test(): variants = [] def __init__(self, **kwargs): - + """New test.""" defaults = {'description': '', 'keep': False, 'accept': False, @@ -118,22 +118,22 @@ class Test(): """Delete directory tree containing current results.""" try: shutil.rmtree(self.dirCurrent()) - except: + except FileNotFoundError: logging.warning('removal of directory "{}" not possible...'.format(self.dirCurrent())) try: os.mkdir(self.dirCurrent()) return True - except: + except FileExistsError: logging.critical('creation of directory "{}" failed.'.format(self.dirCurrent())) return False def prepareAll(self): - """Do all necessary preparations for the whole test""" + """Do all necessary preparations for the whole test.""" return True def prepare(self,variant): - """Do all necessary preparations for the run of each test variant""" + """Do all necessary preparations for the run of each test variant.""" return True @@ -205,9 +205,9 @@ class Test(): for source,target in zip(list(map(mapA,A)),list(map(mapB,B))): try: shutil.copy2(source,target) - except: + except FileNotFoundError: logging.critical('error copying {} to {}'.format(source,target)) - raise + raise FileNotFoundError def copy_Reference2Current(self,sourcefiles=[],targetfiles=[]): @@ -216,9 +216,9 @@ class Test(): for i,f in enumerate(sourcefiles): try: shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i])) - except: + except FileNotFoundError: logging.critical('Reference2Current: Unable to copy file "{}"'.format(f)) - raise + raise FileNotFoundError def copy_Base2Current(self,sourceDir,sourcefiles=[],targetfiles=[]): @@ -228,10 +228,10 @@ class Test(): for i,f in enumerate(sourcefiles): try: shutil.copy2(os.path.join(source,f),self.fileInCurrent(targetfiles[i])) - except: + except FileNotFoundError: logging.error(os.path.join(source,f)) logging.critical('Base2Current: Unable to copy file "{}"'.format(f)) - raise + raise FileNotFoundError def copy_Current2Reference(self,sourcefiles=[],targetfiles=[]): @@ -240,9 +240,9 @@ class Test(): for i,f in enumerate(sourcefiles): try: shutil.copy2(self.fileInCurrent(f),self.fileInReference(targetfiles[i])) - except: + except FileNotFoundError: logging.critical('Current2Reference: Unable to copy file "{}"'.format(f)) - raise + raise FileNotFoundError def copy_Proof2Current(self,sourcefiles=[],targetfiles=[]): @@ -251,9 +251,9 @@ class Test(): for i,f in enumerate(sourcefiles): try: shutil.copy2(self.fileInProof(f),self.fileInCurrent(targetfiles[i])) - except: + except FileNotFoundError: logging.critical('Proof2Current: Unable to copy file "{}"'.format(f)) - raise + raise FileNotFoundError def copy_Current2Current(self,sourcefiles=[],targetfiles=[]): @@ -261,9 +261,10 @@ class Test(): for i,f in enumerate(sourcefiles): try: shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i])) - except: + except FileNotFoundError: logging.critical('Current2Current: Unable to copy file "{}"'.format(f)) - raise + raise FileNotFoundError + def execute_inCurrentDir(self,cmd,streamIn=None,env=None): @@ -437,7 +438,7 @@ class Test(): stdTol = 1.0e-6, preFilter = 1.0e-9): """ - Calculate statistics of tables + Calculate statistics of tables. threshold can be used to ignore small values (a negative number disables this feature) """ @@ -490,7 +491,7 @@ class Test(): rtol = 1e-5, atol = 1e-8, debug = False): - """Compare multiple tables with np.allclose""" + """Compare multiple tables with np.allclose.""" if not (isinstance(files, Iterable) and not isinstance(files, str)): # check whether list of files is requested files = [str(files)] From fad679a9a5432ee5d2c8cf860f2fa562ebb77360 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 22 Nov 2019 15:18:29 +0100 Subject: [PATCH 10/26] need to catch IOError --- python/damask/asciitable.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/damask/asciitable.py b/python/damask/asciitable.py index 74bb97173..13ca0116b 100644 --- a/python/damask/asciitable.py +++ b/python/damask/asciitable.py @@ -448,7 +448,10 @@ class ASCIItable(): def data_readArray(self, labels = []): """Read whole data of all (given) labels as numpy array.""" - self.data_rewind() + try: + self.data_rewind() # try to wind back to start of data + except IOError: + pass # assume/hope we are at data start already... if labels is None or labels == []: use = None # use all columns (and keep labels intact) From c00af5c402b497002a753688fe2847b1c336c76d Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 22 Nov 2019 20:52:36 +0100 Subject: [PATCH 11/26] 4 space indents are common practice --- python/damask/geom.py | 726 +++++++++++++++++++++--------------------- 1 file changed, 362 insertions(+), 364 deletions(-) diff --git a/python/damask/geom.py b/python/damask/geom.py index 176d2bd12..5bf4f7750 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -10,380 +10,378 @@ from . import version class Geom(): - """Geometry definition for grid solvers.""" + """Geometry definition for grid solvers.""" - def __init__(self,microstructure,size,origin=[0.0,0.0,0.0],homogenization=1,comments=[]): - """ - New geometry definition from array of microstructures and size. + def __init__(self,microstructure,size,origin=[0.0,0.0,0.0],homogenization=1,comments=[]): + """ + New geometry definition from array of microstructures and size. - Parameters - ---------- - microstructure : numpy.ndarray - microstructure array (3D) - size : list or numpy.ndarray - physical size of the microstructure in meter. - origin : list or numpy.ndarray, optional - physical origin of the microstructure in meter. - homogenization : integer, optional - homogenization index. - comments : list of str, optional - comments lines. + Parameters + ---------- + microstructure : numpy.ndarray + microstructure array (3D) + size : list or numpy.ndarray + physical size of the microstructure in meter. + origin : list or numpy.ndarray, optional + physical origin of the microstructure in meter. + homogenization : integer, optional + homogenization index. + comments : list of str, optional + comments lines. - """ - self.__transforms__ = \ - self.set_microstructure(microstructure) - self.set_size(size) - self.set_origin(origin) - self.set_homogenization(homogenization) - self.set_comments(comments) - - - def __repr__(self): - """Basic information on geometry definition.""" - return util.srepr([ - 'grid a b c: {}'.format(' x '.join(map(str,self.get_grid ()))), - 'size x y z: {}'.format(' x '.join(map(str,self.get_size ()))), - 'origin x y z: {}'.format(' '.join(map(str,self.get_origin()))), - 'homogenization: {}'.format(self.get_homogenization()), - '# microstructures: {}'.format(len(np.unique(self.microstructure))), - 'max microstructure: {}'.format(np.nanmax(self.microstructure)), - ]) - - def update(self,microstructure=None,size=None,origin=None,rescale=False): - """ - Updates microstructure and size. - - Parameters - ---------- - microstructure : numpy.ndarray, optional - microstructure array (3D). - size : list or numpy.ndarray, optional - physical size of the microstructure in meter. - origin : list or numpy.ndarray, optional - physical origin of the microstructure in meter. - rescale : bool, optional - ignore size parameter and rescale according to change of grid points. - - """ - grid_old = self.get_grid() - size_old = self.get_size() - origin_old = self.get_origin() - unique_old = len(np.unique(self.microstructure)) - max_old = np.nanmax(self.microstructure) - - if size is not None and rescale: - raise ValueError('Either set size explicitly or rescale automatically') - - self.set_microstructure(microstructure) - self.set_origin(origin) - - if size is not None: - self.set_size(size) - elif rescale: - self.set_size(self.get_grid()/grid_old*self.size) - - message = ['grid a b c: {}'.format(' x '.join(map(str,grid_old)))] - if np.any(grid_old != self.get_grid()): - message[-1] = util.delete(message[-1]) - message.append(util.emph('grid a b c: {}'.format(' x '.join(map(str,self.get_grid()))))) - - message.append('size x y z: {}'.format(' x '.join(map(str,size_old)))) - if np.any(size_old != self.get_size()): - message[-1] = util.delete(message[-1]) - message.append(util.emph('size x y z: {}'.format(' x '.join(map(str,self.get_size()))))) - - message.append('origin x y z: {}'.format(' '.join(map(str,origin_old)))) - if np.any(origin_old != self.get_origin()): - message[-1] = util.delete(message[-1]) - message.append(util.emph('origin x y z: {}'.format(' '.join(map(str,self.get_origin()))))) - - message.append('homogenization: {}'.format(self.get_homogenization())) - - message.append('# microstructures: {}'.format(unique_old)) - if unique_old != len(np.unique(self.microstructure)): - message[-1] = util.delete(message[-1]) - message.append(util.emph('# microstructures: {}'.format(len(np.unique(self.microstructure))))) - - message.append('max microstructure: {}'.format(max_old)) - if max_old != np.nanmax(self.microstructure): - message[-1] = util.delete(message[-1]) - message.append(util.emph('max microstructure: {}'.format(np.nanmax(self.microstructure)))) - - return util.return_message(message) - - def set_comments(self,comments): - """ - Replaces all existing comments. - - Parameters - ---------- - comments : list of str - new comments. - - """ - self.comments = [] - self.add_comments(comments) - - def add_comments(self,comments): - """ - Appends comments to existing comments. - - Parameters - ---------- - comments : list of str - new comments. - - """ - self.comments += [str(c) for c in comments] if isinstance(comments,list) else [str(comments)] - - def set_microstructure(self,microstructure): - """ - Replaces the existing microstructure representation. - - Parameters - ---------- - microstructure : numpy.ndarray - microstructure array (3D). - - """ - if microstructure is not None: - if len(microstructure.shape) != 3: - raise ValueError('Invalid microstructure shape {}'.format(*microstructure.shape)) - elif microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']: - raise TypeError('Invalid data type {} for microstructure'.format(microstructure.dtype)) - else: - self.microstructure = np.copy(microstructure) - - def set_size(self,size): - """ - Replaces the existing size information. - - Parameters - ---------- - size : list or numpy.ndarray - physical size of the microstructure in meter. - - """ - if size is None: - grid = np.asarray(self.microstructure.shape) - self.size = grid/np.max(grid) - else: - if len(size) != 3 or any(np.array(size)<=0): - raise ValueError('Invalid size {}'.format(*size)) - else: - self.size = np.array(size) - - def set_origin(self,origin): - """ - Replaces the existing origin information. - - Parameters - ---------- - origin : list or numpy.ndarray - physical origin of the microstructure in meter - - """ - if origin is not None: - if len(origin) != 3: - raise ValueError('Invalid origin {}'.format(*origin)) - else: - self.origin = np.array(origin) - - def set_homogenization(self,homogenization): - """ - Replaces the existing homogenization index. - - Parameters - ---------- - homogenization : integer - homogenization index - - """ - if homogenization is not None: - if not isinstance(homogenization,int) or homogenization < 1: - raise TypeError('Invalid homogenization {}'.format(homogenization)) - else: - self.homogenization = homogenization - - - def get_microstructure(self): - """Return the microstructure representation.""" - return np.copy(self.microstructure) - - def get_size(self): - """Return the physical size in meter.""" - return np.copy(self.size) - - def get_origin(self): - """Return the origin in meter.""" - return np.copy(self.origin) - - def get_grid(self): - """Return the grid discretization.""" - return np.array(self.microstructure.shape) - - def get_homogenization(self): - """Return the homogenization index.""" - return self.homogenization - - def get_comments(self): - """Return the comments.""" - return self.comments[:] - - def get_header(self): - """Return the full header (grid, size, origin, homogenization, comments).""" - header = ['{} header'.format(len(self.comments)+4)] + self.comments - header.append('grid a {} b {} c {}'.format(*self.get_grid())) - header.append('size x {} y {} z {}'.format(*self.get_size())) - header.append('origin x {} y {} z {}'.format(*self.get_origin())) - header.append('homogenization {}'.format(self.get_homogenization())) - return header + """ + self.__transforms__ = \ + self.set_microstructure(microstructure) + self.set_size(size) + self.set_origin(origin) + self.set_homogenization(homogenization) + self.set_comments(comments) - @classmethod - def from_file(cls,fname): - """ - Reads a geom file. - Parameters - ---------- - fname : str or file handle - geometry file to read. + def __repr__(self): + """Basic information on geometry definition.""" + return util.srepr([ + 'grid a b c: {}'.format(' x '.join(map(str,self.get_grid ()))), + 'size x y z: {}'.format(' x '.join(map(str,self.get_size ()))), + 'origin x y z: {}'.format(' '.join(map(str,self.get_origin()))), + 'homogenization: {}'.format(self.get_homogenization()), + '# microstructures: {}'.format(len(np.unique(self.microstructure))), + 'max microstructure: {}'.format(np.nanmax(self.microstructure)), + ]) - """ - with (open(fname) if isinstance(fname,str) else fname) as f: - f.seek(0) - header_length,keyword = f.readline().split()[:2] - header_length = int(header_length) - content = f.readlines() + def update(self,microstructure=None,size=None,origin=None,rescale=False): + """ + Updates microstructure and size. - if not keyword.startswith('head') or header_length < 3: - raise TypeError('Header length information missing or invalid') + Parameters + ---------- + microstructure : numpy.ndarray, optional + microstructure array (3D). + size : list or numpy.ndarray, optional + physical size of the microstructure in meter. + origin : list or numpy.ndarray, optional + physical origin of the microstructure in meter. + rescale : bool, optional + ignore size parameter and rescale according to change of grid points. - comments = [] - for i,line in enumerate(content[:header_length]): - items = line.lower().strip().split() - key = items[0] if len(items) > 0 else '' - if key == 'grid': - grid = np.array([ int(dict(zip(items[1::2],items[2::2]))[i]) for i in ['a','b','c']]) - elif key == 'size': - size = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']]) - elif key == 'origin': - origin = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']]) - elif key == 'homogenization': - homogenization = int(items[1]) - else: - comments.append(line.strip()) + """ + grid_old = self.get_grid() + size_old = self.get_size() + origin_old = self.get_origin() + unique_old = len(np.unique(self.microstructure)) + max_old = np.nanmax(self.microstructure) + + if size is not None and rescale: + raise ValueError('Either set size explicitly or rescale automatically') - microstructure = np.empty(grid.prod()) # initialize as flat array - i = 0 - for line in content[header_length:]: - items = line.split() - if len(items) == 3: - if items[1].lower() == 'of': - items = np.ones(int(items[0]))*float(items[2]) - elif items[1].lower() == 'to': - items = np.linspace(int(items[0]),int(items[2]), - abs(int(items[2])-int(items[0]))+1,dtype=float) - else: items = list(map(float,items)) - else: items = list(map(float,items)) + self.set_microstructure(microstructure) + self.set_origin(origin) + + if size is not None: + self.set_size(size) + elif rescale: + self.set_size(self.get_grid()/grid_old*self.size) + + message = ['grid a b c: {}'.format(' x '.join(map(str,grid_old)))] + if np.any(grid_old != self.get_grid()): + message[-1] = util.delete(message[-1]) + message.append(util.emph('grid a b c: {}'.format(' x '.join(map(str,self.get_grid()))))) + + message.append('size x y z: {}'.format(' x '.join(map(str,size_old)))) + if np.any(size_old != self.get_size()): + message[-1] = util.delete(message[-1]) + message.append(util.emph('size x y z: {}'.format(' x '.join(map(str,self.get_size()))))) + + message.append('origin x y z: {}'.format(' '.join(map(str,origin_old)))) + if np.any(origin_old != self.get_origin()): + message[-1] = util.delete(message[-1]) + message.append(util.emph('origin x y z: {}'.format(' '.join(map(str,self.get_origin()))))) + + message.append('homogenization: {}'.format(self.get_homogenization())) + + message.append('# microstructures: {}'.format(unique_old)) + if unique_old != len(np.unique(self.microstructure)): + message[-1] = util.delete(message[-1]) + message.append(util.emph('# microstructures: {}'.format(len(np.unique(self.microstructure))))) + + message.append('max microstructure: {}'.format(max_old)) + if max_old != np.nanmax(self.microstructure): + message[-1] = util.delete(message[-1]) + message.append(util.emph('max microstructure: {}'.format(np.nanmax(self.microstructure)))) + + return util.return_message(message) + + def set_comments(self,comments): + """ + Replaces all existing comments. + + Parameters + ---------- + comments : list of str + new comments. + + """ + self.comments = [] + self.add_comments(comments) - microstructure[i:i+len(items)] = items - i += len(items) + def add_comments(self,comments): + """ + Appends comments to existing comments. + + Parameters + ---------- + comments : list of str + new comments. + + """ + self.comments += [str(c) for c in comments] if isinstance(comments,list) else [str(comments)] + + def set_microstructure(self,microstructure): + """ + Replaces the existing microstructure representation. + + Parameters + ---------- + microstructure : numpy.ndarray + microstructure array (3D). + + """ + if microstructure is not None: + if len(microstructure.shape) != 3: + raise ValueError('Invalid microstructure shape {}'.format(*microstructure.shape)) + elif microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']: + raise TypeError('Invalid data type {} for microstructure'.format(microstructure.dtype)) + else: + self.microstructure = np.copy(microstructure) + + def set_size(self,size): + """ + Replaces the existing size information. + + Parameters + ---------- + size : list or numpy.ndarray + physical size of the microstructure in meter. + + """ + if size is None: + grid = np.asarray(self.microstructure.shape) + self.size = grid/np.max(grid) + else: + if len(size) != 3 or any(np.array(size)<=0): + raise ValueError('Invalid size {}'.format(*size)) + else: + self.size = np.array(size) + + def set_origin(self,origin): + """ + Replaces the existing origin information. + + Parameters + ---------- + origin : list or numpy.ndarray + physical origin of the microstructure in meter + + """ + if origin is not None: + if len(origin) != 3: + raise ValueError('Invalid origin {}'.format(*origin)) + else: + self.origin = np.array(origin) + + def set_homogenization(self,homogenization): + """ + Replaces the existing homogenization index. + + Parameters + ---------- + homogenization : integer + homogenization index + + """ + if homogenization is not None: + if not isinstance(homogenization,int) or homogenization < 1: + raise TypeError('Invalid homogenization {}'.format(homogenization)) + else: + self.homogenization = homogenization + + + def get_microstructure(self): + """Return the microstructure representation.""" + return np.copy(self.microstructure) + + def get_size(self): + """Return the physical size in meter.""" + return np.copy(self.size) + + def get_origin(self): + """Return the origin in meter.""" + return np.copy(self.origin) + + def get_grid(self): + """Return the grid discretization.""" + return np.array(self.microstructure.shape) + + def get_homogenization(self): + """Return the homogenization index.""" + return self.homogenization + + def get_comments(self): + """Return the comments.""" + return self.comments[:] + + def get_header(self): + """Return the full header (grid, size, origin, homogenization, comments).""" + header = ['{} header'.format(len(self.comments)+4)] + self.comments + header.append('grid a {} b {} c {}'.format(*self.get_grid())) + header.append('size x {} y {} z {}'.format(*self.get_size())) + header.append('origin x {} y {} z {}'.format(*self.get_origin())) + header.append('homogenization {}'.format(self.get_homogenization())) + return header + + @classmethod + def from_file(cls,fname): + """ + Reads a geom file. + + Parameters + ---------- + fname : str or file handle + geometry file to read. + + """ + with (open(fname) if isinstance(fname,str) else fname) as f: + f.seek(0) + header_length,keyword = f.readline().split()[:2] + header_length = int(header_length) + content = f.readlines() + + if not keyword.startswith('head') or header_length < 3: + raise TypeError('Header length information missing or invalid') + + comments = [] + for i,line in enumerate(content[:header_length]): + items = line.lower().strip().split() + key = items[0] if len(items) > 0 else '' + if key == 'grid': + grid = np.array([ int(dict(zip(items[1::2],items[2::2]))[i]) for i in ['a','b','c']]) + elif key == 'size': + size = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']]) + elif key == 'origin': + origin = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']]) + elif key == 'homogenization': + homogenization = int(items[1]) + else: + comments.append(line.strip()) + + microstructure = np.empty(grid.prod()) # initialize as flat array + i = 0 + for line in content[header_length:]: + items = line.split() + if len(items) == 3: + if items[1].lower() == 'of': + items = np.ones(int(items[0]))*float(items[2]) + elif items[1].lower() == 'to': + items = np.linspace(int(items[0]),int(items[2]), + abs(int(items[2])-int(items[0]))+1,dtype=float) + else: items = list(map(float,items)) + else: items = list(map(float,items)) + microstructure[i:i+len(items)] = items + i += len(items) + + if i != grid.prod(): + raise TypeError('Invalid file: expected {} entries,found {}'.format(grid.prod(),i)) + + microstructure = microstructure.reshape(grid,order='F') + if not np.any(np.mod(microstructure.flatten(),1) != 0.0): # no float present + microstructure = microstructure.astype('int') + + return cls(microstructure.reshape(grid),size,origin,homogenization,comments) + + + def to_file(self,fname): + """ + Writes a geom file. + + Parameters + ---------- + fname : str or file handle + geometry file to write. + + """ + header = self.get_header() + grid = self.get_grid() + format_string = '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) if self.microstructure.dtype == int \ + else '%g' + np.savetxt(fname, + self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, + header='\n'.join(header), fmt=format_string, comments='') + + def to_vtk(self,fname=None): + """ + Generates vtk file. + + Parameters + ---------- + fname : str, optional + vtk file to write. If no file is given, a string is returned. + + """ + grid = self.get_grid() + np.ones(3,dtype=int) + size = self.get_size() + origin = self.get_origin() + + coords = [ + np.linspace(0,size[0],grid[0]) + origin[0], + np.linspace(0,size[1],grid[1]) + origin[1], + np.linspace(0,size[2],grid[2]) + origin[2] + ] + + rGrid = vtk.vtkRectilinearGrid() + coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()] + + rGrid.SetDimensions(*grid) + for d,coord in enumerate(coords): + for c in coord: + coordArray[d].InsertNextValue(c) + + rGrid.SetXCoordinates(coordArray[0]) + rGrid.SetYCoordinates(coordArray[1]) + rGrid.SetZCoordinates(coordArray[2]) + + ms = numpy_support.numpy_to_vtk(num_array=self.microstructure.flatten(order='F'), + array_type=vtk.VTK_INT if self.microstructure.dtype == int else vtk.VTK_FLOAT) + ms.SetName('microstructure') + rGrid.GetCellData().AddArray(ms) + + + if fname is None: + writer = vtk.vtkDataSetWriter() + writer.SetHeader('damask.Geom '+version) + writer.WriteToOutputStringOn() + else: + writer = vtk.vtkXMLRectilinearGridWriter() + writer.SetCompressorTypeToZLib() + writer.SetDataModeToBinary() + + ext = os.path.splitext(fname)[1] + if ext == '': + name = fname + '.' + writer.GetDefaultFileExtension() + elif ext == writer.GetDefaultFileExtension(): + name = fname + else: + raise ValueError("unknown extension {}".format(ext)) + writer.SetFileName(name) - if i != grid.prod(): - raise TypeError('Invalid file: expected {} entries,found {}'.format(grid.prod(),i)) - - microstructure = microstructure.reshape(grid,order='F') - if not np.any(np.mod(microstructure.flatten(),1) != 0.0): # no float present - microstructure = microstructure.astype('int') - - return cls(microstructure.reshape(grid),size,origin,homogenization,comments) + writer.SetInputData(rGrid) + writer.Write() - def to_file(self,fname): - """ - Writes a geom file. + if fname is None: return writer.GetOutputString() - Parameters - ---------- - fname : str or file handle - geometry file to write. - - """ - header = self.get_header() - grid = self.get_grid() - format_string = '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) if self.microstructure.dtype == int \ - else '%g' - np.savetxt(fname, - self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, - header='\n'.join(header), fmt=format_string, comments='') - - - - def to_vtk(self,fname=None): - """ - Generates vtk file. - - Parameters - ---------- - fname : str, optional - vtk file to write. If no file is given, a string is returned. - - """ - grid = self.get_grid() + np.ones(3,dtype=int) - size = self.get_size() - origin = self.get_origin() - - coords = [ - np.linspace(0,size[0],grid[0]) + origin[0], - np.linspace(0,size[1],grid[1]) + origin[1], - np.linspace(0,size[2],grid[2]) + origin[2] - ] - - rGrid = vtk.vtkRectilinearGrid() - coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()] - - rGrid.SetDimensions(*grid) - for d,coord in enumerate(coords): - for c in coord: - coordArray[d].InsertNextValue(c) - - rGrid.SetXCoordinates(coordArray[0]) - rGrid.SetYCoordinates(coordArray[1]) - rGrid.SetZCoordinates(coordArray[2]) - - ms = numpy_support.numpy_to_vtk(num_array=self.microstructure.flatten(order='F'), - array_type=vtk.VTK_INT if self.microstructure.dtype == int else vtk.VTK_FLOAT) - ms.SetName('microstructure') - rGrid.GetCellData().AddArray(ms) - - - if fname is None: - writer = vtk.vtkDataSetWriter() - writer.SetHeader('damask.Geom '+version) - writer.WriteToOutputStringOn() - else: - writer = vtk.vtkXMLRectilinearGridWriter() - writer.SetCompressorTypeToZLib() - writer.SetDataModeToBinary() - - ext = os.path.splitext(fname)[1] - if ext == '': - name = fname + '.' + writer.GetDefaultFileExtension() - elif ext == writer.GetDefaultFileExtension(): - name = fname - else: - raise ValueError("unknown extension {}".format(ext)) - writer.SetFileName(name) - - writer.SetInputData(rGrid) - writer.Write() - - if fname is None: return writer.GetOutputString() - - - def show(self): - """Show raw content (as in file).""" - f=StringIO() - self.to_file(f) - f.seek(0) - return ''.join(f.readlines()) + + def show(self): + """Show raw content (as in file).""" + f=StringIO() + self.to_file(f) + f.seek(0) + return ''.join(f.readlines()) From dfb95df68975b019040fb76ba4ced18db3c2c1fa Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 22 Nov 2019 21:48:41 +0100 Subject: [PATCH 12/26] migrating shell scripts to python class --- processing/pre/geom_clean.py | 10 ++----- processing/pre/geom_mirror.py | 20 +------------- python/damask/geom.py | 49 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index aeafe4f09..7ee87fe0a 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -15,11 +15,6 @@ scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptID = ' '.join([scriptName,damask.version]) -def mostFrequent(arr): - unique, inverse = np.unique(arr, return_inverse=True) - return unique[np.argmax(np.bincount(inverse))] - - #-------------------------------------------------------------------------------------------------- # MAIN #-------------------------------------------------------------------------------------------------- @@ -46,9 +41,8 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(geom.update(ndimage.filters.generic_filter( - geom.microstructure,mostFrequent, - size=(options.stencil,)*3).astype(geom.microstructure.dtype))) + damask.util.croak(geom.clean(options.stencil)) + geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) if name is None: diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index 67bd2366f..3f9755818 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -38,16 +38,6 @@ parser.set_defaults(reflect = False) (options, filenames) = parser.parse_args() -if options.directions is None: - parser.error('no direction given.') - -if not set(options.directions).issubset(validDirections): - invalidDirections = [str(e) for e in set(options.directions).difference(validDirections)] - parser.error('invalid directions {}. '.format(*invalidDirections)) - -limits = [None,None] if options.reflect else [-2,0] - - if filenames == []: filenames = [None] for name in filenames: @@ -55,15 +45,7 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - microstructure = geom.get_microstructure() - if 'z' in options.directions: - microstructure = np.concatenate([microstructure,microstructure[:,:,limits[0]:limits[1]:-1]],2) - if 'y' in options.directions: - microstructure = np.concatenate([microstructure,microstructure[:,limits[0]:limits[1]:-1,:]],1) - if 'x' in options.directions: - microstructure = np.concatenate([microstructure,microstructure[limits[0]:limits[1]:-1,:,:]],0) - - damask.util.croak(geom.update(microstructure,rescale=True)) + damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) if name is None: diff --git a/python/damask/geom.py b/python/damask/geom.py index 5bf4f7750..640203be1 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -2,6 +2,7 @@ import os from io import StringIO import numpy as np +from scipy import ndimage import vtk from vtk.util import numpy_support @@ -385,3 +386,51 @@ class Geom(): self.to_file(f) f.seek(0) return ''.join(f.readlines()) + + + def mirror(self,directions,reflect=False): + """ + Mirror microstructure along given directions. + Parameters + ---------- + directions : iterable containing str + direction(s) along which the microstructure is mirrored. Valid entries are 'x', 'y', 'z'. + reflect : bool, optional + reflect (include) outermost layers. + """ + valid = {'x','y','z'} + if not all(isinstance(d, str) for d in directions): + raise TypeError('Directions are not of type str.') + elif not set(directions).issubset(valid): + raise ValueError('Invalid direction specified {}'.format(*set(directions).difference(valid))) + + limits = [None,None] if reflect else [-2,0] + ms = self.get_microstructure() + + if 'z' in directions: + ms = np.concatenate([ms,ms[:,:,limits[0]:limits[1]:-1]],2) + if 'y' in directions: + ms = np.concatenate([ms,ms[:,limits[0]:limits[1]:-1,:]],1) + if 'x' in directions: + ms = np.concatenate([ms,ms[limits[0]:limits[1]:-1,:,:]],0) + + return self.update(ms,rescale=True) + #self.add_comments('tbd') + + + def clean(self,stencil=3): + """ + Smooth microstructure by selecting most frequent index within given stencil at each location. + Parameters + ---------- + stencil : int, optional + size of smoothing stencil. + """ + def mostFrequent(arr): + unique, inverse = np.unique(arr, return_inverse=True) + return unique[np.argmax(np.bincount(inverse))] + + return self.update(ndimage.filters.generic_filter(self.microstructure, + mostFrequent, + size=(stencil,)*3).astype(self.microstructure.dtype)) + #self.add_comments('tbd') From 834cd43b665acaf5153a6e063c3c5429319e3d78 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 23 Nov 2019 08:06:26 +0100 Subject: [PATCH 13/26] intention not clear any ideas why __transforms__ was used? --- python/damask/geom.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/damask/geom.py b/python/damask/geom.py index 640203be1..6fb9af5a5 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -31,7 +31,6 @@ class Geom(): comments lines. """ - self.__transforms__ = \ self.set_microstructure(microstructure) self.set_size(size) self.set_origin(origin) From 406ae2989740df90bc25a556a3096ed48740e891 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 23 Nov 2019 19:22:18 +0100 Subject: [PATCH 14/26] adopting rename in mechanics --- python/damask/dadf5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py index 7390724c8..60f993bf9 100644 --- a/python/damask/dadf5.py +++ b/python/damask/dadf5.py @@ -620,7 +620,7 @@ class DADF5(): raise ValueError return { - 'data': mechanics.deviator(x['data']), + 'data': mechanics.deviatoric_part(x['data']), 'label': 's_{}'.format(x['label']), 'meta': { 'Unit': x['meta']['Unit'], From f30cbde99e27ff2d8fce711599d824eb347dd0e8 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 08:04:40 +0100 Subject: [PATCH 15/26] use first constituent as default --- python/damask/dadf5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py index 60f993bf9..84168cd94 100644 --- a/python/damask/dadf5.py +++ b/python/damask/dadf5.py @@ -369,7 +369,7 @@ class DADF5(): return f[self.get_dataset_location('orientation')[0]].attrs['Lattice'].astype('str') # np.bytes_ to string - def read_dataset(self,path,c): + def read_dataset(self,path,c=0): """ Dataset for all points/cells. From c9b19444930d7ea9a9f6db6cf1d15535f7c459d9 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 08:50:27 +0100 Subject: [PATCH 16/26] handling of derived datatypes/structs currently required for orientation --- processing/post/DADF5_postResults.py | 4 ++-- python/damask/dadf5.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/processing/post/DADF5_postResults.py b/processing/post/DADF5_postResults.py index 0db018173..a16ef147c 100755 --- a/processing/post/DADF5_postResults.py +++ b/processing/post/DADF5_postResults.py @@ -65,7 +65,7 @@ for filename in options.filenames: x = results.get_dataset_location(label) if len(x) == 0: continue - array = results.read_dataset(x,0) + array = results.read_dataset(x,0,plain=True) d = int(np.product(np.shape(array)[1:])) data = np.concatenate((data,np.reshape(array,[np.product(results.grid),d])),1) @@ -80,7 +80,7 @@ for filename in options.filenames: x = results.get_dataset_location(label) if len(x) == 0: continue - array = results.read_dataset(x,0) + array = results.read_dataset(x,0,plain=True) d = int(np.product(np.shape(array)[1:])) data = np.concatenate((data,np.reshape(array,[np.product(results.grid),d])),1) diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py index 84168cd94..69b19fefd 100644 --- a/python/damask/dadf5.py +++ b/python/damask/dadf5.py @@ -369,7 +369,7 @@ class DADF5(): return f[self.get_dataset_location('orientation')[0]].attrs['Lattice'].astype('str') # np.bytes_ to string - def read_dataset(self,path,c=0): + def read_dataset(self,path,c=0,plain=False): """ Dataset for all points/cells. @@ -402,7 +402,7 @@ class DADF5(): a=a.reshape([a.shape[0],1]) dataset[p,:] = a[u,:] - return dataset + return dataset if not plain else dataset.view(('float64',len(dataset.dtype.names))) def cell_coordinates(self): From 6060abb3752b01fbf5d7efbe98ea0856ed4fc29b Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 08:52:46 +0100 Subject: [PATCH 17/26] cleaning --- processing/pre/geom_clean.py | 3 --- processing/pre/geom_mirror.py | 2 -- python/damask/geom.py | 8 ++++++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 7ee87fe0a..50f3657b2 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -5,9 +5,6 @@ import sys from io import StringIO from optparse import OptionParser -from scipy import ndimage -import numpy as np - import damask diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index 3f9755818..77ec1f4d7 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -5,8 +5,6 @@ import sys from io import StringIO from optparse import OptionParser -import numpy as np - import damask diff --git a/python/damask/geom.py b/python/damask/geom.py index 6fb9af5a5..6dfcda013 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -311,8 +311,8 @@ class Geom(): """ header = self.get_header() grid = self.get_grid() - format_string = '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) if self.microstructure.dtype == int \ - else '%g' + format_string = '%g' if self.microstructure in np.sctypes['float'] else \ + '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) np.savetxt(fname, self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, header='\n'.join(header), fmt=format_string, comments='') @@ -390,12 +390,14 @@ class Geom(): def mirror(self,directions,reflect=False): """ Mirror microstructure along given directions. + Parameters ---------- directions : iterable containing str direction(s) along which the microstructure is mirrored. Valid entries are 'x', 'y', 'z'. reflect : bool, optional reflect (include) outermost layers. + """ valid = {'x','y','z'} if not all(isinstance(d, str) for d in directions): @@ -420,10 +422,12 @@ class Geom(): def clean(self,stencil=3): """ Smooth microstructure by selecting most frequent index within given stencil at each location. + Parameters ---------- stencil : int, optional size of smoothing stencil. + """ def mostFrequent(arr): unique, inverse = np.unique(arr, return_inverse=True) From 02df55b9bde2d94c2caacf4a4a9ecac8f5dd7e35 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 11:18:41 +0100 Subject: [PATCH 18/26] bugfix casting to plain array works only (and makes sense only) for a derived type --- python/damask/dadf5.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py index 69b19fefd..d879946eb 100644 --- a/python/damask/dadf5.py +++ b/python/damask/dadf5.py @@ -401,8 +401,11 @@ class DADF5(): if len(a.shape) == 1: a=a.reshape([a.shape[0],1]) dataset[p,:] = a[u,:] - - return dataset if not plain else dataset.view(('float64',len(dataset.dtype.names))) + + if plain and dataset.dtype.names is not None: + return dataset.view(('float64',len(dataset.dtype.names))) + else: + return dataset def cell_coordinates(self): From 8186be6293de991047a9acfbbbc6ad20397f2d28 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 14:27:24 +0100 Subject: [PATCH 19/26] compress functionality should be part of the geom class automated decision is base on heuristic whether compression is memory efficient --- processing/pre/geom_clean.py | 5 +-- processing/pre/geom_mirror.py | 5 +-- processing/pre/geom_pack.py | 41 ++---------------------- processing/pre/geom_unpack.py | 5 +-- python/damask/geom.py | 60 +++++++++++++++++++++++++++++++---- 5 files changed, 59 insertions(+), 57 deletions(-) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 50f3657b2..65700ab61 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -42,7 +42,4 @@ for name in filenames: geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index 77ec1f4d7..f27b7eb66 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -46,7 +46,4 @@ for name in filenames: damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_pack.py b/processing/pre/geom_pack.py index 786a40b95..e927c006f 100755 --- a/processing/pre/geom_pack.py +++ b/processing/pre/geom_pack.py @@ -33,42 +33,5 @@ for name in filenames: damask.util.croak(geom) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - - compressType = None - former = start = -1 - reps = 0 - - if name is None: - f = sys.stdout - else: - f= open(name,'w') - - for current in geom.microstructure.flatten('F'): - if abs(current - former) == 1 and (start - current) == reps*(former - current): - compressType = 'to' - reps += 1 - elif current == former and start == former: - compressType = 'of' - reps += 1 - else: - if compressType is None: - f.write('\n'.join(geom.get_header())+'\n') - elif compressType == '.': - f.write('{}\n'.format(former)) - elif compressType == 'to': - f.write('{} to {}\n'.format(start,former)) - elif compressType == 'of': - f.write('{} of {}\n'.format(reps,former)) - - compressType = '.' - start = current - reps = 1 - - former = current - - if compressType == '.': - f.write('{}\n'.format(former)) - elif compressType == 'to': - f.write('{} to {}\n'.format(start,former)) - elif compressType == 'of': - f.write('{} of {}\n'.format(reps,former)) + + geom.to_file(sys.stdout if name is None else name,pack=True) diff --git a/processing/pre/geom_unpack.py b/processing/pre/geom_unpack.py index 2a2d54130..58bd5de87 100755 --- a/processing/pre/geom_unpack.py +++ b/processing/pre/geom_unpack.py @@ -34,7 +34,4 @@ for name in filenames: damask.util.croak(geom) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/python/damask/geom.py b/python/damask/geom.py index 6dfcda013..61d7c64a4 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -299,7 +299,7 @@ class Geom(): return cls(microstructure.reshape(grid),size,origin,homogenization,comments) - def to_file(self,fname): + def to_file(self,fname,pack=None): """ Writes a geom file. @@ -307,15 +307,63 @@ class Geom(): ---------- fname : str or file handle geometry file to write. + pack : bool, optional + compress geometry with 'x of y' and 'a to b'. """ header = self.get_header() grid = self.get_grid() - format_string = '%g' if self.microstructure in np.sctypes['float'] else \ - '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) - np.savetxt(fname, - self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, - header='\n'.join(header), fmt=format_string, comments='') + + if pack is None: + plain = grid.prod()/np.unique(self.microstructure).size < 250 + else: + plain = not pack + + if plain: + format_string = '%g' if self.microstructure in np.sctypes['float'] else \ + '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) + np.savetxt(fname, + self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, + header='\n'.join(header), fmt=format_string, comments='') + else: + if isinstance(fname,str): + f = open(fname,'w') + else: + f = fname + + compressType = None + former = start = -1 + reps = 0 + for current in self.microstructure.flatten('F'): + if abs(current - former) == 1 and (start - current) == reps*(former - current): + compressType = 'to' + reps += 1 + elif current == former and start == former: + compressType = 'of' + reps += 1 + else: + if compressType is None: + f.write('\n'.join(self.get_header())+'\n') + elif compressType == '.': + f.write('{}\n'.format(former)) + elif compressType == 'to': + f.write('{} to {}\n'.format(start,former)) + elif compressType == 'of': + f.write('{} of {}\n'.format(reps,former)) + + compressType = '.' + start = current + reps = 1 + + former = current + + if compressType == '.': + f.write('{}\n'.format(former)) + elif compressType == 'to': + f.write('{} to {}\n'.format(start,former)) + elif compressType == 'of': + f.write('{} of {}\n'.format(reps,former)) + def to_vtk(self,fname=None): """ From 816e86ae5fa5240bfae20b75ce305aa19564bb11 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 15:13:26 +0100 Subject: [PATCH 20/26] phasing out python shell scripts --- processing/pre/geom_rescale.py | 19 ++++--------------- python/damask/geom.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index e84c7597b..84ac9e793 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -55,20 +55,9 @@ for name in filenames: np.array([o*float(n.lower().replace('x','')) if n.lower().endswith('x') \ else float(n) for o,n in zip(size,options.size)],dtype=float) - damask.util.croak(geom.update(microstructure = - ndimage.interpolation.zoom( - geom.microstructure, - new_grid/grid, - output=geom.microstructure.dtype, - order=0, - mode='nearest', - prefilter=False, - ) if np.any(new_grid != grid) \ - else None, - size = new_size)) + geom.scale(new_grid) + damask.util.croak(geom.update(microstructure = None, + size = new_size)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/python/damask/geom.py b/python/damask/geom.py index 61d7c64a4..872445459 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -467,6 +467,29 @@ class Geom(): #self.add_comments('tbd') + def scale(self,grid): + """ + Scale microstructure to new grid + + Parameters + ---------- + grid : iterable of int + new grid dimension + + """ + return self.update( + ndimage.interpolation.zoom( + self.microstructure, + grid/self.get_grid(), + output=self.microstructure.dtype, + order=0, + mode='nearest', + prefilter=False + ) + ) + #self.add_comments('tbd') + + def clean(self,stencil=3): """ Smooth microstructure by selecting most frequent index within given stencil at each location. From b2cdabd0094c07e291a0b6f93437fa0a711a2de2 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 18:21:05 +0100 Subject: [PATCH 21/26] getting rid of shell scripts --- processing/pre/geom_canvas.py | 5 +---- processing/pre/geom_clean.py | 3 --- processing/pre/geom_mirror.py | 2 -- processing/pre/geom_renumber.py | 13 ++----------- processing/pre/geom_rescale.py | 4 +--- processing/pre/geom_rotate.py | 5 +---- processing/pre/geom_vicinityOffset.py | 5 +---- python/damask/geom.py | 17 +++++++++++++++-- 8 files changed, 21 insertions(+), 33 deletions(-) diff --git a/processing/pre/geom_canvas.py b/processing/pre/geom_canvas.py index a44065dd2..e1093d33b 100755 --- a/processing/pre/geom_canvas.py +++ b/processing/pre/geom_canvas.py @@ -71,7 +71,4 @@ for name in filenames: damask.util.croak(geom.update(canvas,origin=origin+offset*size/old,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 65700ab61..153d1eebd 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -37,9 +37,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(geom.clean(options.stencil)) - geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index f27b7eb66..ff47cb88b 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -42,8 +42,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_renumber.py b/processing/pre/geom_renumber.py index 2eee189e1..df817b1fc 100755 --- a/processing/pre/geom_renumber.py +++ b/processing/pre/geom_renumber.py @@ -32,15 +32,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - - renumbered = np.empty(geom.get_grid(),dtype=geom.microstructure.dtype) - for i, oldID in enumerate(np.unique(geom.microstructure)): - renumbered = np.where(geom.microstructure == oldID, i+1, renumbered) - - damask.util.croak(geom.update(renumbered)) + damask.util.croak(self.renumber) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index 84ac9e793..8f5276b9d 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -56,8 +56,6 @@ for name in filenames: else float(n) for o,n in zip(size,options.size)],dtype=float) geom.scale(new_grid) - damask.util.croak(geom.update(microstructure = None, - size = new_size)) + damask.util.croak(geom.update(microstructure = None,size = new_size)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_rotate.py b/processing/pre/geom_rotate.py index c2a4af04b..6cca99caf 100755 --- a/processing/pre/geom_rotate.py +++ b/processing/pre/geom_rotate.py @@ -95,7 +95,4 @@ for name in filenames: damask.util.croak(geom.update(microstructure,origin=origin-(np.asarray(microstructure.shape)-grid)/2*size/grid,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_vicinityOffset.py b/processing/pre/geom_vicinityOffset.py index 3a4853121..0b86adfb9 100755 --- a/processing/pre/geom_vicinityOffset.py +++ b/processing/pre/geom_vicinityOffset.py @@ -82,7 +82,4 @@ for name in filenames: geom.microstructure + offset,geom.microstructure))) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/python/damask/geom.py b/python/damask/geom.py index 872445459..e544765a4 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -504,7 +504,20 @@ class Geom(): unique, inverse = np.unique(arr, return_inverse=True) return unique[np.argmax(np.bincount(inverse))] - return self.update(ndimage.filters.generic_filter(self.microstructure, + return self.update(ndimage.filters.generic_filter( + self.microstructure, mostFrequent, - size=(stencil,)*3).astype(self.microstructure.dtype)) + size=(stencil,)*3 + ).astype(self.microstructure.dtype) + ) + #self.add_comments('tbd') + + + def renumber(self): + """Renumber sorted microstructure indices to 1,...,N.""" + renumbered = np.empty(self.get_grid(),dtype=self.microstructure.dtype) + for i, oldID in enumerate(np.unique(self.microstructure)): + renumbered = np.where(self.microstructure == oldID, i+1, renumbered) + + return self.update(renumbered)) #self.add_comments('tbd') From c9f9f7c681e6e6ae841c9956dd3cf1dc0b5d2874 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 19:02:19 +0100 Subject: [PATCH 22/26] pack causes trouble with tests .. --- processing/pre/geom_addPrimitive.py | 5 +---- processing/pre/geom_canvas.py | 4 ++-- processing/pre/geom_clean.py | 2 +- processing/pre/geom_fromDREAM3D.py | 2 +- processing/pre/geom_fromMinimalSurface.py | 5 +---- processing/pre/geom_fromOsteonGeometry.py | 5 +---- processing/pre/geom_fromScratch.py | 5 +---- processing/pre/geom_fromTable.py | 5 +---- processing/pre/geom_fromVoronoiTessellation.py | 5 +---- processing/pre/geom_grainGrowth.py | 5 +---- processing/pre/geom_mirror.py | 2 +- processing/pre/geom_renumber.py | 2 +- processing/pre/geom_rescale.py | 2 +- processing/pre/geom_rotate.py | 2 +- processing/pre/geom_translate.py | 5 +---- processing/pre/geom_vicinityOffset.py | 2 +- python/damask/geom.py | 2 +- 17 files changed, 18 insertions(+), 42 deletions(-) diff --git a/processing/pre/geom_addPrimitive.py b/processing/pre/geom_addPrimitive.py index 3e147e24d..f33ba27b1 100755 --- a/processing/pre/geom_addPrimitive.py +++ b/processing/pre/geom_addPrimitive.py @@ -132,7 +132,4 @@ for name in filenames: damask.util.croak(geom.update(np.where(mask,geom.microstructure,fill))) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_canvas.py b/processing/pre/geom_canvas.py index e1093d33b..edd5fe622 100755 --- a/processing/pre/geom_canvas.py +++ b/processing/pre/geom_canvas.py @@ -61,7 +61,7 @@ for name in filenames: canvas = np.full(new,options.fill if options.fill is not None else np.nanmax(geom.microstructure)+1,geom.microstructure.dtype) - l = np.clip( offset, 0,np.minimum(old +offset,new)) + l = np.clip( offset, 0,np.minimum(old +offset,new)) # noqa r = np.clip( offset+old,0,np.minimum(old*2+offset,new)) L = np.clip(-offset, 0,np.minimum(new -offset,old)) R = np.clip(-offset+new,0,np.minimum(new*2-offset,old)) @@ -71,4 +71,4 @@ for name in filenames: damask.util.croak(geom.update(canvas,origin=origin+offset*size/old,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 153d1eebd..8883c1b2a 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -39,4 +39,4 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) damask.util.croak(geom.clean(options.stencil)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromDREAM3D.py b/processing/pre/geom_fromDREAM3D.py index 5d41e05b9..b20749b45 100755 --- a/processing/pre/geom_fromDREAM3D.py +++ b/processing/pre/geom_fromDREAM3D.py @@ -155,4 +155,4 @@ for name in filenames: homogenization=options.homogenization,comments=header) damask.util.croak(geom) - geom.to_file(os.path.splitext(name)[0]+'.geom') + geom.to_file(os.path.splitext(name)[0]+'.geom',pack=False) diff --git a/processing/pre/geom_fromMinimalSurface.py b/processing/pre/geom_fromMinimalSurface.py index ab42ce5af..bb6859b54 100755 --- a/processing/pre/geom_fromMinimalSurface.py +++ b/processing/pre/geom_fromMinimalSurface.py @@ -89,7 +89,4 @@ geom=damask.Geom(microstructure,options.size, comments=[scriptID + ' ' + ' '.join(sys.argv[1:])]) damask.util.croak(geom) -if name is None: - sys.stdout.write(str(geom.show())) -else: - geom.to_file(name) +geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromOsteonGeometry.py b/processing/pre/geom_fromOsteonGeometry.py index 146bf216c..499a8867f 100755 --- a/processing/pre/geom_fromOsteonGeometry.py +++ b/processing/pre/geom_fromOsteonGeometry.py @@ -145,7 +145,4 @@ geom = damask.Geom(microstructure.reshape(grid), homogenization=options.homogenization,comments=header) damask.util.croak(geom) -if name is None: - sys.stdout.write(str(geom.show())) -else: - geom.to_file(name) +geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromScratch.py b/processing/pre/geom_fromScratch.py index bfb294080..89fd27be5 100755 --- a/processing/pre/geom_fromScratch.py +++ b/processing/pre/geom_fromScratch.py @@ -63,7 +63,4 @@ geom = damask.Geom(microstructure=np.full(options.grid,options.fill,dtype=dtype) comments=scriptID + ' ' + ' '.join(sys.argv[1:])) damask.util.croak(geom) -if name is None: - sys.stdout.write(str(geom.show())) -else: - geom.to_file(name) +geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromTable.py b/processing/pre/geom_fromTable.py index aa37451c7..f513c4834 100755 --- a/processing/pre/geom_fromTable.py +++ b/processing/pre/geom_fromTable.py @@ -152,7 +152,4 @@ for name in filenames: homogenization=options.homogenization,comments=header) damask.util.croak(geom) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(os.path.splitext(name)[0]+'.geom') + geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False) diff --git a/processing/pre/geom_fromVoronoiTessellation.py b/processing/pre/geom_fromVoronoiTessellation.py index 9d4573c2c..28e215f85 100755 --- a/processing/pre/geom_fromVoronoiTessellation.py +++ b/processing/pre/geom_fromVoronoiTessellation.py @@ -302,7 +302,4 @@ for name in filenames: homogenization=options.homogenization,comments=header) damask.util.croak(geom) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(os.path.splitext(name)[0]+'.geom') + geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False) diff --git a/processing/pre/geom_grainGrowth.py b/processing/pre/geom_grainGrowth.py index b31fc13f2..bdf8d8efe 100755 --- a/processing/pre/geom_grainGrowth.py +++ b/processing/pre/geom_grainGrowth.py @@ -172,7 +172,4 @@ for name in filenames: damask.util.croak(geom.update(microstructure[0:grid_original[0],0:grid_original[1],0:grid_original[2]])) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index ff47cb88b..cca0a4e10 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -44,4 +44,4 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_renumber.py b/processing/pre/geom_renumber.py index df817b1fc..1e28ca921 100755 --- a/processing/pre/geom_renumber.py +++ b/processing/pre/geom_renumber.py @@ -34,4 +34,4 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) damask.util.croak(self.renumber) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index 8f5276b9d..55c2e57a2 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -58,4 +58,4 @@ for name in filenames: geom.scale(new_grid) damask.util.croak(geom.update(microstructure = None,size = new_size)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_rotate.py b/processing/pre/geom_rotate.py index 6cca99caf..977e00b65 100755 --- a/processing/pre/geom_rotate.py +++ b/processing/pre/geom_rotate.py @@ -95,4 +95,4 @@ for name in filenames: damask.util.croak(geom.update(microstructure,origin=origin-(np.asarray(microstructure.shape)-grid)/2*size/grid,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_translate.py b/processing/pre/geom_translate.py index 4b91920ae..2d4279821 100755 --- a/processing/pre/geom_translate.py +++ b/processing/pre/geom_translate.py @@ -58,7 +58,4 @@ for name in filenames: damask.util.croak(geom.update(substituted,origin=geom.get_origin()+options.origin)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_vicinityOffset.py b/processing/pre/geom_vicinityOffset.py index 0b86adfb9..e30779d31 100755 --- a/processing/pre/geom_vicinityOffset.py +++ b/processing/pre/geom_vicinityOffset.py @@ -82,4 +82,4 @@ for name in filenames: geom.microstructure + offset,geom.microstructure))) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/python/damask/geom.py b/python/damask/geom.py index e544765a4..e2dcd9bcb 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -519,5 +519,5 @@ class Geom(): for i, oldID in enumerate(np.unique(self.microstructure)): renumbered = np.where(self.microstructure == oldID, i+1, renumbered) - return self.update(renumbered)) + return self.update(renumbered) #self.add_comments('tbd') From 3e8518d861e68146f31fecc9c13c90634125858d Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 19:25:01 +0100 Subject: [PATCH 23/26] following prospector guidelines --- processing/pre/geom_fromDREAM3D.py | 12 ++++++------ processing/pre/geom_renumber.py | 4 +--- processing/pre/geom_rescale.py | 5 ++--- python/damask/geom.py | 2 +- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/processing/pre/geom_fromDREAM3D.py b/processing/pre/geom_fromDREAM3D.py index b20749b45..159793cd8 100755 --- a/processing/pre/geom_fromDREAM3D.py +++ b/processing/pre/geom_fromDREAM3D.py @@ -86,7 +86,7 @@ for name in filenames: * inFile[os.path.join(group_geom,'SPACING')][...] grid = inFile[os.path.join(group_geom,'DIMENSIONS')][...] origin = inFile[os.path.join(group_geom,'ORIGIN')][...] - except: + except KeyError: errors.append('Geometry data ({}) not found'.format(group_geom)) @@ -98,13 +98,13 @@ for name in filenames: try: quats = np.reshape(inFile[dataset][...],(np.product(grid),4)) rot = [damask.Rotation.fromQuaternion(q,True,P=+1) for q in quats] - except: + except KeyError: errors.append('Pointwise orientation (quaternion) data ({}) not readable'.format(dataset)) dataset = os.path.join(group_pointwise,options.phase) try: phase = np.reshape(inFile[dataset][...],(np.product(grid))) - except: + except KeyError: errors.append('Pointwise phase data ({}) not readable'.format(dataset)) microstructure = np.arange(1,np.product(grid)+1,dtype=int).reshape(grid,order='F') @@ -116,7 +116,7 @@ for name in filenames: dataset = os.path.join(group_pointwise,options.microstructure) try: microstructure = np.transpose(inFile[dataset][...].reshape(grid[::-1]),(2,1,0)) # convert from C ordering - except: + except KeyError: errors.append('Link between pointwise and grain average data ({}) not readable'.format(dataset)) group_average = os.path.join(rootDir,options.basegroup,options.average) @@ -124,13 +124,13 @@ for name in filenames: dataset = os.path.join(group_average,options.quaternion) try: rot = [damask.Rotation.fromQuaternion(q,True,P=+1) for q in inFile[dataset][...][1:]] # skip first entry (unindexed) - except: + except KeyError: errors.append('Average orientation data ({}) not readable'.format(dataset)) dataset = os.path.join(group_average,options.phase) try: phase = [i[0] for i in inFile[dataset][...]][1:] # skip first entry (unindexed) - except: + except KeyError: errors.append('Average phase data ({}) not readable'.format(dataset)) if errors != []: diff --git a/processing/pre/geom_renumber.py b/processing/pre/geom_renumber.py index 1e28ca921..b1db6ed13 100755 --- a/processing/pre/geom_renumber.py +++ b/processing/pre/geom_renumber.py @@ -5,8 +5,6 @@ import sys from io import StringIO from optparse import OptionParser -import numpy as np - import damask @@ -32,6 +30,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(self.renumber) + damask.util.croak(geom.renumber) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index 55c2e57a2..b1a15593c 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -2,11 +2,10 @@ import os import sys -import numpy as np - from io import StringIO from optparse import OptionParser -from scipy import ndimage + +import numpy as np import damask diff --git a/python/damask/geom.py b/python/damask/geom.py index e2dcd9bcb..69dfa9ec3 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -469,7 +469,7 @@ class Geom(): def scale(self,grid): """ - Scale microstructure to new grid + Scale microstructure to new grid. Parameters ---------- From 476569390a6e16990464426833b2de0a4244c4a3 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 25 Nov 2019 13:47:14 +0100 Subject: [PATCH 24/26] enable use of path objects, strings, and opened files --- python/damask/geom.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/python/damask/geom.py b/python/damask/geom.py index 69dfa9ec3..1c9e10cd1 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -250,11 +250,15 @@ class Geom(): geometry file to read. """ - with (open(fname) if isinstance(fname,str) else fname) as f: - f.seek(0) - header_length,keyword = f.readline().split()[:2] - header_length = int(header_length) - content = f.readlines() + try: + f = open(fname) + except TypeError: + f = fname + + f.seek(0) + header_length,keyword = f.readline().split()[:2] + header_length = int(header_length) + content = f.readlines() if not keyword.startswith('head') or header_length < 3: raise TypeError('Header length information missing or invalid') @@ -320,15 +324,15 @@ class Geom(): plain = not pack if plain: - format_string = '%g' if self.microstructure in np.sctypes['float'] else \ + format_string = '%g' if self.microstructure.dtype in np.sctypes['float'] else \ '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) np.savetxt(fname, self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, header='\n'.join(header), fmt=format_string, comments='') else: - if isinstance(fname,str): + try: f = open(fname,'w') - else: + except TypeError: f = fname compressType = None From 592878d3644c646b11561b3ddc4de9208ffd8c09 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 25 Nov 2019 17:29:13 +0100 Subject: [PATCH 25/26] need to invoke function --- processing/pre/geom_renumber.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/processing/pre/geom_renumber.py b/processing/pre/geom_renumber.py index b1db6ed13..6e51062a5 100755 --- a/processing/pre/geom_renumber.py +++ b/processing/pre/geom_renumber.py @@ -30,6 +30,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(geom.renumber) + damask.util.croak(geom.renumber()) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) geom.to_file(sys.stdout if name is None else name,pack=False) From 81c739192a499ea40b60f18e355b3e9922bab4d0 Mon Sep 17 00:00:00 2001 From: Test User Date: Tue, 26 Nov 2019 02:14:29 +0100 Subject: [PATCH 26/26] [skip ci] updated version information after successful test of v2.0.3-1097-ga7fca4df --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index a5ac7d281..1a1650364 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v2.0.3-1073-g6f3cb071 +v2.0.3-1097-ga7fca4df