[7884] | 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 | y[i]=(10-randomarray[i]) |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | return y |
---|
| 19 | |
---|
| 20 | def width(x): |
---|
| 21 | |
---|
| 22 | y = zeros(len(x),Float) |
---|
| 23 | for i in range(len(x)): |
---|
| 24 | y[i]=randomarray[i] |
---|
| 25 | randomarray[i]=random.normalvariate(3,1) |
---|
| 26 | return y |
---|
| 27 | |
---|
| 28 | def bed(x): |
---|
| 29 | |
---|
| 30 | y = zeros(len(x),Float) |
---|
| 31 | for i in range(len(x)): |
---|
| 32 | y[i]=randomarray2[i] |
---|
| 33 | randomarray2[i]=random.normalvariate(3,1) |
---|
| 34 | return y |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | def stage(x): |
---|
| 39 | |
---|
| 40 | y = zeros(len(x),Float) |
---|
| 41 | for i in range(len(x)): |
---|
| 42 | if x[i]<200 and x[i]>150: |
---|
| 43 | y[i]=8.0 |
---|
| 44 | else: |
---|
| 45 | y[i]=8.0 |
---|
| 46 | return y |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | import time |
---|
| 52 | |
---|
| 53 | # Set final time and yield time for simulation |
---|
| 54 | finaltime = 10.0 |
---|
| 55 | yieldstep = finaltime |
---|
| 56 | |
---|
| 57 | # Length of channel (m) |
---|
| 58 | L = 1000.0 |
---|
| 59 | # Define the number of cells |
---|
| 60 | number_of_cells = [100] |
---|
| 61 | |
---|
| 62 | # Define cells for finite volume and their size |
---|
| 63 | N = int(number_of_cells[0]) |
---|
| 64 | print "Evaluating domain with %d cells" %N |
---|
| 65 | cell_len = L/N # Origin = 0.0 |
---|
| 66 | points = zeros(N+1,Float) |
---|
| 67 | |
---|
| 68 | # Define the centroid points |
---|
| 69 | for j in range(N+1): |
---|
| 70 | points[j] = j*cell_len |
---|
| 71 | |
---|
| 72 | # Create domain with centroid points as defined above |
---|
| 73 | domain = Domain(points) |
---|
| 74 | |
---|
| 75 | # Define random array for width |
---|
| 76 | randomarray=zeros(len(points),Float) |
---|
| 77 | for j in range(N+1): |
---|
| 78 | randomarray[j]=random.normalvariate(3,1) |
---|
| 79 | randomarray2=zeros(len(points),Float) |
---|
| 80 | for j in range(N+1): |
---|
| 81 | randomarray2[j]=random.normalvariate(3,1) |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | # Set initial values of quantities - default to zero |
---|
| 85 | domain.set_quantity('stage',stage,'centroids') |
---|
| 86 | domain.set_quantity('elevation', bed) |
---|
| 87 | domain.set_quantity('width',width) |
---|
| 88 | domain.setstageflag = True |
---|
| 89 | # Set boundry type, order, timestepping method and limiter |
---|
| 90 | domain.set_boundary({'exterior':Reflective_boundary(domain)}) |
---|
| 91 | domain.order = 2 |
---|
| 92 | domain.set_timestepping_method('rk2') |
---|
| 93 | domain.set_CFL(1.0) |
---|
| 94 | domain.set_limiter("vanleer") |
---|
| 95 | #domain.h0=0.0001 |
---|
| 96 | |
---|
| 97 | # Start timer |
---|
| 98 | t0 = time.time() |
---|
| 99 | |
---|
| 100 | print domain.quantities['elevation'].vertex_values |
---|
| 101 | |
---|
| 102 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
| 103 | domain.write_time() |
---|
| 104 | |
---|
| 105 | print domain.quantities['elevation'].vertex_values |
---|
| 106 | |
---|
| 107 | N = float(N) |
---|
| 108 | HeightC = domain.quantities['height'].centroid_values |
---|
| 109 | DischargeC = domain.quantities['discharge'].centroid_values |
---|
| 110 | C = domain.centroids |
---|
| 111 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 112 | X = domain.vertices |
---|
| 113 | HeightQ = domain.quantities['height'].vertex_values |
---|
| 114 | VelocityQ = domain.quantities['velocity'].vertex_values |
---|
| 115 | x = X.flat |
---|
| 116 | z = domain.quantities['elevation'].vertex_values.flat |
---|
| 117 | b = domain.quantities['width'].vertex_values.flat |
---|
| 118 | stage=HeightQ.flat+z |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | from pylab import plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot |
---|
| 123 | |
---|
| 124 | #hold(False) |
---|
| 125 | |
---|
| 126 | plot1=subplot(211) |
---|
| 127 | |
---|
| 128 | plot(x,stage,x,z,x,b) |
---|
| 129 | |
---|
| 130 | plot1.set_ylim([-5,15]) |
---|
| 131 | xlabel('Position') |
---|
| 132 | ylabel('Stage') |
---|
| 133 | legend(('Solution', 'Bed','Width'), |
---|
| 134 | 'upper right', shadow=True) |
---|
| 135 | |
---|
| 136 | plot2=subplot(212) |
---|
| 137 | plot(x,VelocityQ.flat) |
---|
| 138 | plot2.set_ylim([-10,10]) |
---|
| 139 | xlabel('Position') |
---|
| 140 | ylabel('Velocity') |
---|
| 141 | |
---|
| 142 | show() |
---|
| 143 | |
---|