unipbs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/sh -efu
  2. #
  3. # The 'unipbs' allows users to launch a specified UrQMD input file
  4. # in the Portable Batch System. It creates and spawns a number of jobs
  5. # at the node.
  6. #
  7. # Copyright (C) 2019 Nikita (arei) Ermakov <coffe92@gmail.com>
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. #
  22. PROG="unipbs"
  23. show_help()
  24. {
  25. cat <<EOF
  26. $PROG - create and spawn jobs to Portable Batch System
  27. Usage: $PROG <options> -- [<PBS options>]
  28. Options:
  29. -j --jobs number of jobs
  30. -i --input input file path
  31. -o --output output directory to place files (default: out)
  32. -u --urqmd path to the UrQMD executable
  33. -g --unigen path to the urqmd2u converter executable
  34. -f --fake do not submit jobs, just create tree structure
  35. -n --nowait do not wait a few seconds before building and running jobs
  36. -h --help print this help
  37. PBS options: this optional arguments will be transfered directly to the qsub command.
  38. EOF
  39. exit 0
  40. }
  41. fatal()
  42. {
  43. echo "$@"
  44. exit 1
  45. }
  46. # Parse input arguments
  47. if [ "$#" = "0" ]; then
  48. show_help
  49. fi
  50. OPTS="$(getopt -n "$PROG" -o "j:,i:,o:,h,u:,g:,f,n" -l "jobs:,input:,output:,help,urqmd:,unigen:,fake,nowait" -- "$@")" ||
  51. show_help
  52. eval set -- "$OPTS"
  53. while :; do
  54. case "$1" in
  55. --) shift; break
  56. ;;
  57. -j|--jobs) shift; njobs="$1"
  58. ;;
  59. -i|--input) shift; infile="$1"
  60. ;;
  61. -o|--output) shift; outdir="$1"
  62. ;;
  63. -g|--unigen) shift; unigen="$1"
  64. ;;
  65. -u|--urqmd) shift; urqmd="$1"
  66. ;;
  67. -h|--help) show_help
  68. ;;
  69. -f|--fake) fake=1
  70. ;;
  71. -n|--nowait) nowait=1
  72. ;;
  73. *) fatal "Unrecognized option: $1"
  74. ;;
  75. esac
  76. shift
  77. done
  78. # Create or go to the output directory
  79. outdir="${outdir-out}"
  80. [ -d "${outdir}" ] || mkdir -p "$outdir"
  81. # Check UrQMD
  82. urqmd="$(readlink -f "${urqmd-}")"
  83. [ -x "${urqmd}" ] || fatal "Can't find UrQMD at ${urqmd} or it is not an executable (links was resolved)"
  84. # Check unigen
  85. unigen="$(readlink -f "${unigen-}")"
  86. [ -x "${unigen}" ] || fatal "Can't find unigen at ${unigen} or it is not an executable (links was resolved)"
  87. # Check infile
  88. infile="$(readlink -f "${infile-}")"
  89. [ -f "${infile}" ] || fatal "Can't find input file at ${infile} (links was resolved)"
  90. # Wait few sec to take a time to user to check settings
  91. ftn="$(grep -i "^#f1[0-9]" $infile | tail -1 | sed -e "s;#f;;gI")"
  92. pro="$(grep -i "^pro" $infile | tail -1 | tr -s " " | sed -e "s;^pro ;;gI")"
  93. tar="$(grep -i "^tar" $infile | tail -1 | tr -s " " | sed -e "s;^tar ;;gI")"
  94. imp="$(grep -i "^imp" $infile | tail -1 | tr -s " " | sed -e "s;^imp ;;gI")"
  95. cat << EOF
  96. Using UrQMD: $urqmd
  97. Using urqmd2u: $unigen
  98. Using inputfile: $infile
  99. Output directory: $outdir
  100. ------- INPUT FILE DETAILS -------
  101. Output file type will be f$ftn
  102. Projectile: $pro
  103. Target: $tar
  104. Impact: $imp
  105. EOF
  106. if [ "${nowait-}" = "" ]; then
  107. echo "Preparing to run jobs..."
  108. for (( i=5; i > 0; i=i-1 )); do
  109. echo -ne "$i sec... "
  110. sleep 1
  111. done
  112. fi
  113. pushd "$outdir" >/dev/null
  114. # Create jobs directories
  115. for (( i=0; i < njobs; i=i+1 )); do
  116. echo -ne "\nBuilding the job $i... "
  117. temp="$(mktemp -d "job.$i.XXXXXXX")"
  118. pushd "$temp" >/dev/null
  119. cp "$infile" inputfile
  120. seed="$(printf "%d" 0x$(xxd -l 3 -ps -c 10 /dev/random))"
  121. events="$(grep -i "^nev" inputfile | tr -s " " | tail -1 | cut -d" " -f2)"
  122. if grep -i -q "^rsd" inputfile; then
  123. sed -i "s;^rsd.*;rsd $seed;gI" inputfile
  124. else
  125. sed -i "s;^xxx;rsd $seed\nxxx;gI" inputfile
  126. fi
  127. ln -s "$urqmd" urqmd
  128. ln -s "$unigen" unigen
  129. cat > script << EOF
  130. #!/bin/sh -efu
  131. # This script was automatically generated at $(date -R) by unipbs
  132. export ftn09="inputfile"
  133. export ftn$ftn="out_$seed.$ftn"
  134. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$(dirname $unigen)"
  135. export UNIGEN="$(dirname $unigen)"
  136. time ./urqmd
  137. time ./unigen "out_$seed.$ftn" "out_${seed}_$ftn.root" "$events"
  138. EOF
  139. chmod +x script
  140. echo "DONE"
  141. if [ "${fake-}" = "" ]; then
  142. qsub "$@" script
  143. else
  144. echo "qsub $@ script"
  145. fi
  146. popd >/dev/null
  147. done
  148. # Go back
  149. popd >/dev/null