1 | import os, time, csv, pprint |
---|
2 | from scipy.special import jn |
---|
3 | from scipy import sin, cos, sqrt, linspace, pi, zeros |
---|
4 | from rootsearch import * |
---|
5 | from bisect_function import * |
---|
6 | from Numeric import zeros,Float,dot |
---|
7 | from gaussPivot import * |
---|
8 | from config import g |
---|
9 | from analytical_prescription import * |
---|
10 | |
---|
11 | |
---|
12 | def j0(x): |
---|
13 | return jn(0.0, x) |
---|
14 | |
---|
15 | def j1(x): |
---|
16 | return jn(1.0, x) |
---|
17 | |
---|
18 | def j2(x): |
---|
19 | return jn(2.0, x) |
---|
20 | |
---|
21 | def j3(x): |
---|
22 | return jn(3.0, x) |
---|
23 | |
---|
24 | def jm1(x): |
---|
25 | return jn(-1.0, x) |
---|
26 | |
---|
27 | def jm2(x): |
---|
28 | return jn(-2.0, x) |
---|
29 | |
---|
30 | def bed(x): |
---|
31 | return x-1.0 |
---|
32 | |
---|
33 | |
---|
34 | def wu_at_O_by_formal_expansion(t): |
---|
35 | W1 = j0(4*pi/T)*cos(2*pi*t/T) |
---|
36 | U1 = -j1(4*pi/T)*sin(2*pi*t/T) |
---|
37 | |
---|
38 | gu = -2*pi/T*j0(4*pi/T)*sin(2*pi*t/T) |
---|
39 | gw = -2*pi/T*j1(4*pi/T)*cos(2*pi*t/T) |
---|
40 | fu = gw |
---|
41 | fw = gu + j1(4*pi/T)*sin(2*pi*t/T) |
---|
42 | W2 = gu*U1 + gw*W1 - 0.5*U1**2 |
---|
43 | U2 = fu*U1 + fw*W1 |
---|
44 | |
---|
45 | w = W1*A + W2*A*A |
---|
46 | u = U1*A + U2*A*A |
---|
47 | return w,u |
---|
48 | |
---|
49 | def wu_at_O_by_fixed_point_iteration(t): |
---|
50 | w1 = A*j0(4.0*pi/T)*cos(2.0*pi*t/T) |
---|
51 | u1 = -1.0*A*j1(4.0*pi/T)*sin(2.0*pi*t/T) |
---|
52 | |
---|
53 | w2 = -0.5*u1**2.0 + A*j0(4.0*pi*(w1+1.0)**0.5/T)*cos(2.0*pi*(u1+t)/T) |
---|
54 | u2 = -1.0*A*j1(4.0*pi*(w1+1.0)**0.5/T)*(w1+1.0)**-0.5*sin(2.0*pi*(u1+t)/T) |
---|
55 | |
---|
56 | #w3 = -0.5*u2**2.0 + A*j0(4.0*pi*(w2+1.0)**0.5/T)*cos(2.0*pi*(u2+t)/T) |
---|
57 | #u3 = -1.0*A*j1(4.0*pi*(w2+1.0)**0.5/T)*(w2+1.0)**-0.5*sin(2.0*pi*(u2+t)/T) |
---|
58 | |
---|
59 | #w4 = -0.5*u3**2.0 + A*j0(4.0*pi*(w3+1.0)**0.5/T)*cos(2.0*pi*(u3+t)/T) |
---|
60 | #u4 = -1.0*A*j1(4.0*pi*(w3+1.0)**0.5/T)*(w3+1.0)**-0.5*sin(2.0*pi*(u3+t)/T) |
---|
61 | |
---|
62 | #w5 = -0.5*u4**2.0 + A*j0(4.0*pi*(w4+1.0)**0.5/T)*cos(2.0*pi*(u4+t)/T) |
---|
63 | #u5 = -1.0*A*j1(4.0*pi*(w4+1.0)**0.5/T)*(w4+1.0)**-0.5*sin(2.0*pi*(u4+t)/T) |
---|
64 | return w2,u2 |
---|
65 | |
---|
66 | |
---|
67 | Time = linspace(0.0,T,1000) |
---|
68 | N_T = len(Time) |
---|
69 | |
---|
70 | |
---|
71 | Stage = zeros(N_T, Float) |
---|
72 | Veloc = zeros(N_T, Float) |
---|
73 | for i in range(N_T): |
---|
74 | t=Time[i] |
---|
75 | #print "Here.................................t=",t |
---|
76 | zet, vel = prescribe(0.0,t) |
---|
77 | Stage[i] = zet |
---|
78 | Veloc[i] = vel |
---|
79 | |
---|
80 | |
---|
81 | Stage_sudi = zeros(N_T, Float) |
---|
82 | Veloc_sudi = zeros(N_T, Float) |
---|
83 | for i in range(N_T): |
---|
84 | t=Time[i] |
---|
85 | Stage_sudi[i],Veloc_sudi[i] = wu_at_O_by_formal_expansion(t)#wu_at_O_by_fixed_point_iteration(t) |
---|
86 | |
---|
87 | |
---|
88 | num=len(Stage) |
---|
89 | error_w=(1.0/num)*sum(abs(Stage-Stage_sudi))*h_0 |
---|
90 | error_u=(1.0/num)*sum(abs(Veloc-Veloc_sudi))*sqrt(g*h_0) |
---|
91 | print "error_w=", error_w |
---|
92 | print "error_u=", error_u |
---|
93 | |
---|
94 | #######Numerical C-G at centroids######## |
---|
95 | table = zeros((num,3),Float) |
---|
96 | for r in range(num): |
---|
97 | for c in range(3): |
---|
98 | if c==0: |
---|
99 | table[r][c] = Time[r] |
---|
100 | elif c==1: |
---|
101 | table[r][c] = Stage[r] |
---|
102 | else: |
---|
103 | table[r][c] = Veloc[r] |
---|
104 | |
---|
105 | outname = "%s%f%s" %("Exact_at_O_", Tp, ".csv") |
---|
106 | outfile = open(outname, 'w') |
---|
107 | writer = csv.writer(outfile) |
---|
108 | for row in table: |
---|
109 | writer.writerow(row) |
---|
110 | outfile.close() |
---|
111 | |
---|
112 | |
---|
113 | #######Numerical stage and velocity######## |
---|
114 | table = zeros((num,3),Float) |
---|
115 | for r in range(num): |
---|
116 | for c in range(3): |
---|
117 | if c==0: |
---|
118 | table[r][c] = Time[r] |
---|
119 | elif c==1: |
---|
120 | table[r][c] = Stage_sudi[r] |
---|
121 | else: |
---|
122 | table[r][c] = Veloc_sudi[r] |
---|
123 | |
---|
124 | outname = "%s%f%s" %("Numeric_w2u2_FE_at_O_", Tp, ".csv") |
---|
125 | outfile = open(outname, 'w') |
---|
126 | writer = csv.writer(outfile) |
---|
127 | for row in table: |
---|
128 | writer.writerow(row) |
---|
129 | outfile.close() |
---|
130 | |
---|
131 | from pylab import clf,plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot |
---|
132 | |
---|
133 | hold(False) |
---|
134 | clf() |
---|
135 | plot1 = subplot(211) |
---|
136 | plot(Time/T,Stage*h_0,'b-', Time/T,Stage_sudi*h_0,'k--') |
---|
137 | #xlabel('t/T') |
---|
138 | ylabel('Stage') |
---|
139 | #plot1.set_xlim([0.000,0.030]) |
---|
140 | #plot1.set_ylim([0.980,1.005]) #([-9.0e-3,9.0e-3]) |
---|
141 | legend(('C-G', 'M-R'), |
---|
142 | 'lower left', shadow=False) |
---|
143 | |
---|
144 | plot2 = subplot(212) |
---|
145 | plot(Time/T,Veloc*sqrt(g*h_0),'b-', Time/T,Veloc_sudi*sqrt(g*h_0),'k--') |
---|
146 | xlabel('t/T') |
---|
147 | ylabel('Velocity') |
---|
148 | #plot1.set_xlim([0.0,1.1]) |
---|
149 | #plot2.set_ylim([-0.05,0.05]) #([-1.0e-12,1.0e-12]) |
---|
150 | legend(('C-G', 'M-R'), |
---|
151 | 'upper right', shadow=False) |
---|
152 | |
---|
153 | |
---|
154 | #filename = "discrepancy-closer" |
---|
155 | #filename += str(i) |
---|
156 | #filename += ".eps" |
---|
157 | #savefig(filename) |
---|
158 | #show() |
---|
159 | |
---|
160 | #plot(Time,Vel_at_O) |
---|
161 | show() |
---|