1
2 """
3 Class that implements pyFoamPVSnapshot
4 """
5
6 from optparse import OptionGroup
7
8 from PyFoamApplication import PyFoamApplication
9
10 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
11 from PyFoam.Paraview.ServermanagerWrapper import ServermanagerWrapper as SM
12 from PyFoam.Paraview.StateFile import StateFile
13
14 from os import path,unlink
15 import sys,string
16
19 description="""
20 Generates snapshots of an OpenFOAM-case and a predefined paraview-State-File
21 using the PV3FoamReader that comes with OpenFOAM.
22
23 The state-file can be generated using a different case (the script adjusts
24 it before using) but the original case has to have a similar structure to the
25 current one. Also exactly one PV3Reader has to be used in the state-file (this
26 requirement is fullfilled if the StateFile was generated using paraFoam)
27
28 In TextSources the string "%(casename)s" gets replaced by the casename. Additional
29 replacements can be specified
30 """
31 PyFoamApplication.__init__(self,
32 args=args,
33 description=description,
34 usage="%prog [options] <case>",
35 interspersed=True,
36 nr=1)
37
38 typeTable={"png":"vtkPNGWriter",
39 "jpg":"vtkJPEGWriter"}
40
42 time=OptionGroup(self.parser,
43 "Time Specification",
44 "Which times should be plotted")
45 time.add_option("--time",
46 type="float",
47 dest="time",
48 default=[],
49 action="append",
50 help="Timestep that should be vizualized. Can be used more than once")
51 time.add_option("--latest-time",
52 dest="latest",
53 action="store_true",
54 default=False,
55 help="Make a picture of the latest time")
56 time.add_option("--all-times",
57 dest="all",
58 action="store_true",
59 default=False,
60 help="Make a picture of all times")
61 time.add_option("--unique-times",
62 dest="unique",
63 action="store_true",
64 default=False,
65 help="Use each time-directory only once")
66
67 self.parser.add_option_group(time)
68
69 paraview=OptionGroup(self.parser,
70 "Paraview specifications",
71 "Options concerning paraview")
72 paraview.add_option("--state-file",
73 dest="state",
74 default=None,
75 help="The pvsm-file that should be used. If none is specified the file 'default.pvsm' in the case-directory is used")
76 paraview.add_option("--maginfication",
77 dest="magnification",
78 default=1,
79 type="int",
80 help="Magnification factor of the picture (integer). Default: %default")
81 paraview.add_option("--type",
82 dest="type",
83 type="choice",
84 choices=self.typeTable.keys(),
85 default="png",
86 help="The type of the bitmap-file. Possibilities are "+string.join(self.typeTable.keys(),", ")+". Default: %default")
87 paraview.add_option("--no-progress",
88 dest="progress",
89 action="store_false",
90 default=True,
91 help="Paraview will not print the progress of the filters")
92 self.parser.add_option_group(paraview)
93
94 filename=OptionGroup(self.parser,
95 "Filename specifications",
96 "The names of the resulting files")
97 filename.add_option("--file-prefix",
98 dest="prefix",
99 default="Snapshot",
100 help="Start of the filename for the bitmap-files")
101 filename.add_option("--no-casename",
102 dest="casename",
103 action="store_false",
104 default=True,
105 help="Do not add the casename to the filename")
106 filename.add_option("--no-timename",
107 dest="timename",
108 action="store_false",
109 default=True,
110 help="Do not append the string 't=<time>' to the filename")
111 self.parser.add_option_group(filename)
112
113 replace=OptionGroup(self.parser,
114 "Replacements etc",
115 "Manipuations of the statefile")
116 replace.add_option("--replacements",
117 dest="replacements",
118 default="{}",
119 help="Dictionary with replacement strings. Default: %default")
120 replace.add_option("--casename-key",
121 dest="casenameKey",
122 default="casename",
123 help="Key with which the caename should be replaced. Default: %default")
124
126 case=path.abspath(self.parser.getArgs()[0])
127 short=path.basename(case)
128
129 if self.opts.state==None:
130 self.opts.state=path.join(case,"default.pvsm")
131
132 if not path.exists(self.opts.state):
133 self.error("The state file",self.opts.state,"does not exist")
134
135 timeString=""
136
137 if self.opts.casename:
138 timeString+="_"+short
139 timeString+="_%(nr)05d"
140 if self.opts.timename:
141 timeString+="_t=%(t)s"
142 timeString+="."+self.opts.type
143
144 sol=SolutionDirectory(case,paraviewLink=False,archive=None)
145 if self.opts.latest:
146 self.opts.time.append(float(sol.getLast()))
147 if self.opts.all:
148 for t in sol.getTimes():
149 self.opts.time.append(float(t))
150
151 self.opts.time.sort()
152
153 times=[]
154
155 for s in self.opts.time:
156 times.append(sol.timeName(s,minTime=True))
157
158 if self.opts.unique:
159 tmp=[]
160 last=None
161 cnt=0
162 for s in times:
163 if last!=s:
164 tmp.append(s)
165 else:
166 cnt+=1
167 last=s
168 if cnt>0:
169 self.warning("Removed",cnt,"duplicate times")
170 times=tmp
171
172 if len(times)==0:
173 self.warning("No valid times specified")
174 return
175
176 dataFile=path.join(case,short+".OpenFOAM")
177 createdDataFile=False
178 if not path.exists(dataFile):
179 createdDataFile=True
180 f=open(dataFile,"w")
181 f.close()
182
183 sf=StateFile(self.opts.state)
184 sf.setCase(dataFile)
185
186 values=eval(self.opts.replacements)
187 values[self.opts.casenameKey]=short
188 sf.rewriteTexts(values)
189 newState=sf.writeTemp()
190
191 sm=SM()
192 if not self.opts.progress:
193 sm.ToggleProgressPrinting()
194
195
196 sm.LoadState(newState)
197 views=sm.GetRenderViews()
198
199 if len(views)>1:
200 self.warning("More than 1 view in state-file. Generating multiple series")
201 timeString="_View%(view)02d"+timeString
202 timeString=self.opts.prefix+timeString
203
204 for view in views:
205 view.UseOffscreenRenderingForScreenshots=True
206
207 for i,t in enumerate(times):
208 print "Snapshot ",i," for t=",t
209 for j,view in enumerate(views):
210 view.ViewTime=float(t)
211 fn = timeString % {'nr':i,'t':t,'view':j}
212 view.WriteImage(fn,self.typeTable[self.opts.type],self.opts.magnification)
213
214 if createdDataFile:
215 self.warning("Removing pseudo-data-file",dataFile)
216 unlink(dataFile)
217