#!/bin/sh # # Create a directory which is a subdirectory of the current one # and is unique # usage() { cat < -N -M -t -g -h? EOF } # mac doesn't have readlink with -f option to canonicalize the path canonicalDirname() { cd "$(dirname "$1")" echo $(pwd -P) } # print help page help() { usage cat <. This operation is repeated from the cycle after the one indicated on the -N option (default 0) to the one indicated on the -M option (default 5). -d run with the bin/flukadpm executable -e name of the fluka executable to be run -N number of the previous run cycle number -M final run number -t maximum CPU time (s) allowed for each run -g run through a debugger (e.g. gdb) -p search path used from OAUXFI routine used to define the flair project folder -h | -? print this help file input file for fluka, with or without .inp EOF } if [ $# -eq 0 ]; then usage exit fi # Default values CONFIG="$(dirname "$0")/fluka-config" if [ ! -x "${CONFIG}" ]; then echo echo "Error: fluka is not properly compiled." echo "File: \"${CONFIG}\" is missing" exit 1 fi FLUKADIR=$("${CONFIG}" --path) BINDIR=$("${CONFIG}" --bin) LIBDIR="$(dirname "$0")/lib" FORT="fort" FORTD="." FORT0="" TARGET_MACHINE=$(uname) EXE_EXT= case $TARGET_MACHINE in Linux) # default values ;; CYGWIN_NT-10.0-WOW) EXE_EXT=.exe ;; Darwin|OSF1|AIX) # default values ;; HP-UX) FORT="ftn" FORTD="" FORT0="0" ;; *) echo " !!! undefined OS system !!!!" ;; esac if [ -z "$FLUKADATA" ]; then export FLUKADATA=$("${CONFIG}" --data) fi echo echo " F L U K A" echo echo "Dir: ${FLUKADIR}" echo "Data: ${FLUKADATA}" EXE="${BINDIR}/fluka${EXE_EXT}" INPF="" DBG="" NUM=0 MAX=5 GFOR_SYS=$(gfortran -dumpversion | cut -d. -f1) GFOR_BIN=$("${CONFIG}" --compver) # check for compatible gfortran if !([ $GFOR_SYS -eq 7 ] && [ $GFOR_BIN -eq 7 ]) && !([ $GFOR_SYS -ge 8 ] && [ $GFOR_BIN -ge 8 ]); then echo "Error: Incompatible gfortran version!" echo " System gfortran version: ${GFOR_SYS}" echo " FLUKA package version: ${GFOR_BIN}" exit 3 fi # loop until all arguments are processed while [ $# -gt 0 ]; do while getopts "de:g:hN:M:t:p:" opt; do case $opt in h) help exit ;; d) EXE="${BINDIR}/flukadpm${EXE_EXT}" ;; e) EXE=$(canonicalDirname "${OPTARG}")/$(basename "${OPTARG}") ;; N) NUM=${OPTARG} ;; M) MAX=${OPTARG} ;; t) CPULIMIT=${OPTARG} export CPULIMIT ;; p) FLUKAPATH=${OPTARG} export FLUKAPATH ;; g) DBG=${OPTARG} ;; \?) echo "Error: Invalid option -$OPTARG" exit 1 ;; :) echo "Option -$OPTARG requires an argument!" ;; esac done # shift the remaining arguments shift $((OPTIND-1)) # check if any arguments left? if [ $# -gt 0 ]; then if [ .${INPF} = . ]; then # For safety remove the extension .inp if any ARG="`echo $1 | sed -es/\.inp\$//`" INPF="${ARG}.inp" shift else echo "Error: Too many input files specified!" exit 2 fi OPTIND=1 fi done echo "Exec: ${EXE}" CURDIR=`pwd` if [ "$INPF" = "" ]; then echo "Error: An input file should be specified!" exit 4 fi INPD=$(dirname "${INPF}") if [ "$INPD" = "." ]; then INPF=${CURDIR}/$INPF fi INPN=$(basename "${INPF}") ROOF=$(basename "${ARG}") # check if file exists if [ ! -r "${INPF}" ]; then echo "Error: Input \"${INPF}\" not found!" exit 5 fi # check if is not empty if [ ! -s "${INPF}" ]; then echo "Error: Input \"${INPF}\" is empty!" exit 13 fi echo "Input: ${INPF}" # check NUM and MAX if [ $NUM -ge $MAX ]; then echo "Error: Final cycle -M $MAX is smaller or equal than previous cycle -N $NUM !" exit 6 fi EXED=$(dirname "${EXE}") if [ "$EXED" = "." ]; then EXE=${CURDIR}/$EXE fi # check if EXE is newer than libfluka.a if [ -x "$EXE" ]; then if [ "${LIBDIR}/libfluka.a" -nt "$EXE" ]; then echo "Error: ${LIBDIR}/libfluka.a is newer than $EXE. Please relink" exit 7 fi else echo "Error: $EXE does not exist or it is not executable!" exit 8 fi # renice: # renice +10 ${$} CURSUB=fluka_${$} if [ -d $CURSUB ]; then echo "$CURSUB exists, making a new unique one" i=0 while [ 1 ]; do BAS=$(printf "%02d" $i) if [ $i -gt 100 ]; then echo "Error: Cannot find free directory name in first 100" exit 9 fi TRY=${CURSUB}_00${i} if [ ! -d ${CURSUB}_00${i} ]; then CURSUB=$TRY break fi i=`expr $i + 1` done fi if [ $NUM -eq 0 ]; then RAN1=${CURDIR}/ran${ROOF}001 if [ ! -r "$RAN1" ]; then echo "Initial seed copied from ${FLUKADATA}" cp "${FLUKADATA}/random.dat" "$RAN1" else echo "Initial seed already exists" fi fi # create running sub-directory mkdir $CURSUB cd $CURSUB echo "Running fluka in `pwd`" # copy input file to directory cp "${INPF}" "${INPN}" echo while [ $NUM -lt $MAX ]; do ERR=0 NUM=$((NUM+1)) PST=$(printf "%03d" $NUM) # Export a variable with the NUM value FLUKA_CUR_RUN="$NUM" export FLUKA_CUR_RUN NUP=$((NUM+1)) PSP=$(printf "%03d" $NUP) # Start run RAN1=$CURDIR/ran${ROOF}${PST} ln -s -f "${RAN1}" ${FORT}${FORTD}${FORT0}1 RAN2=ran${ROOF}${PSP} ln -s -f "${RAN2}" ${FORT}${FORTD}${FORT0}2 OUTF=${ROOF}${PST}.out LOGF=${ROOF}${PST}.log ERRF=${ROOF}${PST}.err ln -s "${OUTF}" ${FORT}${FORTD}11 ln -s "${ERRF}" ${FORT}${FORTD}15 echo "======================== Running FLUKA for cycle # ${NUM} ========================" if [ .${DBG} = . ]; then "${EXE}" "${INPF}" 1> "${LOGF}" 2>&1 else "${DBG}" --args "${EXE}" "${INPF}" fi RC=$? if [ $RC -ne 0 ]; then touch core # signal flair for abort echo "Error: \"${EXE}\" executable returned RC=${RC}" exit $RC fi # remove the links we know we can remove echo "Removing links" for i in \ ${FORT}${FORTD}${FORT0}1 \ ${FORT}${FORTD}${FORT0}2 \ ${FORT}${FORTD}11 \ ${FORT}${FORTD}12 \ ${FORT}${FORTD}15 do if [ -h "${i}" ]; then \rm "${i}" elif [ -r "${i}" ]; then echo "Error: Something wrong, "${i}" is a file and not a link!" touch core # signal flair for abort exit 10 fi done # remove the temporary files we know we can remove echo "Removing temporary files" for i in ${FORT}${FORTD}${FORT0}8 ${FORT}${FORTD}16 geom.scr emf.scr .timer.out; do if [ -f "${i}" ]; then \rm "${i}" elif [ -h "${i}" ]; then echo "Error: Something wrong, "${i}" is a link and not a file!" touch core # signal flair for abort exit 11 fi done # move 'up' the files we know we have to save echo "Saving output and random number seed" for i in "${OUTF}" "${RAN2}" "${LOGF}" "${ERRF}"; do if [ -r "${i}" ]; then \mv "${i}" "${CURDIR}" else if grep -q "^START " "${INPF}"; then echo "Error: No "${i}" generated!" ERR=1 fi fi done # move up all the other files with a name echo "Saving additional files generated" for i in *; do # check for a possible user request to stop the run right away if [ "${i}" = "rfluka.stop" ]; then NUM=${MAX} \rm "${i}" # check for a possible user request to stop the run after # the current cycle has been completed elif [ "${i}" = "flukacycle.stop" ]; then NUM=${MAX} \rm "${i}" # move all but the copy of the input file elif [ "${i}" != "${INPN}" ]; then echo " Moving \"${i}\" to \"${CURDIR}/${ROOF}${PST}_${i}\"" \mv "${i}" "${CURDIR}/${ROOF}${PST}_${i}" fi done #echo "No additional files to move" if [ $ERR -gt 0 ]; then touch core # signal flair for abort exit 12 fi done # cleanup files and directory # remove .timer.out if [ -f .timer.out ]; then \rm .timer.out fi \rm "${INPN}" cd "${CURDIR}" rmdir "${CURSUB}" echo "End of FLUKA run"