source: inundation/pyvolution/test_generic_boundary_conditions.py @ 1882

Last change on this file since 1882 was 1835, checked in by ole, 19 years ago

Implemented tms file format (like sww without x, y)
Fixed broken test in sww2ers

File size: 8.0 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        This is using the .tms file format
106        """
107
108        from domain import Domain
109        from quantity import Quantity
110        import time, os
111        from math import sin, pi
112        from config import time_format
113
114        a = [0.0, 0.0]
115        b = [0.0, 2.0]
116        c = [2.0, 0.0]
117        d = [0.0, 4.0]
118        e = [2.0, 2.0]
119        f = [4.0, 0.0]
120
121        points = [a, b, c, d, e, f]
122
123        #bac, bce, ecf, dbe
124        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
125
126        domain = Domain(points, elements)
127        domain.conserved_quantities = ['stage', 'ymomentum']
128        domain.quantities['stage'] =\
129                                   Quantity(domain, [[1,2,3], [5,5,5],
130                                                     [0,0,9], [-6, 3, 3]])
131
132        domain.quantities['ymomentum'] =\
133                                   Quantity(domain, [[2,3,4], [5,5,5],
134                                                     [0,0,9], [-6, 3, 3]])
135
136        domain.check_integrity()
137
138
139        #Write file
140        filename = 'boundarytest' + str(time.time())
141        fid = open(filename + '.txt', 'w')
142        start = time.mktime(time.strptime('2000', '%Y'))
143        dt = 5*60  #Five minute intervals
144        for i in range(10):
145            t = start + i*dt
146            t_string = time.strftime(time_format, time.gmtime(t))
147
148            fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10)))
149        fid.close()
150
151
152        #Convert ASCII file to NetCDF (Which is what we really like!)
153       
154        from data_manager import timefile2netcdf               
155        timefile2netcdf(filename, quantity_names = ['stage', 'ymomentum'])
156       
157
158
159        F = File_boundary(filename + '.tms', domain)
160
161       
162        os.remove(filename + '.txt')
163        os.remove(filename + '.tms')       
164
165
166        #Check that midpoint coordinates at boundary are correctly computed
167        assert allclose( F.midpoint_coordinates,
168                         [[0.0, 3.0], [1.0, 3.0], [0.0, 1.0],
169                         [1.0, 0.0], [3.0, 0.0], [3.0, 1.0]])
170
171        #assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0])
172        #assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0])
173        #assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0])
174        #assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0])
175        #assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0])
176        #assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0])
177
178
179        #Check time interpolation
180        from config import default_boundary_tag
181        domain.set_boundary( {default_boundary_tag: F} )
182
183        domain.time = 5*30/2  #A quarter way through first step
184        q = F.evaluate()
185        assert allclose(q, [1.0/4, sin(2*pi/10)/4])
186
187
188        domain.time = 2.5*5*60  #Half way between steps 2 and 3
189        q = F.evaluate()
190        assert allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2])
191
192
193
194    def test_fileboundary_exception(self):
195        """Test that boundary object complains if number of
196        conserved quantities are wrong
197        """
198
199        from domain import Domain
200        from quantity import Quantity
201        import time, os
202        from math import sin, pi
203        from config import time_format
204
205        a = [0.0, 0.0]
206        b = [0.0, 2.0]
207        c = [2.0,0.0]
208        d = [0.0, 4.0]
209        e = [2.0, 2.0]
210        f = [4.0,0.0]
211
212        points = [a, b, c, d, e, f]
213
214        #bac, bce, ecf, dbe
215        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
216
217        domain = Domain(points, elements)
218        domain.conserved_quantities = ['stage', 'xmomentum', 'ymomentum']
219        domain.quantities['stage'] =\
220                                   Quantity(domain, [[1,2,3], [5,5,5],
221                                                     [0,0,9], [-6, 3, 3]])
222
223        domain.quantities['xmomentum'] =\
224                                   Quantity(domain, [[2,3,4], [5,5,5],
225                                                     [0,0,9], [-6, 3, 3]])
226        domain.quantities['ymomentum'] =\
227                                   Quantity(domain, [[2,3,4], [5,5,5],
228                                                     [0,0,9], [-6, 3, 3]])
229
230        domain.check_integrity()
231
232        #Write file (with only two values)
233        filename = 'boundarytest' + str(time.time())
234        fid = open(filename + '.txt', 'w')
235        start = time.mktime(time.strptime('2000', '%Y'))
236        dt = 5*60  #Five minute intervals
237        for i in range(10):
238            t = start + i*dt
239            t_string = time.strftime(time_format, time.gmtime(t))
240
241            fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10)))
242        fid.close()
243
244
245        #Convert ASCII file to NetCDF (Which is what we really like!)
246        from data_manager import timefile2netcdf               
247        timefile2netcdf(filename, quantity_names = ['stage', 'xmomentum'])
248
249       
250        try:
251            F = File_boundary(filename + '.tms', domain)           
252        except:
253            pass
254        else:
255            raise 'Should have raised an exception'       
256       
257        os.remove(filename + '.txt')
258        os.remove(filename + '.tms')       
259
260
261
262
263
264#-------------------------------------------------------------
265if __name__ == "__main__":
266    suite = unittest.makeSuite(Test_Generic_Boundary_Conditions,'test')
267    runner = unittest.TextTestRunner()
268    runner.run(suite)
269
270
271
Note: See TracBrowser for help on using the repository browser.