source: anuga_core/source/anuga/abstract_2d_finite_volumes/test_generic_boundary_conditions.py @ 5657

Last change on this file since 5657 was 5657, checked in by ole, 16 years ago

Implemented default_boundary option in File_boundary and Field_boundary as
per ticket:293 and added a note in the user manual.

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