source: anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_generic_boundary_conditions.py @ 5899

Last change on this file since 5899 was 5899, checked in by rwilson, 15 years ago

Initial NumPy? changes (again!).

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