[3696] | 1 | #!/usr/bin/env python |
---|
[3873] | 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 | ########## |
---|
[3696] | 8 | |
---|
[3966] | 9 | # Import the realtime visualiser |
---|
[3873] | 10 | from anuga.visualiser import RealtimeVisualiser |
---|
| 11 | from vtk import vtkCubeAxesActor2D |
---|
| 12 | |
---|
[3697] | 13 | import time |
---|
[3696] | 14 | from Numeric import array |
---|
| 15 | from anuga.shallow_water import Domain |
---|
[3697] | 16 | from anuga.shallow_water import Reflective_boundary |
---|
[3696] | 17 | from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular |
---|
| 18 | |
---|
[3873] | 19 | |
---|
[3696] | 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 | |
---|
[3958] | 35 | M = 20 |
---|
[3697] | 36 | points, vertices, boundary = rectangular(M, M, len1 = 1.0, len2 = 1.0) |
---|
[3696] | 37 | |
---|
[3697] | 38 | yieldstep = 0.002 |
---|
[3874] | 39 | finaltime = 0.8 |
---|
[3697] | 40 | rect = [0.0, 0.0, 1.0, 1.0] |
---|
[3696] | 41 | |
---|
[3697] | 42 | domain = Domain(points, vertices, boundary) |
---|
| 43 | |
---|
[3873] | 44 | # Turn on the visualisation. The argument to the realtime visualier |
---|
| 45 | # is a domain object. |
---|
| 46 | v = RealtimeVisualiser(domain) |
---|
[3696] | 47 | |
---|
[3873] | 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) |
---|
[4267] | 51 | # Make the stage semitransparent |
---|
| 52 | v.render_quantity_height("stage", opacity=0.5, dynamic=True) |
---|
[3696] | 53 | |
---|
[3873] | 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)) |
---|
[3696] | 64 | |
---|
[3873] | 65 | # Draw some axes on the visualiser so we can see how big the wave is |
---|
| 66 | v.render_axes() |
---|
[3696] | 67 | |
---|
[3873] | 68 | # Increase the number of labels on the axes |
---|
| 69 | v.alter_axes(vtkCubeAxesActor2D.SetNumberOfLabels, (5,)) |
---|
[3696] | 70 | |
---|
[3874] | 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)) |
---|
[3696] | 73 | |
---|
[3873] | 74 | # Start the visualiser (in its own thread). |
---|
| 75 | v.start() |
---|
[3696] | 76 | |
---|
[3697] | 77 | #----------------------------------------------------------------- |
---|
| 78 | # Boundaries and Initial conditions |
---|
| 79 | #----------------------------------------------------------------- |
---|
| 80 | R = Reflective_boundary(domain) |
---|
| 81 | domain.set_boundary( {'left': R, 'right': R, 'bottom': R, 'top': R} ) |
---|
[3958] | 82 | domain.set_quantity('stage', Set_Stage(0.2, 0.4, 0.25, 0.75, 2.0, 0.00)) |
---|
[3697] | 83 | |
---|
| 84 | #----------------------------------------------------------------- |
---|
| 85 | # Evolve |
---|
| 86 | #----------------------------------------------------------------- |
---|
| 87 | t0 = time.time() |
---|
[3696] | 88 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
[3873] | 89 | v.update() |
---|
[3696] | 90 | domain.write_time() |
---|
[3873] | 91 | # Unhook the visualiser from the evolve loop. |
---|
| 92 | # It won't shutdown cleanly unless you do this. |
---|
| 93 | v.evolveFinished() |
---|
[3696] | 94 | |
---|
| 95 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 96 | |
---|
[3873] | 97 | # Wait for the visualiser to be closed |
---|
| 98 | v.join() |
---|