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