2 #------------------------------------------------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 2004-2011 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 #------------------------------------------------------------------------------
35 toolsDir
=${0%/*}/tools
36 siteDir
=${WM_PROJECT_SITE:-${WM_PROJECT_INST_DIR:-<unknown>}/site}
37 userDir
=$HOME/.OpenFOAM
41 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
44 Usage: $Script [OPTIONS] <log>
45 -list lists but does not extract
46 -n create single column files with the extracted data only
47 -quiet quiet operation
48 -localDB only use the local database file
51 $Script - extracts xy files from OpenFOAM logs.
57 #------------------------------------------------------------------------------
61 -----------------------------------------------------------------------------
62 The default is to extract for all the 'Solved for' variables the initial
63 residual, the final residual and the number of iterations. Additionally, a
64 (user editable) database is used to extract data for standard non-solved for
65 variables like Courant number, and execution time.
67 $Script -l lists all the possible variables without extracting them.
69 The program will generate and run an awk script which writes a set of files,
70 logs/<var>_<subIter>, for every <var> specified, for every occurrence inside
73 For variables that are 'Solved for', the initial residual name will be
74 <var>, the final residual receive the name <var>FinalRes,
76 The files are output in a simple xy format with the first column Time (default)
77 and the second the extracted values. Option -n creates single column
78 files with the extracted data only.
81 The query database is a simple text format with three entries per line,
83 Column 1 is the name of the variable (cannot contain spaces).
84 Column 2 is the extended regular expression (egrep) to select the line.
85 Column 3 is the string (fgrep) to select the column inside the line.
86 The value taken will be the first (non-space)word after this column.
87 The database ($Script.db) will taken from these locations:
90 $userDir/$WM_PROJECT_VERSION
92 $siteDir/$WM_PROJECT_VERSION
97 Option -q suppresses the default information and only prints the extracted
99 -----------------------------------------------------------------------------
107 unset listOpt quietOpt localDB
125 -q |
-quiet |
-s |
-silent)
134 usage
"unknown option: '$*'"
142 # find the database file
144 [ -f $DBFILE ] || DBFILE
=`foamEtcFile $Script.db` || DBFILE
=$toolsDir/$Script.db
146 # need the database file
148 echo "$Script: Cannot read database $DBFILE"
156 [ -r "$LOG" ] && [ -f "$LOG" ] || usage
"Cannot read log $LOG"
164 [ "$quietOpt" = true
] ||
echo "$*"
168 # getSolvedVars logFile
169 # Prints names of all 'solved for' variables in the log file.
172 fgrep
' Solving for ' $1 | fgrep
',' |
sed -e 's/.* Solving for \([^,]*\)[,:].*/\1/' |
sort -u
176 # getQueries dbFile queryName
177 # Gets regular expressions for a certain queryName from the database
183 [ -f "$dbFile" ] ||
{
184 echo "Cannot find dbFile $dbFile"
188 LINEQ
=`grep -v '^#' $dbFile | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $2}'`
189 NUMQ
=`grep -v '^#' $dbFile | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $3}'`
191 #echo "For $queryName found line selection /$LINEQ/ , column selection /$NUMQ/" 1>&2
192 #if [ ! "$LINEQ" -o ! "$NUMQ" ]
194 # echo "Did not find query for $2 in database $1" 1>&2
199 # getDbQueryList dbFile
200 # Echoes list of possible queries
203 grep -v '^#' $1 |
grep '[^ \t]' |
awk -F '/' '{print $1}'
207 # getSolveQueryList logFile
208 # Echoes list of queries from "solved for" variables in log file
211 solvedVars
=`getSolvedVars $1`
213 for var
in $solvedVars
216 echo "${var}FinalRes"
222 # getAllQueries dbFile logFile
223 # Gets all queries from database and from logfile
226 #-- All solved for queries from log file
227 [ "$localDB" = true
] || queries
=`getSolveQueryList $2`
229 #-- Add ones from database, present in log file
230 # Note: just like awk, line selected with regular expression,
231 # column with string.
232 dbQueries
=`getDbQueryList $1`
234 for var
in $dbQueries
237 line
=`egrep "$LINEQ" $2`
240 column=`echo "$line" | fgrep "$NUMQ"`
243 queries
="$queries $var"
255 #-----------------------------
257 #-----------------------------
259 if [ "$listOpt" = true
]
261 getAllQueries
$DBFILE $LOG
266 outputDir
=$caseDir/logs
268 [ -d "$caseDir" ] ||
{
269 echo "$Script: Cannot read $caseDir"
273 QUERYNAMES
=`getAllQueries $DBFILE $LOG`
276 # Make logs dir in case directory and place awk file there
279 AWKFILE
=$outputDir/$Script.
awk
283 myEcho
" database : $DBFILE"
284 myEcho
" awk file : $AWKFILE"
285 myEcho
" files to : $outputDir"
289 #-----------------------------
290 # Generate Awk program
291 #-----------------------------
293 rm -f $AWKFILE 2> /dev
/null
294 cat << AWK_CONTENTS > $AWKFILE
301 # reset counters used for variable postfix
302 function resetCounters() {
306 for queryName
in $QUERYNAMES
308 varName
=${queryName}Cnt
309 echo " ${varName}=0" >> $AWKFILE
312 echo " # Reset counters for general Solving for extraction" >> $AWKFILE
313 echo " for (varName in subIter)" >> $AWKFILE
314 echo " {" >> $AWKFILE
315 echo " subIter[varName]=0" >> $AWKFILE
316 echo " }" >> $AWKFILE
321 cat << AWK_CONTENTS >> $AWKFILE
322 # Extract value after columnSel
323 function extract(inLine,columnSel,outVar,a,b)
325 a=index(inLine, columnSel)
327 split(substr(inLine, a+b),outVar)
328 gsub("[,:]","",outVar[1])
335 # Code for iteration separator (increments 'Iteration')
337 getQueries
$DBFILE 'Separator'
338 cat << AWK_CONTENTS >> $AWKFILE
339 # Iteration separator (increments 'Iteration')
349 # Code for extracting Time
351 getQueries
$DBFILE 'Time'
352 cat << AWK_CONTENTS >> $AWKFILE
353 # Time extraction (sets 'Time')
355 extract(\$0, "$NUMQ", val)
363 # Code for singularity handling.
365 cat << AWK_CONTENTS >> $AWKFILE
366 # Skip whole line with singularity variable
367 /solution singularity/ {
375 # Code for extracting solved for quantities
377 [ "$localDB" = true
] ||
378 cat << AWK_CONTENTS >> $AWKFILE
379 # Extraction of any solved for variable
381 extract(\$0, "Solving for ", varNameVal)
383 varName=varNameVal[1]
384 file=varName "_" subIter[varName]++
385 file="$outputDir/" file
386 extract(\$0, "Initial residual = ", val)
387 print $timeName "\t" val[1] > file
389 varName=varNameVal[1] "FinalRes"
390 file=varName "_" subIter[varName]++
391 file="$outputDir/" file
392 extract(\$0, "Final residual = ", val)
393 print $timeName "\t" val[1] > file
395 varName=varNameVal[1] "Iters"
396 file=varName "_" subIter[varName]++
397 file="$outputDir/" file
398 extract(\$0, "No Iterations ", val)
399 print $timeName "\t" val[1] > file
406 # Code to process queries
408 for queryName
in $QUERYNAMES
410 getQueries
$DBFILE $queryName
411 if [ "$LINEQ" -a "$NUMQ" ]
413 counter
=${queryName}Cnt
415 echo "# Extraction of $queryName" >> $AWKFILE
416 echo "/$LINEQ/ {" >> $AWKFILE
417 echo " extract(\$0, \"$NUMQ\", val)" >> $AWKFILE
418 echo " file=\"$outputDir/${queryName}_\" ${counter}" >> $AWKFILE
419 echo " print $timeName \"\\t\" val[1] > file" >> $AWKFILE
420 echo " ${counter}++" >> $AWKFILE
427 #-----------------------------
428 # Run awk program on log
429 #-----------------------------
431 cmd
="awk -f $AWKFILE $LOG"
432 myEcho
"Executing: $cmd"
437 #-----------------------------
439 #-----------------------------
440 myEcho
"Generated XY files for:"
441 [ "$quietOpt" = true
] || getAllQueries
$DBFILE $LOG
444 #------------------------------------------------------------------------------