2019-05-30 19:05:45 +05:30
|
|
|
import os
|
2019-05-25 02:00:25 +05:30
|
|
|
from io import StringIO
|
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
import numpy as np
|
2019-05-30 19:05:45 +05:30
|
|
|
import vtk
|
|
|
|
from vtk.util import numpy_support
|
2019-05-26 15:33:21 +05:30
|
|
|
|
2019-05-28 01:30:26 +05:30
|
|
|
from . import util
|
2019-05-30 19:05:45 +05:30
|
|
|
from . import version
|
2019-05-28 01:30:26 +05:30
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
class Geom():
|
2019-05-25 02:01:34 +05:30
|
|
|
"""Geometry definition for grid solvers"""
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
def __init__(self,microstructure,size,origin=[0.0,0.0,0.0],homogenization=1,comments=[]):
|
2019-05-25 13:44:53 +05:30
|
|
|
"""New geometry definition from array of microstructures and size"""
|
2019-05-28 06:44:09 +05:30
|
|
|
self.set_size(size)
|
|
|
|
self.set_origin(origin)
|
|
|
|
self.set_microstructure(microstructure)
|
|
|
|
self.set_homogenization(homogenization)
|
|
|
|
self.set_comments(comments)
|
2019-05-26 21:18:59 +05:30
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-25 13:44:53 +05:30
|
|
|
def __repr__(self):
|
|
|
|
"""Basic information on geometry definition"""
|
2019-05-30 05:13:51 +05:30
|
|
|
return util.srepr([
|
|
|
|
'grid a b c: {}'.format(' x '.join(map(str,self.get_grid ()))),
|
|
|
|
'size x y z: {}'.format(' x '.join(map(str,self.get_size ()))),
|
|
|
|
'origin x y z: {}'.format(' x '.join(map(str,self.get_origin()))),
|
|
|
|
'homogenization: {}'.format(self.get_homogenization()),
|
|
|
|
'# microstructures: {}'.format(len(np.unique(self.microstructure))),
|
|
|
|
'max microstructure: {}'.format(np.nanmax(self.microstructure)),
|
|
|
|
])
|
2019-05-26 21:18:59 +05:30
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
def update(self,microstructure=None,size=None,origin=None,rescale=False):
|
2019-05-26 21:18:59 +05:30
|
|
|
"""Updates microstructure and size"""
|
|
|
|
grid_old = self.get_grid()
|
2019-05-26 23:58:19 +05:30
|
|
|
size_old = self.get_size()
|
2019-05-28 06:44:09 +05:30
|
|
|
origin_old = self.get_origin()
|
2019-05-26 21:18:59 +05:30
|
|
|
unique_old = len(np.unique(self.microstructure))
|
2019-05-28 06:44:09 +05:30
|
|
|
max_old = np.nanmax(self.microstructure)
|
2019-05-26 21:18:59 +05:30
|
|
|
|
|
|
|
if size is not None and rescale:
|
|
|
|
raise ValueError('Either set size explicitly or rescale automatically')
|
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
self.set_microstructure(microstructure)
|
2019-05-29 23:06:02 +05:30
|
|
|
self.set_size(self.get_grid()/grid_old*self.size if rescale else size)
|
2019-05-28 06:44:09 +05:30
|
|
|
self.set_origin(origin)
|
|
|
|
|
2019-05-28 01:30:26 +05:30
|
|
|
message = ['grid a b c: {}'.format(' x '.join(map(str,grid_old)))]
|
2019-05-26 21:18:59 +05:30
|
|
|
if np.any(grid_old != self.get_grid()):
|
2019-05-28 06:44:09 +05:30
|
|
|
message[-1] = util.delete(message[-1])
|
2019-05-28 01:30:26 +05:30
|
|
|
message.append('grid a b c: {}'.format(' x '.join(map(str,self.get_grid()))))
|
|
|
|
|
|
|
|
message.append('size x y z: {}'.format(' x '.join(map(str,size_old))))
|
2019-05-28 06:44:09 +05:30
|
|
|
if np.any(size_old != self.get_size()):
|
|
|
|
message[-1] = util.delete(message[-1])
|
|
|
|
message.append('size x y z: {}'.format(' x '.join(map(str,self.get_size()))))
|
|
|
|
|
|
|
|
message.append('origin x y z: {}'.format(' x '.join(map(str,origin_old))))
|
|
|
|
if np.any(origin_old != self.get_origin()):
|
|
|
|
message[-1] = util.delete(message[-1])
|
|
|
|
message.append('origin x y z: {}'.format(' x '.join(map(str,self.get_origin()))))
|
2019-05-28 01:30:26 +05:30
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
message.append('homogenization: {}'.format(self.get_homogenization()))
|
2019-05-28 01:30:26 +05:30
|
|
|
|
|
|
|
message.append('# microstructures: {}'.format(unique_old))
|
2019-05-26 21:18:59 +05:30
|
|
|
if unique_old != len(np.unique(self.microstructure)):
|
2019-05-28 06:44:09 +05:30
|
|
|
message[-1] = util.delete(message[-1])
|
2019-05-28 01:30:26 +05:30
|
|
|
message.append('# microstructures: {}'.format(len(np.unique(self.microstructure))))
|
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
message.append('max microstructure: {}'.format(max_old))
|
|
|
|
if max_old != np.nanmax(self.microstructure):
|
|
|
|
message[-1] = util.delete(message[-1])
|
|
|
|
message.append('max microstructure: {}'.format(np.nanmax(self.microstructure)))
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-28 12:32:29 +05:30
|
|
|
return util.return_message(message)
|
2019-05-26 21:18:59 +05:30
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
def set_comments(self,comments):
|
|
|
|
self.comments = []
|
|
|
|
self.add_comments(comments)
|
|
|
|
|
|
|
|
def add_comments(self,comments):
|
|
|
|
self.comments += [str(c) for c in comments] if isinstance(comments,list) else [str(comments)]
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-26 13:19:56 +05:30
|
|
|
def set_microstructure(self,microstructure):
|
2019-05-28 06:44:09 +05:30
|
|
|
if microstructure is not None:
|
|
|
|
if len(microstructure.shape) != 3:
|
|
|
|
raise ValueError('Invalid microstructure shape {}'.format(*microstructure.shape))
|
2019-05-30 16:59:29 +05:30
|
|
|
elif microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']:
|
2019-05-28 06:44:09 +05:30
|
|
|
raise TypeError('Invalid data type {} for microstructure'.format(microstructure.dtype))
|
|
|
|
else:
|
|
|
|
self.microstructure = np.copy(microstructure)
|
2019-05-26 13:19:56 +05:30
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
def set_size(self,size):
|
2019-05-28 06:44:09 +05:30
|
|
|
if size is not None:
|
|
|
|
if len(size) != 3 or any(np.array(size)<=0):
|
|
|
|
raise ValueError('Invalid size {}'.format(*size))
|
|
|
|
else:
|
|
|
|
self.size = np.array(size)
|
|
|
|
|
|
|
|
def set_origin(self,origin):
|
|
|
|
if origin is not None:
|
|
|
|
if len(origin) != 3:
|
|
|
|
raise ValueError('Invalid origin {}'.format(*origin))
|
|
|
|
else:
|
|
|
|
self.origin = np.array(origin)
|
|
|
|
|
|
|
|
def set_homogenization(self,homogenization):
|
|
|
|
if homogenization is not None:
|
|
|
|
if not isinstance(homogenization,int) or homogenization < 1:
|
|
|
|
raise TypeError('Invalid homogenization {}'.format(homogenization))
|
|
|
|
else:
|
|
|
|
self.homogenization = homogenization
|
|
|
|
|
|
|
|
|
2019-05-26 13:19:56 +05:30
|
|
|
def get_microstructure(self):
|
2019-05-26 23:58:19 +05:30
|
|
|
return np.copy(self.microstructure)
|
2019-05-26 13:19:56 +05:30
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
def get_size(self):
|
2019-05-26 23:58:19 +05:30
|
|
|
return np.copy(self.size)
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
def get_origin(self):
|
|
|
|
return np.copy(self.origin)
|
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
def get_grid(self):
|
|
|
|
return np.array(self.microstructure.shape)
|
|
|
|
|
|
|
|
def get_homogenization(self):
|
|
|
|
return self.homogenization
|
|
|
|
|
2019-05-27 01:02:48 +05:30
|
|
|
def get_comments(self):
|
|
|
|
return self.comments[:]
|
2019-05-26 15:33:21 +05:30
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
def get_header(self):
|
|
|
|
header = ['{} header'.format(len(self.comments)+4)] + self.comments
|
|
|
|
header.append('grid a {} b {} c {}'.format(*self.get_grid()))
|
|
|
|
header.append('size x {} y {} z {}'.format(*self.get_size()))
|
|
|
|
header.append('origin x {} y {} z {}'.format(*self.get_origin()))
|
|
|
|
header.append('homogenization {}'.format(self.get_homogenization()))
|
|
|
|
return header
|
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
@classmethod
|
|
|
|
def from_file(cls,fname):
|
2019-05-28 06:44:09 +05:30
|
|
|
"""Reads a geom file"""
|
|
|
|
with (open(fname) if isinstance(fname,str) else fname) as f:
|
|
|
|
f.seek(0)
|
|
|
|
header_length,keyword = f.readline().split()[:2]
|
|
|
|
header_length = int(header_length)
|
|
|
|
content = f.readlines()
|
|
|
|
|
|
|
|
if not keyword.startswith('head') or header_length < 3:
|
|
|
|
raise TypeError('Header length information missing or invalid')
|
2019-05-25 02:00:25 +05:30
|
|
|
|
|
|
|
comments = []
|
2019-05-28 06:44:09 +05:30
|
|
|
for i,line in enumerate(content[:header_length]):
|
|
|
|
items = line.lower().strip().split()
|
|
|
|
key = items[0] if len(items) > 0 else ''
|
|
|
|
if key == 'grid':
|
|
|
|
grid = np.array([ int(dict(zip(items[1::2],items[2::2]))[i]) for i in ['a','b','c']])
|
|
|
|
elif key == 'size':
|
|
|
|
size = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
elif key == 'origin':
|
|
|
|
origin = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
elif key == 'homogenization':
|
|
|
|
homogenization = int(items[1])
|
2019-05-25 02:00:25 +05:30
|
|
|
else:
|
2019-05-28 06:44:09 +05:30
|
|
|
comments.append(line.strip())
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
microstructure = np.empty(grid.prod()) # initialize as flat array
|
2019-05-25 11:47:51 +05:30
|
|
|
i = 0
|
2019-05-28 06:44:09 +05:30
|
|
|
for line in content[header_length:]:
|
2019-05-25 11:47:51 +05:30
|
|
|
items = line.split()
|
|
|
|
if len(items) == 3:
|
|
|
|
if items[1].lower() == 'of':
|
2019-05-25 12:06:00 +05:30
|
|
|
items = np.ones(int(items[0]))*float(items[2])
|
2019-05-25 11:47:51 +05:30
|
|
|
elif items[1].lower() == 'to':
|
|
|
|
items = np.linspace(int(items[0]),int(items[2]),
|
|
|
|
abs(int(items[2])-int(items[0]))+1,dtype=float)
|
|
|
|
else: items = list(map(float,items))
|
|
|
|
else: items = list(map(float,items))
|
|
|
|
|
|
|
|
microstructure[i:i+len(items)] = items
|
|
|
|
i += len(items)
|
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
if i != grid.prod():
|
|
|
|
raise TypeError('Invalid file: expected {} entries,found {}'.format(grid.prod(),i))
|
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
microstructure = microstructure.reshape(grid,order='F')
|
2019-05-28 06:44:09 +05:30
|
|
|
if not np.any(np.mod(microstructure.flatten(),1) != 0.0): # no float present
|
2019-05-25 11:47:51 +05:30
|
|
|
microstructure = microstructure.astype('int')
|
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
return cls(microstructure.reshape(grid),size,origin,homogenization,comments)
|
2019-05-25 02:00:25 +05:30
|
|
|
|
|
|
|
def to_file(self,fname):
|
2019-05-28 06:44:09 +05:30
|
|
|
"""Writes to file"""
|
|
|
|
header = self.get_header()
|
|
|
|
grid = self.get_grid()
|
2019-05-30 13:50:07 +05:30
|
|
|
format_string = '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) if self.microstructure.dtype == int \
|
2019-05-28 06:44:09 +05:30
|
|
|
else '%g'
|
|
|
|
np.savetxt(fname,
|
|
|
|
self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T,
|
2019-05-25 02:00:25 +05:30
|
|
|
header='\n'.join(header), fmt=format_string, comments='')
|
2019-05-30 19:05:45 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to_vtk(self,fname=None):
|
|
|
|
"""Generates vtk file. If file name is given, stored in file otherwise returned as string"""
|
|
|
|
grid = self.get_grid() + np.ones(3,dtype=int)
|
|
|
|
size = self.get_size()
|
|
|
|
origin = self.get_origin()
|
|
|
|
|
|
|
|
coords = [
|
|
|
|
np.linspace(0,size[0],grid[0]) - origin[0],
|
|
|
|
np.linspace(0,size[1],grid[1]) - origin[1],
|
|
|
|
np.linspace(0,size[2],grid[2]) - origin[2]
|
|
|
|
]
|
|
|
|
|
|
|
|
rGrid = vtk.vtkRectilinearGrid()
|
|
|
|
coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()]
|
|
|
|
|
|
|
|
rGrid.SetDimensions(*grid)
|
|
|
|
for d,coord in enumerate(coords):
|
|
|
|
for c in coord:
|
|
|
|
coordArray[d].InsertNextValue(c)
|
|
|
|
|
|
|
|
rGrid.SetXCoordinates(coordArray[0])
|
|
|
|
rGrid.SetYCoordinates(coordArray[1])
|
|
|
|
rGrid.SetZCoordinates(coordArray[2])
|
|
|
|
|
|
|
|
ms = numpy_support.numpy_to_vtk(num_array=self.microstructure.flatten(order='F'),array_type=vtk.VTK_INT)
|
|
|
|
ms.SetName('microstructure')
|
|
|
|
rGrid.GetCellData().AddArray(ms)
|
|
|
|
|
|
|
|
|
|
|
|
if fname is not None:
|
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
|
|
|
|
ext = os.path.splitext(fname)[1]
|
|
|
|
if ext == '':
|
|
|
|
name = fname + '.' + writer.GetDefaultFileExtension()
|
|
|
|
elif ext == writer.GetDefaultFileExtension():
|
|
|
|
name = fname
|
|
|
|
else:
|
|
|
|
raise ValueError("unknown extension {}".format(ext))
|
|
|
|
writer.SetFileName(name)
|
|
|
|
else:
|
|
|
|
writer = vtk.vtkDataSetWriter()
|
|
|
|
writer.SetHeader('damask.Geom '+version)
|
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
|
|
|
|
writer.SetInputData(rGrid)
|
|
|
|
writer.Write()
|
|
|
|
|
|
|
|
if fname is None: return writer.GetOutputString()
|
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-25 13:44:53 +05:30
|
|
|
def show(self):
|
2019-05-26 15:33:21 +05:30
|
|
|
"""Show raw content (as in file)"""
|
2019-05-25 13:44:53 +05:30
|
|
|
f=StringIO()
|
|
|
|
self.to_file(f)
|
|
|
|
f.seek(0)
|
|
|
|
return ''.join(f.readlines())
|