2 #------------------------------------------------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
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 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
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
30 # extracts info from log file
33 # -solution singularity not handled
34 #------------------------------------------------------------------------------
37 PROGNAME
=`basename $0`
39 if [ -r $HOME/.
${PROGNAME}.db
]; then
40 DBFILE
=$HOME/.
${PROGNAME}.db
42 DBFILE
=$PROGDIR/$PROGNAME.db
48 $PROGNAME - extracts xy files from Foam logs.
50 Usage: $PROGNAME [-n][-s] <log>
51 extracts xy files from log
53 lists but does not extract
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
100 if [ "$VERBOSE" ]; then
105 # getSolvedVars logFile
106 # Prints names of all 'solved for' variables in the log file.
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
115 if [ ! -f "$1" ]; then
116 echo "Cannot find dbFile $1"
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
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
147 echo "${var}FinalRes"
152 # getAllQueries dbFile logFile
153 # Gets all queries from database and from logfile
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
166 line
=`egrep "$LINEQ" $2`
168 column=`echo "$line" | fgrep "$NUMQ"`
169 if [ "$column" ]; then
170 queries
="$queries $var"
181 #-----------------------------
183 #-----------------------------
190 while getopts nslh flags
210 shift `expr $OPTIND - 1`
212 if [ ! -f $DBFILE ]; then
213 echo "$PROGNAME: Cannot read database $DBFILE"
217 if [ "$LISTONLY" ]; then
218 if [ $# -ne 1 ]; then
223 if [ ! -r $LOG ]; then
224 echo "$PROGNAME: Cannot read log $LOG"
227 getAllQueries
$DBFILE $LOG
231 if [ $# -ne 1 ]; then
238 if [ ! -r $LOG ]; then
239 echo "$PROGNAME: Cannot read log $LOG"
243 QUERYNAMES
=`getAllQueries $DBFILE $LOG`
246 if [ ! "$CASEDIR" ]; then
251 if [ ! -d "$CASEDIR" ]; then
252 echo "$PROGNAME: Cannot read $CASEDIR"
256 if [ ! -f "$LOG" ]; then
257 echo "$PROGNAME: Cannot read log file $LOG"
262 #-- Make logs dir in case directory and put awk file there.
264 mkdir
-p $CASEDIR/logs
265 AWKFILE
=$CASEDIR/logs
/$PROGNAME.
awk
269 myEcho
" database : $DBFILE"
270 myEcho
" awk file : $AWKFILE"
271 myEcho
" files to : $CASEDIR/logs"
275 #-----------------------------
276 # Generate Awk program
277 #-----------------------------
283 rm -f $AWKFILE; touch $AWKFILE
284 echo "BEGIN {" >> $AWKFILE
285 echo " Iteration=0" >> $AWKFILE
286 echo " resetCounters()" >> $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
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
306 cat <<LABEL >> $AWKFILE
307 # Extract value after columnSel
308 function extract(inLine,columnSel,outVar,
311 a=index(inLine, columnSel)
313 split(substr(inLine, a+b),outVar)
314 gsub("[,:]","",outVar[1])
323 #-- Generate code for iteration separator (increments 'Iteration')
324 getQueries
$DBFILE 'Separator'
325 cat <<LABSEP >> $AWKFILE
326 #-- Iteration separator (increments 'Iteration')
335 #-- Generate code for extracting Time
336 getQueries
$DBFILE 'Time'
337 cat <<LABTIME >> $AWKFILE
338 #-- Time extraction (sets 'Time')
340 extract(\$0, "$NUMQ", val)
347 #-- Generate code for singularity handling.
348 cat <<LABSING >> $AWKFILE
349 #-- Skip whole line with singularity variable
350 /solution singularity/ {
356 #-- Generate code for extracting solved for quantities
357 cat <<LABSOLVE >> $AWKFILE
358 #-- Extraction of any solved for variable
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
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
403 #-----------------------------
404 # Run awk program on log
405 #-----------------------------
407 cmd
="awk -f $AWKFILE $LOG"
408 myEcho
"Executing: $cmd"
413 #-----------------------------
415 #-----------------------------
416 myEcho
"Generated XY files for:"
417 getAllQueries
$DBFILE $LOG
419 #------------------------------------------------------------------------------