diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 501b9e193..a421c6e79 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -192,21 +192,6 @@ Post_addGradient: - master - release -Post_ParaviewRelated: - stage: postprocessing - script: ParaviewRelated/test.py - except: - - master - - release - -Post_OrientationConversion: - stage: postprocessing - script: - - OrientationConversion/test.py - except: - - master - - release - Post_OrientationAverageMisorientation: stage: postprocessing script: diff --git a/PRIVATE b/PRIVATE index a6109acd2..073913bf1 160000 --- a/PRIVATE +++ b/PRIVATE @@ -1 +1 @@ -Subproject commit a6109acd264c683fd335b1d1f69934fc4a4078e3 +Subproject commit 073913bf152b3c48f358dfbea5145807c0f7690d diff --git a/VERSION b/VERSION index 4a0b49cc6..be876f016 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v2.0.3-2654-g5c544a6e +v2.0.3-2717-g52aacf37 diff --git a/examples/ConfigFiles/Phase_DisloUCLA_Tungsten.config b/examples/ConfigFiles/Phase_DisloUCLA_Tungsten.config index 6920aee2b..2cdd5c58f 100644 --- a/examples/ConfigFiles/Phase_DisloUCLA_Tungsten.config +++ b/examples/ConfigFiles/Phase_DisloUCLA_Tungsten.config @@ -27,7 +27,7 @@ SolidSolutionStrength 0.0 # 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] diff --git a/installation/symlink_Processing.py b/installation/symlink_Processing.py index 5972a7f6a..441e9a9ea 100755 --- a/installation/symlink_Processing.py +++ b/installation/symlink_Processing.py @@ -1,56 +1,30 @@ #!/usr/bin/env python3 # Makes postprocessing routines accessible from everywhere. -import os import sys +from pathlib import Path import damask -damaskEnv = damask.Environment() -baseDir = damaskEnv.relPath('processing/') -binDir = damaskEnv.relPath('bin/') +env = damask.Environment() +bin_dir = env.root_dir/Path('bin') -if not os.path.isdir(binDir): - os.mkdir(binDir) +if not bin_dir.exists(): + bin_dir.mkdir() -#define ToDo list -processing_subDirs = ['pre', - 'post', - ] sys.stdout.write('\nsymbolic linking...\n') +for sub_dir in ['pre','post']: + the_dir = env.root_dir/Path('processing')/Path(sub_dir) -for subDir in processing_subDirs: - theDir = os.path.abspath(os.path.join(baseDir,subDir)) - - sys.stdout.write('\n'+binDir+' ->\n'+theDir+damask.util.deemph(' ...')+'\n') - - for theFile in os.listdir(theDir): - theName,theExt = os.path.splitext(theFile) - if theExt in ['.py']: - - src = os.path.abspath(os.path.join(theDir,theFile)) - sym_link = os.path.abspath(os.path.join(binDir,theName)) - - if os.path.lexists(sym_link): - os.remove(sym_link) - output = theName+damask.util.deemph(theExt) - else: - output = damask.util.emph(theName)+damask.util.deemph(theExt) - - sys.stdout.write(damask.util.deemph('... ')+output+'\n') - os.symlink(src,sym_link) + for the_file in the_dir.glob('*.py'): + src = the_dir/the_file + dst = bin_dir/Path(the_file.with_suffix('').name) + if dst.is_file(): dst.unlink() # dst.unlink(True) for Python >3.8 + dst.symlink_to(src) sys.stdout.write('\npruning broken links...\n') - -brokenLinks = 0 - -for filename in os.listdir(binDir): - path = os.path.join(binDir,filename) - if os.path.islink(path) and not os.path.exists(path): - sys.stdout.write(' '+damask.util.delete(path)+'\n') - os.remove(path) - brokenLinks += 1 - -sys.stdout.write(('none.' if brokenLinks == 0 else '')+'\n') +for filename in bin_dir.glob('*'): + if not filename.is_file(): + filename.unlink diff --git a/processing/post/addAPS34IDEstrainCoords.py b/processing/legacy/addAPS34IDEstrainCoords.py similarity index 100% rename from processing/post/addAPS34IDEstrainCoords.py rename to processing/legacy/addAPS34IDEstrainCoords.py diff --git a/processing/post/addCumulative.py b/processing/legacy/addCumulative.py similarity index 100% rename from processing/post/addCumulative.py rename to processing/legacy/addCumulative.py diff --git a/processing/post/addEhkl.py b/processing/legacy/addEhkl.py similarity index 100% rename from processing/post/addEhkl.py rename to processing/legacy/addEhkl.py diff --git a/processing/post/imageData.py b/processing/legacy/imageData.py similarity index 100% rename from processing/post/imageData.py rename to processing/legacy/imageData.py diff --git a/processing/post/imageDataDeformed.py b/processing/legacy/imageDataDeformed.py similarity index 100% rename from processing/post/imageDataDeformed.py rename to processing/legacy/imageDataDeformed.py diff --git a/processing/post/imageDataRGB.py b/processing/legacy/imageDataRGB.py similarity index 100% rename from processing/post/imageDataRGB.py rename to processing/legacy/imageDataRGB.py diff --git a/processing/post/binXY.py b/processing/post/binXY.py deleted file mode 100755 index 860b70027..000000000 --- a/processing/post/binXY.py +++ /dev/null @@ -1,143 +0,0 @@ -#!/usr/bin/env python3 - -import os -import sys -from io import StringIO -from optparse import OptionParser - -import numpy as np - -import damask - - -scriptName = os.path.splitext(os.path.basename(__file__))[0] -scriptID = ' '.join([scriptName,damask.version]) - - -# -------------------------------------------------------------------- -# MAIN -# -------------------------------------------------------------------- - -parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """ -Produces a binned grid of two columns from an ASCIItable, i.e. a two-dimensional probability density map. - -""", version = scriptID) - -parser.add_option('-d','--data', - dest = 'data', - type = 'string', nargs = 2, metavar = 'string string', - help = 'column labels containing x and y ') -parser.add_option('-w','--weight', - dest = 'weight', - type = 'string', metavar = 'string', - help = 'column label containing weight of (x,y) point') -parser.add_option('-b','--bins', - dest = 'bins', - type = 'int', nargs = 2, metavar = 'int int', - help = 'number of bins in x and y direction [%default]') -parser.add_option('-t','--type', - dest = 'type', - type = 'string', nargs = 3, metavar = 'string string string', - help = 'type (linear/log) of x, y, and z axis [%default]') -parser.add_option('-x','--xrange', - dest = 'xrange', - type = 'float', nargs = 2, metavar = 'float float', - help = 'min max limits in x direction (optional)') -parser.add_option('-y','--yrange', - dest = 'yrange', - type = 'float', nargs = 2, metavar = 'float float', - help = 'min max limits in y direction (optional)') -parser.add_option('-z','--zrange', - dest = 'zrange', - type = 'float', nargs = 2, metavar = 'float float', - help = 'min max limits in z direction (optional)') -parser.add_option('-i','--invert', - dest = 'invert', - action = 'store_true', - help = 'invert probability density') -parser.add_option('-r','--rownormalize', - dest = 'normRow', - action = 'store_true', - help = 'normalize probability density in each row') -parser.add_option('-c','--colnormalize', - dest = 'normCol', - action = 'store_true', - help = 'normalize probability density in each column') - -parser.set_defaults(bins = (10,10), - type = ('linear','linear','linear'), - xrange = (0.0,0.0), - yrange = (0.0,0.0), - zrange = (0.0,0.0), - ) - -(options,filenames) = parser.parse_args() -if filenames == []: filenames = [None] - -minmax = np.array([options.xrange,options.yrange,options.zrange]) -result = np.empty((options.bins[0],options.bins[1],3),'f') - -if options.data is None: parser.error('no data columns specified.') - -for name in filenames: - damask.util.report(scriptName,name) - - table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) - data = np.hstack((table.get(options.data[0]),table.get(options.data[1]))) - - for c in (0,1): # check data minmax for x and y (i = 0 and 1) - if (minmax[c] == 0.0).all(): minmax[c] = [data[:,c].min(),data[:,c].max()] - if options.type[c].lower() == 'log': # if log scale - data[:,c] = np.log(data[:,c]) # change x,y coordinates to log - minmax[c] = np.log(minmax[c]) # change minmax to log, too - - delta = minmax[:,1]-minmax[:,0] - (grid,xedges,yedges) = np.histogram2d(data[:,0],data[:,1], - bins=options.bins, - range=minmax[:2], - weights=table.get(options.weight) if options.weight else None) - if options.normCol: - for x in range(options.bins[0]): - sum = np.sum(grid[x,:]) - if sum > 0.0: - grid[x,:] /= sum - if options.normRow: - for y in range(options.bins[1]): - sum = np.sum(grid[:,y]) - if sum > 0.0: - grid[:,y] /= sum - - if (minmax[2] == 0.0).all(): minmax[2] = [grid.min(),grid.max()] # auto scale from data - if minmax[2,0] == minmax[2,1]: - minmax[2,0] -= 1. - minmax[2,1] += 1. - if (minmax[2] == 0.0).all(): # no data in grid? - damask.util.croak('no data found on grid...') - minmax[2,:] = np.array([0.0,1.0]) # making up arbitrary z minmax - if options.type[2].lower() == 'log': - grid = np.log(grid) - minmax[2] = np.log(minmax[2]) - - delta[2] = minmax[2,1]-minmax[2,0] - - for x in range(options.bins[0]): - for y in range(options.bins[1]): - result[x,y,:] = [minmax[0,0]+delta[0]/options.bins[0]*(x+0.5), - minmax[1,0]+delta[1]/options.bins[1]*(y+0.5), - np.clip((grid[x,y]-minmax[2,0])/delta[2],0.0,1.0)] - - for c in (0,1): - if options.type[c].lower() == 'log': result[:,:,c] = np.exp(result[:,:,c]) - - if options.invert: result[:,:,2] = 1.0 - result[:,:,2] - - comments = scriptID + '\t' + ' '.join(sys.argv[1:]) - shapes = {'bin_%s'%options.data[0]:(1,),'bin_%s'%options.data[1]:(1,),'z':(1,)} - table = damask.Table(result.reshape(options.bins[0]*options.bins[1],3),shapes,[comments]) - if name: - outname = os.path.join(os.path.dirname(name),'binned-{}-{}_'.format(*options.data) + - ('weighted-{}_'.format(options.weight) if options.weight else '') + - os.path.basename(name)) - table.to_ASCII(outname) - else: - table.to_ASCII(sys.stdout) diff --git a/processing/post/vtk_addData.py b/processing/post/vtk_addData.py deleted file mode 100755 index 25fcdada0..000000000 --- a/processing/post/vtk_addData.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env python3 - -import os -import sys -from io import StringIO -from optparse import OptionParser - -import numpy as np - -import damask - -scriptName = os.path.splitext(os.path.basename(__file__))[0] -scriptID = ' '.join([scriptName,damask.version]) - - -# -------------------------------------------------------------------- -# MAIN -# -------------------------------------------------------------------- - -parser = OptionParser(option_class=damask.extendableOption, - usage='%prog options [ASCIItable(s)]', - description = 'Add scalars/vectors, tensors, and/or a RGB tuples from ASCIItable ' - + 'to existing VTK file (.vtr/.vtu/.vtp).', - version = scriptID) - -parser.add_option( '--vtk', - dest = 'vtk', - type = 'string', metavar = 'string', - help = 'VTK file name') -parser.add_option('-d', '--data', - dest = 'data', - action = 'extend', metavar = '', - help = 'scalar/vector value(s) label(s)') -parser.add_option('-t', '--tensor', - dest = 'tensor', - action = 'extend', metavar = '', - help = 'tensor (3x3) value label(s)') -parser.add_option('-c', '--color', - dest = 'color', - action = 'extend', metavar = '', - help = 'RGB color tuple label') - -parser.set_defaults(data = [], - tensor = [], - color = [], -) - -(options, filenames) = parser.parse_args() -if filenames == []: filenames = [None] - -if not options.vtk: - parser.error('No VTK file specified.') - -for name in filenames: - damask.util.report(scriptName,name) - - table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) - vtk = damask.VTK.from_file(options.vtk) - - for data in options.data+options.tensor: - vtk.add(table.get(data),data) - for color in options.color: - vtk.add((table.get(color)*255).astype(np.uint8),color) - - vtk.write(options.vtk) diff --git a/processing/post/vtk_pointCloud.py b/processing/post/vtk_pointCloud.py deleted file mode 100755 index aae31d0ff..000000000 --- a/processing/post/vtk_pointCloud.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python3 - -import os -import sys -from io import StringIO -from optparse import OptionParser - -import damask - - -scriptName = os.path.splitext(os.path.basename(__file__))[0] -scriptID = ' '.join([scriptName,damask.version]) - - -# -------------------------------------------------------------------- -# MAIN -# -------------------------------------------------------------------- - -parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """ -Produce a VTK point cloud dataset based on coordinates given in an ASCIItable. - -""", version = scriptID) - -parser.add_option('-p', - '--pos', '--position', - dest = 'pos', - type = 'string', metavar = 'string', - help = 'label of coordinates [%default]') - -parser.set_defaults(pos = 'pos', - ) - -(options, filenames) = parser.parse_args() -if filenames == []: filenames = [None] - -for name in filenames: - damask.util.report(scriptName,name) - - table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) - v = damask.VTK.from_polyData(table.get(options.pos)) - - if name: - v.write(os.path.splitext(name)[0]) - else: - sys.stdout.write(v.__repr__()) diff --git a/processing/post/vtk_rectilinearGrid.py b/processing/post/vtk_rectilinearGrid.py deleted file mode 100755 index c8a7d1a6d..000000000 --- a/processing/post/vtk_rectilinearGrid.py +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env python3 - -import os -import sys -from io import StringIO -from optparse import OptionParser - -import damask - - -scriptName = os.path.splitext(os.path.basename(__file__))[0] -scriptID = ' '.join([scriptName,damask.version]) - - -# -------------------------------------------------------------------- -# MAIN -# -------------------------------------------------------------------- - -parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """ -Create regular voxel grid from points in an ASCIItable. - -""", version = scriptID) - -parser.add_option('-m', - '--mode', - dest = 'mode', - metavar='string', - type = 'choice', choices = ['cell','point'], - help = 'cell-centered or point-centered coordinates') -parser.add_option('-p', - '--pos', '--position', - dest = 'pos', - type = 'string', metavar = 'string', - help = 'label of coordinates [%default]') - -parser.set_defaults(mode = 'cell', - pos = 'pos', - ) - -(options, filenames) = parser.parse_args() -if filenames == []: filenames = [None] - -for name in filenames: - damask.util.report(scriptName,name) - - table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) - - if options.mode == 'cell': - grid, size, origin = damask.grid_filters.cell_coord0_gridSizeOrigin(table.get(options.pos)) - elif options.mode == 'point': - grid, size, origin = damask.grid_filters.node_coord0_gridSizeOrigin(table.get(options.pos)) - - v = damask.VTK.from_rectilinearGrid(grid,size,origin) - - if name: - v.write('{}_{}({})'.format(os.path.splitext(name)[0],options.pos,options.mode)) - else: - sys.stdout.write(v.__repr__()) diff --git a/processing/pre/mentat_pbcOnBoxMesh.py b/processing/pre/mentat_pbcOnBoxMesh.py index c171c6ccd..adf0b1aff 100755 --- a/processing/pre/mentat_pbcOnBoxMesh.py +++ b/processing/pre/mentat_pbcOnBoxMesh.py @@ -6,7 +6,7 @@ import numpy as np from optparse import OptionParser import damask -sys.path.append(damask.solver.Marc().libraryPath()) +sys.path.append(str(damask.solver.Marc().library_path)) scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptID = ' '.join([scriptName,damask.version]) @@ -197,14 +197,15 @@ def add_servoLinks(mfd_data,active=[True,True,True]): # directions on which to if mfd_data[i]['uid'] == 1705: del mfd_data[i] mfd_data.insert(i, links) - + + #-------------------------------------------------------------------------------------------------- # MAIN #-------------------------------------------------------------------------------------------------- + parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """ Set up servo linking to achieve periodic boundary conditions for a regular hexahedral mesh. Use *py_connection to operate on model presently opened in MSC.Mentat. - """, version = scriptID) parser.add_option('-p', '--port', @@ -229,10 +230,7 @@ if remote and filenames != []: if filenames == []: filenames = [None] if remote: - try: import py_mentat - except: - damask.util.croak('no valid Mentat release found.') - sys.exit(-1) + import py_mentat damask.util.report(scriptName, 'waiting to connect...') filenames = [os.path.join(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + '.mfd')] @@ -240,14 +238,14 @@ if remote: py_mentat.py_connect('',options.port) py_mentat.py_send('*set_save_formatted on') py_mentat.py_send('*save_as_model "{}" yes'.format(filenames[0])) - py_mentat.py_get_int("nnodes()") # hopefully blocks until file is written - except: - damask.util.croak('failed. try setting Tools/Python/"Run as Separate Process" & "Initiate".') - sys.exit() + py_mentat.py_get_int("nnodes()") + except py_mentat.InputError as err: + damask.util.croak('{}. Try Tools/Python/"Run as Separate Process" & "Initiate".'.format(err)) + sys.exit(-1) damask.util.croak( 'connected...') for name in filenames: - while remote and not os.path.exists(name): time.sleep(0.5) # wait for Mentat to write MFD file + while remote and not os.path.exists(name): time.sleep(0.5) with open( name,'r') if name is not None else sys.stdin as fileIn: damask.util.report(scriptName, name) mfd = parseMFD(fileIn) @@ -257,5 +255,4 @@ for name in filenames: fileOut.write(asMFD(mfd)) if remote: - try: py_mentat.py_send('*open_model "{}"'.format(filenames[0])) - except: damask.util.croak('lost connection on sending open command for "{}".'.format(filenames[0])) + py_mentat.py_send('*open_model "{}"'.format(filenames[0])) diff --git a/processing/pre/mentat_spectralBox.py b/processing/pre/mentat_spectralBox.py index a61bef57a..e6d138952 100755 --- a/processing/pre/mentat_spectralBox.py +++ b/processing/pre/mentat_spectralBox.py @@ -9,7 +9,7 @@ import damask scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptID = ' '.join([scriptName,damask.version]) -sys.path.append(damask.solver.Marc().libraryPath()) +sys.path.append(str(damask.solver.Marc().library_path)) #------------------------------------------------------------------------------------------------- def outMentat(cmd,locals): diff --git a/python/damask/__init__.py b/python/damask/__init__.py index 0f343a581..6d65f7cd1 100644 --- a/python/damask/__init__.py +++ b/python/damask/__init__.py @@ -1,9 +1,9 @@ """Tools for pre and post processing of DAMASK simulations.""" -import os as _os +from pathlib import Path as _Path import re as _re name = 'damask' -with open(_os.path.join(_os.path.dirname(__file__),'VERSION')) as _f: +with open(_Path(__file__).parent/_Path('VERSION')) as _f: version = _re.sub(r'^v','',_f.readline().strip()) # make classes directly accessible as damask.Class diff --git a/python/damask/_asciitable.py b/python/damask/_asciitable.py index 8410b23a1..9d762369a 100644 --- a/python/damask/_asciitable.py +++ b/python/damask/_asciitable.py @@ -157,7 +157,7 @@ class ASCIItable(): def head_write(self, header = True): """Write current header information (info + labels).""" - head = ['{}\theader'.format(len(self.info)+self.__IO__['labeled'])] if header else [] + head = [f"{len(self.info)+self.__IO__['labeled']}\theader"] if header else [] head.append(self.info) if self.__IO__['labeled']: head.append('\t'.join(map(self._quote,self.tags))) @@ -209,7 +209,7 @@ class ASCIItable(): labelList.append(tags[id]) else: label = tags[id][2:] # get label - while id < len(tags) and tags[id] == '{}_{}'.format(dim,label): # check successors + while id < len(tags) and tags[id] == f'{dim}_{label}': # check successors id += 1 # next label... dim += 1 # ...should be one higher dimension labelList.append(label) # reached end --> store diff --git a/python/damask/_colormaps.py b/python/damask/_colormaps.py index 2117b0b57..6c6f82604 100644 --- a/python/damask/_colormaps.py +++ b/python/damask/_colormaps.py @@ -1,4 +1,5 @@ import numpy as np +from . import util class Color: """Color representation in and conversion between different color-spaces.""" @@ -450,7 +451,7 @@ class Colormap: def rad_diff(a,b): return abs(a[2]-b[2]) - + def adjust_hue(Msh_sat, Msh_unsat): """If saturation of one of the two colors is too less than the other, hue of the less.""" if Msh_sat[0] >= Msh_unsat[0]: @@ -502,7 +503,7 @@ class Colormap: [RGB] colormap for use in paraview or gmsh, or as raw string, or array. Arguments: name, format, steps, crop. - Format is one of (paraview, gmsh, raw, list). + Format is one of (paraview, gmsh, gom, raw, list). Crop selects a (sub)range in [-1.0,1.0]. Generates sequential map if one limiting color is either white or black, diverging map otherwise. @@ -511,23 +512,22 @@ class Colormap: frac = 0.5*(np.array(crop) + 1.0) # rescale crop range to fractions colors = [self.color(float(i)/(steps-1)*(frac[1]-frac[0])+frac[0]).express_as(model).color for i in range(steps)] if format == 'paraview': - colormap = ['[\n {{\n "ColorSpace": "RGB", "Name": "{}", "DefaultMap": true,\n "RGBPoints" : ['.format(name)] \ - + [' {:4d},{:8.6f},{:8.6f},{:8.6f},'.format(i,color[0],color[1],color[2],) \ - for i,color in enumerate(colors[:-1])] \ - + [' {:4d},{:8.6f},{:8.6f},{:8.6f} '.format(len(colors),colors[-1][0],colors[-1][1],colors[-1][2],)] \ + colormap = [f'[\n {{\n "ColorSpace": "RGB", "Name": "{name}", "DefaultMap": true,\n "RGBPoints" : ['] \ + + [f' {i:4d},{color[0]:8.6f},{color[1]:8.6f},{color[2]:8.6f}{"," if i+1 bcc transformation @@ -616,17 +616,17 @@ class Lattice: # ToDo: Make a subclass of Symmetry! try: relationship = models[model] except KeyError : - raise KeyError('Orientation relationship "{}" is unknown'.format(model)) + raise KeyError(f'Orientation relationship "{model}" is unknown') if self.lattice not in relationship['mapping']: - raise ValueError('Relationship "{}" not supported for lattice "{}"'.format(model,self.lattice)) + raise ValueError(f'Relationship "{model}" not supported for lattice "{self.lattice}"') r = {'lattice':Lattice((set(relationship['mapping'])-{self.lattice}).pop()), # target lattice 'rotations':[] } myPlane_id = relationship['mapping'][self.lattice] otherPlane_id = (myPlane_id+1)%2 - myDir_id = myPlane_id +2 + myDir_id = myPlane_id +2 otherDir_id = otherPlane_id +2 for miller in np.hstack((relationship['planes'],relationship['directions'])): diff --git a/python/damask/_orientation.py b/python/damask/_orientation.py index 3c2d73a92..b3395acfd 100644 --- a/python/damask/_orientation.py +++ b/python/damask/_orientation.py @@ -61,7 +61,7 @@ class Orientation: # ToDo: make subclass of lattice and Rotation? if self.lattice.symmetry != other.lattice.symmetry: raise NotImplementedError('disorientation between different symmetry classes not supported yet.') - mySymEqs = self.equivalent if SST else self.equivalent[0] # take all or only first sym operation + mySymEqs = self.equivalent if SST else self.equivalent[0] #ToDo: This is just me! # take all or only first sym operation otherSymEqs = other.equivalent for i,sA in enumerate(mySymEqs): diff --git a/python/damask/_result.py b/python/damask/_result.py index 1396b560d..713905fe0 100644 --- a/python/damask/_result.py +++ b/python/damask/_result.py @@ -6,6 +6,7 @@ import os import datetime import xml.etree.ElementTree as ET import xml.dom.minidom +from pathlib import Path from functools import partial import h5py @@ -50,8 +51,7 @@ class Result: self.version_minor = f.attrs['DADF5-minor'] if self.version_major != 0 or not 2 <= self.version_minor <= 6: - raise TypeError('Unsupported DADF5 version {}.{} '.format(self.version_major, - self.version_minor)) + raise TypeError(f'Unsupported DADF5 version {self.version_major}.{self.version_minor}') self.structured = 'grid' in f['geometry'].attrs.keys() @@ -89,7 +89,7 @@ class Result: 'con_physics': self.con_physics, 'mat_physics': self.mat_physics } - self.fname = os.path.abspath(fname) + self.fname = Path(fname).absolute() self._allow_modification = False @@ -107,7 +107,7 @@ class Result: self.pick('increments',all_selected_increments) in_between = '' if len(all_selected_increments) < 3 else \ - ''.join(['\n{}\n ...\n'.format(inc) for inc in all_selected_increments[1:-2]]) + ''.join([f'\n{inc}\n ...\n' for inc in all_selected_increments[1:-2]]) return util.srepr(first + in_between + last) @@ -137,7 +137,7 @@ class Result: if what == 'increments': choice = [c if isinstance(c,str) and c.startswith('inc') else - 'inc{}'.format(c) for c in choice] + f'inc{c}' for c in choice] elif what == 'times': what = 'increments' if choice == ['*']: @@ -412,21 +412,19 @@ class Result: message = '' with h5py.File(self.fname,'r') as f: for i in self.iterate('increments'): - message += '\n{} ({}s)\n'.format(i,self.times[self.increments.index(i)]) + message += f'\n{i} ({self.times[self.increments.index(i)]}s)\n' for o,p in zip(['constituents','materialpoints'],['con_physics','mat_physics']): for oo in self.iterate(o): - message += ' {}\n'.format(oo) + message += f' {oo}\n' for pp in self.iterate(p): - message += ' {}\n'.format(pp) + message += f' {pp}\n' group = '/'.join([i,o[:-1],oo,pp]) # o[:-1]: plural/singular issue for d in f[group].keys(): try: dataset = f['/'.join([group,d])] + unit = f" / {dataset.attrs['Unit'].decode()}" if 'Unit' in dataset.attrs else '' description = dataset.attrs['Description'].decode() - if 'Unit' in dataset.attrs: - message += ' {} / ({}): {}\n'.format(d,dataset.attrs['Unit'].decode(),description) - else: - message += ' {}: {}\n'.format(d,description) + message += f' {d}{unit}: {description}\n' except KeyError: pass return message @@ -528,10 +526,10 @@ class Result: def _add_absolute(x): return { 'data': np.abs(x['data']), - 'label': '|{}|'.format(x['label']), + 'label': f'|{x["label"]}|', 'meta': { 'Unit': x['meta']['Unit'], - 'Description': 'Absolute value of {} ({})'.format(x['label'],x['meta']['Description']), + 'Description': f"Absolute value of {x['label']} ({x['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -552,14 +550,14 @@ class Result: def _add_calculation(**kwargs): formula = kwargs['formula'] for d in re.findall(r'#(.*?)#',formula): - formula = formula.replace('#{}#'.format(d),"kwargs['{}']['data']".format(d)) + formula = formula.replace(f'#{d}#',f"kwargs['{d}']['data']") return { 'data': eval(formula), 'label': kwargs['label'], 'meta': { 'Unit': kwargs['unit'], - 'Description': '{} (formula: {})'.format(kwargs['description'],kwargs['formula']), + 'Description': f"{kwargs['description']} (formula: {kwargs['formula']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -596,8 +594,9 @@ class Result: 'label': 'sigma', 'meta': { 'Unit': P['meta']['Unit'], - 'Description': 'Cauchy stress calculated from {} ({}) and {} ({})'\ - .format(P['label'],P['meta']['Description'],F['label'],F['meta']['Description']), + 'Description': "Cauchy stress calculated " + f"from {P['label']} ({P['meta']['Description']})" + f" and {F['label']} ({F['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -620,10 +619,10 @@ class Result: def _add_determinant(T): return { 'data': np.linalg.det(T['data']), - 'label': 'det({})'.format(T['label']), + 'label': f"det({T['label']})", 'meta': { 'Unit': T['meta']['Unit'], - 'Description': 'Determinant of tensor {} ({})'.format(T['label'],T['meta']['Description']), + 'Description': f"Determinant of tensor {T['label']} ({T['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -644,10 +643,10 @@ class Result: def _add_deviator(T): return { 'data': mechanics.deviatoric_part(T['data']), - 'label': 's_{}'.format(T['label']), + 'label': f"s_{T['label']}", 'meta': { 'Unit': T['meta']['Unit'], - 'Description': 'Deviator of tensor {} ({})'.format(T['label'],T['meta']['Description']), + 'Description': f"Deviator of tensor {T['label']} ({T['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -675,10 +674,10 @@ class Result: return { 'data': mechanics.eigenvalues(T_sym['data'])[:,p], - 'label': 'lambda_{}({})'.format(eigenvalue,T_sym['label']), + 'label': f"lambda_{eigenvalue}({T_sym['label']})", 'meta' : { 'Unit': T_sym['meta']['Unit'], - 'Description': '{} eigenvalue of {} ({})'.format(label,T_sym['label'],T_sym['meta']['Description']), + 'Description': f"{label} eigenvalue of {T_sym['label']} ({T_sym['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -708,14 +707,14 @@ class Result: print('p',eigenvalue) return { 'data': mechanics.eigenvectors(T_sym['data'])[:,p], - 'label': 'v_{}({})'.format(eigenvalue,T_sym['label']), + 'label': f"v_{eigenvalue}({T_sym['label']})", 'meta' : { 'Unit': '1', - 'Description': 'Eigenvector corresponding to {} eigenvalue of {} ({})'\ - .format(label,T_sym['label'],T_sym['meta']['Description']), + 'Description': f"Eigenvector corresponding to {label} eigenvalue" + f" of {T_sym['label']} ({T_sym['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } - } + } def add_eigenvector(self,T_sym,eigenvalue='max'): """ Add eigenvector of symmetric tensor. @@ -768,10 +767,10 @@ class Result: def _add_maximum_shear(T_sym): return { 'data': mechanics.maximum_shear(T_sym['data']), - 'label': 'max_shear({})'.format(T_sym['label']), + 'label': f"max_shear({T_sym['label']})", 'meta': { 'Unit': T_sym['meta']['Unit'], - 'Description': 'Maximum shear component of {} ({})'.format(T_sym['label'],T_sym['meta']['Description']), + 'Description': f"Maximum shear component of {T_sym['label']} ({T_sym['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -794,11 +793,11 @@ class Result: 'stress' return { - 'data': mechanics.Mises_strain(T_sym['data']) if t=='strain' else mechanics.Mises_stress(T_sym['data']), - 'label': '{}_vM'.format(T_sym['label']), + 'data': (mechanics.Mises_strain if t=='strain' else mechanics.Mises_stress)(T_sym['data']), + 'label': f"{T_sym['label']}_vM", 'meta': { 'Unit': T_sym['meta']['Unit'], - 'Description': 'Mises equivalent {} of {} ({})'.format(t,T_sym['label'],T_sym['meta']['Description']), + 'Description': f"Mises equivalent {t} of {T_sym['label']} ({T_sym['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -831,10 +830,10 @@ class Result: return { 'data': np.linalg.norm(x['data'],ord=o,axis=axis,keepdims=True), - 'label': '|{}|_{}'.format(x['label'],o), + 'label': f"|{x['label']}|_{o}", 'meta': { 'Unit': x['meta']['Unit'], - 'Description': '{}-norm of {} {} ({})'.format(o,t,x['label'],x['meta']['Description']), + 'Description': f"{o}-norm of {t} {x['label']} ({x['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -860,19 +859,20 @@ class Result: 'label': 'S', 'meta': { 'Unit': P['meta']['Unit'], - 'Description': '2. Piola-Kirchhoff stress calculated from {} ({}) and {} ({})'\ - .format(P['label'],P['meta']['Description'],F['label'],F['meta']['Description']), + 'Description': "2. Piola-Kirchhoff stress calculated " + f"from {P['label']} ({P['meta']['Description']})" + f" and {F['label']} ({F['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } def add_PK2(self,P='P',F='F'): """ - Add second Piola-Kirchhoff calculated from first Piola-Kirchhoff stress and deformation gradient. + Add second Piola-Kirchhoff stress calculated from first Piola-Kirchhoff stress and deformation gradient. Parameters ---------- P : str, optional - Label first Piola-Kirchhoff stress dataset. Defaults to ‘P’. + Label of first Piola-Kirchhoff stress dataset. Defaults to ‘P’. F : str, optional Label of deformation gradient dataset. Defaults to ‘F’. @@ -922,10 +922,10 @@ class Result: def _add_rotational_part(F): return { 'data': mechanics.rotational_part(F['data']), - 'label': 'R({})'.format(F['label']), + 'label': f"R({F['label']})", 'meta': { 'Unit': F['meta']['Unit'], - 'Description': 'Rotational part of {} ({})'.format(F['label'],F['meta']['Description']), + 'Description': f"Rotational part of {F['label']} ({F['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -946,10 +946,10 @@ class Result: def _add_spherical(T): return { 'data': mechanics.spherical_part(T['data']), - 'label': 'p_{}'.format(T['label']), + 'label': f"p_{T['label']}", 'meta': { 'Unit': T['meta']['Unit'], - 'Description': 'Spherical component of tensor {} ({})'.format(T['label'],T['meta']['Description']), + 'Description': f"Spherical component of tensor {T['label']} ({T['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -970,10 +970,10 @@ class Result: def _add_strain_tensor(F,t,m): return { 'data': mechanics.strain_tensor(F['data'],t,m), - 'label': 'epsilon_{}^{}({})'.format(t,m,F['label']), + 'label': f"epsilon_{t}^{m}({F['label']})", 'meta': { 'Unit': F['meta']['Unit'], - 'Description': 'Strain tensor of {} ({})'.format(F['label'],F['meta']['Description']), + 'Description': f"Strain tensor of {F['label']} ({F['meta']['Description']})", 'Creator': inspect.stack()[0][3][1:] } } @@ -1000,11 +1000,11 @@ class Result: @staticmethod def _add_stretch_tensor(F,t): return { - 'data': mechanics.left_stretch(F['data']) if t == 'V' else mechanics.right_stretch(F['data']), - 'label': '{}({})'.format(t,F['label']), + 'data': (mechanics.left_stretch if t.upper() == 'V' else mechanics.right_stretch)(F['data']), + 'label': f"{t}({F['label']})", 'meta': { 'Unit': F['meta']['Unit'], - 'Description': '{} stretch tensor of {} ({})'.format('Left' if t == 'V' else 'Right', + 'Description': '{} stretch tensor of {} ({})'.format('Left' if t.upper() == 'V' else 'Right', F['label'],F['meta']['Description']), 'Creator': inspect.stack()[0][3][1:] } @@ -1040,7 +1040,7 @@ class Result: r = func(**datasets_in,**args) return [group,r] except Exception as err: - print('Error during calculation: {}.'.format(err)) + print(f'Error during calculation: {err}.') return None @@ -1051,14 +1051,17 @@ class Result: Parameters ---------- func : function - Callback function that calculates a new dataset from one or more datasets per HDF5 group. + Callback function that calculates a new dataset from one or + more datasets per HDF5 group. datasets : dictionary - Details of the datasets to be used: label (in HDF5 file) and arg (argument to which the data is parsed in func). + Details of the datasets to be used: label (in HDF5 file) and + arg (argument to which the data is parsed in func). args : dictionary, optional Arguments parsed to func. """ - pool = multiprocessing.Pool(int(Environment().options['DAMASK_NUM_THREADS'])) + num_threads = Environment().options['DAMASK_NUM_THREADS'] + pool = multiprocessing.Pool(int(num_threads) if num_threads is not None else None) lock = multiprocessing.Manager().Lock() groups = self.groups_with_datasets(datasets.values()) @@ -1082,11 +1085,11 @@ class Result: for l,v in result[1]['meta'].items(): dataset.attrs[l]=v.encode() - creator = 'damask.Result.{} v{}'.format(dataset.attrs['Creator'].decode(),version) + creator = f"damask.Result.{dataset.attrs['Creator'].decode()} v{version}" dataset.attrs['Creator'] = creator.encode() except (OSError,RuntimeError) as err: - print('Could not add dataset: {}.'.format(err)) + print(f'Could not add dataset: {err}.') lock.release() pool.close() @@ -1119,7 +1122,7 @@ class Result: time_data = ET.SubElement(time, 'DataItem') time_data.attrib={'Format': 'XML', 'NumberType': 'Float', - 'Dimensions': '{}'.format(len(self.times))} + 'Dimensions': f'{len(self.times)}'} time_data.text = ' '.join(map(str,self.times)) attributes = [] @@ -1160,7 +1163,7 @@ class Result: data_items[-1].attrib={'Format': 'HDF', 'Precision': '8', 'Dimensions': '{} {} {} 3'.format(*(self.grid+1))} - data_items[-1].text='{}:/{}/geometry/u_n'.format(os.path.split(self.fname)[1],inc) + data_items[-1].text=f'{os.path.split(self.fname)[1]}:/{inc}/geometry/u_n' for o,p in zip(['constituents','materialpoints'],['con_physics','mat_physics']): for oo in getattr(self,o): @@ -1175,17 +1178,17 @@ class Result: if (shape not in [(1,), (3,), (3,3)]) or dtype != np.float64: continue attributes.append(ET.SubElement(grid, 'Attribute')) - attributes[-1].attrib={'Name': '{}'.format(name.split('/',2)[2]), + attributes[-1].attrib={'Name': name.split('/',2)[2], 'Center': 'Cell', 'AttributeType': 'Tensor'} data_items.append(ET.SubElement(attributes[-1], 'DataItem')) data_items[-1].attrib={'Format': 'HDF', 'NumberType': 'Float', - 'Precision': '{}'.format(prec), + 'Precision': f'{prec}', 'Dimensions': '{} {} {} {}'.format(*self.grid,np.prod(shape))} - data_items[-1].text='{}:{}'.format(os.path.split(self.fname)[1],name) + data_items[-1].text=f'{os.path.split(self.fname)[1]}:{name}' - with open(os.path.splitext(self.fname)[0]+'.xdmf','w') as f: + with open(self.fname.with_suffix('.xdmf').name,'w') as f: f.write(xml.dom.minidom.parseString(ET.tostring(xdmf).decode()).toprettyxml()) @@ -1261,7 +1264,4 @@ class Result: u = self.read_dataset(self.get_dataset_location('u_n' if mode.lower() == 'cell' else 'u_p')) v.add(u,'u') - file_out = '{}_inc{}'.format(os.path.splitext(os.path.basename(self.fname))[0], - inc[3:].zfill(N_digits)) - - v.write(file_out) + v.write(f'{self.fname.stem}_inc{inc[3:].zfill(N_digits)}') diff --git a/python/damask/_rotation.py b/python/damask/_rotation.py index 26fd3e261..91f126c88 100644 --- a/python/damask/_rotation.py +++ b/python/damask/_rotation.py @@ -20,8 +20,8 @@ class Rotation: when viewing from the end point of the rotation axis towards the origin. - rotations will be interpreted in the passive sense. - Euler angle triplets are implemented using the Bunge convention, - with the angular ranges as [0, 2π],[0, π],[0, 2π]. - - the rotation angle ω is limited to the interval [0, π]. + with the angular ranges as [0,2π], [0,π], [0,2π]. + - the rotation angle ω is limited to the interval [0,π]. - the real part of a quaternion is positive, Re(q) > 0 - P = -1 (as default). @@ -49,7 +49,8 @@ class Rotation: Parameters ---------- quaternion : numpy.ndarray, optional - Unit quaternion that follows the conventions. Use .from_quaternion to perform a sanity check. + Unit quaternion in positive real hemisphere. + Use .from_quaternion to perform a sanity check. """ if quaternion.shape[-1] != 4: @@ -76,7 +77,7 @@ class Rotation: return 'Quaternions:\n'+str(self.quaternion) # ToDo: could be nicer ... return '\n'.join([ 'Quaternion: (real={:.3f}, imag=<{:+.3f}, {:+.3f}, {:+.3f}>)'.format(*(self.quaternion)), - 'Matrix:\n{}'.format(self.as_matrix()), + 'Matrix:\n{}'.format(np.round(self.as_matrix(),8)), 'Bunge Eulers / deg: ({:3.2f}, {:3.2f}, {:3.2f})'.format(*self.as_Eulers(degrees=True)), ]) @@ -116,7 +117,7 @@ class Rotation: p_o = other.quaternion[...,1:] q = (q_m*q_o - np.einsum('...i,...i',p_m,p_o).reshape(self.shape+(1,))) p = q_m*p_o + q_o*p_m + _P * np.cross(p_m,p_o) - return self.__class__(np.block([q,p])).standardize() + return self.__class__(np.block([q,p]))._standardize() elif isinstance(other,np.ndarray): if self.shape + (3,) == other.shape: @@ -139,28 +140,26 @@ class Rotation: else: raise ValueError('Can only rotate vectors, 2nd order tensors, and 4th order tensors') else: - raise TypeError('Cannot rotate {}'.format(type(other))) - - def inverse(self): - """In-place inverse rotation/backward rotation.""" - self.quaternion[...,1:] *= -1 - return self - - #@property - def inversed(self): - """Inverse rotation/backward rotation.""" - return self.copy().inverse() + raise TypeError(f'Cannot rotate {type(other)}') - def standardize(self): - """In-place quaternion representation with positive real part.""" + def _standardize(self): + """Standardize (ensure positive real hemisphere).""" self.quaternion[self.quaternion[...,0] < 0.0] *= -1 return self - #@property - def standardized(self): - """Quaternion representation with positive real part.""" - return self.copy().standardize() + def inverse(self): + """In-place inverse rotation (backward rotation).""" + self.quaternion[...,1:] *= -1 + return self + + def __invert__(self): + """Inverse rotation (backward rotation).""" + return self.copy().inverse() + + def inversed(self): + """Inverse rotation (backward rotation).""" + return ~ self def misorientation(self,other): @@ -173,7 +172,7 @@ class Rotation: Rotation to which the misorientation is computed. """ - return other*self.inversed() + return other@~self def broadcast_to(self,shape): @@ -188,7 +187,7 @@ class Rotation: return self.__class__(q) - def average(self,other): + def average(self,other): #ToDo: discuss calling for vectors """ Calculate the average rotation. @@ -208,25 +207,31 @@ class Rotation: def as_quaternion(self): """ - Unit quaternion [q, p_1, p_2, p_3]. + Represent as unit quaternion. - Parameters - ---------- - quaternion : bool, optional - return quaternion as DAMASK object. + Returns + ------- + q : numpy.ndarray of shape (...,4) + Unit quaternion in positive real hemisphere: (q_0, q_1, q_2, q_3), |q|=1, q_0 ≥ 0. """ - return self.quaternion + return self.quaternion.copy() def as_Eulers(self, degrees = False): """ - Bunge-Euler angles: (φ_1, ϕ, φ_2). + Represent as Bunge-Euler angles. Parameters ---------- degrees : bool, optional - return angles in degrees. + Return angles in degrees. + + Returns + ------- + phi : numpy.ndarray of shape (...,3) + Bunge-Euler angles: (φ_1, ϕ, φ_2), φ_1 ∈ [0,2π], ϕ ∈ [0,π], φ_2 ∈ [0,2π] + unless degrees == True: φ_1 ∈ [0,360], ϕ ∈ [0,180], φ_2 ∈ [0,360] """ eu = Rotation._qu2eu(self.quaternion) @@ -237,14 +242,21 @@ class Rotation: degrees = False, pair = False): """ - Axis angle representation [n_1, n_2, n_3, ω] unless pair == True: ([n_1, n_2, n_3], ω). + Represent as axis angle pair. Parameters ---------- degrees : bool, optional - return rotation angle in degrees. + Return rotation angle in degrees. Defaults to False. pair : bool, optional - return tuple of axis and angle. + Return tuple of axis and angle. Defaults to False. + + Returns + ------- + axis_angle : numpy.ndarray of shape (...,4) unless pair == True: + tuple containing numpy.ndarray of shapes (...,3) and (...) + Axis angle pair: (n_1, n_2, n_3, ω), |n| = 1 and ω ∈ [0,π] + unless degrees = True: ω ∈ [0,180]. """ ax = Rotation._qu2ax(self.quaternion) @@ -252,30 +264,60 @@ class Rotation: return (ax[...,:3],ax[...,3]) if pair else ax def as_matrix(self): - """Rotation matrix.""" + """ + Represent as rotation matrix. + + Returns + ------- + R : numpy.ndarray of shape (...,3,3) + Rotation matrix R, det(R) = 1, R.T∙R=I. + + """ return Rotation._qu2om(self.quaternion) def as_Rodrigues(self, vector = False): """ - Rodrigues-Frank vector representation [n_1, n_2, n_3, tan(ω/2)] unless vector == True: [n_1, n_2, n_3] * tan(ω/2). + Represent as Rodrigues-Frank vector with separated axis and angle argument. Parameters ---------- vector : bool, optional - return as actual Rodrigues--Frank vector, i.e. rotation axis scaled by tan(ω/2). + Return as actual Rodrigues-Frank vector, i.e. axis + and angle argument are not separated. + + Returns + ------- + rho : numpy.ndarray of shape (...,4) unless vector == True: + numpy.ndarray of shape (...,3) + Rodrigues-Frank vector: [n_1, n_2, n_3, tan(ω/2)], |n| = 1 and ω ∈ [0,π]. """ ro = Rotation._qu2ro(self.quaternion) - with np.errstate(invalid='ignore'): - return ro[...,:3]*ro[...,3:] if vector else ro + return ro[...,:3]*ro[...,3:4] if vector else ro def as_homochoric(self): - """Homochoric vector: (h_1, h_2, h_3).""" + """ + Represent as homochoric vector. + + Returns + ------- + h : numpy.ndarray of shape (...,3) + Homochoric vector: (h_1, h_2, h_3), |h| < 1/2*π^(2/3). + + """ return Rotation._qu2ho(self.quaternion) def as_cubochoric(self): - """Cubochoric vector: (c_1, c_2, c_3).""" + """ + Represent as cubochoric vector. + + Returns + ------- + c : numpy.ndarray of shape (...,3) + Cubochoric vector: (c_1, c_2, c_3), max(c_i) < 1/2*π^(2/3). + + """ return Rotation._qu2cu(self.quaternion) @property @@ -296,18 +338,34 @@ class Rotation: # Static constructors. The input data needs to follow the conventions, options allow to # relax the conventions. @staticmethod - def from_quaternion(quaternion, + def from_quaternion(q, accept_homomorph = False, P = -1, - acceptHomomorph = None): + acceptHomomorph = None): # old name (for compatibility) + """ + Initialize from quaternion. + Parameters + ---------- + q : numpy.ndarray of shape (...,4) + Unit quaternion in positive real hemisphere: (q_0, q_1, q_2, q_3), + |q|=1, q_0 ≥ 0. + accept_homomorph : boolean, optional + Allow homomorphic variants, i.e. q_0 < 0 (negative real hemisphere). + Defaults to False. + P : integer ∈ {-1,1}, optional + Convention used. Defaults to -1. + + """ if acceptHomomorph is not None: - accept_homomorph = acceptHomomorph - qu = np.array(quaternion,dtype=float) + accept_homomorph = acceptHomomorph # for compatibility + qu = np.array(q,dtype=float) if qu.shape[:-2:-1] != (4,): raise ValueError('Invalid shape.') + if abs(P) != 1: + raise ValueError('P ∉ {-1,1}') - if P > 0: qu[...,1:4] *= -1 # convert from P=1 to P=-1 + if P == 1: qu[...,1:4] *= -1 if accept_homomorph: qu[qu[...,0] < 0.0] *= -1 else: @@ -319,10 +377,21 @@ class Rotation: return Rotation(qu) @staticmethod - def from_Eulers(eulers, + def from_Eulers(phi, degrees = False): + """ + Initialize from Bunge-Euler angles. - eu = np.array(eulers,dtype=float) + Parameters + ---------- + phi : numpy.ndarray of shape (...,3) + Bunge-Euler angles: (φ_1, ϕ, φ_2), φ_1 ∈ [0,2π], ϕ ∈ [0,π], φ_2 ∈ [0,2π] + unless degrees == True: φ_1 ∈ [0,360], ϕ ∈ [0,180], φ_2 ∈ [0,360]. + degrees : boolean, optional + Bunge-Euler angles are given in degrees. Defaults to False. + + """ + eu = np.array(phi,dtype=float) if eu.shape[:-2:-1] != (3,): raise ValueError('Invalid shape.') @@ -337,12 +406,29 @@ class Rotation: degrees = False, normalise = False, P = -1): + """ + Initialize from Axis angle pair. + Parameters + ---------- + axis_angle : numpy.ndarray of shape (...,4) + Axis angle pair: [n_1, n_2, n_3, ω], |n| = 1 and ω ∈ [0,π] + unless degrees = True: ω ∈ [0,180]. + degrees : boolean, optional + Angle ω is given in degrees. Defaults to False. + normalize: boolean, optional + Allow |n| ≠ 1. Defaults to False. + P : integer ∈ {-1,1}, optional + Convention used. Defaults to -1. + + """ ax = np.array(axis_angle,dtype=float) if ax.shape[:-2:-1] != (4,): raise ValueError('Invalid shape.') + if abs(P) != 1: + raise ValueError('P ∉ {-1,1}') - if P > 0: ax[...,0:3] *= -1 # convert from P=1 to P=-1 + if P == 1: ax[...,0:3] *= -1 if degrees: ax[..., 3] = np.radians(ax[...,3]) if normalise: ax[...,0:3] /= np.linalg.norm(ax[...,0:3],axis=-1) if np.any(ax[...,3] < 0.0) or np.any(ax[...,3] > np.pi): @@ -356,7 +442,19 @@ class Rotation: def from_basis(basis, orthonormal = True, reciprocal = False): + """ + Initialize from lattice basis vectors. + Parameters + ---------- + basis : numpy.ndarray of shape (...,3,3) + Three lattice basis vectors in three dimensions. + orthonormal : boolean, optional + Basis is strictly orthonormal, i.e. is free of stretch components. Defaults to True. + reciprocal : boolean, optional + Basis vectors are given in reciprocal (instead of real) space. Defaults to False. + + """ om = np.array(basis,dtype=float) if om.shape[:-3:-1] != (3,3): raise ValueError('Invalid shape.') @@ -377,20 +475,43 @@ class Rotation: return Rotation(Rotation._om2qu(om)) @staticmethod - def from_matrix(om): + def from_matrix(R): + """ + Initialize from rotation matrix. - return Rotation.from_basis(om) + Parameters + ---------- + R : numpy.ndarray of shape (...,3,3) + Rotation matrix: det(R) = 1, R.T∙R=I. + + """ + return Rotation.from_basis(R) @staticmethod - def from_Rodrigues(rodrigues, + def from_Rodrigues(rho, normalise = False, P = -1): + """ + Initialize from Rodrigues-Frank vector. - ro = np.array(rodrigues,dtype=float) + Parameters + ---------- + rho : numpy.ndarray of shape (...,4) + Rodrigues-Frank vector (angle separated from axis). + (n_1, n_2, n_3, tan(ω/2)), |n| = 1 and ω ∈ [0,π]. + normalize : boolean, optional + Allow |n| ≠ 1. Defaults to False. + P : integer ∈ {-1,1}, optional + Convention used. Defaults to -1. + + """ + ro = np.array(rho,dtype=float) if ro.shape[:-2:-1] != (4,): raise ValueError('Invalid shape.') + if abs(P) != 1: + raise ValueError('P ∉ {-1,1}') - if P > 0: ro[...,0:3] *= -1 # convert from P=1 to P=-1 + if P == 1: ro[...,0:3] *= -1 if normalise: ro[...,0:3] /= np.linalg.norm(ro[...,0:3],axis=-1) if np.any(ro[...,3] < 0.0): raise ValueError('Rodrigues vector rotation angle not positive.') @@ -400,14 +521,26 @@ class Rotation: return Rotation(Rotation._ro2qu(ro)) @staticmethod - def from_homochoric(homochoric, + def from_homochoric(h, P = -1): + """ + Initialize from homochoric vector. - ho = np.array(homochoric,dtype=float) + Parameters + ---------- + h : numpy.ndarray of shape (...,3) + Homochoric vector: (h_1, h_2, h_3), |h| < (3/4*π)^(1/3). + P : integer ∈ {-1,1}, optional + Convention used. Defaults to -1. + + """ + ho = np.array(h,dtype=float) if ho.shape[:-2:-1] != (3,): raise ValueError('Invalid shape.') + if abs(P) != 1: + raise ValueError('P ∉ {-1,1}') - if P > 0: ho *= -1 # convert from P=1 to P=-1 + if P == 1: ho *= -1 if np.any(np.linalg.norm(ho,axis=-1) >_R1+1e-9): raise ValueError('Homochoric coordinate outside of the sphere.') @@ -415,18 +548,30 @@ class Rotation: return Rotation(Rotation._ho2qu(ho)) @staticmethod - def from_cubochoric(cubochoric, - P = -1): + def from_cubochoric(c, + P = -1): + """ + Initialize from cubochoric vector. - cu = np.array(cubochoric,dtype=float) + Parameters + ---------- + c : numpy.ndarray of shape (...,3) + Cubochoric vector: (c_1, c_2, c_3), max(c_i) < 1/2*π^(2/3). + P : integer ∈ {-1,1}, optional + Convention used. Defaults to -1. + + """ + cu = np.array(c,dtype=float) if cu.shape[:-2:-1] != (3,): raise ValueError('Invalid shape.') + if abs(P) != 1: + raise ValueError('P ∉ {-1,1}') - if np.abs(np.max(cu))>np.pi**(2./3.) * 0.5+1e-9: - raise ValueError('Cubochoric coordinate outside of the cube: {} {} {}.'.format(*cu)) + if np.abs(np.max(cu)) > np.pi**(2./3.) * 0.5+1e-9: + raise ValueError('Cubochoric coordinate outside of the cube.') ho = Rotation._cu2ho(cu) - if P > 0: ho *= -1 # convert from P=1 to P=-1 + if P == 1: ho *= -1 return Rotation(Rotation._ho2qu(ho)) @@ -479,7 +624,7 @@ class Rotation: np.cos(2.0*np.pi*r[...,1])*B, np.sin(2.0*np.pi*r[...,0])*A],axis=-1) - return Rotation(q.reshape(r.shape[:-1]+(4,)) if shape is not None else q).standardize() + return Rotation(q.reshape(r.shape[:-1]+(4,)) if shape is not None else q)._standardize() # for compatibility (old names do not follow convention) @@ -493,7 +638,7 @@ class Rotation: #################################################################################################### # Code below available according to the following conditions on https://github.com/MarDiehl/3Drotations #################################################################################################### -# Copyright (c) 2017-2019, Martin Diehl/Max-Planck-Institut für Eisenforschung GmbH +# Copyright (c) 2017-2020, Martin Diehl/Max-Planck-Institut für Eisenforschung GmbH # Copyright (c) 2013-2014, Marc De Graef/Carnegie Mellon University # All rights reserved. # @@ -552,7 +697,7 @@ class Rotation: np.zeros(qu.shape[:-1]+(2,))]), np.where(np.abs(q03_s) < 1.0e-8, np.block([np.arctan2( 2.0*qu[...,1:2]*qu[...,2:3],qu[...,1:2]**2-qu[...,2:3]**2), - np.broadcast_to(np.pi,qu.shape[:-1]+(1,)), + np.broadcast_to(np.pi,qu[...,0:1].shape), np.zeros(qu.shape[:-1]+(1,))]), np.block([np.arctan2((-_P*q02+q13)*chi, (-_P*q01-q23)*chi), np.arctan2( 2.0*chi, q03_s-q12_s ), @@ -575,7 +720,7 @@ class Rotation: s = np.sign(qu[...,0:1])/np.sqrt(qu[...,1:2]**2+qu[...,2:3]**2+qu[...,3:4]**2) omega = 2.0 * np.arccos(np.clip(qu[...,0:1],-1.0,1.0)) ax = np.where(np.broadcast_to(qu[...,0:1] < 1.0e-8,qu.shape), - np.block([qu[...,1:4],np.broadcast_to(np.pi,qu.shape[:-1]+(1,))]), + np.block([qu[...,1:4],np.broadcast_to(np.pi,qu[...,0:1].shape)]), np.block([qu[...,1:4]*s,omega])) ax[np.isclose(qu[...,0],1.,rtol=0.0)] = [0.0, 0.0, 1.0, 0.0] return ax @@ -586,7 +731,7 @@ class Rotation: with np.errstate(invalid='ignore',divide='ignore'): s = np.linalg.norm(qu[...,1:4],axis=-1,keepdims=True) ro = np.where(np.broadcast_to(np.abs(qu[...,0:1]) < 1.0e-12,qu.shape), - np.block([qu[...,1:2], qu[...,2:3], qu[...,3:4], np.broadcast_to(np.inf,qu.shape[:-1]+(1,))]), + np.block([qu[...,1:2], qu[...,2:3], qu[...,3:4], np.broadcast_to(np.inf,qu[...,0:1].shape)]), np.block([qu[...,1:2]/s,qu[...,2:3]/s,qu[...,3:4]/s, np.tan(np.arccos(np.clip(qu[...,0:1],-1.0,1.0))) ]) diff --git a/python/damask/_table.py b/python/damask/_table.py index d180e5914..435b7afa9 100644 --- a/python/damask/_table.py +++ b/python/damask/_table.py @@ -4,6 +4,7 @@ import pandas as pd import numpy as np from . import version +from . import util class Table: """Store spreadsheet-like data.""" @@ -24,7 +25,7 @@ class Table: """ self.comments = [] if comments is None else [c for c in comments] self.data = pd.DataFrame(data=data) - self.shapes = shapes + self.shapes = { k:(v,) if isinstance(v,(np.int,int)) else v for k,v in shapes.items() } self._label_condensed() @@ -33,7 +34,7 @@ class Table: labels = [] for label,shape in self.shapes.items(): size = int(np.prod(shape)) - labels += ['{}{}'.format('' if size == 1 else '{}_'.format(i+1),label) for i in range(size)] + labels += [('' if size == 1 else f'{i+1}_')+label for i in range(size)] self.data.columns = labels @@ -47,8 +48,7 @@ class Table: def _add_comment(self,label,shape,info): if info is not None: - c = '{}{}: {}'.format(label,' '+str(shape) if np.prod(shape,dtype=int) > 1 else '',info) - self.comments.append(c) + self.comments.append(f'{label}{" "+str(shape) if np.prod(shape,dtype=int) > 1 else ""}: {info}') @staticmethod @@ -126,8 +126,6 @@ class Table: Filename or file for reading. """ - shapes = {'eu':(3,), 'pos':(2,), - 'IQ':(1,), 'CI':(1,), 'ID':(1,), 'intensity':(1,), 'fit':(1,)} try: f = open(fname) except TypeError: @@ -136,7 +134,7 @@ class Table: content = f.readlines() - comments = ['table.py:from_ang v {}'.format(version)] + comments = [f'table.py:from_ang v{version}'] for line in content: if line.startswith('#'): comments.append(line.strip()) @@ -144,8 +142,11 @@ class Table: break data = np.loadtxt(content) - for c in range(data.shape[1]-10): - shapes['n/a_{}'.format(c+1)] = (1,) + + shapes = {'eu':3, 'pos':2, 'IQ':1, 'CI':1, 'ID':1, 'intensity':1, 'fit':1} + remainder = data.shape[1]-sum(shapes.values()) + if remainder > 0: # 3.8 can do: if (remainder := data.shape[1]-sum(shapes.values())) > 0 + shapes['unknown'] = remainder return Table(data,shapes,comments) @@ -234,7 +235,6 @@ class Table: """ self.data.drop(columns=label,inplace=True) - del self.shapes[label] @@ -251,8 +251,7 @@ class Table: """ self.data.rename(columns={label_old:label_new},inplace=True) - c = '{} => {}{}'.format(label_old,label_new,'' if info is None else ': {}'.format(info)) - self.comments.append(c) + self.comments.append(f'{label_old} => {label_new}'+('' if info is None else f': {info}')) self.shapes = {(label if label != label_old else label_new):self.shapes[label] for label in self.shapes} @@ -271,7 +270,7 @@ class Table: self._label_flat() self.data.sort_values(labels,axis=0,inplace=True,ascending=ascending) self._label_condensed() - self.comments.append('sorted by [{}]'.format(', '.join(labels))) + self.comments.append(f'sorted by [{", ".join(labels)}]') def append(self,other): @@ -328,18 +327,18 @@ class Table: labels = [] for l in [x for x in self.data.columns if not (x in seen or seen.add(x))]: if self.shapes[l] == (1,): - labels.append('{}'.format(l)) + labels.append(f'{l}') elif len(self.shapes[l]) == 1: - labels += ['{}_{}'.format(i+1,l) \ + labels += [f'{i+1}_{l}' \ for i in range(self.shapes[l][0])] else: - labels += ['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[l]]),i+1,l) \ + labels += [f'{util.srepr(self.shapes[l],"x")}:{i+1}_{l}' \ for i in range(np.prod(self.shapes[l]))] if new_style: - header = ['# {}'.format(comment) for comment in self.comments] + header = [f'# {comment}' for comment in self.comments] else: - header = ['{} header'.format(len(self.comments)+1)] \ + header = [f'{len(self.comments)+1} header'] \ + self.comments \ try: diff --git a/python/damask/_test.py b/python/damask/_test.py index a46eedac8..9c4c7ad3d 100644 --- a/python/damask/_test.py +++ b/python/damask/_test.py @@ -53,7 +53,7 @@ class Test: self.dirBase = os.path.dirname(os.path.realpath(sys.modules[self.__class__.__module__].__file__)) self.parser = OptionParser(option_class=damask.extendableOption, - description = '{} (Test class version: {})'.format(self.description,damask.version), + description = f'{self.description} (Test class version: {damask.version})', usage = './test.py [options]') self.parser.add_option("-k", "--keep", action = "store_true", @@ -93,7 +93,7 @@ class Test: for variant,object in enumerate(self.variants): name = self.variantName(variant) if self.options.show: - logging.critical('{}: {}'.format(variant+1,name)) + logging.critical(f'{variant+1}: {name}') elif self.options.select is not None \ and not (name in self.options.select or str(variant+1) in self.options.select): pass @@ -106,12 +106,12 @@ class Test: self.postprocess(variant) if self.options.update: - if self.update(variant) != 0: logging.critical('update for "{}" failed.'.format(name)) + if self.update(variant) != 0: logging.critical(f'update for "{name}" failed.') elif not (self.options.accept or self.compare(variant)): # no update, do comparison return variant+1 # return culprit except Exception as e: - logging.critical('exception during variant execution: "{}"'.format(str(e))) + logging.critical(f'exception during variant execution: "{e}"') return variant+1 # return culprit return 0 @@ -124,13 +124,13 @@ class Test: try: shutil.rmtree(self.dirCurrent()) except FileNotFoundError: - logging.warning('removal of directory "{}" not possible...'.format(self.dirCurrent())) + logging.warning(f'removal of directory "{self.dirCurrent()}" not possible...') try: os.mkdir(self.dirCurrent()) return True except FileExistsError: - logging.critical('creation of directory "{}" failed.'.format(self.dirCurrent())) + logging.critical(f'creation of directory "{self.dirCurrent()}" failed.') return False def prepareAll(self): @@ -211,7 +211,7 @@ class Test: try: shutil.copy2(source,target) except FileNotFoundError: - logging.critical('error copying {} to {}'.format(source,target)) + logging.critical(f'error copying {source} to {target}') raise FileNotFoundError @@ -222,7 +222,7 @@ class Test: try: shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i])) except FileNotFoundError: - logging.critical('Reference2Current: Unable to copy file "{}"'.format(f)) + logging.critical(f'Reference2Current: Unable to copy file "{f}"') raise FileNotFoundError @@ -235,7 +235,7 @@ class Test: shutil.copy2(os.path.join(source,f),self.fileInCurrent(targetfiles[i])) except FileNotFoundError: logging.error(os.path.join(source,f)) - logging.critical('Base2Current: Unable to copy file "{}"'.format(f)) + logging.critical(f'Base2Current: Unable to copy file "{f}"') raise FileNotFoundError @@ -246,7 +246,7 @@ class Test: try: shutil.copy2(self.fileInCurrent(f),self.fileInReference(targetfiles[i])) except FileNotFoundError: - logging.critical('Current2Reference: Unable to copy file "{}"'.format(f)) + logging.critical(f'Current2Reference: Unable to copy file "{f}"') raise FileNotFoundError @@ -257,7 +257,7 @@ class Test: try: shutil.copy2(self.fileInProof(f),self.fileInCurrent(targetfiles[i])) except FileNotFoundError: - logging.critical('Proof2Current: Unable to copy file "{}"'.format(f)) + logging.critical(f'Proof2Current: Unable to copy file "{f}"') raise FileNotFoundError @@ -267,7 +267,7 @@ class Test: try: shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i])) except FileNotFoundError: - logging.critical('Current2Current: Unable to copy file "{}"'.format(f)) + logging.critical(f'Current2Current: Unable to copy file "{f}"') raise FileNotFoundError @@ -302,9 +302,7 @@ class Test: max_loc=np.argmax(abs(refArrayNonZero[curArray.nonzero()]/curArray[curArray.nonzero()]-1.)) refArrayNonZero = refArrayNonZero[curArray.nonzero()] curArray = curArray[curArray.nonzero()] - print(' ********\n * maximum relative error {} between {} and {}\n ********'.format(max_err, - refArrayNonZero[max_loc], - curArray[max_loc])) + print(f' ********\n * maximum relative error {max_err} between {refArrayNonZero[max_loc]} and {curArray[max_loc]}\n ********') return max_err else: raise Exception('mismatch in array size to compare') @@ -350,7 +348,7 @@ class Test: for i in range(dataLength): if headings0[i]['shape'] != headings1[i]['shape']: - raise Exception('shape mismatch between {} and {} '.format(headings0[i]['label'],headings1[i]['label'])) + raise Exception(f"shape mismatch between {headings0[i]['label']} and {headings1[i]['label']}") shape[i] = headings0[i]['shape'] for j in range(np.shape(shape[i])[0]): length[i] *= shape[i][j] @@ -358,9 +356,7 @@ class Test: for j in range(np.shape(normShape[i])[0]): normLength[i] *= normShape[i][j] else: - raise Exception('trying to compare {} with {} normed by {} data sets'.format(len(headings0), - len(headings1), - len(normHeadings))) + raise Exception(f'trying to compare {len(headings0)} with {len(headings1)} normed by {len(normHeadings)} data sets') table0 = damask.ASCIItable(name=file0,readonly=True) table0.head_read() @@ -372,11 +368,11 @@ class Test: key1 = ('1_' if length[i]>1 else '') + headings1[i]['label'] normKey = ('1_' if normLength[i]>1 else '') + normHeadings[i]['label'] if key0 not in table0.labels(raw = True): - raise Exception('column "{}" not found in first table...\n'.format(key0)) + raise Exception(f'column "{key0}" not found in first table...') elif key1 not in table1.labels(raw = True): - raise Exception('column "{}" not found in second table...\n'.format(key1)) + raise Exception(f'column "{key1}" not found in second table...') elif normKey not in table0.labels(raw = True): - raise Exception('column "{}" not found in first table...\n'.format(normKey)) + raise Exception(f'column "{normKey}" not found in first table...') else: column[0][i] = table0.label_index(key0) column[1][i] = table1.label_index(key1) @@ -404,9 +400,9 @@ class Test: norm[i] = [1.0 for j in range(line0-len(skipLines))] absTol[i] = True if perLine: - logging.warning('At least one norm of "{}" in first table is 0.0, using absolute tolerance'.format(headings0[i]['label'])) + logging.warning(f"At least one norm of \"{headings0[i]['label']}\" in first table is 0.0, using absolute tolerance") else: - logging.warning('Maximum norm of "{}" in first table is 0.0, using absolute tolerance'.format(headings0[i]['label'])) + logging.warning(f"Maximum norm of \"{headings0[i]['label']}\" in first table is 0.0, using absolute tolerance") line1 = 0 while table1.data_read(): # read next data line of ASCII table @@ -418,18 +414,14 @@ class Test: norm[i][line1-len(skipLines)]) line1 +=1 - if (line0 != line1): raise Exception('found {} lines in first table but {} in second table'.format(line0,line1)) + if (line0 != line1): raise Exception(f'found {line0} lines in first table but {line1} in second table') logging.info(' ********') for i in range(dataLength): if absTol[i]: - logging.info(' * maximum absolute error {} between {} and {}'.format(maxError[i], - headings0[i]['label'], - headings1[i]['label'])) + logging.info(f" * maximum absolute error {maxError[i]} between {headings0[i]['label']} and {headings1[i]['label']}") else: - logging.info(' * maximum relative error {} between {} and {}'.format(maxError[i], - headings0[i]['label'], - headings1[i]['label'])) + logging.info(f" * maximum relative error {maxError[i]} between {headings0[i]['label']} and {headings1[i]['label']}") logging.info(' ********') return maxError @@ -480,8 +472,8 @@ class Test: normedDelta = np.where(normBy>preFilter,delta/normBy,0.0) mean = np.amax(np.abs(np.mean(normedDelta,0))) std = np.amax(np.std(normedDelta,0)) - logging.info('mean: {:f}'.format(mean)) - logging.info('std: {:f}'.format(std)) + logging.info(f'mean: {mean:f}') + logging.info(f'std: {std:f}') return (mean atol + rtol*np.absolute(data[1]) - logging.info('shape of violators: {}'.format(violators.shape)) + logging.info(f'shape of violators: {violators.shape}') for j,culprits in enumerate(violators): goodguys = np.logical_not(culprits) if culprits.any(): - logging.info('{} has {}'.format(j,np.sum(culprits))) - logging.info('deviation: {}'.format(np.absolute(data[0][j]-data[1][j])[culprits])) - logging.info('data : {}'.format(np.absolute(data[1][j])[culprits])) - logging.info('deviation: {}'.format(np.absolute(data[0][j]-data[1][j])[goodguys])) - logging.info('data : {}'.format(np.absolute(data[1][j])[goodguys])) + logging.info(f'{j} has {np.sum(culprits)}') + logging.info(f'deviation: {np.absolute(data[0][j]-data[1][j])[culprits]}') + logging.info(f'data : {np.absolute(data[1][j])[culprits]}') + logging.info(f'deviation: {np.absolute(data[0][j]-data[1][j])[goodguys]}') + logging.info(f'data : {np.absolute(data[1][j])[goodguys]}') allclose = True # start optimistic for i in range(1,len(data)): @@ -588,12 +580,12 @@ class Test: if culprit == 0: count = len(self.variants) if self.options.select is None else len(self.options.select) - msg = 'Test passed.' if count == 1 else 'All {} tests passed.'.format(count) + msg = 'Test passed.' if count == 1 else f'All {count} tests passed.' elif culprit == -1: msg = 'Warning: could not start test...' ret = 0 else: - msg = 'Test "{}" failed.'.format(self.variantName(culprit-1)) + msg = f'Test "{self.variantName(culprit-1)}" failed.' logging.critical('\n'.join(['*'*40,msg,'*'*40]) + '\n') return ret diff --git a/python/damask/_vtk.py b/python/damask/_vtk.py index 58b43dc1f..9a34800dc 100644 --- a/python/damask/_vtk.py +++ b/python/damask/_vtk.py @@ -1,4 +1,4 @@ -import os +from pathlib import Path import pandas as pd import numpy as np @@ -51,9 +51,11 @@ class VTK: """ geom = vtk.vtkRectilinearGrid() geom.SetDimensions(*(grid+1)) - geom.SetXCoordinates(np_to_vtk(np.linspace(origin[0],origin[0]+size[0],grid[0]+1),deep=True)) - geom.SetYCoordinates(np_to_vtk(np.linspace(origin[1],origin[1]+size[1],grid[1]+1),deep=True)) - geom.SetZCoordinates(np_to_vtk(np.linspace(origin[2],origin[2]+size[2],grid[2]+1),deep=True)) + coord = [np_to_vtk(np.linspace(origin[i],origin[i]+size[i],grid[i]+1),deep=True) for i in [0,1,2]] + [coord[i].SetName(n) for i,n in enumerate(['x','y','z'])] + geom.SetXCoordinates(coord[0]) + geom.SetYCoordinates(coord[1]) + geom.SetZCoordinates(coord[2]) return VTK(geom) @@ -85,7 +87,7 @@ class VTK: geom = vtk.vtkUnstructuredGrid() geom.SetPoints(vtk_nodes) - geom.SetCells(eval('vtk.VTK_{}'.format(cell_type.split('_',1)[-1].upper())),cells) + geom.SetCells(eval(f'vtk.VTK_{cell_type.split("_",1)[-1].upper()}'),cells) return VTK(geom) @@ -119,17 +121,17 @@ class VTK: Parameters ---------- - fname : str + fname : str or pathlib.Path Filename for reading. Valid extensions are .vtr, .vtu, .vtp, and .vtk. dataset_type : str, optional Name of the vtk.vtkDataSet subclass when opening an .vtk file. Valid types are vtkRectilinearGrid, vtkUnstructuredGrid, and vtkPolyData. """ - ext = os.path.splitext(fname)[1] - if ext == '.vtk': + ext = Path(fname).suffix + if ext == '.vtk' or dataset_type: reader = vtk.vtkGenericDataObjectReader() - reader.SetFileName(fname) + reader.SetFileName(str(fname)) reader.Update() if dataset_type is None: raise TypeError('Dataset type for *.vtk file not given.') @@ -140,7 +142,7 @@ class VTK: elif dataset_type.lower().endswith('polydata'): geom = reader.GetPolyDataOutput() else: - raise TypeError('Unknown dataset type for vtk file {}'.format(dataset_type)) + raise TypeError(f'Unknown dataset type {dataset_type} for vtk file') else: if ext == '.vtr': reader = vtk.vtkXMLRectilinearGridReader() @@ -149,9 +151,9 @@ class VTK: elif ext == '.vtp': reader = vtk.vtkXMLPolyDataReader() else: - raise TypeError('Unknown file extension {}'.format(ext)) + raise TypeError(f'Unknown file extension {ext}') - reader.SetFileName(fname) + reader.SetFileName(str(fname)) reader.Update() geom = reader.GetOutput() @@ -164,7 +166,7 @@ class VTK: Parameters ---------- - fname : str + fname : str or pathlib.Path Filename for writing. """ @@ -176,10 +178,10 @@ class VTK: writer = vtk.vtkXMLPolyDataWriter() default_ext = writer.GetDefaultFileExtension() - name, ext = os.path.splitext(fname) + ext = Path(fname).suffix if ext and ext != '.'+default_ext: - raise ValueError('Given extension {} is not .{}'.format(ext,default_ext)) - writer.SetFileName('{}.{}'.format(name,default_ext)) + raise ValueError(f'Given extension {ext} does not match default .{default_ext}') + writer.SetFileName(str(Path(fname).with_suffix('.'+default_ext))) writer.SetCompressorTypeToZLib() writer.SetDataModeToBinary() writer.SetInputData(self.geom) @@ -214,7 +216,7 @@ class VTK: def __repr__(self): """ASCII representation of the VTK data.""" writer = vtk.vtkDataSetWriter() - writer.SetHeader('# DAMASK.VTK v{}'.format(version)) + writer.SetHeader(f'# DAMASK.VTK v{version}') writer.WriteToOutputStringOn() writer.SetInputData(self.geom) writer.Write() diff --git a/python/damask/config/material.py b/python/damask/config/material.py index a30db1242..433c70d03 100644 --- a/python/damask/config/material.py +++ b/python/damask/config/material.py @@ -1,6 +1,7 @@ import re import os +from damask import util class Section(): def __init__(self,data = {'__order__':[]},part = ''): @@ -27,7 +28,7 @@ class Section(): if multiKey not in self.parameters: self.parameters[multiKey] = [] if multiKey not in self.parameters['__order__']: self.parameters['__order__'] += [multiKey] self.parameters[multiKey] += [[item] for item in data] if isinstance(data, list) else [[data]] - + def data(self): return self.parameters @@ -42,27 +43,27 @@ class Crystallite(Section): def __init__(self,data = {'__order__':[]}): """New material.config section.""" Section.__init__(self,data) - + class Phase(Section): def __init__(self,data = {'__order__':[]}): """New material.config section.""" Section.__init__(self,data) - + class Microstructure(Section): def __init__(self,data = {'__order__':[]}): """New material.config section.""" Section.__init__(self,data) - - + + class Texture(Section): def __init__(self,data = {'__order__':[]}): """New material.config section.""" Section.__init__(self,data) - + def add_component(self,theType,properties): - + scatter = properties['scatter'] if 'scatter' in list(map(str.lower,list(properties.keys()))) else 0.0 fraction = properties['fraction'] if 'fraction' in list(map(str.lower,list(properties.keys()))) else 1.0 @@ -81,45 +82,45 @@ class Texture(Section): ) ) - + class Material(): """Read, manipulate, and write material.config files.""" def __init__(self,verbose=True): """Generates ordered list of parts.""" self.parts = [ - 'homogenization', - 'crystallite', - 'phase', - 'texture', - 'microstructure', + 'homogenization', + 'crystallite', + 'phase', + 'texture', + 'microstructure', ] - self.data = {\ - 'homogenization': {'__order__': []}, - 'microstructure': {'__order__': []}, - 'crystallite': {'__order__': []}, - 'phase': {'__order__': []}, - 'texture': {'__order__': []}, - } + self.data = { + 'homogenization': {'__order__': []}, + 'microstructure': {'__order__': []}, + 'crystallite': {'__order__': []}, + 'phase': {'__order__': []}, + 'texture': {'__order__': []}, + } self.verbose = verbose - + def __repr__(self): """Returns current data structure in material.config format.""" me = [] for part in self.parts: - if self.verbose: print('processing <{}>'.format(part)) + if self.verbose: print(f'processing <{part}>') me += ['', '#'*100, - '<{}>'.format(part), + f'<{part}>', '#'*100, ] for section in self.data[part]['__order__']: - me += ['[{}] {}'.format(section,'#'+'-'*max(0,96-len(section)))] + me += [f'[{section}] {"#"+"-"*max(0,96-len(section))}'] for key in self.data[part][section]['__order__']: if key.startswith('(') and key.endswith(')'): # multiple (key) - me += ['{}\t{}'.format(key,' '.join(values)) for values in self.data[part][section][key]] + me += [f'{key}\t{" ".join(values)}' for values in self.data[part][section][key]] else: # plain key - me += ['{}\t{}'.format(key,' '.join(map(str,self.data[part][section][key])))] + me += [f'{key}\t{util.srepr(self.data[part][section][key]," ")}'] return '\n'.join(me) + '\n' def parse(self, part=None, sections=[], content=None): @@ -158,7 +159,7 @@ class Material(): self.data[part][name_section][items[0]].append(items[1:]) else: # plain key self.data[part][name_section][items[0]] = items[1:] - + def read(self,filename=None): @@ -174,20 +175,20 @@ class Material(): recursiveRead(match.group(1) if match.group(1).startswith('/') else os.path.normpath(os.path.join(os.path.dirname(filename),match.group(1)))) return result - + c = recursiveRead(filename) for p in self.parts: self.parse(part=p, content=c) - + def write(self,filename='material.config', overwrite=False): """Write to material.config.""" i = 0 outname = filename while os.path.exists(outname) and not overwrite: i += 1 - outname = '{}_{}'.format(filename,i) + outname = f'{filename}_{i}' - if self.verbose: print('Writing material data to {}'.format(outname)) + if self.verbose: print(f'Writing material data to {outname}') with open(outname,'w') as f: f.write(str(self)) return outname @@ -196,11 +197,11 @@ class Material(): """Add Update.""" part = part.lower() section = section.lower() - if part not in self.parts: raise Exception('invalid part {}'.format(part)) + if part not in self.parts: raise Exception(f'invalid part {part}') if not isinstance(initialData, dict): initialData = initialData.data() - + if section not in self.data[part]: self.data[part]['__order__'] += [section] if section in self.data[part] and merge: for existing in self.data[part][section]['__order__']: # replace existing @@ -215,9 +216,9 @@ class Material(): self.data[part][section]['__order__'] += [new] else: self.data[part][section] = initialData - - - + + + def add_microstructure(self, section='', components={}, # dict of phase,texture, and fraction lists @@ -226,7 +227,7 @@ class Material(): microstructure = Microstructure() # make keys lower case (http://stackoverflow.com/questions/764235/dictionary-to-lowercase-in-python) components=dict((k.lower(), v) for k,v in components.items()) - + for key in ['phase','texture','fraction','crystallite']: if isinstance(components[key], list): for i, x in enumerate(components[key]): @@ -239,7 +240,7 @@ class Material(): components[key] = [components[key].lower()] except AttributeError: components[key] = [components[key]] - + for (phase,texture,fraction,crystallite) in zip(components['phase'],components['texture'], components['fraction'],components['crystallite']): microstructure.add_multiKey('constituent','phase %i\ttexture %i\tfraction %g\ncrystallite %i'%( @@ -247,19 +248,19 @@ class Material(): self.data['texture']['__order__'].index(texture)+1, fraction, self.data['crystallite']['__order__'].index(crystallite)+1)) - + self.add_section('microstructure',section,microstructure) - - def change_value(self, part=None, - section=None, - key=None, + + def change_value(self, part=None, + section=None, + key=None, value=None): - if not isinstance(value,list): + if not isinstance(value,list): if not isinstance(value,str): value = '%s'%value value = [value] - newlen = len(value) + newlen = len(value) oldval = self.data[part.lower()][section.lower()][key.lower()] oldlen = len(oldval) print('changing %s:%s:%s from %s to %s '%(part.lower(),section.lower(),key.lower(),oldval,value)) diff --git a/python/damask/grid_filters.py b/python/damask/grid_filters.py index 76ab20872..6c3f24cff 100644 --- a/python/damask/grid_filters.py +++ b/python/damask/grid_filters.py @@ -233,7 +233,7 @@ def cell_coord0_gridSizeOrigin(coord0,ordered=True): origin[_np.where(grid==1)] = 0.0 if grid.prod() != len(coord0): - raise ValueError('Data count {} does not match grid {}.'.format(len(coord0),grid)) + raise ValueError('Data count {len(coord0)} does not match grid {grid}.') start = origin + delta*.5 end = origin - delta*.5 + size @@ -384,7 +384,7 @@ def node_coord0_gridSizeOrigin(coord0,ordered=True): origin = mincorner if (grid+1).prod() != len(coord0): - raise ValueError('Data count {} does not match grid {}.'.format(len(coord0),grid)) + raise ValueError('Data count {len(coord0)} does not match grid {grid}.') atol = _np.max(size)*5e-2 if not (_np.allclose(coords[0],_np.linspace(mincorner[0],maxcorner[0],grid[0]+1),atol=atol) and \ diff --git a/python/damask/solver/_marc.py b/python/damask/solver/_marc.py index 22e9842f5..23ae5639d 100644 --- a/python/damask/solver/_marc.py +++ b/python/damask/solver/_marc.py @@ -1,7 +1,7 @@ -import os import subprocess import shlex import string +from pathlib import Path from .._environment import Environment @@ -19,28 +19,24 @@ class Marc: """ self.solver = 'Marc' - try: - self.version = int(version) - except TypeError: - self.version = -1 + self.version = version - -#-------------------------- - def libraryPath(self): + @property + def library_path(self): path_MSC = Environment().options['MSC_ROOT'] - path_lib = '{}/mentat{}/shlib/linux64'.format(path_MSC,self.version) + path_lib = Path(f'{path_MSC}/mentat{self.version}/shlib/linux64') - return path_lib if os.path.exists(path_lib) else '' + return path_lib if path_lib.is_dir() else None -#-------------------------- - def toolsPath(self): + @property + def tools_path(self): path_MSC = Environment().options['MSC_ROOT'] - path_tools = '{}/marc{}/tools'.format(path_MSC,self.version) + path_tools = Path(f'{path_MSC}/marc{self.version}/tools') - return path_tools if os.path.exists(path_tools) else '' + return path_tools if path_tools.is_dir() else None #-------------------------- @@ -53,23 +49,23 @@ class Marc: ): - damaskEnv = Environment() + env = Environment() - user = os.path.join(damaskEnv.relPath('src'),'DAMASK_marc{}.{}'.format(self.version,'f90' if compile else 'marc')) - if not os.path.isfile(user): - raise FileNotFoundError("DAMASK4Marc ({}) '{}' not found".format(('source' if compile else 'binary'),user)) + usersub = env.root_dir/Path(f'src/DAMASK_marc{self.version}').with_suffix('.f90' if compile else '.marc') + if not usersub.is_file(): + raise FileNotFoundError("DAMASK4Marc ({}) '{}' not found".format(('source' if compile else 'binary'),usersub)) # Define options [see Marc Installation and Operation Guide, pp 23] - script = 'run_damask_{}mp'.format(optimization) + script = f'run_damask_{optimization}mp' - cmd = os.path.join(self.toolsPath(),script) + \ + cmd = str(self.tools_path/Path(script)) + \ ' -jid ' + model + '_' + job + \ ' -nprocd 1 -autorst 0 -ci n -cr n -dcoup 0 -b no -v no' - if compile: cmd += ' -u ' + user + ' -save y' - else: cmd += ' -prog ' + os.path.splitext(user)[0] + if compile: cmd += ' -u ' + str(usersub) + ' -save y' + else: cmd += ' -prog ' + str(usersub.with_suffix('')) - print('job submission {} compilation: {}'.format('with' if compile else 'without',user)) + print('job submission {} compilation: {}'.format(('with' if compile else 'without'),usersub)) if logfile: log = open(logfile, 'w') print(cmd) process = subprocess.Popen(shlex.split(cmd),stdout = log,stderr = subprocess.STDOUT) diff --git a/python/damask/util.py b/python/damask/util.py index cb1d8757d..733fd0010 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -27,7 +27,7 @@ __all__=[ #################################################################################################### def srepr(arg,glue = '\n'): r""" - Join arguments as individual lines. + Join arguments with glue string. Parameters ---------- @@ -93,7 +93,7 @@ def strikeout(what): def execute(cmd, - streamIn = None, + stream_in = None, wd = './', env = None): """ @@ -103,7 +103,7 @@ def execute(cmd, ---------- cmd : str Command to be executed. - streanIn :, optional + stream_in : file object, optional Input (via pipe) for executed process. wd : str, optional Working directory of process. Defaults to ./ . @@ -119,14 +119,14 @@ def execute(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, env = myEnv) - out,error = [i for i in (process.communicate() if streamIn is None - else process.communicate(streamIn.read().encode('utf-8')))] + stdout, stderr = [i for i in (process.communicate() if stream_in is None + else process.communicate(stream_in.read().encode('utf-8')))] os.chdir(initialPath) - out = out.decode('utf-8').replace('\x08','') - error = error.decode('utf-8').replace('\x08','') + stdout = stdout.decode('utf-8').replace('\x08','') + stderr = stderr.decode('utf-8').replace('\x08','') if process.returncode != 0: - raise RuntimeError('{} failed with returncode {}'.format(cmd,process.returncode)) - return out,error + raise RuntimeError(f'{cmd} failed with returncode {process.returncode}') + return stdout, stderr def show_progress(iterable,N_iter=None,prefix='',bar_length=50): @@ -156,7 +156,7 @@ def show_progress(iterable,N_iter=None,prefix='',bar_length=50): def scale_to_coprime(v): """Scale vector to co-prime (relatively prime) integers.""" - MAX_DENOMINATOR = 1000 + MAX_DENOMINATOR = 1000000 def get_square_denominator(x): """Denominator of the square of a number.""" @@ -166,10 +166,13 @@ def scale_to_coprime(v): """Least common multiple.""" return a * b // np.gcd(a, b) - denominators = [int(get_square_denominator(i)) for i in v] - s = reduce(lcm, denominators) ** 0.5 - m = (np.array(v)*s).astype(np.int) - return m//reduce(np.gcd,m) + m = (np.array(v) * reduce(lcm, map(lambda x: int(get_square_denominator(x)),v)) ** 0.5).astype(np.int) + m = m//reduce(np.gcd,m) + + if not np.allclose(v[v.nonzero()]/m[v.nonzero()],v[v.nonzero()][0]/m[m.nonzero()][0]): + raise ValueError(f'Invalid result {m} for input {v}. Insufficient precision?') + + return m #################################################################################################### @@ -223,21 +226,20 @@ class _ProgressBar: self.start_time = datetime.datetime.now() self.last_fraction = 0.0 - sys.stderr.write('{} {} 0% ETA n/a'.format(self.prefix, '░'*self.bar_length)) + sys.stderr.write(f"{self.prefix} {'░'*self.bar_length} 0% ETA n/a") sys.stderr.flush() def update(self,iteration): fraction = (iteration+1) / self.total + filled_length = int(self.bar_length * fraction) - if int(self.bar_length * fraction) > int(self.bar_length * self.last_fraction): + if filled_length > int(self.bar_length * self.last_fraction): + bar = '█' * filled_length + '░' * (self.bar_length - filled_length) delta_time = datetime.datetime.now() - self.start_time remaining_time = (self.total - (iteration+1)) * delta_time / (iteration+1) remaining_time -= datetime.timedelta(microseconds=remaining_time.microseconds) # remove μs - - filled_length = int(self.bar_length * fraction) - bar = '█' * filled_length + '░' * (self.bar_length - filled_length) - sys.stderr.write('\r{} {} {:>4.0%} ETA {}'.format(self.prefix, bar, fraction, remaining_time)) + sys.stderr.write(f'\r{self.prefix} {bar} {fraction:>4.0%} ETA {remaining_time}') sys.stderr.flush() self.last_fraction = fraction @@ -249,7 +251,7 @@ class _ProgressBar: class bcolors: """ - ASCII Colors. + ASCII colors. https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py https://stackoverflow.com/questions/287871 diff --git a/python/tests/conftest.py b/python/tests/conftest.py index af195ad6d..2e013d02a 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -1,4 +1,4 @@ -import os +from pathlib import Path import numpy as np import pytest @@ -16,7 +16,7 @@ def update(request): @pytest.fixture def reference_dir_base(): """Directory containing reference results.""" - return os.path.join(os.path.dirname(__file__),'reference') + return Path(__file__).parent/'reference' @pytest.fixture def set_of_quaternions(): diff --git a/python/tests/reference/VTK/polyData.vtp b/python/tests/reference/VTK/polyData.vtp new file mode 100644 index 000000000..e93e90f58 --- /dev/null +++ b/python/tests/reference/VTK/polyData.vtp @@ -0,0 +1,67 @@ + + + + + + + AQAAAACAAADwAAAAagAAAA==eF5jYMAGPuyXOV4IRHvsIfQZe8u+xxZ9j1/sh/Eh9B37IjDj4f5QMLhqD6Gf2odB+Pth6iD0G3sFiLn7ofqg+j/CxOH6IfRX+xCouRYQ+6H0D/sCqH6YuRD6D1wd1B9QmsEBxgcAJsNfhw== + + + 0.7453559925 + + + 2.4494897428 + + + + + + + + + AQAAAACAAADwAAAAagAAAA==eF5jYMAGPuyXOV4IRHvsIfQZe8u+xxZ9j1/sh/Eh9B37IjDj4f5QMLhqD6Gf2odB+Pth6iD0G3sFiLn7ofqg+j/CxOH6IfRX+xCouRYQ+6H0D/sCqH6YuRD6D1wd1B9QmsEBxgcAJsNfhw== + + + 0.7453559925 + + + 2.4494897428 + + + + + + + AAAAAACAAAAAAAAA + + + AAAAAACAAAAAAAAA + + + + + AAAAAACAAAAAAAAA + + + AAAAAACAAAAAAAAA + + + + + AAAAAACAAAAAAAAA + + + AAAAAACAAAAAAAAA + + + + + AAAAAACAAAAAAAAA + + + AAAAAACAAAAAAAAA + + + + + diff --git a/python/tests/reference/VTK/rectilinearGrid.vtr b/python/tests/reference/VTK/rectilinearGrid.vtr new file mode 100644 index 000000000..3e965bd2e --- /dev/null +++ b/python/tests/reference/VTK/rectilinearGrid.vtr @@ -0,0 +1,44 @@ + + + + + + + AQAAAACAAACAHwAAywMAAA==eF51mTGKVUEQRX88izByB8byY0MDlyG4CTfgFswMDWQigw8/Ejr40E7woGkYXqLJW4LDb8qmz71VDDjvWMGhijvd0KeTr8dXn/++f/x59rwIf3j6+untw1PS34S/udez8KgP97r+///w8bwIDx/f34SHD3nU4DXxIS/CVx/2N+GrTxWfUV18PC/C132xvwlf9zV51PDck/mQF+HrfNjfhK/z2cXn273+iI/nRXj4+P4mPHzI1zqSfZEX4eu+2N+Er/uanPXl9buXn+9n5n3lRTjzvvY34cx78Pgee7ye6eN5Ec6804ecefc+NfEhL8KZd+9TE58qPqO6+HhehDPv9CFn3v189mQ+5EU48+7ns4sPefhE7ujjeRHOvNOHnHmnz6gj2Rd5Ec6804ecefc+kbtLkvcLfCb3eb/AZ3Kf90uS9+njOfN+SfI+fch93ulTEx9y5p0+7Gfe6VPFZ1QXH8+Zd+6L/cw799XFZ3ju4uM58875sJ9553x28VlzN308Z94vSd6nD7nPO/d1iI/nzDv3xX7mnfs6Ep/Tafvx8eXnl+R95UU48772N+HMe/D4Hnu8nunjeRHOvNOHnHn3PjXxIS/CmXfvUxOfKj6juvh4XoQz7/QhZ979fPZkPuRFOPPu57OLD3n4RO7o43kRzrzTh5x5p8+oI9kXeRHOvNOHnHn3PnHO3iTvK+f5fkvO9xt8Jmfeg8f32GOcs9PHc57vN8k7fciZd+9TEx9ynu/0YT/Pd/pU8RnVxcdznu/cF/t5vnNfXXyG5y4+nvN853zYz/Od89nFZz1np4/nPN9vknf6kDPv9Bl1iI/nPN+5L/bzfOe+jsTndLr/Gdh+S95XXoQz72t/E868B4/vscfrmT6eF+HMO33ImXfvUxMf8iKcefc+NfGp4jOqi4/nRTjzTh9y5t3PZ0/mQ16EM+9+Prv4kIdP5I4+nhfhzDt9yJl3+ow6kn2RF+HMO33ImXfvE/fqTfK+ct7nt+Q+v8FncuY9eHyPPca9evp4zvv8JnmnDznz7n1q4kPO+zx92M/7PH2q+Izq4uM57/PcF/t5n+e+uvgMz118POd9nvNhP+/znM8uPpE7+njO+/wmeacPOfNOn1GH+HjO+zz3xX7e57mvI/GJ6pL3lfM9rkve1/4mnHkPHr+NPca72PTxnO9xXfK+9jfhzLv3qYkPOd/j6MP+Jpx5p8/6zX2R8z2O+2J/E868r//yPY7zIed7HOfD/iaceadP5I4+nvM9rkve6UPOvNNn1CE+nvM9jvtifxPOvAf/B5cwXsg= + + + 0 + + + 1.268857754 + + + + + + + AQAAAACAAACwEwAAZQIAAA==eF511r2KFEEYRmHjjY2N9Ao0lQ79yQy8jBVjc29gL0Ezhc1maDBfU0FB3NVgNyqQuged1leZ56sqBpo5HRx4+wS13nn989l6vjzfzm45u/vk9+/NcvL17cuHJx8Lf3D/cD4XfvPq9vmj68vCH19vLwpf/3pvbedT8crjlccrj1ce77vtXBavPF55vPJ45fG+3/hN8crjlccrj1d+vHOb7NyKV368cyte+XrUVZ901YtXftxVL175Ss/f96dX+9MPpedwew6353B7DrdnvXJ71iu3Z73pTa/cnvXK7VlvetMrt2e9cnse79wmO7fildvzeOdWvOlt3FUvXrk9j7vqE+9uWQ/46qL0HG7P4fYcbs/h9qxXbs965fasN73plduzXrk9601veuX2rFduz+Od22TnVrxyex7v3Io3vY276sUrt+dxV33i3f37/vYcbs/h9hxuz+H2rFduz3rl9pynPYfbs165PeuV27NeuT3rlduz3j//22TnVrxyew6353B7DrdnvXJ71iu353uHa8jZl9JzuD2H23O4PYfbs165PeuV27Pe9KZXbs965fasN73plduzXrk9j3duk51b8crtebxzK970Nu6qF6/cnsdd9Yl3tzzdLtbfSs/h9hxuz+H2HG7PeuX2rFduz3rTm165PeuV27Pe9KZXbs965fY83rlNdm7FK7fn8c6teNPbuKtevHJ7HnfVJ97d8uJwDdn/KD2H23O4PYfbc7g965Xbs165PetNb3rl9qxXbs9605teuT3rldvzeOc22bkVr9yexzu34k1v46568crtedzVf/4LDINsdg== + + + 0.10871961483 + + + 1.1607924027 + + + + + + + AQAAAACAAAAwAAAAJwAAAA==eF5jYICAHXKtrwN37LOH0Ofsua4vLrDlug7l37M3BoPH9gCdQxK6 + + + AQAAAACAAAA4AAAAIgAAAA==eF5jYICAUDA4ag+hr9pDRB9A+U/tV4HBK6j4B3sAk7wQqg== + + + AQAAAACAAABAAAAALAAAAA==eF5jYICASSqeQLTJHkIfsr+9LReITkP5l+zB3NvXoOK37SG6HtgDANusGUo= + + + + + diff --git a/python/tests/test_Geom.py b/python/tests/test_Geom.py index 7a3823ab3..28c9d14b0 100644 --- a/python/tests/test_Geom.py +++ b/python/tests/test_Geom.py @@ -6,6 +6,7 @@ import numpy as np from damask import Geom from damask import Rotation +from damask import util def geom_equal(a,b): @@ -85,8 +86,8 @@ class TestGeom: def test_mirror(self,default,update,reference_dir,directions,reflect): modified = copy.deepcopy(default) modified.mirror(directions,reflect) - tag = 'directions={}_reflect={}'.format('-'.join(directions),reflect) - reference = os.path.join(reference_dir,'mirror_{}.geom'.format(tag)) + tag = f'directions={"-".join(directions)}_reflect={reflect}' + reference = os.path.join(reference_dir,f'mirror_{tag}.geom') if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) @@ -94,8 +95,8 @@ class TestGeom: def test_clean(self,default,update,reference_dir,stencil): modified = copy.deepcopy(default) modified.clean(stencil) - tag = 'stencil={}'.format(stencil) - reference = os.path.join(reference_dir,'clean_{}.geom'.format(tag)) + tag = f'stencil={stencil}' + reference = os.path.join(reference_dir,f'clean_{tag}.geom') if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) @@ -111,8 +112,8 @@ class TestGeom: def test_scale(self,default,update,reference_dir,grid): modified = copy.deepcopy(default) modified.scale(grid) - tag = 'grid={}'.format('-'.join([str(x) for x in grid])) - reference = os.path.join(reference_dir,'scale_{}.geom'.format(tag)) + tag = f'grid={util.srepr(grid,"-")}' + reference = os.path.join(reference_dir,f'scale_{tag}.geom') if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) @@ -150,8 +151,8 @@ class TestGeom: def test_rotate(self,default,update,reference_dir,Eulers): modified = copy.deepcopy(default) modified.rotate(Rotation.from_Eulers(Eulers,degrees=True)) - tag = 'Eulers={}-{}-{}'.format(*Eulers) - reference = os.path.join(reference_dir,'rotate_{}.geom'.format(tag)) + tag = f'Eulers={util.srepr(Eulers,"-")}' + reference = os.path.join(reference_dir,f'rotate_{tag}.geom') if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) diff --git a/python/tests/test_Lattice.py b/python/tests/test_Lattice.py index aa201c645..93fc56445 100644 --- a/python/tests/test_Lattice.py +++ b/python/tests/test_Lattice.py @@ -154,4 +154,4 @@ class TestSymmetry: def test_invalid_argument(self,function): s = Symmetry() # noqa with pytest.raises(ValueError): - eval('s.{}(np.ones(4))'.format(function)) + eval(f's.{function}(np.ones(4))') diff --git a/python/tests/test_Orientation.py b/python/tests/test_Orientation.py index 4be146ec7..1c5288923 100644 --- a/python/tests/test_Orientation.py +++ b/python/tests/test_Orientation.py @@ -80,7 +80,7 @@ class TestOrientation: @pytest.mark.parametrize('model',['Bain','KS','GT','GT_prime','NW','Pitsch']) @pytest.mark.parametrize('lattice',['fcc','bcc']) def test_relationship_reference(self,update,reference_dir,model,lattice): - reference = os.path.join(reference_dir,'{}_{}.txt'.format(lattice,model)) + reference = os.path.join(reference_dir,f'{lattice}_{model}.txt') ori = Orientation(Rotation(),lattice) eu = np.array([o.rotation.as_Eulers(degrees=True) for o in ori.related(model)]) if update: diff --git a/python/tests/test_Result.py b/python/tests/test_Result.py index 676e94d50..20c27649b 100644 --- a/python/tests/test_Result.py +++ b/python/tests/test_Result.py @@ -137,7 +137,7 @@ class TestResult: default.add_Cauchy('P','F') default.add_eigenvalue('sigma',eigenvalue) loc = {'sigma' :default.get_dataset_location('sigma'), - 'lambda':default.get_dataset_location('lambda_{}(sigma)'.format(eigenvalue))} + 'lambda':default.get_dataset_location(f'lambda_{eigenvalue}(sigma)')} in_memory = function(mechanics.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) @@ -147,7 +147,7 @@ class TestResult: default.add_Cauchy('P','F') default.add_eigenvector('sigma',eigenvalue) loc = {'sigma' :default.get_dataset_location('sigma'), - 'v(sigma)':default.get_dataset_location('v_{}(sigma)'.format(eigenvalue))} + 'v(sigma)':default.get_dataset_location(f'v_{eigenvalue}(sigma)')} in_memory = mechanics.eigenvectors(default.read_dataset(loc['sigma'],0))[:,idx] in_file = default.read_dataset(loc['v(sigma)'],0) assert np.allclose(in_memory,in_file) @@ -179,7 +179,7 @@ class TestResult: t = ['V','U'][np.random.randint(0,2)] m = np.random.random()*2.0 - 1.0 default.add_strain_tensor('F',t,m) - label = 'epsilon_{}^{}(F)'.format(t,m) + label = f'epsilon_{t}^{m}(F)' default.add_Mises(label) loc = {label :default.get_dataset_location(label), label+'_vM':default.get_dataset_location(label+'_vM')} @@ -248,7 +248,7 @@ class TestResult: t = ['V','U'][np.random.randint(0,2)] m = np.random.random()*2.0 - 1.0 default.add_strain_tensor('F',t,m) - label = 'epsilon_{}^{}(F)'.format(t,m) + label = f'epsilon_{t}^{m}(F)' loc = {'F': default.get_dataset_location('F'), label: default.get_dataset_location(label)} in_memory = mechanics.strain_tensor(default.read_dataset(loc['F'],0),t,m) @@ -302,8 +302,8 @@ class TestResult: @pytest.mark.parametrize('allowed',['off','on']) def test_rename(self,default,allowed): - F = default.read_dataset(default.get_dataset_location('F')) if allowed == 'on': + F = default.read_dataset(default.get_dataset_location('F')) default.allow_modification() default.rename('F','new_name') assert np.all(F == default.read_dataset(default.get_dataset_location('new_name'))) diff --git a/python/tests/test_Rotation.py b/python/tests/test_Rotation.py index b4166bf1b..0cf2185c0 100644 --- a/python/tests/test_Rotation.py +++ b/python/tests/test_Rotation.py @@ -97,7 +97,7 @@ def qu2ax(qu): Modified version of the original formulation, should be numerically more stable """ - if np.isclose(qu[0],1.,rtol=0.0): # set axis to [001] if the angle is 0/360 + if np.isclose(qu[0],1.,rtol=0.0): # set axis to [001] if the angle is 0/360 ax = np.array([ 0.0, 0.0, 1.0, 0.0 ]) elif qu[0] > 1.e-8: s = np.sign(qu[0])/np.sqrt(qu[1]**2+qu[2]**2+qu[3]**2) @@ -461,7 +461,7 @@ def mul(me, other): other_p = other.quaternion[1:] R = me.__class__(np.append(me_q*other_q - np.dot(me_p,other_p), me_q*other_p + other_q*me_p + _P * np.cross(me_p,other_p))) - return R.standardize() + return R._standardize() elif isinstance(other, np.ndarray): if other.shape == (3,): A = me.quaternion[0]**2.0 - np.dot(me.quaternion[1:],me.quaternion[1:]) @@ -481,9 +481,9 @@ def mul(me, other): axes = ((0, 2, 4, 6), (0, 1, 2, 3)) return np.tensordot(RRRR, other, axes) else: - raise ValueError('Can only rotate vectors, 2nd order ternsors, and 4th order tensors') + raise ValueError('Can only rotate vectors, 2nd order tensors, and 4th order tensors') else: - raise TypeError('Cannot rotate {}'.format(type(other))) + raise TypeError(f'Cannot rotate {type(other)}') class TestRotation: @@ -781,6 +781,10 @@ class TestRotation: rot = Rotation.from_basis(om,False,reciprocal=reciprocal) assert np.isclose(np.linalg.det(rot.as_matrix()),1.0) + @pytest.mark.parametrize('shape',[None,1,(4,4)]) + def test_random(self,shape): + Rotation.from_random(shape) + @pytest.mark.parametrize('function',[Rotation.from_quaternion, Rotation.from_Eulers, Rotation.from_axis_angle, @@ -793,6 +797,16 @@ class TestRotation: with pytest.raises(ValueError): function(invalid_shape) + @pytest.mark.parametrize('fr,to',[(Rotation.from_quaternion,'as_quaternion'), + (Rotation.from_axis_angle,'as_axis_angle'), + (Rotation.from_Rodrigues, 'as_Rodrigues'), + (Rotation.from_homochoric,'as_homochoric'), + (Rotation.from_cubochoric,'as_cubochoric')]) + def test_invalid_P(self,fr,to): + R = Rotation.from_random(np.random.randint(8,32,(3))) # noqa + with pytest.raises(ValueError): + fr(eval(f'R.{to}()'),P=-30) + @pytest.mark.parametrize('shape',[None,(3,),(4,2)]) def test_broadcast(self,shape): rot = Rotation.from_random(shape) @@ -859,14 +873,18 @@ class TestRotation: phi_2 = 2*np.pi - phi_1 R_1 = Rotation.from_Eulers(np.array([phi_1,0.,0.])) R_2 = Rotation.from_Eulers(np.array([0.,0.,phi_2])) - assert np.allclose(data,R_2*(R_1*data)) + assert np.allclose(data,R_2@(R_1@data)) + + def test_rotate_inverse(self): + R = Rotation.from_random() + assert np.allclose(np.eye(3),(~R@R).as_matrix()) @pytest.mark.parametrize('data',[np.random.rand(3), np.random.rand(3,3), np.random.rand(3,3,3,3)]) - def test_rotate_inverse(self,data): + def test_rotate_inverse_array(self,data): R = Rotation.from_random() - assert np.allclose(data,R.inversed()*(R*data)) + assert np.allclose(data,~R@(R@data)) @pytest.mark.parametrize('data',[np.random.rand(4), np.random.rand(3,2), @@ -883,3 +901,7 @@ class TestRotation: R = Rotation.from_random() with pytest.raises(TypeError): R*data + + def test_misorientation(self): + R = Rotation.from_random() + assert np.allclose(R.misorientation(R).as_matrix(),np.eye(3)) diff --git a/python/tests/test_VTK.py b/python/tests/test_VTK.py index 627ea4007..ef92a09f1 100644 --- a/python/tests/test_VTK.py +++ b/python/tests/test_VTK.py @@ -1,14 +1,13 @@ -import os - import pytest import numpy as np from damask import VTK +from damask import grid_filters @pytest.fixture def reference_dir(reference_dir_base): """Directory containing reference results.""" - return os.path.join(reference_dir_base,'Result') + return reference_dir_base/'VTK' class TestVTK: @@ -17,18 +16,24 @@ class TestVTK: size = np.random.random(3) + 1.0 origin = np.random.random(3) v = VTK.from_rectilinearGrid(grid,size,origin) - s = v.__repr__() - v.write(os.path.join(tmp_path,'rectilinearGrid')) - v = VTK.from_file(os.path.join(tmp_path,'rectilinearGrid.vtr')) - assert(s == v.__repr__()) + string = v.__repr__() + v.write(tmp_path/'rectilinearGrid') + vtr = VTK.from_file(tmp_path/'rectilinearGrid.vtr') + with open(tmp_path/'rectilinearGrid.vtk','w') as f: + f.write(string) + vtk = VTK.from_file(tmp_path/'rectilinearGrid.vtk','VTK_rectilinearGrid') + assert(string == vtr.__repr__() == vtk.__repr__()) def test_polyData(self,tmp_path): points = np.random.rand(3,100) v = VTK.from_polyData(points) - s = v.__repr__() - v.write(os.path.join(tmp_path,'polyData')) - v = VTK.from_file(os.path.join(tmp_path,'polyData.vtp')) - assert(s == v.__repr__()) + string = v.__repr__() + v.write(tmp_path/'polyData') + vtp = VTK.from_file(tmp_path/'polyData.vtp') + with open(tmp_path/'polyData.vtk','w') as f: + f.write(string) + vtk = VTK.from_file(tmp_path/'polyData.vtk','polyData') + assert(string == vtp.__repr__() == vtk.__repr__()) @pytest.mark.parametrize('cell_type,n',[ ('VTK_hexahedron',8), @@ -41,7 +46,43 @@ class TestVTK: nodes = np.random.rand(n,3) connectivity = np.random.choice(np.arange(n),n,False).reshape(-1,n) v = VTK.from_unstructuredGrid(nodes,connectivity,cell_type) - s = v.__repr__() - v.write(os.path.join(tmp_path,'unstructuredGrid')) - v = VTK.from_file(os.path.join(tmp_path,'unstructuredGrid.vtu')) - assert(s == v.__repr__()) + string = v.__repr__() + v.write(tmp_path/'unstructuredGrid') + vtu = VTK.from_file(tmp_path/'unstructuredGrid.vtu') + with open(tmp_path/'unstructuredGrid.vtk','w') as f: + f.write(string) + vtk = VTK.from_file(tmp_path/'unstructuredGrid.vtk','unstructuredgrid') + assert(string == vtu.__repr__() == vtk.__repr__()) + + @pytest.mark.parametrize('name,dataset_type',[('this_file_does_not_exist.vtk',None), + ('this_file_does_not_exist.vtk','vtk'), + ('this_file_does_not_exist.vtx', None)]) + def test_invalid_dataset_type(self,dataset_type,name): + with pytest.raises(TypeError): + VTK.from_file('this_file_does_not_exist.vtk',dataset_type) + + + def test_compare_reference_polyData(self,update,reference_dir,tmp_path): + points=np.dstack((np.linspace(0.,1.,10),np.linspace(0.,2.,10),np.linspace(-1.,1.,10))).squeeze() + polyData = VTK.from_polyData(points) + polyData.add(points,'coordinates') + if update: + polyData.write(reference_dir/'polyData') + else: + reference = VTK.from_file(reference_dir/'polyData.vtp') + assert polyData.__repr__() == reference.__repr__() + + def test_compare_reference_rectilinearGrid(self,update,reference_dir,tmp_path): + grid = np.array([5,6,7],int) + size = np.array([.6,1.,.5]) + rectilinearGrid = VTK.from_rectilinearGrid(grid,size) + c = grid_filters.cell_coord0(grid,size).reshape(-1,3,order='F') + n = grid_filters.node_coord0(grid,size).reshape(-1,3,order='F') + rectilinearGrid.add(c,'cell') + rectilinearGrid.add(n,'node') + if update: + rectilinearGrid.write(reference_dir/'rectilinearGrid') + else: + reference = VTK.from_file(reference_dir/'rectilinearGrid.vtr') + assert rectilinearGrid.__repr__() == reference.__repr__() + diff --git a/python/tests/test_grid_filters.py b/python/tests/test_grid_filters.py index 8a343e26b..3acb554a7 100644 --- a/python/tests/test_grid_filters.py +++ b/python/tests/test_grid_filters.py @@ -30,8 +30,8 @@ class TestGridFilters: grid = np.random.randint(8,32,(3)) size = np.random.random(3) origin = np.random.random(3) - coord0 = eval('grid_filters.{}_coord0(grid,size,origin)'.format(mode)) # noqa - _grid,_size,_origin = eval('grid_filters.{}_coord0_gridSizeOrigin(coord0.reshape(-1,3,order="F"))'.format(mode)) + coord0 = eval(f'grid_filters.{mode}_coord0(grid,size,origin)') # noqa + _grid,_size,_origin = eval(f'grid_filters.{mode}_coord0_gridSizeOrigin(coord0.reshape(-1,3,order="F"))') assert np.allclose(grid,_grid) and np.allclose(size,_size) and np.allclose(origin,_origin) def test_displacement_fluct_equivalence(self): @@ -67,8 +67,8 @@ class TestGridFilters: origin= np.random.random(3) size = np.random.random(3) # noqa grid = np.random.randint(8,32,(3)) - shifted = eval('grid_filters.{}_coord0(grid,size,origin)'.format(mode)) - unshifted = eval('grid_filters.{}_coord0(grid,size)'.format(mode)) + shifted = eval(f'grid_filters.{mode}_coord0(grid,size,origin)') + unshifted = eval(f'grid_filters.{mode}_coord0(grid,size)') if mode == 'cell': assert np.allclose(shifted,unshifted+np.broadcast_to(origin,tuple(grid) +(3,))) elif mode == 'node': diff --git a/python/tests/test_util.py b/python/tests/test_util.py new file mode 100644 index 000000000..c786e0de1 --- /dev/null +++ b/python/tests/test_util.py @@ -0,0 +1,33 @@ +import pytest +import numpy as np +from damask import util + + +class TestUtil: + + def test_execute_direct(self): + out,err = util.execute('echo test') + assert out=='test\n' and err=='' + + def test_execute_env(self): + out,err = util.execute('sh -c "echo $test_for_execute"',env={'test_for_execute':'test'}) + assert out=='test\n' and err=='' + + def test_croak(self): + util.croak('Burp!') + + @pytest.mark.parametrize('input,output', + [ + ([2,0],[1,0]), + ([0.5,0.5],[1,1]), + ([1./2.,1./3.],[3,2]), + ([2./3.,1./2.,1./3.],[4,3,2]), + ]) + + def test_scale2coprime(self,input,output): + assert np.allclose(util.scale_to_coprime(np.array(input)), + np.array(output).astype(int)) + + def test_lackofprecision(self): + with pytest.raises(ValueError): + util.scale_to_coprime(np.array([1/3333,1,1])) diff --git a/src/CPFEM.f90 b/src/CPFEM.f90 index 7791d2b12..d285a1e01 100644 --- a/src/CPFEM.f90 +++ b/src/CPFEM.f90 @@ -35,29 +35,15 @@ module CPFEM CPFEM_dcsdE !< Cauchy stress tangent real(pReal), dimension (:,:,:,:), allocatable, private :: & CPFEM_dcsdE_knownGood !< known good tangent - integer(pInt), public :: & - cycleCounter = 0_pInt, & !< needs description - theInc = -1_pInt, & !< needs description - lastLovl = 0_pInt !< lovl in previous call to marc hypela2 - real(pReal), public :: & - theTime = 0.0_pReal, & !< needs description - theDelta = 0.0_pReal - logical, public :: & - outdatedFFN1 = .false., & !< needs description - lastIncConverged = .false., & !< needs description - outdatedByNewInc = .false. !< needs description - logical, public, protected :: & - CPFEM_init_done = .false. !< remember whether init has been done already - logical, private :: & - CPFEM_calc_done = .false. !< remember whether first ip has already calced the results + integer(pInt), public :: & + cycleCounter = 0_pInt !< needs description integer(pInt), parameter, public :: & - CPFEM_COLLECT = 2_pInt**0_pInt, & - CPFEM_CALCRESULTS = 2_pInt**1_pInt, & - CPFEM_AGERESULTS = 2_pInt**2_pInt, & - CPFEM_BACKUPJACOBIAN = 2_pInt**3_pInt, & - CPFEM_RESTOREJACOBIAN = 2_pInt**4_pInt + CPFEM_CALCRESULTS = 2_pInt**0_pInt, & + CPFEM_AGERESULTS = 2_pInt**1_pInt, & + CPFEM_BACKUPJACOBIAN = 2_pInt**2_pInt, & + CPFEM_RESTOREJACOBIAN = 2_pInt**3_pInt public :: & CPFEM_general, & @@ -68,14 +54,10 @@ contains !-------------------------------------------------------------------------------------------------- -!> @brief call (thread safe) all module initializations +!> @brief call all module initializations !-------------------------------------------------------------------------------------------------- -subroutine CPFEM_initAll(el,ip) +subroutine CPFEM_initAll - integer(pInt), intent(in) :: el, & !< FE el number - ip !< FE integration point number - - CPFEM_init_done = .true. call DAMASK_interface_init call prec_init call IO_init @@ -88,7 +70,7 @@ subroutine CPFEM_initAll(el,ip) call YAML_init call HDF5_utilities_init call results_init(.false.) - call discretization_marc_init(ip, el) + call discretization_marc_init call lattice_init call material_init(.false.) call constitutive_init @@ -124,7 +106,7 @@ end subroutine CPFEM_init !-------------------------------------------------------------------------------------------------- !> @brief perform initialization at first call, update variables and call the actual material model !-------------------------------------------------------------------------------------------------- -subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt, elFE, ip, cauchyStress, jacobian) +subroutine CPFEM_general(mode, ffn, ffn1, temperature_inp, dt, elFE, ip, cauchyStress, jacobian) integer(pInt), intent(in) :: elFE, & !< FE element number ip !< integration point number @@ -133,24 +115,21 @@ subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt ffn1 !< deformation gradient for t=t1 integer(pInt), intent(in) :: mode !< computation mode 1: regular computation plus aging of results real(pReal), intent(in) :: temperature_inp !< temperature - logical, intent(in) :: parallelExecution !< flag indicating parallel computation of requested IPs real(pReal), dimension(6), intent(out) :: cauchyStress !< stress as 6 vector real(pReal), dimension(6,6), intent(out) :: jacobian !< jacobian as 66 tensor (Consistent tangent dcs/dE) real(pReal) J_inverse, & ! inverse of Jacobian rnd - real(pReal), dimension (3,3) :: Kirchhoff, & ! Piola-Kirchhoff stress - cauchyStress33 ! stress vector + real(pReal), dimension (3,3) :: Kirchhoff ! Piola-Kirchhoff stress real(pReal), dimension (3,3,3,3) :: H_sym, & - H, & - jacobian3333 ! jacobian in Matrix notation + H integer(pInt) elCP, & ! crystal plasticity element number i, j, k, l, m, n, ph, homog, mySource logical updateJaco ! flag indicating if Jacobian has to be updated - real(pReal), parameter :: ODD_STRESS = 1e15_pReal, & !< return value for stress in case of ping pong dummy cycle - ODD_JACOBIAN = 1e50_pReal !< return value for jacobian in case of ping pong dummy cycle + real(pReal), parameter :: ODD_STRESS = 1e15_pReal, & !< return value for stress if terminallyIll + ODD_JACOBIAN = 1e50_pReal !< return value for jacobian if terminallyIll elCP = mesh_FEM2DAMASK_elem(elFE) @@ -159,9 +138,6 @@ subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt write(6,'(/,a)') '#############################################' write(6,'(a1,a22,1x,i8,a13)') '#','element', elCP, '#' write(6,'(a1,a22,1x,i8,a13)') '#','ip', ip, '#' - write(6,'(a1,a22,1x,f15.7,a6)') '#','theTime', theTime, '#' - write(6,'(a1,a22,1x,f15.7,a6)') '#','theDelta', theDelta, '#' - write(6,'(a1,a22,1x,i8,a13)') '#','theInc', theInc, '#' write(6,'(a1,a22,1x,i8,a13)') '#','cycleCounter', cycleCounter, '#' write(6,'(a1,a22,1x,i8,a13)') '#','computationMode',mode, '#' if (terminallyIll) & @@ -174,13 +150,8 @@ subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt if (iand(mode, CPFEM_RESTOREJACOBIAN) /= 0_pInt) & CPFEM_dcsde = CPFEM_dcsde_knownGood - !*** age results if (iand(mode, CPFEM_AGERESULTS) /= 0_pInt) call CPFEM_forward - - !*** collection of FEM input with returning of randomize odd stress and jacobian - !* If no parallel execution is required, there is no need to collect FEM input - if (.not. parallelExecution) then chosenThermal1: select case (thermal_type(material_homogenizationAt(elCP))) case (THERMAL_conduction_ID) chosenThermal1 temperature(material_homogenizationAt(elCP))%p(thermalMapping(material_homogenizationAt(elCP))%p(ip,elCP)) = & @@ -189,64 +160,22 @@ subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt materialpoint_F0(1:3,1:3,ip,elCP) = ffn materialpoint_F(1:3,1:3,ip,elCP) = ffn1 - elseif (iand(mode, CPFEM_COLLECT) /= 0_pInt) then - call random_number(rnd) - if (rnd < 0.5_pReal) rnd = rnd - 1.0_pReal - CPFEM_cs(1:6,ip,elCP) = rnd * ODD_STRESS - CPFEM_dcsde(1:6,1:6,ip,elCP) = ODD_JACOBIAN * math_identity2nd(6) - chosenThermal2: select case (thermal_type(material_homogenizationAt(elCP))) - case (THERMAL_conduction_ID) chosenThermal2 - temperature(material_homogenizationAt(elCP))%p(thermalMapping(material_homogenizationAt(elCP))%p(ip,elCP)) = & - temperature_inp - end select chosenThermal2 - materialpoint_F0(1:3,1:3,ip,elCP) = ffn - materialpoint_F(1:3,1:3,ip,elCP) = ffn1 - CPFEM_calc_done = .false. - endif - - - !*** calculation of stress and jacobian if (iand(mode, CPFEM_CALCRESULTS) /= 0_pInt) then - !*** deformation gradient outdated or any actual deformation gradient differs more than relevantStrain from the stored one - validCalculation: if (terminallyIll & - .or. outdatedFFN1 & - .or. any(abs(ffn1 - materialpoint_F(1:3,1:3,ip,elCP)) > defgradTolerance)) then - if (any(abs(ffn1 - materialpoint_F(1:3,1:3,ip,elCP)) > defgradTolerance)) then - if (iand(debug_level(debug_CPFEM), debug_levelBasic) /= 0_pInt) then - write(6,'(a,1x,i8,1x,i2)') '<< CPFEM >> OUTDATED at elFE ip',elFE,ip - write(6,'(a,/,3(12x,3(f10.6,1x),/))') '<< CPFEM >> FFN1 old:',& - transpose(materialpoint_F(1:3,1:3,ip,elCP)) - write(6,'(a,/,3(12x,3(f10.6,1x),/))') '<< CPFEM >> FFN1 now:',transpose(ffn1) - endif - outdatedFFN1 = .true. - endif + validCalculation: if (terminallyIll) then call random_number(rnd) if (rnd < 0.5_pReal) rnd = rnd - 1.0_pReal CPFEM_cs(1:6,ip,elCP) = ODD_STRESS * rnd CPFEM_dcsde(1:6,1:6,ip,elCP) = ODD_JACOBIAN * math_identity2nd(6) - !*** deformation gradient is not outdated else validCalculation updateJaco = mod(cycleCounter,iJacoStiffness) == 0 - !* no parallel computation, so we use just one single elFE and ip for computation - if (.not. parallelExecution) then - FEsolving_execElem = elCP - FEsolving_execIP = ip - if (iand(debug_level(debug_CPFEM), debug_levelExtensive) /= 0_pInt) & - write(6,'(a,i8,1x,i2)') '<< CPFEM >> calculation for elFE ip ',elFE,ip - call materialpoint_stressAndItsTangent(updateJaco, dt) + FEsolving_execElem = elCP + FEsolving_execIP = ip + if (iand(debug_level(debug_CPFEM), debug_levelExtensive) /= 0_pInt) & + write(6,'(a,i8,1x,i2)') '<< CPFEM >> calculation for elFE ip ',elFE,ip + call materialpoint_stressAndItsTangent(updateJaco, dt) - !* parallel computation and calulation not yet done - elseif (.not. CPFEM_calc_done) then - if (iand(debug_level(debug_CPFEM), debug_levelExtensive) /= 0_pInt) & - write(6,'(a,i8,a,i8)') '<< CPFEM >> calculation for elements ',FEsolving_execElem(1),& - ' to ',FEsolving_execElem(2) - call materialpoint_stressAndItsTangent(updateJaco, dt) - CPFEM_calc_done = .true. - endif - - !* map stress and stiffness (or return odd values if terminally ill) terminalIllness: if (terminallyIll) then call random_number(rnd) @@ -256,7 +185,7 @@ subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt else terminalIllness - ! translate from P to CS + ! translate from P to sigma Kirchhoff = matmul(materialpoint_P(1:3,1:3,ip,elCP), transpose(materialpoint_F(1:3,1:3,ip,elCP))) J_inverse = 1.0_pReal / math_det33(materialpoint_F(1:3,1:3,ip,elCP)) CPFEM_cs(1:6,ip,elCP) = math_sym33to6(J_inverse * Kirchhoff,weighted=.false.) @@ -280,7 +209,6 @@ subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt endif terminalIllness endif validCalculation - !* report stress and stiffness if ((iand(debug_level(debug_CPFEM), debug_levelExtensive) /= 0_pInt) & .and. ((debug_e == elCP .and. debug_i == ip) & .or. .not. iand(debug_level(debug_CPFEM), debug_levelSelective) /= 0_pInt)) then @@ -293,35 +221,11 @@ subroutine CPFEM_general(mode, parallelExecution, ffn, ffn1, temperature_inp, dt endif - !*** warn if stiffness close to zero if (all(abs(CPFEM_dcsdE(1:6,1:6,ip,elCP)) < 1e-10_pReal)) call IO_warning(601,elCP,ip) - !*** copy to output if using commercial FEM solver cauchyStress = CPFEM_cs (1:6, ip,elCP) jacobian = CPFEM_dcsdE(1:6,1:6,ip,elCP) - - !*** remember extreme values of stress ... - cauchyStress33 = math_6toSym33(CPFEM_cs(1:6,ip,elCP),weighted=.false.) - if (maxval(cauchyStress33) > debug_stressMax) then - debug_stressMaxLocation = [elCP, ip] - debug_stressMax = maxval(cauchyStress33) - endif - if (minval(cauchyStress33) < debug_stressMin) then - debug_stressMinLocation = [elCP, ip] - debug_stressMin = minval(cauchyStress33) - endif - !*** ... and Jacobian - jacobian3333 = math_66toSym3333(CPFEM_dcsdE(1:6,1:6,ip,elCP),weighted=.false.) - if (maxval(jacobian3333) > debug_jacobianMax) then - debug_jacobianMaxLocation = [elCP, ip] - debug_jacobianMax = maxval(jacobian3333) - endif - if (minval(jacobian3333) < debug_jacobianMin) then - debug_jacobianMinLocation = [elCP, ip] - debug_jacobianMin = minval(jacobian3333) - endif - end subroutine CPFEM_general diff --git a/src/DAMASK_marc.f90 b/src/DAMASK_marc.f90 index fa0711b02..efa054dbf 100644 --- a/src/DAMASK_marc.f90 +++ b/src/DAMASK_marc.f90 @@ -42,9 +42,7 @@ module DAMASK_interface logical, protected, public :: symmetricSolver character(len=*), parameter, public :: INPUTFILEEXTENSION = '.dat' - - logical, dimension(:,:), public, allocatable :: & - calcMode !< calculate or collect (ping pong scheme) + public :: & DAMASK_interface_init, & @@ -175,62 +173,62 @@ subroutine hypela2(d,g,e,de,s,t,dt,ngens,m,nn,kcus,matus,ndi,nshear,disp, & dispt,coord,ffn,frotn,strechn,eigvn,ffn1,frotn1, & strechn1,eigvn1,ncrd,itel,ndeg,ndm,nnode, & jtype,lclass,ifr,ifu) - use prec - use DAMASK_interface - use numerics - use FEsolving - use debug - use discretization_marc - use homogenization - use CPFEM + use prec + use DAMASK_interface + use numerics + use FEsolving + use debug + use discretization_marc + use homogenization + use CPFEM - implicit none -!$ include "omp_lib.h" ! the openMP function library - integer, intent(in) :: & ! according to MSC.Marc 2012 Manual D - ngens, & !< size of stress-strain law - nn, & !< integration point number - ndi, & !< number of direct components - nshear, & !< number of shear components - ncrd, & !< number of coordinates - itel, & !< dimension of F and R, either 2 or 3 - ndeg, & !< number of degrees of freedom - ndm, & !< not specified in MSC.Marc 2012 Manual D - nnode, & !< number of nodes per element - jtype, & !< element type - ifr, & !< set to 1 if R has been calculated - ifu !< set to 1 if stretch has been calculated - integer, dimension(2), intent(in) :: & ! according to MSC.Marc 2012 Manual D - m, & !< (1) user element number, (2) internal element number - matus, & !< (1) user material identification number, (2) internal material identification number - kcus, & !< (1) layer number, (2) internal layer number - lclass !< (1) element class, (2) 0: displacement, 1: low order Herrmann, 2: high order Herrmann - real(pReal), dimension(*), intent(in) :: & ! has dimension(1) according to MSC.Marc 2012 Manual D, but according to example hypela2.f dimension(*) - e, & !< total elastic strain - de, & !< increment of strain - dt !< increment of state variables - real(pReal), dimension(itel), intent(in) :: & ! according to MSC.Marc 2012 Manual D - strechn, & !< square of principal stretch ratios, lambda(i) at t=n - strechn1 !< square of principal stretch ratios, lambda(i) at t=n+1 - real(pReal), dimension(3,3), intent(in) :: & ! has dimension(itel,*) according to MSC.Marc 2012 Manual D, but we alway assume dimension(3,3) - ffn, & !< deformation gradient at t=n - ffn1 !< deformation gradient at t=n+1 - real(pReal), dimension(itel,*), intent(in) :: & ! according to MSC.Marc 2012 Manual D - frotn, & !< rotation tensor at t=n - eigvn, & !< i principal direction components for j eigenvalues at t=n - frotn1, & !< rotation tensor at t=n+1 - eigvn1 !< i principal direction components for j eigenvalues at t=n+1 - real(pReal), dimension(ndeg,*), intent(in) :: & ! according to MSC.Marc 2012 Manual D - disp, & !< incremental displacements - dispt !< displacements at t=n (at assembly, lovl=4) and displacements at t=n+1 (at stress recovery, lovl=6) - real(pReal), dimension(ncrd,*), intent(in) :: & ! according to MSC.Marc 2012 Manual D - coord !< coordinates - real(pReal), dimension(*), intent(inout) :: & ! according to MSC.Marc 2012 Manual D - t !< state variables (comes in at t=n, must be updated to have state variables at t=n+1) - real(pReal), dimension(ndi+nshear), intent(out) :: & ! has dimension(*) according to MSC.Marc 2012 Manual D, but we need to loop over it - s, & !< stress - should be updated by user - g !< change in stress due to temperature effects - real(pReal), dimension(ngens,ngens), intent(out) :: & ! according to MSC.Marc 2012 Manual D, but according to example hypela2.f dimension(ngens,*) - d !< stress-strain law to be formed + implicit none + include "omp_lib.h" ! the openMP function library + integer, intent(in) :: & ! according to MSC.Marc 2012 Manual D + ngens, & !< size of stress-strain law + nn, & !< integration point number + ndi, & !< number of direct components + nshear, & !< number of shear components + ncrd, & !< number of coordinates + itel, & !< dimension of F and R, either 2 or 3 + ndeg, & !< number of degrees of freedom + ndm, & !< not specified in MSC.Marc 2012 Manual D + nnode, & !< number of nodes per element + jtype, & !< element type + ifr, & !< set to 1 if R has been calculated + ifu !< set to 1 if stretch has been calculated + integer, dimension(2), intent(in) :: & ! according to MSC.Marc 2012 Manual D + m, & !< (1) user element number, (2) internal element number + matus, & !< (1) user material identification number, (2) internal material identification number + kcus, & !< (1) layer number, (2) internal layer number + lclass !< (1) element class, (2) 0: displacement, 1: low order Herrmann, 2: high order Herrmann + real(pReal), dimension(*), intent(in) :: & ! has dimension(1) according to MSC.Marc 2012 Manual D, but according to example hypela2.f dimension(*) + e, & !< total elastic strain + de, & !< increment of strain + dt !< increment of state variables + real(pReal), dimension(itel), intent(in) :: & ! according to MSC.Marc 2012 Manual D + strechn, & !< square of principal stretch ratios, lambda(i) at t=n + strechn1 !< square of principal stretch ratios, lambda(i) at t=n+1 + real(pReal), dimension(3,3), intent(in) :: & ! has dimension(itel,*) according to MSC.Marc 2012 Manual D, but we alway assume dimension(3,3) + ffn, & !< deformation gradient at t=n + ffn1 !< deformation gradient at t=n+1 + real(pReal), dimension(itel,*), intent(in) :: & ! according to MSC.Marc 2012 Manual D + frotn, & !< rotation tensor at t=n + eigvn, & !< i principal direction components for j eigenvalues at t=n + frotn1, & !< rotation tensor at t=n+1 + eigvn1 !< i principal direction components for j eigenvalues at t=n+1 + real(pReal), dimension(ndeg,*), intent(in) :: & ! according to MSC.Marc 2012 Manual D + disp, & !< incremental displacements + dispt !< displacements at t=n (at assembly, lovl=4) and displacements at t=n+1 (at stress recovery, lovl=6) + real(pReal), dimension(ncrd,*), intent(in) :: & ! according to MSC.Marc 2012 Manual D + coord !< coordinates + real(pReal), dimension(*), intent(inout) :: & ! according to MSC.Marc 2012 Manual D + t !< state variables (comes in at t=n, must be updated to have state variables at t=n+1) + real(pReal), dimension(ndi+nshear), intent(out) :: & ! has dimension(*) according to MSC.Marc 2012 Manual D, but we need to loop over it + s, & !< stress - should be updated by user + g !< change in stress due to temperature effects + real(pReal), dimension(ngens,ngens), intent(out) :: & ! according to MSC.Marc 2012 Manual D, but according to example hypela2.f dimension(ngens,*) + d !< stress-strain law to be formed !-------------------------------------------------------------------------------------------------- ! Marc common blocks are in fixed format so they have to be reformated to free format (f90) @@ -239,137 +237,108 @@ subroutine hypela2(d,g,e,de,s,t,dt,ngens,m,nn,kcus,matus,ndi,nshear,disp, & #include QUOTE(PASTE(./marc/include/concom,Marc4DAMASK)) ! concom is needed for inc, lovl #include QUOTE(PASTE(./marc/include/creeps,Marc4DAMASK)) ! creeps is needed for timinc (time increment) - logical :: cutBack - real(pReal), dimension(6) :: stress - real(pReal), dimension(6,6) :: ddsdde - integer :: computationMode, i, cp_en, node, CPnodeID - !$ integer(4) :: defaultNumThreadsInt !< default value set by Marc + logical :: cutBack + real(pReal), dimension(6) :: stress + real(pReal), dimension(6,6) :: ddsdde + integer :: computationMode, i, cp_en, node, CPnodeID + integer(4) :: defaultNumThreadsInt !< default value set by Marc - if (iand(debug_level(debug_MARC),debug_LEVELBASIC) /= 0) then - write(6,'(a,/,i8,i8,i2)') ' MSC.MARC information on shape of element(2), IP:', m, nn - write(6,'(a,2(i1))') ' Jacobian: ', ngens,ngens - write(6,'(a,i1)') ' Direct stress: ', ndi - write(6,'(a,i1)') ' Shear stress: ', nshear - write(6,'(a,i2)') ' DoF: ', ndeg - write(6,'(a,i2)') ' Coordinates: ', ncrd - write(6,'(a,i12)') ' Nodes: ', nnode - write(6,'(a,i1)') ' Deformation gradient: ', itel - write(6,'(/,a,/,3(3(f12.7,1x)/))',advance='no') ' Deformation gradient at t=n:', & - transpose(ffn) - write(6,'(/,a,/,3(3(f12.7,1x)/))',advance='no') ' Deformation gradient at t=n+1:', & - transpose(ffn1) - endif + integer(pInt), save :: & + theInc = -1_pInt, & !< needs description + lastLovl = 0_pInt !< lovl in previous call to marc hypela2 + real(pReal), save :: & + theTime = 0.0_pReal, & !< needs description + theDelta = 0.0_pReal + logical, save :: & + lastIncConverged = .false., & !< needs description + outdatedByNewInc = .false., & !< needs description + CPFEM_init_done = .false. !< remember whether init has been done already + + if (iand(debug_level(debug_MARC),debug_LEVELBASIC) /= 0) then + write(6,'(a,/,i8,i8,i2)') ' MSC.MARC information on shape of element(2), IP:', m, nn + write(6,'(a,2(i1))') ' Jacobian: ', ngens,ngens + write(6,'(a,i1)') ' Direct stress: ', ndi + write(6,'(a,i1)') ' Shear stress: ', nshear + write(6,'(a,i2)') ' DoF: ', ndeg + write(6,'(a,i2)') ' Coordinates: ', ncrd + write(6,'(a,i12)') ' Nodes: ', nnode + write(6,'(a,i1)') ' Deformation gradient: ', itel + write(6,'(/,a,/,3(3(f12.7,1x)/))',advance='no') ' Deformation gradient at t=n:', & + transpose(ffn) + write(6,'(/,a,/,3(3(f12.7,1x)/))',advance='no') ' Deformation gradient at t=n+1:', & + transpose(ffn1) + endif - !$ defaultNumThreadsInt = omp_get_num_threads() ! remember number of threads set by Marc - !$ call omp_set_num_threads(DAMASK_NumThreadsInt) ! set number of threads for parallel execution set by DAMASK_NUM_THREADS + defaultNumThreadsInt = omp_get_num_threads() ! remember number of threads set by Marc + call omp_set_num_threads(1) ! no openMP - if (.not. CPFEM_init_done) call CPFEM_initAll(m(1),nn) + if (.not. CPFEM_init_done) then + CPFEM_init_done = .true. + call CPFEM_initAll + endif - computationMode = 0 ! save initialization value, since it does not result in any calculation - if (lovl == 4 ) then ! jacobian requested by marc - if (timinc < theDelta .and. theInc == inc .and. lastLovl /= lovl) & ! first after cutback - computationMode = CPFEM_RESTOREJACOBIAN - elseif (lovl == 6) then ! stress requested by marc - cp_en = mesh_FEM2DAMASK_elem(m(1)) - if (cptim > theTime .or. inc /= theInc) then ! reached "convergence" - terminallyIll = .false. - cycleCounter = -1 ! first calc step increments this to cycle = 0 - if (inc == 0) then ! >> start of analysis << - lastIncConverged = .false. ! no Jacobian backup - outdatedByNewInc = .false. ! no aging of state - calcMode = .false. ! pretend last step was collection - lastLovl = lovl ! pretend that this is NOT the first after a lovl change - write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> start of analysis..! ',m(1),nn - flush(6) - else if (inc - theInc > 1) then ! >> restart of broken analysis << - lastIncConverged = .false. ! no Jacobian backup - outdatedByNewInc = .false. ! no aging of state - calcMode = .true. ! pretend last step was calculation - write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> restart of analysis..! ',m(1),nn - flush(6) - else ! >> just the next inc << - lastIncConverged = .true. ! request Jacobian backup - outdatedByNewInc = .true. ! request aging of state - calcMode = .true. ! assure last step was calculation - write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> new increment..! ',m(1),nn - flush(6) - endif - else if ( timinc < theDelta ) then ! >> cutBack << - lastIncConverged = .false. ! no Jacobian backup - outdatedByNewInc = .false. ! no aging of state - terminallyIll = .false. - cycleCounter = -1 ! first calc step increments this to cycle = 0 - calcMode = .true. ! pretend last step was calculation - write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> cutback detected..! ',m(1),nn - flush(6) - endif ! convergence treatment end + computationMode = 0 ! save initialization value, since it does not result in any calculation + if (lovl == 4 ) then ! jacobian requested by marc + if (timinc < theDelta .and. theInc == inc .and. lastLovl /= lovl) & ! first after cutback + computationMode = CPFEM_RESTOREJACOBIAN + elseif (lovl == 6) then ! stress requested by marc + computationMode = CPFEM_CALCRESULTS + cp_en = mesh_FEM2DAMASK_elem(m(1)) + if (cptim > theTime .or. inc /= theInc) then ! reached "convergence" + terminallyIll = .false. + cycleCounter = -1 ! first calc step increments this to cycle = 0 + if (inc == 0) then ! >> start of analysis << + lastIncConverged = .false. + outdatedByNewInc = .false. + lastLovl = lovl ! pretend that this is NOT the first after a lovl change + write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> start of analysis..! ',m(1),nn + else if (inc - theInc > 1) then ! >> restart of broken analysis << + lastIncConverged = .false. + outdatedByNewInc = .false. + write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> restart of analysis..! ',m(1),nn + else ! >> just the next inc << + lastIncConverged = .true. + outdatedByNewInc = .true. + write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> new increment..! ',m(1),nn + endif + else if ( timinc < theDelta ) then ! >> cutBack << + lastIncConverged = .false. + outdatedByNewInc = .false. + terminallyIll = .false. + cycleCounter = -1 ! first calc step increments this to cycle = 0 + write(6,'(a,i6,1x,i2)') '<< HYPELA2 >> cutback detected..! ',m(1),nn + endif ! convergence treatment end + flush(6) + if (lastLovl /= lovl) then + cycleCounter = cycleCounter + 1 + !mesh_cellnode = mesh_build_cellnodes() ! update cell node coordinates + !call mesh_build_ipCoordinates() ! update ip coordinates + endif + if (outdatedByNewInc) then + computationMode = ior(computationMode,CPFEM_AGERESULTS) + outdatedByNewInc = .false. + endif + if (lastIncConverged) then + computationMode = ior(computationMode,CPFEM_BACKUPJACOBIAN) + lastIncConverged = .false. + endif - if (usePingPong) then - calcMode(nn,cp_en) = .not. calcMode(nn,cp_en) ! ping pong (calc <--> collect) - if (calcMode(nn,cp_en)) then ! now --- CALC --- - computationMode = CPFEM_CALCRESULTS - if (lastLovl /= lovl) then ! first after ping pong - call debug_reset() ! resets debugging - outdatedFFN1 = .false. - cycleCounter = cycleCounter + 1 - !mesh_cellnode = mesh_build_cellnodes() ! update cell node coordinates - !call mesh_build_ipCoordinates() ! update ip coordinates - endif - if (outdatedByNewInc) then - computationMode = ior(computationMode,CPFEM_AGERESULTS) ! calc and age results - outdatedByNewInc = .false. ! reset flag - endif - else ! now --- COLLECT --- - computationMode = CPFEM_COLLECT ! plain collect - if (lastLovl /= lovl .and. & .not. terminallyIll) & - call debug_info() ! first after ping pong reports (meaningful) debugging - if (lastIncConverged) then - computationMode = ior(computationMode,CPFEM_BACKUPJACOBIAN) ! collect and backup Jacobian after convergence - lastIncConverged = .false. ! reset flag - endif - !do node = 1,theMesh%elem%nNodes - !CPnodeID = mesh_element(4+node,cp_en) - !mesh_node(1:ndeg,CPnodeID) = mesh_node0(1:ndeg,CPnodeID) + numerics_unitlength * dispt(1:ndeg,node) - !enddo - endif + theTime = cptim + theDelta = timinc + theInc = inc - else ! --- PLAIN MODE --- - computationMode = CPFEM_CALCRESULTS ! always calc - if (lastLovl /= lovl) then - if (.not. terminallyIll) & - call debug_info() ! first reports (meaningful) debugging - call debug_reset() ! and resets debugging - outdatedFFN1 = .false. - cycleCounter = cycleCounter + 1 - !mesh_cellnode = mesh_build_cellnodes() ! update cell node coordinates - !call mesh_build_ipCoordinates() ! update ip coordinates - endif - if (outdatedByNewInc) then - computationMode = ior(computationMode,CPFEM_AGERESULTS) - outdatedByNewInc = .false. ! reset flag - endif - if (lastIncConverged) then - computationMode = ior(computationMode,CPFEM_BACKUPJACOBIAN) ! backup Jacobian after convergence - lastIncConverged = .false. ! reset flag - endif - endif + endif + lastLovl = lovl - theTime = cptim ! record current starting time - theDelta = timinc ! record current time increment - theInc = inc ! record current increment number + call CPFEM_general(computationMode,ffn,ffn1,t(1),timinc,m(1),nn,stress,ddsdde) - endif - lastLovl = lovl ! record lovl + d = ddsdde(1:ngens,1:ngens) + s = stress(1:ndi+nshear) + g = 0.0_pReal + if(symmetricSolver) d = 0.5_pReal*(d+transpose(d)) - call CPFEM_general(computationMode,usePingPong,ffn,ffn1,t(1),timinc,m(1),nn,stress,ddsdde) - - d = ddsdde(1:ngens,1:ngens) - s = stress(1:ndi+nshear) - g = 0.0_pReal - if(symmetricSolver) d = 0.5_pReal*(d+transpose(d)) - - !$ call omp_set_num_threads(defaultNumThreadsInt) ! reset number of threads to stored default value + call omp_set_num_threads(defaultNumThreadsInt) ! reset number of threads to stored default value end subroutine hypela2 diff --git a/src/constitutive_plastic_dislotwin.f90 b/src/constitutive_plastic_dislotwin.f90 index 7c7d24ab8..dc40d269e 100644 --- a/src/constitutive_plastic_dislotwin.f90 +++ b/src/constitutive_plastic_dislotwin.f90 @@ -830,7 +830,7 @@ module subroutine plastic_dislotwin_results(instance,group) 'mobile dislocation density','1/m²') case('rho_dip') if(prm%sum_N_sl>0) call results_writeDataset(group,stt%rho_dip,'rho_dip',& - 'dislocation dipole density''1/m²') + 'dislocation dipole density','1/m²') case('gamma_sl') if(prm%sum_N_sl>0) call results_writeDataset(group,stt%gamma_sl,'gamma_sl',& 'plastic shear','1') diff --git a/src/crystallite.f90 b/src/crystallite.f90 index 929fec862..2f65702e7 100644 --- a/src/crystallite.f90 +++ b/src/crystallite.f90 @@ -245,7 +245,7 @@ subroutine crystallite_init enddo !$OMP END PARALLEL DO - if(any(plasticState%nonlocal) .and. .not. usePingPong) call IO_error(601) ! exit if nonlocal but no ping-pong ToDo: Why not check earlier? or in nonlocal? + !if(any(plasticState%nonlocal) .and. .not. usePingPong) call IO_error(601) crystallite_partionedFp0 = crystallite_Fp0 crystallite_partionedFi0 = crystallite_Fi0 @@ -276,9 +276,6 @@ subroutine crystallite_init write(6,'(a42,1x,i10)') 'max # of constituents/integration point: ', cMax flush(6) endif - - call debug_info - call debug_reset #endif end subroutine crystallite_init @@ -287,11 +284,9 @@ end subroutine crystallite_init !-------------------------------------------------------------------------------------------------- !> @brief calculate stress (P) !-------------------------------------------------------------------------------------------------- -function crystallite_stress(dummyArgumentToPreventInternalCompilerErrorWithGCC) +function crystallite_stress() logical, dimension(discretization_nIP,discretization_nElem) :: crystallite_stress - real(pReal), intent(in), optional :: & - dummyArgumentToPreventInternalCompilerErrorWithGCC real(pReal) :: & formerSubStep integer :: & diff --git a/src/debug.f90 b/src/debug.f90 index a0947c410..7564c3037 100644 --- a/src/debug.f90 +++ b/src/debug.f90 @@ -49,26 +49,11 @@ module debug debug_i = 1, & debug_g = 1 - integer, dimension(2), public :: & - debug_stressMaxLocation = 0, & - debug_stressMinLocation = 0, & - debug_jacobianMaxLocation = 0, & - debug_jacobianMinLocation = 0 - - - real(pReal), public :: & - debug_stressMax = -huge(1.0_pReal), & - debug_stressMin = huge(1.0_pReal), & - debug_jacobianMax = -huge(1.0_pReal), & - debug_jacobianMin = huge(1.0_pReal) - #ifdef PETSc character(len=1024), parameter, public :: & PETSCDEBUG = ' -snes_view -snes_monitor ' #endif - public :: debug_init, & - debug_reset, & - debug_info + public :: debug_init contains @@ -230,42 +215,4 @@ subroutine debug_init end subroutine debug_init - -!-------------------------------------------------------------------------------------------------- -!> @brief resets all debug values -!-------------------------------------------------------------------------------------------------- -subroutine debug_reset - - debug_stressMaxLocation = 0 - debug_stressMinLocation = 0 - debug_jacobianMaxLocation = 0 - debug_jacobianMinLocation = 0 - debug_stressMax = -huge(1.0_pReal) - debug_stressMin = huge(1.0_pReal) - debug_jacobianMax = -huge(1.0_pReal) - debug_jacobianMin = huge(1.0_pReal) - -end subroutine debug_reset - - -!-------------------------------------------------------------------------------------------------- -!> @brief writes debug statements to standard out -!-------------------------------------------------------------------------------------------------- -subroutine debug_info - - !$OMP CRITICAL (write2out) - debugOutputCPFEM: if (iand(debug_level(debug_CPFEM),debug_LEVELBASIC) /= 0 & - .and. any(debug_stressMinLocation /= 0) & - .and. any(debug_stressMaxLocation /= 0) ) then - write(6,'(2/,a,/)') ' Extreme values of returned stress and Jacobian' - write(6,'(a39)') ' value el ip' - write(6,'(a14,1x,e12.3,1x,i8,1x,i4)') ' stress min :', debug_stressMin, debug_stressMinLocation - write(6,'(a14,1x,e12.3,1x,i8,1x,i4)') ' max :', debug_stressMax, debug_stressMaxLocation - write(6,'(a14,1x,e12.3,1x,i8,1x,i4)') ' Jacobian min :', debug_jacobianMin, debug_jacobianMinLocation - write(6,'(a14,1x,e12.3,1x,i8,1x,i4,/)') ' max :', debug_jacobianMax, debug_jacobianMaxLocation - endif debugOutputCPFEM - !$OMP END CRITICAL (write2out) - -end subroutine debug_info - end module debug diff --git a/src/discretization.f90 b/src/discretization.f90 index e9a987912..e52784c6d 100644 --- a/src/discretization.f90 +++ b/src/discretization.f90 @@ -49,7 +49,7 @@ subroutine discretization_init(homogenizationAt,microstructureAt,& IPcoords0, & NodeCoords0 integer, optional, intent(in) :: & - sharedNodesBegin + sharedNodesBegin !< index of first node shared among different processes (MPI) write(6,'(/,a)') ' <<<+- discretization init -+>>>'; flush(6) diff --git a/src/homogenization.f90 b/src/homogenization.f90 index 62150899e..4fdded94e 100644 --- a/src/homogenization.f90 +++ b/src/homogenization.f90 @@ -249,7 +249,8 @@ subroutine materialpoint_stressAndItsTangent(updateJaco,dt) NiterationHomog = 0 cutBackLooping: do while (.not. terminallyIll .and. & - any(subStep(:,FEsolving_execELem(1):FEsolving_execElem(2)) > num%subStepMinHomog)) + any(subStep(FEsolving_execIP(1):FEsolving_execIP(2),& + FEsolving_execElem(1):FEsolving_execElem(2)) > num%subStepMinHomog)) !$OMP PARALLEL DO PRIVATE(myNgrains) elementLooping1: do e = FEsolving_execElem(1),FEsolving_execElem(2) diff --git a/src/marc/discretization_marc.f90 b/src/marc/discretization_marc.f90 index 0221ad693..d1026d568 100644 --- a/src/marc/discretization_marc.f90 +++ b/src/marc/discretization_marc.f90 @@ -45,10 +45,8 @@ contains !> @brief initializes the mesh by calling all necessary private routines the mesh module !! Order and routines strongly depend on type of solver !-------------------------------------------------------------------------------------------------- -subroutine discretization_marc_init(ip,el) +subroutine discretization_marc_init - integer, intent(in) :: el, ip - real(pReal), dimension(:,:), allocatable :: & node0_elem, & !< node x,y,z coordinates (initially!) node0_cell @@ -70,7 +68,7 @@ subroutine discretization_marc_init(ip,el) real(pReal), dimension(:,:,:,:),allocatable :: & unscaledNormals - write(6,'(/,a)') ' <<<+- mesh init -+>>>'; flush(6) + write(6,'(/,a)') ' <<<+- discretization_marc init -+>>>'; flush(6) mesh_unitlength = numerics_unitlength ! set physical extent of a length unit in mesh @@ -83,10 +81,6 @@ subroutine discretization_marc_init(ip,el) FEsolving_execElem = [1,nElems] FEsolving_execIP = [1,elem%nIPs] - allocate(calcMode(elem%nIPs,nElems),source=.false.) ! pretend to have collected what first call is asking (F = I) - calcMode(ip,mesh_FEM2DAMASK_elem(el)) = .true. ! first ip,el needs to be already pingponged to "calc" - - allocate(cellNodeDefinition(elem%nNodes-1)) allocate(connectivity_cell(elem%NcellNodesPerCell,elem%nIPs,nElems)) call buildCells(connectivity_cell,cellNodeDefinition,& diff --git a/src/mesh/discretization_mesh.f90 b/src/mesh/discretization_mesh.f90 index 80db37e8d..9d84b09c2 100644 --- a/src/mesh/discretization_mesh.f90 +++ b/src/mesh/discretization_mesh.f90 @@ -8,53 +8,50 @@ module discretization_mesh #include #include #include - use PETScdmplex - use PETScdmda - use PETScis + use PETScdmplex + use PETScdmda + use PETScis - use DAMASK_interface - use IO - use debug - use discretization - use numerics - use FEsolving - use FEM_quadrature - use prec - - implicit none - private - - integer, public, protected :: & - mesh_Nboundaries, & - mesh_NcpElemsGlobal + use DAMASK_interface + use IO + use debug + use discretization + use numerics + use FEsolving + use FEM_quadrature + use prec - integer :: & - mesh_NcpElems !< total number of CP elements in mesh + implicit none + private + + integer, public, protected :: & + mesh_Nboundaries, & + mesh_NcpElemsGlobal + + integer :: & + mesh_NcpElems !< total number of CP elements in mesh !!!! BEGIN DEPRECATED !!!!! - integer, public, protected :: & - mesh_maxNips !< max number of IPs in any CP element + integer, public, protected :: & + mesh_maxNips !< max number of IPs in any CP element !!!! BEGIN DEPRECATED !!!!! - integer, dimension(:,:), allocatable :: & - mesh_element !DEPRECATED + real(pReal), dimension(:,:), allocatable :: & + mesh_ipVolume, & !< volume associated with IP (initially!) + mesh_node0 !< node x,y,z coordinates (initially!) - real(pReal), dimension(:,:), allocatable :: & - mesh_ipVolume, & !< volume associated with IP (initially!) - mesh_node0 !< node x,y,z coordinates (initially!) - - real(pReal), dimension(:,:,:), allocatable :: & - mesh_ipCoordinates !< IP x,y,z coordinates (after deformation!) + real(pReal), dimension(:,:,:), allocatable :: & + mesh_ipCoordinates !< IP x,y,z coordinates (after deformation!) - DM, public :: geomMesh - - PetscInt, dimension(:), allocatable, public, protected :: & - mesh_boundaries + DM, public :: geomMesh - public :: & - discretization_mesh_init, & - mesh_FEM_build_ipVolumes, & - mesh_FEM_build_ipCoordinates + PetscInt, dimension(:), allocatable, public, protected :: & + mesh_boundaries + + public :: & + discretization_mesh_init, & + mesh_FEM_build_ipVolumes, & + mesh_FEM_build_ipCoordinates contains @@ -67,39 +64,33 @@ subroutine discretization_mesh_init(restart) logical, intent(in) :: restart - integer, dimension(1), parameter:: FE_geomtype = [1] !< geometry type of particular element type - integer, dimension(1) :: FE_Nips !< number of IPs in a specific type of element - integer, allocatable, dimension(:) :: chunkPos integer :: dimPlex, & mesh_Nnodes, & !< total number of nodes in mesh j, l - integer, parameter :: & - mesh_ElemType=1 !< Element type of the mesh (only support homogeneous meshes) PetscSF :: sf DM :: globalMesh PetscInt :: nFaceSets PetscInt, pointer, dimension(:) :: pFaceSets character(len=pStringLen), dimension(:), allocatable :: fileContent - IS :: faceSetIS + IS :: faceSetIS PetscErrorCode :: ierr + integer, dimension(:), allocatable :: & + homogenizationAt, & + microstructureAt + - write(6,'(/,a)') ' <<<+- mesh init -+>>>' - ! read in file call DMPlexCreateFromFile(PETSC_COMM_WORLD,geometryFile,PETSC_TRUE,globalMesh,ierr) CHKERRQ(ierr) - ! get spatial dimension (2 or 3?) call DMGetDimension(globalMesh,dimPlex,ierr) CHKERRQ(ierr) - write(6,*) 'dimension',dimPlex;flush(6) call DMGetStratumSize(globalMesh,'depth',dimPlex,mesh_NcpElemsGlobal,ierr) CHKERRQ(ierr) ! get number of IDs in face sets (for boundary conditions?) call DMGetLabelSize(globalMesh,'Face Sets',mesh_Nboundaries,ierr) CHKERRQ(ierr) - write(6,*) 'number of "Face Sets"',mesh_Nboundaries;flush(6) call MPI_Bcast(mesh_Nboundaries,1,MPI_INTEGER,0,PETSC_COMM_WORLD,ierr) call MPI_Bcast(mesh_NcpElemsGlobal,1,MPI_INTEGER,0,PETSC_COMM_WORLD,ierr) call MPI_Bcast(dimPlex,1,MPI_INTEGER,0,PETSC_COMM_WORLD,ierr) @@ -142,48 +133,42 @@ subroutine discretization_mesh_init(restart) enddo call DMClone(globalMesh,geomMesh,ierr) CHKERRQ(ierr) - else + else call DMPlexDistribute(globalMesh,0,sf,geomMesh,ierr) CHKERRQ(ierr) - endif + endif call DMDestroy(globalMesh,ierr); CHKERRQ(ierr) - + call DMGetStratumSize(geomMesh,'depth',dimPlex,mesh_NcpElems,ierr) CHKERRQ(ierr) call DMGetStratumSize(geomMesh,'depth',0,mesh_Nnodes,ierr) CHKERRQ(ierr) - FE_Nips(FE_geomtype(1)) = FEM_nQuadrature(dimPlex,integrationOrder) - mesh_maxNips = FE_Nips(1) - - write(6,*) 'mesh_maxNips',mesh_maxNips + mesh_maxNips = FEM_nQuadrature(dimPlex,integrationOrder) + call mesh_FEM_build_ipCoordinates(dimPlex,FEM_quadrature_points(dimPlex,integrationOrder)%p) call mesh_FEM_build_ipVolumes(dimPlex) - - allocate (mesh_element (4,mesh_NcpElems)); mesh_element = 0 - do j = 1, mesh_NcpElems - mesh_element( 1,j) = -1 ! DEPRECATED - mesh_element( 2,j) = mesh_elemType ! elem type - mesh_element( 3,j) = 1 ! homogenization - call DMGetLabelValue(geomMesh,'material',j-1,mesh_element(4,j),ierr) - CHKERRQ(ierr) - end do - if (debug_e < 1 .or. debug_e > mesh_NcpElems) & - call IO_error(602,ext_msg='element') ! selected element does not exist - if (debug_i < 1 .or. debug_i > FE_Nips(FE_geomtype(mesh_element(2,debug_e)))) & - call IO_error(602,ext_msg='IP') ! selected element does not have requested IP - - FEsolving_execElem = [ 1,mesh_NcpElems ] ! parallel loop bounds set to comprise all DAMASK elements - FEsolving_execIP = [1,FE_Nips(FE_geomtype(mesh_element(2,1)))] - + allocate(microstructureAt(mesh_NcpElems)) + allocate(homogenizationAt(mesh_NcpElems),source=1) + do j = 1, mesh_NcpElems + call DMGetLabelValue(geomMesh,'material',j-1,microstructureAt(j),ierr) + CHKERRQ(ierr) + end do + + if (debug_e < 1 .or. debug_e > mesh_NcpElems) call IO_error(602,ext_msg='element') + if (debug_i < 1 .or. debug_i > mesh_maxNips) call IO_error(602,ext_msg='IP') + + FEsolving_execElem = [1,mesh_NcpElems] ! parallel loop bounds set to comprise all DAMASK elements + FEsolving_execIP = [1,mesh_maxNips] + allocate(mesh_node0(3,mesh_Nnodes),source=0.0_pReal) - call discretization_init(mesh_element(3,:),mesh_element(4,:),& + call discretization_init(microstructureAt,homogenizationAt,& reshape(mesh_ipCoordinates,[3,mesh_maxNips*mesh_NcpElems]), & mesh_node0) - + end subroutine discretization_mesh_init @@ -191,23 +176,23 @@ end subroutine discretization_mesh_init !> @brief Calculates IP volume. Allocates global array 'mesh_ipVolume' !-------------------------------------------------------------------------------------------------- subroutine mesh_FEM_build_ipVolumes(dimPlex) - + PetscInt,intent(in):: dimPlex PetscReal :: vol PetscReal, pointer,dimension(:) :: pCent, pNorm PetscInt :: cellStart, cellEnd, cell PetscErrorCode :: ierr - + allocate(mesh_ipVolume(mesh_maxNips,mesh_NcpElems),source=0.0_pReal) - + call DMPlexGetHeightStratum(geomMesh,0,cellStart,cellEnd,ierr); CHKERRQ(ierr) allocate(pCent(dimPlex)) allocate(pNorm(dimPlex)) do cell = cellStart, cellEnd-1 - call DMPlexComputeCellGeometryFVM(geomMesh,cell,vol,pCent,pNorm,ierr) + call DMPlexComputeCellGeometryFVM(geomMesh,cell,vol,pCent,pNorm,ierr) CHKERRQ(ierr) mesh_ipVolume(:,cell+1) = vol/real(mesh_maxNips,pReal) - enddo + enddo end subroutine mesh_FEM_build_ipVolumes @@ -219,20 +204,20 @@ subroutine mesh_FEM_build_ipCoordinates(dimPlex,qPoints) PetscInt, intent(in) :: dimPlex PetscReal, intent(in) :: qPoints(mesh_maxNips*dimPlex) - + PetscReal, pointer,dimension(:) :: pV0, pCellJ, pInvcellJ PetscReal :: detJ PetscInt :: cellStart, cellEnd, cell, qPt, dirI, dirJ, qOffset PetscErrorCode :: ierr - - + + allocate(mesh_ipCoordinates(3,mesh_maxNips,mesh_NcpElems),source=0.0_pReal) - + allocate(pV0(dimPlex)) allocatE(pCellJ(dimPlex**2)) allocatE(pinvCellJ(dimPlex**2)) call DMPlexGetHeightStratum(geomMesh,0,cellStart,cellEnd,ierr); CHKERRQ(ierr) - do cell = cellStart, cellEnd-1 !< loop over all elements + do cell = cellStart, cellEnd-1 !< loop over all elements call DMPlexComputeCellGeometryAffineFEM(geomMesh,cell,pV0,pCellJ,pInvcellJ,detJ,ierr) CHKERRQ(ierr) qOffset = 0 @@ -246,7 +231,7 @@ subroutine mesh_FEM_build_ipCoordinates(dimPlex,qPoints) enddo qOffset = qOffset + dimPlex enddo - enddo + enddo end subroutine mesh_FEM_build_ipCoordinates diff --git a/src/numerics.f90 b/src/numerics.f90 index b0163aee3..77c3397de 100644 --- a/src/numerics.f90 +++ b/src/numerics.f90 @@ -28,8 +28,6 @@ module numerics numerics_unitlength = 1.0_pReal, & !< determines the physical length of one computational length unit charLength = 1.0_pReal, & !< characteristic length scale for gradient problems residualStiffness = 1.0e-6_pReal !< non-zero residual damage - logical, protected, public :: & - usePingPong = .true. !-------------------------------------------------------------------------------------------------- ! field parameters: @@ -133,8 +131,6 @@ subroutine numerics_init defgradTolerance = IO_floatValue(line,chunkPos,2) case ('ijacostiffness') iJacoStiffness = IO_intValue(line,chunkPos,2) - case ('usepingpong') - usepingpong = IO_intValue(line,chunkPos,2) > 0 case ('unitlength') numerics_unitlength = IO_floatValue(line,chunkPos,2) @@ -221,7 +217,6 @@ subroutine numerics_init ! writing parameters to output write(6,'(a24,1x,es8.1)') ' defgradTolerance: ',defgradTolerance write(6,'(a24,1x,i8)') ' iJacoStiffness: ',iJacoStiffness - write(6,'(a24,1x,L8)') ' use ping pong scheme: ',usepingpong write(6,'(a24,1x,es8.1,/)')' unitlength: ',numerics_unitlength !-------------------------------------------------------------------------------------------------- diff --git a/src/quaternions.f90 b/src/quaternions.f90 index f5f5276e8..c337bd681 100644 --- a/src/quaternions.f90 +++ b/src/quaternions.f90 @@ -101,6 +101,8 @@ module quaternions assignment(=), & conjg, aimag, & log, exp, & + abs, dot_product, & + inverse, & real contains