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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 918 - (hide annotations)
Wed Jan 3 06:30:00 2007 UTC (12 years, 6 months ago) by gross
File MIME type: text/x-python
File size: 19140 byte(s)
fixes on ESySXML to get runmodel going.

    * object ids are now local for read and write of XML
    * ParameterSets are now written with class name
    * ParameterSets linked by other ParameterSets (previously only Models) are written now 
    * list are now lists of str (rather than bools), lists with bool, int or float are mapped to numarray
    * numarray are writen with generic types Bool, Int, Float (portability!)


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