simplified option parsing (so far, only for addCalculation and addMises)

This commit is contained in:
Martin Diehl 2014-06-17 07:10:10 +00:00
parent 3533138936
commit 51b92f3ef9
4 changed files with 36 additions and 51 deletions

View File

@ -13,6 +13,7 @@ from .result import Result # only one class
from .geometry import Geometry # one class with subclasses from .geometry import Geometry # one class with subclasses
from .solver import Solver # one class with subclasses from .solver import Solver # one class with subclasses
from .test import Test from .test import Test
from .util import extendableOption
try: try:
from . import core from . import core

View File

@ -2,6 +2,8 @@
# damask utility functions # damask utility functions
import math import math
from optparse import OptionParser, Option
try: try:
import numpy import numpy
except: except:
@ -16,3 +18,21 @@ for f in ['cos', 'sin', 'tan']:
exec('def %sd(deg): return (math.%s(deg/180.*math.pi))'%(f,f)) exec('def %sd(deg): return (math.%s(deg/180.*math.pi))'%(f,f))
exec('def a%sd(val): return (math.a%s(val)*180./math.pi)'%(f,f)) exec('def a%sd(val): return (math.a%s(val)*180./math.pi)'%(f,f))
# -----------------------------
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)

View File

@ -1,8 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
import os,re,sys,math,string,damask,numpy import os,re,sys,math,string
from optparse import OptionParser, Option import numpy as np
from optparse import OptionParser
import damask
scriptID = '$Id$' scriptID = '$Id$'
scriptName = scriptID.split()[1] scriptName = scriptID.split()[1]
@ -11,31 +13,11 @@ def unravel(item):
if hasattr(item,'__contains__'): return ' '.join(map(unravel,item)) if hasattr(item,'__contains__'): return ' '.join(map(unravel,item))
else: return str(item) else: return str(item)
# -----------------------------
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 # MAIN
# -------------------------------------------------------------------- # --------------------------------------------------------------------
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """ parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Add column(s) with derived values according to user defined arithmetic operation between column(s). Add column(s) with derived values according to user defined arithmetic operation between column(s).
Columns can be specified either by label or index. Use ';' for ',' in functions. Columns can be specified either by label or index. Use ';' for ',' in functions.
@ -113,7 +95,7 @@ for file in files:
table.data_read() table.data_read()
labelLen = {} labelLen = {}
for label in options.labels: for label in options.labels:
labelLen[label] = numpy.size(eval(eval(evaluator[label]))) labelLen[label] = np.size(eval(eval(evaluator[label])))
# ------------------------------------------ assemble header --------------------------------------- # ------------------------------------------ assemble header ---------------------------------------
for label,formula in zip(options.labels,options.formulas): for label,formula in zip(options.labels,options.formulas):

View File

@ -1,37 +1,19 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
import os,re,sys,math,numpy,string,damask import os,re,sys,math,string
from optparse import OptionParser, Option import numpy as np
from optparse import OptionParser
import damask
scriptID = '$Id$' scriptID = '$Id$'
scriptName = scriptID.split()[1] scriptName = scriptID.split()[1]
# -----------------------------
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)
def Mises(what,tensor): def Mises(what,tensor):
dev = tensor - numpy.trace(tensor)/3.0*numpy.eye(3) dev = tensor - np.trace(tensor)/3.0*np.eye(3)
symdev = 0.5*(dev+dev.T) symdev = 0.5*(dev+dev.T)
return math.sqrt(numpy.sum(symdev*symdev.T)* return math.sqrt(np.sum(symdev*symdev.T)*
{ {
'stress': 3.0/2.0, 'stress': 3.0/2.0,
'strain': 2.0/3.0, 'strain': 2.0/3.0,
@ -41,7 +23,7 @@ def Mises(what,tensor):
# MAIN # MAIN
# -------------------------------------------------------------------- # --------------------------------------------------------------------
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """ parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Add vonMises equivalent values for symmetric part of requested strains and/or stresses. Add vonMises equivalent values for symmetric part of requested strains and/or stresses.
""" + string.replace(scriptID,'\n','\\n') """ + string.replace(scriptID,'\n','\\n')
@ -120,7 +102,7 @@ for file in files:
for datatype,labels in active.items(): # loop over vector,tensor for datatype,labels in active.items(): # loop over vector,tensor
for label in labels: # loop over all requested norms for label in labels: # loop over all requested norms
table.data_append(Mises(datatype, table.data_append(Mises(datatype,
numpy.array(map(float,table.data[column[datatype][label]: np.array(map(float,table.data[column[datatype][label]:
column[datatype][label]+datainfo[datatype]['len']]),'d').reshape(3,3))) column[datatype][label]+datainfo[datatype]['len']]),'d').reshape(3,3)))
table.data_write() # output processed line table.data_write() # output processed line