2 #---------------------------------*- sh -*-------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 1991-2010 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
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
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/>.
29 # extracts info from log file
32 # -solution singularity not handled
33 #------------------------------------------------------------------------------
36 PROGNAME
=`basename $0`
38 if [ -r $HOME/.
${PROGNAME}.db
]; then
39 DBFILE
=$HOME/.
${PROGNAME}.db
41 DBFILE
=$PROGDIR/$PROGNAME.db
47 $PROGNAME - extracts xy files from Foam logs.
49 Usage: $PROGNAME [-n][-s] <log>
50 extracts xy files from log
52 lists but does not extract
63 The default is to extract for all the 'Solved for' variables the
64 initial residual, the final residual and the number of iterations. On
65 top of this a (user editable) database of standard non-solved for
66 variables is used to extract data like Courant number, execution time.
68 $PROGNAME -l shows all the possible variables but does not extract them.
70 The program will generate and run an awk script which writes a set of
71 files, logs/<var>_<subIter>, for every <var> specified, for every
72 occurrence inside a time step.
74 For variables that are 'Solved for' the initial residual name will
75 be <var>, the final residual will get name <var>FinalRes,
77 The files are a simple xy format with the first column Time (default)
78 and the second the extracted values. Option -n creates single column
79 files with the extracted data only.
82 The query database is a simple text format with three entries per line,
83 separated with '/'. Column 1 is the name of the variable (cannot contain
84 spaces), column 2 is the extended regular expression (egrep) to select
85 the line and column 3 is the string (fgrep) to select the column inside the
86 line. The value taken will be the first (non-space)word after this
87 column. The database will either be \$HOME/.${PROGNAME}.db or if not
88 found $PROGDIR/${PROGNAME}.db.
90 Option -s suppresses the default information and only prints the extracted
99 if [ "$VERBOSE" ]; then
104 # getSolvedVars logFile
105 # Prints names of all 'solved for' variables in the log file.
107 fgrep
' Solving for ' $1 | fgrep
',' |
sed -e 's/.* Solving for \([^,]*\)[,:].*/\1/' |
sort -u
111 # getQueries dbFile queryName
112 # Gets regular expressions for a certain queryName from the database
114 if [ ! -f "$1" ]; then
115 echo "Cannot find dbFile $1"
121 LINEQ
=`grep -v '^#' $1 | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $2}'`
122 NUMQ
=`grep -v '^#' $1 | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $3}'`
124 #echo "For $queryName found line selection /$LINEQ/ , column selection /$NUMQ/" 1>&2
125 #if [ ! "$LINEQ" -o ! "$NUMQ" ]; then
126 # echo "Did not find query for $2 in database $1" 1>&2
131 # getDbQueryList dbFile
132 # Echoes list of possible queries
134 grep -v '^#' $1 |
grep '[^ \t]' |
awk -F '/' '{print $1}'
138 # getSolveQueryList logFile
139 # Echoes list of queries from "solved for" variables in log file
140 getSolveQueryList
() {
141 solvedVars
=`getSolvedVars $1`
143 for var
in $solvedVars
146 echo "${var}FinalRes"
151 # getAllQueries dbFile logFile
152 # Gets all queries from database and from logfile
154 #-- All solved for queries from log file
155 queries
=`getSolveQueryList $2`
157 #-- Add ones from database, present in log file
158 # Note: just like awk, line selected with regular expression,
159 # column with string.
160 dbQueries
=`getDbQueryList $1`
162 for var
in $dbQueries
165 line
=`egrep "$LINEQ" $2`
167 column=`echo "$line" | fgrep "$NUMQ"`
168 if [ "$column" ]; then
169 queries
="$queries $var"
180 #-----------------------------
182 #-----------------------------
189 while getopts nslh flags
209 shift `expr $OPTIND - 1`
211 if [ ! -f $DBFILE ]; then
212 echo "$PROGNAME: Cannot read database $DBFILE"
216 if [ "$LISTONLY" ]; then
217 if [ $# -ne 1 ]; then
222 if [ ! -r $LOG ]; then
223 echo "$PROGNAME: Cannot read log $LOG"
226 getAllQueries
$DBFILE $LOG
230 if [ $# -ne 1 ]; then
237 if [ ! -r $LOG ]; then
238 echo "$PROGNAME: Cannot read log $LOG"
242 QUERYNAMES
=`getAllQueries $DBFILE $LOG`
245 if [ ! "$CASEDIR" ]; then
250 if [ ! -d "$CASEDIR" ]; then
251 echo "$PROGNAME: Cannot read $CASEDIR"
255 if [ ! -f "$LOG" ]; then
256 echo "$PROGNAME: Cannot read log file $LOG"
261 #-- Make logs dir in case directory and put awk file there.
263 mkdir
-p $CASEDIR/logs
264 AWKFILE
=$CASEDIR/logs
/$PROGNAME.
awk
268 myEcho
" database : $DBFILE"
269 myEcho
" awk file : $AWKFILE"
270 myEcho
" files to : $CASEDIR/logs"
274 #-----------------------------
275 # Generate Awk program
276 #-----------------------------
282 rm -f $AWKFILE; touch $AWKFILE
283 echo "BEGIN {" >> $AWKFILE
284 echo " Iteration=0" >> $AWKFILE
285 echo " resetCounters()" >> $AWKFILE
289 echo "# reset counters used for variable postfix" >> $AWKFILE
290 echo "function resetCounters() {" >> $AWKFILE
291 for queryName
in $QUERYNAMES
293 varName
=${queryName}Cnt
294 echo " ${varName}=0" >> $AWKFILE
296 echo " # Reset counters for general Solving for extraction" >> $AWKFILE
297 echo " for (varName in subIter)" >> $AWKFILE
298 echo " {" >> $AWKFILE
299 echo " subIter[varName]=0" >> $AWKFILE
300 echo " }" >> $AWKFILE
305 cat <<LABEL >> $AWKFILE
306 # Extract value after columnSel
307 function extract(inLine,columnSel,outVar,
310 a=index(inLine, columnSel)
312 split(substr(inLine, a+b),outVar)
313 gsub("[,:]","",outVar[1])
322 #-- Generate code for iteration separator (increments 'Iteration')
323 getQueries
$DBFILE 'Separator'
324 cat <<LABSEP >> $AWKFILE
325 #-- Iteration separator (increments 'Iteration')
334 #-- Generate code for extracting Time
335 getQueries
$DBFILE 'Time'
336 cat <<LABTIME >> $AWKFILE
337 #-- Time extraction (sets 'Time')
339 extract(\$0, "$NUMQ", val)
346 #-- Generate code for singularity handling.
347 cat <<LABSING >> $AWKFILE
348 #-- Skip whole line with singularity variable
349 /solution singularity/ {
355 #-- Generate code for extracting solved for quantities
356 cat <<LABSOLVE >> $AWKFILE
357 #-- Extraction of any solved for variable
359 extract(\$0, "Solving for ", varNameVal)
361 varName=varNameVal[1]
362 file=varName "_" subIter[varName]++
363 file="$CASEDIR/logs/" file
364 extract(\$0, "Initial residual = ", val)
365 print $TIMENAME "\t" val[1] > file
367 varName=varNameVal[1] "FinalRes"
368 file=varName "_" subIter[varName]++
369 file="$CASEDIR/logs/" file
370 extract(\$0, "Final residual = ", val)
371 print $TIMENAME "\t" val[1] > file
373 varName=varNameVal[1] "Iters"
374 file=varName "_" subIter[varName]++
375 file="$CASEDIR/logs/" file
376 extract(\$0, "No Iterations ", val)
377 print $TIMENAME "\t" val[1] > file
382 #-- generate code to process queries
383 for queryName
in $QUERYNAMES
385 getQueries
$DBFILE $queryName
386 if [ "$LINEQ" -a "$NUMQ" ]; then
387 counter
=${queryName}Cnt
389 echo "#-- Extraction of $queryName" >> $AWKFILE
390 echo "/$LINEQ/ {" >> $AWKFILE
391 echo " extract(\$0, \"$NUMQ\", val)" >> $AWKFILE
392 echo " file=\"$CASEDIR/logs/${queryName}_\" ${counter}" >> $AWKFILE
393 echo " print $TIMENAME \"\\t\" val[1] > file" >> $AWKFILE
394 echo " ${counter}++" >> $AWKFILE
402 #-----------------------------
403 # Run awk program on log
404 #-----------------------------
406 cmd
="awk -f $AWKFILE $LOG"
407 myEcho
"Executing: $cmd"
412 #-----------------------------
414 #-----------------------------
415 myEcho
"Generated XY files for:"
416 getAllQueries
$DBFILE $LOG
418 #------------------------------------------------------------------------------