1 | #!/usr/bin/env python |
---|
2 | ########## |
---|
3 | # Demonstration of the VTK realtime Visualiser. |
---|
4 | # Based on run_sw_rectangle.py |
---|
5 | # Jack Kelly and Stephen Roberts |
---|
6 | # October 2006 |
---|
7 | ########## |
---|
8 | |
---|
9 | # Import the realtime visualiser |
---|
10 | from anuga.visualiser import RealtimeVisualiser |
---|
11 | from vtk import vtkCubeAxesActor2D |
---|
12 | |
---|
13 | import time |
---|
14 | from Numeric import array |
---|
15 | from anuga.shallow_water import Domain |
---|
16 | from anuga.shallow_water import Reflective_boundary |
---|
17 | from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular |
---|
18 | |
---|
19 | |
---|
20 | class Set_Stage: |
---|
21 | """Set an initial condition with constant water height, for x<x0 |
---|
22 | """ |
---|
23 | |
---|
24 | def __init__(self, x0=0.25, x1=0.75, y0=0.0, y1=1.0, h=5.0, h0=0.0): |
---|
25 | self.x0 = x0 |
---|
26 | self.x1 = x1 |
---|
27 | self.y0 = y0 |
---|
28 | self.y1 = y1 |
---|
29 | self.h = h |
---|
30 | self.h0 = h0 |
---|
31 | |
---|
32 | def __call__(self, x, y): |
---|
33 | return self.h0 + self.h*((x>self.x0)&(x<self.x1)&(y>self.y0)&(y<self.y1)) |
---|
34 | |
---|
35 | M = 20 |
---|
36 | points, vertices, boundary = rectangular(M, M, len1 = 1.0, len2 = 1.0) |
---|
37 | |
---|
38 | yieldstep = 0.002 |
---|
39 | finaltime = 0.8 |
---|
40 | rect = [0.0, 0.0, 1.0, 1.0] |
---|
41 | |
---|
42 | domain = Domain(points, vertices, boundary) |
---|
43 | |
---|
44 | # Turn on the visualisation. The argument to the realtime visualier |
---|
45 | # is a domain object. |
---|
46 | v = RealtimeVisualiser(domain) |
---|
47 | |
---|
48 | # Specify the height-based-quantities to render. |
---|
49 | # Remember to set dynamic=True for time-varying quantities |
---|
50 | v.render_quantity_height("elevation", dynamic=False) |
---|
51 | # Make the stage semitransparent |
---|
52 | v.render_quantity_height("stage", opacity=0.5, dynamic=True) |
---|
53 | |
---|
54 | # Colour the stage: |
---|
55 | # Either with an RGB value as a 3-tuple of Floats, |
---|
56 | #v.colour_height_quantity('stage', (0.0, 0.0, 0.8)) |
---|
57 | # Or with a function of the quantities at that point, such as the stage height: |
---|
58 | # 0 and 1 are the minimum and maximum values of the stage. |
---|
59 | v.colour_height_quantity('stage', (lambda q:q['stage'], 0, 1)) |
---|
60 | # Or with the magnitude of the momentum at that point: |
---|
61 | # Needs the sqrt function from Numeric. Again, 0 and 10 define the colour range. |
---|
62 | #v.colour_height_quantity('stage', (lambda q:sqrt((q['xmomentum'] ** 2) + |
---|
63 | # (q['ymomentum'] ** 2)), 0, 10)) |
---|
64 | |
---|
65 | # Draw some axes on the visualiser so we can see how big the wave is |
---|
66 | v.render_axes() |
---|
67 | |
---|
68 | # Increase the number of labels on the axes |
---|
69 | v.alter_axes(vtkCubeAxesActor2D.SetNumberOfLabels, (5,)) |
---|
70 | |
---|
71 | # Draw a yellow polygon at height 2 |
---|
72 | v.overlay_polygon([(0, 0), (0, 0.1), (0.1, 0)], 2, colour=(1.0, 1.0, 0.0)) |
---|
73 | |
---|
74 | # Start the visualiser (in its own thread). |
---|
75 | v.start() |
---|
76 | |
---|
77 | #----------------------------------------------------------------- |
---|
78 | # Boundaries and Initial conditions |
---|
79 | #----------------------------------------------------------------- |
---|
80 | R = Reflective_boundary(domain) |
---|
81 | domain.set_boundary( {'left': R, 'right': R, 'bottom': R, 'top': R} ) |
---|
82 | domain.set_quantity('stage', Set_Stage(0.2, 0.4, 0.25, 0.75, 2.0, 0.00)) |
---|
83 | |
---|
84 | #----------------------------------------------------------------- |
---|
85 | # Evolve |
---|
86 | #----------------------------------------------------------------- |
---|
87 | t0 = time.time() |
---|
88 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
89 | v.update() |
---|
90 | domain.write_time() |
---|
91 | # Unhook the visualiser from the evolve loop. |
---|
92 | # It won't shutdown cleanly unless you do this. |
---|
93 | v.evolveFinished() |
---|
94 | |
---|
95 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
96 | |
---|
97 | # Wait for the visualiser to be closed |
---|
98 | v.join() |
---|