2010-08-17 02:17:27 +05:30
|
|
|
#!/usr/bin/env python
|
2013-05-13 16:57:59 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2010-08-17 02:17:27 +05:30
|
|
|
'''
|
|
|
|
Writes meaningful labels to the marc input file (*.dat)
|
|
|
|
based on the files
|
|
|
|
<modelname_jobname>.output<Homogenization/Crystallite/Constitutive>
|
|
|
|
that are written during the first run of the model.
|
|
|
|
'''
|
2014-11-18 13:30:45 +05:30
|
|
|
import sys,os,re,string
|
2010-08-17 02:17:27 +05:30
|
|
|
from optparse import OptionParser
|
2014-11-18 13:30:45 +05:30
|
|
|
import damask
|
2010-08-17 02:17:27 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-07-18 18:58:54 +05:30
|
|
|
|
2010-08-17 02:17:27 +05:30
|
|
|
# -----------------------------
|
|
|
|
def ParseOutputFormat(filename,what,me):
|
|
|
|
# -----------------------------
|
|
|
|
format = {'outputs':{},'specials':{'brothers':[]}}
|
|
|
|
|
|
|
|
outputmetafile = filename+'.output'+what
|
|
|
|
try:
|
|
|
|
file = open(outputmetafile)
|
|
|
|
except:
|
|
|
|
print('Could not open file %s'%outputmetafile)
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
content = file.readlines()
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
tag = ''
|
|
|
|
tagID = 0
|
|
|
|
for line in content:
|
|
|
|
if re.match("\s*$",line) or re.match("#",line): # skip blank lines and comments
|
|
|
|
continue
|
|
|
|
m = re.match("\[(.+)\]",line) # look for block indicator
|
|
|
|
if m: # next section
|
|
|
|
tag = m.group(1)
|
|
|
|
tagID += 1
|
|
|
|
format['specials']['brothers'].append(tag)
|
|
|
|
if tag == me or (me.isdigit() and tagID == int(me)):
|
|
|
|
format['specials']['_id'] = tagID
|
|
|
|
format['outputs'] = []
|
|
|
|
tag = me
|
|
|
|
else: # data from section
|
|
|
|
if tag == me:
|
|
|
|
(output,length) = line.split()
|
|
|
|
output.lower()
|
|
|
|
if length.isdigit():
|
|
|
|
length = int(length)
|
|
|
|
if re.match("\((.+)\)",output): # special data, (e.g. (Ngrains)
|
|
|
|
format['specials'][output] = length
|
|
|
|
elif length > 0:
|
|
|
|
format['outputs'].append([output,length])
|
|
|
|
return format
|
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [options] Marc.inputfile(s)', description="""
|
2012-10-18 12:54:44 +05:30
|
|
|
Transfer the output variables requested in the material.config to
|
|
|
|
properly labelled user defined variables within the Marc input file (*.dat).
|
|
|
|
|
|
|
|
Requires the files
|
|
|
|
<modelname_jobname>.output<Homogenization/Crystallite/Constitutive>
|
|
|
|
that are written during the first run of the model.
|
|
|
|
|
|
|
|
Specify which user block format you want to apply by stating the homogenization, crystallite, and phase identifiers.
|
|
|
|
Or have an existing set of user variables copied over from another *.dat file.
|
2014-11-18 13:30:45 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
|
|
|
|
2010-08-17 02:17:27 +05:30
|
|
|
parser.add_option('-n','--number', dest='number', type='int', \
|
|
|
|
help='maximum requested User Defined Variable [%default]')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('--homogenization', dest='homog', \
|
2010-08-17 02:17:27 +05:30
|
|
|
help='homogenization identifier (as string or integer [%default])')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('--crystallite', dest='cryst', \
|
2010-08-17 02:17:27 +05:30
|
|
|
help='crystallite identifier (as string or integer [%default])')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('--phase', dest='phase', \
|
2010-08-17 02:17:27 +05:30
|
|
|
help='phase identifier (as string or integer [%default])')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('--use', dest='useFile', \
|
2010-08-17 02:17:27 +05:30
|
|
|
help='Optionally parse output descriptors from '+
|
|
|
|
'different <model_job>.outputZZZ file. Saves the effort '+
|
|
|
|
'to start a calculation for each job [%default])')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('--option', dest='damaskOption', \
|
2012-03-13 19:38:09 +05:30
|
|
|
help='Add damask option to input file '+
|
|
|
|
'for example: "periodic x z" [%default]')
|
2010-08-17 02:17:27 +05:30
|
|
|
parser.set_defaults(number = 0)
|
|
|
|
parser.set_defaults(homog = '1')
|
|
|
|
parser.set_defaults(cryst = '1')
|
|
|
|
parser.set_defaults(phase = '1')
|
|
|
|
parser.set_defaults(useFile = '')
|
2012-03-13 19:38:09 +05:30
|
|
|
parser.set_defaults(damaskOption = '')
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
(options, files) = parser.parse_args()
|
|
|
|
|
|
|
|
if not files:
|
|
|
|
parser.print_help()
|
|
|
|
parser.error('no file(s) specified...')
|
|
|
|
|
|
|
|
me = { 'Homogenization': options.homog,
|
|
|
|
'Crystallite': options.cryst,
|
|
|
|
'Constitutive': options.phase,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for file in files:
|
2013-07-18 18:58:54 +05:30
|
|
|
print '\033[1m'+scriptName+'\033[0m: '+file+'\n'
|
2010-08-17 02:17:27 +05:30
|
|
|
if options.useFile != '':
|
|
|
|
formatFile = os.path.splitext(options.useFile)[0]
|
|
|
|
else:
|
|
|
|
formatFile = os.path.splitext(file)[0]
|
|
|
|
file = os.path.splitext(file)[0]+'.dat'
|
|
|
|
if not os.path.lexists(file):
|
|
|
|
print file,'not found'
|
|
|
|
continue
|
|
|
|
|
|
|
|
print('Scanning format files of: %s'%formatFile)
|
|
|
|
|
|
|
|
if options.number < 1:
|
|
|
|
outputFormat = {}
|
|
|
|
|
|
|
|
for what in me:
|
|
|
|
outputFormat[what] = ParseOutputFormat(formatFile,what,me[what])
|
|
|
|
if not '_id' in outputFormat[what]['specials']:
|
|
|
|
print "'%s' not found in <%s>"%(me[what],what)
|
|
|
|
print '\n'.join(map(lambda x:' '+x,outputFormat[what]['specials']['brothers']))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2011-11-23 14:39:00 +05:30
|
|
|
UserVars = ['HomogenizationCount']
|
2010-08-17 02:17:27 +05:30
|
|
|
for var in outputFormat['Homogenization']['outputs']:
|
|
|
|
if var[1] > 1:
|
|
|
|
UserVars += ['%i_%s'%(i+1,var[0]) for i in range(var[1])]
|
|
|
|
else:
|
|
|
|
UserVars += ['%s'%(var[0]) for i in range(var[1])]
|
2011-11-23 14:39:00 +05:30
|
|
|
|
|
|
|
UserVars += ['GrainCount']
|
|
|
|
|
2010-08-17 02:17:27 +05:30
|
|
|
for grain in range(outputFormat['Homogenization']['specials']['(ngrains)']):
|
|
|
|
UserVars += ['%i_CrystalliteCount'%(grain+1)]
|
|
|
|
for var in outputFormat['Crystallite']['outputs']:
|
|
|
|
if var[1] > 1:
|
|
|
|
UserVars += ['%i_%i_%s'%(grain+1,i+1,var[0]) for i in range(var[1])]
|
|
|
|
else:
|
|
|
|
UserVars += ['%i_%s'%(grain+1,var[0]) for i in range(var[1])]
|
|
|
|
|
|
|
|
UserVars += ['%i_ConstitutiveCount'%(grain+1)]
|
|
|
|
for var in outputFormat['Constitutive']['outputs']:
|
|
|
|
if var[1] > 1:
|
|
|
|
UserVars += ['%i_%i_%s'%(grain+1,i+1,var[0]) for i in range(var[1])]
|
|
|
|
else:
|
|
|
|
UserVars += ['%i_%s'%(grain+1,var[0]) for i in range(var[1])]
|
|
|
|
|
|
|
|
# Now change *.dat file(s)
|
|
|
|
print('Adding labels to: %s'%file)
|
|
|
|
inFile = open(file)
|
|
|
|
input = inFile.readlines()
|
|
|
|
inFile.close()
|
|
|
|
output = open(file,'w')
|
|
|
|
thisSection = ''
|
2012-03-13 19:38:09 +05:30
|
|
|
if options.damaskOption:
|
|
|
|
output.write('$damask {0}\n'.format(options.damaskOption))
|
2010-08-17 02:17:27 +05:30
|
|
|
for line in input:
|
|
|
|
m = re.match('(\w+)\s',line)
|
|
|
|
if m:
|
|
|
|
lastSection = thisSection
|
|
|
|
thisSection = m.group(1)
|
|
|
|
if (lastSection == 'post' and thisSection == 'parameters'):
|
|
|
|
if options.number > 0:
|
|
|
|
for i in range(options.number):
|
|
|
|
output.write('%10i%10i\n'%(-i-1,0))
|
|
|
|
else:
|
|
|
|
for i in range(len(UserVars)):
|
|
|
|
output.write('%10i%10i%s\n'%(-i-1,0,UserVars[i]))
|
|
|
|
if (thisSection != 'post' or not re.match('\s*\-',line)):
|
|
|
|
output.write(line)
|
|
|
|
output.close()
|