source: anuga_core/source/anuga/shallow_water/test_read_sww.py @ 7675

Last change on this file since 7675 was 7562, checked in by steve, 14 years ago

Updating the balanced and parallel code

File size: 6.9 KB
Line 
1#!/usr/bin/env python
2#
3
4# This file was reverted from changeset:5484 to changeset:5470 on 10th July
5# by Ole.
6
7import unittest
8import copy
9import os
10import numpy as num
11               
12# This is needed to run the tests of local functions
13import data_manager
14
15class Test_read_sww(unittest.TestCase):
16    # Class variable
17    verbose = False
18
19    def set_verbose(self):
20        Test_read_sww.verbose = True
21       
22    def setUp(self):
23        pass
24
25    def tearDown(self):
26        pass
27
28    def test_read_sww(self):
29        """
30        Save to an sww file and then read back the info.
31        Here we store the info "uniquely"
32        """
33
34        #---------------------------------------------------------------------
35        # Import necessary modules
36        #---------------------------------------------------------------------
37        from anuga.interface import rectangular_cross
38        from anuga.interface import Domain
39        from anuga.interface import Reflective_boundary
40        from anuga.interface import Dirichlet_boundary
41        from anuga.interface import Time_boundary
42
43        #---------------------------------------------------------------------
44        # Setup computational domain
45        #---------------------------------------------------------------------
46        length = 8.
47        width = 4.
48        dx = dy = 2   # Resolution: Length of subdivisions on both axes
49       
50        inc = 0.05 # Elevation increment
51
52        points, vertices, boundary = rectangular_cross(int(length/dx), 
53                                                       int(width/dy),
54                                                       len1=length, 
55                                                       len2=width)
56        domain = Domain(points, vertices, boundary)
57        domain.set_name('read_sww_test'+str(domain.processor))  # Output name
58        domain.set_quantities_to_be_stored({'elevation': 2,
59                                            'stage': 2,
60                                            'xmomentum': 2,
61                                            'ymomentum': 2,
62                                            'friction': 1})
63
64        domain.set_store_vertices_uniquely(True)
65       
66        #---------------------------------------------------------------------
67        # Setup initial conditions
68        #---------------------------------------------------------------------
69        domain.set_quantity('elevation', 0.0)    # Flat bed initially
70        domain.set_quantity('friction', 0.01)    # Constant friction
71        domain.set_quantity('stage', 0.0)        # Dry initial condition
72
73        #------------------------------------------------------------------
74        # Setup boundary conditions
75        #------------------------------------------------------------------
76        Bi = Dirichlet_boundary([0.4, 0, 0])          # Inflow
77        Br = Reflective_boundary(domain)              # Solid reflective wall
78        Bo = Dirichlet_boundary([-5, 0, 0])           # Outflow
79
80        domain.set_boundary({'left': Bi, 'right': Bo, 'top': Br, 'bottom': Br})
81
82        #-------------------------------------------------------------------
83        # Evolve system through time
84        #-------------------------------------------------------------------
85
86        for t in domain.evolve(yieldstep=1, finaltime=4.0):
87            pass
88           
89           
90        # Check that quantities have been stored correctly   
91        source = domain.get_name() + '.sww'
92
93
94        #x = fid.variables['x'][:]
95        #y = fid.variables['y'][:]
96        #stage = fid.variables['stage'][:]
97        #elevation = fid.variables['elevation'][:]
98        #fid.close()
99                   
100        #assert len(stage.shape) == 2
101        #assert len(elevation.shape) == 2       
102       
103        #M, N = stage.shape
104               
105        sww_file = data_manager.Read_sww(source)
106
107        #print 'last frame number',sww_file.get_last_frame_number()
108
109        assert num.allclose(sww_file.x, domain.get_vertex_coordinates()[:,0])
110        assert num.allclose(sww_file.y, domain.get_vertex_coordinates()[:,1])
111
112
113        assert num.allclose(sww_file.time, [0.0, 1.0, 2.0, 3.0, 4.0])
114       
115        M = domain.get_number_of_triangles()
116       
117        assert num.allclose(num.reshape(num.arange(3*M), (M,3)), sww_file.vertices)
118
119        last_frame_number = sww_file.get_last_frame_number() 
120        assert last_frame_number == 4
121
122        assert num.allclose(sww_file.get_bounds(), [0.0, length, 0.0, width])
123
124        assert 'stage'     in sww_file.quantities.keys()
125        assert 'friction'  in sww_file.quantities.keys()
126        assert 'elevation' in sww_file.quantities.keys()
127        assert 'xmomentum' in sww_file.quantities.keys()
128        assert 'ymomentum' in sww_file.quantities.keys()
129
130
131        for qname, q in sww_file.read_quantities(last_frame_number).items():
132            assert num.allclose(domain.get_quantity(qname).get_values(), q)
133
134        #-----------------------------------------
135        # Start the evolution off again at frame 3
136        #-----------------------------------------
137        sww_file.read_quantities(last_frame_number-1)
138
139        points, vertices, boundary = rectangular_cross(int(length/dx), 
140                                                       int(width/dy),
141                                                       len1=length, 
142                                                       len2=width)
143        new_domain = Domain(points, vertices, boundary)
144        new_domain.set_quantities_to_be_stored(None)
145
146        new_domain.set_store_vertices_uniquely(True)
147
148        for qname, q in sww_file.read_quantities(last_frame_number-1).items():
149            new_domain.set_quantity(qname, q)   
150
151
152        new_domain.set_starttime(sww_file.get_time())
153        #------------------------------------------------------------------
154        # Setup boundary conditions
155        #------------------------------------------------------------------
156        Bi = Dirichlet_boundary([0.4, 0, 0])          # Inflow
157        Br = Reflective_boundary(new_domain)          # Solid reflective wall
158        Bo = Dirichlet_boundary([-5, 0, 0])           # Outflow
159
160        new_domain.set_boundary({'left': Bi, 'right': Bo, 'top': Br, 'bottom': Br})
161
162        #-------------------------------------------------------------------
163        # Evolve system through time
164        #-------------------------------------------------------------------
165
166        for t in new_domain.evolve(yieldstep=1.0, finaltime=1.0):
167             pass
168
169        # Compare  new_domain and domain quantities
170        for quantity in domain.get_quantity_names():
171            dv = domain.get_quantity(quantity).get_values()
172            ndv = new_domain.get_quantity(quantity).get_values()
173
174            assert num.allclose( dv, ndv)
175
176        # Clean up
177        os.remove(source)
178       
179
180if __name__ == "__main__":
181    suite = unittest.makeSuite(Test_read_sww, 'test')
182    runner = unittest.TextTestRunner() #verbosity=2)
183    runner.run(suite)
184   
Note: See TracBrowser for help on using the repository browser.