1
2 """
3 Application class that implements pyFoamRunner
4 """
5
6 from PyFoamApplication import PyFoamApplication
7
8 from PyFoam.FoamInformation import changeFoamVersion
9
10 from PyFoam.Execution.AnalyzedRunner import AnalyzedRunner
11 from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
12 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
13 from PyFoam.RunDictionary.RegionCases import RegionCases
14
15 from PyFoam.Execution.ParallelExecution import LAMMachine
16
17 from PyFoam.Error import warning,error
18
19 from CommonPlotLines import CommonPlotLines
20 from CommonClearCase import CommonClearCase
21 from CommonWriteAllTrigger import CommonWriteAllTrigger
22
23 from os import path
24
25 -class Runner(PyFoamApplication,
26 CommonPlotLines,
27 CommonWriteAllTrigger,
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("--foamVersion",
68 dest="foamVersion",
69 default=None,
70 help="Change the OpenFOAM-version that is to be used")
71 self.parser.add_option("--logname",
72 dest="logname",
73 default=None,
74 help="Name of the logfile")
75
76 self.parser.add_option("--all-regions",
77 action="store_true",
78 default=False,
79 dest="regions",
80 help="Executes the command for all available regions (builds a pseudo-case for each region)")
81
82 self.parser.add_option("--region",
83 dest="region",
84 default=None,
85 help="Executes the command for a region (builds a pseudo-case for that region)")
86
87 self.parser.add_option("--keep-pseudocases",
88 action="store_true",
89 default=False,
90 dest="keeppseudo",
91 help="Keep the pseudo-cases that were built for a multi-region case")
92 self.parser.add_option("--report-usage",
93 action="store_true",
94 default=False,
95 dest="reportUsage",
96 help="After the execution the maximum memory usage is printed to the screen")
97
98 CommonPlotLines.addOptions(self)
99 CommonClearCase.addOptions(self)
100 CommonWriteAllTrigger.addOptions(self)
101
103 if self.opts.keeppseudo and (not self.opts.regions and self.opts.region==None):
104 warning("Option --keep-pseudocases only makes sense for multi-region-cases")
105 regionNames=[self.opts.region]
106 regions=None
107
108 if self.opts.regions or self.opts.region!=None:
109 print "Building Pseudocases"
110 sol=SolutionDirectory(self.parser.getArgs()[2],archive=None)
111 regions=RegionCases(sol,clean=True)
112
113 if self.opts.regions:
114 regionNames=sol.getRegions()
115
116 if self.opts.foamVersion!=None:
117 changeFoamVersion(self.opts.foamVersion)
118
119 self.processPlotLineOptions(autoPath=path.join(self.parser.getArgs()[1],self.parser.getArgs()[2]))
120
121 self.clearCase(SolutionDirectory(self.parser.getArgs()[2],archive=None))
122
123 lam=None
124 if self.opts.procnr!=None or self.opts.machinefile!=None:
125 lam=LAMMachine(machines=self.opts.machinefile,nr=self.opts.procnr)
126
127 for theRegion in regionNames:
128 args=self.parser.getArgs()[:]
129 if theRegion!=None:
130 args[2]+="."+theRegion
131
132 if self.opts.logname==None:
133 self.opts.logname="PyFoamSolve."+args[0]
134
135 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)
136
137 self.addPlotLineAnalyzers(run)
138
139 self.addWriteAllTrigger(run,SolutionDirectory(self.parser.getArgs()[2],archive=None))
140
141 run.start()
142
143 if self.opts.reportUsage:
144 print "\n Used Memory: ",run.run.usedMemory(),"MB"
145
146 if theRegion!=None:
147 print "Syncing into master case"
148 regions.resync(theRegion)
149
150
151 if regions!=None:
152 if not self.opts.keeppseudo:
153 print "Removing pseudo-regions"
154 regions.cleanAll()
155 else:
156 for r in sol.getRegions():
157 if r not in regionNames:
158 regions.clean(r)
159