/[escript]/trunk/modellib/py_src/visualization.py
ViewVC logotype

Contents of /trunk/modellib/py_src/visualization.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (10 years, 2 months ago) by jfenwick
File MIME type: text/x-python
File size: 19673 byte(s)
Don't panic.
Updating copyright stamps

1
2 ########################################################
3 #
4 # Copyright (c) 2003-2010 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2003-2010 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __url__="https://launchpad.net/escript-finley"
21
22 from esys.escript.modelframe import Model, Link
23 from esys.escript import saveVTK, Data
24 import os
25
26 class Visualization(Model):
27 """
28 Generic visualization Model
29
30 :ivar t: current time (in)
31 :ivar n: frame counter (in)
32 :ivar dt: increment for output (in)
33 :ivar filename: name of the output file (in)
34 """
35
36 def __init__(self, **kwargs):
37 """
38 Initialisation of the visualisation model object
39
40 :keyword debug: Debugging flag
41 """
42 super(Visualization,self).__init__(**kwargs)
43 self.declareParameter(t=0.,
44 n=0,
45 dt=1,
46 filename="movie.mpg")
47
48 def doInitialization(self):
49 """
50 does some kind of initialisation
51 """
52 self.__last_t=self.t
53 fnc=self.filename.split('.')
54 if len(fnc)==0:
55 self.__frame_filename="data.%s.xml"
56 else:
57 n=fnc[0]
58 for i in range(1,len(fnc)-1):
59 n+="."+fnc[i]
60 if len(fnc)==1:
61 self.__frame_filename=n+".%s"
62 else:
63 self.__frame_filename=n+".%s."+fnc[-1]
64 self.trace("output filename is %s."%self.__frame_filename)
65
66 def getFrameFileName(self):
67 return self.__frame_filename%self.getFrameCounter()
68
69 def writeFrame(self):
70 """
71 returns True if the time stamp for writing frame is reached.
72 """
73 out=self.t>=self.__last_t+self.dt
74 if out:
75 self.__last_t+=self.dt
76 self.n+=1
77 return out
78
79 def getFrameCounter(self):
80 """
81 returns a frame counter
82 """
83 return self.n
84
85 def getSafeTimeStepSize(self,dt):
86 """
87 returns new step size
88
89 :param dt: last time step size used
90 :type dt: ``float``
91 :return: time step size that can savely be used
92 :rtype: ``float``
93 """
94 return self.__last_t+self.dt-self.t
95
96 class Movie(Visualization):
97 """
98 Generic visualization of scalar, vector and/or tensorial data
99
100 :ivar scalar: scalar data set (in)
101 :ivar vector: vector data set (in)
102 :ivar tensor: tensor data set (in)
103 """
104
105 def __init__(self, **kwargs):
106 """
107 Initialisation of the visualisation model object
108
109 :keyword debug: Debugging flag
110 """
111 super(Movie,self).__init__(**kwargs)
112 self.declareParameter(scalar=None,
113 vector=None,
114 tensor=None)
115
116 def doStepPostprocessing(self, dt):
117 """
118 renders the scene
119 """
120 super(Movie, self).doStepPostprocessing(dt)
121 if self.writeFrame():
122 if self.debug():
123 self.trace("%s-th frame at time %s"%(self.getFrameCounter(),self.t))
124 if isinstance(self.scalar,Data):
125 self.trace("scalar data: (min,max) =(%s,%s)"%(inf(self.scalar),sup(self.scalar)))
126 if isinstance(self.vector,Data):
127 self.trace("vector data: (min,max) =(%s,%s)"%(inf(self.vector),sup(self.vector)))
128 if isinstance(self.tensor,Data):
129 self.trace("tensor data: (min,max) =(%s,%s)"%(inf(self.tensor),sup(self.tensor)))
130
131 class ShadePlot(Movie):
132 """
133 Shaded contour plots
134 """
135
136 def __init(self, **kwargs):
137 """
138 Initialisation
139 """
140 Movie.__init__(self, **kwargs)
141 self.declareParameter(filename="shadePlot.%s.png")
142
143 def doStepPostprocessing(self, dt):
144 """
145 Do any necessary postprocessing operations after a timestep.
146
147 :param dt:
148 """
149 self.counter += 1
150 if self.counter % self.stride == 0:
151 n = self.counter/self.stride
152
153 # look for a vtk xml file of the right name and plot it
154 dataFname = "data.s.%d.xml" % n
155 if not os.path.exists(dataFname):
156 print "Data file doesn't exist! Skipping frame generation."
157
158 else:
159 import pyvisi
160 import pyvisi.renderers.vtk
161
162 scene = pyvisi.renderers.vtk.Scene()
163 plot = pyvisi.renderers.vtk.ContourPlot(scene)
164 plot.setData(fname=dataFname,
165 format='vtk-xml',
166 scalars='escript_scalar_data')
167 scene.save(fname="shadePlot.%05d.png" % n, format="png")
168
169
170 class ArrowPlot(Movie):
171 """
172 Arrow/vector/quiver plots
173 """
174
175 def __init(self, **kwargs):
176 """
177 Initialisation
178 """
179 Movie.__init__(self, **kwargs)
180 self.declareParameter(filename="arrowPlot.%s.png")
181
182 def doStepPostprocessing(self, dt):
183 """
184 Do any necessary postprocessing operations after a timestep.
185
186 :param dt:
187 """
188 self.counter += 1
189 if self.counter % self.stride == 0:
190 n = self.counter/self.stride
191
192 # look for a vtk xml file of the right name and plot it
193 dataFname = "data.v.%d.xml" % n
194 if not os.path.exists(dataFname):
195 print "Data file doesn't exist! Skipping frame generation."
196
197 else:
198 import pyvisi
199 import pyvisi.renderers.vtk
200
201 scene = pyvisi.renderers.vtk.Scene()
202 plot = pyvisi.renderers.vtk.ArrowPlot3D(scene)
203 plot.setData(fname=dataFname,
204 format='vtk-xml')
205 scene.save(fname="arrowPlot.%05d.png" % n, format="png")
206
207
208
209 class EllipsoidPlot(Movie):
210 """
211 Ellipsoid plots
212 """
213
214 def __init(self, **kwargs):
215 """
216 Initialisation
217 """
218 Movie.__init__(self, **kwargs)
219
220
221 class WriteVTK(Visualization):
222 """
223 Writes data into a VTK file.
224
225 The Model can handel up to 20 data sets that are written into a single file tagged with the given name. If no name is given and
226 the data are defined by a `Link` the name of the target attribute is used as a tag.
227
228 :ivar data0: data set 0 to be written
229 :type data0: `escript.Data`
230 :ivar name0: name tag for data set 0
231 :type name0: ``str`` or ``None``
232 :ivar data1: data set 1 to be written
233 :type data1: `escript.Data`
234 :ivar name1: name tag for data set 1
235 :type name1: ``str`` or ``None``
236 :ivar data2: data set 2 to be written
237 :type data2: `escript.Data`
238 :ivar name2: name tag for data set 2
239 :type name2: ``str`` or ``None``
240 :ivar data3: data set 3 to be written
241 :type data3: `escript.Data`
242 :ivar name3: name tag for data set 3
243 :type name3: ``str`` or ``None``
244 :ivar data4: data set 4 to be written
245 :type data4: `escript.Data`
246 :ivar name4: name tag for data set 4
247 :type name4: ``str`` or ``None``
248 :ivar data5: data set 5 to be written
249 :type data5: `escript.Data`
250 :ivar name5: name tag for data set 5
251 :type name5: ``str`` or ``None``
252 :ivar data6: data set 6 to be written
253 :type data6: `escript.Data`
254 :ivar name6: name tag for data set 6
255 :type name6: ``str`` or ``None``
256 :ivar data7: data set 7 to be written
257 :type data7: `escript.Data`
258 :ivar name7: name tag for data set 7
259 :type name7: ``str`` or ``None``
260 :ivar data8: data set 8 to be written
261 :type data8: `escript.Data`
262 :ivar name8: name tag for data set 8
263 :type name8: ``str`` or ``None``
264 :ivar data9: data set 9 to be written
265 :type data9: `escript.Data`
266 :ivar name9: name tag for data set 9
267 :type name9: ``str`` or ``None``
268 :ivar data10: data set 10 to be written
269 :type data10: `escript.Data`
270 :ivar name10: name tag for data set 10
271 :type name10: ``str`` or ``None``
272 :ivar data11: data set 11 to be written
273 :type data11: `escript.Data`
274 :ivar name11: name tag for data set 11
275 :type name11: ``str`` or ``None``
276 :ivar data12: data set 12 to be written
277 :type data12: `escript.Data`
278 :ivar name12: name tag for data set 12
279 :type name12: ``str`` or ``None``
280 :ivar data13: data set 13 to be written
281 :type data13: `escript.Data`
282 :ivar name13: name tag for data set 13
283 :type name13: ``str`` or ``None``
284 :ivar data14: data set 14 to be written
285 :type data14: `escript.Data`
286 :ivar name14: name tag for data set 14
287 :type name14: ``str`` or ``None``
288 :ivar data15: data set 15 to be written
289 :type data15: `escript.Data`
290 :ivar name15: name tag for data set 15
291 :type name15: ``str`` or ``None``
292 :ivar data16: data set 16 to be written
293 :type data16: `escript.Data`
294 :ivar name16: name tag for data set 16
295 :type name16: ``str`` or ``None``
296 :ivar data17: data set 17 to be written
297 :type data17: `escript.Data`
298 :ivar name17: name tag for data set 17
299 :type name17: ``str`` or ``None``
300 :ivar data18: data set 18 to be written
301 :type data18: `escript.Data`
302 :ivar name18: name tag for data set 18
303 :type name18: ``str`` or ``None``
304 :ivar data19: data set 19 to be written
305 :type data19: `escript.Data`
306 :ivar name19: name tag for data set 19
307 :type name19: ``str`` or ``None``
308 """
309 def __init__(self, **kwargs):
310 """
311 Initialisation of the WriteVTK object
312
313 :keyword debug: debugging flag
314 :type debug: ``bool``
315 """
316 super(WriteVTK,self).__init__(**kwargs)
317 self.declareParameter(data0=None,name0=None,
318 data1=None,name1=None,
319 data2=None,name2=None,
320 data3=None,name3=None,
321 data4=None,name4=None,
322 data5=None,name5=None,
323 data6=None,name6=None,
324 data7=None,name7=None,
325 data8=None,name8=None,
326 data9=None,name9=None,
327 data10=None,name10=None,
328 data11=None,name11=None,
329 data12=None,name12=None,
330 data13=None,name13=None,
331 data14=None,name14=None,
332 data15=None,name15=None,
333 data16=None,name16=None,
334 data17=None,name17=None,
335 data18=None,name18=None,
336 data19=None,name19=None)
337 def collectData(self):
338 kwargs={}
339 if isinstance(self.data0, Data):
340 if self.name0 == None:
341 if isinstance(self.getAttributeObject("data0"),Link):
342 kwargs[self.getAttributeObject("data0").getAttributeName()]=self.data0
343 else:
344 kwargs["data0"]=self.data0
345 else:
346 kwargs[str(self.name0)]=self.data0
347
348
349 if isinstance(self.data1, Data):
350 if self.name1 == None:
351 if isinstance(self.getAttributeObject("data1"),Link):
352 kwargs[self.getAttributeObject("data1").getAttributeName()]=self.data1
353 else:
354 kwargs["data1"]=self.data1
355 else:
356 kwargs[str(self.name1)]=self.data1
357
358
359 if isinstance(self.data2, Data):
360 if self.name2 == None:
361 if isinstance(self.getAttributeObject("data2"),Link):
362 kwargs[self.getAttributeObject("data2").getAttributeName()]=self.data2
363 else:
364 kwargs["data2"]=self.data2
365 else:
366 kwargs[str(self.name2)]=self.data2
367
368
369 if isinstance(self.data3, Data):
370 if self.name3 == None:
371 if isinstance(self.getAttributeObject("data3"),Link):
372 kwargs[self.getAttributeObject("data3").getAttributeName()]=self.data3
373 else:
374 kwargs["data3"]=self.data3
375 else:
376 kwargs[str(self.name3)]=self.data3
377
378
379 if isinstance(self.data4, Data):
380 if self.name4 == None:
381 if isinstance(self.getAttributeObject("data4"),Link):
382 kwargs[self.getAttributeObject("data4").getAttributeName()]=self.data4
383 else:
384 kwargs["data4"]=self.data4
385 else:
386 kwargs[str(self.name4)]=self.data4
387
388
389 if isinstance(self.data5, Data):
390 if self.name5 == None:
391 if isinstance(self.getAttributeObject("data5"),Link):
392 kwargs[self.getAttributeObject("data5").getAttributeName()]=self.data5
393 else:
394 kwargs["data5"]=self.data5
395 else:
396 kwargs[str(self.name5)]=self.data5
397
398
399 if isinstance(self.data6, Data):
400 if self.name6 == None:
401 if isinstance(self.getAttributeObject("data6"),Link):
402 kwargs[self.getAttributeObject("data6").getAttributeName()]=self.data6
403 else:
404 kwargs["data6"]=self.data6
405 else:
406 kwargs[str(self.name6)]=self.data6
407
408
409 if isinstance(self.data7, Data):
410 if self.name7 == None:
411 if isinstance(self.getAttributeObject("data7"),Link):
412 kwargs[self.getAttributeObject("data7").getAttributeName()]=self.data7
413 else:
414 kwargs["data7"]=self.data7
415 else:
416 kwargs[str(self.name7)]=self.data7
417
418
419 if isinstance(self.data8, Data):
420 if self.name8 == None:
421 if isinstance(self.getAttributeObject("data8"),Link):
422 kwargs[self.getAttributeObject("data8").getAttributeName()]=self.data8
423 else:
424 kwargs["data8"]=self.data8
425 else:
426 kwargs[str(self.name8)]=self.data8
427
428
429 if isinstance(self.data9, Data):
430 if self.name9 == None:
431 if isinstance(self.getAttributeObject("data9"),Link):
432 kwargs[self.getAttributeObject("data9").getAttributeName()]=self.data9
433 else:
434 kwargs["data9"]=self.data9
435 else:
436 kwargs[str(self.name9)]=self.data9
437
438
439 if isinstance(self.data10, Data):
440 if self.name10 == None:
441 if isinstance(self.getAttributeObject("data10"),Link):
442 kwargs[self.getAttributeObject("data10").getAttributeName()]=self.data10
443 else:
444 kwargs["data10"]=self.data10
445 else:
446 kwargs[str(self.name10)]=self.data10
447
448
449 if isinstance(self.data11, Data):
450 if self.name11 == None:
451 if isinstance(self.getAttributeObject("data11"),Link):
452 kwargs[self.getAttributeObject("data11").getAttributeName()]=self.data11
453 else:
454 kwargs["data11"]=self.data11
455 else:
456 kwargs[str(self.name11)]=self.data11
457
458
459 if isinstance(self.data12, Data):
460 if self.name12 == None:
461 if isinstance(self.getAttributeObject("data12"),Link):
462 kwargs[self.getAttributeObject("data12").getAttributeName()]=self.data12
463 else:
464 kwargs["data12"]=self.data12
465 else:
466 kwargs[str(self.name12)]=self.data12
467
468
469 if isinstance(self.data13, Data):
470 if self.name13 == None:
471 if isinstance(self.getAttributeObject("data13"),Link):
472 kwargs[self.getAttributeObject("data13").getAttributeName()]=self.data13
473 else:
474 kwargs["data13"]=self.data13
475 else:
476 kwargs[str(self.name13)]=self.data13
477
478
479 if isinstance(self.data14, Data):
480 if self.name14 == None:
481 if isinstance(self.getAttributeObject("data14"),Link):
482 kwargs[self.getAttributeObject("data14").getAttributeName()]=self.data14
483 else:
484 kwargs["data14"]=self.data14
485 else:
486 kwargs[str(self.name14)]=self.data14
487
488
489 if isinstance(self.data15, Data):
490 if self.name15 == None:
491 if isinstance(self.getAttributeObject("data15"),Link):
492 kwargs[self.getAttributeObject("data15").getAttributeName()]=self.data15
493 else:
494 kwargs["data15"]=self.data15
495 else:
496 kwargs[str(self.name15)]=self.data15
497
498
499 if isinstance(self.data16, Data):
500 if self.name16 == None:
501 if isinstance(self.getAttributeObject("data16"),Link):
502 kwargs[self.getAttributeObject("data16").getAttributeName()]=self.data16
503 else:
504 kwargs["data16"]=self.data16
505 else:
506 kwargs[str(self.name16)]=self.data16
507
508
509 if isinstance(self.data17, Data):
510 if self.name17 == None:
511 if isinstance(self.getAttributeObject("data17"),Link):
512 kwargs[self.getAttributeObject("data17").getAttributeName()]=self.data17
513 else:
514 kwargs["data17"]=self.data17
515 else:
516 kwargs[str(self.name17)]=self.data17
517
518
519 if isinstance(self.data18, Data):
520 if self.name18 == None:
521 if isinstance(self.getAttributeObject("data18"),Link):
522 kwargs[self.getAttributeObject("data18").getAttributeName()]=self.data18
523 else:
524 kwargs["data18"]=self.data18
525 else:
526 kwargs[str(self.name18)]=self.data18
527
528
529 if isinstance(self.data19, Data):
530 if self.name19 == None:
531 if isinstance(self.getAttributeObject("data19"),Link):
532 kwargs[self.getAttributeObject("data19").getAttributeName()]=self.data19
533 else:
534 kwargs["data19"]=self.data19
535 else:
536 kwargs[str(self.name19)]=self.data19
537 return kwargs
538
539 def doInitialPostprocessing(self):
540 """
541 writes vtk file at the end of initial iteration
542 """
543 super(WriteVTK,self).doInitialPostprocessing()
544 kwargs=self.collectData()
545 if len(kwargs)>0:
546 saveVTK(self.getFrameFileName(),**kwargs)
547 self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.getFrameFileName()))
548
549 def doStepPostprocessing(self, dt):
550 """
551 writes vtk file at the end of time iteration
552 """
553 super(WriteVTK,self).doStepPostprocessing(dt)
554 if self.writeFrame():
555 kwargs=self.collectData()
556 if len(kwargs)>0:
557 saveVTK(self.getFrameFileName(),**kwargs)
558 self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.getFrameFileName()))
559
560 # vim: expandtab shiftwidth=4:

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26