ENH: Update FreeFOAM contributions to GPL v3
[freefoam.git] / bin / freefoam-solverSweeps.py.in
blobd0305c076189ca2cb649e1fb67056a2d32250b9f
1 #!@PYTHON_EXECUTABLE@
2 #-------------------------------------------------------------------------------
3 # ______ _ ____ __ __
4 # | ____| _| |_ / __ \ /\ | \/ |
5 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
6 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
7 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
8 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
10 # FreeFOAM: The Cross-Platform CFD Toolkit
12 # Copyright (C) 2008-2012 Michael Wild <themiwi@users.sf.net>
13 # Gerber van der Graaf <gerber_graaf@users.sf.net>
14 #-------------------------------------------------------------------------------
15 # License
16 # This file is part of FreeFOAM.
18 # FreeFOAM is free software: you can redistribute it and/or modify it
19 # under the terms of the GNU General Public License as published by the
20 # Free Software Foundation, either version 3 of the License, or (at your
21 # option) any later version.
23 # FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
24 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 # for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with FreeFOAM. If not, see <http://www.gnu.org/licenses/>.
31 # Script
32 # freefoam-solverSweeps
34 # Description
35 # Extracts statistics from a log file
37 #------------------------------------------------------------------------------
39 """Usage: freefoam@PY_SCRIPT_SUFFIX@ solverSweeps [-h, -help] [<log>]
41 This utility script extracts solver statistics from a log file and prints them
42 to the screen. The extracted quantities are:
43 - execution time of the first iteration
44 - overall execution time
45 - the number of iterations
46 - the first iteration number
47 - the last iteration number
48 - the number of solver sweeps for the velocity and pressure fields
50 Options
51 -------
52 -h, -help Print this help message
53 <log> Analyze this log file. If this is -, read log from standard input.
54 If not specified, the name of the file (NOT the data) is read from
55 standard input.
57 """
59 import os
60 import os.path
61 import sys
62 import re
63 # want to be future proof
64 sys.path.insert(0, '@FOAM_PYTHON_DIR@')
65 from FreeFOAM.compat import *
67 logFile = None
68 logName = None
69 args = sys.argv[1:]
70 while len(args) > 0:
71 a = args[0]
72 if a == '-h' or a == '-help':
73 print(__doc__)
74 sys.exit(0)
75 elif a == '-':
76 logName = 'stdin'
77 logFile = sys.stdin
78 break
79 elif a[0] == '-':
80 sys.stderr.write('Error: unknown option "%s"\n'%a)
81 sys.stderr.write(__doc__+'\n')
82 sys.exit(1)
83 else:
84 logName = a
85 break
87 if not logFile:
88 if not logName:
89 sys.stderr.write('Name of log file [log] : ')
90 logName = sys.stdin.readline().strip()
91 if os.path.isfile(logName):
92 logFile = open(logName, 'rt')
93 else:
94 sys.stderr.write('Error: No such file "%s"\n'%logName)
95 sys.exit(1)
97 # Main
98 #~~~~~~
100 program = None
101 runTime = []
102 time = []
103 pIter = 0
104 UIter = 0
106 progRegex = re.compile(r'^Exec\s+: (?P<program>\S+)')
107 execTimeRegex = re.compile(r'^ExecutionTime\s*=.*')
108 timeRegex = re.compile(r'^Time\s*=.*')
109 pRegex = re.compile(r'.*Solving for p,.*\s+(?P<niter>\d+)$')
110 URegex = re.compile(r'.*Solving for U.,.*\s+(?P<niter>\d+)$')
111 for l in logFile:
112 l = l.strip()
113 if not program:
114 m = progRegex.match(l)
115 if m:
116 program = m.group('program')
117 continue
118 if execTimeRegex.match(l):
119 runTime.append(l)
120 continue
121 if timeRegex.match(l):
122 time.append(l)
123 continue
124 m = pRegex.match(l)
125 if m:
126 pIter += int(m.group('niter'))
127 continue
128 m = URegex.match(l)
129 if m:
130 UIter += int(m.group('niter'))
131 continue
133 print('\nProgram: %s'%program)
135 print('\nRuntime:')
136 print(' 1st iter : %s'%runTime[0])
137 print(' overall : %s'%runTime[-1])
139 print('\nSimulation:')
140 print(' steps: %d'%len(time))
141 print(' from : %s'%time[0])
142 print(' to : %s'%time[-1])
144 print('\nSolver sweeps:')
145 print(' p : %d'%pIter)
146 print(' U(U0,U1,U2) : %d'%UIter)
148 # ------------------- vim: set sw=3 sts=3 ft=python et: ------------ end-of-file