1
2 """
3 Application class that implements pyFoamSteadyRunner
4 """
5
6 from os import path
7
8 from PyFoamApplication import PyFoamApplication
9 from PyFoam.FoamInformation import changeFoamVersion
10
11 from PyFoam.Execution.ConvergenceRunner import ConvergenceRunner
12 from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
13 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
14
15 from PyFoam.Execution.ParallelExecution import LAMMachine
16 from PyFoam.Error import warning
17
20 description="""
21 Runs an OpenFoam steady solver. Needs the usual 3 arguments (<solver>
22 <directory> <case>) and passes them on (plus additional arguments)
23 Output is sent to stdout and a logfile inside the case directory
24 (PyFoamSolver.logfile). The Directory PyFoamSolver.analyzed contains
25 this information a) Residuals and other information of the linear
26 solvers b) Execution time c) continuity information d) bounding of
27 variables
28
29 If the solver has converged (linear solvers below threshold) it is
30 stopped and the last simulation state is written to disk
31 """
32
33 PyFoamApplication.__init__(self,args=args,description=description)
34
36 self.parser.add_option("--procnr",
37 type="int",
38 dest="procnr",
39 default=None,
40 help="The number of processors the run should be started on")
41 self.parser.add_option("--machinefile",
42 dest="machinefile",
43 default=None,
44 help="The machinefile that specifies the parallel machine")
45 self.parser.add_option("--clear-case",
46 action="store_true",
47 default=False,
48 dest="clearCase",
49 help="Clear all timesteps except for the first before running")
50 self.parser.add_option("--progress",
51 action="store_true",
52 default=False,
53 dest="progress",
54 help="Only prints the progress of the simulation, but swallows all the other output")
55 self.parser.add_option("--foamVersion",
56 dest="foamVersion",
57 default=None,
58 help="Change the OpenFOAM-version that is to be used")
59 self.parser.add_option("--safe-until",
60 type="float",
61 dest="safeUntil",
62 default=None,
63 help="Sets lower under-relaxation and lower-order convection-schemes for the start of the simulation")
64 self.parser.add_option("--safe-relaxation-factor",
65 type="float",
66 dest="safeRelaxation",
67 default=0.5,
68 help="The factor by which the relaxation-factors should be scaled down (when calculating safe)")
69 self.parser.add_option("--report-usage",
70 action="store_true",
71 default=False,
72 dest="reportUsage",
73 help="After the execution the maximum memory usage is printed to the screen")
74
76 if self.opts.foamVersion!=None:
77 changeFoamVersion(self.opts.foamVersion)
78
79 cName=self.parser.getArgs()[2]
80 sol=SolutionDirectory(cName,archive=None)
81
82 if self.opts.clearCase:
83 print "Clearing out old timesteps ...."
84 sol.clearResults()
85
86 lam=None
87 if self.opts.procnr!=None or self.opts.machinefile!=None:
88 lam=LAMMachine(machines=self.opts.machinefile,nr=self.opts.procnr)
89
90
91 run=ConvergenceRunner(BoundingLogAnalyzer(progress=self.opts.progress),silent=self.opts.progress,argv=self.parser.getArgs(),server=True,lam=lam)
92
93 if self.opts.safeUntil:
94 warning("Adding Trigger and resetting to safer start-settings")
95 trig=SafeTrigger(sol,self.opts.safeRelaxation)
96 run.addTrigger(self.opts.safeUntil,trig.resetIt)
97 run.addEndTrigger(trig.resetIt)
98
99 run.start()
100
101 if self.opts.reportUsage:
102 print "\n Used Memory: ",run.run.usedMemory(),"MB"
103
104 import re
105 from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile
106
109 self.solution=ParsedParameterFile(path.join(sol.systemDir(),"fvSolution"),backup=True)
110 self.schemes=ParsedParameterFile(path.join(sol.systemDir(),"fvSchemes"),backup=True)
111
112 self.fresh=True
113
114 try:
115 relax=self.solution["relaxationFactors"]
116 for var in relax:
117 relax[var]*=factor
118
119 cExp=re.compile("div\((.+),(.+)\)")
120 conv=self.schemes["divSchemes"]
121 for nm in conv:
122 if cExp.match(nm) or nm=="default":
123 conv[nm]="Gauss upwind"
124
125 self.solution.writeFile()
126 self.schemes.writeFile()
127 except Exception,e:
128 warning("Restoring defaults")
129 self.solution.restore()
130 self.schemes.restore()
131 raise e
132
134 if self.fresh:
135 warning("Trigger called: Resetting fvSchemes and fvSolution")
136 self.solution.restore()
137 self.schemes.restore()
138 self.fresh=False
139