1
2 """Run a OpenFOAM command"""
3
4 import sys
5 import string
6 from os import path
7
8 if not 'curdir' in dir(path) or not 'sep' in dir(path):
9 print "Warning: Inserting symbols into os.path (Python-Version<2.3)"
10 path.curdir='.'
11 path.sep ='/'
12
13 from FoamThread import FoamThread
14 from PyFoam.Infrastructure.FoamServer import FoamServer
15 from PyFoam.Infrastructure.Logging import foamLogger
16 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
17 from PyFoam.RunDictionary.ParameterFile import ParameterFile
18
20 """Base class for the running of commands
21
22 When the command is run the output is copied to a LogFile and
23 (optionally) standard-out
24
25 The argument list assumes for the first three elements the
26 OpenFOAM-convention:
27
28 <cmd> <dir> <case>
29
30 The directory name for outputs is therefor created from <dir> and
31 <case>
32
33 Provides some handle-methods that are to be overloaded for
34 additional functionality"""
35
36 - def __init__(self,argv=None,silent=False,logname="PyFoam",lam=None,server=False):
37 """@param argv: list with the tokens that are the command line
38 if not set the standard command line is used
39 @param silent: if True no output is sent to stdout
40 @param logname: name of the logfile
41 @param lam: Information about a parallel run
42 @param server: Whether or not to start the network-server
43 @type lam: PyFoam.Execution.ParallelExecution.LAMMachine"""
44
45 if sys.version_info < (2,3):
46
47 server=False
48
49 if argv==None:
50 self.argv=sys.argv[1:]
51 else:
52 self.argv=argv
53 self.dir=path.join(self.argv[1],self.argv[2])
54 if self.argv[2][-1]==path.sep:
55 self.argv[2]=self.argv[2][:-1]
56
57 self.silent=silent
58 self.lam=lam
59 self.origArgv=self.argv
60
61 if self.lam!=None:
62 self.argv=lam.buildMPIrun(self.argv)
63 self.cmd=string.join(self.argv," ")
64 foamLogger().info("Starting: "+self.cmd+" in "+path.abspath(path.curdir))
65 self.logFile=path.join(self.dir,logname+".logfile")
66
67 self.fatalError=False
68 self.warnings=0
69 self.started=False
70
71 self.run=FoamThread(self.cmd)
72
73 self.server=None
74 if server:
75 self.server=FoamServer(run=self.run,master=self)
76 self.server.setDaemon(True)
77 self.server.start()
78
79 self.createTime=None
80 self.nowTime=None
81
82 self.stopMe=False
83
146
148 """checks whether the run was successful"""
149 if self.started:
150 return not self.fatalError
151 else:
152 return False
153
155 """to be called before the program is started"""
156 pass
157
167
169 """called after the program has stopped"""
170 if self.stopMe:
171 self.controlDict.restore()
172
174 """called every time a new line is read"""
175 pass
176
178 """Get the name of the logfiles"""
179 return self.logFile
180
182 """@return: The directory of the case
183 @rtype: PyFoam.RunDictionary.SolutionDirectory
184 @param archive: Name of the directory for archiving results"""
185
186 return SolutionDirectory(self.dir,archive=archive)
187
188 import re
189
191 """A small class that does primitve checking for BasicRunner
192 Duplicates other efforts, but ...."""
193
194 floatRegExp="[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?"
195
199
201 """Does this line contain time information?"""
202 m=self.timeExpr.match(line)
203 if m:
204 return float(m.group(1))
205 else:
206 return None
207
209 """Does this line contain mesh time information?"""
210 m=self.createExpr.match(line)
211 if m:
212 return float(m.group(1))
213 else:
214 return None
215