source: trunk/anuga_core/source/anuga/structures/test_inlet_operator.py @ 8092

Last change on this file since 8092 was 8092, checked in by habili, 13 years ago

Updated the inlet operator to deal with variable discharge rates.
inlet_operator_test1.tms and inlet_operator_test1.tms are unit tests to test the variable
discharge rate code.

File size: 5.7 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5import os.path
6import sys
7
8import numpy
9import anuga
10
11from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross
12from anuga.shallow_water.shallow_water_domain import Domain
13from anuga.abstract_2d_finite_volumes.util import file_function
14
15from anuga.structures.inlet_operator import Inlet_operator
16
17class Test_inlet_operator(unittest.TestCase):
18    """
19        Test the boyd box operator, in particular the discharge_routine!
20    """
21
22    def setUp(self):
23        pass
24
25    def tearDown(self):
26        pass
27   
28   
29    def _create_domain(self,d_length,
30                            d_width,
31                            dx,
32                            dy,
33                            elevation_0,
34                            elevation_1,
35                            stage_0,
36                            stage_1):
37       
38        points, vertices, boundary = rectangular_cross(int(d_length/dx), int(d_width/dy),
39                                                        len1=d_length, len2=d_width)
40        domain = Domain(points, vertices, boundary)   
41        domain.set_name('Test_Outlet_Inlet')                 # Output name
42        domain.set_store()
43        domain.set_default_order(2)
44        domain.H0 = 0.01
45        domain.tight_slope_limiters = 1
46
47        #print 'Size', len(domain)
48
49        #------------------------------------------------------------------------------
50        # Setup initial conditions
51        #------------------------------------------------------------------------------
52
53        def elevation(x, y):
54            """Set up a elevation
55            """
56           
57            z = numpy.zeros(x.shape,dtype='d')
58            z[:] = elevation_0
59           
60            numpy.putmask(z, x > d_length/2, elevation_1)
61   
62            return z
63           
64        def stage(x,y):
65            """Set up stage
66            """
67            z = numpy.zeros(x.shape,dtype='d')
68            z[:] = stage_0
69           
70            numpy.putmask(z, x > d_length/2, stage_1)
71
72            return z
73           
74        #print 'Setting Quantities....'
75        domain.set_quantity('elevation', elevation)  # Use function for elevation
76        domain.set_quantity('stage',  stage)   # Use function for elevation
77
78        Br = anuga.Reflective_boundary(domain)
79        domain.set_boundary({'left': Br, 'right': Br, 'top': Br, 'bottom': Br})
80       
81        return domain
82
83    def test_inlet_constant_Q(self):
84        """test_inlet_Q
85       
86        This tests that the inlet operator adds the correct amount of water
87        """
88
89        stage_0 = 11.0
90        stage_1 = 10.0
91        elevation_0 = 10.0
92        elevation_1 = 10.0
93
94        domain_length = 200.0
95        domain_width = 200.0
96       
97
98        domain = self._create_domain(d_length=domain_length,
99                                     d_width=domain_width,
100                                     dx = 10.0,
101                                     dy = 10.0,
102                                     elevation_0 = elevation_0,
103                                     elevation_1 = elevation_1,
104                                     stage_0 = stage_0,
105                                     stage_1 = stage_1)
106
107        vol0 = domain.compute_total_volume()
108
109        finaltime = 3.0
110        line1 = [[95.0, 10.0], [105.0, 10.0]]
111        Q1 = 5.00
112       
113        line2 = [[10.0, 90.0], [20.0, 90.0]]
114        Q2 = 10.0
115       
116        Inlet_operator(domain, line1, Q1)
117        Inlet_operator(domain, line2, Q2)
118
119        for t in domain.evolve(yieldstep = 1.0, finaltime = finaltime):
120            #domain.write_time()
121            #print domain.volumetric_balance_statistics()
122            pass
123 
124
125        vol1 = domain.compute_total_volume()
126
127        assert numpy.allclose((Q1+Q2)*finaltime, vol1-vol0, rtol=1.0e-8) 
128       
129    def test_inlet_variable_Q(self):
130        """test_inlet_Q
131       
132        This tests that the inlet operator adds the correct amount of water
133        """
134
135        stage_0 = 11.0
136        stage_1 = 10.0
137        elevation_0 = 10.0
138        elevation_1 = 10.0
139
140        domain_length = 200.0
141        domain_width = 200.0
142       
143
144        domain = self._create_domain(d_length=domain_length,
145                                     d_width=domain_width,
146                                     dx = 10.0,
147                                     dy = 10.0,
148                                     elevation_0 = elevation_0,
149                                     elevation_1 = elevation_1,
150                                     stage_0 = stage_0,
151                                     stage_1 = stage_1)
152
153        vol0 = domain.compute_total_volume()
154
155        finaltime = 3.0
156        line1 = [[95.0, 10.0], [105.0, 10.0]]
157        Q1 = file_function('inlet_operator_test1.tms', quantities=['hydrograph'])
158       
159        line2 = [[10.0, 90.0], [20.0, 90.0]]
160        Q2 = file_function('inlet_operator_test2.tms', quantities=['hydrograph'])
161       
162        Inlet_operator(domain, line1, Q1)
163        Inlet_operator(domain, line2, Q2)
164
165        for t in domain.evolve(yieldstep = 1.0, finaltime = finaltime):
166            #domain.write_time()
167            #print domain.volumetric_balance_statistics()
168            pass
169 
170
171        vol1 = domain.compute_total_volume()
172       
173        print vol1 - vol0
174       
175        assert numpy.allclose(13.5, vol1-vol0, rtol=1.0e-8) 
176               
177
178
179# =========================================================================
180if __name__ == "__main__":
181    suite = unittest.makeSuite(Test_inlet_operator, 'test')
182    runner = unittest.TextTestRunner()
183    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.