[6453] | 1 | import os |
---|
| 2 | import random |
---|
| 3 | from math import sqrt, pow, pi |
---|
| 4 | from channel_domain_Ab import * |
---|
| 5 | from Numeric import allclose, array, zeros, ones, Float, take, sqrt |
---|
| 6 | from config import g, epsilon |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | print "Variable Width Only Test" |
---|
| 10 | |
---|
| 11 | # Define functions for initial quantities |
---|
| 12 | def initial_area(x): |
---|
| 13 | y = zeros(len(x),Float) |
---|
| 14 | for i in range(len(x)): |
---|
| 15 | #if x[i]>=500: |
---|
| 16 | # y[i]=0 |
---|
| 17 | #else: |
---|
| 18 | # if x[i]<900: |
---|
| 19 | y[i]=(5-bed([x[i]]))*bed([x[i]]) |
---|
| 20 | # else: |
---|
| 21 | # y[i]=10-bed([x[i]]) |
---|
| 22 | return y |
---|
| 23 | |
---|
| 24 | def bed(x): |
---|
| 25 | y = zeros(len(x),Float) |
---|
| 26 | for i in range(len(x)): |
---|
| 27 | if x[i]>100: |
---|
| 28 | y[i]=2 |
---|
| 29 | else: |
---|
| 30 | y[i]=1 |
---|
| 31 | return y |
---|
| 32 | |
---|
| 33 | def width(x): |
---|
| 34 | return 1 |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | import time |
---|
| 39 | |
---|
| 40 | # Set final time and yield time for simulation |
---|
| 41 | finaltime =100.0 |
---|
| 42 | yieldstep = finaltime |
---|
| 43 | |
---|
| 44 | # Length of channel (m) |
---|
| 45 | L = 1000.0 |
---|
| 46 | # Define the number of cells |
---|
| 47 | number_of_cells = [10] |
---|
| 48 | |
---|
| 49 | # Define cells for finite volume and their size |
---|
| 50 | N = int(number_of_cells[0]) |
---|
| 51 | print "Evaluating domain with %d cells" %N |
---|
| 52 | cell_len = L/N # Origin = 0.0 |
---|
| 53 | points = zeros(N+1,Float) |
---|
| 54 | |
---|
| 55 | # Define the centroid points |
---|
| 56 | for j in range(N+1): |
---|
| 57 | points[j] = j*cell_len |
---|
| 58 | |
---|
| 59 | # Create domain with centroid points as defined above |
---|
| 60 | domain = Domain(points) |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | # Set initial values of quantities - default to zero |
---|
| 64 | domain.set_quantity('area', initial_area) |
---|
| 65 | domain.set_quantity('elevation',bed) |
---|
| 66 | domain.set_quantity('width',bed) |
---|
| 67 | |
---|
| 68 | # Set boundry type, order, timestepping method and limiter |
---|
| 69 | domain.set_boundary({'exterior':Reflective_boundary(domain)}) |
---|
| 70 | domain.order = 2 |
---|
| 71 | domain.set_timestepping_method('rk2') |
---|
| 72 | domain.set_CFL(1.0) |
---|
| 73 | domain.set_limiter("vanleer") |
---|
| 74 | #domain.h0=0.0001 |
---|
| 75 | |
---|
| 76 | # Start timer |
---|
| 77 | t0 = time.time() |
---|
| 78 | |
---|
| 79 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
| 80 | domain.write_time() |
---|
| 81 | |
---|
| 82 | N = float(N) |
---|
| 83 | HeightC = domain.quantities['height'].centroid_values |
---|
| 84 | DischargeC = domain.quantities['discharge'].centroid_values |
---|
| 85 | C = domain.centroids |
---|
| 86 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 87 | X = domain.vertices |
---|
| 88 | HeightQ = domain.quantities['height'].vertex_values |
---|
| 89 | VelocityQ = domain.quantities['velocity'].vertex_values |
---|
| 90 | x = X.flat |
---|
| 91 | z = domain.quantities['elevation'].vertex_values.flat |
---|
| 92 | stage=HeightQ.flat+z |
---|
| 93 | b = domain.quantities['width'].vertex_values.flat |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | from pylab import plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot |
---|
| 97 | |
---|
| 98 | hold(False) |
---|
| 99 | |
---|
| 100 | plot1 = subplot(211) |
---|
| 101 | |
---|
| 102 | plot(x,z,x,stage,x,b) |
---|
| 103 | |
---|
| 104 | plot1.set_ylim([-1,11]) |
---|
| 105 | xlabel('Position') |
---|
| 106 | ylabel('Stage') |
---|
| 107 | ## legend(('Analytical Solution', 'Numerical Solution'), |
---|
| 108 | ## 'upper right', shadow=True) |
---|
| 109 | plot2 = subplot(212) |
---|
| 110 | plot(x,VelocityQ.flat) |
---|
| 111 | plot2.set_ylim([-10,10]) |
---|
| 112 | |
---|
| 113 | xlabel('Position') |
---|
| 114 | ylabel('Velocity') |
---|
| 115 | |
---|
| 116 | show() |
---|