Merge commit 'b5be6201e00421a59e574a07b3d28cde5defff84'
[foam-extend-4.0.git] / bin / foamJob
blob7815b8eb50f87ca26fd8e174e87a32d0780b23c0
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | foam-extend: Open Source CFD
5 # \\ / O peration | Version: 4.0
6 # \\ / A nd | Web: http://www.foam-extend.org
7 # \\/ M anipulation | For copyright notice see file Copyright
8 #------------------------------------------------------------------------------
9 # License
10 # This file is part of foam-extend.
12 # foam-extend 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 3 of the License, or (at your
15 # option) any later version.
17 # foam-extend is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
25 # Script
26 # foamJob
28 # Description
30 #------------------------------------------------------------------------------
31 usage() {
32 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
33 cat<<USAGE
35 usage: ${0##*/} [OPTION] <application> ...
37 options:
38 -case dir specify case directory
39 -s also sends output to screen
40 -p parallel run of processors
41 -v ver specify OpenFOAM version
42 -help this usage
44 * run an OpenFOAM job in background.
45 Redirects the output to 'log' in the case directory
47 USAGE
48 exit 1
51 unset version
53 # replacement for possibly buggy 'which'
54 findExec() {
55 case "$1" in
56 */*)
57 if [ -x "$1" ] ; then
58 echo "$1"
59 return 0
62 esac
64 oldIFS=$IFS
65 IFS=':'
66 for d in $PATH
68 # echo "testing: $d/$1" 1>&2
69 if [ -x "$d/$1" -a ! -d "$d/$1" ] ; then
70 # echo "Found exec: $d/$1" 1>&2
71 IFS=$oldIFS
72 echo "$d/$1"
73 return 0
75 done
76 IFS=$oldIFS
77 echo ""
78 return 1
81 # grep for $1
82 getPID() {
83 ps -u $LOGNAME -o 'pid,args' | fgrep "$1 " | fgrep -v grep | head -1 | awk '{ print $1 }'
87 consultGuide() {
88 cat<<EOF
90 Please consult the User Guide for details of parallel running
91 EOF
94 # MAIN SCRIPT
95 #~~~~~~~~~~~~
96 SCREEN=no
97 PARALLEL=no
100 # parse options
101 while [ "$#" -gt 0 ]
103 case "$1" in
104 -h | -help)
105 usage
107 -case)
108 [ "$#" -ge 2 ] || usage "'-case' option requires an argument"
109 caseDir=$2
110 shift 2
111 cd "$caseDir" 2>/dev/null || usage "directory does not exist: '$caseDir'"
114 SCREEN=yes
115 shift
118 PARALLEL=yes
119 shift
122 shift
123 version=$1
124 shift
127 shift
128 break
131 usage "invalid option '$1'"
134 break
136 esac
137 done
139 if [ "$#" -lt 1 ]; then
140 usage "No application specified"
143 # use foamExec for a specified version and for remote (parallel) runs
144 if [ -n "$version" -o "$PARALLEL" = yes ]; then
145 APPLICATION=`findExec foamExec`
146 if [ $? -ne 0 ]; then
147 usage "'foamExec' not found"
149 if [ -n "$version" ]; then
150 APPLICATION="$APPLICATION -v $version"
152 else
153 APPLICATION=`findExec $1`
154 if [ $? -ne 0 ]; then
155 usage "Application '$1' executable not found"
157 echo "Application : $1"
158 shift
161 if [ "$PARALLEL" = no ]; then
163 # RUN ON SINGLE PROCESSOR
165 if [ "$SCREEN" = no ]; then
166 echo "Executing: $APPLICATION $@ > log 2>&1 &"
167 $APPLICATION $@ > log 2>&1 &
168 else
169 echo "Executing: $APPLICATION $@ | tee log &"
170 $APPLICATION $@ | tee log &
171 wait $!
173 else
176 # IS THE CASE DECOMPOSED?
178 if [ -r "processor0" ] ; then
179 NPROCS="`/bin/ls -1d processor* | wc -l`"
180 else
181 echo "Case is not currently decomposed"
182 if [ -r system/decomposeParDict ] ; then
183 echo "system/decomposeParDict exists"
184 echo "Try decomposing with \"foamJob decomposePar\""
185 exit 1
186 else
187 echo "Cannot find system/decomposeParDict file required to decompose the case for parallel running."
188 consultGuide
189 exit 1
193 # LOCATE MPIRUN
195 mpirun=`findExec mpirun`
196 if [ $? -ne 0 ]; then
197 usage "'mpirun' not found"
199 mpiopts="-np $NPROCS"
202 # IS THE MACHINE READY TO RUN PARALLEL?
204 echo "Parallel processing using $WM_MPLIB with $NPROCS processors"
205 case "$WM_MPLIB" in
206 OPENMPI)
207 # add hostfile info
208 for hostfile in \
209 hostfile \
210 machines \
211 system/hostfile \
212 system/machines \
215 if [ -r $hostfile ]; then
216 mpiopts="$mpiopts -hostfile $hostfile"
217 break
219 done
221 esac
224 # RUN IN PARALLEL
226 if [ "$SCREEN" = no ] ; then
227 echo "Executing: mpirun $mpiopts $APPLICATION $@ -parallel > log 2>&1"
228 $mpirun $mpiopts $APPLICATION $@ -parallel > log 2>&1 &
229 else
230 echo "Executing: mpirun $mpiopts $APPLICATION $@ -parallel | tee log"
231 $mpirun $mpiopts $APPLICATION $@ -parallel | tee log
235 #------------------------------------------------------------------------------