diff --git a/moltemplate/scripts/moltemplate.sh b/moltemplate/scripts/moltemplate.sh index ec501485..219dff86 100755 --- a/moltemplate/scripts/moltemplate.sh +++ b/moltemplate/scripts/moltemplate.sh @@ -6,8 +6,8 @@ # Copyright (c) 2013 G_PROGRAM_NAME="moltemplate.sh" -G_VERSION="2.18.5" -G_DATE="2020-8-06" +G_VERSION="2.18.6" +G_DATE="2020-8-13" echo "${G_PROGRAM_NAME} v${G_VERSION} ${G_DATE}" >&2 echo "" >&2 @@ -1355,6 +1355,7 @@ IFS=$OIFS # "pair_coeff", "bond_coeff", "angle_coeff", "dihedral_coeff", "improper_coeff". # These files need to be processed further. + OUT_FILES_WITH_COEFF_COMMANDS="" for f in *.template; do HAS_COEFF_COMMANDS=`awk 'BEGIN{has_coeffs=0} {if ((NF>0)&&(($1=="pair_coeff")||($1=="bond_coeff")||($1=="angle_coeff")||($1=="dihedral_coeff")||($1=="improper_coeff"))) has_coeffs=1} END{print has_coeffs}' < "$f"` @@ -1368,7 +1369,6 @@ for f in *.template; do done - # Deal with wildcard characters ('*', '?') in "_coeff" commands # appearing in any LAMMPS input scripts generated by moltemplate. # Replace them with explicit variable names. Do this before rendering @@ -2248,6 +2248,37 @@ else fi +# Later on, it will be useful to keep track of the files that the user created. +# This must be inferred from the files ending in ".template". +# (Do this now before we discard all of the ".template" files.) + +RENDERED_FILES="" +for f in *.template; do + + # Moltemplate (ttree.py) generates two copies of each file that the + # user creates using the "write" and "write_once" commands: + # 1) The original file requested by the user. This file is a "rendered" + # file. All of the $-style and @-style variables have been replaced + # with other text (usually integers). + # 2) A version of that file that contains the original variable names + # instead of integers. (These file names end in ".template".) + # We no longer care about the files ending in ".template", but we can loop + # over files ending in ".template" created by moltemplate. We can infer + # the name of the other file name by removing the ".template" suffix. + + bn=`basename "$f" .template` #file name without the ".template" suffix + if [ ! -f "$bn" ]; then + continue + fi + if [ -n "$RENDERED_FILES" ]; then + printf -v RENDERED_FILES "${RENDERED_FILES}\n$bn" + else + RENDERED_FILES="$bn" + fi +done + + + # ############## CLEAN UP ################ # A lot of files have been created along the way. @@ -2359,7 +2390,7 @@ if [ -e "$in_prefix_no_space" ]; then mv -f "$in_prefix_no_space" output_ttree/ fi - +# ------ Postprocess coeff commands after rendering. ---------- # Swap the order of atom types I, J in all "pair_coeff I J ..." commands # whenever I > J. Do this for every input script file generated by moltemplate. # (Perhaps later I'll check to make sure the user did not specify contradictory @@ -2367,9 +2398,64 @@ fi # here, because at this point we've thrown away the original atom type names, # so there's no easy way to explain the problem to the user if there is one.) -echo "" > input_scripts_so_far.tmp +# -- First we must figure out which files we need to consider. -- +# Ugly details. We already created a variable ($RENDERED_FILES) storing a +# list of files which are created by the user. However some of those files no +# longer exist. We also need do add several files to this list and remove +# duplicates. Lacking data structures, SH and BASH are really bad at this kind +# thing, so the code is ugly. Must create temporary files and sort them. + +echo "$RENDERED_FILES" > RENDERED_FILENAMES.tmp +echo "$OUT_FILE_INIT" >> RENDERED_FILENAMES.tmp +echo "$OUT_FILE_INPUT_SCRIPT " >> RENDERED_FILENAMES.tmp +echo "$OUT_FILE_SETTINGS " >> RENDERED_FILENAMES.tmp +echo "$RENDERED_FILES_WITH_COEFF_COMMANDS" >> RENDERED_FILENAMES.tmp + +# Now remove duplicate file names +sort RENDERED_FILENAMES.tmp > RENDERED_FILENAMES.tmp2 +awk '{if ((NF>0)&&(NR>1)&&($0!=prev)) {print $0} prev=$0}' \ + < RENDERED_FILENAMES.tmp2 > RENDERED_FILENAMES.tmp3 + +# Now update the list of files stored in $RENDERED_FILES +RENDERED_FILES="" +while read file_name; do + if [ ! -f "$file_name" ]; then + continue # make sure the file exists + fi + if [ -n "$RENDERED_FILES" ]; then + printf -v RENDERED_FILES "${RENDERED_FILES}\n$file_name" + else + RENDERED_FILES="$file_name" + fi +done < RENDERED_FILENAMES.tmp3 + +rm -f RENDERED_FILENAMES.tmp* + + +# Now filter out files which do not contain "_coeff" commands +RENDERED_FILES_WITH_COEFF_COMMANDS="" +IFS=$CR +for file_name in $RENDERED_FILES; do + if [ ! -f "$file_name" ]; then + continue # make sure the file exists + fi + HAS_COEFF_COMMANDS=`awk 'BEGIN{has_coeffs=0} {if ((NF>0)&&(($1=="pair_coeff")||($1=="bond_coeff")||($1=="angle_coeff")||($1=="dihedral_coeff")||($1=="improper_coeff"))) has_coeffs=1} END{print has_coeffs}' < "$file_name"` + if [ "$HAS_COEFF_COMMANDS" -eq "1" ]; then + if [ -n "$RENDERED_FILES_WITH_COEFF_COMMANDS" ]; then + printf -v RENDERED_FILES_WITH_COEFF_COMMANDS "${RENDERED_FILES_WITH_COEFF_COMMANDS}\n$file_name" + else + RENDERED_FILES_WITH_COEFF_COMMANDS="$file_name" + fi + fi +done +IFS=$OIFS + -for file_name in "$OUT_FILE_INIT" "$OUT_FILE_INPUT_SCRIPT" "$OUT_FILE_SETTINGS"; do + +# ---- Now loop over all user-created files containing "coeff" commands. ---- +echo "" > input_scripts_so_far.tmp +IFS=$CR +for file_name in $RENDERED_FILES_WITH_COEFF_COMMANDS; do if [ -s "$file_name" ]; then echo "postprocessing file \"$file_name\"" >&2 if ! $PYTHON_COMMAND "${PY_SCR_DIR}/postprocess_input_script.py" input_scripts_so_far.tmp < "$file_name" > "$file_name.tmp"; then @@ -2404,6 +2490,7 @@ for file_name in "$OUT_FILE_INIT" "$OUT_FILE_INPUT_SCRIPT" "$OUT_FILE_SETTINGS"; fi fi done +IFS=$OIFS # Process any custom input script files created by the user afterwards diff --git a/setup.py b/setup.py index 0bc45b18..427fcf3f 100644 --- a/setup.py +++ b/setup.py @@ -45,9 +45,9 @@ url='https://github.com/jewettaij/moltemplate', - download_url='https://github.com/jewettaij/moltemplate/archive/v2.18.5.zip', + download_url='https://github.com/jewettaij/moltemplate/archive/v2.18.6.zip', - version='2.18.5', + version='2.18.6', keywords=['simulation', 'LAMMPS', 'molecule editor', 'molecule builder', 'ESPResSo'],