STYLE: date
[OpenFOAM-2.0.x.git] / bin / foamLog
blobfd3e6c72971b183d0b328321800452b6d415a52f
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2004-2011 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 # foamLog
28 # Description
29 # extracts info from log file
31 # Bugs
32 # -solution singularity not handled
33 #------------------------------------------------------------------------------
34 Script=${0##*/}
35 toolsDir=${0%/*}/tools
36 siteDir=${WM_PROJECT_SITE:-${WM_PROJECT_INST_DIR:-<unknown>}/site}
37 userDir=$HOME/.OpenFOAM
39 usage() {
40 exec 1>&2
41 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
42 cat <<USAGE
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
49 -help print the usage
51 $Script - extracts xy files from OpenFOAM logs.
53 USAGE
54 exit 1
57 #------------------------------------------------------------------------------
59 printHelp() {
60 cat <<HELP
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
71 a time step.
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,
82 separated with '/' :
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
91 $userDir
92 $siteDir/$WM_PROJECT_VERSION
93 $siteDir
94 $WM_PROJECT_DIR/etc
95 $toolsDir
97 Option -q suppresses the default information and only prints the extracted
98 variables.
99 -----------------------------------------------------------------------------
100 HELP
102 usage
106 timeName=Time
107 unset listOpt quietOpt localDB
109 # parse options
110 while [ "$#" -gt 0 ]
112 case "$1" in
113 -h | -help)
114 printHelp
115 exit 0
118 unset timeName
119 shift
121 -l | -list)
122 listOpt=true
123 shift
125 -q | -quiet | -s | -silent)
126 quietOpt=true
127 shift
129 -localDB)
130 localDB=true
131 shift
134 usage "unknown option: '$*'"
137 break
139 esac
140 done
142 # find the database file
143 DBFILE=$Script.db
144 [ -f $DBFILE ] || DBFILE=`foamEtcFile $Script.db` || DBFILE=$toolsDir/$Script.db
146 # need the database file
147 [ -f $DBFILE ] || {
148 echo "$Script: Cannot read database $DBFILE"
149 exit 1
152 # single logFile
153 if [ $# -eq 1 ]
154 then
155 LOG=$1
156 [ -r "$LOG" ] && [ -f "$LOG" ] || usage "Cannot read log $LOG"
157 else
158 usage
162 myEcho()
164 [ "$quietOpt" = true ] || echo "$*"
168 # getSolvedVars logFile
169 # Prints names of all 'solved for' variables in the log file.
170 getSolvedVars()
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
178 getQueries()
180 dbFile=$1
181 queryName=$2
183 [ -f "$dbFile" ] || {
184 echo "Cannot find dbFile $dbFile"
185 exit 1
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" ]
193 #then
194 # echo "Did not find query for $2 in database $1" 1>&2
199 # getDbQueryList dbFile
200 # Echoes list of possible queries
201 getDbQueryList()
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
209 getSolveQueryList()
211 solvedVars=`getSolvedVars $1`
213 for var in $solvedVars
215 echo "${var}"
216 echo "${var}FinalRes"
217 echo "${var}Iters"
218 done
222 # getAllQueries dbFile logFile
223 # Gets all queries from database and from logfile
224 getAllQueries()
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
236 getQueries $1 "$var"
237 line=`egrep "$LINEQ" $2`
238 if [ "$line" ]
239 then
240 column=`echo "$line" | fgrep "$NUMQ"`
241 if [ "$column" ]
242 then
243 queries="$queries $var"
246 done
248 for q in $queries
250 echo "$q"
251 done | sort -u
255 #-----------------------------
256 # Main
257 #-----------------------------
259 if [ "$listOpt" = true ]
260 then
261 getAllQueries $DBFILE $LOG
262 exit 0
265 caseDir=.
266 outputDir=$caseDir/logs
268 [ -d "$caseDir" ] || {
269 echo "$Script: Cannot read $caseDir"
270 exit 1
273 QUERYNAMES=`getAllQueries $DBFILE $LOG`
276 # Make logs dir in case directory and place awk file there
278 mkdir -p $outputDir
279 AWKFILE=$outputDir/$Script.awk
281 myEcho "Using:"
282 myEcho " log : $LOG"
283 myEcho " database : $DBFILE"
284 myEcho " awk file : $AWKFILE"
285 myEcho " files to : $outputDir"
286 myEcho ""
289 #-----------------------------
290 # Generate Awk program
291 #-----------------------------
293 rm -f $AWKFILE 2> /dev/null
294 cat << AWK_CONTENTS > $AWKFILE
295 # header
296 BEGIN {
297 Iteration=0
298 resetCounters()
301 # reset counters used for variable postfix
302 function resetCounters() {
303 AWK_CONTENTS
304 # ----------
306 for queryName in $QUERYNAMES
308 varName=${queryName}Cnt
309 echo " ${varName}=0" >> $AWKFILE
310 done
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
317 echo "}" >> $AWKFILE
318 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)
326 b=length(columnSel)
327 split(substr(inLine, a+b),outVar)
328 gsub("[,:]","",outVar[1])
331 AWK_CONTENTS
332 # ----------
335 # Code for iteration separator (increments 'Iteration')
337 getQueries $DBFILE 'Separator'
338 cat << AWK_CONTENTS >> $AWKFILE
339 # Iteration separator (increments 'Iteration')
340 /$LINEQ/ {
341 Iteration++
342 resetCounters()
345 AWK_CONTENTS
346 # ----------
349 # Code for extracting Time
351 getQueries $DBFILE 'Time'
352 cat << AWK_CONTENTS >> $AWKFILE
353 # Time extraction (sets 'Time')
354 /$LINEQ/ {
355 extract(\$0, "$NUMQ", val)
356 Time=val[1]
359 AWK_CONTENTS
360 # ----------
363 # Code for singularity handling.
365 cat << AWK_CONTENTS >> $AWKFILE
366 # Skip whole line with singularity variable
367 /solution singularity/ {
368 next;
371 AWK_CONTENTS
372 # ----------
375 # Code for extracting solved for quantities
377 [ "$localDB" = true ] ||
378 cat << AWK_CONTENTS >> $AWKFILE
379 # Extraction of any solved for variable
380 /Solving for/ {
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
402 AWK_CONTENTS
403 # ----------
406 # Code to process queries
408 for queryName in $QUERYNAMES
410 getQueries $DBFILE $queryName
411 if [ "$LINEQ" -a "$NUMQ" ]
412 then
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
421 echo "}" >> $AWKFILE
422 echo "" >> $AWKFILE
424 done
427 #-----------------------------
428 # Run awk program on log
429 #-----------------------------
431 cmd="awk -f $AWKFILE $LOG"
432 myEcho "Executing: $cmd"
433 $cmd
434 myEcho ""
437 #-----------------------------
438 # Print found
439 #-----------------------------
440 myEcho "Generated XY files for:"
441 [ "$quietOpt" = true ] || getAllQueries $DBFILE $LOG
442 myEcho "End"
444 #------------------------------------------------------------------------------