Last active
August 10, 2017 16:51
-
-
Save tueda/73ca3589793bba1b6e73 to your computer and use it in GitHub Desktop.
Runs qgraf in a one-liner. #bin #sh #qgraf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# | |
# @qgraf.sh | |
# | |
# Runs qgraf in a one-liner. The output file goes to the standard output, while | |
# the qgraf output goes to the standard error. | |
# | |
set -eu | |
# The program name. | |
prog=`basename "$0"` | |
# Wraps "mktemp -d $1XXXXXXXXXX" | |
mktemp_d() {( | |
umask 077 | |
{ | |
# Use mktemp if available. | |
dir=`mktemp -d "$1XXXXXXXXXX" 2>/dev/null` && [ -d "$dir" ] | |
} || { | |
# Fall back on mkdir. | |
dir= | |
i=0 | |
while [ $i -lt 100 ]; do | |
next_dir=$1$$$i$RANDOM$RANDOM | |
if mkdir "$next_dir" >/dev/null 2>&1; then | |
dir=$next_dir | |
break | |
fi | |
i=`expr $i + 1` | |
done | |
[ "x$dir" != x ] && [ -d "$dir" ] | |
} || { | |
echo "$prog: failed to create a temporary directory" >&2 | |
exit 1 | |
} | |
echo "$dir" | |
exit 0 | |
)} | |
# Prints the absolute path. | |
abs_path() {( | |
if [ ! -f "$1" ]; then | |
echo "$prog: \"$1\" not found" >&2 | |
exit 1 | |
fi | |
rel_dir=`dirname "$1"` | |
abs_dir=`cd "$rel_dir"; pwd` | |
fname=`basename "$1"` | |
echo "$abs_dir/$fname" | |
)} | |
# Prints the usage. | |
print_usage() { | |
cat << END | |
Usage: | |
$prog style=<style-file> model=<model-file> | |
in=<in-particles..> out=<out-particles..> loops=<number-of_loops> | |
[loop_momentum=<prefix>] [options=<options..>] [<logicals..>] | |
Options include: | |
onepi, onshell, nosigma, nosnail, notadpole, simple, | |
onepr, offshell, sigma, snail, tadpole, notsimple, | |
floop, topol | |
<logical>=<operator>[<arg1>,...,<arg_k>] | |
where <logical> is true or false and <operator> is one of | |
bridge, chord, iprop, rbridge, sbridge, vsum, psum | |
Example: | |
$ $prog style=form.sty model=qcd in=qua out=qua,glu loops=5 \\ | |
options=notadpole,onshell,nosnail | |
END | |
} | |
# Determine the path to qgraf. | |
if [ -z "${QGRAF:-}" ]; then | |
if [ -f ./qgraf ]; then | |
# Priority goes to qgraf in the current directory. | |
QGRAF=./qgraf | |
else | |
# Hopefully in $PATH. | |
QGRAF=`command -v qgraf || :` | |
fi | |
fi | |
# Convert it into the absolute path when it looks like a relative path. | |
case "$QGRAF" in | |
*/*) | |
if [ ! -f "$QGRAF" ]; then | |
echo "$prog: \"$QGRAF\" not found" >&2 | |
exit 1 | |
fi | |
QGRAF=`abs_path "$QGRAF"` | |
;; | |
esac | |
# Parse the command line arguments. | |
opt_style= | |
opt_model= | |
opt_in= | |
opt_out= | |
opt_loops= | |
opt_loop_momentum= | |
opt_options= | |
opt_logicals= | |
for opt_arg in "$@"; do | |
case $opt_arg in | |
style=*|model=*|in=*|out=*|loops=*|loop_momentum=*|options=*) | |
opt_var=`expr "$opt_arg" : '\([^=]*\)=.*' | sed 's/-/_/g'` | |
opt_val=`{ expr "$opt_arg" : '[^=]*=\(.*\)' || :; }` | |
eval "opt_$opt_var=\"\$opt_val\"" | |
;; | |
true=*|false=*) | |
opt_logicals="$opt_logicals$opt_arg; | |
" | |
;; | |
*) | |
echo "$prog: unrecognized option: $opt_arg" >&2 | |
print_usage >&2 | |
exit 1 | |
;; | |
esac | |
done | |
# Check the arguments. | |
for a in style model in out loops; do | |
if [ -z `eval echo "\\$opt_$a"` ]; then | |
echo "$prog: forgotten \"$a\". It must have a value" >&2 | |
print_usage >&2 | |
exit 1 | |
fi | |
done | |
opt_style=`abs_path "$opt_style"` | |
opt_model=`abs_path "$opt_model"` | |
# Make a temporary directory. | |
tmpdir=`mktemp_d "${TMPDIR:-/tmp}/qgraf"` | |
trap 'rm -rf "$tmpdir"' 0 1 2 13 15 | |
# Make links to the style and model files. | |
ln -s "$opt_style" "$tmpdir" | |
ln -s "$opt_model" "$tmpdir" | |
# Move to the temporary directory. | |
( | |
cd "$tmpdir" | |
# Write qgraf.dat. | |
cat << END >qgraf.dat | |
output = 'qgraf.out'; | |
style = '`basename "$opt_style"`'; | |
model = '`basename "$opt_model"`'; | |
in = $opt_in; | |
out = $opt_out; | |
loops = $opt_loops; | |
loop_momentum = $opt_loop_momentum; | |
options = $opt_options; | |
$opt_logicals | |
END | |
# Run qgraf. | |
$QGRAF >&2 | |
# qgraf always returns with an exit code of 0. Check if the output file was | |
# actually written. | |
if [ ! -f qgraf.out ]; then | |
echo "$prog: qgraf failed" >&2 | |
exit 1 | |
fi | |
# Print the output file. | |
cat qgraf.out | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment