source: anuga_core/source/anuga/advection/test_advection.py @ 4972

Last change on this file since 4972 was 4972, checked in by steve, 16 years ago
File size: 4.7 KB
Line 
1import sys
2from os import sep
3
4import unittest
5from math import sqrt, pi
6
7from anuga.config import g, epsilon
8from Numeric import allclose, array, zeros, ones, Float
9from anuga.advection.advection import Domain, Transmissive_boundary, Dirichlet_boundary
10
11class Test_Advection(unittest.TestCase):
12    def setUp(self):
13        pass
14
15    def tearDown(self):
16        pass
17
18    def test_init(self):
19        a = [0.0, 0.0]
20        b = [0.0, 2.0]
21        c = [2.0,0.0]
22        d = [0.0, 4.0]
23        e = [2.0, 2.0]
24        f = [4.0,0.0]
25
26        points = [a, b, c, d, e, f]
27        #bac, bce, ecf, dbe, daf, dae
28        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
29
30        domain = Domain(points, vertices)
31        domain.check_integrity()
32
33        assert domain.quantities.has_key('stage')
34
35        assert domain.get_conserved_quantities(0, edge=1) == 0.
36
37
38    def test_flux_1_triangle0(self):
39        a = [0.0, 0.5]
40        b = [0.0, 0.0]
41        c = [0.5, 0.5]
42
43        points = [a, b, c]
44        vertices = [ [0,1,2] ]
45        domain = Domain(points, vertices)
46        domain.check_integrity()
47
48
49        #Populate boundary array with dirichlet conditions.
50        domain.neighbours = array([[-1,-2,-3]])
51        domain.quantities['stage'].boundary_values[:] = 1.0
52
53        domain.order = 1
54
55        domain.distribute_to_vertices_and_edges() #Use first order default
56
57        domain.check_integrity()
58
59        domain.compute_fluxes()
60        U = -domain.quantities['stage'].explicit_update
61        R = -0.5/domain.areas[0]
62
63        print 'U ', U
64        print 'R ', R
65       
66
67        assert U==R, '%s %s' %(U, R)
68
69
70    def test_flux_1_triangle1(self):
71
72        a = [0.0, 0.5]
73        b = [0.0, 0.0]
74        c = [0.5, 0.5]
75
76        points = [a, b, c]
77        vertices = [ [0,1,2] ]
78        domain = Domain(points, vertices)
79        domain.check_integrity()
80
81        domain.set_quantity('stage', [1.0], location='centroids')
82
83        domain.distribute_to_vertices_and_edges()
84        domain.check_integrity()
85
86
87        domain.compute_fluxes()
88        U = -domain.quantities['stage'].explicit_update
89        R = 0.5/domain.areas[0]
90
91        assert U==R, '%s %s' %(U, R)
92
93
94
95    def test_flux_1_triangle2(self):
96
97        a = [0.0, 0.5]
98        b = [0.0, 0.0]
99        c = [0.5, 0.5]
100
101        points = [a, b, c]
102        vertices = [ [0,1,2] ]
103        domain = Domain(points, vertices)
104        domain.check_integrity()
105
106
107        #Populate boundary array with dirichlet conditions.
108        domain.neighbours = array([[-1,-2,-3]])
109        domain.quantities['stage'].boundary_values[0] = 1.0
110
111        domain.distribute_to_vertices_and_edges() #Use first order default
112
113        domain.check_integrity()
114
115        domain.compute_fluxes()
116        U = domain.quantities['stage'].explicit_update
117        assert allclose(U, 0)
118
119
120
121
122    def test_flux_2_triangles(self):
123        """Flow between two triangles
124        Check that fluxes have opposite signs
125        """
126
127        a = [0.0, 0.5]
128        b = [0.0, 0.0]
129        c = [0.5, 0.5]
130        d = [0.5, 0.0]
131
132        points = [a, b, c, d]
133        vertices = [ [0,1,2], [3,2,1] ]
134        domain = Domain(points, vertices)
135        domain.check_integrity()
136
137
138        #Populate boundary array with dirichlet conditions.
139        domain.neighbours = array([[1,-1,-2], [0,-3,-4]])
140        domain.set_quantity('stage', [1.0, 0.0], location='centroids')
141        domain.distribute_to_vertices_and_edges()
142
143        domain.compute_fluxes()
144
145        X = domain.quantities['stage'].explicit_update
146        assert X[0] == -X[1]
147
148
149    def FIXME_test_advection_example(self):
150        #Test that system can evolve
151
152        from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular
153
154        points, vertices, boundary = rectangular(6, 6)
155
156        #Create advection domain with direction (1,-1)
157        domain = Domain(points, vertices, boundary, velocity=[1.0, -1.0])
158
159        # Initial condition is zero by default
160
161        #Boundaries
162        T = Transmissive_boundary(domain)
163        D = Dirichlet_boundary(array([3.1415]))
164
165        domain.set_boundary( {'left': D, 'right': T, 'bottom': T, 'top': T} )
166        domain.check_integrity()
167
168        #Check that the boundary value gets propagated to all elements
169        for t in domain.evolve(yieldstep = 0.05, finaltime = 10):
170            if allclose(domain.quantities['stage'].centroid_values, 3.1415):
171                break
172
173        assert allclose(domain.quantities['stage'].centroid_values, 3.1415)
174
175
176#-------------------------------------------------------------
177if __name__ == "__main__":
178    suite = unittest.makeSuite(Test_Advection, 'test')
179    runner = unittest.TextTestRunner()
180    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.