source: anuga_core/source_numpy_conversion/anuga/advection/test_advection.py @ 5908

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

NumPy? conversion.

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