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