2012-11-14 16:16:51 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-11-14 16:16:51 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
import os,sys,string
|
2014-07-22 01:25:05 +05:30
|
|
|
from collections import defaultdict
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2012-11-14 16:16:51 +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-11-14 16:16:51 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
oneThird = 1.0/3.0
|
2012-11-14 16:16:51 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
def deviator(m,spherical = False): # Carefull, do not change the value of m (its intent(inout)!)
|
2012-11-14 16:16:51 +05:30
|
|
|
sph = oneThird*(m[0]+m[4]+m[8])
|
2015-08-08 00:33:26 +05:30
|
|
|
dev = [
|
|
|
|
m[0]-sph, m[1], m[2],
|
2014-09-12 18:43:05 +05:30
|
|
|
m[3], m[4]-sph, m[5],
|
2015-08-08 00:33:26 +05:30
|
|
|
m[6], m[7], m[8]-sph,
|
|
|
|
]
|
|
|
|
return dev,sph if spherical else dev
|
2012-11-14 16:16:51 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2012-11-14 16:16:51 +05:30
|
|
|
Add column(s) containing deviator of requested tensor column(s).
|
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2012-11-14 16:16:51 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-t','--tensor',
|
|
|
|
dest = 'tensor',
|
|
|
|
action = 'extend', metavar='<string LIST>',
|
|
|
|
help = 'heading of columns containing tensor field values')
|
|
|
|
parser.add_option('-s','--spherical',
|
|
|
|
dest = 'spherical',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'report spherical part of tensor (hydrostatic component, pressure)')
|
2012-11-14 16:16:51 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-05-09 18:15:30 +05:30
|
|
|
if options.tensor == None:
|
2012-11-14 16:16:51 +05:30
|
|
|
parser.error('no data column specified...')
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-18 22:54:15 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-18 22:54:15 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name, buffered = False)
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name else ''))
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
2012-11-14 16:16:51 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
items = {
|
|
|
|
'tensor': {'dim': 9, 'shape': [3,3], 'labels':options.tensor, 'active':[], 'column': []},
|
|
|
|
}
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
column = {}
|
|
|
|
|
|
|
|
for type, data in items.iteritems():
|
|
|
|
for what in data['labels']:
|
|
|
|
dim = table.label_dimension(what)
|
|
|
|
if dim != data['dim']: remarks.append('column {} is not a {}.'.format(what,type))
|
|
|
|
else:
|
|
|
|
items[type]['active'].append(what)
|
|
|
|
items[type]['column'].append(table.label_index(what))
|
|
|
|
|
|
|
|
if remarks != []: table.croak(remarks)
|
|
|
|
if errors != []:
|
|
|
|
table.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header --------------------------------------
|
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
|
|
for type, data in items.iteritems():
|
|
|
|
for label in data['active']:
|
|
|
|
table.labels_append(['{}_dev({})'.format(i+1,label) for i in xrange(data['dim'])] + \
|
|
|
|
(['sph({})'.format(label)] if options.spherical else [])) # extend ASCII header with new labels
|
2012-11-14 16:16:51 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-22 01:25:05 +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
|
|
|
for type, data in items.iteritems():
|
|
|
|
for column in data['column']:
|
|
|
|
table.data_append(deviator(map(float,table.data[column:
|
|
|
|
column+data['dim']]),options.spherical))
|
2014-07-22 01:25:05 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2012-11-14 16:16:51 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2012-11-14 16:16:51 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close input ASCII table (works for stdin)
|