[195] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import unittest |
---|
| 4 | from math import sqrt, pi |
---|
| 5 | |
---|
| 6 | from config import g, epsilon |
---|
| 7 | from Numeric import allclose, array, zeros, ones, Float |
---|
| 8 | from advection import * |
---|
| 9 | |
---|
[1018] | 10 | |
---|
| 11 | class 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. |
---|
[1018] | 50 | domain.neighbours = 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 | |
---|
[773] | 77 | domain.set_quantity('stage', [1.0], '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. |
---|
[1018] | 104 | domain.neighbours = array([[-1,-2,-3]]) |
---|
[773] | 105 | domain.quantities['stage'].boundary_values[0] = 1.0 |
---|
[1018] | 106 | |
---|
[195] | 107 | domain.distribute_to_vertices_and_edges() #Use first order default |
---|
[1018] | 108 | |
---|
[195] | 109 | domain.check_integrity() |
---|
| 110 | |
---|
| 111 | domain.compute_fluxes() |
---|
[773] | 112 | U = domain.quantities['stage'].explicit_update |
---|
[195] | 113 | assert allclose(U, 0) |
---|
| 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. |
---|
[1150] | 135 | domain.neighbours = array([[1,-1,-2], [0,-3,-4]]) |
---|
[773] | 136 | domain.set_quantity('stage', [1.0, 0.0], '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 | |
---|
[195] | 145 | def test_advection_example(self): |
---|
| 146 | #Test that system can evolve |
---|
[1018] | 147 | |
---|
[195] | 148 | from mesh_factory import rectangular |
---|
| 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) |
---|
| 159 | D = Dirichlet_boundary(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): |
---|
[773] | 166 | if allclose(domain.quantities['stage'].centroid_values, 3.1415): |
---|
[195] | 167 | break |
---|
[1018] | 168 | |
---|
[773] | 169 | assert allclose(domain.quantities['stage'].centroid_values, 3.1415) |
---|
[195] | 170 | |
---|
[1018] | 171 | |
---|
[195] | 172 | #------------------------------------------------------------- |
---|
| 173 | if __name__ == "__main__": |
---|
[1018] | 174 | suite = unittest.makeSuite(Test_Advection,'test') |
---|
[195] | 175 | runner = unittest.TextTestRunner() |
---|
| 176 | runner.run(suite) |
---|