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