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

Last change on this file since 7737 was 7737, checked in by hudson, 14 years ago

Various refactorings, all unit tests pass.
Domain renamed to generic domain.

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