more user friendly errors
This commit is contained in:
parent
be3513d1e0
commit
dcb79afc84
|
@ -1,24 +1,28 @@
|
||||||
import numpy as np
|
|
||||||
import math
|
import math
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
class Geom():
|
class Geom():
|
||||||
"""Geometry definition for grid solvers"""
|
"""Geometry definition for grid solvers"""
|
||||||
|
|
||||||
def __init__(self,size,microstructure,homogenization=1,comments=[]):
|
def __init__(self,size,microstructure,homogenization=1,comments=[]):
|
||||||
"""New geometry definition from array of microstructures and size"""
|
"""New geometry definition from array of microstructures and size"""
|
||||||
if len(size) != 3 or any(np.array(size)<=0):
|
if len(size) != 3 or any(np.array(size)<=0):
|
||||||
raise ValueError('invalid size')
|
raise ValueError('Invalid size {}'.format(*size))
|
||||||
else:
|
else:
|
||||||
self.size = np.array(size)
|
self.size = np.array(size)
|
||||||
|
|
||||||
if len(microstructure.shape) != 3:
|
if len(microstructure.shape) != 3:
|
||||||
raise ValueError('invalid microstructure')
|
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:
|
else:
|
||||||
self.microstructure = microstructure
|
self.microstructure = microstructure
|
||||||
|
|
||||||
if not isinstance(homogenization,int) or homogenization < 1:
|
if not isinstance(homogenization,int) or homogenization < 1:
|
||||||
raise ValueError('invalid homogenization')
|
raise TypeError('Invalid homogenization {}'.format(homogenization))
|
||||||
else:
|
else:
|
||||||
self.homogenization = homogenization
|
self.homogenization = homogenization
|
||||||
|
|
||||||
|
@ -47,6 +51,7 @@ class Geom():
|
||||||
def set_size(self,size):
|
def set_size(self,size):
|
||||||
self.size = np.array(size)
|
self.size = np.array(size)
|
||||||
|
|
||||||
|
|
||||||
def get_microstructure(self):
|
def get_microstructure(self):
|
||||||
return self.microstructure
|
return self.microstructure
|
||||||
|
|
||||||
|
@ -59,16 +64,22 @@ class Geom():
|
||||||
def get_homogenization(self):
|
def get_homogenization(self):
|
||||||
return self.homogenization
|
return self.homogenization
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_file(cls,fname):
|
def from_file(cls,fname):
|
||||||
|
"""Reads from *.geom file"""
|
||||||
if isinstance(fname,str):
|
if isinstance(fname,str):
|
||||||
with open(fname) as f:
|
f = open(fname)
|
||||||
header_length = int(f.readline().split()[0])
|
header_length,keyword = f.readline().split()
|
||||||
comments_old = [f.readline() for i in range(header_length)]
|
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:
|
else:
|
||||||
fname.seek(0)
|
fname.seek(0)
|
||||||
header_length = int(fname.readline().split()[0])
|
header_length,keyword = f.readline().split()
|
||||||
comments_old = [fname.readline() for i in range(header_length)]
|
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 = []
|
comments = []
|
||||||
for i,line in enumerate(comments_old):
|
for i,line in enumerate(comments_old):
|
||||||
|
@ -82,14 +93,13 @@ class Geom():
|
||||||
comments.append(line.rstrip().strip())
|
comments.append(line.rstrip().strip())
|
||||||
|
|
||||||
if isinstance(fname,str):
|
if isinstance(fname,str):
|
||||||
with open(fname) as f:
|
raw = f.readlines()
|
||||||
raw = f.readlines()[header_length+1:]
|
f.close()
|
||||||
else:
|
else:
|
||||||
raw = fname.readlines()
|
raw = fname.readlines()
|
||||||
|
|
||||||
microstructure = np.empty(grid.prod()) # initialize as flat array
|
microstructure = np.empty(grid.prod()) # initialize as flat array
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
for line in raw:
|
for line in raw:
|
||||||
items = line.split()
|
items = line.split()
|
||||||
if len(items) == 3:
|
if len(items) == 3:
|
||||||
|
@ -104,6 +114,9 @@ class Geom():
|
||||||
microstructure[i:i+len(items)] = items
|
microstructure[i:i+len(items)] = items
|
||||||
i += len(items)
|
i += len(items)
|
||||||
|
|
||||||
|
if i != grid.prod():
|
||||||
|
raise TypeError('Invalid file: expected {} entries,found {}'.format(grid.prod(),i))
|
||||||
|
|
||||||
microstructure = microstructure.reshape(grid,order='F')
|
microstructure = microstructure.reshape(grid,order='F')
|
||||||
|
|
||||||
if np.any(np.mod(microstructure.flatten(),1)!=0.0):
|
if np.any(np.mod(microstructure.flatten(),1)!=0.0):
|
||||||
|
@ -114,6 +127,7 @@ class Geom():
|
||||||
return cls(size,microstructure.reshape(grid),homogenization,comments)
|
return cls(size,microstructure.reshape(grid),homogenization,comments)
|
||||||
|
|
||||||
def to_file(self,fname):
|
def to_file(self,fname):
|
||||||
|
"""Saves to file"""
|
||||||
grid = self.get_grid()
|
grid = self.get_grid()
|
||||||
header = ['{} header'.format(len(self.comments)+3)]
|
header = ['{} header'.format(len(self.comments)+3)]
|
||||||
header += self.comments
|
header += self.comments
|
||||||
|
@ -129,6 +143,7 @@ class Geom():
|
||||||
header='\n'.join(header), fmt=format_string, comments='')
|
header='\n'.join(header), fmt=format_string, comments='')
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
|
"""Show raw content (as in file)"""
|
||||||
f=StringIO()
|
f=StringIO()
|
||||||
self.to_file(f)
|
self.to_file(f)
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
|
|
Loading…
Reference in New Issue