[195] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import unittest |
---|
| 4 | from math import sqrt |
---|
| 5 | |
---|
| 6 | from domain import * |
---|
| 7 | from config import epsilon |
---|
| 8 | from Numeric import allclose, array |
---|
| 9 | from util import distance |
---|
| 10 | |
---|
| 11 | class TestCase(unittest.TestCase): |
---|
| 12 | def setUp(self): |
---|
| 13 | pass |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | def tearDown(self): |
---|
| 17 | pass |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | def test_simple(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] |
---|
| 29 | #bac, bce, ecf, dbe, daf, dae |
---|
| 30 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]] |
---|
| 31 | |
---|
| 32 | conserved_quantities = ['level', 'xmomentum', 'ymomentum'] |
---|
| 33 | other_quantities = ['elevation', 'friction'] |
---|
| 34 | |
---|
| 35 | domain = Domain(points, vertices, None, |
---|
| 36 | conserved_quantities, other_quantities) |
---|
| 37 | domain.check_integrity() |
---|
| 38 | |
---|
| 39 | for name in conserved_quantities + other_quantities: |
---|
| 40 | assert domain.quantities.has_key(name) |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | assert domain.get_conserved_quantities(0, edge=1) == 0. |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | def test_conserved_quantities(self): |
---|
| 47 | |
---|
| 48 | a = [0.0, 0.0] |
---|
| 49 | b = [0.0, 2.0] |
---|
| 50 | c = [2.0,0.0] |
---|
| 51 | d = [0.0, 4.0] |
---|
| 52 | e = [2.0, 2.0] |
---|
| 53 | f = [4.0,0.0] |
---|
| 54 | |
---|
| 55 | points = [a, b, c, d, e, f] |
---|
| 56 | #bac, bce, ecf, dbe, daf, dae |
---|
| 57 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]] |
---|
| 58 | |
---|
| 59 | domain = Domain(points, vertices, boundary=None, |
---|
| 60 | conserved_quantities =\ |
---|
| 61 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 65 | [0,0,9], [-6, 3, 3], |
---|
| 66 | [0,0,0], [0,0,0]]) |
---|
| 67 | |
---|
| 68 | domain.set_quantity('xmomentum', [[1,2,3], [5,5,5], |
---|
| 69 | [0,0,9], [-6, 3, 3], |
---|
| 70 | [0,0,0], [0,0,0]]) |
---|
| 71 | |
---|
| 72 | domain.check_integrity() |
---|
| 73 | |
---|
| 74 | #Centroids |
---|
| 75 | q = domain.get_conserved_quantities(0) |
---|
| 76 | assert allclose(q, [2., 2., 0.]) |
---|
| 77 | |
---|
| 78 | q = domain.get_conserved_quantities(1) |
---|
| 79 | assert allclose(q, [5., 5., 0.]) |
---|
| 80 | |
---|
| 81 | q = domain.get_conserved_quantities(2) |
---|
| 82 | assert allclose(q, [3., 3., 0.]) |
---|
| 83 | |
---|
| 84 | q = domain.get_conserved_quantities(3) |
---|
| 85 | assert allclose(q, [0., 0., 0.]) |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | #Edges |
---|
| 89 | q = domain.get_conserved_quantities(0, edge=0) |
---|
| 90 | assert allclose(q, [2.5, 2.5, 0.]) |
---|
| 91 | q = domain.get_conserved_quantities(0, edge=1) |
---|
| 92 | assert allclose(q, [2., 2., 0.]) |
---|
| 93 | q = domain.get_conserved_quantities(0, edge=2) |
---|
| 94 | assert allclose(q, [1.5, 1.5, 0.]) |
---|
| 95 | |
---|
| 96 | for i in range(3): |
---|
| 97 | q = domain.get_conserved_quantities(1, edge=i) |
---|
| 98 | assert allclose(q, [5, 5, 0.]) |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | q = domain.get_conserved_quantities(2, edge=0) |
---|
| 102 | assert allclose(q, [4.5, 4.5, 0.]) |
---|
| 103 | q = domain.get_conserved_quantities(2, edge=1) |
---|
| 104 | assert allclose(q, [4.5, 4.5, 0.]) |
---|
| 105 | q = domain.get_conserved_quantities(2, edge=2) |
---|
| 106 | assert allclose(q, [0., 0., 0.]) |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | q = domain.get_conserved_quantities(3, edge=0) |
---|
| 110 | assert allclose(q, [3., 3., 0.]) |
---|
| 111 | q = domain.get_conserved_quantities(3, edge=1) |
---|
| 112 | assert allclose(q, [-1.5, -1.5, 0.]) |
---|
| 113 | q = domain.get_conserved_quantities(3, edge=2) |
---|
| 114 | assert allclose(q, [-1.5, -1.5, 0.]) |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | def test_boundary_conditions(self): |
---|
| 119 | |
---|
| 120 | a = [0.0, 0.0] |
---|
| 121 | b = [0.0, 2.0] |
---|
| 122 | c = [2.0,0.0] |
---|
| 123 | d = [0.0, 4.0] |
---|
| 124 | e = [2.0, 2.0] |
---|
| 125 | f = [4.0,0.0] |
---|
| 126 | |
---|
| 127 | points = [a, b, c, d, e, f] |
---|
| 128 | #bac, bce, ecf, dbe |
---|
| 129 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 130 | boundary = { (0, 0): 'First', |
---|
| 131 | (0, 2): 'First', |
---|
| 132 | (2, 0): 'Second', |
---|
| 133 | (2, 1): 'Second', |
---|
| 134 | (3, 1): 'Second', |
---|
| 135 | (3, 2): 'Second'} |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | domain = Domain(points, vertices, boundary, |
---|
| 139 | conserved_quantities =\ |
---|
| 140 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 141 | domain.check_integrity() |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 146 | [0,0,9], [-6, 3, 3]]) |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | domain.set_boundary( {'First': Dirichlet_boundary([5,2,1]), |
---|
| 150 | 'Second': Transmissive_boundary(domain)} ) |
---|
| 151 | |
---|
| 152 | domain.update_boundary() |
---|
| 153 | |
---|
| 154 | assert domain.quantities['level'].boundary_values[0] == 5. #Dirichlet |
---|
| 155 | assert domain.quantities['level'].boundary_values[1] == 5. #Dirichlet |
---|
| 156 | assert domain.quantities['level'].boundary_values[2] ==\ |
---|
| 157 | domain.get_conserved_quantities(2, edge=0)[0] #Transmissive (4.5) |
---|
| 158 | assert domain.quantities['level'].boundary_values[3] ==\ |
---|
| 159 | domain.get_conserved_quantities(2, edge=1)[0] #Transmissive (4.5) |
---|
| 160 | assert domain.quantities['level'].boundary_values[4] ==\ |
---|
| 161 | domain.get_conserved_quantities(3, edge=1)[0] #Transmissive (-1.5) |
---|
| 162 | assert domain.quantities['level'].boundary_values[5] ==\ |
---|
| 163 | domain.get_conserved_quantities(3, edge=2)[0] #Transmissive (-1.5) |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | def test_distribute(self): |
---|
| 168 | """Domain implements a default first order gradient limiter |
---|
| 169 | """ |
---|
| 170 | |
---|
| 171 | a = [0.0, 0.0] |
---|
| 172 | b = [0.0, 2.0] |
---|
| 173 | c = [2.0,0.0] |
---|
| 174 | d = [0.0, 4.0] |
---|
| 175 | e = [2.0, 2.0] |
---|
| 176 | f = [4.0,0.0] |
---|
| 177 | |
---|
| 178 | points = [a, b, c, d, e, f] |
---|
| 179 | #bac, bce, ecf, dbe |
---|
| 180 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 181 | boundary = { (0, 0): 'Third', |
---|
| 182 | (0, 2): 'First', |
---|
| 183 | (2, 0): 'Second', |
---|
| 184 | (2, 1): 'Second', |
---|
| 185 | (3, 1): 'Second', |
---|
| 186 | (3, 2): 'Third'} |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | domain = Domain(points, vertices, boundary, |
---|
| 190 | conserved_quantities =\ |
---|
| 191 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 192 | domain.check_integrity() |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 196 | [0,0,9], [-6, 3, 3]]) |
---|
| 197 | |
---|
| 198 | assert allclose( domain.quantities['level'].centroid_values, |
---|
| 199 | [2,5,3,0] ) |
---|
| 200 | |
---|
| 201 | domain.set_quantity('xmomentum', [[1,1,1], [2,2,2], |
---|
| 202 | [3,3,3], [4, 4, 4]]) |
---|
| 203 | |
---|
| 204 | domain.set_quantity('ymomentum', [[10,10,10], [20,20,20], |
---|
| 205 | [30,30,30], [40, 40, 40]]) |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | #print domain.quantities['level'].centroid_values |
---|
| 209 | domain.distribute_to_vertices_and_edges() |
---|
| 210 | |
---|
| 211 | #First order extrapolation |
---|
| 212 | assert allclose( domain.quantities['level'].vertex_values, |
---|
| 213 | [[ 2., 2., 2.], |
---|
| 214 | [ 5., 5., 5.], |
---|
| 215 | [ 3., 3., 3.], |
---|
| 216 | [ 0., 0., 0.]]) |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | def test_update_conserved_quantities(self): |
---|
| 222 | a = [0.0, 0.0] |
---|
| 223 | b = [0.0, 2.0] |
---|
| 224 | c = [2.0,0.0] |
---|
| 225 | d = [0.0, 4.0] |
---|
| 226 | e = [2.0, 2.0] |
---|
| 227 | f = [4.0,0.0] |
---|
| 228 | |
---|
| 229 | points = [a, b, c, d, e, f] |
---|
| 230 | #bac, bce, ecf, dbe |
---|
| 231 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 232 | boundary = { (0, 0): 'Third', |
---|
| 233 | (0, 2): 'First', |
---|
| 234 | (2, 0): 'Second', |
---|
| 235 | (2, 1): 'Second', |
---|
| 236 | (3, 1): 'Second', |
---|
| 237 | (3, 2): 'Third'} |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | domain = Domain(points, vertices, boundary, |
---|
| 241 | conserved_quantities =\ |
---|
| 242 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 243 | domain.check_integrity() |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 247 | [0,0,9], [-6, 3, 3]]) |
---|
| 248 | |
---|
| 249 | domain.set_quantity('xmomentum', [[1,1,1], [2,2,2], |
---|
| 250 | [3,3,3], [4, 4, 4]]) |
---|
| 251 | |
---|
| 252 | domain.set_quantity('ymomentum', [[10,10,10], [20,20,20], |
---|
| 253 | [30,30,30], [40, 40, 40]]) |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | #Assign some values to update vectors |
---|
| 257 | #domain.explicit_updates[] |
---|
| 258 | |
---|
| 259 | |
---|
| 260 | #------------------------------------------------------------- |
---|
| 261 | if __name__ == "__main__": |
---|
| 262 | suite = unittest.makeSuite(TestCase,'test') |
---|
| 263 | runner = unittest.TextTestRunner() |
---|
| 264 | runner.run(suite) |
---|