2 #------------------------------------------------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
8 #-------------------------------------------------------------------------------
10 # This file is part of OpenFOAM.
12 # OpenFOAM is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
29 # Ends running job on current machine. Called with root,case,pid.
30 # - checks if pid exists
31 # - modifies controlDict
34 # - controlDict modified
35 # to restore controlDict
37 #-------------------------------------------------------------------------------
42 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
44 Usage: $Script [OPTION] <pid>
45 Usage: $Script [OPTION] -c
48 -clear use blockMesh reader (uses .blockMesh extension)
49 -case <dir> specify alternative case directory, default is the cwd
50 -now stop at next time step
53 Tries to end running OpenFOAM application at next write (or optionally
54 at the next time step). It needs runTimeModifiable switched on in the
55 controlDict. It changes stopAt in the controlDict and waits for the
56 job to finish. Restores original controlDict if
59 - controlDict gets modified (by user)
60 - $Script gets killed.
62 The -clear option clears any outstanding $Script for the case.
68 unset clearOpt stopOpt
78 [ "$#" -ge 2 ] || usage
"'$1' option requires an argument"
79 cd "$2" 2>/dev
/null || usage
"directory does not exist: '$2'"
91 usage
"unknown option: '$*'"
100 # parent directory for normal or parallel
102 processor
*) caseDir
=".." ;;
106 # check that case directory is writeable
108 echo "$Script : $caseDir is not writeable"
112 # check that controlDict is writeable
113 controlDict
=$caseDir/system
/controlDict
114 [ -w $controlDict ] ||
{
115 echo "$Script : $controlDict is not writeable"
119 # need a pid unless with the -clear option
120 if [ "$clearOpt" = true
]
122 [ $# -eq 0 ] || usage
124 [ $# -eq 1 ] || usage
129 #-------------------------------------------------------------------------------
133 #-------------------------------------------------------------------------------
135 # getNumberedLine dictionary entry
136 # Prints dictionary entry line + lineno
138 grep -n "^[ \t]*$2[ \t]" $1 |
grep -v '^//' |
head -1
141 # getLine dictionary entry
142 # Prints dictionary entry line (without lineno)
144 getNumberedLine
$1 "$2" |
sed -e 's/^[^:]*://'
147 # getRawEntry dictionary entry
148 # Prints value of dictionary entry
150 getLine
$1 "$2" |
sed -e "s/^[ \t]*$2[ \t][ \t]*//"
153 # getEntry dictionary entry
154 # Like getRawEntry but strips " and ending ';'
156 getRawEntry
$1 "$2" |
sed -e 's/^"//' -e 's/;$//' -e 's/"$//'
160 # Prints first item on line
162 echo "$1" |
sed -e 's/[ \t]*\(.*\)[ \t].*/\1/'
166 # setRawEntry dictionary entry newValue
167 # Replaces value of entry
169 oldNumLine
=`getNumberedLine $1 "$2"`
170 lineNo
=`echo "$oldNumLine" | sed -e 's/:.*//'`
171 oldLine
=`echo "$oldNumLine" | sed -e 's/^[^:]*://'`
172 oldKey
=`getKey "$oldLine"`
173 oldVal
=`getRawEntry $1 "$2"`
175 if [ ! "$oldKey" -o ! "$oldVal" -o ! "$oldLine" ]
177 echo "setRawStringEntry: entry $2 not found in $1"
178 echo "oldKey=$oldKey"
179 echo "lineNo=$lineNo"
180 echo "oldLine=$oldLine"
184 #echo "oldKey=$oldKey"
185 #echo "lineNo=$lineNo"
186 #echo "oldLine=$oldLine"
187 #echo "oldVal=$oldVal"
189 sed -e "${lineNo}s/ ${oldVal}/ $3;/" ${1}_tmp > $1
195 # like getEntry but returns true if boolean is logical true
200 y | yes | true | on | 1)
203 n | no | false | off | 0)
207 echo "$Script : getBoolEntry
: Illegal boolean value
$val in dictionary
$1"
214 # Returns true if pid exists.
216 ps -u $LOGNAME -o pid | fgrep $1 >/dev/null
219 # Restore controlDict and clean up
223 echo "$Script : Restoring controlDict from controlDict_bak
"
224 if [ -r ${controlDict}_bak ]
226 cp ${controlDict}_bak $controlDict
231 echo "$Script : Exiting
"
236 #-------------------------------------------------------------------------------
240 #-------------------------------------------------------------------------------
242 #-- Force standards behaving ps
243 # Get info on all $USER processes
256 # Pid actually running
257 if [ "$clearOpt" != true ]
262 echo "$Script : process
$PID not running
"
268 getBoolEntry $controlDict runTimeModifiable || {
269 echo "$Script : runTimeModifiable not true
in dictionary
$controlDict"
274 #- Check if another foamEndJob running
276 if [ "$clear" = true ]
278 pidFiles=`ls $caseDir/.foamEndJob* 2>/dev/null`
279 for pidFile in $pidFiles
284 echo "$Script : found
$Script (pid
$pid) for OpenFOAM process
"
285 echo " case: $PWD/$caseDir"
286 echo "$Script : Killing
$Script (pid
$pid)"
294 pidFile=$caseDir/.foamEndJob$PID
303 echo "$Script : found running
$Script (pid
$pid) for OpenFOAM process
"
304 echo " case: $PWD/$caseDir"
306 echo " lock
: $pidFile"
307 echo "Remove the lock
if this is not the
case"
317 #- Get controlDict entries
322 startTime=`getEntry $controlDict startTime`
323 [ "$startTime" ] || {
324 echo "$Script : startTime not
set in dictionary
$controlDict"
329 writeInterval=`getEntry $controlDict writeInterval`
330 [ "$writeInterval" ] || {
331 echo "$Script : writeInterval not
set in dictionary
$controlDict"
336 stopAt=`getEntry $controlDict stopAt`
338 echo "$Script : stopAt not
set in dictionary
$controlDict"
343 endTime=`getEntry $controlDict endTime`
345 echo "$Script : endTime not
set in dictionary
$controlDict"
350 echo "$Script : Read from controlDict
:"
351 echo " controlDict
: $controlDict"
352 echo " writeInterval
: $writeInterval"
353 #echo " startTime
: $startTime"
354 echo " stopAt
: $stopAt"
355 #echo " endTime
: $endTime"
357 echo "$Script : Making backup of controlDict to controlDict_bak
"
358 cp $controlDict ${controlDict}_bak
360 #- Set up handler to restore controlDict
361 trap restoreDict QUIT TERM INT
363 if [ "$stopOpt" = now ]
365 setRawEntry $controlDict stopAt nextWrite
366 setRawEntry $controlDict writeInterval 1
368 echo "$Script : Changed
in controlDict
:"
369 echo " `getLine $controlDict stopAt`"
370 echo " `getLine $controlDict writeInterval`"
372 setRawEntry $controlDict stopAt nextWrite
374 echo "$Script : Changed
in controlDict
:"
375 echo " `getLine $controlDict stopAt`"
380 #- Just to make sure time has changed
385 #- Give bak a later date
386 touch ${controlDict}_bak
388 #- Loop a while to give NFS time to update
389 if [ ${controlDict} -nt ${controlDict}_bak ]
391 echo "$Script : controlDict newer than controlDict_bak
"
392 echo "$Script : Waiting
for file dates to get updated
"
395 while [ ${controlDict} -nt ${controlDict}_bak ]
399 #- 120*5 sec = 10 mins passed. Give up
400 echo "$Script : File
date not yet ok after
10 mins ... giving up
"
403 #- Give _bak a later time
404 touch ${controlDict}_bak
406 #- Give NFS a chance to update time on controlDict.
409 iter=`expr $iter + 1`
414 #- Start waiting until:
415 # - pid finished. Restore controlDict.
416 # - controlDict modified. No restore.
417 # - controlDict_bak removed. No restore.
419 echo "$Script : Waiting
for OpenFOAM job
$PID to finish ...
"
425 [ -r ${controlDict}_bak ] || {
426 echo "$Script : ${controlDict}_bak disappeared. Exiting without restore
"
430 if [ ${controlDict} -nt ${controlDict}_bak ]
432 echo "$Script : ${controlDict} modified externally. Exiting without restore
"
442 # echo "OpenFOAM job
$PID still running ...
"
445 #- Dictionary restore
448 #------------------------------------------------------------------------------