1
2 """
3 Application class that implements pyFoamRunner
4 """
5
6 from PyFoamApplication import PyFoamApplication
7
8 from PyFoam.Execution.AnalyzedRunner import AnalyzedRunner
9 from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
10 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
11 from PyFoam.RunDictionary.RegionCases import RegionCases
12
13 from PyFoam.Execution.ParallelExecution import LAMMachine
14
15 from PyFoam.Error import warning,error
16
17 from CommonPlotLines import CommonPlotLines
18 from CommonClearCase import CommonClearCase
19 from CommonWriteAllTrigger import CommonWriteAllTrigger
20 from CommonLibFunctionTrigger import CommonLibFunctionTrigger
21
22 from os import path
23
24 -class Runner(PyFoamApplication,
25 CommonPlotLines,
26 CommonWriteAllTrigger,
27 CommonLibFunctionTrigger,
28 CommonClearCase):
30 description="""
31 Runs an OpenFoam solver. Needs the usual 3 arguments (<solver>
32 <directory> <case>) and passes them on (plus additional arguments).
33 Output is sent to stdout and a logfile inside the case directory
34 (PyFoamSolver.logfile) The Directory PyFoamSolver.analyzed contains
35 this information: a) Residuals and other information of the linear
36 solvers b Execution time c) continuity information d) bounding of
37 variables
38 """
39
40 CommonPlotLines.__init__(self)
41 PyFoamApplication.__init__(self,
42 exactNr=False,
43 args=args,
44 description=description)
45
47 self.parser.add_option("--procnr",
48 type="int",
49 dest="procnr",
50 default=None,
51 help="The number of processors the run should be started on")
52 self.parser.add_option("--machinefile",
53 dest="machinefile",
54 default=None,
55 help="The machinefile that specifies the parallel machine")
56 self.parser.add_option("--restart",
57 action="store_true",
58 default=False,
59 dest="restart",
60 help="Restart the simulation from the last time-step")
61
62 self.parser.add_option("--progress",
63 action="store_true",
64 default=False,
65 dest="progress",
66 help="Only prints the progress of the simulation, but swallows all the other output")
67 self.parser.add_option("--logname",
68 dest="logname",
69 default=None,
70 help="Name of the logfile")
71
72 self.parser.add_option("--all-regions",
73 action="store_true",
74 default=False,
75 dest="regions",
76 help="Executes the command for all available regions (builds a pseudo-case for each region)")
77
78 self.parser.add_option("--region",
79 dest="region",
80 default=None,
81 help="Executes the command for a region (builds a pseudo-case for that region)")
82
83 self.parser.add_option("--keep-pseudocases",
84 action="store_true",
85 default=False,
86 dest="keeppseudo",
87 help="Keep the pseudo-cases that were built for a multi-region case")
88 self.parser.add_option("--report-usage",
89 action="store_true",
90 default=False,
91 dest="reportUsage",
92 help="After the execution the maximum memory usage is printed to the screen")
93
94 CommonPlotLines.addOptions(self)
95 CommonClearCase.addOptions(self)
96 CommonWriteAllTrigger.addOptions(self)
97 CommonLibFunctionTrigger.addOptions(self)
98
100 if self.opts.keeppseudo and (not self.opts.regions and self.opts.region==None):
101 warning("Option --keep-pseudocases only makes sense for multi-region-cases")
102 regionNames=[self.opts.region]
103 regions=None
104
105 casePath=self.parser.casePath()
106
107 self.checkCase(casePath)
108
109 if self.opts.regions or self.opts.region!=None:
110 print "Building Pseudocases"
111 sol=SolutionDirectory(casePath,archive=None)
112 regions=RegionCases(sol,clean=True)
113
114 if self.opts.regions:
115 regionNames=sol.getRegions()
116
117 self.processPlotLineOptions(autoPath=casePath)
118
119 self.clearCase(SolutionDirectory(casePath,archive=None))
120
121 lam=None
122 if self.opts.procnr!=None or self.opts.machinefile!=None:
123 lam=LAMMachine(machines=self.opts.machinefile,nr=self.opts.procnr)
124
125 for theRegion in regionNames:
126 args=self.parser.getArgs()[:]
127 if theRegion!=None:
128 args[2]+="."+theRegion
129
130 if self.opts.logname==None:
131 self.opts.logname="PyFoamSolve."+path.basename(args[0])
132
133 run=AnalyzedRunner(BoundingLogAnalyzer(progress=self.opts.progress),silent=self.opts.progress,argv=args,server=True,lam=lam,restart=self.opts.restart,logname=self.opts.logname)
134
135 self.addPlotLineAnalyzers(run)
136
137 self.addWriteAllTrigger(run,SolutionDirectory(casePath,archive=None))
138 self.addLibFunctionTrigger(run,SolutionDirectory(casePath,archive=None))
139
140 run.start()
141
142 if self.opts.reportUsage:
143 print "\n Used Memory: ",run.run.usedMemory(),"MB"
144
145 if theRegion!=None:
146 print "Syncing into master case"
147 regions.resync(theRegion)
148
149
150 if regions!=None:
151 if not self.opts.keeppseudo:
152 print "Removing pseudo-regions"
153 regions.cleanAll()
154 else:
155 for r in sol.getRegions():
156 if r not in regionNames:
157 regions.clean(r)
158