2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2012-02-02 01:14:36 +05:30
|
|
|
import re
|
2018-07-06 22:30:58 +05:30
|
|
|
import os
|
|
|
|
|
2011-12-22 16:00:25 +05:30
|
|
|
|
2012-02-02 01:14:36 +05:30
|
|
|
class Section():
|
|
|
|
def __init__(self,data = {'__order__':[]},part = ''):
|
|
|
|
classes = {
|
|
|
|
'homogenization':Homogenization,
|
|
|
|
'microstructure':Microstructure,
|
|
|
|
'crystallite':Crystallite,
|
|
|
|
'phase':Phase,
|
|
|
|
'texture':Texture,
|
|
|
|
}
|
|
|
|
self.parameters = {}
|
|
|
|
for key in data:
|
2018-07-06 22:30:58 +05:30
|
|
|
self.parameters[key] = data[key] if isinstance(data[key], list) else [data[key]]
|
2012-02-02 01:14:36 +05:30
|
|
|
|
|
|
|
if '__order__' not in self.parameters:
|
2016-07-02 16:58:32 +05:30
|
|
|
self.parameters['__order__'] = list(self.parameters.keys())
|
2012-02-02 01:14:36 +05:30
|
|
|
if part.lower() in classes:
|
|
|
|
self.__class__ = classes[part.lower()]
|
|
|
|
self.__init__(data)
|
|
|
|
|
|
|
|
def add_multiKey(self,key,data):
|
|
|
|
multiKey = '(%s)'%key
|
|
|
|
if multiKey not in self.parameters: self.parameters[multiKey] = []
|
|
|
|
if multiKey not in self.parameters['__order__']: self.parameters['__order__'] += [multiKey]
|
2018-07-06 22:30:58 +05:30
|
|
|
self.parameters[multiKey] += [[item] for item in data] if isinstance(data, list) else [[data]]
|
2012-02-02 01:14:36 +05:30
|
|
|
|
|
|
|
def data(self):
|
|
|
|
return self.parameters
|
|
|
|
|
|
|
|
|
|
|
|
class Homogenization(Section):
|
|
|
|
def __init__(self,data = {'__order__':[]}):
|
|
|
|
Section.__init__(self,data)
|
|
|
|
|
|
|
|
|
|
|
|
class Crystallite(Section):
|
|
|
|
def __init__(self,data = {'__order__':[]}):
|
|
|
|
Section.__init__(self,data)
|
|
|
|
|
|
|
|
|
|
|
|
class Phase(Section):
|
|
|
|
def __init__(self,data = {'__order__':[]}):
|
|
|
|
Section.__init__(self,data)
|
|
|
|
|
|
|
|
|
|
|
|
class Microstructure(Section):
|
|
|
|
def __init__(self,data = {'__order__':[]}):
|
|
|
|
Section.__init__(self,data)
|
|
|
|
|
|
|
|
|
|
|
|
class Texture(Section):
|
|
|
|
def __init__(self,data = {'__order__':[]}):
|
|
|
|
Section.__init__(self,data)
|
|
|
|
|
|
|
|
def add_component(self,theType,properties):
|
|
|
|
|
2018-07-06 22:30:58 +05:30
|
|
|
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
|
2012-02-02 01:14:36 +05:30
|
|
|
|
2014-06-17 14:23:37 +05:30
|
|
|
try:
|
|
|
|
multiKey = theType.lower()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2012-02-02 01:14:36 +05:30
|
|
|
|
|
|
|
if multiKey == 'gauss':
|
|
|
|
self.add_multiKey(multiKey,'phi1 %g\tPhi %g\tphi2 %g\tscatter %g\tfraction %g'%(
|
|
|
|
properties['eulers'][0],
|
|
|
|
properties['eulers'][1],
|
|
|
|
properties['eulers'][2],
|
|
|
|
scatter,
|
|
|
|
fraction,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if multiKey == 'fiber':
|
|
|
|
self.add_multiKey(multiKey,'alpha1 %g\talpha2 %g\tbeta1 %g\tbeta2 %g\tscatter %g\tfraction %g'%(
|
|
|
|
properties['eulers'][0],
|
|
|
|
properties['eulers'][1],
|
|
|
|
properties['eulers'][2],
|
|
|
|
properties['eulers'][3],
|
|
|
|
scatter,
|
|
|
|
fraction,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
class Material():
|
2016-03-04 22:25:11 +05:30
|
|
|
"""Reads, manipulates and writes material.config files"""
|
|
|
|
|
2015-04-23 00:17:45 +05:30
|
|
|
def __init__(self,verbose=True):
|
2016-10-25 00:46:29 +05:30
|
|
|
"""Generates ordered list of parts"""
|
2011-12-14 01:32:26 +05:30
|
|
|
self.parts = [
|
|
|
|
'homogenization',
|
|
|
|
'microstructure',
|
|
|
|
'crystallite',
|
|
|
|
'phase',
|
|
|
|
'texture',
|
2016-03-04 22:25:11 +05:30
|
|
|
]
|
2011-12-14 01:32:26 +05:30
|
|
|
self.data = {\
|
|
|
|
'homogenization': {'__order__': []},
|
|
|
|
'microstructure': {'__order__': []},
|
|
|
|
'crystallite': {'__order__': []},
|
|
|
|
'phase': {'__order__': []},
|
|
|
|
'texture': {'__order__': []},
|
|
|
|
}
|
2015-04-23 00:17:45 +05:30
|
|
|
self.verbose = verbose
|
2011-12-14 01:32:26 +05:30
|
|
|
|
|
|
|
def __repr__(self):
|
2018-07-06 22:30:58 +05:30
|
|
|
"""Returns current data structure in material.config format"""
|
2011-12-14 01:32:26 +05:30
|
|
|
me = []
|
|
|
|
for part in self.parts:
|
2018-07-06 22:30:58 +05:30
|
|
|
if self.verbose: print('processing <{}>'.format(part))
|
|
|
|
me += ['',
|
|
|
|
'#-----------------------------#',
|
|
|
|
'<{}>'.format(part),
|
|
|
|
'#-----------------------------#',
|
|
|
|
]
|
2011-12-14 01:32:26 +05:30
|
|
|
for section in self.data[part]['__order__']:
|
2018-07-06 22:30:58 +05:30
|
|
|
me += ['',
|
|
|
|
'[{}] {}'.format(section,'#'*max(0,27-len(section))),
|
|
|
|
'',
|
|
|
|
]
|
2011-12-14 01:32:26 +05:30
|
|
|
for key in self.data[part][section]['__order__']:
|
|
|
|
if key.startswith('(') and key.endswith(')'): # multiple (key)
|
2018-07-06 22:30:58 +05:30
|
|
|
me += ['{}\t{}'.format(key,' '.join(values)) for values in self.data[part][section][key]]
|
2011-12-14 01:32:26 +05:30
|
|
|
else: # plain key
|
2018-07-06 22:30:58 +05:30
|
|
|
me += ['{}\t{}'.format(key,' '.join(map(str,self.data[part][section][key])))]
|
|
|
|
return '\n'.join(me) + '\n'
|
2011-12-14 01:32:26 +05:30
|
|
|
|
2018-07-06 22:30:58 +05:30
|
|
|
def parse(self, part=None, sections=[], content=None):
|
2011-12-14 01:32:26 +05:30
|
|
|
|
|
|
|
re_part = re.compile(r'^<(.+)>$') # pattern for part
|
|
|
|
re_sec = re.compile(r'^\[(.+)\]$') # pattern for section
|
|
|
|
|
|
|
|
name_section = ''
|
|
|
|
active = False
|
|
|
|
|
|
|
|
for line in content:
|
2012-02-02 01:14:36 +05:30
|
|
|
line = line.split('#')[0].strip() # kill comments and extra whitespace
|
2015-03-15 20:54:45 +05:30
|
|
|
line = line.split('/echo/')[0].strip() # remove '/echo/' tags
|
2014-06-16 18:41:26 +05:30
|
|
|
line = line.lower() # be case insensitive
|
2018-07-06 22:30:58 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
if line: # content survives...
|
2018-07-06 22:30:58 +05:30
|
|
|
match_part = re_part.match(line)
|
|
|
|
if match_part: # found <...> separator
|
2011-12-14 01:32:26 +05:30
|
|
|
active = (match_part.group(1) == part) # only active in <part>
|
|
|
|
continue
|
|
|
|
if active:
|
2018-07-06 22:30:58 +05:30
|
|
|
match_sec = re_sec.match(line)
|
2011-12-14 01:32:26 +05:30
|
|
|
if match_sec: # found [section]
|
|
|
|
name_section = match_sec.group(1) # remember name ...
|
|
|
|
if '__order__' not in self.data[part]: self.data[part]['__order__'] = []
|
|
|
|
self.data[part]['__order__'].append(name_section) # ... and position
|
|
|
|
self.data[part][name_section] = {'__order__':[]}
|
|
|
|
continue
|
2018-07-06 22:30:58 +05:30
|
|
|
|
|
|
|
if sections == [] or name_section in sections: # possibly restrict to subset
|
2011-12-14 01:32:26 +05:30
|
|
|
items = line.split()
|
|
|
|
if items[0] not in self.data[part][name_section]: # first encounter of key?
|
|
|
|
self.data[part][name_section][items[0]] = [] # create item
|
|
|
|
self.data[part][name_section]['__order__'].append(items[0])
|
|
|
|
if items[0].startswith('(') and items[0].endswith(')'): # multiple "(key)"
|
|
|
|
self.data[part][name_section][items[0]].append(items[1:])
|
|
|
|
else: # plain key
|
|
|
|
self.data[part][name_section][items[0]] = items[1:]
|
2018-07-06 22:30:58 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read(self,filename=None):
|
|
|
|
"""Reads material.config file"""
|
|
|
|
def recursiveRead(filename):
|
|
|
|
"""Takes care of include statements like '{}'"""
|
|
|
|
result = []
|
|
|
|
re_include = re.compile(r'^{(.+)}$')
|
|
|
|
with open(filename) as f: lines = f.readlines()
|
|
|
|
for line in lines:
|
|
|
|
match = re_include.match(line.split()[0]) if line.strip() else False
|
|
|
|
result += [line] if not match else \
|
|
|
|
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)
|
2012-02-02 01:14:36 +05:30
|
|
|
for p in self.parts:
|
2018-07-06 22:30:58 +05:30
|
|
|
self.parse(part=p, content=c)
|
|
|
|
|
|
|
|
def write(self,filename='material.config', overwrite=False):
|
|
|
|
"""Writes to material.config"""
|
2012-02-02 01:14:36 +05:30
|
|
|
i = 0
|
2018-07-06 22:30:58 +05:30
|
|
|
outname = filename
|
|
|
|
while os.path.exists(outname) and not overwrite:
|
2012-02-02 01:14:36 +05:30
|
|
|
i += 1
|
2018-07-06 22:30:58 +05:30
|
|
|
outname = '{}_{}'.format(filename,i)
|
2012-02-02 01:14:36 +05:30
|
|
|
|
2018-07-06 22:30:58 +05:30
|
|
|
if self.verbose: print('Writing material data to {}'.format(outname))
|
|
|
|
with open(outname,'w') as f:
|
|
|
|
f.write(str(self))
|
|
|
|
return outname
|
2012-02-02 01:14:36 +05:30
|
|
|
|
2018-07-06 22:30:58 +05:30
|
|
|
def add_section(self, part=None, section=None, initialData=None, merge=False):
|
2016-03-04 22:25:11 +05:30
|
|
|
"""adding/updating"""
|
2014-06-16 18:41:26 +05:30
|
|
|
part = part.lower()
|
|
|
|
section = section.lower()
|
2018-07-06 22:30:58 +05:30
|
|
|
if part not in self.parts: raise Exception('invalid part {}'.format(part))
|
2012-02-02 01:14:36 +05:30
|
|
|
|
2018-07-06 22:30:58 +05:30
|
|
|
if not isinstance(initialData, dict):
|
2014-06-16 18:41:26 +05:30
|
|
|
initialData = initialData.data()
|
2012-02-02 01:14:36 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
if section not in self.data[part]: self.data[part]['__order__'] += [section]
|
2012-02-02 01:14:36 +05:30
|
|
|
if section in self.data[part] and merge:
|
2014-06-17 14:23:37 +05:30
|
|
|
for existing in self.data[part][section]['__order__']: # replace existing
|
2014-06-16 18:41:26 +05:30
|
|
|
if existing in initialData['__order__']:
|
2014-06-17 14:23:37 +05:30
|
|
|
if existing.startswith('(') and existing.endswith(')'): # multiple (key)
|
2014-06-16 18:41:26 +05:30
|
|
|
self.data[part][section][existing] += initialData[existing] # add new multiple entries to existing ones
|
2014-06-17 14:23:37 +05:30
|
|
|
else: # regular key
|
2014-06-16 18:41:26 +05:30
|
|
|
self.data[part][section][existing] = initialData[existing] # plain replice
|
|
|
|
for new in initialData['__order__']: # merge new content
|
2012-02-02 01:14:36 +05:30
|
|
|
if new not in self.data[part][section]['__order__']:
|
2014-06-16 18:41:26 +05:30
|
|
|
self.data[part][section][new] = initialData[new]
|
2012-02-02 01:14:36 +05:30
|
|
|
self.data[part][section]['__order__'] += [new]
|
|
|
|
else:
|
2014-06-16 18:41:26 +05:30
|
|
|
self.data[part][section] = initialData
|
2012-02-02 01:14:36 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
|
|
|
|
|
2012-02-02 01:14:36 +05:30
|
|
|
|
|
|
|
def add_microstructure(self, section='',
|
|
|
|
components={}, # dict of phase,texture, and fraction lists
|
|
|
|
):
|
2016-07-02 16:58:32 +05:30
|
|
|
"""Experimental! Needs expansion to multi-constituent microstructures..."""
|
2012-02-02 01:14:36 +05:30
|
|
|
microstructure = Microstructure()
|
2016-03-04 22:25:11 +05:30
|
|
|
# make keys lower case (http://stackoverflow.com/questions/764235/dictionary-to-lowercase-in-python)
|
2016-07-02 16:58:32 +05:30
|
|
|
components=dict((k.lower(), v) for k,v in components.items())
|
2014-06-17 14:23:37 +05:30
|
|
|
|
|
|
|
for key in ['phase','texture','fraction','crystallite']:
|
2018-07-06 22:30:58 +05:30
|
|
|
if isinstance(components[key], list):
|
2014-06-17 14:23:37 +05:30
|
|
|
for i, x in enumerate(components[key]):
|
|
|
|
try:
|
|
|
|
components[key][i] = x.lower()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2018-07-06 22:30:58 +05:30
|
|
|
else:
|
|
|
|
try:
|
|
|
|
components[key] = [components[key].lower()]
|
|
|
|
except AttributeError:
|
|
|
|
components[key] = [components[key]]
|
2014-06-17 14:23:37 +05:30
|
|
|
|
2016-03-04 23:20:13 +05:30
|
|
|
for (phase,texture,fraction,crystallite) in zip(components['phase'],components['texture'],
|
|
|
|
components['fraction'],components['crystallite']):
|
2012-05-24 21:30:32 +05:30
|
|
|
microstructure.add_multiKey('constituent','phase %i\ttexture %i\tfraction %g\ncrystallite %i'%(
|
2012-02-02 01:14:36 +05:30
|
|
|
self.data['phase']['__order__'].index(phase)+1,
|
|
|
|
self.data['texture']['__order__'].index(texture)+1,
|
2012-05-24 21:30:32 +05:30
|
|
|
fraction,
|
|
|
|
self.data['crystallite']['__order__'].index(crystallite)+1))
|
2014-06-17 14:23:37 +05:30
|
|
|
|
2012-02-02 01:14:36 +05:30
|
|
|
self.add_section('microstructure',section,microstructure)
|
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
|
|
|
|
def change_value(self, part=None,
|
|
|
|
section=None,
|
|
|
|
key=None,
|
|
|
|
value=None):
|
2016-03-04 22:25:11 +05:30
|
|
|
if not isinstance(value,list):
|
|
|
|
if not isinstance(value,str):
|
2011-12-14 01:32:26 +05:30
|
|
|
value = '%s'%value
|
|
|
|
value = [value]
|
|
|
|
newlen = len(value)
|
2014-06-16 18:41:26 +05:30
|
|
|
oldval = self.data[part.lower()][section.lower()][key.lower()]
|
2011-12-14 01:32:26 +05:30
|
|
|
oldlen = len(oldval)
|
2014-06-16 18:41:26 +05:30
|
|
|
print('changing %s:%s:%s from %s to %s '%(part.lower(),section.lower(),key.lower(),oldval,value))
|
|
|
|
self.data[part.lower()][section.lower()][key.lower()] = value
|
2011-12-14 01:32:26 +05:30
|
|
|
if newlen is not oldlen:
|
|
|
|
print('Length of value was changed from %i to %i!'%(oldlen,newlen))
|
|
|
|
|
2018-07-06 22:30:58 +05:30
|
|
|
|