[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 | |
---|
| 10 | |
---|
| 11 | class TestCase(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], [3,0,5], [3,0,4]] |
---|
| 29 | |
---|
| 30 | domain = Domain(points, vertices) |
---|
| 31 | domain.check_integrity() |
---|
| 32 | |
---|
| 33 | assert domain.quantities.has_key('level') |
---|
| 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 | assert allclose(domain.neighbours, [[-1,-2,-3]]) |
---|
| 49 | |
---|
| 50 | #Populate boundary array with dirichlet conditions. |
---|
| 51 | domain.quantities['level'].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['level'].explicit_update |
---|
| 61 | R = -0.5/domain.areas[0] |
---|
| 62 | |
---|
| 63 | assert U==R, '%s %s' %(U, R) |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | def test_flux_1_triangle1(self): |
---|
| 67 | |
---|
| 68 | a = [0.0, 0.5] |
---|
| 69 | b = [0.0, 0.0] |
---|
| 70 | c = [0.5, 0.5] |
---|
| 71 | |
---|
| 72 | points = [a, b, c] |
---|
| 73 | vertices = [ [0,1,2] ] |
---|
| 74 | domain = Domain(points, vertices) |
---|
| 75 | domain.check_integrity() |
---|
| 76 | |
---|
| 77 | domain.set_quantity('level', [1.0], 'centroids') |
---|
| 78 | |
---|
| 79 | domain.distribute_to_vertices_and_edges() |
---|
| 80 | domain.check_integrity() |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | domain.compute_fluxes() |
---|
| 84 | U = -domain.quantities['level'].explicit_update |
---|
| 85 | R = 0.5/domain.areas[0] |
---|
| 86 | |
---|
| 87 | assert U==R, '%s %s' %(U, R) |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | def test_flux_1_triangle2(self): |
---|
| 92 | |
---|
| 93 | a = [0.0, 0.5] |
---|
| 94 | b = [0.0, 0.0] |
---|
| 95 | c = [0.5, 0.5] |
---|
| 96 | |
---|
| 97 | points = [a, b, c] |
---|
| 98 | vertices = [ [0,1,2] ] |
---|
| 99 | domain = Domain(points, vertices) |
---|
| 100 | domain.check_integrity() |
---|
| 101 | |
---|
| 102 | assert allclose(domain.neighbours, [[-1,-2,-3]]) |
---|
| 103 | |
---|
| 104 | #Populate boundary array with dirichlet conditions. |
---|
| 105 | domain.quantities['level'].boundary_values[0] = 1.0 |
---|
| 106 | |
---|
| 107 | domain.distribute_to_vertices_and_edges() #Use first order default |
---|
| 108 | |
---|
| 109 | domain.check_integrity() |
---|
| 110 | |
---|
| 111 | domain.compute_fluxes() |
---|
| 112 | U = domain.quantities['level'].explicit_update |
---|
| 113 | assert allclose(U, 0) |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 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] |
---|
| 124 | b = [0.0, 0.0] |
---|
| 125 | c = [0.5, 0.5] |
---|
| 126 | d = [0.5, 0.0] |
---|
| 127 | |
---|
| 128 | points = [a, b, c, d] |
---|
| 129 | vertices = [ [0,1,2], [3,2,1] ] |
---|
| 130 | domain = Domain(points, vertices) |
---|
| 131 | domain.check_integrity() |
---|
| 132 | |
---|
| 133 | assert allclose(domain.neighbours, [[1,-1,-2], [0,-3,-4]]) |
---|
| 134 | |
---|
| 135 | #Populate boundary array with dirichlet conditions. |
---|
| 136 | |
---|
| 137 | domain.set_quantity('level', [1.0, 0.0], 'centroids') |
---|
| 138 | domain.distribute_to_vertices_and_edges() |
---|
| 139 | |
---|
| 140 | domain.compute_fluxes() |
---|
| 141 | |
---|
| 142 | X = domain.quantities['level'].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['level'].centroid_values, 3.1415): |
---|
| 168 | break |
---|
| 169 | |
---|
| 170 | assert allclose(domain.quantities['level'].centroid_values, 3.1415) |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | #------------------------------------------------------------- |
---|
| 174 | if __name__ == "__main__": |
---|
| 175 | suite = unittest.makeSuite(TestCase,'test') |
---|
| 176 | runner = unittest.TextTestRunner() |
---|
| 177 | runner.run(suite) |
---|