FIX: avoid bad copy (foamCloneCase) when time dir is missing (fixes #2975)

This commit is contained in:
Mark Olesen
2023-10-06 11:55:43 +02:00
committed by Mark Olesen
parent a811e25556
commit 3458cea49e

View File

@ -7,6 +7,7 @@
# \\/ M anipulation | # \\/ M anipulation |
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Copyright (C) 2015 OpenFOAM Foundation # Copyright (C) 2015 OpenFOAM Foundation
# Copyright (C) 2023 OpenCFD Ltd.
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# License # License
# This file is part of OpenFOAM, distributed under GPL-3.0-or-later. # This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -18,62 +19,164 @@
# Create a new case directory that includes time, system and constant # Create a new case directory that includes time, system and constant
# directories from a source case. # directories from a source case.
# The time directory is the first time directory by default # The time directory is the first time directory by default
# - requires foamListTimes v2.3.x and newer #
# Requires
# foamListTimes
# #
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
usage() { printHelp() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat <<USAGE cat <<USAGE
Usage: ${0##*/} [OPTION] <sourceCase> <targetCase> Usage: ${0##*/} [OPTION] <sourceCase> <targetCase>
options: options:
-l | -latestTime clone the latest time directory -force Force overwrite of existing target
-h | -help print the usage -l | -latestTime Select the latest time directory
-h | -help Print the usage
Create a new <targetCase> case directory that includes time, system and constant Create a new <targetCase> case directory with a copy of time, system, constant
directories of <sourceCase> directory. directories from <sourceCase> directory.
The time directory is the first time directory by default. The time directory is the first time directory by default.
USAGE USAGE
exit 0 # A clean exit
}
# Report error and exit
die()
{
exec 1>&2
echo
echo "Error encountered:"
while [ "$#" -ge 1 ]; do echo " $1"; shift; done
echo
echo "See '${0##*/} -help' for usage"
echo
exit 1 exit 1
} }
TIME_OPTION="head -1"
#------------------------------------------------------------------------------
# Default: use the first time, combined with -withZero this will likely
# find the 0/ directory
filter="head -1"
unset optForce
# parse options # parse options
while [ "$#" -gt 0 ] while [ "$#" -gt 0 ]
do do
case "$1" in case "$1" in
-h | -help*) (- | --)
usage shift
break # Stop option parsing
;; ;;
-l | -latestTime) (-h | -help* | --help*)
TIME_OPTION="tail -1" printHelp
shift 1 ;;
-force)
optForce=true
;;
-l | -latest*)
## Also possible: opt_foamListTimes="-latestTime"
filter="tail -1"
;; ;;
-*) -*)
usage "unknown option: '$*'" die "unknown option: '$*'"
;; ;;
*) *)
break break
;; ;;
esac esac
shift
done done
[ $# -eq 2 ] || usage "Incorrect arguments specified"
if [ "$(foamListTimes -case $1 2>&1 >/dev/null | grep 'FOAM FATAL ERROR')" ] if [ "$#" -ne 2 ]
then then
usage "$1 is not does not a valid case directory" die "Incorrect number of arguments specified"
fi fi
! [ -e $2 ] || usage "$2 file/directory already exists, delete and re-run"
echo "Making $2 case directory" srcDir="$1"
mkdir $2 dstDir="$2"
TIME_DIR="$(foamListTimes -withZero -case $1 | $TIME_OPTION)" #------------------------------------------------------------------------------
echo "Copying case directories from $1 to $2" if [ -d "$srcDir" ]
cp -r $1/system $1/constant $1/${TIME_DIR} $2 then
unset missing
for dir in constant system
do
if [ ! -d "$srcDir/$dir" ]
then
missing="$missing${missing:+, }<$dir>"
fi
done
if [ -n "$missing" ]
then
die \
"Source directory is missing standard directories:" \
" -> $srcDir" \
" $missing"
fi
else
die \
"Source directory does not exist:" \
" -> $srcDir"
fi
command -v foamListTimes || die "Requires 'foamListTimes' (openfoam)"
if [ -e "$dstDir" ]
then
if [ "$optForce" = true ]
then
echo "------------" 1>&2
echo "Overwriting: $dstDir" 1>&2
echo "------------" 1>&2
rm -rf "$dstDir" # Remove old directory
else
die "Destination already exists, remove and re-run" \
" -> $dstDir"
fi
fi
if [ "$(foamListTimes -case "$srcDir" 2>&1 >/dev/null | grep 'FATAL ERROR')" ]
then
die "'$srcDir' does not appear to be a valid OpenFOAM case"
fi
timeDir="$(foamListTimes -withZero -case "$srcDir" | $filter)"
# Fallback for missing timeDir
if [ -z "$timeDir" ] && [ -d "$srcDir/0.orig" ]
then
timeDir="0.orig"
fi
echo "Copying case directories" 1>&2
echo " $srcDir" 1>&2
echo " -> $dstDir" 1>&2
if [ -n "$timeDir" ]
then
echo " Time: $timeDir" 1>&2
else
echo " No time directories" 1>&2
fi
mkdir -p "$dstDir"
echo "----" 1>&2
for dir in system constant "$timeDir"
do
if [ -n "$dir" ] && [ -d "$srcDir/$dir" ]
then
echo " .../$dir" 1>&2
cp -r "$srcDir/$dir" "$dstDir"
fi
done
echo "----" 1>&2
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------