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

Last change on this file since 7276 was 7276, checked in by ole, 15 years ago

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

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