1 |
# $Id$ |
2 |
|
3 |
from esys.escript.modelframe import Model |
4 |
from esys.escript import saveVTK |
5 |
import os |
6 |
|
7 |
class Visualization(Model): |
8 |
""" |
9 |
Generic visualization of scalar, vector and tensorial data |
10 |
(not implemeted yet) |
11 |
|
12 |
@ivar t: current time |
13 |
@ivar n: frame counter |
14 |
@ivar scalar: scalar data set |
15 |
@ivar vector: vector data set |
16 |
@ivar tensor: tensor data set |
17 |
@ivar dt: increment for output |
18 |
@ivar filename: name of the output file |
19 |
""" |
20 |
|
21 |
def __init__(self, debug=False): |
22 |
""" |
23 |
Initialisation of the visualisation model object |
24 |
|
25 |
@param debug: Debugging flag |
26 |
""" |
27 |
super(Visualization,self).__init__(debug=debug) |
28 |
self.declareParameter(t=0., |
29 |
n=0, |
30 |
scalar=None, |
31 |
vector=None, |
32 |
tensor=None, |
33 |
dt=1, |
34 |
filename="movie.mpg") |
35 |
|
36 |
def doInitialization(self): |
37 |
""" |
38 |
Does some kind of initialisation |
39 |
""" |
40 |
self.__last_t=self.t |
41 |
|
42 |
def writeFrame(self): |
43 |
out=self.t>=self.__last_t+self.dt |
44 |
if out: |
45 |
self.__last_t+=self.dt |
46 |
self.n+=1 |
47 |
return out |
48 |
|
49 |
def getFrameCounter(self): |
50 |
return self.n-1 |
51 |
|
52 |
def getSafeTimeStepSize(self,dt): |
53 |
""" |
54 |
returns new step size |
55 |
|
56 |
@param dt: last time step size used |
57 |
@type dt: C{float} |
58 |
@return: time step size that can savely be used |
59 |
@rtype: C{float} |
60 |
""" |
61 |
return self.__last_t+self.dt-self.t |
62 |
|
63 |
|
64 |
def doStepPostprocessing(self, dt): |
65 |
""" |
66 |
renders the scene |
67 |
|
68 |
@note: to be overwritten |
69 |
""" |
70 |
if self.writeFrame(): |
71 |
self.trace("%s-th frame at time %s"%(self.getFrameCounter(),self.t)) |
72 |
if not self.scalar==None: |
73 |
self.trace("scalar data: (min,max) =(%s,%s)"%(inf(self.scalar),sup(self.scalar))) |
74 |
if not self.vector==None: |
75 |
self.trace("vector data: (min,max) =(%s,%s)"%(inf(self.vector),sup(self.vector))) |
76 |
if not self.tensor==None: |
77 |
self.trace("tensor data: (min,max) =(%s,%s)"%(inf(self.tensor),sup(self.tensor))) |
78 |
|
79 |
def doFinalization(self): |
80 |
""" |
81 |
Finalises the visualisation. For instance, makes a movie of the image files. |
82 |
|
83 |
@note: to be overwritten |
84 |
""" |
85 |
pass |
86 |
|
87 |
class WriteVTK(Visualization): |
88 |
""" |
89 |
Writes data into VTK files for further processing. |
90 |
""" |
91 |
|
92 |
def __init__(self, debug=False): |
93 |
""" |
94 |
Initialisation of the WriteVTK object |
95 |
|
96 |
@param debug: Debugging flag |
97 |
""" |
98 |
super(WriteVTK,self).__init__(debug=debug) |
99 |
|
100 |
def doInitialization(self): |
101 |
""" |
102 |
Does some kind of initialisation |
103 |
""" |
104 |
super(WriteVTK,self).doInitialization() |
105 |
fnc=self.filename.split('.') |
106 |
if len(fnc)==0: |
107 |
self.__filename="data.%s.xml" |
108 |
else: |
109 |
n=fnc[0] |
110 |
for i in range(1,len(fnc)-1): |
111 |
n+="."+fnc[i] |
112 |
if len(fnc)==1: |
113 |
self.__filename=n+".%s" |
114 |
else: |
115 |
self.__filename=n+".%s."+fnc[-1] |
116 |
self.trace("output filename is %s."%self.__filename) |
117 |
|
118 |
|
119 |
def doStepPostprocessing(self, dt): |
120 |
""" |
121 |
Do any necessary postprocessing operations after a timestep. |
122 |
|
123 |
@param dt: |
124 |
""" |
125 |
if self.writeFrame(): |
126 |
kwargs={} |
127 |
if not self.scalar==None: kwargs["scalar"] = self.scalar |
128 |
if not self.vector==None: kwargs["vector"] = self.vector |
129 |
if not self.tensor==None: kwargs["tensor"] = self.tensor |
130 |
saveVTK(self.__filename%self.getFrameCounter(),**kwargs) |
131 |
self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.__filename%self.getFrameCounter())) |
132 |
|
133 |
class ShadePlot(Visualization): |
134 |
""" |
135 |
Shaded contour plots |
136 |
""" |
137 |
|
138 |
def __init(self, debug=False): |
139 |
""" |
140 |
Initialisation |
141 |
""" |
142 |
Visualization.__init__(self, debug) |
143 |
self.declareParameter(filename="shadePlot.%s.png") |
144 |
|
145 |
def doStepPostprocessing(self, dt): |
146 |
""" |
147 |
Do any necessary postprocessing operations after a timestep. |
148 |
|
149 |
@param dt: |
150 |
""" |
151 |
self.counter += 1 |
152 |
if self.counter % self.stride == 0: |
153 |
n = self.counter/self.stride |
154 |
|
155 |
# look for a vtk xml file of the right name and plot it |
156 |
dataFname = "data.s.%d.xml" % n |
157 |
if not os.path.exists(dataFname): |
158 |
print "Data file doesn't exist! Skipping frame generation." |
159 |
|
160 |
else: |
161 |
import pyvisi |
162 |
import pyvisi.renderers.vtk |
163 |
|
164 |
scene = pyvisi.renderers.vtk.Scene() |
165 |
plot = pyvisi.renderers.vtk.ContourPlot(scene) |
166 |
plot.setData(fname=dataFname, |
167 |
format='vtk-xml', |
168 |
scalars='escript_scalar_data') |
169 |
scene.save(fname="shadePlot.%05d.png" % n, format="png") |
170 |
|
171 |
|
172 |
class ArrowPlot(Visualization): |
173 |
""" |
174 |
Arrow/vector/quiver plots |
175 |
""" |
176 |
|
177 |
def __init(self, debug=False): |
178 |
""" |
179 |
Initialisation |
180 |
""" |
181 |
Visualization.__init__(self, debug) |
182 |
self.declareParameter(filename="arrowPlot.%s.png") |
183 |
|
184 |
def doStepPostprocessing(self, dt): |
185 |
""" |
186 |
Do any necessary postprocessing operations after a timestep. |
187 |
|
188 |
@param dt: |
189 |
""" |
190 |
self.counter += 1 |
191 |
if self.counter % self.stride == 0: |
192 |
n = self.counter/self.stride |
193 |
|
194 |
# look for a vtk xml file of the right name and plot it |
195 |
dataFname = "data.v.%d.xml" % n |
196 |
if not os.path.exists(dataFname): |
197 |
print "Data file doesn't exist! Skipping frame generation." |
198 |
|
199 |
else: |
200 |
import pyvisi |
201 |
import pyvisi.renderers.vtk |
202 |
|
203 |
scene = pyvisi.renderers.vtk.Scene() |
204 |
plot = pyvisi.renderers.vtk.ArrowPlot3D(scene) |
205 |
plot.setData(fname=dataFname, |
206 |
format='vtk-xml') |
207 |
scene.save(fname="arrowPlot.%05d.png" % n, format="png") |
208 |
|
209 |
|
210 |
|
211 |
class EllipsoidPlot(Visualization): |
212 |
""" |
213 |
Ellipsoid plots |
214 |
""" |
215 |
|
216 |
def __init(self, debug=False): |
217 |
""" |
218 |
Initialisation |
219 |
""" |
220 |
Visualization.__init__(self, debug) |
221 |
|
222 |
|
223 |
# vim: expandtab shiftwidth=4: |