2019-05-25 02:00:25 +05:30
|
|
|
import math
|
|
|
|
from io import StringIO
|
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def __init__(self,size,microstructure,homogenization=1,comments=[]):
|
2019-05-25 13:44:53 +05:30
|
|
|
"""New geometry definition from array of microstructures and size"""
|
2019-05-25 02:00:25 +05:30
|
|
|
if len(size) != 3 or any(np.array(size)<=0):
|
2019-05-26 15:33:21 +05:30
|
|
|
raise ValueError('Invalid size {}'.format(*size))
|
2019-05-25 02:00:25 +05:30
|
|
|
else:
|
|
|
|
self.size = np.array(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']:
|
|
|
|
raise TypeError('Invalid data type {} for microstructure'.format(microstructure.dtype))
|
2019-05-25 02:00:25 +05:30
|
|
|
else:
|
|
|
|
self.microstructure = microstructure
|
|
|
|
|
|
|
|
if not isinstance(homogenization,int) or homogenization < 1:
|
2019-05-26 15:33:21 +05:30
|
|
|
raise TypeError('Invalid homogenization {}'.format(homogenization))
|
2019-05-25 02:00:25 +05:30
|
|
|
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.get_size()))) + \
|
|
|
|
'homogenization: {}\n'.format(self.get_homogenization()) + \
|
|
|
|
'# microstructures: {}\n'.format(len(np.unique(self.microstructure))) + \
|
|
|
|
'max microstructures: {}\n'.format(np.max(self.microstructure))
|
2019-05-25 02:00:25 +05:30
|
|
|
|
|
|
|
def add_comment(self,comment):
|
|
|
|
if not isinstance(comment,list):
|
|
|
|
self.comments += [str(comment)]
|
|
|
|
else:
|
|
|
|
self.comments += [str(c) for c in comment]
|
|
|
|
|
2019-05-26 13:19:56 +05:30
|
|
|
def set_microstructure(self,microstructure):
|
|
|
|
self.microstructure = microstructure
|
|
|
|
|
2019-05-25 02:00:25 +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):
|
|
|
|
return self.microstructure
|
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
def get_size(self):
|
|
|
|
return self.size
|
|
|
|
|
|
|
|
def get_grid(self):
|
|
|
|
return np.array(self.microstructure.shape)
|
|
|
|
|
|
|
|
def get_homogenization(self):
|
|
|
|
return self.homogenization
|
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
@classmethod
|
|
|
|
def from_file(cls,fname):
|
2019-05-26 15:33:21 +05:30
|
|
|
"""Reads from *.geom file"""
|
2019-05-25 02:00:25 +05:30
|
|
|
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))]
|
2019-05-25 02:00:25 +05:30
|
|
|
else:
|
|
|
|
fname.seek(0)
|
2019-05-26 15:33:21 +05:30
|
|
|
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 = [fname.readline() for i in range(int(header_length))]
|
2019-05-25 02:00:25 +05:30
|
|
|
|
|
|
|
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)
|
2019-05-25 02:00:25 +05:30
|
|
|
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)
|
2019-05-25 02:00:25 +05:30
|
|
|
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())
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-25 11:47:51 +05:30
|
|
|
if isinstance(fname,str):
|
2019-05-26 15:33:21 +05:30
|
|
|
raw = f.readlines()
|
|
|
|
f.close()
|
2019-05-25 11:47:51 +05:30
|
|
|
else:
|
|
|
|
raw = fname.readlines()
|
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
|
|
|
|
for line in raw:
|
|
|
|
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-25 11:47:51 +05:30
|
|
|
|
|
|
|
if np.any(np.mod(microstructure.flatten(),1)!=0.0):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
microstructure = microstructure.astype('int')
|
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
return cls(size,microstructure.reshape(grid),homogenization,comments)
|
|
|
|
|
|
|
|
def to_file(self,fname):
|
2019-05-26 15:33:21 +05:30
|
|
|
"""Saves to file"""
|
2019-05-25 02:00:25 +05:30
|
|
|
grid = self.get_grid()
|
|
|
|
header = ['{} header'.format(len(self.comments)+3)]
|
|
|
|
header += self.comments
|
|
|
|
header.append('grid a {} b {} c {}'.format(*grid))
|
2019-05-25 11:47:51 +05:30
|
|
|
header.append('size x {} y {} z {}'.format(*self.get_size()))
|
2019-05-25 02:00:25 +05:30
|
|
|
header.append('homogenization {}'.format(self.get_homogenization()))
|
2019-05-25 17:18:38 +05:30
|
|
|
|
2019-05-25 02:00:25 +05:30
|
|
|
if self.microstructure.dtype == 'int':
|
|
|
|
format_string='%{}i'.format(int(math.floor(math.log10(self.microstructure.max())+1)))
|
|
|
|
else:
|
|
|
|
format_string='%.18e'
|
2019-05-25 11:47:51 +05:30
|
|
|
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-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())
|