From 8a6669300115acdedfd1901097447601615e358c Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 5 Mar 2013 16:31:17 +0000 Subject: [PATCH] added script to convert data used by R.A. Lebensohn to spectral solver (geom, material.config) format --- processing/pre/geom_fromAng.py | 12 +-- processing/pre/geom_fromVPSC.py | 125 +++++++++++++++++++++++++ processing/setup/symLink_Processing.py | 1 + 3 files changed, 132 insertions(+), 6 deletions(-) create mode 100755 processing/pre/geom_fromVPSC.py diff --git a/processing/pre/geom_fromAng.py b/processing/pre/geom_fromAng.py index a58812c84..c43d091c1 100755 --- a/processing/pre/geom_fromAng.py +++ b/processing/pre/geom_fromAng.py @@ -34,17 +34,17 @@ Dual phases can be discriminated based on threshold value in a given data column parser.add_option('--column', dest='column', type='int', \ - help='data column to separate phase 1 and 2 [%default]') -parser.add_option('-f','--threshold', dest='threshold', type='float', \ - help='threshold value to discriminate phase 1 from 2') + help='data column to discriminate phase 1 from 2 [%default]') +parser.add_option('-t','--threshold', dest='threshold', type='float', \ + help='threshold value to discriminate phase 1 from 2 [%default]') parser.add_option('--homogenization', dest='homogenization', type='int', \ - help='homogenization index to be used') + help='homogenization index to be used [%default]') parser.add_option('--phase', dest='phase', type='int', nargs = 2, \ help='two phase indices to be used %default') parser.add_option('--crystallite', dest='crystallite', type='int', \ - help='crystallite index to be used') + help='crystallite index to be used [%default]') parser.add_option('-c', '--configuration', dest='config', action='store_true', \ - help='output material configuration') + help='output material configuration [%default]') parser.set_defaults(column = 1) parser.set_defaults(threshold = 0.0) diff --git a/processing/pre/geom_fromVPSC.py b/processing/pre/geom_fromVPSC.py new file mode 100755 index 000000000..cd84c6330 --- /dev/null +++ b/processing/pre/geom_fromVPSC.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python + +import os,sys,math,string,numpy +from optparse import OptionParser, Option + +# ----------------------------- +class extendableOption(Option): +# ----------------------------- +# used for definition of new option parser action 'extend', which enables to take multiple option arguments +# taken from online tutorial http://docs.python.org/library/optparse.html + + ACTIONS = Option.ACTIONS + ("extend",) + STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) + TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) + ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) + + def take_action(self, action, dest, opt, value, values, parser): + if action == "extend": + lvalue = value.split(",") + values.ensure_value(dest, []).extend(lvalue) + else: + Option.take_action(self, action, dest, opt, value, values, parser) + + +# -------------------------------------------------------------------- +# MAIN +# -------------------------------------------------------------------- + +parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """ +Generate geometry description and material configuration from input files used by R.A. Lebensohn +""" + string.replace('$Id$','\n','\\n') +) + + +parser.add_option('--column', dest='column', type='int', \ + help='data column to discriminate phase 1 from 2 [%default]') +parser.add_option('-t','--treshold', dest='threshold', type='float', \ + help='threshold value to discriminate phase 1 from 2 [%default]') +parser.add_option('--homogenization', dest='homogenization', type='int', \ + help='homogenization index to be used [%default]') +parser.add_option('--phase', dest='phase', type='int', nargs = 2, \ + help='two phase indices to be used %default') +parser.add_option('--crystallite', dest='crystallite', type='int', \ + help='crystallite index to be used [%default]') +parser.add_option('-c', '--configuration', dest='config', action='store_true', \ + help='output material configuration [%default]') + +parser.set_defaults(column = 7) +parser.set_defaults(threshold = 1.0) +parser.set_defaults(homogenization = 1) +parser.set_defaults(phase = [1,2]) +parser.set_defaults(crystallite = 1) +parser.set_defaults(config = False) + +(options,filenames) = parser.parse_args() + +# ------------------------------------------ setup file handles --------------------------------------- + +files = [] +if filenames == []: + files.append({'name':'STDIN', + 'input':sys.stdin, + 'output':sys.stdout, + 'croak':sys.stderr, + }) +else: + for name in filenames: + if os.path.exists(name): + files.append({'name':name, + 'input':open(name), + 'output':open(name+'_tmp','w'), + 'croak':sys.stdout, + }) + + +# ------------------------------------------ loop over input files --------------------------------------- + +for file in files: + if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n') + + point = 0 + geomdim = [0.0,0.0,0.0] + needInfo = [True,True,True] + resolution = [0,0,0] + microstructure = [''] + texture = [''] + + for line in file['input']: + if line.strip(): + point += 1 + words = line.split() + if options.config: # write configuration (line by line) + me = str(point) + microstructure += ['[Grain%s]\n'%me + \ + 'crystallite\t%i\n'%options.crystallite + \ + '(constituent)\tphase %s\ttexture %s\tfraction 1.0\n'%(options.phase[{True:0,False:1}[float(words[options.column-1]) geomdim[i]: + geomdim[i] = currPos[i] + resolution[i]+=1 + + if options.config: + file['output'].write('\n'.join(microstructure) + \ + '\n'.join(texture)) + else: + file['output'].write("4 header\n" + \ + "resolution\ta %i\tb %i\tc %i\n"%tuple(resolution) + \ + "dimension\tx %g\ty %g\tz %g\n"%tuple(geomdim) + \ + "origin\tx 0\ty 0\tz 0\n" + \ + "homogenization %i\n"%options.homogenization + \ + "1 to %i\n"%(resolution[0]*resolution[1]*resolution[2])) + + # ------------------------------------------ output finalization --------------------------------------- + + if file['name'] != 'STDIN': + file['output'].close() + os.rename(file['name']+'_tmp',os.path.splitext(file['name'])[0] + \ + {True: '_material.config', + False:'.geom'}[options.config]) diff --git a/processing/setup/symLink_Processing.py b/processing/setup/symLink_Processing.py index 938c7b69b..59037bd2b 100755 --- a/processing/setup/symLink_Processing.py +++ b/processing/setup/symLink_Processing.py @@ -18,6 +18,7 @@ bin_link = { \ 'patchFromReconstructedBoundaries.py', 'randomSeeding.py', 'geom_fromAng.py', + 'geom_fromVPSC.py', 'geom_fromMinimalSurface.py', 'geom_fromVoronoiTessellation.py', 'geom_Osteon.py',