ThirdParty packages: Add comment in README file. Contributed by Darrin Stephens
[OpenFOAM-1.6-ext.git] / bin / foamLog
blob6ca32e67bd4fb75967bdda8a87d5424843bc2906
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright held by original author
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 # foamLog
29 # Description
30 # extracts info from log file
32 # Bugs
33 # -solution singularity not handled
34 #------------------------------------------------------------------------------
36 PROGDIR=`dirname $0`
37 PROGNAME=`basename $0`
39 if [ -r $HOME/.${PROGNAME}.db ]; then
40 DBFILE=$HOME/.${PROGNAME}.db
41 else
42 DBFILE=$PROGDIR/$PROGNAME.db
46 printUsage() {
47 cat <<LABUSAGE
48 $PROGNAME - extracts xy files from Foam logs.
50 Usage: $PROGNAME [-n][-s] <log>
51 extracts xy files from log
52 $PROGNAME -l <log>
53 lists but does not extract
54 $PROGNAME -h
55 for a help message
57 LABUSAGE
61 printHelp() {
62 printUsage
63 cat <<LABHELP
64 The default is to extract for all the 'Solved for' variables the
65 initial residual, the final residual and the number of iterations. On
66 top of this a (user editable) database of standard non-solved for
67 variables is used to extract data like Courant number, execution time.
69 $PROGNAME -l shows all the possible variables but does not extract them.
71 The program will generate and run an awk script which writes a set of
72 files, logs/<var>_<subIter>, for every <var> specified, for every
73 occurrence inside a time step.
75 For variables that are 'Solved for' the initial residual name will
76 be <var>, the final residual will get name <var>FinalRes,
78 The files are a simple xy format with the first column Time (default)
79 and the second the extracted values. Option -n creates single column
80 files with the extracted data only.
83 The query database is a simple text format with three entries per line,
84 separated with '/'. Column 1 is the name of the variable (cannot contain
85 spaces), column 2 is the extended regular expression (egrep) to select
86 the line and column 3 is the string (fgrep) to select the column inside the
87 line. The value taken will be the first (non-space)word after this
88 column. The database will either be \$HOME/.${PROGNAME}.db or if not
89 found $PROGDIR/${PROGNAME}.db.
91 Option -s suppresses the default information and only prints the extracted
92 variables.
94 LABHELP
99 myEcho() {
100 if [ "$VERBOSE" ]; then
101 echo "$*"
105 # getSolvedVars logFile
106 # Prints names of all 'solved for' variables in the log file.
107 getSolvedVars() {
108 fgrep ' Solving for ' $1 | fgrep ',' | sed -e 's/.* Solving for \([^,]*\)[,:].*/\1/' | sort -u
112 # getQueries dbFile queryName
113 # Gets regular expressions for a certain queryName from the database
114 getQueries() {
115 if [ ! -f "$1" ]; then
116 echo "Cannot find dbFile $1"
117 exit 1
120 queryName=$2
122 LINEQ=`grep -v '^#' $1 | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $2}'`
123 NUMQ=`grep -v '^#' $1 | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $3}'`
125 #echo "For $queryName found line selection /$LINEQ/ , column selection /$NUMQ/" 1>&2
126 #if [ ! "$LINEQ" -o ! "$NUMQ" ]; then
127 # echo "Did not find query for $2 in database $1" 1>&2
132 # getDbQueryList dbFile
133 # Echoes list of possible queries
134 getDbQueryList() {
135 grep -v '^#' $1 | grep '[^ \t]' | awk -F '/' '{print $1}'
139 # getSolveQueryList logFile
140 # Echoes list of queries from "solved for" variables in log file
141 getSolveQueryList() {
142 solvedVars=`getSolvedVars $1`
144 for var in $solvedVars
146 echo "${var}"
147 echo "${var}FinalRes"
148 echo "${var}Iters"
149 done
152 # getAllQueries dbFile logFile
153 # Gets all queries from database and from logfile
154 getAllQueries() {
155 #-- All solved for queries from log file
156 queries=`getSolveQueryList $2`
158 #-- Add ones from database, present in log file
159 # Note: just like awk, line selected with regular expression,
160 # column with string.
161 dbQueries=`getDbQueryList $1`
163 for var in $dbQueries
165 getQueries $1 "$var"
166 line=`egrep "$LINEQ" $2`
167 if [ "$line" ]; then
168 column=`echo "$line" | fgrep "$NUMQ"`
169 if [ "$column" ]; then
170 queries="$queries $var"
173 done
175 for q in $queries
177 echo "$q"
178 done | sort -u
181 #-----------------------------
182 # Main
183 #-----------------------------
185 # sort arguments
186 TIMENAME='Time'
187 VERBOSE='yes'
188 LISTONLY=''
190 while getopts nslh flags
192 case $flags in
193 n) TIMENAME=""
195 h) printHelp
196 exit 0
198 s) VERBOSE=""
200 l) LISTONLY='yes'
202 \?) printUsage
203 exit 1
205 esac
206 done
209 # Shift options
210 shift `expr $OPTIND - 1`
212 if [ ! -f $DBFILE ]; then
213 echo "$PROGNAME: Cannot read database $DBFILE"
214 exit 1
217 if [ "$LISTONLY" ]; then
218 if [ $# -ne 1 ]; then
219 printUsage
220 exit 1
222 LOG=$1;
223 if [ ! -r $LOG ]; then
224 echo "$PROGNAME: Cannot read log $LOG"
225 exit 1
227 getAllQueries $DBFILE $LOG
228 exit 0
231 if [ $# -ne 1 ]; then
232 printUsage
233 exit 1
236 CASEDIR=.
237 LOG=$1
238 if [ ! -r $LOG ]; then
239 echo "$PROGNAME: Cannot read log $LOG"
240 exit 1
243 QUERYNAMES=`getAllQueries $DBFILE $LOG`
246 if [ ! "$CASEDIR" ]; then
247 printUsage
248 exit 1
251 if [ ! -d "$CASEDIR" ]; then
252 echo "$PROGNAME: Cannot read $CASEDIR"
253 exit 1
256 if [ ! -f "$LOG" ]; then
257 echo "$PROGNAME: Cannot read log file $LOG"
258 exit 1
262 #-- Make logs dir in case directory and put awk file there.
264 mkdir -p $CASEDIR/logs
265 AWKFILE=$CASEDIR/logs/$PROGNAME.awk
267 myEcho "Using:"
268 myEcho " log : $LOG"
269 myEcho " database : $DBFILE"
270 myEcho " awk file : $AWKFILE"
271 myEcho " files to : $CASEDIR/logs"
272 myEcho ""
275 #-----------------------------
276 # Generate Awk program
277 #-----------------------------
281 #-- header
283 rm -f $AWKFILE; touch $AWKFILE
284 echo "BEGIN {" >> $AWKFILE
285 echo " Iteration=0" >> $AWKFILE
286 echo " resetCounters()" >> $AWKFILE
287 echo "}" >> $AWKFILE
289 echo "" >> $AWKFILE
290 echo "# reset counters used for variable postfix" >> $AWKFILE
291 echo "function resetCounters() {" >> $AWKFILE
292 for queryName in $QUERYNAMES
294 varName=${queryName}Cnt
295 echo " ${varName}=0" >> $AWKFILE
296 done
297 echo " # Reset counters for general Solving for extraction" >> $AWKFILE
298 echo " for (varName in subIter)" >> $AWKFILE
299 echo " {" >> $AWKFILE
300 echo " subIter[varName]=0" >> $AWKFILE
301 echo " }" >> $AWKFILE
302 echo "}" >> $AWKFILE
303 echo "" >> $AWKFILE
306 cat <<LABEL >> $AWKFILE
307 # Extract value after columnSel
308 function extract(inLine,columnSel,outVar,
309 a,b)
311 a=index(inLine, columnSel)
312 b=length(columnSel)
313 split(substr(inLine, a+b),outVar)
314 gsub("[,:]","",outVar[1])
317 LABEL
323 #-- Generate code for iteration separator (increments 'Iteration')
324 getQueries $DBFILE 'Separator'
325 cat <<LABSEP >> $AWKFILE
326 #-- Iteration separator (increments 'Iteration')
327 /$LINEQ/ {
328 Iteration++
329 resetCounters()
332 LABSEP
335 #-- Generate code for extracting Time
336 getQueries $DBFILE 'Time'
337 cat <<LABTIME >> $AWKFILE
338 #-- Time extraction (sets 'Time')
339 /$LINEQ/ {
340 extract(\$0, "$NUMQ", val)
341 Time=val[1]
344 LABTIME
347 #-- Generate code for singularity handling.
348 cat <<LABSING >> $AWKFILE
349 #-- Skip whole line with singularity variable
350 /solution singularity/ {
351 next;
353 LABSING
356 #-- Generate code for extracting solved for quantities
357 cat <<LABSOLVE >> $AWKFILE
358 #-- Extraction of any solved for variable
359 /Solving for/ {
360 extract(\$0, "Solving for ", varNameVal)
362 varName=varNameVal[1]
363 file=varName "_" subIter[varName]++
364 file="$CASEDIR/logs/" file
365 extract(\$0, "Initial residual = ", val)
366 print $TIMENAME "\t" val[1] > file
368 varName=varNameVal[1] "FinalRes"
369 file=varName "_" subIter[varName]++
370 file="$CASEDIR/logs/" file
371 extract(\$0, "Final residual = ", val)
372 print $TIMENAME "\t" val[1] > file
374 varName=varNameVal[1] "Iters"
375 file=varName "_" subIter[varName]++
376 file="$CASEDIR/logs/" file
377 extract(\$0, "No Iterations ", val)
378 print $TIMENAME "\t" val[1] > file
381 LABSOLVE
383 #-- generate code to process queries
384 for queryName in $QUERYNAMES
386 getQueries $DBFILE $queryName
387 if [ "$LINEQ" -a "$NUMQ" ]; then
388 counter=${queryName}Cnt
390 echo "#-- Extraction of $queryName" >> $AWKFILE
391 echo "/$LINEQ/ {" >> $AWKFILE
392 echo " extract(\$0, \"$NUMQ\", val)" >> $AWKFILE
393 echo " file=\"$CASEDIR/logs/${queryName}_\" ${counter}" >> $AWKFILE
394 echo " print $TIMENAME \"\\t\" val[1] > file" >> $AWKFILE
395 echo " ${counter}++" >> $AWKFILE
396 echo "}" >> $AWKFILE
397 echo "" >> $AWKFILE
399 done
403 #-----------------------------
404 # Run awk program on log
405 #-----------------------------
407 cmd="awk -f $AWKFILE $LOG"
408 myEcho "Executing: $cmd"
409 $cmd
410 myEcho ""
413 #-----------------------------
414 # Print found
415 #-----------------------------
416 myEcho "Generated XY files for:"
417 getAllQueries $DBFILE $LOG
419 #------------------------------------------------------------------------------