2012-02-07 18:39:10 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
import os,sys,string
|
2014-07-10 14:57:51 +05:30
|
|
|
import numpy as np
|
2014-07-09 15:37:24 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2011-06-21 21:55:48 +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]
|
2014-02-26 20:26:06 +05:30
|
|
|
|
2011-06-21 21:55:48 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-07-09 15:37:24 +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 Cauchy stress based on given column(s) of deformation gradient and first Piola--Kirchhoff stress.
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2011-06-21 21:55:48 +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',
|
|
|
|
)
|
2011-06-21 21:55:48 +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 ------------------------------------------
|
2011-06-21 21:55:48 +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
|
2015-05-09 18:15:30 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header --------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2014-08-04 23:23:41 +05:30
|
|
|
table.labels_append(['%i_Cauchy'%(i+1) for i in xrange(9)]) # extend ASCII header with new labels
|
2011-11-24 20:18:02 +05:30
|
|
|
table.head_write()
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2014-02-26 20:26:06 +05:30
|
|
|
outputAlive = True
|
2014-07-10 14:57:51 +05:30
|
|
|
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-10 14:57:51 +05:30
|
|
|
table.data_append(list(1.0/np.linalg.det(F)*np.dot(P,F.T).reshape(9))) # [Cauchy] = (1/det(F)) * [P].[F_transpose]
|
|
|
|
outputAlive = table.data_write() # output processed line
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2011-06-21 21:55:48 +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
|