intersection with triangle plane for miss
[OpenFOAM-1.5.x.git] / bin / foamEndJob
blob34c6bbca25622a3ff2732ac34c8d1e98287d747c
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
7 # \\/ M anipulation |
8 #-------------------------------------------------------------------------------
9 # License
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 the
14 # Free Software Foundation; either version 2 of the License, or (at your
15 # 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
20 # for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM; if not, write to the Free Software Foundation,
24 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 # Script
27 # foamEndJob
29 # Description
30 # Ends running job on current machine. Called with root,case,pid.
31 # - checks if pid exists
32 # - modifies controlDict
33 # - waits until
34 # - pid disappeared
35 # - controlDict modified
36 # to restore controlDict
38 #-------------------------------------------------------------------------------
40 PROGNAME=`basename $0`
43 #-------------------------------------------------------------------------------
45 # Functions
47 #-------------------------------------------------------------------------------
49 # getNumberedLine dictionary entry
50 # Prints dictionary entry line + lineno
51 getNumberedLine() {
52 grep -n "^[ \t]*$2[ \t]" $1 | grep -v '^//' | head -1
55 # getLine dictionary entry
56 # Prints dictionary entry line (without lineno)
57 getLine() {
58 getNumberedLine $1 "$2" | sed -e 's/^[^:]*://'
61 # getRawEntry dictionary entry
62 # Prints value of dictionary entry
63 getRawEntry() {
64 getLine $1 "$2" | sed -e "s/^[ \t]*$2[ \t][ \t]*//"
67 # getEntry dictionary entry
68 # Like getRawEntry but strips " and ending ';'
69 getEntry() {
70 getRawEntry $1 "$2" | sed -e 's/^"//' -e 's/;$//' -e 's/"$//'
73 # getKey entryLine
74 # Prints first item on line
75 getKey() {
76 echo "$1" | sed -e 's/[ \t]*\(.*\)[ \t].*/\1/'
80 # setRawEntry dictionary entry newValue
81 # Replaces value of entry
82 setRawEntry() {
83 oldNumLine=`getNumberedLine $1 "$2"`
84 lineNo=`echo "$oldNumLine" | sed -e 's/:.*//'`
85 oldLine=`echo "$oldNumLine" | sed -e 's/^[^:]*://'`
86 oldKey=`getKey "$oldLine"`
87 oldVal=`getRawEntry $1 "$2"`
88 if [ ! "$oldKey" -o ! "$oldVal" -o ! "$oldLine" ]; then
89 echo "setRawStringEntry: entry $2 not found in $1"
90 echo "oldKey=$oldKey"
91 echo "lineNo=$lineNo"
92 echo "oldLine=$oldLine"
93 exit 1
95 #echo "oldKey=$oldKey"
96 #echo "lineNo=$lineNo"
97 #echo "oldLine=$oldLine"
98 #echo "oldVal=$oldVal"
99 mv $1 ${1}_tmp
100 sed -e "${lineNo}s/ ${oldVal}/ $3;/" ${1}_tmp > $1
101 rm -f ${1}_tmp
106 # like getEntry but returns true if boolean is logical true
107 getBoolEntry()
109 val=`getEntry $1 $2`
110 case "$val" in
111 'yes')
112 return 0
114 'no')
115 return 123
117 'true')
118 return 0
120 'false')
121 return 123
124 return 0
127 return 123
130 echo "$PROGNAME : getBoolEntry : Illegal boolean value $val in dictionary $1"
131 exit 1
133 esac
136 # newerFile file1 file2
137 newerFile() {
138 latest=`ls -1 -t $1 $2 2> /dev/null | head -1`
139 if [ "$latest" = $1 ]; then
140 return 0
141 else
142 return 1
146 # processExists pid
147 # Returns true if pid exists.
148 processExists() {
149 ps -u $LOGNAME -o 'pid' | fgrep $1 >/dev/null
152 printUsage() {
153 cat << USAGELABEL
154 Usage: $PROGNAME [-n] <root> <case> <pid>
156 $PROGNAME -c <root> <case>
158 Tries to end running Foam application at next write or at next time
159 step (-n option). It needs runTimeModifiable switched on in the
160 controlDict. It changes stopAt in the controlDict and waits for the job to
161 finish. Restores original controlDict if
162 - job has finished
163 - controlDict gets modified (by user)
164 - $PROGNAME gets killed.
166 The -c option clears any outstanding $PROGNAME for the case.
168 USAGELABEL
172 # Restore controlDict and clean up
173 restoreDict() {
174 trap 2 3 15
176 echo "$PROGNAME : Restoring controlDict from controlDict_bak."
177 if [ -r ${controlDict}_bak ]; then
178 cp ${controlDict}_bak $controlDict
181 rm -f $pidFile
183 echo "$PROGNAME : Exiting."
184 exit 0
188 #-------------------------------------------------------------------------------
190 # Main
192 #-------------------------------------------------------------------------------
194 ARCH=`uname -s`
196 #-- Force standards behaving ps
197 # Get info on all $USER processes
198 case $ARCH in
199 HP-UX*)
200 UNIX95=a; export UNIX95
202 IRIX*)
203 _XPG=1; export _XPG
205 esac
209 # Initial checks
211 if [ $# -lt 3 ]; then
212 printUsage
213 exit 1
215 STOPNOW=''
216 if [ $1 = '-n' ]; then
217 STOPNOW='yes'
218 shift
220 CLEAR=''
221 if [ $1 = '-c' ]; then
222 CLEAR='yes'
223 shift
224 if [ $# -ne 2 ]; then
225 printUsage
226 exit 1
228 ROOT=$1
229 CASE=$2
230 else
231 if [ $# -ne 3 ]; then
232 printUsage
233 exit 1
235 ROOT=$1
236 CASE=$2
237 PID=$3
239 CASE=`echo $CASE | sed -e 's!/.*!!'` #strip of processorXXX ending
241 #- Pid actually running
242 if [ ! "$CLEAR" ]; then
243 processExists $PID
244 if [ $? -ne 0 ] ;then
245 echo "$PROGNAME : process $PID not running."
246 exit 1
250 #- case directory writeable
251 if [ ! -w $ROOT/$CASE ]; then
252 echo "$PROGNAME : $ROOT/$CASE is not writeable."
253 exit 1
256 #- Controldict writeable
257 controlDict=$ROOT/$CASE/system/controlDict
258 if [ ! -w $controlDict ]; then
259 echo "$PROGNAME : $controlDict is not writeable."
260 exit 1
263 #- runTimeModifiable
264 getBoolEntry $controlDict 'runTimeModifiable'
265 if [ $? -ne 0 ]; then
266 echo "$PROGNAME : runTimeModifiable not true in dictionary $controlDict."
267 exit 1
271 #- Check if another foamEndJob running
273 if [ "$CLEAR" ]; then
274 pidFiles=`ls $ROOT/$CASE/.foamEndJob* 2>/dev/null`
275 for pidFile in $pidFiles
277 pid=`cat $pidFile`
278 if [ "$pid" ]; then
279 echo "$PROGNAME : found $PROGNAME (pid $pid) for Foam process"
280 echo " root: $ROOT"
281 echo " case: $CASE"
282 echo "$PROGNAME : Killing $PROGNAME (pid $pid)."
283 kill $pid
284 rm -f $pidFile
286 done
287 exit 0
290 pidFile=$ROOT/$CASE/.foamEndJob${PID}
291 if [ -f $pidFile ]; then
292 pid=`cat $pidFile`
293 if [ "$pid" ]; then
294 processExists $pid
295 if [ $? -eq 0 ] ;then
296 echo "$PROGNAME : found running $PROGNAME (pid $pid) for Foam process"
297 echo " root: $ROOT"
298 echo " case: $CASE"
299 echo " pid : $PID"
300 echo " lock: $pidFile"
301 echo "Remove the lock if this is not the case."
302 exit 1
307 # Mark with my pid
308 echo $$ > $pidFile
311 #- Get controlDict entries
315 #- startTime
316 startTime=`getEntry $controlDict 'startTime'`
317 if [ ! "$startTime" ]; then
318 echo "$PROGNAME : startTime not set in dictionary $controlDict."
319 exit 1
322 #- Write interval
323 writeInterval=`getEntry $controlDict 'writeInterval'`
324 if [ ! "$writeInterval" ]; then
325 echo "$PROGNAME : writeInterval not set in dictionary $controlDict."
326 exit 1
329 #- stopAt
330 stopAt=`getEntry $controlDict 'stopAt'`
331 if [ ! "$stopAt" ]; then
332 echo "$PROGNAME : stopAt not set in dictionary $controlDict."
333 exit 1
336 #- endTime
337 endTime=`getEntry $controlDict 'endTime'`
338 if [ ! "$endTime" ]; then
339 echo "$PROGNAME : endTime not set in dictionary $controlDict."
340 exit 1
344 echo "$PROGNAME : Read from controlDict:"
345 echo " controlDict : $controlDict"
346 echo " writeInterval : $writeInterval"
347 #echo " startTime : $startTime"
348 echo " stopAt : $stopAt"
349 #echo " endTime : $endTime"
351 echo "$PROGNAME : Making backup of controlDict to controlDict_bak"
352 cp $controlDict ${controlDict}_bak
353 #- Set up handler to restore controlDict
354 trap restoreDict 2 3 15
356 if [ "$STOPNOW" ]; then
357 setRawEntry $controlDict 'stopAt' 'nextWrite'
358 setRawEntry $controlDict 'writeInterval' '1'
360 echo "$PROGNAME : Changed in controlDict:"
361 echo " `getLine $controlDict 'stopAt'`"
362 echo " `getLine $controlDict 'writeInterval'`"
363 else
364 setRawEntry $controlDict 'stopAt' 'nextWrite'
366 echo "$PROGNAME : Changed in controlDict:"
367 echo " `getLine $controlDict 'stopAt'`"
372 #- Just to make sure time has changed
373 touch ${controlDict}
375 sleep 5
377 #- Give bak a later date
378 touch ${controlDict}_bak
380 #- Loop a while to give NFS time to update
381 if newerFile ${controlDict} ${controlDict}_bak; then
382 echo "$PROGNAME : controlDict newer than controlDict_bak."
383 echo "$PROGNAME : Waiting for file dates to get updated."
385 iter=0
386 while newerFile ${controlDict} ${controlDict}_bak
388 if [ $iter -ge 120 ]; then
389 #- 120*5 sec = 10 mins passed. Give up
390 echo "$PROGNAME : File date not yet ok after 10 mins. Giving up."
391 break
393 #- Give _bak a later time
394 touch ${controlDict}_bak
396 #- Give nfs some time to update time on controlDict.
397 sleep 5
399 iter=`expr $iter + 1`
400 done
404 #- Start waiting until:
405 # - pid finished. Restore controlDict.
406 # - controlDict modified. No restore.
407 # - controlDict_bak removed. No restore.
409 echo "$PROGNAME : Waiting for Foam job $PID to finish ..."
411 while true
413 sleep 5
415 if [ ! -r ${controlDict}_bak ]; then
416 echo "$PROGNAME : ${controlDict}_bak dissappeared. Exiting without restore."
417 exit 1
420 if newerFile ${controlDict} ${controlDict}_bak; then
421 echo "$PROGNAME : ${controlDict} modified externally. Exiting without restore."
422 exit 0
425 processExists $PID
426 if [ $? -ne 0 ] ;then
427 #- Job finished
428 break
430 #echo "Foam job $PID still running ..."
431 done
433 #- Dictionary restore
434 restoreDict
436 #------------------------------------------------------------------------------