source: inundation/ga/storm_surge/pyvolution/test_generic_boundary_conditions.py @ 1697

Last change on this file since 1697 was 1671, checked in by ole, 20 years ago

Moved old file function stuff out and re-tested.
File_function is now all in one function which is based on NetCDF

File size: 7.9 KB
Line 
1#!/usr/bin/env python
2
3import unittest
4from math import sqrt, pi
5
6from generic_boundary_conditions import *
7from config import epsilon
8from Numeric import allclose, array
9
10
11class Test_Generic_Boundary_Conditions(unittest.TestCase):
12    def setUp(self):
13        pass
14        #print "  Setting up"
15
16    def tearDown(self):
17        pass
18        #print "  Tearing down"
19
20
21    def test_generic(self):
22        b = Boundary()
23
24        try:
25            b.evaluate()
26        except:
27            pass
28        else:
29            raise 'Should have raised exception'
30
31
32    def test_dirichlet_empty(self):
33
34        try:
35            Bd = Dirichlet_boundary()
36        except:
37            pass
38        else:
39            raise 'Should have raised exception'
40
41    def test_dirichlet(self):
42        x = [3.14,0,0.1]
43        Bd = Dirichlet_boundary(x)
44
45        q = Bd.evaluate()
46        assert allclose(q, x)
47
48
49    def test_transmissive(self):
50        from domain import Domain
51        from quantity import Quantity
52
53        a = [0.0, 0.0]
54        b = [0.0, 2.0]
55        c = [2.0,0.0]
56        d = [0.0, 4.0]
57        e = [2.0, 2.0]
58        f = [4.0,0.0]
59
60        points = [a, b, c, d, e, f]
61
62        #bac, bce, ecf, dbe
63        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
64
65        domain = Domain(points, elements)
66        domain.check_integrity()
67
68        domain.conserved_quantities = ['stage', 'ymomentum']
69        domain.quantities['stage'] =\
70                                   Quantity(domain, [[1,2,3], [5,5,5],
71                                                     [0,0,9], [-6, 3, 3]])
72
73        domain.quantities['ymomentum'] =\
74                                   Quantity(domain, [[2,3,4], [5,5,5],
75                                                     [0,0,9], [-6, 3, 3]])
76
77
78        domain.check_integrity()
79
80        #Test transmissve bdry
81        try:
82            T = Transmissive_boundary()
83        except:
84            pass
85        else:
86            raise 'Should have raised exception'
87
88        T = Transmissive_boundary(domain)
89
90        from config import default_boundary_tag
91        domain.set_boundary( {default_boundary_tag: T} )
92
93
94        #FIXME: should not necessarily be true always.
95        #E.g. with None as a boundary object.
96        assert len(domain.boundary) == len(domain.boundary_objects)
97
98        q = T.evaluate(0, 2)  #Vol=0, edge=2
99
100        assert allclose(q, [1.5, 2.5])
101
102
103    def test_fileboundary_time_only(self):
104        """Test that boundary values can be read from file and interpolated
105        """
106
107        from domain import Domain
108        from quantity import Quantity
109        import time, os
110        from math import sin, pi
111        from config import time_format
112
113        a = [0.0, 0.0]
114        b = [0.0, 2.0]
115        c = [2.0, 0.0]
116        d = [0.0, 4.0]
117        e = [2.0, 2.0]
118        f = [4.0, 0.0]
119
120        points = [a, b, c, d, e, f]
121
122        #bac, bce, ecf, dbe
123        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
124
125        domain = Domain(points, elements)
126        domain.conserved_quantities = ['stage', 'ymomentum']
127        domain.quantities['stage'] =\
128                                   Quantity(domain, [[1,2,3], [5,5,5],
129                                                     [0,0,9], [-6, 3, 3]])
130
131        domain.quantities['ymomentum'] =\
132                                   Quantity(domain, [[2,3,4], [5,5,5],
133                                                     [0,0,9], [-6, 3, 3]])
134
135        domain.check_integrity()
136
137
138        #Write file
139        filename = 'boundarytest' + str(time.time())
140        fid = open(filename + '.txt', 'w')
141        start = time.mktime(time.strptime('2000', '%Y'))
142        dt = 5*60  #Five minute intervals
143        for i in range(10):
144            t = start + i*dt
145            t_string = time.strftime(time_format, time.gmtime(t))
146
147            fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10)))
148        fid.close()
149
150
151        #Convert ASCII file to NetCDF (Which is what we really like!)
152       
153        from data_manager import timefile2swww               
154        timefile2swww(filename, quantity_names = ['stage', 'ymomentum'])
155       
156
157
158        F = File_boundary(filename + '.sww', domain)
159
160       
161        os.remove(filename + '.txt')
162        os.remove(filename + '.sww')       
163
164
165        #Check that midpoint coordinates at boundary are correctly computed
166        assert allclose( F.midpoint_coordinates,
167                         [[0.0, 3.0], [1.0, 3.0], [0.0, 1.0],
168                         [1.0, 0.0], [3.0, 0.0], [3.0, 1.0]])
169
170        #assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0])
171        #assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0])
172        #assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0])
173        #assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0])
174        #assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0])
175        #assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0])
176
177
178        #Check time interpolation
179        from config import default_boundary_tag
180        domain.set_boundary( {default_boundary_tag: F} )
181
182        domain.time = 5*30/2  #A quarter way through first step
183        q = F.evaluate()
184        assert allclose(q, [1.0/4, sin(2*pi/10)/4])
185
186
187        domain.time = 2.5*5*60  #Half way between steps 2 and 3
188        q = F.evaluate()
189        assert allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2])
190
191
192
193    def test_fileboundary_exception(self):
194        """Test that boundary object complains if number of
195        conserved quantities are wrong
196        """
197
198        from domain import Domain
199        from quantity import Quantity
200        import time, os
201        from math import sin, pi
202        from config import time_format
203
204        a = [0.0, 0.0]
205        b = [0.0, 2.0]
206        c = [2.0,0.0]
207        d = [0.0, 4.0]
208        e = [2.0, 2.0]
209        f = [4.0,0.0]
210
211        points = [a, b, c, d, e, f]
212
213        #bac, bce, ecf, dbe
214        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
215
216        domain = Domain(points, elements)
217        domain.conserved_quantities = ['stage', 'xmomentum', 'ymomentum']
218        domain.quantities['stage'] =\
219                                   Quantity(domain, [[1,2,3], [5,5,5],
220                                                     [0,0,9], [-6, 3, 3]])
221
222        domain.quantities['xmomentum'] =\
223                                   Quantity(domain, [[2,3,4], [5,5,5],
224                                                     [0,0,9], [-6, 3, 3]])
225        domain.quantities['ymomentum'] =\
226                                   Quantity(domain, [[2,3,4], [5,5,5],
227                                                     [0,0,9], [-6, 3, 3]])
228
229        domain.check_integrity()
230
231        #Write file (with only two values)
232        filename = 'boundarytest' + str(time.time())
233        fid = open(filename + '.txt', 'w')
234        start = time.mktime(time.strptime('2000', '%Y'))
235        dt = 5*60  #Five minute intervals
236        for i in range(10):
237            t = start + i*dt
238            t_string = time.strftime(time_format, time.gmtime(t))
239
240            fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10)))
241        fid.close()
242
243
244        #Convert ASCII file to NetCDF (Which is what we really like!)
245        from data_manager import timefile2swww               
246        timefile2swww(filename, quantity_names = ['stage', 'xmomentum'])
247
248       
249        try:
250            F = File_boundary(filename + '.sww', domain)           
251        except:
252            pass
253        else:
254            raise 'Should have raised an exception'       
255       
256        os.remove(filename + '.txt')
257        os.remove(filename + '.sww')       
258
259
260
261
262
263#-------------------------------------------------------------
264if __name__ == "__main__":
265    suite = unittest.makeSuite(Test_Generic_Boundary_Conditions,'test')
266    runner = unittest.TextTestRunner()
267    runner.run(suite)
268
269
270
Note: See TracBrowser for help on using the repository browser.