/[escript]/release/5.2/modellib/py_src/visualization.py
ViewVC logotype

Contents of /release/5.2/modellib/py_src/visualization.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6692 - (show annotations)
Mon Jun 25 02:31:06 2018 UTC (2 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 16069 byte(s)
Fix

1
2 ##############################################################################
3 #
4 # Copyright (c) 2003-2018 by The University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Apache License, version 2.0
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 from __future__ import print_function, division
18
19 __copyright__="""Copyright (c) 2003-2018 by The University of Queensland
20 http://www.uq.edu.au
21 Primary Business: Queensland, Australia"""
22 __license__="""Licensed under the Apache License, version 2.0
23 http://www.apache.org/licenses/LICENSE-2.0"""
24 __url__="https://launchpad.net/escript-finley"
25
26 from esys.escript.modelframe import Model, Link
27 from esys.escript import Data
28 from esys.weipa import saveVTK
29 import os
30
31 class Visualization(Model):
32 """
33 Generic visualization Model
34
35 :ivar t: current time (in)
36 :ivar n: frame counter (in)
37 :ivar dt: increment for output (in)
38 :ivar filename: name of the output file (in)
39 """
40
41 def __init__(self, **kwargs):
42 """
43 Initialisation of the visualisation model object
44
45 :keyword debug: Debugging flag
46 """
47 super(Visualization,self).__init__(**kwargs)
48 self.declareParameter(t=0.,
49 n=0,
50 dt=1,
51 filename="movie.mpg")
52
53 def doInitialization(self):
54 """
55 does some kind of initialisation
56 """
57 self.__last_t=self.t
58 fnc=self.filename.split('.')
59 if len(fnc)==0:
60 self.__frame_filename="data.%s.xml"
61 else:
62 n=fnc[0]
63 for i in range(1,len(fnc)-1):
64 n+="."+fnc[i]
65 if len(fnc)==1:
66 self.__frame_filename=n+".%s"
67 else:
68 self.__frame_filename=n+".%s."+fnc[-1]
69 self.trace("output filename is %s."%self.__frame_filename)
70
71 def getFrameFileName(self):
72 return self.__frame_filename%self.getFrameCounter()
73
74 def writeFrame(self):
75 """
76 returns True if the time stamp for writing frame is reached.
77 """
78 out=self.t>=self.__last_t+self.dt
79 if out:
80 self.__last_t+=self.dt
81 self.n+=1
82 return out
83
84 def getFrameCounter(self):
85 """
86 returns a frame counter
87 """
88 return self.n
89
90 def getSafeTimeStepSize(self,dt):
91 """
92 returns new step size
93
94 :param dt: last time step size used
95 :type dt: ``float``
96 :return: time step size that can savely be used
97 :rtype: ``float``
98 """
99 return self.__last_t+self.dt-self.t
100
101 class WriteVTK(Visualization):
102 """
103 Writes data into a VTK file.
104
105 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
106 the data are defined by a `Link` the name of the target attribute is used as a tag.
107
108 :ivar data0: data set 0 to be written
109 :type data0: `escript.Data`
110 :ivar name0: name tag for data set 0
111 :type name0: ``str`` or ``None``
112 :ivar data1: data set 1 to be written
113 :type data1: `escript.Data`
114 :ivar name1: name tag for data set 1
115 :type name1: ``str`` or ``None``
116 :ivar data2: data set 2 to be written
117 :type data2: `escript.Data`
118 :ivar name2: name tag for data set 2
119 :type name2: ``str`` or ``None``
120 :ivar data3: data set 3 to be written
121 :type data3: `escript.Data`
122 :ivar name3: name tag for data set 3
123 :type name3: ``str`` or ``None``
124 :ivar data4: data set 4 to be written
125 :type data4: `escript.Data`
126 :ivar name4: name tag for data set 4
127 :type name4: ``str`` or ``None``
128 :ivar data5: data set 5 to be written
129 :type data5: `escript.Data`
130 :ivar name5: name tag for data set 5
131 :type name5: ``str`` or ``None``
132 :ivar data6: data set 6 to be written
133 :type data6: `escript.Data`
134 :ivar name6: name tag for data set 6
135 :type name6: ``str`` or ``None``
136 :ivar data7: data set 7 to be written
137 :type data7: `escript.Data`
138 :ivar name7: name tag for data set 7
139 :type name7: ``str`` or ``None``
140 :ivar data8: data set 8 to be written
141 :type data8: `escript.Data`
142 :ivar name8: name tag for data set 8
143 :type name8: ``str`` or ``None``
144 :ivar data9: data set 9 to be written
145 :type data9: `escript.Data`
146 :ivar name9: name tag for data set 9
147 :type name9: ``str`` or ``None``
148 :ivar data10: data set 10 to be written
149 :type data10: `escript.Data`
150 :ivar name10: name tag for data set 10
151 :type name10: ``str`` or ``None``
152 :ivar data11: data set 11 to be written
153 :type data11: `escript.Data`
154 :ivar name11: name tag for data set 11
155 :type name11: ``str`` or ``None``
156 :ivar data12: data set 12 to be written
157 :type data12: `escript.Data`
158 :ivar name12: name tag for data set 12
159 :type name12: ``str`` or ``None``
160 :ivar data13: data set 13 to be written
161 :type data13: `escript.Data`
162 :ivar name13: name tag for data set 13
163 :type name13: ``str`` or ``None``
164 :ivar data14: data set 14 to be written
165 :type data14: `escript.Data`
166 :ivar name14: name tag for data set 14
167 :type name14: ``str`` or ``None``
168 :ivar data15: data set 15 to be written
169 :type data15: `escript.Data`
170 :ivar name15: name tag for data set 15
171 :type name15: ``str`` or ``None``
172 :ivar data16: data set 16 to be written
173 :type data16: `escript.Data`
174 :ivar name16: name tag for data set 16
175 :type name16: ``str`` or ``None``
176 :ivar data17: data set 17 to be written
177 :type data17: `escript.Data`
178 :ivar name17: name tag for data set 17
179 :type name17: ``str`` or ``None``
180 :ivar data18: data set 18 to be written
181 :type data18: `escript.Data`
182 :ivar name18: name tag for data set 18
183 :type name18: ``str`` or ``None``
184 :ivar data19: data set 19 to be written
185 :type data19: `escript.Data`
186 :ivar name19: name tag for data set 19
187 :type name19: ``str`` or ``None``
188 """
189 def __init__(self, **kwargs):
190 """
191 Initialisation of the WriteVTK object
192
193 :keyword debug: debugging flag
194 :type debug: ``bool``
195 """
196 super(WriteVTK,self).__init__(**kwargs)
197 self.declareParameter(data0=None,name0=None,
198 data1=None,name1=None,
199 data2=None,name2=None,
200 data3=None,name3=None,
201 data4=None,name4=None,
202 data5=None,name5=None,
203 data6=None,name6=None,
204 data7=None,name7=None,
205 data8=None,name8=None,
206 data9=None,name9=None,
207 data10=None,name10=None,
208 data11=None,name11=None,
209 data12=None,name12=None,
210 data13=None,name13=None,
211 data14=None,name14=None,
212 data15=None,name15=None,
213 data16=None,name16=None,
214 data17=None,name17=None,
215 data18=None,name18=None,
216 data19=None,name19=None)
217 def collectData(self):
218 kwargs={}
219 if isinstance(self.data0, Data):
220 if self.name0 == None:
221 if isinstance(self.getAttributeObject("data0"),Link):
222 kwargs[self.getAttributeObject("data0").getAttributeName()]=self.data0
223 else:
224 kwargs["data0"]=self.data0
225 else:
226 kwargs[str(self.name0)]=self.data0
227
228
229 if isinstance(self.data1, Data):
230 if self.name1 == None:
231 if isinstance(self.getAttributeObject("data1"),Link):
232 kwargs[self.getAttributeObject("data1").getAttributeName()]=self.data1
233 else:
234 kwargs["data1"]=self.data1
235 else:
236 kwargs[str(self.name1)]=self.data1
237
238
239 if isinstance(self.data2, Data):
240 if self.name2 == None:
241 if isinstance(self.getAttributeObject("data2"),Link):
242 kwargs[self.getAttributeObject("data2").getAttributeName()]=self.data2
243 else:
244 kwargs["data2"]=self.data2
245 else:
246 kwargs[str(self.name2)]=self.data2
247
248
249 if isinstance(self.data3, Data):
250 if self.name3 == None:
251 if isinstance(self.getAttributeObject("data3"),Link):
252 kwargs[self.getAttributeObject("data3").getAttributeName()]=self.data3
253 else:
254 kwargs["data3"]=self.data3
255 else:
256 kwargs[str(self.name3)]=self.data3
257
258
259 if isinstance(self.data4, Data):
260 if self.name4 == None:
261 if isinstance(self.getAttributeObject("data4"),Link):
262 kwargs[self.getAttributeObject("data4").getAttributeName()]=self.data4
263 else:
264 kwargs["data4"]=self.data4
265 else:
266 kwargs[str(self.name4)]=self.data4
267
268
269 if isinstance(self.data5, Data):
270 if self.name5 == None:
271 if isinstance(self.getAttributeObject("data5"),Link):
272 kwargs[self.getAttributeObject("data5").getAttributeName()]=self.data5
273 else:
274 kwargs["data5"]=self.data5
275 else:
276 kwargs[str(self.name5)]=self.data5
277
278
279 if isinstance(self.data6, Data):
280 if self.name6 == None:
281 if isinstance(self.getAttributeObject("data6"),Link):
282 kwargs[self.getAttributeObject("data6").getAttributeName()]=self.data6
283 else:
284 kwargs["data6"]=self.data6
285 else:
286 kwargs[str(self.name6)]=self.data6
287
288
289 if isinstance(self.data7, Data):
290 if self.name7 == None:
291 if isinstance(self.getAttributeObject("data7"),Link):
292 kwargs[self.getAttributeObject("data7").getAttributeName()]=self.data7
293 else:
294 kwargs["data7"]=self.data7
295 else:
296 kwargs[str(self.name7)]=self.data7
297
298
299 if isinstance(self.data8, Data):
300 if self.name8 == None:
301 if isinstance(self.getAttributeObject("data8"),Link):
302 kwargs[self.getAttributeObject("data8").getAttributeName()]=self.data8
303 else:
304 kwargs["data8"]=self.data8
305 else:
306 kwargs[str(self.name8)]=self.data8
307
308
309 if isinstance(self.data9, Data):
310 if self.name9 == None:
311 if isinstance(self.getAttributeObject("data9"),Link):
312 kwargs[self.getAttributeObject("data9").getAttributeName()]=self.data9
313 else:
314 kwargs["data9"]=self.data9
315 else:
316 kwargs[str(self.name9)]=self.data9
317
318
319 if isinstance(self.data10, Data):
320 if self.name10 == None:
321 if isinstance(self.getAttributeObject("data10"),Link):
322 kwargs[self.getAttributeObject("data10").getAttributeName()]=self.data10
323 else:
324 kwargs["data10"]=self.data10
325 else:
326 kwargs[str(self.name10)]=self.data10
327
328
329 if isinstance(self.data11, Data):
330 if self.name11 == None:
331 if isinstance(self.getAttributeObject("data11"),Link):
332 kwargs[self.getAttributeObject("data11").getAttributeName()]=self.data11
333 else:
334 kwargs["data11"]=self.data11
335 else:
336 kwargs[str(self.name11)]=self.data11
337
338
339 if isinstance(self.data12, Data):
340 if self.name12 == None:
341 if isinstance(self.getAttributeObject("data12"),Link):
342 kwargs[self.getAttributeObject("data12").getAttributeName()]=self.data12
343 else:
344 kwargs["data12"]=self.data12
345 else:
346 kwargs[str(self.name12)]=self.data12
347
348
349 if isinstance(self.data13, Data):
350 if self.name13 == None:
351 if isinstance(self.getAttributeObject("data13"),Link):
352 kwargs[self.getAttributeObject("data13").getAttributeName()]=self.data13
353 else:
354 kwargs["data13"]=self.data13
355 else:
356 kwargs[str(self.name13)]=self.data13
357
358
359 if isinstance(self.data14, Data):
360 if self.name14 == None:
361 if isinstance(self.getAttributeObject("data14"),Link):
362 kwargs[self.getAttributeObject("data14").getAttributeName()]=self.data14
363 else:
364 kwargs["data14"]=self.data14
365 else:
366 kwargs[str(self.name14)]=self.data14
367
368
369 if isinstance(self.data15, Data):
370 if self.name15 == None:
371 if isinstance(self.getAttributeObject("data15"),Link):
372 kwargs[self.getAttributeObject("data15").getAttributeName()]=self.data15
373 else:
374 kwargs["data15"]=self.data15
375 else:
376 kwargs[str(self.name15)]=self.data15
377
378
379 if isinstance(self.data16, Data):
380 if self.name16 == None:
381 if isinstance(self.getAttributeObject("data16"),Link):
382 kwargs[self.getAttributeObject("data16").getAttributeName()]=self.data16
383 else:
384 kwargs["data16"]=self.data16
385 else:
386 kwargs[str(self.name16)]=self.data16
387
388
389 if isinstance(self.data17, Data):
390 if self.name17 == None:
391 if isinstance(self.getAttributeObject("data17"),Link):
392 kwargs[self.getAttributeObject("data17").getAttributeName()]=self.data17
393 else:
394 kwargs["data17"]=self.data17
395 else:
396 kwargs[str(self.name17)]=self.data17
397
398
399 if isinstance(self.data18, Data):
400 if self.name18 == None:
401 if isinstance(self.getAttributeObject("data18"),Link):
402 kwargs[self.getAttributeObject("data18").getAttributeName()]=self.data18
403 else:
404 kwargs["data18"]=self.data18
405 else:
406 kwargs[str(self.name18)]=self.data18
407
408
409 if isinstance(self.data19, Data):
410 if self.name19 == None:
411 if isinstance(self.getAttributeObject("data19"),Link):
412 kwargs[self.getAttributeObject("data19").getAttributeName()]=self.data19
413 else:
414 kwargs["data19"]=self.data19
415 else:
416 kwargs[str(self.name19)]=self.data19
417 return kwargs
418
419 def doInitialPostprocessing(self):
420 """
421 writes vtk file at the end of initial iteration
422 """
423 super(WriteVTK,self).doInitialPostprocessing()
424 kwargs=self.collectData()
425 if len(kwargs)>0:
426 saveVTK(self.getFrameFileName(),**kwargs)
427 self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.getFrameFileName()))
428
429 def doStepPostprocessing(self, dt):
430 """
431 writes vtk file at the end of time iteration
432 """
433 super(WriteVTK,self).doStepPostprocessing(dt)
434 if self.writeFrame():
435 kwargs=self.collectData()
436 if len(kwargs)>0:
437 saveVTK(self.getFrameFileName(),**kwargs)
438 self.trace("%s-th frame at time %s is writen to %s"%(self.getFrameCounter(),self.t,self.getFrameFileName()))
439
440 # 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