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

Last change on this file since 4668 was 3662, checked in by duncan, 18 years ago

fixing import

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