2019-02-15 03:26:39 +05:30
|
|
|
#!/usr/bin/env python3
|
2017-11-28 21:29:53 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2019-12-22 00:48:33 +05:30
|
|
|
from io import StringIO
|
2017-11-28 21:29:53 +05:30
|
|
|
from optparse import OptionParser
|
2019-06-14 16:33:30 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2017-11-28 21:29:53 +05:30
|
|
|
import damask
|
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2017-11-28 21:29:53 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
|
|
|
|
|
|
|
def derivative(coordinates,what):
|
2020-09-15 10:28:06 +05:30
|
|
|
|
2017-11-28 21:29:53 +05:30
|
|
|
result = np.empty_like(what)
|
2020-09-15 10:28:06 +05:30
|
|
|
|
2017-11-28 21:29:53 +05:30
|
|
|
# use differentiation by interpolation
|
|
|
|
# as described in http://www2.math.umd.edu/~dlevy/classes/amsc466/lecture-notes/differentiation-chap.pdf
|
|
|
|
|
|
|
|
result[1:-1,:] = + what[1:-1,:] * (2.*coordinates[1:-1]-coordinates[:-2]-coordinates[2:]) / \
|
|
|
|
((coordinates[1:-1]-coordinates[:-2])*(coordinates[1:-1]-coordinates[2:])) \
|
|
|
|
+ what[2:,:] * (coordinates[1:-1]-coordinates[:-2]) / \
|
|
|
|
((coordinates[2:]-coordinates[1:-1])*(coordinates[2:]-coordinates[:-2])) \
|
|
|
|
+ what[:-2,:] * (coordinates[1:-1]-coordinates[2:]) / \
|
|
|
|
((coordinates[:-2]-coordinates[1:-1])*(coordinates[:-2]-coordinates[2:])) \
|
|
|
|
|
|
|
|
result[0,:] = (what[0,:] - what[1,:]) / \
|
|
|
|
(coordinates[0] - coordinates[1])
|
|
|
|
result[-1,:] = (what[-1,:] - what[-2,:]) / \
|
|
|
|
(coordinates[-1] - coordinates[-2])
|
2020-09-15 10:28:06 +05:30
|
|
|
|
2017-11-28 21:29:53 +05:30
|
|
|
return result
|
2019-06-14 16:33:30 +05:30
|
|
|
|
|
|
|
|
2017-11-28 21:29:53 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-16 22:11:56 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2017-11-28 21:29:53 +05:30
|
|
|
Add column(s) containing numerical derivative of requested column(s) with respect to given coordinates.
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
|
|
|
parser.add_option('-c','--coordinates',
|
|
|
|
dest = 'coordinates',
|
|
|
|
type = 'string', metavar='string',
|
|
|
|
help = 'heading of coordinate column')
|
|
|
|
parser.add_option('-l','--label',
|
2019-12-22 00:48:33 +05:30
|
|
|
dest = 'labels',
|
2017-11-28 21:29:53 +05:30
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'heading of column(s) to differentiate')
|
|
|
|
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
2019-12-22 00:48:33 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2017-11-28 21:29:53 +05:30
|
|
|
|
|
|
|
if options.coordinates is None:
|
2019-12-22 00:48:33 +05:30
|
|
|
parser.error('no coordinate column specified.')
|
|
|
|
if options.labels is None:
|
|
|
|
parser.error('no data column specified.')
|
2017-11-28 21:29:53 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2019-12-22 00:48:33 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2017-11-28 21:29:53 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
table = damask.Table.load_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
2019-12-22 00:48:33 +05:30
|
|
|
for label in options.labels:
|
2020-09-14 10:34:01 +05:30
|
|
|
table = table.add('d({})/d({})'.format(label,options.coordinates),
|
|
|
|
derivative(table.get(options.coordinates),table.get(label)),
|
|
|
|
scriptID+' '+' '.join(sys.argv[1:]))
|
2017-11-28 21:29:53 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
table.save_ASCII(sys.stdout if name is None else name)
|