introduced material.config parsing and self representation, i.e. printing of class object.
This commit is contained in:
parent
dc6c29a910
commit
3c7ebc7a30
|
@ -1,46 +1,100 @@
|
||||||
|
import os,string,re
|
||||||
|
|
||||||
class DAMASK_TOOLS():
|
class DAMASK_TOOLS():
|
||||||
import os,string
|
|
||||||
def check_env(self):
|
def check_env(self):
|
||||||
import os
|
import os
|
||||||
if os.getenv('DAMASK_ROOT') is None:
|
if os.getenv('DAMASK_ROOT') is None:
|
||||||
print('No DAMASK_ROOT environment variable, did you run DAMASK/installation/setup_shellrc?')
|
print('No DAMASK_ROOT environment variable, did you run DAMASK/installation/setup_shellrc?')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class MATERIAL_CONFIG():
|
class MATERIAL_CONFIG():
|
||||||
import os,sys
|
__slots__ = ['data']
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
homogenization=[]
|
self.data = {\
|
||||||
|
'homogenization': {'__order__': []},
|
||||||
|
'microstructure': {'__order__': []},
|
||||||
|
'crystallite': {'__order__': []},
|
||||||
|
'phase': {'__order__': []},
|
||||||
|
'texture': {'__order__': []},
|
||||||
|
}
|
||||||
|
|
||||||
def add_homogenization(self, name=None, type=None):
|
def __repr__(self):
|
||||||
|
me = []
|
||||||
|
for part in [
|
||||||
|
'homogenization',
|
||||||
|
'microstructure',
|
||||||
|
'crystallite',
|
||||||
|
'phase',
|
||||||
|
'texture',
|
||||||
|
]:
|
||||||
|
me += ['','#-----------------------------#','<%s>'%part,'#-----------------------------#',]
|
||||||
|
for section in self.data[part]['__order__']:
|
||||||
|
me += ['','[%s]\t\t#-----------------------'%section,'',]
|
||||||
|
for key in self.data[part][section]['__order__']:
|
||||||
|
if key.startswith('(') and key.endswith(')'): # multiple (key)
|
||||||
|
me += ['%s\t%s'%(key,' '.join(values)) for values in self.data[part][section][key]]
|
||||||
|
else: # plain key
|
||||||
|
me += ['%s\t%s'%(key,' '.join(self.data[part][section][key]))]
|
||||||
|
|
||||||
if type is 'isostrain':
|
return '\n'.join(me)
|
||||||
h={'type':type}
|
|
||||||
h['ngrains']=ngrains
|
|
||||||
return h
|
|
||||||
|
|
||||||
def add_crystallite(self, name=None):
|
|
||||||
pass
|
def parse_data(self, part=None, sections=[], content=None):
|
||||||
def add_texture(self, name=None):
|
|
||||||
pass
|
re_part = re.compile(r'^<(.+)>$') # pattern for part
|
||||||
def add_phase(self, name=None):
|
re_sec = re.compile(r'^\[(.+)\]$') # pattern for section
|
||||||
pass
|
|
||||||
def add_microstructure(self, name=None, phase=None, texture=None):
|
name_section = ''
|
||||||
pass
|
idx_section = 0
|
||||||
|
active = False
|
||||||
|
|
||||||
|
for line in content:
|
||||||
|
line = line.split('#')[0].strip() # kill comments and extra whitespace
|
||||||
|
if line: # content survives...
|
||||||
|
match_part = re_part.match(line)
|
||||||
|
if match_part: # found <part> separator
|
||||||
|
active = (match_part.group(1) == part) # only active in <part>
|
||||||
|
continue
|
||||||
|
if active:
|
||||||
|
match_sec = re_sec.match(line)
|
||||||
|
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
|
||||||
|
|
||||||
|
if sections == [] or name_section in sections: # respect subset
|
||||||
|
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:]
|
||||||
|
|
||||||
|
|
||||||
|
def add_homogenization(self, name=None, type=None):
|
||||||
|
|
||||||
|
if type is 'isostrain':
|
||||||
|
h={'type':type}
|
||||||
|
h['ngrains']=ngrains
|
||||||
|
return h
|
||||||
|
|
||||||
|
def add_crystallite(self, name=None):
|
||||||
|
pass
|
||||||
|
def add_texture(self, name=None):
|
||||||
|
pass
|
||||||
|
def add_phase(self, name=None):
|
||||||
|
pass
|
||||||
|
def add_microstructure(self, name=None, phases=None, textures=None, fractions=None):
|
||||||
|
self.data['microstructure'][name] = {} # kill existing entry (should warn?)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def write_file(self,label=None):
|
|
||||||
fname='material.config'
|
|
||||||
if label is not None: fname+='_%s'%label
|
|
||||||
#f=open(fname)
|
|
||||||
#f.write()
|
|
||||||
#f.close()
|
|
||||||
|
|
||||||
|
|
||||||
class HOMOGENIZATION():
|
|
||||||
type=None
|
|
||||||
|
|
Loading…
Reference in New Issue