[258] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import unittest |
---|
| 4 | from math import sqrt |
---|
| 5 | |
---|
| 6 | from domain import * |
---|
| 7 | from config import epsilon |
---|
[486] | 8 | from Numeric import allclose, array, ones, Float |
---|
[258] | 9 | |
---|
[459] | 10 | |
---|
| 11 | def add_to_verts(tag, elements, domain): |
---|
| 12 | if tag == "mound": |
---|
[469] | 13 | domain.test = "Mound" |
---|
[459] | 14 | |
---|
| 15 | |
---|
[546] | 16 | def set_bottom_friction(tag, elements, domain): |
---|
| 17 | if tag == "bottom": |
---|
| 18 | #print 'bottom - indexes',elements |
---|
| 19 | domain.set_quantity('friction', 0.09, indexes = elements) |
---|
| 20 | |
---|
| 21 | def set_top_friction(tag, elements, domain): |
---|
| 22 | if tag == "top": |
---|
| 23 | #print 'top - indexes',elements |
---|
| 24 | domain.set_quantity('friction', 1., indexes = elements) |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | def set_all_friction(tag, elements, domain): |
---|
| 28 | if tag == "all": |
---|
| 29 | new_values = domain.get_quantity('friction', indexes = elements) + 10.0 |
---|
| 30 | |
---|
| 31 | domain.set_quantity('friction', new_values, indexes = elements) |
---|
| 32 | |
---|
| 33 | |
---|
[258] | 34 | class TestCase(unittest.TestCase): |
---|
| 35 | def setUp(self): |
---|
| 36 | pass |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | def tearDown(self): |
---|
| 40 | pass |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | def test_simple(self): |
---|
| 44 | a = [0.0, 0.0] |
---|
| 45 | b = [0.0, 2.0] |
---|
| 46 | c = [2.0,0.0] |
---|
| 47 | d = [0.0, 4.0] |
---|
| 48 | e = [2.0, 2.0] |
---|
| 49 | f = [4.0,0.0] |
---|
| 50 | |
---|
| 51 | points = [a, b, c, d, e, f] |
---|
| 52 | #bac, bce, ecf, dbe, daf, dae |
---|
| 53 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]] |
---|
| 54 | |
---|
| 55 | conserved_quantities = ['level', 'xmomentum', 'ymomentum'] |
---|
| 56 | other_quantities = ['elevation', 'friction'] |
---|
| 57 | |
---|
| 58 | domain = Domain(points, vertices, None, |
---|
| 59 | conserved_quantities, other_quantities) |
---|
| 60 | domain.check_integrity() |
---|
| 61 | |
---|
| 62 | for name in conserved_quantities + other_quantities: |
---|
| 63 | assert domain.quantities.has_key(name) |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | assert domain.get_conserved_quantities(0, edge=1) == 0. |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | def test_conserved_quantities(self): |
---|
| 70 | |
---|
| 71 | a = [0.0, 0.0] |
---|
| 72 | b = [0.0, 2.0] |
---|
| 73 | c = [2.0,0.0] |
---|
| 74 | d = [0.0, 4.0] |
---|
| 75 | e = [2.0, 2.0] |
---|
| 76 | f = [4.0,0.0] |
---|
| 77 | |
---|
| 78 | points = [a, b, c, d, e, f] |
---|
| 79 | #bac, bce, ecf, dbe, daf, dae |
---|
| 80 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]] |
---|
| 81 | |
---|
| 82 | domain = Domain(points, vertices, boundary=None, |
---|
| 83 | conserved_quantities =\ |
---|
| 84 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 88 | [0,0,9], [-6, 3, 3], |
---|
| 89 | [0,0,0], [0,0,0]]) |
---|
| 90 | |
---|
| 91 | domain.set_quantity('xmomentum', [[1,2,3], [5,5,5], |
---|
| 92 | [0,0,9], [-6, 3, 3], |
---|
| 93 | [0,0,0], [0,0,0]]) |
---|
| 94 | |
---|
| 95 | domain.check_integrity() |
---|
| 96 | |
---|
| 97 | #Centroids |
---|
| 98 | q = domain.get_conserved_quantities(0) |
---|
| 99 | assert allclose(q, [2., 2., 0.]) |
---|
| 100 | |
---|
| 101 | q = domain.get_conserved_quantities(1) |
---|
| 102 | assert allclose(q, [5., 5., 0.]) |
---|
| 103 | |
---|
| 104 | q = domain.get_conserved_quantities(2) |
---|
| 105 | assert allclose(q, [3., 3., 0.]) |
---|
| 106 | |
---|
| 107 | q = domain.get_conserved_quantities(3) |
---|
| 108 | assert allclose(q, [0., 0., 0.]) |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | #Edges |
---|
| 112 | q = domain.get_conserved_quantities(0, edge=0) |
---|
| 113 | assert allclose(q, [2.5, 2.5, 0.]) |
---|
| 114 | q = domain.get_conserved_quantities(0, edge=1) |
---|
| 115 | assert allclose(q, [2., 2., 0.]) |
---|
| 116 | q = domain.get_conserved_quantities(0, edge=2) |
---|
| 117 | assert allclose(q, [1.5, 1.5, 0.]) |
---|
| 118 | |
---|
| 119 | for i in range(3): |
---|
| 120 | q = domain.get_conserved_quantities(1, edge=i) |
---|
| 121 | assert allclose(q, [5, 5, 0.]) |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | q = domain.get_conserved_quantities(2, edge=0) |
---|
| 125 | assert allclose(q, [4.5, 4.5, 0.]) |
---|
| 126 | q = domain.get_conserved_quantities(2, edge=1) |
---|
| 127 | assert allclose(q, [4.5, 4.5, 0.]) |
---|
| 128 | q = domain.get_conserved_quantities(2, edge=2) |
---|
| 129 | assert allclose(q, [0., 0., 0.]) |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | q = domain.get_conserved_quantities(3, edge=0) |
---|
| 133 | assert allclose(q, [3., 3., 0.]) |
---|
| 134 | q = domain.get_conserved_quantities(3, edge=1) |
---|
| 135 | assert allclose(q, [-1.5, -1.5, 0.]) |
---|
| 136 | q = domain.get_conserved_quantities(3, edge=2) |
---|
| 137 | assert allclose(q, [-1.5, -1.5, 0.]) |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | def test_boundary_conditions(self): |
---|
| 142 | |
---|
| 143 | a = [0.0, 0.0] |
---|
| 144 | b = [0.0, 2.0] |
---|
| 145 | c = [2.0,0.0] |
---|
| 146 | d = [0.0, 4.0] |
---|
| 147 | e = [2.0, 2.0] |
---|
| 148 | f = [4.0,0.0] |
---|
| 149 | |
---|
| 150 | points = [a, b, c, d, e, f] |
---|
| 151 | #bac, bce, ecf, dbe |
---|
| 152 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 153 | boundary = { (0, 0): 'First', |
---|
| 154 | (0, 2): 'First', |
---|
| 155 | (2, 0): 'Second', |
---|
| 156 | (2, 1): 'Second', |
---|
| 157 | (3, 1): 'Second', |
---|
| 158 | (3, 2): 'Second'} |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | domain = Domain(points, vertices, boundary, |
---|
| 162 | conserved_quantities =\ |
---|
| 163 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 164 | domain.check_integrity() |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 169 | [0,0,9], [-6, 3, 3]]) |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | domain.set_boundary( {'First': Dirichlet_boundary([5,2,1]), |
---|
| 173 | 'Second': Transmissive_boundary(domain)} ) |
---|
| 174 | |
---|
| 175 | domain.update_boundary() |
---|
| 176 | |
---|
| 177 | assert domain.quantities['level'].boundary_values[0] == 5. #Dirichlet |
---|
| 178 | assert domain.quantities['level'].boundary_values[1] == 5. #Dirichlet |
---|
| 179 | assert domain.quantities['level'].boundary_values[2] ==\ |
---|
| 180 | domain.get_conserved_quantities(2, edge=0)[0] #Transmissive (4.5) |
---|
| 181 | assert domain.quantities['level'].boundary_values[3] ==\ |
---|
| 182 | domain.get_conserved_quantities(2, edge=1)[0] #Transmissive (4.5) |
---|
| 183 | assert domain.quantities['level'].boundary_values[4] ==\ |
---|
| 184 | domain.get_conserved_quantities(3, edge=1)[0] #Transmissive (-1.5) |
---|
| 185 | assert domain.quantities['level'].boundary_values[5] ==\ |
---|
| 186 | domain.get_conserved_quantities(3, edge=2)[0] #Transmissive (-1.5) |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | def test_distribute_first_order(self): |
---|
| 191 | """Domain implements a default first order gradient limiter |
---|
| 192 | """ |
---|
| 193 | |
---|
| 194 | a = [0.0, 0.0] |
---|
| 195 | b = [0.0, 2.0] |
---|
| 196 | c = [2.0,0.0] |
---|
| 197 | d = [0.0, 4.0] |
---|
| 198 | e = [2.0, 2.0] |
---|
| 199 | f = [4.0,0.0] |
---|
| 200 | |
---|
| 201 | points = [a, b, c, d, e, f] |
---|
| 202 | #bac, bce, ecf, dbe |
---|
| 203 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 204 | boundary = { (0, 0): 'Third', |
---|
| 205 | (0, 2): 'First', |
---|
| 206 | (2, 0): 'Second', |
---|
| 207 | (2, 1): 'Second', |
---|
| 208 | (3, 1): 'Second', |
---|
| 209 | (3, 2): 'Third'} |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | domain = Domain(points, vertices, boundary, |
---|
| 213 | conserved_quantities =\ |
---|
| 214 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 215 | domain.check_integrity() |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 219 | [0,0,9], [-6, 3, 3]]) |
---|
| 220 | |
---|
| 221 | assert allclose( domain.quantities['level'].centroid_values, |
---|
| 222 | [2,5,3,0] ) |
---|
| 223 | |
---|
| 224 | domain.set_quantity('xmomentum', [[1,1,1], [2,2,2], |
---|
| 225 | [3,3,3], [4, 4, 4]]) |
---|
| 226 | |
---|
| 227 | domain.set_quantity('ymomentum', [[10,10,10], [20,20,20], |
---|
| 228 | [30,30,30], [40, 40, 40]]) |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | domain.distribute_to_vertices_and_edges() |
---|
| 232 | |
---|
| 233 | #First order extrapolation |
---|
| 234 | assert allclose( domain.quantities['level'].vertex_values, |
---|
| 235 | [[ 2., 2., 2.], |
---|
| 236 | [ 5., 5., 5.], |
---|
| 237 | [ 3., 3., 3.], |
---|
| 238 | [ 0., 0., 0.]]) |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | def test_update_conserved_quantities(self): |
---|
| 244 | a = [0.0, 0.0] |
---|
| 245 | b = [0.0, 2.0] |
---|
| 246 | c = [2.0,0.0] |
---|
| 247 | d = [0.0, 4.0] |
---|
| 248 | e = [2.0, 2.0] |
---|
| 249 | f = [4.0,0.0] |
---|
| 250 | |
---|
| 251 | points = [a, b, c, d, e, f] |
---|
| 252 | #bac, bce, ecf, dbe |
---|
| 253 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 254 | boundary = { (0, 0): 'Third', |
---|
| 255 | (0, 2): 'First', |
---|
| 256 | (2, 0): 'Second', |
---|
| 257 | (2, 1): 'Second', |
---|
| 258 | (3, 1): 'Second', |
---|
| 259 | (3, 2): 'Third'} |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | domain = Domain(points, vertices, boundary, |
---|
| 263 | conserved_quantities =\ |
---|
| 264 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 265 | domain.check_integrity() |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | domain.set_quantity('level', [1,2,3,4], 'centroids') |
---|
| 269 | domain.set_quantity('xmomentum', [1,2,3,4], 'centroids') |
---|
| 270 | domain.set_quantity('ymomentum', [1,2,3,4], 'centroids') |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | #Assign some values to update vectors |
---|
| 274 | #Set explicit_update |
---|
| 275 | |
---|
| 276 | for name in domain.conserved_quantities: |
---|
| 277 | domain.quantities[name].explicit_update = array([4.,3.,2.,1.]) |
---|
| 278 | domain.quantities[name].semi_implicit_update = array([1.,1.,1.,1.]) |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | #Update with given timestep (assuming no other forcing terms) |
---|
| 282 | domain.timestep = 0.1 |
---|
| 283 | domain.update_conserved_quantities() |
---|
| 284 | |
---|
[486] | 285 | sem = array([1.,1.,1.,1.])/array([1, 2, 3, 4]) |
---|
| 286 | denom = ones(4, Float)-domain.timestep*sem |
---|
[258] | 287 | |
---|
[486] | 288 | x = array([1, 2, 3, 4]) + array( [.4,.3,.2,.1] ) |
---|
| 289 | x /= denom |
---|
[258] | 290 | |
---|
[486] | 291 | for name in domain.conserved_quantities: |
---|
| 292 | assert allclose(domain.quantities[name].centroid_values, x) |
---|
[459] | 293 | |
---|
[486] | 294 | |
---|
[546] | 295 | def test_set_region(self): |
---|
[486] | 296 | """Set quantities for sub region |
---|
[459] | 297 | """ |
---|
[258] | 298 | |
---|
[459] | 299 | a = [0.0, 0.0] |
---|
| 300 | b = [0.0, 2.0] |
---|
| 301 | c = [2.0,0.0] |
---|
| 302 | d = [0.0, 4.0] |
---|
| 303 | e = [2.0, 2.0] |
---|
| 304 | f = [4.0,0.0] |
---|
| 305 | |
---|
| 306 | points = [a, b, c, d, e, f] |
---|
| 307 | #bac, bce, ecf, dbe |
---|
| 308 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 309 | boundary = { (0, 0): 'Third', |
---|
| 310 | (0, 2): 'First', |
---|
| 311 | (2, 0): 'Second', |
---|
| 312 | (2, 1): 'Second', |
---|
| 313 | (3, 1): 'Second', |
---|
| 314 | (3, 2): 'Third'} |
---|
| 315 | |
---|
| 316 | domain = Domain(points, vertices, boundary, |
---|
| 317 | conserved_quantities =\ |
---|
| 318 | ['level', 'xmomentum', 'ymomentum']) |
---|
| 319 | domain.check_integrity() |
---|
| 320 | |
---|
| 321 | domain.set_quantity('level', [[1,2,3], [5,5,5], |
---|
| 322 | [0,0,9], [-6, 3, 3]]) |
---|
| 323 | |
---|
| 324 | assert allclose( domain.quantities['level'].centroid_values, |
---|
| 325 | [2,5,3,0] ) |
---|
| 326 | |
---|
| 327 | domain.set_quantity('xmomentum', [[1,1,1], [2,2,2], |
---|
| 328 | [3,3,3], [4, 4, 4]]) |
---|
| 329 | |
---|
| 330 | domain.set_quantity('ymomentum', [[10,10,10], [20,20,20], |
---|
| 331 | [30,30,30], [40, 40, 40]]) |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | domain.distribute_to_vertices_and_edges() |
---|
| 335 | |
---|
| 336 | #First order extrapolation |
---|
| 337 | assert allclose( domain.quantities['level'].vertex_values, |
---|
| 338 | [[ 2., 2., 2.], |
---|
| 339 | [ 5., 5., 5.], |
---|
| 340 | [ 3., 3., 3.], |
---|
| 341 | [ 0., 0., 0.]]) |
---|
| 342 | |
---|
| 343 | domain.build_tagged_elements_dictionary({'mound':[0,1]}) |
---|
| 344 | domain.set_region([add_to_verts]) |
---|
[258] | 345 | |
---|
[486] | 346 | self.failUnless(domain.test == "Mound", |
---|
[469] | 347 | 'set region failed') |
---|
| 348 | |
---|
[546] | 349 | |
---|
| 350 | |
---|
| 351 | def test_region_tags(self): |
---|
| 352 | """ |
---|
| 353 | get values based on triangle lists. |
---|
| 354 | """ |
---|
| 355 | from mesh_factory import rectangular |
---|
| 356 | from shallow_water import Domain |
---|
| 357 | from Numeric import zeros, Float |
---|
| 358 | |
---|
| 359 | #Create basic mesh |
---|
| 360 | points, vertices, boundary = rectangular(1, 3) |
---|
| 361 | |
---|
| 362 | #Create shallow water domain |
---|
| 363 | domain = Domain(points, vertices, boundary) |
---|
| 364 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
| 365 | 'top':[4,5], |
---|
| 366 | 'all':[0,1,2,3,4,5]}) |
---|
| 367 | |
---|
| 368 | |
---|
| 369 | #Set friction |
---|
| 370 | manning = 0.07 |
---|
| 371 | domain.set_quantity('friction', manning) |
---|
| 372 | |
---|
| 373 | domain.set_region([set_bottom_friction, set_top_friction]) |
---|
| 374 | #print domain.quantities['friction'].get_values() |
---|
| 375 | assert allclose(domain.quantities['friction'].get_values(), [[ 0.09, 0.09, 0.09], |
---|
| 376 | [ 0.09, 0.09, 0.09], |
---|
| 377 | [ 0.07, 0.07, 0.07], |
---|
| 378 | [ 0.07, 0.07, 0.07], |
---|
| 379 | [ 1.0, 1.0, 1.0], |
---|
| 380 | [ 1.0, 1.0, 1.0]]) |
---|
| 381 | |
---|
| 382 | domain.set_region([set_all_friction]) |
---|
| 383 | #print domain.quantities['friction'].get_values() |
---|
| 384 | assert allclose(domain.quantities['friction'].get_values(), |
---|
| 385 | [[ 10.09, 10.09, 10.09], |
---|
| 386 | [ 10.09, 10.09, 10.09], |
---|
| 387 | [ 10.07, 10.07, 10.07], |
---|
| 388 | [ 10.07, 10.07, 10.07], |
---|
| 389 | [ 11.0, 11.0, 11.0], |
---|
| 390 | [ 11.0, 11.0, 11.0]]) |
---|
[258] | 391 | #------------------------------------------------------------- |
---|
| 392 | if __name__ == "__main__": |
---|
[486] | 393 | suite = unittest.makeSuite(TestCase,'test') |
---|
[258] | 394 | runner = unittest.TextTestRunner() |
---|
| 395 | runner.run(suite) |
---|