Merge branch 'upstream/OpenFOAM-1.7.x' into upstream/OpenFOAM
[freefoam.git] / bin / foamNewCase
blob52cd516bb695f44160f40c1398667ba6d4a7a049
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2010-2010 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
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
20 # for more details.
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/>.
25 # Script
26 # foamNewCase
28 # Description
29 # Create a new case from a template for particular applications
30 # - requires rsync
32 #------------------------------------------------------------------------------
33 siteDir=${WM_PROJECT_INST_DIR:-unknown}/site
34 userDir=$HOME/.OpenFOAM
35 version=${WM_PROJECT_VERSION:-unknown}
36 templateDir="appTemplates"
38 #------------------------------------------------------------------------------
39 usage() {
40 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
41 cat<<USAGE
43 Usage: ${0##*/} [OPTION]
44 options:
45 -app <name> specify the application to use
46 -case <dir> specify alternative case directory, default is the cwd
47 -list list the applications available
48 -version <ver> specify an alternative version (default: '$WM_PROJECT_VERSION')
50 clone initial application settings to the specified case from
51 $userDir/$templateDir/{$version,}/<APP>
52 $siteDir/$templateDir/{$version,}/<APP>
54 USAGE
55 exit 1
57 #------------------------------------------------------------------------------
58 unset appName caseName listOpt
60 # parse options
61 while [ "$#" -gt 0 ]
63 case "$1" in
64 -h | -help)
65 usage
67 -app)
68 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
69 appName="$2"
70 shift 2
72 -case)
73 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
74 caseName="$2"
75 shift 2
77 -l | -list)
78 listOpt=true
79 shift
81 -v | -ver | -version)
82 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
83 version="$2"
84 shift 2
86 -*)
87 usage "unknown option: '$*'"
90 usage "unexpected argument: '$*'"
92 esac
93 done
95 # need rsync, except for when listing
96 type rsync >/dev/null 2>&1 || [ "$listOpt" = true ] || usage "Error: 'rsync' seems to be missing"
99 #------------------------------------------------------------------------------
101 [ -n "$version" ] || {
102 echo "Error: no -version specified and \$WM_PROJECT_VERSION is not set"
103 exit 1
107 # find apps in current directory
108 # considered an app if it has constant/ and system/ directories
110 findApps()
112 for app in $(/bin/ls -d * 2>/dev/null)
114 [ -d "$app/constant" -a -d "$app/system" ] && echo $app
115 done
119 appList=$(
120 for dir in $userDir/$templateDir $siteDir/$templateDir
122 if cd $dir 2>/dev/null
123 then
124 findApps ## generic
125 cd $version 2>/dev/null && findApps ## version-specific
127 done | sort | uniq
131 listApps()
133 echo
134 echo "applications available:"
135 for i in $appList
137 echo " $i"
138 done
139 echo
143 if [ "$listOpt" = true ]
144 then
145 listApps
146 exit 0
147 elif [ "$(echo $appList | wc -w)" -eq 0 ]
148 then
149 echo "Error: no applications available"
150 exit 1
151 elif [ -z "$appName" ]
152 then
153 echo "Error: no -app specified"
154 listApps
155 exit 1
159 # get the corresponding srcDir name
160 srcDir=$(
161 for dir in $userDir/$templateDir $siteDir/$templateDir
163 if [ -d $dir ]
164 then
165 for appDir in $dir/$version/$appName $dir/$appName
167 if [ -d $appDir -a -d $appDir/constant -a -d $appDir/system ]
168 then
169 echo "$appDir"
170 break 2
172 done
174 done
178 [ -d "$srcDir" ] || {
179 echo "Error: could not find template for $appName"
180 listApps
181 exit 1
185 # adjust for caseName as required
186 if [ -n "$caseName" ]
187 then
188 [ -d "$caseName" ] || mkdir -p "$caseName"
189 cd "$caseName" 2>/dev/null || usage "directory does not exist: '$caseName'"
191 newDir=$PWD
194 [ -d "$newDir" -a -w "$newDir" ] || {
195 echo "Error: target directory does not exist or is unwritable"
196 echo " $newDir"
197 exit 1
200 # add some useful subdirs:
201 mkdir -p $newDir/postPro
204 echo " application $appName"
205 echo " source $srcDir"
206 echo " target $newDir"
208 echo " syncing ..."
209 # sync updated files only, itemize changes so we know what is going on
210 rsync -aui $srcDir/ $newDir
214 # reuse or create new FOAM_SETTINGS (useful for queuing systems)
216 if [ -e "$newDir/FOAM_SETTINGS" ]
217 then
218 echo " retaining FOAM_SETTINGS"
219 else
220 echo " creating FOAM_SETTINGS"
221 cat << SETTINGS > "$newDir/FOAM_SETTINGS"
222 APPLICATION=$appName
223 FOAM_VERSION=OpenFOAM-$version
224 SETTINGS
227 echo Done
229 #------------------------------------------------------------------------------