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 stage(x): |
---|
29 | |
---|
30 | y = zeros(len(x),Float) |
---|
31 | for i in range(len(x)): |
---|
32 | y[i]=8.0 |
---|
33 | return y |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | import time |
---|
39 | |
---|
40 | # Set final time and yield time for simulation |
---|
41 | finaltime = 10.0 |
---|
42 | yieldstep = finaltime |
---|
43 | |
---|
44 | # Length of channel (m) |
---|
45 | L = 1000.0 |
---|
46 | # Define the number of cells |
---|
47 | number_of_cells = [50] |
---|
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 | # Define random array for width |
---|
63 | |
---|
64 | randomarray=zeros(len(points),Float) |
---|
65 | for j in range(N+1): |
---|
66 | randomarray[j]=random.normalvariate(3,1) |
---|
67 | |
---|
68 | |
---|
69 | # Set initial values of quantities - default to zero |
---|
70 | domain.set_quantity('stage',stage) |
---|
71 | #domain.set_quantity('area', initial_area) |
---|
72 | domain.set_quantity('width',width) |
---|
73 | domain.setstageflag = True |
---|
74 | # Set boundry type, order, timestepping method and limiter |
---|
75 | domain.set_boundary({'exterior':Reflective_boundary(domain)}) |
---|
76 | domain.order = 2 |
---|
77 | domain.set_timestepping_method('rk2') |
---|
78 | domain.set_CFL(1.0) |
---|
79 | domain.set_limiter("vanleer") |
---|
80 | #domain.h0=0.0001 |
---|
81 | |
---|
82 | # Start timer |
---|
83 | t0 = time.time() |
---|
84 | |
---|
85 | print domain.quantities['elevation'].vertex_values |
---|
86 | |
---|
87 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
88 | domain.write_time() |
---|
89 | |
---|
90 | print domain.quantities['elevation'].vertex_values |
---|
91 | |
---|
92 | N = float(N) |
---|
93 | HeightC = domain.quantities['height'].centroid_values |
---|
94 | DischargeC = domain.quantities['discharge'].centroid_values |
---|
95 | C = domain.centroids |
---|
96 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
97 | X = domain.vertices |
---|
98 | HeightQ = domain.quantities['height'].vertex_values |
---|
99 | VelocityQ = domain.quantities['velocity'].vertex_values |
---|
100 | x = X.flat |
---|
101 | z = domain.quantities['width'].vertex_values.flat |
---|
102 | stage=HeightQ.flat+0 |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | from pylab import plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot |
---|
107 | |
---|
108 | #hold(False) |
---|
109 | |
---|
110 | plot1=subplot(211) |
---|
111 | |
---|
112 | plot(x,stage,x,z) |
---|
113 | |
---|
114 | plot1.set_ylim([-1,11]) |
---|
115 | xlabel('Position') |
---|
116 | ylabel('Stage') |
---|
117 | legend(('Solution', 'Width'), |
---|
118 | 'upper right', shadow=True) |
---|
119 | |
---|
120 | plot2=subplot(212) |
---|
121 | plot(x,VelocityQ.flat) |
---|
122 | plot2.set_ylim([-5,5]) |
---|
123 | xlabel('Position') |
---|
124 | ylabel('Velocity') |
---|
125 | |
---|
126 | show() |
---|
127 | |
---|