/[escript]/branches/3.4.1/modellib/py_src/visualization.py
ViewVC logotype

Contents of /branches/3.4.1/modellib/py_src/visualization.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4587 - (show annotations)
Wed Dec 11 06:17:09 2013 UTC (5 years, 3 months ago) by jfenwick
File MIME type: text/x-python
File size: 15964 byte(s)
Preparation begins

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 WriteVTK(Visualization):
99 """
100 Writes data into a VTK file.
101
102 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
103 the data are defined by a `Link` the name of the target attribute is used as a tag.
104
105 :ivar data0: data set 0 to be written
106 :type data0: `escript.Data`
107 :ivar name0: name tag for data set 0
108 :type name0: ``str`` or ``None``
109 :ivar data1: data set 1 to be written
110 :type data1: `escript.Data`
111 :ivar name1: name tag for data set 1
112 :type name1: ``str`` or ``None``
113 :ivar data2: data set 2 to be written
114 :type data2: `escript.Data`
115 :ivar name2: name tag for data set 2
116 :type name2: ``str`` or ``None``
117 :ivar data3: data set 3 to be written
118 :type data3: `escript.Data`
119 :ivar name3: name tag for data set 3
120 :type name3: ``str`` or ``None``
121 :ivar data4: data set 4 to be written
122 :type data4: `escript.Data`
123 :ivar name4: name tag for data set 4
124 :type name4: ``str`` or ``None``
125 :ivar data5: data set 5 to be written
126 :type data5: `escript.Data`
127 :ivar name5: name tag for data set 5
128 :type name5: ``str`` or ``None``
129 :ivar data6: data set 6 to be written
130 :type data6: `escript.Data`
131 :ivar name6: name tag for data set 6
132 :type name6: ``str`` or ``None``
133 :ivar data7: data set 7 to be written
134 :type data7: `escript.Data`
135 :ivar name7: name tag for data set 7
136 :type name7: ``str`` or ``None``
137 :ivar data8: data set 8 to be written
138 :type data8: `escript.Data`
139 :ivar name8: name tag for data set 8
140 :type name8: ``str`` or ``None``
141 :ivar data9: data set 9 to be written
142 :type data9: `escript.Data`
143 :ivar name9: name tag for data set 9
144 :type name9: ``str`` or ``None``
145 :ivar data10: data set 10 to be written
146 :type data10: `escript.Data`
147 :ivar name10: name tag for data set 10
148 :type name10: ``str`` or ``None``
149 :ivar data11: data set 11 to be written
150 :type data11: `escript.Data`
151 :ivar name11: name tag for data set 11
152 :type name11: ``str`` or ``None``
153 :ivar data12: data set 12 to be written
154 :type data12: `escript.Data`
155 :ivar name12: name tag for data set 12
156 :type name12: ``str`` or ``None``
157 :ivar data13: data set 13 to be written
158 :type data13: `escript.Data`
159 :ivar name13: name tag for data set 13
160 :type name13: ``str`` or ``None``
161 :ivar data14: data set 14 to be written
162 :type data14: `escript.Data`
163 :ivar name14: name tag for data set 14
164 :type name14: ``str`` or ``None``
165 :ivar data15: data set 15 to be written
166 :type data15: `escript.Data`
167 :ivar name15: name tag for data set 15
168 :type name15: ``str`` or ``None``
169 :ivar data16: data set 16 to be written
170 :type data16: `escript.Data`
171 :ivar name16: name tag for data set 16
172 :type name16: ``str`` or ``None``
173 :ivar data17: data set 17 to be written
174 :type data17: `escript.Data`
175 :ivar name17: name tag for data set 17
176 :type name17: ``str`` or ``None``
177 :ivar data18: data set 18 to be written
178 :type data18: `escript.Data`
179 :ivar name18: name tag for data set 18
180 :type name18: ``str`` or ``None``
181 :ivar data19: data set 19 to be written
182 :type data19: `escript.Data`
183 :ivar name19: name tag for data set 19
184 :type name19: ``str`` or ``None``
185 """
186 def __init__(self, **kwargs):
187 """
188 Initialisation of the WriteVTK object
189
190 :keyword debug: debugging flag
191 :type debug: ``bool``
192 """
193 super(WriteVTK,self).__init__(**kwargs)
194 self.declareParameter(data0=None,name0=None,
195 data1=None,name1=None,
196 data2=None,name2=None,
197 data3=None,name3=None,
198 data4=None,name4=None,
199 data5=None,name5=None,
200 data6=None,name6=None,
201 data7=None,name7=None,
202 data8=None,name8=None,
203 data9=None,name9=None,
204 data10=None,name10=None,
205 data11=None,name11=None,
206 data12=None,name12=None,
207 data13=None,name13=None,
208 data14=None,name14=None,
209 data15=None,name15=None,
210 data16=None,name16=None,
211 data17=None,name17=None,
212 data18=None,name18=None,
213 data19=None,name19=None)
214 def collectData(self):
215 kwargs={}
216 if isinstance(self.data0, Data):
217 if self.name0 == None:
218 if isinstance(self.getAttributeObject("data0"),Link):
219 kwargs[self.getAttributeObject("data0").getAttributeName()]=self.data0
220 else:
221 kwargs["data0"]=self.data0
222 else:
223 kwargs[str(self.name0)]=self.data0
224
225
226 if isinstance(self.data1, Data):
227 if self.name1 == None:
228 if isinstance(self.getAttributeObject("data1"),Link):
229 kwargs[self.getAttributeObject("data1").getAttributeName()]=self.data1
230 else:
231 kwargs["data1"]=self.data1
232 else:
233 kwargs[str(self.name1)]=self.data1
234
235
236 if isinstance(self.data2, Data):
237 if self.name2 == None:
238 if isinstance(self.getAttributeObject("data2"),Link):
239 kwargs[self.getAttributeObject("data2").getAttributeName()]=self.data2
240 else:
241 kwargs["data2"]=self.data2
242 else:
243 kwargs[str(self.name2)]=self.data2
244
245
246 if isinstance(self.data3, Data):
247 if self.name3 == None:
248 if isinstance(self.getAttributeObject("data3"),Link):
249 kwargs[self.getAttributeObject("data3").getAttributeName()]=self.data3
250 else:
251 kwargs["data3"]=self.data3
252 else:
253 kwargs[str(self.name3)]=self.data3
254
255
256 if isinstance(self.data4, Data):
257 if self.name4 == None:
258 if isinstance(self.getAttributeObject("data4"),Link):
259 kwargs[self.getAttributeObject("data4").getAttributeName()]=self.data4
260 else:
261 kwargs["data4"]=self.data4
262 else:
263 kwargs[str(self.name4)]=self.data4
264
265
266 if isinstance(self.data5, Data):
267 if self.name5 == None:
268 if isinstance(self.getAttributeObject("data5"),Link):
269 kwargs[self.getAttributeObject("data5").getAttributeName()]=self.data5
270 else:
271 kwargs["data5"]=self.data5
272 else:
273 kwargs[str(self.name5)]=self.data5
274
275
276 if isinstance(self.data6, Data):
277 if self.name6 == None:
278 if isinstance(self.getAttributeObject("data6"),Link):
279 kwargs[self.getAttributeObject("data6").getAttributeName()]=self.data6
280 else:
281 kwargs["data6"]=self.data6
282 else:
283 kwargs[str(self.name6)]=self.data6
284
285
286 if isinstance(self.data7, Data):
287 if self.name7 == None:
288 if isinstance(self.getAttributeObject("data7"),Link):
289 kwargs[self.getAttributeObject("data7").getAttributeName()]=self.data7
290 else:
291 kwargs["data7"]=self.data7
292 else:
293 kwargs[str(self.name7)]=self.data7
294
295
296 if isinstance(self.data8, Data):
297 if self.name8 == None:
298 if isinstance(self.getAttributeObject("data8"),Link):
299 kwargs[self.getAttributeObject("data8").getAttributeName()]=self.data8
300 else:
301 kwargs["data8"]=self.data8
302 else:
303 kwargs[str(self.name8)]=self.data8
304
305
306 if isinstance(self.data9, Data):
307 if self.name9 == None:
308 if isinstance(self.getAttributeObject("data9"),Link):
309 kwargs[self.getAttributeObject("data9").getAttributeName()]=self.data9
310 else:
311 kwargs["data9"]=self.data9
312 else:
313 kwargs[str(self.name9)]=self.data9
314
315
316 if isinstance(self.data10, Data):
317 if self.name10 == None:
318 if isinstance(self.getAttributeObject("data10"),Link):
319 kwargs[self.getAttributeObject("data10").getAttributeName()]=self.data10
320 else:
321 kwargs["data10"]=self.data10
322 else:
323 kwargs[str(self.name10)]=self.data10
324
325
326 if isinstance(self.data11, Data):
327 if self.name11 == None:
328 if isinstance(self.getAttributeObject("data11"),Link):
329 kwargs[self.getAttributeObject("data11").getAttributeName()]=self.data11
330 else:
331 kwargs["data11"]=self.data11
332 else:
333 kwargs[str(self.name11)]=self.data11
334
335
336 if isinstance(self.data12, Data):
337 if self.name12 == None:
338 if isinstance(self.getAttributeObject("data12"),Link):
339 kwargs[self.getAttributeObject("data12").getAttributeName()]=self.data12
340 else:
341 kwargs["data12"]=self.data12
342 else:
343 kwargs[str(self.name12)]=self.data12
344
345
346 if isinstance(self.data13, Data):
347 if self.name13 == None:
348 if isinstance(self.getAttributeObject("data13"),Link):
349 kwargs[self.getAttributeObject("data13").getAttributeName()]=self.data13
350 else:
351 kwargs["data13"]=self.data13
352 else:
353 kwargs[str(self.name13)]=self.data13
354
355
356 if isinstance(self.data14, Data):
357 if self.name14 == None:
358 if isinstance(self.getAttributeObject("data14"),Link):
359 kwargs[self.getAttributeObject("data14").getAttributeName()]=self.data14
360 else:
361 kwargs["data14"]=self.data14
362 else:
363 kwargs[str(self.name14)]=self.data14
364
365
366 if isinstance(self.data15, Data):
367 if self.name15 == None:
368 if isinstance(self.getAttributeObject("data15"),Link):
369 kwargs[self.getAttributeObject("data15").getAttributeName()]=self.data15
370 else:
371 kwargs["data15"]=self.data15
372 else:
373 kwargs[str(self.name15)]=self.data15
374
375
376 if isinstance(self.data16, Data):
377 if self.name16 == None:
378 if isinstance(self.getAttributeObject("data16"),Link):
379 kwargs[self.getAttributeObject("data16").getAttributeName()]=self.data16
380 else:
381 kwargs["data16"]=self.data16
382 else:
383 kwargs[str(self.name16)]=self.data16
384
385
386 if isinstance(self.data17, Data):
387 if self.name17 == None:
388 if isinstance(self.getAttributeObject("data17"),Link):
389 kwargs[self.getAttributeObject("data17").getAttributeName()]=self.data17
390 else:
391 kwargs["data17"]=self.data17
392 else:
393 kwargs[str(self.name17)]=self.data17
394
395
396 if isinstance(self.data18, Data):
397 if self.name18 == None:
398 if isinstance(self.getAttributeObject("data18"),Link):
399 kwargs[self.getAttributeObject("data18").getAttributeName()]=self.data18
400 else:
401 kwargs["data18"]=self.data18
402 else:
403 kwargs[str(self.name18)]=self.data18
404
405
406 if isinstance(self.data19, Data):
407 if self.name19 == None:
408 if isinstance(self.getAttributeObject("data19"),Link):
409 kwargs[self.getAttributeObject("data19").getAttributeName()]=self.data19
410 else:
411 kwargs["data19"]=self.data19
412 else:
413 kwargs[str(self.name19)]=self.data19
414 return kwargs
415
416 def doInitialPostprocessing(self):
417 """
418 writes vtk file at the end of initial iteration
419 """
420 super(WriteVTK,self).doInitialPostprocessing()
421 kwargs=self.collectData()
422 if len(kwargs)>0:
423 saveVTK(self.getFrameFileName(),**kwargs)
424 self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.getFrameFileName()))
425
426 def doStepPostprocessing(self, dt):
427 """
428 writes vtk file at the end of time iteration
429 """
430 super(WriteVTK,self).doStepPostprocessing(dt)
431 if self.writeFrame():
432 kwargs=self.collectData()
433 if len(kwargs)>0:
434 saveVTK(self.getFrameFileName(),**kwargs)
435 self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.getFrameFileName()))
436
437 # 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