DAMASK_EICMD/python/damask/geom.py

194 lines
7.2 KiB
Python
Raw Normal View History

import math
from io import StringIO
2019-05-26 15:33:21 +05:30
import numpy as np
class Geom():
2019-05-25 02:01:34 +05:30
"""Geometry definition for grid solvers"""
def __init__(self,microstructure,size,homogenization=1,comments=[]):
2019-05-25 13:44:53 +05:30
"""New geometry definition from array of microstructures and size"""
if len(microstructure.shape) != 3:
2019-05-26 15:33:21 +05:30
raise ValueError('Invalid microstructure shape {}'.format(*microstructure.shape))
elif microstructure.dtype not in [int,float]:
2019-05-26 15:33:21 +05:30
raise TypeError('Invalid data type {} for microstructure'.format(microstructure.dtype))
else:
self.microstructure = microstructure
if len(size) != 3 or any(np.array(size)<=0):
raise ValueError('Invalid size {}'.format(*size))
else:
self.size = np.array(size)
if not isinstance(homogenization,int) or homogenization < 1:
2019-05-26 15:33:21 +05:30
raise TypeError('Invalid homogenization {}'.format(homogenization))
else:
self.homogenization = homogenization
if not isinstance(comments,list):
self.comments = [str(comments)]
else:
self.comments = [str(comment) for comment in comments]
2019-05-25 13:44:53 +05:30
def __repr__(self):
"""Basic information on geometry definition"""
return 'grid a b c: {}\n'.format(' x '.join(map(str,self.get_grid()))) + \
'size x y z: {}\n'.format(' x '.join(map(str,self.size))) + \
'homogenization: {}\n'.format(self.homogenization) + \
2019-05-25 13:44:53 +05:30
'# microstructures: {}\n'.format(len(np.unique(self.microstructure))) + \
'max microstructures: {}\n'.format(np.max(self.microstructure))
def update(self,microstructure=None,size=None,rescale=False):
"""Updates microstructure and size"""
grid_old = self.get_grid()
2019-05-26 23:58:19 +05:30
size_old = self.get_size()
unique_old = len(np.unique(self.microstructure))
max_old = np.max(self.microstructure)
if size is not None and rescale:
raise ValueError('Either set size explicitly or rescale automatically')
if microstructure is not None:
if len(microstructure.shape) != 3:
raise ValueError('Invalid microstructure shape {}'.format(*microstructure.shape))
elif microstructure.dtype not in ['int','float']:
raise TypeError('Invalid data type {} for microstructure'.format(microstructure.dtype))
else:
self.microstructure = microstructure
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)
if rescale:
self.size = self.size * self.get_grid()/grid_old
message = ''
if np.any(grid_old != self.get_grid()):
message += 'grid a b c: {}\n'.format(' x '.join(map(str,self.get_grid())))
2019-05-26 23:58:19 +05:30
if np.any(size_old != self.size):
message += 'size x y z: {}\n'.format(' x '.join(map(str,self.size)))
if unique_old != len(np.unique(self.microstructure)):
message += '# microstructures: {}\n'.format(len(np.unique(self.microstructure)))
if max_old != np.max(self.microstructure):
message += 'max microstructures: {}\n'.format(np.max(self.microstructure))
if message != '': return message
def add_comment(self,comment):
if not isinstance(comment,list):
self.comments = [str(comment)] + self.comments
else:
self.comments = [str(c) for c in comment] + self.comments
2019-05-26 13:19:56 +05:30
def set_microstructure(self,microstructure):
2019-05-26 23:58:19 +05:30
self.microstructure = np.copy(microstructure)
2019-05-26 13:19:56 +05:30
def set_size(self,size):
self.size = np.array(size)
2019-05-26 15:33:21 +05:30
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
def get_size(self):
2019-05-26 23:58:19 +05:30
return np.copy(self.size)
def get_grid(self):
return np.array(self.microstructure.shape)
def get_homogenization(self):
return self.homogenization
def get_comments(self):
return self.comments[:]
2019-05-26 15:33:21 +05:30
@classmethod
def from_file(cls,fname):
2019-05-26 15:33:21 +05:30
"""Reads from *.geom file"""
if isinstance(fname,str):
2019-05-26 15:33:21 +05:30
f = open(fname)
header_length,keyword = f.readline().split()
if not keyword.startswith('head') or int(header_length) < 3:
raise TypeError('Header length information missing or invalid')
comments_old = [f.readline() for i in range(int(header_length))]
else:
fname.seek(0)
2019-05-27 01:38:42 +05:30
header_length,keyword = fname.readline().split()
2019-05-26 15:33:21 +05:30
if not keyword.startswith('head') or int(header_length) < 3:
raise TypeError('Header length information missing or invalid')
comments_old = [fname.readline() for i in range(int(header_length))]
comments = []
for i,line in enumerate(comments_old):
if line.lower().strip().startswith('grid'):
2019-05-26 15:33:21 +05:30
grid = np.array([int(line.split()[j]) for j in [2,4,6]]) # assume correct order (a,b,c)
elif line.lower().strip().startswith('size'):
2019-05-26 15:33:21 +05:30
size = np.array([float(line.split()[j]) for j in [2,4,6]]) # assume correct order (x,y,z)
elif line.lower().strip().startswith('homogenization'):
homogenization = int(line.split()[1])
else:
2019-05-25 17:18:38 +05:30
comments.append(line.rstrip().strip())
if isinstance(fname,str):
2019-05-26 15:33:21 +05:30
raw = f.readlines()
f.close()
else:
raw = fname.readlines()
2019-05-26 15:33:21 +05:30
microstructure = np.empty(grid.prod()) # initialize as flat array
i = 0
for line in raw:
items = line.split()
if len(items) == 3:
if items[1].lower() == 'of':
items = np.ones(int(items[0]))*float(items[2])
elif items[1].lower() == 'to':
items = np.linspace(int(items[0]),int(items[2]),
abs(int(items[2])-int(items[0]))+1,dtype=float)
else: items = list(map(float,items))
else: items = list(map(float,items))
microstructure[i:i+len(items)] = items
i += len(items)
2019-05-26 15:33:21 +05:30
if i != grid.prod():
raise TypeError('Invalid file: expected {} entries,found {}'.format(grid.prod(),i))
microstructure = microstructure.reshape(grid,order='F')
if '.' in raw[0]: # contains float values
pass
else: # assume int values
microstructure = microstructure.astype('int')
return cls(microstructure.reshape(grid),size,homogenization,comments)
def to_file(self,fname):
2019-05-26 15:33:21 +05:30
"""Saves to file"""
grid = self.get_grid()
header = ['{} header'.format(len(self.comments)+3)]
header += self.comments
header.append('grid a {} b {} c {}'.format(*grid))
header.append('size x {} y {} z {}'.format(*self.size))
header.append('homogenization {}'.format(self.get_homogenization()))
2019-05-25 17:18:38 +05:30
if self.microstructure.dtype == 'int':
format_string='%{}i'.format(int(math.floor(math.log10(self.microstructure.max())+1)))
else:
format_string='%.18e'
np.savetxt(fname, self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T,
header='\n'.join(header), fmt=format_string, comments='')
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())