now all methods use generic add_data

added change_value method
This commit is contained in:
Claudio Zambaldi 2011-11-23 15:21:16 +00:00
parent 48f8d4ccb5
commit 205bb49b34
1 changed files with 63 additions and 39 deletions

View File

@ -231,14 +231,14 @@ class MATERIAL_CONFIG():
f=open(file,'w') f=open(file,'w')
f.write(str(self)) f.write(str(self))
f.close() f.close()
return file
def add_data(self, part=None, section=None, data={}): def add_data(self, part=None, section=None, data={}):
'''Generic data adding/updating'''
if part not in self.parts: raise Exception('invalid part %s'%part) if part not in self.parts: raise Exception('invalid part %s'%part)
if section not in self.data[part]: self.data[part]['__order__'] += [section] if section not in self.data[part]: self.data[part]['__order__'] += [section]
self.data[part][section] = data self.data[part][section] = data
def add_homogenization(self, label='', type='', Ngrains=None): def add_homogenization(self, label='', type='', Ngrains=None):
if type.lower() == 'isostrain': if type.lower() == 'isostrain':
self.add_data(part='homogenization', self.add_data(part='homogenization',
@ -252,28 +252,28 @@ class MATERIAL_CONFIG():
raise Exception('Please implement me') raise Exception('Please implement me')
def add_crystallite(self, label='', output=[]): def add_crystallite(self, label='', output=[]):
old_len=len(self.data['crystallite']) self.add_data(part='crystallite',
self.data['crystallite'][label]={'(output)':[[o] for o in output],'__order__':'(output)'} section=label,
if len(self.data['crystallite'])>old_len: # added new label data={'(output)':[[o] for o in output],
self.data['crystallite']['__order__'].append(label) '__order__':'(output)'})
def add_texture(self, label='',type='', eulers=[], scatter=0., fraction=1.): def add_texture(self, label='',type='', eulers=[], scatter=0., fraction=1.):
''' Experimental! Needs expansion to multi-component textures...''' ''' Experimental! Needs expansion to multi-component textures...'''
old_len=len(self.data['texture'])
if type == '(gauss)': if type == '(gauss)':
gauss={type:[['phi1',eulers[0],'Phi',eulers[1], 'phi2',eulers[2],'scatter',scatter, 'fraction',fraction]],'__order__':label} texture={type:[['phi1','%f'%float(eulers[0]),'Phi','%f'%float(eulers[1]), 'phi2','%f'%float(eulers[2]),'scatter','%f'%float(scatter), 'fraction','%f'%fraction]],'__order__':label}
self.data['texture'][label]=gauss #self.data['texture'][label]=texture
if len(self.data['texture'])>old_len: # added new label #if len(self.data['texture'])>old_len: # added new label
self.data['texture']['__order__'].append(label) # self.data['texture']['__order__'].append(label)
else:
raise Exception('Please implement me.')
self.add_data(part='texture',section=label, data=texture)
def add_phase(self, file='', label='', phase=None): def add_phase(self, file='', label='', newlabel=None, phase=None):
''' USAGE: ''' USAGE:
- read phase "label" from file - read phase "label" from file
OR OR
- phase is dict with one key - phase is dict with one key
''' '''
print(file,label,phase)
old_len=len(self.data['phase'])
if file and label and (phase is None): if file and label and (phase is None):
other=MATERIAL_CONFIG() other=MATERIAL_CONFIG()
other.read(file=file) other.read(file=file)
@ -281,33 +281,57 @@ class MATERIAL_CONFIG():
label=None label=None
print phase print phase
if len(phase)==1 and label is None: if len(phase)==1 and label is None:
print('Adding phase %s'%phase.keys()[0]) if newlabel:
label=newlabel
else:
label=phase.keys()[0] label=phase.keys()[0]
self.data['phase'][label]=phase[label] print('Adding phase %s'%label)
if len(self.data['phase'])>old_len: # added new label self.add_data(part='phase', section=label, data=phase)
self.data['phase']['__order__'].append(label)
else: else:
raise Exception('Wrong arguments') raise Exception('Wrong arguments')
def add_microstructure(self, label=None, def add_microstructure(self, label=None,
crystallite=None, crystallite=None, # label
phases=None, phases=None, # list of labels
textures=None, textures=None, # list of labels
fractions=None): fractions=None): # list of floats
''' Experimental! Needs expansion to multi-constituent microstructures...''' ''' Experimental! Needs expansion to multi-constituent microstructures...'''
old_len=len(self.data['microstructure'])
c=self.data['crystallite']['__order__'].index(crystallite)+1 c=self.data['crystallite']['__order__'].index(crystallite)+1
constituent=phases[:] constituent=phases[:]
if fractions is None:
fractions=[1./len(phases)]*len(phases)
for i in range(len(phases)): for i in range(len(phases)):
p=self.data['phase']['__order__'].index(phases[i])+1 p=self.data['phase']['__order__'].index(phases[i])+1
t=self.data['texture']['__order__'].index(textures[i])+1 t=self.data['texture']['__order__'].index(textures[i])+1
f=fractions[i] f=fractions[i]
constituent[i]=['phase','%i'%p,'texture','%i'%t,'fraction','%f'%f] constituent[i]=['phase','%i'%p,'texture','%i'%t,'fraction','%f'%f]
self.data['microstructure'][label]={'crystallite':['%i'%c], data={'crystallite':['%i'%c],
'(constituent)':constituent, '(constituent)':constituent,
'__order__':['crystallite','(constituent)']} '__order__':['crystallite','(constituent)']}
if len(self.data['microstructure'])>old_len: # added new label self.add_data(part='microstructure',section=label,data=data)
self.data['microstructure']['__order__'].append(label)
def change_value(self,part=None,section=None,key=None,value=None):
if type(value) is not type([]):
if type(value) is not type('s'):
value='%s'%value
value=[value]
newlen=len(value)
oldval=self.data[part][section][key]
oldlen=len(oldval)
print('changing %s:%s:%s:%s'%(part,section,key,oldval))
self.data[part][section][key]=value
print('new: %s'%self.data[part][section][key])
if newlen is not oldlen:
print('Length of value was changed from %i to %i!'%(oldlen,newlen))