Changeset 1027
- Timestamp:
- Mar 7, 2005, 5:13:39 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/test_pmesh2domain.py
r1018 r1027 37 37 38 38 fileName = tempfile.mktemp(".tsh") 39 print fileName 39 40 file = open(fileName,"w") 40 41 file.write("4 3 # <vertex #> <x> <y> [attributes]\n \ … … 163 164 164 165 165 -
inundation/ga/storm_surge/pyvolution/test_shallow_water.py
r1016 r1027 15 15 Low speeds at center and edges 16 16 """ 17 17 18 18 from math import sqrt, exp, cos, pi 19 19 20 20 x = array(x) 21 y = array(y) 21 y = array(y) 22 22 23 23 N = len(x) 24 24 s = 0*x #New array 25 25 26 26 for k in range(N): 27 27 28 28 r = sqrt(x[k]**2 + y[k]**2) 29 29 … … 49 49 50 50 x = array(x) 51 y = array(y) 51 y = array(y) 52 52 53 53 N = len(x) 54 54 a = 0*x #New array 55 55 56 for k in range(N): 57 r = sqrt(x[k]**2 + y[k]**2) 58 56 for k in range(N): 57 r = sqrt(x[k]**2 + y[k]**2) 58 59 59 angle = atan(y[k]/x[k]) 60 60 … … 64 64 #Take normal direction 65 65 angle -= pi/2 66 67 #Ensure positive radians 66 67 #Ensure positive radians 68 68 if angle < 0: 69 69 angle += 2*pi 70 70 71 71 a[k] = angle/pi*180 72 72 73 73 return a 74 74 75 76 class Test Case(unittest.TestCase):75 76 class Test_Shallow_Water(unittest.TestCase): 77 77 def setUp(self): 78 78 pass 79 79 80 80 def tearDown(self): 81 81 pass 82 82 83 83 def test_rotate(self): 84 normal = array([0.0,-1.0]) 84 normal = array([0.0,-1.0]) 85 85 86 86 q = array([1.0,2.0,3.0]) 87 87 88 88 r = rotate(q, normal, direction = 1) 89 89 assert r[0] == 1 … … 91 91 assert r[2] == 2 92 92 93 w = rotate(r, normal, direction = -1) 93 w = rotate(r, normal, direction = -1) 94 94 assert allclose(w, q) 95 95 … … 114 114 def test_flux_constants(self): 115 115 w = 2.0 116 117 normal = array([1.,0]) 116 117 normal = array([1.,0]) 118 118 ql = array([w, 0, 0]) 119 119 qr = array([w, 0, 0]) 120 120 zl = zr = 0. 121 121 h = w - (zl+zr)/2 122 122 123 123 flux, max_speed = flux_function(normal, ql, qr, zl, zr) 124 124 125 125 assert allclose(flux, [0., 0.5*g*h**2, 0.]) 126 126 assert max_speed == sqrt(g*h) 127 127 128 128 #def test_flux_slope(self): 129 129 # #FIXME: TODO 130 130 # w = 2.0 131 # 132 # normal = array([1.,0]) 131 # 132 # normal = array([1.,0]) 133 133 # ql = array([w, 0, 0]) 134 134 # qr = array([w, 0, 0]) 135 135 # zl = zr = 0. 136 136 # h = w - (zl+zr)/2 137 # 137 # 138 138 # flux, max_speed = flux_function(normal, ql, qr, zl, zr) 139 139 # 140 140 # assert allclose(flux, [0., 0.5*g*h**2, 0.]) 141 141 # assert max_speed == sqrt(g*h) 142 142 143 143 144 144 def test_flux1(self): 145 145 #Use data from previous version of pyvolution 146 normal = array([1.,0]) 146 normal = array([1.,0]) 147 147 ql = array([-0.2, 2, 3]) 148 148 qr = array([-0.2, 2, 3]) … … 156 156 def test_flux2(self): 157 157 #Use data from previous version of pyvolution 158 normal = array([0., -1.]) 158 normal = array([0., -1.]) 159 159 ql = array([-0.075, 2, 3]) 160 160 qr = array([-0.075, 2, 3]) … … 166 166 167 167 def test_flux3(self): 168 #Use data from previous version of pyvolution 169 normal = array([-sqrt(2)/2, sqrt(2)/2]) 168 #Use data from previous version of pyvolution 169 normal = array([-sqrt(2)/2, sqrt(2)/2]) 170 170 ql = array([-0.075, 2, 3]) 171 171 qr = array([-0.075, 2, 3]) … … 177 177 178 178 def test_flux4(self): 179 #Use data from previous version of pyvolution 180 normal = array([-sqrt(2)/2, sqrt(2)/2]) 179 #Use data from previous version of pyvolution 180 normal = array([-sqrt(2)/2, sqrt(2)/2]) 181 181 ql = array([-0.34319278, 0.10254161, 0.07273855]) 182 182 qr = array([-0.30683287, 0.1071986, 0.05930515]) … … 185 185 186 186 assert allclose(flux, [-0.04072676, -0.07096636, -0.01604364]) 187 assert allclose(max_speed, 1.31414103233) 187 assert allclose(max_speed, 1.31414103233) 188 188 189 189 def test_sw_domain_simple(self): … … 196 196 197 197 points = [a, b, c, d, e, f] 198 #bac, bce, ecf, dbe, daf, dae 198 #bac, bce, ecf, dbe, daf, dae 199 199 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]] 200 201 domain = Domain(points, vertices) 200 201 domain = Domain(points, vertices) 202 202 domain.check_integrity() 203 203 … … 207 207 208 208 209 assert domain.get_conserved_quantities(0, edge=1) == 0. 209 assert domain.get_conserved_quantities(0, edge=1) == 0. 210 210 211 211 212 212 def test_boundary_conditions(self): 213 213 214 214 a = [0.0, 0.0] 215 215 b = [0.0, 2.0] … … 227 227 (2, 1): 'Second', 228 228 (3, 1): 'Second', 229 (3, 2): 'Third'} 230 229 (3, 2): 'Third'} 230 231 231 232 232 domain = Domain(points, vertices, boundary) … … 241 241 242 242 domain.set_quantity('ymomentum', [[10,10,10], [20,20,20], 243 [30,30,30], [40, 40, 40]]) 243 [30,30,30], [40, 40, 40]]) 244 244 245 245 … … 248 248 R = Reflective_boundary(domain) 249 249 domain.set_boundary( {'First': D, 'Second': T, 'Third': R}) 250 250 251 251 domain.update_boundary() 252 252 … … 254 254 assert domain.quantities['stage'].boundary_values[0] == 2.5 255 255 assert domain.quantities['stage'].boundary_values[0] ==\ 256 domain.get_conserved_quantities(0, edge=0)[0] #Reflective (2.5) 256 domain.get_conserved_quantities(0, edge=0)[0] #Reflective (2.5) 257 257 assert domain.quantities['stage'].boundary_values[1] == 5. #Dirichlet 258 258 assert domain.quantities['stage'].boundary_values[2] ==\ … … 275 275 domain.get_conserved_quantities(3, edge=1)[1] #Transmissive 276 276 assert domain.quantities['xmomentum'].boundary_values[5] == -4.0 #Reflective 277 277 278 278 #Ymomentum 279 279 assert domain.quantities['ymomentum'].boundary_values[0] == -10.0 #Reflective … … 286 286 287 287 def test_boundary_conditionsII(self): 288 288 289 289 a = [0.0, 0.0] 290 290 b = [0.0, 2.0] … … 303 303 (3, 1): 'Second', 304 304 (3, 2): 'Third', 305 (0, 1): 'Internal'} 306 305 (0, 1): 'Internal'} 306 307 307 308 308 domain = Domain(points, vertices, boundary) … … 317 317 318 318 domain.set_quantity('ymomentum', [[10,10,10], [20,20,20], 319 [30,30,30], [40, 40, 40]]) 319 [30,30,30], [40, 40, 40]]) 320 320 321 321 … … 333 333 #Do a full triangle and check that fluxes cancel out for 334 334 #the constant stage case 335 335 336 336 a = [0.0, 0.0] 337 337 b = [0.0, 2.0] … … 344 344 #bac, bce, ecf, dbe 345 345 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 346 347 domain = Domain(points, vertices) 346 347 domain = Domain(points, vertices) 348 348 domain.set_quantity('stage', [[2,2,2], [2,2,2], 349 349 [2,2,2], [2,2,2]]) … … 351 351 352 352 assert allclose(domain.neighbours, [[-1,1,-1], [2,3,0], [-1,-1,1],[1,-1,-1]]) 353 assert allclose(domain.neighbour_edges, [[-1,2,-1], [2,0,1], [-1,-1,0],[1,-1,-1]]) 353 assert allclose(domain.neighbour_edges, [[-1,2,-1], [2,0,1], [-1,-1,0],[1,-1,-1]]) 354 354 355 355 zl=zr=0. #Assume flat bed 356 356 357 357 #Flux across right edge of volume 1 358 normal = domain.get_normal(1,0) 358 normal = domain.get_normal(1,0) 359 359 ql = domain.get_conserved_quantities(vol_id=1, edge=0) 360 360 qr = domain.get_conserved_quantities(vol_id=2, edge=2) 361 361 flux0, max_speed = flux_function(normal, ql, qr, zl, zr) 362 362 363 363 #Check that flux seen from other triangles is inverse 364 364 tmp = qr; qr=ql; ql=tmp 365 365 normal = domain.get_normal(2,2) 366 flux, max_speed = flux_function(normal, ql, qr, zl, zr) 366 flux, max_speed = flux_function(normal, ql, qr, zl, zr) 367 367 assert allclose(flux + flux0, 0.) 368 368 369 369 #Flux across upper edge of volume 1 370 normal = domain.get_normal(1,1) 370 normal = domain.get_normal(1,1) 371 371 ql = domain.get_conserved_quantities(vol_id=1, edge=1) 372 372 qr = domain.get_conserved_quantities(vol_id=3, edge=0) 373 373 flux1, max_speed = flux_function(normal, ql, qr, zl, zr) 374 374 375 375 #Check that flux seen from other triangles is inverse 376 376 tmp = qr; qr=ql; ql=tmp 377 377 normal = domain.get_normal(3,0) 378 flux, max_speed = flux_function(normal, ql, qr, zl, zr) 378 flux, max_speed = flux_function(normal, ql, qr, zl, zr) 379 379 assert allclose(flux + flux1, 0.) 380 380 381 381 #Flux across lower left hypotenuse of volume 1 382 normal = domain.get_normal(1,2) 382 normal = domain.get_normal(1,2) 383 383 ql = domain.get_conserved_quantities(vol_id=1, edge=2) 384 384 qr = domain.get_conserved_quantities(vol_id=0, edge=1) 385 385 flux2, max_speed = flux_function(normal, ql, qr, zl, zr) 386 386 387 387 #Check that flux seen from other triangles is inverse 388 388 tmp = qr; qr=ql; ql=tmp 389 389 normal = domain.get_normal(0,1) 390 flux, max_speed = flux_function(normal, ql, qr, zl, zr) 390 flux, max_speed = flux_function(normal, ql, qr, zl, zr) 391 391 assert allclose(flux + flux2, 0.) 392 392 … … 396 396 e1 = domain.edgelengths[1, 1] 397 397 e2 = domain.edgelengths[1, 2] 398 398 399 399 assert allclose(e0*flux0+e1*flux1+e2*flux2, 0.) 400 400 … … 410 410 def test_compute_fluxes1(self): 411 411 #Use values from previous version 412 412 413 413 a = [0.0, 0.0] 414 414 b = [0.0, 2.0] … … 421 421 #bac, bce, ecf, dbe 422 422 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 423 423 424 424 domain = Domain(points, vertices) 425 425 val0 = 2.+2.0/3 … … 427 427 val2 = 8.+2.0/3 428 428 val3 = 2.+8.0/3 429 429 430 430 domain.set_quantity('stage', [[val0, val0, val0], [val1, val1, val1], 431 431 [val2, val2, val2], [val3, val3, val3]]) … … 435 435 436 436 #Flux across right edge of volume 1 437 normal = domain.get_normal(1,0) 437 normal = domain.get_normal(1,0) 438 438 ql = domain.get_conserved_quantities(vol_id=1, edge=0) 439 439 qr = domain.get_conserved_quantities(vol_id=2, edge=2) … … 444 444 445 445 #Flux across upper edge of volume 1 446 normal = domain.get_normal(1,1) 446 normal = domain.get_normal(1,1) 447 447 ql = domain.get_conserved_quantities(vol_id=1, edge=1) 448 448 qr = domain.get_conserved_quantities(vol_id=3, edge=0) … … 452 452 453 453 #Flux across lower left hypotenuse of volume 1 454 normal = domain.get_normal(1,2) 454 normal = domain.get_normal(1,2) 455 455 ql = domain.get_conserved_quantities(vol_id=1, edge=2) 456 456 qr = domain.get_conserved_quantities(vol_id=0, edge=1) … … 459 459 assert allclose(flux2, [9.63942522, -61.59685738, -61.59685738]) 460 460 assert allclose(max_speed, 7.22956891292) 461 461 462 462 #Scale, add up and check that compute_fluxes is correct for vol 1 463 463 e0 = domain.edgelengths[1, 0] … … 487 487 [-69.68888889, -166.6, 69.68888889, 0]) 488 488 assert allclose(domain.quantities['ymomentum'].explicit_update, 489 [-69.68888889, -35.93333333, 0., 69.68888889]) 490 491 489 [-69.68888889, -35.93333333, 0., 69.68888889]) 490 491 492 492 #assert allclose(domain.quantities[name].explicit_update 493 493 494 495 494 495 496 496 497 497 498 498 def test_compute_fluxes2(self): 499 499 #Random values, incl momentum 500 500 501 501 a = [0.0, 0.0] 502 502 b = [0.0, 2.0] … … 509 509 #bac, bce, ecf, dbe 510 510 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 511 511 512 512 domain = Domain(points, vertices) 513 513 val0 = 2.+2.0/3 … … 519 519 520 520 domain.set_quantity('elevation', zl*ones( (4,3) )) 521 522 521 522 523 523 domain.set_quantity('stage', [[val0, val0-1, val0-2], 524 524 [val1, val1+1, val1], … … 532 532 domain.set_quantity('ymomentum', 533 533 [[1, -1, 0], [0, -3, 2], 534 [0, 1, 0], [-1, 2, 2]]) 535 536 534 [0, 1, 0], [-1, 2, 2]]) 535 536 537 537 domain.check_integrity() 538 538 … … 540 540 541 541 #Flux across right edge of volume 1 542 normal = domain.get_normal(1,0) 542 normal = domain.get_normal(1,0) 543 543 ql = domain.get_conserved_quantities(vol_id=1, edge=0) 544 544 qr = domain.get_conserved_quantities(vol_id=2, edge=2) … … 546 546 547 547 #Flux across upper edge of volume 1 548 normal = domain.get_normal(1,1) 548 normal = domain.get_normal(1,1) 549 549 ql = domain.get_conserved_quantities(vol_id=1, edge=1) 550 550 qr = domain.get_conserved_quantities(vol_id=3, edge=0) … … 552 552 553 553 #Flux across lower left hypotenuse of volume 1 554 normal = domain.get_normal(1,2) 554 normal = domain.get_normal(1,2) 555 555 ql = domain.get_conserved_quantities(vol_id=1, edge=2) 556 556 qr = domain.get_conserved_quantities(vol_id=0, edge=1) 557 557 flux2, max_speed = flux_function(normal, ql, qr, zl, zr) 558 558 559 559 #Scale, add up and check that compute_fluxes is correct for vol 1 560 560 e0 = domain.edgelengths[1, 0] … … 564 564 total_flux = -(e0*flux0+e1*flux1+e2*flux2)/domain.areas[1] 565 565 566 566 567 567 domain.compute_fluxes() 568 568 for i, name in enumerate(['stage', 'xmomentum', 'ymomentum']): 569 569 assert allclose(total_flux[i], 570 domain.quantities[name].explicit_update[1]) 570 domain.quantities[name].explicit_update[1]) 571 571 #assert allclose(total_flux, domain.explicit_update[1,:]) 572 572 573 573 574 574 def test_compute_fluxes3(self): 575 575 #Random values, incl momentum 576 576 577 577 a = [0.0, 0.0] 578 578 b = [0.0, 2.0] … … 585 585 #bac, bce, ecf, dbe 586 586 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 587 587 588 588 domain = Domain(points, vertices) 589 589 val0 = 2.+2.0/3 … … 594 594 zl=zr=-3.75 #Assume constant bed (must be less than stage) 595 595 domain.set_quantity('elevation', zl*ones( (4,3) )) 596 597 596 597 598 598 domain.set_quantity('stage', [[val0, val0-1, val0-2], 599 599 [val1, val1+1, val1], … … 607 607 domain.set_quantity('ymomentum', 608 608 [[1, -1, 0], [0, -3, 2], 609 [0, 1, 0], [-1, 2, 2]]) 610 611 609 [0, 1, 0], [-1, 2, 2]]) 610 611 612 612 domain.check_integrity() 613 613 … … 615 615 616 616 #Flux across right edge of volume 1 617 normal = domain.get_normal(1,0) 617 normal = domain.get_normal(1,0) 618 618 ql = domain.get_conserved_quantities(vol_id=1, edge=0) 619 619 qr = domain.get_conserved_quantities(vol_id=2, edge=2) … … 621 621 622 622 #Flux across upper edge of volume 1 623 normal = domain.get_normal(1,1) 623 normal = domain.get_normal(1,1) 624 624 ql = domain.get_conserved_quantities(vol_id=1, edge=1) 625 625 qr = domain.get_conserved_quantities(vol_id=3, edge=0) … … 627 627 628 628 #Flux across lower left hypotenuse of volume 1 629 normal = domain.get_normal(1,2) 629 normal = domain.get_normal(1,2) 630 630 ql = domain.get_conserved_quantities(vol_id=1, edge=2) 631 631 qr = domain.get_conserved_quantities(vol_id=0, edge=1) 632 632 flux2, max_speed = flux_function(normal, ql, qr, zl, zr) 633 633 634 634 #Scale, add up and check that compute_fluxes is correct for vol 1 635 635 e0 = domain.edgelengths[1, 0] … … 638 638 639 639 total_flux = -(e0*flux0+e1*flux1+e2*flux2)/domain.areas[1] 640 640 641 641 domain.compute_fluxes() 642 642 for i, name in enumerate(['stage', 'xmomentum', 'ymomentum']): … … 647 647 648 648 def test_catching_negative_heights(self): 649 649 650 650 a = [0.0, 0.0] 651 651 b = [0.0, 2.0] … … 658 658 #bac, bce, ecf, dbe 659 659 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 660 660 661 661 domain = Domain(points, vertices) 662 662 val0 = 2.+2.0/3 … … 681 681 682 682 683 ##################################################### 683 ##################################################### 684 684 def test_initial_condition(self): 685 685 """Test that initial condition is output at time == 0 … … 688 688 from config import g 689 689 import copy 690 690 691 691 a = [0.0, 0.0] 692 692 b = [0.0, 2.0] … … 699 699 #bac, bce, ecf, dbe 700 700 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 701 701 702 702 domain = Domain(points, vertices) 703 703 704 #Set up for a gradient of (3,0) at mid triangle 704 #Set up for a gradient of (3,0) at mid triangle 705 705 def slope(x, y): 706 706 return 3*x … … 725 725 else: 726 726 assert not allclose(stage, initial_stage) 727 728 729 730 731 ##################################################### 727 728 729 730 731 ##################################################### 732 732 def test_gravity(self): 733 733 #Assuming no friction 734 734 735 735 from config import g 736 736 737 737 a = [0.0, 0.0] 738 738 b = [0.0, 2.0] … … 745 745 #bac, bce, ecf, dbe 746 746 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 747 747 748 748 domain = Domain(points, vertices) 749 749 750 #Set up for a gradient of (3,0) at mid triangle 750 #Set up for a gradient of (3,0) at mid triangle 751 751 def slope(x, y): 752 752 return 3*x … … 762 762 assert allclose(domain.quantities[name].explicit_update, 0) 763 763 assert allclose(domain.quantities[name].semi_implicit_update, 0) 764 765 domain.compute_forcing_terms() 764 765 domain.compute_forcing_terms() 766 766 767 767 assert allclose(domain.quantities['stage'].explicit_update, 0) 768 768 assert allclose(domain.quantities['xmomentum'].explicit_update, -g*h*3) 769 769 assert allclose(domain.quantities['ymomentum'].explicit_update, 0) 770 771 770 771 772 772 def test_manning_friction(self): 773 773 from config import g 774 774 775 775 a = [0.0, 0.0] 776 776 b = [0.0, 2.0] … … 783 783 #bac, bce, ecf, dbe 784 784 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 785 785 786 786 domain = Domain(points, vertices) 787 787 788 #Set up for a gradient of (3,0) at mid triangle 788 #Set up for a gradient of (3,0) at mid triangle 789 789 def slope(x, y): 790 790 return 3*x … … 797 797 domain.set_quantity('elevation', slope) 798 798 domain.set_quantity('stage', stage) 799 domain.set_quantity('friction', eta) 799 domain.set_quantity('friction', eta) 800 800 801 801 for name in domain.conserved_quantities: 802 802 assert allclose(domain.quantities[name].explicit_update, 0) 803 803 assert allclose(domain.quantities[name].semi_implicit_update, 0) 804 805 domain.compute_forcing_terms() 804 805 domain.compute_forcing_terms() 806 806 807 807 assert allclose(domain.quantities['stage'].explicit_update, 0) … … 811 811 assert allclose(domain.quantities['stage'].semi_implicit_update, 0) 812 812 assert allclose(domain.quantities['xmomentum'].semi_implicit_update, 0) 813 assert allclose(domain.quantities['ymomentum'].semi_implicit_update, 0) 813 assert allclose(domain.quantities['ymomentum'].semi_implicit_update, 0) 814 814 815 815 #Create some momentum for friction to work with … … 817 817 S = -g * eta**2 / h**(7.0/3) 818 818 819 domain.compute_forcing_terms() 819 domain.compute_forcing_terms() 820 820 assert allclose(domain.quantities['stage'].semi_implicit_update, 0) 821 821 assert allclose(domain.quantities['xmomentum'].semi_implicit_update, S) 822 assert allclose(domain.quantities['ymomentum'].semi_implicit_update, 0) 823 824 #A more complex example 822 assert allclose(domain.quantities['ymomentum'].semi_implicit_update, 0) 823 824 #A more complex example 825 825 domain.quantities['stage'].semi_implicit_update[:] = 0.0 826 826 domain.quantities['xmomentum'].semi_implicit_update[:] = 0.0 827 domain.quantities['ymomentum'].semi_implicit_update[:] = 0.0 828 827 domain.quantities['ymomentum'].semi_implicit_update[:] = 0.0 828 829 829 domain.set_quantity('xmomentum', 3) 830 domain.set_quantity('ymomentum', 4) 830 domain.set_quantity('ymomentum', 4) 831 831 832 832 S = -g * eta**2 * 5 / h**(7.0/3) … … 837 837 assert allclose(domain.quantities['stage'].semi_implicit_update, 0) 838 838 assert allclose(domain.quantities['xmomentum'].semi_implicit_update, 3*S) 839 assert allclose(domain.quantities['ymomentum'].semi_implicit_update, 4*S) 839 assert allclose(domain.quantities['ymomentum'].semi_implicit_update, 4*S) 840 840 841 841 def test_constant_wind_stress(self): … … 854 854 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 855 855 856 856 857 857 domain = Domain(points, vertices) 858 858 … … 860 860 domain.set_quantity('elevation', 0) 861 861 domain.set_quantity('stage', 1.0) 862 domain.set_quantity('friction', 0) 862 domain.set_quantity('friction', 0) 863 863 864 864 Br = Reflective_boundary(domain) … … 870 870 domain.forcing_terms = [] 871 871 domain.forcing_terms.append( Wind_stress(s, phi) ) 872 872 873 873 domain.compute_forcing_terms() 874 874 875 875 876 876 const = eta_w*rho_a/rho_w 877 877 878 878 #Convert to radians 879 879 phi = phi*pi/180 880 880 881 881 #Compute velocity vector (u, v) 882 882 u = s*cos(phi) … … 911 911 domain.set_quantity('elevation', 0) 912 912 domain.set_quantity('stage', 1.0) 913 domain.set_quantity('friction', 0) 913 domain.set_quantity('friction', 0) 914 914 915 915 Br = Reflective_boundary(domain) … … 924 924 domain.forcing_terms = [] 925 925 domain.forcing_terms.append( Wind_stress(s = speed, phi = angle) ) 926 926 927 927 domain.compute_forcing_terms() 928 928 929 929 #Compute reference solution 930 930 const = eta_w*rho_a/rho_w 931 931 932 932 N = domain.number_of_elements 933 933 … … 939 939 s_vec = speed(t,x,y) 940 940 phi_vec = angle(t,x,y) 941 941 942 942 943 943 for k in range(N): … … 945 945 phi = phi_vec[k]*pi/180 946 946 s = s_vec[k] 947 947 948 948 #Compute velocity vector (u, v) 949 949 u = s*cos(phi) … … 966 966 from util import file_function 967 967 import time 968 968 969 969 970 970 a = [0.0, 0.0] … … 984 984 domain.set_quantity('elevation', 0) 985 985 domain.set_quantity('stage', 1.0) 986 domain.set_quantity('friction', 0) 986 domain.set_quantity('friction', 0) 987 987 988 988 Br = Reflective_boundary(domain) … … 995 995 #Take x=1 and y=0 996 996 filename = 'test_windstress_from_file.txt' 997 start = time.mktime(time.strptime('2000', '%Y')) 997 start = time.mktime(time.strptime('2000', '%Y')) 998 998 fid = open(filename, 'w') 999 999 dt = 1 #One second interval … … 1006 1006 angle(t,[1],[0])[0])) 1007 1007 t += dt 1008 1008 1009 1009 fid.close() 1010 1010 1011 1011 #Setup wind stress 1012 1012 F = file_function(filename) 1013 W = Wind_stress(F) 1013 W = Wind_stress(F) 1014 1014 domain.forcing_terms = [] 1015 1015 domain.forcing_terms.append(W) 1016 1016 1017 1017 domain.compute_forcing_terms() 1018 1018 1019 1019 #Compute reference solution 1020 1020 const = eta_w*rho_a/rho_w 1021 1021 1022 1022 N = domain.number_of_elements 1023 1023 … … 1026 1026 s = speed(t,[1],[0])[0] 1027 1027 phi = angle(t,[1],[0])[0] 1028 1028 1029 1029 #Convert to radians 1030 1030 phi = phi*pi/180 1031 1031 1032 1032 1033 1033 #Compute velocity vector (u, v) 1034 1034 u = s*cos(phi) … … 1049 1049 are wrong - e.g. returns a scalar 1050 1050 """ 1051 1051 1052 1052 from config import rho_a, rho_w, eta_w 1053 1053 from math import pi, cos, sin, sqrt … … 1069 1069 domain.set_quantity('elevation', 0) 1070 1070 domain.set_quantity('stage', 1.0) 1071 domain.set_quantity('friction', 0) 1071 domain.set_quantity('friction', 0) 1072 1072 1073 1073 Br = Reflective_boundary(domain) … … 1088 1088 msg = 'Should have raised exception' 1089 1089 raise msg 1090 1090 1091 1091 1092 1092 try: … … 1098 1098 msg = 'Should have raised exception' 1099 1099 raise msg 1100 1100 1101 1101 try: 1102 1102 domain.forcing_terms.append(Wind_stress(s = speed, … … 1107 1107 msg = 'Should have raised exception' 1108 1108 raise msg 1109 1109 1110 1110 1111 1111 ##################################################### 1112 1112 def test_first_order_extrapolator_const_z(self): 1113 1113 1114 1114 a = [0.0, 0.0] 1115 1115 b = [0.0, 2.0] … … 1122 1122 #bac, bce, ecf, dbe 1123 1123 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1124 1124 1125 1125 domain = Domain(points, vertices) 1126 1126 val0 = 2.+2.0/3 … … 1129 1129 val3 = 2.+8.0/3 1130 1130 1131 zl=zr=-3.75 #Assume constant bed (must be less than stage) 1131 zl=zr=-3.75 #Assume constant bed (must be less than stage) 1132 1132 domain.set_quantity('elevation', zl*ones( (4,3) )) 1133 1133 domain.set_quantity('stage', [[val0, val0-1, val0-2], … … 1143 1143 #Check that centroid values were distributed to vertices 1144 1144 C = domain.quantities['stage'].centroid_values 1145 for i in range(3): 1145 for i in range(3): 1146 1146 assert allclose( domain.quantities['stage'].vertex_values[:,i], C) 1147 1147 1148 1148 1149 1149 def test_first_order_limiter_variable_z(self): … … 1151 1151 from Numeric import alltrue, greater_equal 1152 1152 from config import epsilon 1153 1153 1154 1154 a = [0.0, 0.0] 1155 1155 b = [0.0, 2.0] … … 1162 1162 #bac, bce, ecf, dbe 1163 1163 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1164 1164 1165 1165 domain = Domain(points, vertices) 1166 1166 val0 = 2.+2.0/3 … … 1177 1177 1178 1178 E = domain.quantities['elevation'].vertex_values 1179 L = domain.quantities['stage'].vertex_values 1180 1181 1179 L = domain.quantities['stage'].vertex_values 1180 1181 1182 1182 #Check that some stages are not above elevation (within eps) 1183 1183 #- so that the limiter has something to work with 1184 assert not alltrue(alltrue(greater_equal(L,E-epsilon))) 1184 assert not alltrue(alltrue(greater_equal(L,E-epsilon))) 1185 1185 1186 1186 domain.order = 1 1187 domain.distribute_to_vertices_and_edges() 1187 domain.distribute_to_vertices_and_edges() 1188 1188 1189 1189 #Check that all stages are above elevation (within eps) … … 1191 1191 1192 1192 1193 ##################################################### 1193 ##################################################### 1194 1194 def test_distribute_basic(self): 1195 #Using test data generated by pyvolution-2 1195 #Using test data generated by pyvolution-2 1196 1196 #Assuming no friction and flat bed (0.0) 1197 1197 1198 1198 a = [0.0, 0.0] 1199 1199 b = [0.0, 2.0] … … 1206 1206 #bac, bce, ecf, dbe 1207 1207 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1208 1208 1209 1209 domain = Domain(points, vertices) 1210 1210 1211 1211 val0 = 2. 1212 1212 val1 = 4. … … 1216 1216 domain.set_quantity('stage', [val0, val1, val2, val3], 'centroids') 1217 1217 L = domain.quantities['stage'].vertex_values 1218 1218 1219 1219 #First order 1220 domain.order = 1 1220 domain.order = 1 1221 1221 domain.distribute_to_vertices_and_edges() 1222 1222 assert allclose(L[1], val1) 1223 1223 1224 1224 #Second order 1225 domain.order = 2 1225 domain.order = 2 1226 1226 domain.distribute_to_vertices_and_edges() 1227 1227 assert allclose(L[1], [2.2, 4.9, 4.9]) 1228 1229 1230 1231 def test_distribute_away_from_bed(self): 1232 #Using test data generated by pyvolution-2 1228 1229 1230 1231 def test_distribute_away_from_bed(self): 1232 #Using test data generated by pyvolution-2 1233 1233 #Assuming no friction and flat bed (0.0) 1234 1234 1235 1235 a = [0.0, 0.0] 1236 1236 b = [0.0, 2.0] … … 1243 1243 #bac, bce, ecf, dbe 1244 1244 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1245 1245 1246 1246 domain = Domain(points, vertices) 1247 L = domain.quantities['stage'].vertex_values 1248 1247 L = domain.quantities['stage'].vertex_values 1248 1249 1249 def stage(x,y): 1250 1250 return x**2 1251 1251 1252 1252 domain.set_quantity('stage', stage, 'centroids') 1253 1254 a, b = domain.quantities['stage'].compute_gradients() 1253 1254 a, b = domain.quantities['stage'].compute_gradients() 1255 1255 assert allclose(a[1], 3.33333334) 1256 1256 assert allclose(b[1], 0.0) 1257 1257 1258 1258 domain.order = 1 1259 1259 domain.distribute_to_vertices_and_edges() 1260 1260 assert allclose(L[1], 1.77777778) 1261 1261 1262 1262 domain.order = 2 1263 1263 domain.distribute_to_vertices_and_edges() 1264 assert allclose(L[1], [0.57777777, 2.37777778, 2.37777778]) 1265 1266 1267 1268 def test_distribute_away_from_bed1(self): 1269 #Using test data generated by pyvolution-2 1264 assert allclose(L[1], [0.57777777, 2.37777778, 2.37777778]) 1265 1266 1267 1268 def test_distribute_away_from_bed1(self): 1269 #Using test data generated by pyvolution-2 1270 1270 #Assuming no friction and flat bed (0.0) 1271 1271 1272 1272 a = [0.0, 0.0] 1273 1273 b = [0.0, 2.0] … … 1280 1280 #bac, bce, ecf, dbe 1281 1281 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1282 1282 1283 1283 domain = Domain(points, vertices) 1284 L = domain.quantities['stage'].vertex_values 1285 1284 L = domain.quantities['stage'].vertex_values 1285 1286 1286 def stage(x,y): 1287 1287 return x**4+y**2 1288 1288 1289 1289 domain.set_quantity('stage', stage, 'centroids') 1290 #print domain.quantities['stage'].centroid_values 1291 1292 a, b = domain.quantities['stage'].compute_gradients() 1290 #print domain.quantities['stage'].centroid_values 1291 1292 a, b = domain.quantities['stage'].compute_gradients() 1293 1293 assert allclose(a[1], 25.18518519) 1294 1294 assert allclose(b[1], 3.33333333) 1295 1295 1296 1296 domain.order = 1 1297 1297 domain.distribute_to_vertices_and_edges() 1298 1298 assert allclose(L[1], 4.9382716) 1299 1299 1300 1300 domain.order = 2 1301 1301 domain.distribute_to_vertices_and_edges() 1302 assert allclose(L[1], [1.07160494, 6.46058131, 7.28262855]) 1303 1304 1305 1302 assert allclose(L[1], [1.07160494, 6.46058131, 7.28262855]) 1303 1304 1305 1306 1306 def test_distribute_near_bed(self): 1307 #Using test data generated by pyvolution-2 1307 #Using test data generated by pyvolution-2 1308 1308 #Assuming no friction and flat bed (0.0) 1309 1309 … … 1318 1318 #bac, bce, ecf, dbe 1319 1319 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1320 1320 1321 1321 domain = Domain(points, vertices) 1322 1322 1323 1323 1324 #Set up for a gradient of (3,0) at mid triangle 1324 #Set up for a gradient of (3,0) at mid triangle 1325 1325 def slope(x, y): 1326 1326 return 10*x … … 1333 1333 domain.set_quantity('stage', stage, 'centroids') 1334 1334 1335 #print domain.quantities['elevation'].centroid_values 1335 #print domain.quantities['elevation'].centroid_values 1336 1336 #print domain.quantities['stage'].centroid_values 1337 1337 1338 1338 E = domain.quantities['elevation'].vertex_values 1339 1339 L = domain.quantities['stage'].vertex_values 1340 1340 1341 1341 #print E 1342 domain.order = 1 1342 domain.order = 1 1343 1343 domain.distribute_to_vertices_and_edges() 1344 1344 ##assert allclose(L[1], [0.19999999, 20.05, 20.05]) 1345 1345 assert allclose(L[1], [0.1, 20.1, 20.1]) 1346 1347 domain.order = 2 1346 1347 domain.order = 2 1348 1348 domain.distribute_to_vertices_and_edges() 1349 1349 assert allclose(L[1], [0.1, 20.1, 20.1]) 1350 1350 1351 1351 def test_distribute_near_bed1(self): 1352 #Using test data generated by pyvolution-2 1352 #Using test data generated by pyvolution-2 1353 1353 #Assuming no friction and flat bed (0.0) 1354 1354 … … 1363 1363 #bac, bce, ecf, dbe 1364 1364 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1365 1365 1366 1366 domain = Domain(points, vertices) 1367 1367 1368 1368 1369 #Set up for a gradient of (3,0) at mid triangle 1369 #Set up for a gradient of (3,0) at mid triangle 1370 1370 def slope(x, y): 1371 1371 return x**4+y**2 … … 1378 1378 domain.set_quantity('stage', stage) 1379 1379 1380 #print domain.quantities['elevation'].centroid_values 1380 #print domain.quantities['elevation'].centroid_values 1381 1381 #print domain.quantities['stage'].centroid_values 1382 1382 1383 1383 E = domain.quantities['elevation'].vertex_values 1384 1384 L = domain.quantities['stage'].vertex_values … … 1388 1388 domain.distribute_to_vertices_and_edges() 1389 1389 ##assert allclose(L[1], [4.19999999, 16.07142857, 20.02857143]) 1390 assert allclose(L[1], [4.1, 16.1, 20.1]) 1391 1392 domain.order = 2 1390 assert allclose(L[1], [4.1, 16.1, 20.1]) 1391 1392 domain.order = 2 1393 1393 domain.distribute_to_vertices_and_edges() 1394 1394 assert allclose(L[1], [4.1, 16.1, 20.1]) 1395 1395 1396 1396 1397 1397 1398 1398 def test_second_order_distribute_real_data(self): 1399 #Using test data generated by pyvolution-2 1399 #Using test data generated by pyvolution-2 1400 1400 #Assuming no friction and flat bed (0.0) 1401 1401 … … 1411 1411 #bae, efb, cbf, feg 1412 1412 vertices = [ [1,0,4], [4,5,1], [2,1,5], [5,4,6]] 1413 1413 1414 1414 domain = Domain(points, vertices) 1415 1415 … … 1418 1418 1419 1419 domain.set_quantity('elevation', slope) 1420 domain.set_quantity('stage', 1421 [0.01298164, 0.00365611, 0.01440365, -0.0381856437096], 1420 domain.set_quantity('stage', 1421 [0.01298164, 0.00365611, 0.01440365, -0.0381856437096], 1422 1422 'centroids') 1423 domain.set_quantity('xmomentum', 1423 domain.set_quantity('xmomentum', 1424 1424 [0.00670439, 0.01263789, 0.00647805, 0.0178180740668], 1425 1425 'centroids') 1426 domain.set_quantity('ymomentum', 1426 domain.set_quantity('ymomentum', 1427 1427 [-7.23510980e-004, -6.30413883e-005, 6.30413883e-005, 0.000200907255866], 1428 'centroids') 1429 1428 'centroids') 1429 1430 1430 E = domain.quantities['elevation'].vertex_values 1431 1431 L = domain.quantities['stage'].vertex_values 1432 X = domain.quantities['xmomentum'].vertex_values 1433 Y = domain.quantities['ymomentum'].vertex_values 1432 X = domain.quantities['xmomentum'].vertex_values 1433 Y = domain.quantities['ymomentum'].vertex_values 1434 1434 1435 1435 #print E 1436 1436 domain.order = 2 1437 domain.beta_h = 0.0 #Use first order in h-limiter 1437 domain.beta_h = 0.0 #Use first order in h-limiter 1438 1438 domain.distribute_to_vertices_and_edges() 1439 1439 … … 1446 1446 assert allclose(Y[1,:], [-0.000117062180693, 7.94434448109e-005, -0.000151505429018]) 1447 1447 1448 1449 1448 1449 1450 1450 def test_balance_deep_and_shallow(self): 1451 1451 """Test that balanced limiters preserve conserved quantites. 1452 1452 """ 1453 1453 import copy 1454 1454 1455 1455 a = [0.0, 0.0] 1456 1456 b = [0.0, 2.0] … … 1465 1465 elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] 1466 1466 1467 mesh = Domain(points, elements) 1467 mesh = Domain(points, elements) 1468 1468 mesh.check_integrity() 1469 1469 … … 1471 1471 mesh.set_quantity('stage', [[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1472 1472 mesh.set_quantity('elevation', 0) #Flat bed 1473 stage = mesh.quantities['stage'] 1474 1473 stage = mesh.quantities['stage'] 1474 1475 1475 ref_centroid_values = copy.copy(stage.centroid_values[:]) #Copy 1476 1476 1477 1477 #Limit 1478 1478 mesh.distribute_to_vertices_and_edges() … … 1483 1483 assert allclose (ref_centroid_values[k], 1484 1484 sum(stage.vertex_values[k,:])/3) 1485 1485 1486 1486 1487 1487 #Now try with a non-flat bed - closely hugging initial stage in places 1488 1488 #This will create alphas in the range [0, 0.478260, 1] 1489 1489 mesh.set_quantity('stage', [[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1490 mesh.set_quantity('elevation', [[0,0,0], 1491 [1.8,1.9,5.9], 1492 [4.6,0,0], 1490 mesh.set_quantity('elevation', [[0,0,0], 1491 [1.8,1.9,5.9], 1492 [4.6,0,0], 1493 1493 [0,2,4]]) 1494 stage = mesh.quantities['stage'] 1495 1494 stage = mesh.quantities['stage'] 1495 1496 1496 ref_centroid_values = copy.copy(stage.centroid_values[:]) #Copy 1497 ref_vertex_values = copy.copy(stage.vertex_values[:]) #Copy 1498 1497 ref_vertex_values = copy.copy(stage.vertex_values[:]) #Copy 1498 1499 1499 #Limit 1500 1500 mesh.distribute_to_vertices_and_edges() 1501 1501 1502 1502 1503 1503 #Assert that all vertex quantities have changed 1504 1504 for k in range(mesh.number_of_elements): 1505 #print ref_vertex_values[k,:], stage.vertex_values[k,:] 1506 assert not allclose (ref_vertex_values[k,:], stage.vertex_values[k,:]) 1505 #print ref_vertex_values[k,:], stage.vertex_values[k,:] 1506 assert not allclose (ref_vertex_values[k,:], stage.vertex_values[k,:]) 1507 1507 #and assert that quantities are still conserved 1508 1508 from Numeric import sum … … 1510 1510 assert allclose (ref_centroid_values[k], 1511 1511 sum(stage.vertex_values[k,:])/3) 1512 1513 1512 1513 1514 1514 #Also check that Python and C version produce the same 1515 1515 assert allclose (stage.vertex_values, … … 1518 1518 [6.93333333, 4.53333333, 4.53333333], 1519 1519 [5.33333333, 5.33333333, 5.33333333]]) 1520 1521 1522 1523 1520 1521 1522 1523 1524 1524 def test_conservation_1(self): 1525 1525 """Test that stage is conserved globally 1526 1527 This one uses a flat bed, reflective bdries and a suitable 1526 1527 This one uses a flat bed, reflective bdries and a suitable 1528 1528 initial condition 1529 1529 """ … … 1546 1546 1547 1547 domain.set_quantity('elevation', 0) 1548 domain.set_quantity('friction', 0) 1549 domain.set_quantity('stage', x_slope) 1550 1548 domain.set_quantity('friction', 0) 1549 domain.set_quantity('stage', x_slope) 1550 1551 1551 # Boundary conditions (reflective everywhere) 1552 1552 Br = Reflective_boundary(domain) … … 1555 1555 domain.check_integrity() 1556 1556 1557 #domain.visualise = True #If you want to take a sticky beak 1558 1559 initial_volume = domain.quantities['stage'].get_integral() 1560 initial_xmom = domain.quantities['xmomentum'].get_integral() 1561 1557 #domain.visualise = True #If you want to take a sticky beak 1558 1559 initial_volume = domain.quantities['stage'].get_integral() 1560 initial_xmom = domain.quantities['xmomentum'].get_integral() 1561 1562 1562 #print initial_xmom 1563 1563 1564 1564 #Evolution 1565 1565 for t in domain.evolve(yieldstep = 0.05, finaltime = 5.0): 1566 volume = domain.quantities['stage'].get_integral() 1567 assert allclose (volume, initial_volume) 1568 1566 volume = domain.quantities['stage'].get_integral() 1567 assert allclose (volume, initial_volume) 1568 1569 1569 #I don't believe that the total momentum should be the same 1570 1570 #It starts with zero and ends with zero though 1571 #xmom = domain.quantities['xmomentum'].get_integral() 1571 #xmom = domain.quantities['xmomentum'].get_integral() 1572 1572 #print xmom 1573 #assert allclose (xmom, initial_xmom) 1574 1575 1576 1573 #assert allclose (xmom, initial_xmom) 1574 1575 1576 1577 1577 def test_conservation_2(self): 1578 1578 """Test that stage is conserved globally 1579 1580 This one uses a slopy bed, reflective bdries and a suitable 1579 1580 This one uses a slopy bed, reflective bdries and a suitable 1581 1581 initial condition 1582 1582 """ … … 1599 1599 1600 1600 domain.set_quantity('elevation', x_slope) 1601 domain.set_quantity('friction', 0) 1602 domain.set_quantity('stage', 0.4) #Steady 1603 1601 domain.set_quantity('friction', 0) 1602 domain.set_quantity('stage', 0.4) #Steady 1603 1604 1604 # Boundary conditions (reflective everywhere) 1605 1605 Br = Reflective_boundary(domain) … … 1608 1608 domain.check_integrity() 1609 1609 1610 #domain.visualise = True #If you want to take a sticky beak 1611 1612 initial_volume = domain.quantities['stage'].get_integral() 1613 initial_xmom = domain.quantities['xmomentum'].get_integral() 1614 1610 #domain.visualise = True #If you want to take a sticky beak 1611 1612 initial_volume = domain.quantities['stage'].get_integral() 1613 initial_xmom = domain.quantities['xmomentum'].get_integral() 1614 1615 1615 #print initial_xmom 1616 1616 1617 1617 #Evolution 1618 1618 for t in domain.evolve(yieldstep = 0.05, finaltime = 5.0): 1619 volume = domain.quantities['stage'].get_integral() 1620 assert allclose (volume, initial_volume) 1621 1619 volume = domain.quantities['stage'].get_integral() 1620 assert allclose (volume, initial_volume) 1621 1622 1622 #FIXME: What would we expect from momentum 1623 #xmom = domain.quantities['xmomentum'].get_integral() 1623 #xmom = domain.quantities['xmomentum'].get_integral() 1624 1624 #print xmom 1625 #assert allclose (xmom, initial_xmom) 1626 1627 1625 #assert allclose (xmom, initial_xmom) 1626 1627 1628 1628 def test_conservation_3(self): 1629 1629 """Test that stage is conserved globally 1630 1631 This one uses a larger grid, convoluted bed, reflective bdries and a suitable 1630 1631 This one uses a larger grid, convoluted bed, reflective bdries and a suitable 1632 1632 initial condition 1633 1633 """ … … 1650 1650 z = 0*x 1651 1651 for i in range(len(x)): 1652 if x[i] < 0.3: 1652 if x[i] < 0.3: 1653 1653 z[i] = x[i]/3 1654 if 0.3 <= x[i] < 0.5: 1654 if 0.3 <= x[i] < 0.5: 1655 1655 z[i] = -0.5 1656 if 0.5 <= x[i] < 0.7: 1657 z[i] = 0.39 1658 if 0.7 <= x[i]: 1659 z[i] = x[i]/3 1660 return z 1661 1662 1656 if 0.5 <= x[i] < 0.7: 1657 z[i] = 0.39 1658 if 0.7 <= x[i]: 1659 z[i] = x[i]/3 1660 return z 1661 1662 1663 1663 1664 1664 domain.set_quantity('elevation', x_slope) 1665 domain.set_quantity('friction', 0) 1666 domain.set_quantity('stage', 0.4) #Steady 1667 1665 domain.set_quantity('friction', 0) 1666 domain.set_quantity('stage', 0.4) #Steady 1667 1668 1668 # Boundary conditions (reflective everywhere) 1669 1669 Br = Reflective_boundary(domain) … … 1672 1672 domain.check_integrity() 1673 1673 1674 #domain.visualise = True #If you want to take a sticky beak 1675 1676 initial_volume = domain.quantities['stage'].get_integral() 1677 initial_xmom = domain.quantities['xmomentum'].get_integral() 1678 1674 #domain.visualise = True #If you want to take a sticky beak 1675 1676 initial_volume = domain.quantities['stage'].get_integral() 1677 initial_xmom = domain.quantities['xmomentum'].get_integral() 1678 1679 1679 import copy 1680 1680 ref_centroid_values =\ 1681 1681 copy.copy(domain.quantities['stage'].centroid_values) 1682 1683 #print 'ORG', domain.quantities['stage'].centroid_values 1682 1683 #print 'ORG', domain.quantities['stage'].centroid_values 1684 1684 domain.distribute_to_vertices_and_edges() 1685 1686 1685 1686 1687 1687 #print domain.quantities['stage'].centroid_values 1688 1688 assert allclose(domain.quantities['stage'].centroid_values, 1689 ref_centroid_values) 1690 1691 1689 ref_centroid_values) 1690 1691 1692 1692 #Check that initial limiter doesn't violate cons quan 1693 1693 assert allclose (domain.quantities['stage'].get_integral(), 1694 initial_volume) 1695 1694 initial_volume) 1695 1696 1696 #Evolution 1697 1697 for t in domain.evolve(yieldstep = 0.05, finaltime = 10): 1698 volume = domain.quantities['stage'].get_integral() 1698 volume = domain.quantities['stage'].get_integral() 1699 1699 #print t, volume, initial_volume 1700 assert allclose (volume, initial_volume) 1701 1702 1700 assert allclose (volume, initial_volume) 1701 1702 1703 1703 def test_conservation_4(self): 1704 1704 """Test that stage is conserved globally 1705 1706 This one uses a larger grid, convoluted bed, reflective bdries and a suitable 1705 1706 This one uses a larger grid, convoluted bed, reflective bdries and a suitable 1707 1707 initial condition 1708 1708 """ … … 1725 1725 z = 0*x 1726 1726 for i in range(len(x)): 1727 if x[i] < 0.3: 1727 if x[i] < 0.3: 1728 1728 z[i] = x[i]/3 1729 if 0.3 <= x[i] < 0.5: 1729 if 0.3 <= x[i] < 0.5: 1730 1730 z[i] = -0.5 1731 1731 if 0.5 <= x[i] < 0.7: 1732 #z[i] = 0.3 #OK with beta == 0.2 1732 #z[i] = 0.3 #OK with beta == 0.2 1733 1733 z[i] = 0.34 #OK with beta == 0.0 1734 #z[i] = 0.35#Fails after 80 timesteps with an error 1735 #of the order 1.0e-5 1736 if 0.7 <= x[i]: 1737 z[i] = x[i]/3 1738 return z 1739 1740 1734 #z[i] = 0.35#Fails after 80 timesteps with an error 1735 #of the order 1.0e-5 1736 if 0.7 <= x[i]: 1737 z[i] = x[i]/3 1738 return z 1739 1740 1741 1741 1742 1742 domain.set_quantity('elevation', x_slope) 1743 domain.set_quantity('friction', 0) 1744 domain.set_quantity('stage', 0.4) #Steady 1745 1743 domain.set_quantity('friction', 0) 1744 domain.set_quantity('stage', 0.4) #Steady 1745 1746 1746 # Boundary conditions (reflective everywhere) 1747 1747 Br = Reflective_boundary(domain) … … 1750 1750 domain.check_integrity() 1751 1751 1752 #domain.visualise = True #If you want to take a sticky beak 1753 1754 initial_volume = domain.quantities['stage'].get_integral() 1755 initial_xmom = domain.quantities['xmomentum'].get_integral() 1756 1752 #domain.visualise = True #If you want to take a sticky beak 1753 1754 initial_volume = domain.quantities['stage'].get_integral() 1755 initial_xmom = domain.quantities['xmomentum'].get_integral() 1756 1757 1757 import copy 1758 1758 ref_centroid_values =\ 1759 1759 copy.copy(domain.quantities['stage'].centroid_values) 1760 1760 1761 1761 #Test limiter by itself 1762 1762 domain.distribute_to_vertices_and_edges() 1763 1763 1764 1764 #Check that initial limiter doesn't violate cons quan 1765 1765 assert allclose (domain.quantities['stage'].get_integral(), 1766 initial_volume) 1767 #NOTE: This would fail if any initial stage was less than the 1766 initial_volume) 1767 #NOTE: This would fail if any initial stage was less than the 1768 1768 #corresponding bed elevation - but that is reasonable. 1769 1770 1769 1770 1771 1771 #Evolution 1772 1772 for t in domain.evolve(yieldstep = 0.05, finaltime = 10.0): 1773 volume = domain.quantities['stage'].get_integral() 1773 volume = domain.quantities['stage'].get_integral() 1774 1774 1775 1775 #print t, volume, initial_volume 1776 1777 1778 #if not allclose (volume, initial_volume): 1776 1777 1778 #if not allclose (volume, initial_volume): 1779 1779 # print 't==4.05' 1780 1780 # for k in range(domain.number_of_elements): 1781 1781 # pass 1782 1782 # print domain.quantities['stage'].centroid_values[k] -\ 1783 # ref_centroid_values[k] 1784 1785 assert allclose (volume, initial_volume) 1786 1787 1788 1789 1790 1783 # ref_centroid_values[k] 1784 1785 assert allclose (volume, initial_volume) 1786 1787 1788 1789 1790 1791 1791 def test_conservation_5(self): 1792 """Test that momentum is conserved globally in 1792 """Test that momentum is conserved globally in 1793 1793 steady state scenario 1794 1794 1795 1795 This one uses a slopy bed, dirichlet and reflective bdries 1796 1796 """ … … 1813 1813 1814 1814 domain.set_quantity('elevation', x_slope) 1815 domain.set_quantity('friction', 0) 1816 domain.set_quantity('stage', 0.4) #Steady 1817 1815 domain.set_quantity('friction', 0) 1816 domain.set_quantity('stage', 0.4) #Steady 1817 1818 1818 # Boundary conditions (reflective everywhere) 1819 Br = Reflective_boundary(domain) 1819 Br = Reflective_boundary(domain) 1820 1820 Bleft = Dirichlet_boundary([0.5,0,0]) 1821 Bright = Dirichlet_boundary([0.1,0,0]) 1822 domain.set_boundary({'left': Bleft, 'right': Bright, 1821 Bright = Dirichlet_boundary([0.1,0,0]) 1822 domain.set_boundary({'left': Bleft, 'right': Bright, 1823 1823 'top': Br, 'bottom': Br}) 1824 1824 1825 1825 domain.check_integrity() 1826 1826 1827 #domain.visualise = True #If you want to take a sticky beak 1828 1829 initial_volume = domain.quantities['stage'].get_integral() 1830 initial_xmom = domain.quantities['xmomentum'].get_integral() 1831 1827 #domain.visualise = True #If you want to take a sticky beak 1828 1829 initial_volume = domain.quantities['stage'].get_integral() 1830 initial_xmom = domain.quantities['xmomentum'].get_integral() 1831 1832 1832 1833 1833 #Evolution 1834 1834 for t in domain.evolve(yieldstep = 0.05, finaltime = 15.0): 1835 stage = domain.quantities['stage'].get_integral() 1836 xmom = domain.quantities['xmomentum'].get_integral() 1837 ymom = domain.quantities['ymomentum'].get_integral() 1838 1835 stage = domain.quantities['stage'].get_integral() 1836 xmom = domain.quantities['xmomentum'].get_integral() 1837 ymom = domain.quantities['ymomentum'].get_integral() 1838 1839 1839 if allclose(t, 6): #Steady state reached 1840 1840 steady_xmom = domain.quantities['xmomentum'].get_integral() 1841 1841 steady_ymom = domain.quantities['ymomentum'].get_integral() 1842 steady_stage = domain.quantities['stage'].get_integral() 1843 1842 steady_stage = domain.quantities['stage'].get_integral() 1843 1844 1844 if t > 6: 1845 1845 #print '%.2f %14.8f %14.8f' %(t, ymom, steady_ymom) 1846 assert allclose(xmom, steady_xmom) 1846 assert allclose(xmom, steady_xmom) 1847 1847 assert allclose(ymom, steady_ymom) 1848 assert allclose(stage, steady_stage) 1849 1850 1851 1852 1848 assert allclose(stage, steady_stage) 1849 1850 1851 1852 1853 1853 def test_second_order_flat_bed_onestep(self): 1854 1854 … … 1879 1879 #Data from earlier version of pyvolution 1880 1880 assert allclose(domain.min_timestep, 0.0396825396825) 1881 assert allclose(domain.max_timestep, 0.0396825396825) 1881 assert allclose(domain.max_timestep, 0.0396825396825) 1882 1882 1883 1883 assert allclose(domain.quantities['stage'].centroid_values[:12], … … 1897 1897 0.00024152, 0.02656103, 0.00024152, 0.02656103, 1898 1898 0.00024152, 0.02656103, 0.00040506, 0.0272623]) 1899 1899 1900 1900 assert allclose(domain.quantities['stage'].vertex_values[:12,2], 1901 1901 [0.00182037, 0.02656103, 0.00676264, … … 1919 1919 1920 1920 1921 1921 1922 1922 def test_second_order_flat_bed_moresteps(self): 1923 1923 … … 1948 1948 #Data from earlier version of pyvolution 1949 1949 #assert allclose(domain.min_timestep, 0.0396825396825) 1950 #assert allclose(domain.max_timestep, 0.0396825396825) 1950 #assert allclose(domain.max_timestep, 0.0396825396825) 1951 1951 #print domain.quantities['stage'].centroid_values 1952 1952 … … 1972 1972 Br = Reflective_boundary(domain) 1973 1973 Bd = Dirichlet_boundary([0.2,0.,0.]) 1974 1974 1975 1975 domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br}) 1976 1976 domain.check_integrity() 1977 1977 1978 1978 1979 1979 #Evolution 1980 1980 for t in domain.evolve(yieldstep = 0.02, finaltime = 0.5): … … 1982 1982 #domain.write_time() 1983 1983 1984 #FIXME: These numbers were from version before 25/10 1984 #FIXME: These numbers were from version before 25/10 1985 1985 #assert allclose(domain.min_timestep, 0.0140413643926) 1986 1986 #assert allclose(domain.max_timestep, 0.0140947355753) … … 1994 1994 1995 1995 #assert allclose(domain.quantities['ymomentum'].edge_values[:4,i], 1996 # [-0.0060238, -0.00157404, -0.00309633, -0.0001637]) 1997 1996 # [-0.0060238, -0.00157404, -0.00309633, -0.0001637]) 1997 1998 1998 def test_flatbed_second_order(self): 1999 1999 from mesh_factory import rectangular … … 2020 2020 domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br}) 2021 2021 domain.check_integrity() 2022 2022 2023 2023 #Evolution 2024 2024 for t in domain.evolve(yieldstep = 0.01, finaltime = 0.03): … … 2027 2027 2028 2028 assert allclose(domain.min_timestep, 0.0210448446782) 2029 assert allclose(domain.max_timestep, 0.0210448446782) 2029 assert allclose(domain.max_timestep, 0.0210448446782) 2030 2030 2031 2031 #print domain.quantities['stage'].vertex_values[:4,0] … … 2036 2036 #assert allclose(domain.quantities['stage'].vertex_values[:4,0], 2037 2037 # [0.00101913,0.05352143,0.00104852,0.05354394]) 2038 2038 2039 2039 assert allclose(domain.quantities['xmomentum'].vertex_values[:4,0], 2040 2040 [ 0.00064835, 0.03685719, 0.00085073, 0.03687313]) … … 2042 2042 #assert allclose(domain.quantities['xmomentum'].vertex_values[:4,0], 2043 2043 # [0.00090581,0.03685719,0.00088303,0.03687313]) 2044 2044 2045 2045 assert allclose(domain.quantities['ymomentum'].vertex_values[:4,0], 2046 2046 [-0.00139463,0.0006156,-0.00060364,0.00061827]) 2047 2047 2048 2048 2049 2049 2050 2050 def test_flatbed_second_order_distribute(self): 2051 2051 #Use real data from pyvolution 2 … … 2078 2078 for V in [False, True]: 2079 2079 if V: 2080 #Set centroids as if system had been evolved 2080 #Set centroids as if system had been evolved 2081 2081 L = zeros(2*N*N, Float) 2082 2082 L[:32] = [7.21205592e-003, 5.35214298e-002, 1.00910824e-002, … … 2091 2091 4.37962142e-005, 0.00000000e+000, 4.37962142e-005, 2092 2092 0.00000000e+000, 5.57305948e-005] 2093 2093 2094 2094 X = zeros(2*N*N, Float) 2095 2095 X[:32] = [6.48351607e-003, 3.68571894e-002, 8.50733285e-003, … … 2118 2118 0.00000000e+000, -2.57635178e-005] 2119 2119 2120 2120 2121 2121 domain.set_quantity('stage', L, 'centroids') 2122 2122 domain.set_quantity('xmomentum', X, 'centroids') 2123 2123 domain.set_quantity('ymomentum', Y, 'centroids') 2124 2124 2125 2125 domain.check_integrity() 2126 else: 2126 else: 2127 2127 #Evolution 2128 2128 for t in domain.evolve(yieldstep = 0.01, finaltime = 0.03): … … 2130 2130 assert allclose(domain.min_timestep, 0.0210448446782) 2131 2131 assert allclose(domain.max_timestep, 0.0210448446782) 2132 2132 2133 2133 2134 2134 #Centroids were correct but not vertices. … … 2149 2149 ##print domain.quantities['xmomentum'].centroid_values[17], V 2150 2150 ##print 2151 if not V: 2151 if not V: 2152 2152 assert allclose(domain.quantities['xmomentum'].centroid_values[17], 0.0) 2153 else: 2154 assert allclose(domain.quantities['xmomentum'].centroid_values[17], 0.00028606084) 2153 else: 2154 assert allclose(domain.quantities['xmomentum'].centroid_values[17], 0.00028606084) 2155 2155 2156 2156 import copy 2157 2157 XX = copy.copy(domain.quantities['xmomentum'].centroid_values) 2158 2158 assert allclose(domain.quantities['xmomentum'].centroid_values, XX) 2159 2159 2160 2160 domain.distribute_to_vertices_and_edges() 2161 2161 … … 2187 2187 #print domain.quantities['xmomentum'].vertex_values[:4,0] 2188 2188 #assert allclose(domain.quantities['xmomentum'].vertex_values[:4,0], 2189 # [0.00090581,0.03685719,0.00088303,0.03687313]) 2190 2191 2192 2193 2194 2189 # [0.00090581,0.03685719,0.00088303,0.03687313]) 2190 2191 2192 2193 2194 2195 2195 def test_bedslope_problem_first_order(self): 2196 2196 … … 2228 2228 assert allclose(domain.max_timestep, 0.050010003001) 2229 2229 2230 2230 2231 2231 def test_bedslope_problem_first_order_moresteps(self): 2232 2232 … … 2264 2264 #Data from earlier version of pyvolution 2265 2265 #print domain.quantities['stage'].centroid_values 2266 2266 2267 2267 assert allclose(domain.quantities['stage'].centroid_values, 2268 2268 [-0.02998628, -0.01520652, -0.03043492, … … 2290 2290 -0.14175896, -0.14560798, -0.13911658, 2291 2291 -0.14439383, -0.13924047, -0.14829043]) 2292 2293 2292 2293 2294 2294 def test_bedslope_problem_second_order_one_step(self): 2295 2295 … … 2349 2349 #print domain.quantities['stage'].extrapolate_second_order() 2350 2350 #domain.distribute_to_vertices_and_edges() 2351 #print domain.quantities['stage'].vertex_values[:,0] 2352 2351 #print domain.quantities['stage'].vertex_values[:,0] 2352 2353 2353 #Evolution 2354 2354 for t in domain.evolve(yieldstep = 0.05, finaltime = 0.05): 2355 #domain.write_time() 2355 #domain.write_time() 2356 2356 pass 2357 2357 2358 2358 2359 #print domain.quantities['stage'].centroid_values 2359 #print domain.quantities['stage'].centroid_values 2360 2360 assert allclose(domain.quantities['stage'].centroid_values, 2361 2361 [0.01290985, 0.02356019, 0.01619096, 0.02356019, 0.01619096, … … 2377 2377 -0.25865535, -0.24776246, -0.25865535, -0.24521113]) 2378 2378 2379 2379 2380 2380 2381 2381 def test_bedslope_problem_second_order_two_steps(self): … … 2392 2392 domain.smooth = False 2393 2393 domain.default_order=2 2394 domain.beta_h = 0.0 #Use first order in h-limiter 2394 domain.beta_h = 0.0 #Use first order in h-limiter 2395 2395 2396 2396 #Bed-slope and friction at vertices (and interpolated elsewhere) … … 2437 2437 #print domain.quantities['stage'].extrapolate_second_order() 2438 2438 #domain.distribute_to_vertices_and_edges() 2439 #print domain.quantities['stage'].vertex_values[:,0] 2440 2439 #print domain.quantities['stage'].vertex_values[:,0] 2440 2441 2441 #Evolution 2442 2442 for t in domain.evolve(yieldstep = 0.05, finaltime = 0.1): 2443 2443 pass 2444 2444 2445 2445 2446 2446 #Data from earlier version of pyvolution ft=0.1 2447 assert allclose(domain.min_timestep, 0.0376895634803) 2447 assert allclose(domain.min_timestep, 0.0376895634803) 2448 2448 assert allclose(domain.max_timestep, 0.0415635655309) 2449 2449 … … 2469 2469 -0.25031463, -0.24454764, -0.24885323, -0.24286438]) 2470 2470 2471 2472 2473 2471 2472 2473 2474 2474 def test_bedslope_problem_second_order_two_yieldsteps(self): 2475 2475 … … 2485 2485 domain.smooth = False 2486 2486 domain.default_order=2 2487 domain.beta_h = 0.0 #Use first order in h-limiter 2487 domain.beta_h = 0.0 #Use first order in h-limiter 2488 2488 2489 2489 #Bed-slope and friction at vertices (and interpolated elsewhere) … … 2530 2530 #print domain.quantities['stage'].extrapolate_second_order() 2531 2531 #domain.distribute_to_vertices_and_edges() 2532 #print domain.quantities['stage'].vertex_values[:,0] 2533 2532 #print domain.quantities['stage'].vertex_values[:,0] 2533 2534 2534 #Evolution 2535 2535 for t in domain.evolve(yieldstep = 0.05, finaltime = 0.1): #0.05?? 2536 #domain.write_time() 2536 #domain.write_time() 2537 2537 pass 2538 2538 2539 2539 2540 2541 assert allclose(domain.quantities['stage'].centroid_values, 2540 2541 assert allclose(domain.quantities['stage'].centroid_values, 2542 2542 [0.00855788, 0.01575204, 0.00994606, 0.01717072, 0.01005985, 2543 2543 0.01716362, 0.01005985, 0.01716299, 0.01007098, 0.01736248, … … 2574 2574 domain.smooth = False 2575 2575 domain.default_order=2 2576 domain.beta_h = 0.0 #Use first order in h-limiter 2576 domain.beta_h = 0.0 #Use first order in h-limiter 2577 2577 2578 2578 #Bed-slope and friction at vertices (and interpolated elsewhere) … … 2619 2619 #print domain.quantities['stage'].extrapolate_second_order() 2620 2620 #domain.distribute_to_vertices_and_edges() 2621 #print domain.quantities['stage'].vertex_values[:,0] 2622 2621 #print domain.quantities['stage'].vertex_values[:,0] 2622 2623 2623 #Evolution 2624 2624 for t in domain.evolve(yieldstep = 0.05, finaltime = 0.5): 2625 2625 pass 2626 2626 2627 2627 2628 2628 assert allclose(domain.quantities['stage'].centroid_values, 2629 2629 [-0.02907028, -0.01475478, -0.02973417, -0.01447186, -0.02932665, -0.01428285, … … 2631 2631 -0.07597998, -0.06252591, -0.07664854, -0.06312532, -0.07638287, -0.06265139, 2632 2632 -0.07571145, -0.06235231, -0.0756817, -0.06245309, -0.07652292, -0.06289946, 2633 -0.12367464, -0.11088981, -0.12237277, -0.11115338, -0.1218934, -0.1107174, 2633 -0.12367464, -0.11088981, -0.12237277, -0.11115338, -0.1218934, -0.1107174, 2634 2634 -0.12081485, -0.11000491, -0.12038451, -0.11010335, -0.12102113, -0.11012105, 2635 2635 -0.16909116, -0.15831543, -0.16730214, -0.15786249, -0.1665493, -0.15697919, … … 2644 2644 0.00788729, 0.00356522, 0.00780649, 0.00341919, 0.00693525, 0.00310375, 2645 2645 0.02166196, 0.01421475, 0.02017737, 0.01316839, 0.02037015, 0.01368659, 2646 0.02106, 0.01399161, 0.02074514, 0.01354935, 0.01887407, 0.0123113, 2646 0.02106, 0.01399161, 0.02074514, 0.01354935, 0.01887407, 0.0123113, 2647 2647 0.03775083, 0.02855197, 0.03689337, 0.02759782, 0.03732848, 0.02812072, 2648 0.03872545, 0.02913348, 0.03880939, 0.02803804, 0.03546499, 0.0260039, 2648 0.03872545, 0.02913348, 0.03880939, 0.02803804, 0.03546499, 0.0260039, 2649 2649 0.0632131, 0.04730634, 0.0576324, 0.04592336, 0.05790921, 0.04690514, 2650 2650 0.05986467, 0.04871165, 0.06170068, 0.04811572, 0.05657041, 0.04416292, 2651 0.08489642, 0.07188097, 0.07835261, 0.06843406, 0.07986412, 0.0698247, 2651 0.08489642, 0.07188097, 0.07835261, 0.06843406, 0.07986412, 0.0698247, 2652 2652 0.08201071, 0.07216756, 0.08378418, 0.07273624, 0.080399, 0.06645841, 2653 2653 0.01631548, 0.04691608, 0.0206632, 0.044409, 0.02115518, 0.04560305, 2654 2654 0.02160608, 0.04663725, 0.02174734, 0.04795559, 0.02281427, 0.05667111]) 2655 2655 2656 2657 assert allclose(domain.quantities['ymomentum'].centroid_values, 2656 2657 assert allclose(domain.quantities['ymomentum'].centroid_values, 2658 2658 [ 1.45876601e-004, -3.24627393e-004, -1.57572719e-004, -2.92790187e-004, 2659 2659 -9.90988382e-005, -3.06677335e-004, -1.62493106e-004, -3.71310004e-004, … … 2674 2674 -6.17359400e-003, -5.98761937e-003, -6.00540116e-003, -5.01121966e-003, 2675 2675 -4.50964850e-003, -3.06319963e-003, 6.08950810e-004, -4.79537921e-004]) 2676 2677 2678 2676 2677 2678 2679 2679 2680 2680 def test_temp_play(self): … … 2691 2691 domain.smooth = False 2692 2692 domain.default_order=2 2693 domain.beta_h = 0.0 #Use first order in h-limiter 2693 domain.beta_h = 0.0 #Use first order in h-limiter 2694 2694 2695 2695 #Bed-slope and friction at vertices (and interpolated elsewhere) … … 2713 2713 assert allclose(domain.quantities['stage'].centroid_values[:4], 2714 2714 [0.00206836, 0.01296714, 0.00363415, 0.01438924]) 2715 assert allclose(domain.quantities['xmomentum'].centroid_values[:4], 2715 assert allclose(domain.quantities['xmomentum'].centroid_values[:4], 2716 2716 [0.01360154, 0.00671133, 0.01264578, 0.00648503]) 2717 2717 assert allclose(domain.quantities['ymomentum'].centroid_values[:4], 2718 [-1.19201077e-003, -7.23647546e-004, 2718 [-1.19201077e-003, -7.23647546e-004, 2719 2719 -6.39083123e-005, 6.29815168e-005]) 2720 2721 2720 2721 2722 2722 def test_complex_bed(self): 2723 2723 #No friction is tested here 2724 2724 2725 2725 from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\ 2726 2726 Transmissive_boundary, Time_boundary,\ … … 2729 2729 from mesh_factory import rectangular 2730 2730 from Numeric import array 2731 2731 2732 2732 N = 12 2733 2733 points, vertices, boundary = rectangular(N, N/2, len1=1.2,len2=0.6, … … 2740 2740 domain.default_order=2 2741 2741 2742 2742 2743 2743 inflow_stage = 0.1 2744 2744 Z = Weir(inflow_stage) … … 2757 2757 #print domain.quantities['stage'].centroid_values 2758 2758 2759 #FIXME: These numbers were from version before 25/10 2759 #FIXME: These numbers were from version before 25/10 2760 2760 #assert allclose(domain.quantities['stage'].centroid_values, 2761 2761 # [3.95822638e-002, 5.61022588e-002, 4.66437868e-002, 5.73081011e-002, … … 2810 2810 """ 2811 2811 import time 2812 2813 #Create sww file of simple propagation from left to right 2814 2815 2812 2813 #Create sww file of simple propagation from left to right 2814 #through rectangular domain 2815 2816 2816 from mesh_factory import rectangular 2817 2817 … … 2821 2821 #Create shallow water domain 2822 2822 domain1 = Domain(points, vertices, boundary) 2823 2823 2824 2824 from util import mean 2825 domain1.reduction = mean 2825 domain1.reduction = mean 2826 2826 domain1.smooth = False #Exact result 2827 2827 2828 2828 domain1.default_order = 2 2829 domain1.store = True 2829 domain1.store = True 2830 2830 domain1.set_datadir('.') 2831 2831 domain1.set_name('spatio_temporal_boundary_source' + str(time.time())) 2832 2833 2832 2833 #FIXME: This is extremely important! 2834 2834 #How can we test if they weren't stored? 2835 2835 domain1.quantities_to_be_stored = ['stage', 'xmomentum', 'ymomentum'] 2836 2836 2837 2837 2838 2838 #Bed-slope and friction at vertices (and interpolated elsewhere) 2839 2839 domain1.set_quantity('elevation', 0) 2840 domain1.set_quantity('friction', 0) 2840 domain1.set_quantity('friction', 0) 2841 2841 2842 2842 # Boundary conditions … … 2855 2855 2856 2856 cv1 = domain1.quantities['stage'].centroid_values 2857 2858 2859 #Create an triangle shaped domain (reusing coordinates from domain 1), 2860 #formed from the lower and right hand boundaries and 2861 #the sw-ne diagonal 2862 #from domain 1. Call it domain2 2863 2864 points = [ [0,0], [1.0/3,0], [1.0/3,1.0/3], 2865 [2.0/3,0], [2.0/3,1.0/3], [2.0/3,2.0/3], 2866 [1,0], [1,1.0/3], [1,2.0/3], [1,1]] 2867 2868 vertices = [ [1,2,0], 2869 [3,4,1], [2,1,4], [4,5,2], 2870 [6,7,3], [4,3,7], [7,8,4], [5,4,8], [8,9,5]] 2857 2858 2859 #Create an triangle shaped domain (reusing coordinates from domain 1), 2860 #formed from the lower and right hand boundaries and 2861 #the sw-ne diagonal 2862 #from domain 1. Call it domain2 2863 2864 points = [ [0,0], [1.0/3,0], [1.0/3,1.0/3], 2865 [2.0/3,0], [2.0/3,1.0/3], [2.0/3,2.0/3], 2866 [1,0], [1,1.0/3], [1,2.0/3], [1,1]] 2867 2868 vertices = [ [1,2,0], [3,4,1], [2,1,4], [4,5,2], 2869 [6,7,3], [4,3,7], [7,8,4], [5,4,8], [8,9,5]] 2871 2870 2872 2871 boundary = { (0,1):'bottom', (1,1):'bottom', (4,1): 'bottom', 2873 2872 (4,2):'right', (6,2):'right', (8,2):'right', 2874 (0,0):'diagonal', (3,0):'diagonal', (8,0):'diagonal'} 2875 2873 (0,0):'diagonal', (3,0):'diagonal', (8,0):'diagonal'} 2874 2876 2875 domain2 = Domain(points, vertices, boundary) 2877 2878 2876 2877 domain2.reduction = domain1.reduction 2879 2878 domain2.smooth = False 2880 2879 domain2.default_order = 2 2881 2880 2882 2881 #Bed-slope and friction at vertices (and interpolated elsewhere) 2883 2882 domain2.set_quantity('elevation', 0) 2884 2883 domain2.set_quantity('friction', 0) 2885 domain2.set_quantity('stage', 0) 2884 domain2.set_quantity('stage', 0) 2886 2885 2887 2886 # Boundary conditions 2888 Br = Reflective_boundary(domain2) 2889 2887 Br = Reflective_boundary(domain2) 2888 Bf = Spatio_temporal_boundary(domain1.filename + '.' + domain1.format, 2890 2889 domain2) 2891 2890 domain2.set_boundary({'right':Br, 'bottom':Br, 'diagonal':Bf}) 2892 domain2.check_integrity() 2893 2891 domain2.check_integrity() 2892 2894 2893 2895 2894 … … 2898 2897 pass 2899 2898 2900 2901 2902 #and verify that results at right hand side are close. 2899 2900 #Use output from domain1 as spatio-temporal boundary for domain2 2901 #and verify that results at right hand side are close. 2903 2902 2904 2903 cv2 = domain2.quantities['stage'].centroid_values 2905 2904 2906 2905 #print take(cv1, (12,14,16)) #Right 2907 2908 #print take(cv1, (0,6,12)) #Bottom 2909 2910 #print take(cv1, (0,8,16)) #Diag 2911 2912 2913 2914 2906 #print take(cv2, (4,6,8)) 2907 #print take(cv1, (0,6,12)) #Bottom 2908 #print take(cv2, (0,1,4)) 2909 #print take(cv1, (0,8,16)) #Diag 2910 #print take(cv2, (0,3,8)) 2911 2912 assert allclose( take(cv1, (0,8,16)), take(cv2, (0,3,8))) #Diag 2913 assert allclose( take(cv1, (0,6,12)), take(cv2, (0,1,4))) #Bottom 2915 2914 assert allclose( take(cv1, (12,14,16)), take(cv2, (4,6,8))) #RHS 2916 2915 2917 2916 #Cleanup 2918 os.remove(domain1.filename + '.' + domain1.format) 2919 2920 2917 os.remove(domain1.filename + '.' + domain1.format) 2918 2919 2921 2920 2922 2921 def test_spatio_temporal_boundary_2(self): 2923 2922 """Test that boundary values can be read from file and interpolated 2924 2923 in both time and space. 2925 This is a more basic test, verifying that boundary object 2926 2924 This is a more basic test, verifying that boundary object 2925 produces the expected results 2927 2926 2928 2927 2929 2928 """ 2930 2929 import time 2931 2932 #Create sww file of simple propagation from left to right 2933 2934 2930 2931 #Create sww file of simple propagation from left to right 2932 #through rectangular domain 2933 2935 2934 from mesh_factory import rectangular 2936 2935 … … 2940 2939 #Create shallow water domain 2941 2940 domain1 = Domain(points, vertices, boundary) 2942 2941 2943 2942 from util import mean 2944 domain1.reduction = mean 2943 domain1.reduction = mean 2945 2944 domain1.smooth = True #To mimic MOST output 2946 2945 2947 2946 domain1.default_order = 2 2948 domain1.store = True 2947 domain1.store = True 2949 2948 domain1.set_datadir('.') 2950 2949 domain1.set_name('spatio_temporal_boundary_source' + str(time.time())) 2951 2952 2950 2951 #FIXME: This is extremely important! 2953 2952 #How can we test if they weren't stored? 2954 2953 domain1.quantities_to_be_stored = ['stage', 'xmomentum', 'ymomentum'] 2955 2954 2956 2955 2957 2956 #Bed-slope and friction at vertices (and interpolated elsewhere) 2958 2957 domain1.set_quantity('elevation', 0) 2959 domain1.set_quantity('friction', 0) 2958 domain1.set_quantity('friction', 0) 2960 2959 2961 2960 # Boundary conditions 2962 2961 Br = Reflective_boundary(domain1) 2963 Bd = Dirichlet_boundary([0.3,0,0]) 2962 Bd = Dirichlet_boundary([0.3,0,0]) 2964 2963 domain1.set_boundary({'left': Bd, 'top': Bd, 'right': Br, 'bottom': Br}) 2965 2964 #Initial condition … … 2973 2972 #domain1.write_time() 2974 2973 2975 2976 2977 #coordinates from domain 1), 2978 #formed from the lower and right hand boundaries and 2979 #the sw-ne diagonal 2980 2981 2982 2974 2975 #Create an triangle shaped domain (coinciding with some 2976 #coordinates from domain 1), 2977 #formed from the lower and right hand boundaries and 2978 #the sw-ne diagonal 2979 #from domain 1. Call it domain2 2980 2981 points = [ [0,0], [1.0/3,0], [1.0/3,1.0/3], 2983 2982 [2.0/3,0], [2.0/3,1.0/3], [2.0/3,2.0/3], 2984 2983 [1,0], [1,1.0/3], [1,2.0/3], [1,1]] 2985 2986 vertices = [ [1,2,0], 2987 [3,4,1], [2,1,4], [4,5,2], 2988 [6,7,3], [4,3,7], [7,8,4], [5,4,8], [8,9,5]] 2984 2985 vertices = [ [1,2,0], 2986 [3,4,1], [2,1,4], [4,5,2], 2987 [6,7,3], [4,3,7], [7,8,4], [5,4,8], [8,9,5]] 2989 2988 2990 2989 boundary = { (0,1):'bottom', (1,1):'bottom', (4,1): 'bottom', 2991 2990 (4,2):'right', (6,2):'right', (8,2):'right', 2992 (0,0):'diagonal', (3,0):'diagonal', (8,0):'diagonal'} 2993 2991 (0,0):'diagonal', (3,0):'diagonal', (8,0):'diagonal'} 2992 2994 2993 domain2 = Domain(points, vertices, boundary) 2995 2996 2994 2995 domain2.reduction = domain1.reduction 2997 2996 domain2.smooth = False 2998 2997 domain2.default_order = 2 2999 2998 3000 2999 #Bed-slope and friction at vertices (and interpolated elsewhere) 3001 3000 domain2.set_quantity('elevation', 0) 3002 3001 domain2.set_quantity('friction', 0) 3003 domain2.set_quantity('stage', 0) 3004 3005 3006 3007 from Scientific.IO.NetCDF import NetCDFFile 3008 3009 3010 3011 y = fid.variables['y'][:] 3012 3013 s2 = fid.variables['stage'][2,:] 3014 3015 3016 from Numeric import take, reshape, concatenate 3017 3018 points = concatenate( (reshape(x, shp), reshape(y, shp)), axis=1) 3019 3020 3021 3022 assert allclose( take(points, [0,5,10,15]), 3023 [[0,0], [1.0/3, 1.0/3], [2.0/3, 2.0/3], [1,1]]) 3024 3025 3002 domain2.set_quantity('stage', 0) 3003 3004 3005 #Read results for specific timesteps t=1 and t=2 3006 from Scientific.IO.NetCDF import NetCDFFile 3007 fid = NetCDFFile(domain1.filename + '.' + domain1.format) 3008 3009 x = fid.variables['x'][:] 3010 y = fid.variables['y'][:] 3011 s1 = fid.variables['stage'][1,:] 3012 s2 = fid.variables['stage'][2,:] 3013 fid.close() 3014 3015 from Numeric import take, reshape, concatenate 3016 shp = (len(x), 1) 3017 points = concatenate( (reshape(x, shp), reshape(y, shp)), axis=1) 3018 #The diagonal points of domain 1 are 0, 5, 10, 15 3019 3020 #print points[0], points[5], points[10], points[15] 3021 assert allclose( take(points, [0,5,10,15]), 3022 [[0,0], [1.0/3, 1.0/3], [2.0/3, 2.0/3], [1,1]]) 3023 3024 3026 3025 # Boundary conditions 3027 Br = Reflective_boundary(domain2) 3028 3026 Br = Reflective_boundary(domain2) 3027 Bf = Spatio_temporal_boundary(domain1.filename + '.' + domain1.format, 3029 3028 domain2) 3030 3029 domain2.set_boundary({'right':Br, 'bottom':Br, 'diagonal':Bf}) 3031 domain2.check_integrity() 3032 3033 3034 #segments 3035 3036 3030 domain2.check_integrity() 3031 3032 #Test that interpolation points are the mid points of the all boundary 3033 #segments 3034 3035 boundary_midpoints = [[1.0/6, 0], [1.0/2, 0], [5.0/6,0], 3037 3036 [1.0, 1.0/6], [1.0, 1.0/2], [1.0, 5.0/6], 3038 3037 [1.0/6, 1.0/6], [0.5, 0.5], [5.0/6, 5.0/6]] 3039 3038 3040 3039 boundary_midpoints.sort() 3041 R = Bf.F.interpolation_points.tolist() 3042 3043 assert allclose(boundary_midpoints, R) 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 assert allclose(R0[0], (s1[10] + s1[15])/2) 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 assert allclose(R0[0], (s2[10] + s2[15])/2) 3074 3075 3076 3040 R = Bf.F.interpolation_points.tolist() 3041 R.sort() 3042 assert allclose(boundary_midpoints, R) 3043 3044 #Check spatially interpolated output at time == 1 3045 domain2.time = 1 3046 3047 #First diagonal midpoint 3048 R0 = Bf.evaluate(0,0) 3049 assert allclose(R0[0], (s1[0] + s1[5])/2) 3050 3051 #Second diagonal midpoint 3052 R0 = Bf.evaluate(3,0) 3053 assert allclose(R0[0], (s1[5] + s1[10])/2) 3054 3055 #First diagonal midpoint 3056 R0 = Bf.evaluate(8,0) 3057 assert allclose(R0[0], (s1[10] + s1[15])/2) 3058 3059 #Check spatially interpolated output at time == 2 3060 domain2.time = 2 3061 3062 #First diagonal midpoint 3063 R0 = Bf.evaluate(0,0) 3064 assert allclose(R0[0], (s2[0] + s2[5])/2) 3065 3066 #Second diagonal midpoint 3067 R0 = Bf.evaluate(3,0) 3068 assert allclose(R0[0], (s2[5] + s2[10])/2) 3069 3070 #First diagonal midpoint 3071 R0 = Bf.evaluate(8,0) 3072 assert allclose(R0[0], (s2[10] + s2[15])/2) 3073 3074 3075 #Now check temporal interpolation 3077 3076 3078 3077 domain2.time = 1 + 2.0/3 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 assert allclose(R0[0], ((s1[10] + s1[15])/2 + 2.0*(s2[10] + s2[15])/2)/3) 3091 3092 3093 3078 3079 #First diagonal midpoint 3080 R0 = Bf.evaluate(0,0) 3081 assert allclose(R0[0], ((s1[0] + s1[5])/2 + 2.0*(s2[0] + s2[5])/2)/3) 3082 3083 #Second diagonal midpoint 3084 R0 = Bf.evaluate(3,0) 3085 assert allclose(R0[0], ((s1[5] + s1[10])/2 + 2.0*(s2[5] + s2[10])/2)/3) 3086 3087 #First diagonal midpoint 3088 R0 = Bf.evaluate(8,0) 3089 assert allclose(R0[0], ((s1[10] + s1[15])/2 + 2.0*(s2[10] + s2[15])/2)/3) 3090 3091 3092 3094 3093 #Cleanup 3095 os.remove(domain1.filename + '.' + domain1.format) 3096 #------------------------------------------------------------- 3094 os.remove(domain1.filename + '.' + domain1.format) 3095 3096 3097 #------------------------------------------------------------- 3097 3098 if __name__ == "__main__": 3098 suite = unittest.makeSuite(Test Case,'test')3099 suite = unittest.makeSuite(Test_Shallow_Water,'test') 3099 3100 runner = unittest.TextTestRunner() 3100 3101 runner.run(suite) 3101
Note: See TracChangeset
for help on using the changeset viewer.