1
2 """Run a OpenFOAM command"""
3
4 import sys
5 import string
6 from os import path
7 from threading import Timer
8
9 if not 'curdir' in dir(path) or not 'sep' in dir(path):
10 print "Warning: Inserting symbols into os.path (Python-Version<2.3)"
11 path.curdir='.'
12 path.sep ='/'
13
14 from FoamThread import FoamThread
15 from PyFoam.Infrastructure.FoamServer import FoamServer
16 from PyFoam.Infrastructure.Logging import foamLogger
17 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
18 from PyFoam.RunDictionary.ParameterFile import ParameterFile
19 from PyFoam.Error import warning,error
20
22 """Timed function to avoid time-stamp-problems"""
23 warning("Restoring the controlDict")
24 ctrl.restore()
25
27 """Base class for the running of commands
28
29 When the command is run the output is copied to a LogFile and
30 (optionally) standard-out
31
32 The argument list assumes for the first three elements the
33 OpenFOAM-convention:
34
35 <cmd> <dir> <case>
36
37 The directory name for outputs is therefor created from <dir> and
38 <case>
39
40 Provides some handle-methods that are to be overloaded for
41 additional functionality"""
42
43 - def __init__(self,argv=None,silent=False,logname="PyFoam",lam=None,server=False,restart=False):
44 """@param argv: list with the tokens that are the command line
45 if not set the standard command line is used
46 @param silent: if True no output is sent to stdout
47 @param logname: name of the logfile
48 @param lam: Information about a parallel run
49 @param server: Whether or not to start the network-server
50 @type lam: PyFoam.Execution.ParallelExecution.LAMMachine"""
51
52 if sys.version_info < (2,3):
53
54 if server:
55 warning("Can not start server-process because Python-Version is too old")
56 server=False
57
58 if argv==None:
59 self.argv=sys.argv[1:]
60 else:
61 self.argv=argv
62 self.dir=path.join(self.argv[1],self.argv[2])
63 if self.argv[2][-1]==path.sep:
64 self.argv[2]=self.argv[2][:-1]
65
66 try:
67 sol=self.getSolutionDirectory()
68 except OSError:
69 error("Solution directory",self.dir,"does not exist. No use running")
70
71 self.silent=silent
72 self.lam=lam
73 self.origArgv=self.argv
74
75 if self.lam!=None:
76 self.argv=lam.buildMPIrun(self.argv)
77 self.cmd=string.join(self.argv," ")
78 foamLogger().info("Starting: "+self.cmd+" in "+path.abspath(path.curdir))
79 self.logFile=path.join(self.dir,logname+".logfile")
80
81 self.fatalError=False
82 self.warnings=0
83 self.started=False
84
85 self.isRestarted=False
86 if restart:
87 self.controlDict=ParameterFile(path.join(self.dir,"system","controlDict"),backup=True)
88 self.controlDict.replaceParameter("startFrom","latestTime")
89 self.isRestarted=True
90
91 self.run=FoamThread(self.cmd,self)
92
93 self.server=None
94 if server:
95 self.server=FoamServer(run=self.run,master=self)
96 self.server.setDaemon(True)
97 self.server.start()
98 IP,PID,Port=self.server.info()
99 f=open(path.join(self.dir,"PyFoamServer.info"),"w")
100 print >>f,IP,PID,Port
101 f.close()
102
103 self.createTime=None
104 self.nowTime=None
105
106 self.stopMe=False
107 self.writeRequested=False
108
109 self.endTriggers=[]
110
183
185 """checks whether the run was successful"""
186 if self.started:
187 return not self.fatalError
188 else:
189 return False
190
192 """to be called before the program is started"""
193 pass
194
196 """Tells the runner to stop at the next convenient time"""
197 if not self.stopMe:
198 self.stopMe=True
199 if not self.isRestarted:
200 self.controlDict=ParameterFile(path.join(self.dir,"system","controlDict"),backup=True)
201 self.controlDict.replaceParameter("stopAt","writeNow")
202 warning("Stopping run at next write")
203
213
215 """called after the program has stopped"""
216 if self.stopMe or self.isRestarted:
217 self.controlDict.restore()
218
220 """called every time a new line is read"""
221 pass
222
224 """Get the name of the logfiles"""
225 return self.logFile
226
228 """@return: The directory of the case
229 @rtype: PyFoam.RunDictionary.SolutionDirectory
230 @param archive: Name of the directory for archiving results"""
231
232 return SolutionDirectory(self.dir,archive=archive)
233
235 """@param f: A function that is to be executed at the end of the simulation"""
236 self.endTriggers.append(f)
237
238 import re
239
241 """A small class that does primitve checking for BasicRunner
242 Duplicates other efforts, but ...."""
243
244 floatRegExp="[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?"
245
249
251 """Does this line contain time information?"""
252 m=self.timeExpr.match(line)
253 if m:
254 return float(m.group(1))
255 else:
256 return None
257
259 """Does this line contain mesh time information?"""
260 m=self.createExpr.match(line)
261 if m:
262 return float(m.group(1))
263 else:
264 return None
265
267 """Was the controlDict reread?"""
268 if line.find("Reading object controlDict from file"):
269 return True
270 else:
271 return False
272