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 |
def doStepPostprocessing(self, dt): |
64 |
""" |
65 |
renders the scene |
66 |
|
67 |
@note: to be overwritten |
68 |
""" |
69 |
if self.writeFrame(): |
70 |
self.trace("%s-th frame at time %s"%(self.getFrameCounter(),self.t)) |
71 |
if not self.scalar==None: |
72 |
self.trace("scalar data: (min,max) =(%s,%s)"%(inf(self.scalar),sup(self.scalar))) |
73 |
if not self.vector==None: |
74 |
self.trace("vector data: (min,max) =(%s,%s)"%(inf(self.vector),sup(self.vector))) |
75 |
if not self.tensor==None: |
76 |
self.trace("tensor data: (min,max) =(%s,%s)"%(inf(self.tensor),sup(self.tensor))) |
77 |
|
78 |
def doFinalization(self): |
79 |
""" |
80 |
Finalises the visualisation. For instance, makes a movie of the image files. |
81 |
|
82 |
@note: to be overwritten |
83 |
""" |
84 |
pass |
85 |
|
86 |
class WriteVTK(Visualization): |
87 |
""" |
88 |
Writes data into VTK files for further processing. |
89 |
""" |
90 |
|
91 |
def __init__(self, debug=False): |
92 |
""" |
93 |
Initialisation of the WriteVTK object |
94 |
|
95 |
@param debug: Debugging flag |
96 |
""" |
97 |
super(WriteVTK,self).__init__(debug=debug) |
98 |
|
99 |
def doInitialization(self): |
100 |
""" |
101 |
Does some kind of initialisation |
102 |
""" |
103 |
super(WriteVTK,self).doInitialization() |
104 |
fnc=self.filename.split('.') |
105 |
if len(fnc)==0: |
106 |
self.__filename="data.%s.xml" |
107 |
else: |
108 |
n=fnc[0] |
109 |
for i in range(1,len(fnc)-1): |
110 |
n+="."+fnc[i] |
111 |
if len(fnc)==1: |
112 |
self.__filename=n+".%s" |
113 |
else: |
114 |
self.__filename=n+".%s."+fnc[-1] |
115 |
self.trace("output filename is %s."%self.__filename) |
116 |
|
117 |
|
118 |
def doStepPostprocessing(self, dt): |
119 |
""" |
120 |
Do any necessary postprocessing operations after a timestep. |
121 |
|
122 |
@param dt: |
123 |
""" |
124 |
if self.writeFrame(): |
125 |
kwargs={} |
126 |
if not self.scalar==None: kwargs["scalar"] = self.scalar |
127 |
if not self.vector==None: kwargs["vector"] = self.vector |
128 |
if not self.tensor==None: kwargs["tensor"] = self.tensor |
129 |
saveVTK(self.__filename%self.getFrameCounter(),**kwargs) |
130 |
self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.__filename%self.getFrameCounter())) |
131 |
|
132 |
class ShadePlot(Visualization): |
133 |
""" |
134 |
Shaded contour plots |
135 |
""" |
136 |
|
137 |
def __init(self, debug=False): |
138 |
""" |
139 |
Initialisation |
140 |
""" |
141 |
Visualization.__init__(self, debug) |
142 |
self.declareParameter(filename="shadePlot.%s.png") |
143 |
|
144 |
def doStepPostprocessing(self, dt): |
145 |
""" |
146 |
Do any necessary postprocessing operations after a timestep. |
147 |
|
148 |
@param dt: |
149 |
""" |
150 |
self.counter += 1 |
151 |
if self.counter % self.stride == 0: |
152 |
n = self.counter/self.stride |
153 |
|
154 |
# look for a vtk xml file of the right name and plot it |
155 |
dataFname = "data.s.%d.xml" % n |
156 |
if not os.path.exists(dataFname): |
157 |
print "Data file doesn't exist! Skipping frame generation." |
158 |
|
159 |
else: |
160 |
import pyvisi |
161 |
import pyvisi.renderers.vtk |
162 |
|
163 |
scene = pyvisi.renderers.vtk.Scene() |
164 |
plot = pyvisi.renderers.vtk.ContourPlot(scene) |
165 |
plot.setData(fname=dataFname, |
166 |
format='vtk-xml', |
167 |
scalars='escript_scalar_data') |
168 |
scene.save(fname="shadePlot.%05d.png" % n, format="png") |
169 |
|
170 |
|
171 |
class ArrowPlot(Visualization): |
172 |
""" |
173 |
Arrow/vector/quiver plots |
174 |
""" |
175 |
|
176 |
def __init(self, debug=False): |
177 |
""" |
178 |
Initialisation |
179 |
""" |
180 |
Visualization.__init__(self, debug) |
181 |
self.declareParameter(filename="arrowPlot.%s.png") |
182 |
|
183 |
def doStepPostprocessing(self, dt): |
184 |
""" |
185 |
Do any necessary postprocessing operations after a timestep. |
186 |
|
187 |
@param dt: |
188 |
""" |
189 |
self.counter += 1 |
190 |
if self.counter % self.stride == 0: |
191 |
n = self.counter/self.stride |
192 |
|
193 |
# look for a vtk xml file of the right name and plot it |
194 |
dataFname = "data.v.%d.xml" % n |
195 |
if not os.path.exists(dataFname): |
196 |
print "Data file doesn't exist! Skipping frame generation." |
197 |
|
198 |
else: |
199 |
import pyvisi |
200 |
import pyvisi.renderers.vtk |
201 |
|
202 |
scene = pyvisi.renderers.vtk.Scene() |
203 |
plot = pyvisi.renderers.vtk.ArrowPlot3D(scene) |
204 |
plot.setData(fname=dataFname, |
205 |
format='vtk-xml') |
206 |
scene.save(fname="arrowPlot.%05d.png" % n, format="png") |
207 |
|
208 |
|
209 |
|
210 |
class EllipsoidPlot(Visualization): |
211 |
""" |
212 |
Ellipsoid plots |
213 |
""" |
214 |
|
215 |
def __init(self, debug=False): |
216 |
""" |
217 |
Initialisation |
218 |
""" |
219 |
Visualization.__init__(self, debug) |
220 |
|
221 |
|
222 |
# vim: expandtab shiftwidth=4: |