From b67b5f9ef97df054bf35550e7505d3cd156b85e3 Mon Sep 17 00:00:00 2001 From: Claudio Zambaldi Date: Mon, 21 Nov 2011 18:01:08 +0000 Subject: [PATCH] improvements! --- lib/damask_tools.py | 62 +++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/damask_tools.py b/lib/damask_tools.py index c725514c0..824baac95 100644 --- a/lib/damask_tools.py +++ b/lib/damask_tools.py @@ -10,10 +10,11 @@ class DAMASK_TOOLS(): else: return True - class MATERIAL_CONFIG(): __slots__ = ['data'] + + def __init__(self): self.parts = [ 'homogenization', @@ -21,7 +22,7 @@ class MATERIAL_CONFIG(): 'crystallite', 'phase', 'texture', - ] + ] # ordered (!) list of parts self.data = {\ 'homogenization': {'__order__': []}, 'microstructure': {'__order__': []}, @@ -33,18 +34,18 @@ class MATERIAL_CONFIG(): def __repr__(self): me = [] for part in self.parts: + print 'doing',part me += ['','#-----------------------------#','<%s>'%part,'#-----------------------------#',] for section in self.data[part]['__order__']: - me += ['','[%s]\t\t#-----------------------'%section,'',] + me += ['','[%s] %s'%(section,'-'*max(0,27-len(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]))] + me += ['%s\t%s'%(key,' '.join(map(str,self.data[part][section][key])))] return '\n'.join(me) - def parse_data(self, part=None, sections=[], content=None): re_part = re.compile(r'^<(.+)>$') # pattern for part @@ -92,22 +93,30 @@ class MATERIAL_CONFIG(): if os.path.exists(file): i=1 while os.path.exists(file+'_%i'%i):i+=1 - file=file+'_%i'%i + file+='_%i'%i print('Writing material data to file %s'%file) f=open(file,'w') f.write(str(self)) f.close() - + + + def add_data(self, part=None, section=None, data={}): + if part not in self.parts: raise Exception('invalid part %s'%part) + if section not in self.data[part]: self.data[part]['__order__'] += [section] + self.data[part][section] = data + + def add_homogenization(self, label='', type='', Ngrains=None): - old_len=len(self.data['homogenization']) - if type is 'isostrain': - h={'type':[type]} - h['Ngrains']=[Ngrains] - h['__order__']=['type','Ngrains'] - #return h - self.data['homogenization'][label]=h - if len(self.data['homogenization'])>old_len: # added new label - self.data['homogenization']['__order__'].append(label) + if type.lower() == 'isostrain': + self.add_data(part='homogenization', + section=label, + data={'type':[type], + 'Ngrains':[Ngrains], + '__order__':['type','Ngrains'] + } + ) + elif type.lower() == 'rgc': + raise Exception('Please implement me') def add_crystallite(self, label='', output=[]): old_len=len(self.data['crystallite']) @@ -118,7 +127,7 @@ class MATERIAL_CONFIG(): def add_texture(self, label='',type='', eulers=[], scatter=0., fraction=1.): ''' Experimental! Needs expansion to multi-component textures...''' old_len=len(self.data['texture']) - if type is '(gauss)': + if type == '(gauss)': gauss={type:[['phi1',eulers[0],'Phi',eulers[1], 'phi2',eulers[2],'scatter',scatter, 'fraction',fraction]],'__order__':label} self.data['texture'][label]=gauss if len(self.data['texture'])>old_len: # added new label @@ -130,16 +139,13 @@ class MATERIAL_CONFIG(): OR -phase is dict with one key ''' - import copy print(file,label,phase) old_len=len(self.data['phase']) if file and label and (phase is None): - data_so_far = copy.deepcopy(self.data) - self.__init__() - self.read(file=file) - phase={label:self.data['phase'][label]} + other=MATERIAL_CONFIG() + other.read(file=file) + phase={label:other.data['phase'][label]} label=None - self.data=copy.deepcopy(data_so_far) print phase if len(phase)==1 and label is None: print('Adding phase %s'%phase.keys()[0]) @@ -150,8 +156,8 @@ class MATERIAL_CONFIG(): else: raise Exception('Wrong arguments') - def add_microstructure(self, crystallite=None, - label=None, + def add_microstructure(self, label=None, + crystallite=None, phases=None, textures=None, fractions=None): @@ -169,11 +175,7 @@ class MATERIAL_CONFIG(): '__order__':['crystallite','(constituent)']} if len(self.data['microstructure'])>old_len: # added new label self.data['microstructure']['__order__'].append(label) - -class HOMOGENIZATION(): - def __init__(self): - pass - +