[3294] | 1 | from math import sqrt, pi |
---|
| 2 | from shallow_water_1d import * |
---|
| 3 | from Numeric import allclose, array, zeros, ones, Float, take |
---|
| 4 | from config import g, epsilon |
---|
| 5 | |
---|
| 6 | def test_sqrt(): |
---|
| 7 | for i in range (80000): |
---|
| 8 | a = sqrt(4356) |
---|
| 9 | b = sqrt(2031) |
---|
| 10 | |
---|
| 11 | def test_flux1(): |
---|
| 12 | #Use data from previous version of pyvolution |
---|
| 13 | #normal = array([1.,0]) |
---|
| 14 | #ql = array([-0.2, 2, 3]) |
---|
| 15 | #qr = array([-0.2, 2, 3]) |
---|
| 16 | ql = array([-0.2, 2]) |
---|
| 17 | qr = array([-0.2, 2]) |
---|
| 18 | zl = zr = -0.5 |
---|
| 19 | #flux, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
| 20 | flux, max_speed = flux_function(1.0, ql, qr, zl, zr) |
---|
| 21 | print 'flux', flux |
---|
| 22 | |
---|
| 23 | def test_compute_fluxes0(): |
---|
| 24 | #Do a full triangle and check that fluxes cancel out for |
---|
| 25 | #the constant stage case |
---|
| 26 | |
---|
| 27 | print 'check min time step in compute fluxes is ok, John' |
---|
| 28 | |
---|
| 29 | #a = [0.0, 0.0] |
---|
| 30 | #b = [0.0, 2.0] |
---|
| 31 | #c = [2.0,0.0] |
---|
| 32 | #d = [0.0, 4.0] |
---|
| 33 | #e = [2.0, 2.0] |
---|
| 34 | #f = [4.0,0.0] |
---|
| 35 | a=0.0 |
---|
| 36 | b=2.0 |
---|
| 37 | c=4.0 |
---|
| 38 | d=6.0 |
---|
| 39 | e=8.0 |
---|
| 40 | f=10.0 |
---|
| 41 | |
---|
| 42 | points = [a, b, c, d, e, f] |
---|
| 43 | #bac, bce, ecf, dbe |
---|
| 44 | #vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
| 45 | |
---|
| 46 | domain = Domain(points) |
---|
| 47 | #domain.set_quantity('stage', [[2,2,2], [2,2,2], |
---|
| 48 | # [2,2,2], [2,2,2]]) |
---|
| 49 | domain.set_quantity('stage', [[2,2], [2,2], [2,2], [2,2], [2,2]]) |
---|
| 50 | domain.check_integrity() |
---|
| 51 | |
---|
| 52 | #assert allclose(domain.neighbours, [[-1,1,-1], [2,3,0], [-1,-1,1],[1,-1,-1]]) |
---|
| 53 | assert allclose(domain.neighbours, [[-1,1], [0,2], [1,3],[2,4], [3,-1]]) |
---|
| 54 | #assert allclose(domain.neighbour_edges, [[-1,2,-1], [2,0,1], [-1,-1,0],[1,-1,-1]]) |
---|
| 55 | #assert allclose(domain.neighbour_edges, [[-1,0], [1,0], [1,0], [1,0], [1,-1]]) |
---|
| 56 | assert allclose(domain.neighbour_vertices, [[-1,0], [1,0], [1,0], [1,0], [1,-1]]) |
---|
| 57 | |
---|
| 58 | zl=zr=0. #Assume flat bed |
---|
| 59 | |
---|
| 60 | #Flux across right edge of volume 1 |
---|
| 61 | #normal = domain.get_normal(1,0) |
---|
| 62 | #ql = domain.get_conserved_quantities(vol_id=1, edge=0) |
---|
| 63 | #qr = domain.get_conserved_quantities(vol_id=2, edge=2) |
---|
| 64 | #flux0, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
| 65 | |
---|
| 66 | #Flux across right edge of element 1 |
---|
| 67 | ql = domain.get_conserved_quantities(vol_id=1, vertex=1) |
---|
| 68 | qr = domain.get_conserved_quantities(vol_id=2, vertex=0) |
---|
| 69 | #print 'qr and ql 1' |
---|
| 70 | #print qr |
---|
| 71 | #print ql |
---|
| 72 | flux0, max_speed = flux_function(1.0, ql, qr, zl, zr) |
---|
| 73 | |
---|
| 74 | #Check that flux seen from other triangles is inverse |
---|
| 75 | tmp = qr; qr=ql; ql=tmp |
---|
| 76 | #print 'qr and ql 2' |
---|
| 77 | #print qr |
---|
| 78 | #print ql |
---|
| 79 | #normal = domain.get_normal(2,2) |
---|
| 80 | #flux, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
| 81 | flux, max_speed = flux_function(-1.0, ql, qr, zl, zr) |
---|
| 82 | #print 'fluxes' |
---|
| 83 | #print flux0 |
---|
| 84 | #print flux |
---|
| 85 | assert allclose(flux + flux0, 0.) |
---|
| 86 | |
---|
| 87 | #Flux across upper edge of volume 1 |
---|
| 88 | #normal = domain.get_normal(1,1) |
---|
| 89 | #ql = domain.get_conserved_quantities(vol_id=1, edge=1) |
---|
| 90 | #qr = domain.get_conserved_quantities(vol_id=3, edge=0) |
---|
| 91 | #flux1, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
| 92 | |
---|
| 93 | #Flux across left edge of element 1 |
---|
| 94 | #ql = domain.get_conserved_quantities(vol_id=1, edge=0) |
---|
| 95 | #qr = domain.get_conserved_quantities(vol_id=0, edge=1) |
---|
| 96 | ql = domain.get_conserved_quantities(vol_id=1, vertex=0) |
---|
| 97 | qr = domain.get_conserved_quantities(vol_id=0, vertex=1) |
---|
| 98 | flux1, max_speed = flux_function(-1.0, ql, qr, zl, zr) |
---|
| 99 | |
---|
| 100 | #Check that flux seen from other triangles is inverse |
---|
| 101 | tmp = qr; qr=ql; ql=tmp |
---|
| 102 | #normal = domain.get_normal(3,0) |
---|
| 103 | #flux, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
| 104 | flux, max_speed = flux_function(1.0, ql, qr, zl, zr) |
---|
| 105 | assert allclose(flux + flux1, 0.) |
---|
| 106 | |
---|
| 107 | #Flux across lower left hypotenuse of volume 1 |
---|
| 108 | #normal = domain.get_normal(1,2) |
---|
| 109 | #ql = domain.get_conserved_quantities(vol_id=1, edge=2) |
---|
| 110 | #qr = domain.get_conserved_quantities(vol_id=0, edge=1) |
---|
| 111 | #flux2, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
| 112 | |
---|
| 113 | #Check that flux seen from other triangles is inverse |
---|
| 114 | #tmp = qr; qr=ql; ql=tmp |
---|
| 115 | #normal = domain.get_normal(0,1) |
---|
| 116 | #flux, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
| 117 | #assert allclose(flux + flux2, 0.) |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | #Scale by edgelengths, add up anc check that total flux is zero |
---|
| 121 | #e0 = domain.edgelengths[1, 0] |
---|
| 122 | #e1 = domain.edgelengths[1, 1] |
---|
| 123 | #e2 = domain.edgelengths[1, 2] |
---|
| 124 | |
---|
| 125 | #assert allclose(e0*flux0+e1*flux1+e2*flux2, 0.) |
---|
| 126 | print 'flux0',flux0 |
---|
| 127 | print 'flux1',flux1 |
---|
| 128 | assert allclose(flux0+flux1, 0.) |
---|
| 129 | |
---|
| 130 | #Now check that compute_flux yields zeros as well |
---|
| 131 | domain.compute_fluxes() |
---|
| 132 | |
---|
| 133 | #for name in ['stage', 'xmomentum', 'ymomentum']: |
---|
| 134 | for name in ['stage', 'xmomentum']: |
---|
| 135 | #print name, domain.quantities[name].explicit_update |
---|
| 136 | assert allclose(domain.quantities[name].explicit_update[1], 0) |
---|
| 137 | |
---|
| 138 | def test_1d_solution_I(): |
---|
| 139 | print "TEST 1D-SOLUTION I" |
---|
| 140 | |
---|
| 141 | L = 2000.0 # Length of channel (m) |
---|
| 142 | N = 100 # Number of compuational cells |
---|
| 143 | cell_len = L/N # Origin = 0.0 |
---|
| 144 | |
---|
| 145 | points = zeros(N+1,Float) |
---|
| 146 | for i in range(N+1): |
---|
| 147 | points[i] = i*cell_len |
---|
| 148 | |
---|
| 149 | domain = Domain(points) |
---|
| 150 | domain.order = 2 |
---|
| 151 | def stage(x): |
---|
| 152 | for i in range(len(x)): |
---|
| 153 | if x[i]<=1000.0: |
---|
| 154 | x[i] = 10.0 |
---|
| 155 | else: |
---|
| 156 | x[i] = 5.0 |
---|
| 157 | return x |
---|
| 158 | |
---|
| 159 | domain.set_quantity('stage', stage) |
---|
| 160 | #L = domain.quantities['stage'].vertex_values |
---|
| 161 | #print "Initial Stage" |
---|
| 162 | #print L |
---|
| 163 | |
---|
| 164 | domain.set_boundary({'exterior': Reflective_boundary(domain)}) |
---|
| 165 | |
---|
| 166 | import time |
---|
| 167 | t0 = time.time() |
---|
| 168 | yieldstep = 1.0 |
---|
| 169 | finaltime = 50.0 |
---|
| 170 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
| 171 | #xmom = domain.quantities['xmomentum'] |
---|
| 172 | #xmom = xmom.centroid_values |
---|
| 173 | #stage = domain.quantities['stage'] |
---|
| 174 | #stage = stage.centroid_values |
---|
| 175 | #print 'stage', stage |
---|
| 176 | #print 'xmom', xmom |
---|
| 177 | #for i in range N |
---|
| 178 | #u = xmom/stage |
---|
| 179 | pass |
---|
| 180 | |
---|
| 181 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 182 | |
---|
| 183 | #L = domain.quantities['stage'].vertex_values |
---|
| 184 | #print "Final Stage" |
---|
| 185 | #print L |
---|
| 186 | |
---|
| 187 | C = domain.quantities['stage'].vertex_values |
---|
| 188 | #print C |
---|
| 189 | |
---|
| 190 | f = file('test_solution_Ix.out', 'w') |
---|
| 191 | for i in range(N): |
---|
| 192 | f.write(str(C[i,1])) |
---|
| 193 | f.write("\n") |
---|
| 194 | f.close |
---|