2012-06-12 19:39:16 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
import os,sys,string
|
2014-07-25 03:12:13 +05:30
|
|
|
import numpy as np
|
|
|
|
from collections import defaultdict
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2012-06-12 19:39:16 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-07-25 03:12:13 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2014-08-06 18:57:09 +05:30
|
|
|
Add column(s) containing Second Piola--Kirchhoff stress based on given column(s) of deformation gradient and first Piola--Kirchhoff stress.
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-f','--defgrad',
|
|
|
|
dest = 'defgrad',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'heading of columns containing deformation gradient [%default]')
|
|
|
|
parser.add_option('-p','--stress',
|
|
|
|
dest = 'stress',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'heading of columns containing first Piola--Kirchhoff stress [%default]')
|
|
|
|
|
|
|
|
parser.set_defaults(defgrad = 'f',
|
|
|
|
stress = 'p',
|
|
|
|
)
|
2012-06-12 19:39:16 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = ['STDIN']
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
|
|
table = damask.ASCIItable(name = name, outname = name+'_tmp',
|
|
|
|
buffered = False)
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
column = {}
|
|
|
|
|
|
|
|
for tensor in [options.defgrad,options.stress]:
|
|
|
|
dim = table.label_dimension(tensor)
|
|
|
|
if dim < 0: errors.append('column {} not found.'.format(tensor))
|
|
|
|
elif dim != 9: errors.append('column {} is not a tensor.'.format(tensor))
|
|
|
|
else:
|
|
|
|
column[tensor] = table.label_index(tensor)
|
|
|
|
|
|
|
|
if errors != []:
|
|
|
|
table.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2015-05-10 03:02:23 +05:30
|
|
|
# ------------------------------------------ assemble header --------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2015-05-10 16:59:11 +05:30
|
|
|
table.labels_append(['%i_S'%(i+1) for i in xrange(9)]) # extend ASCII header with new labels
|
2012-06-12 19:39:16 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2014-07-25 03:12:13 +05:30
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2015-08-08 00:33:26 +05:30
|
|
|
F = np.array(map(float,table.data[column[options.defgrad]:column[options.defgrad]+9]),'d').reshape(3,3)
|
|
|
|
P = np.array(map(float,table.data[column[options.stress ]:column[options.stress ]+9]),'d').reshape(3,3)
|
2014-07-25 03:12:13 +05:30
|
|
|
table.data_append(list(np.dot(np.linalg.inv(F),P).reshape(9))) # [S] =[P].[F-1]
|
|
|
|
outputAlive = table.data_write() # output processed line
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2012-06-12 19:39:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close input ASCII table (works for stdin)
|
|
|
|
if name != 'STDIN': os.rename(name+'_tmp',name) # overwrite old one with tmp new
|