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