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