1 | from scipy import sin, cos, sqrt, linspace, pi, dot |
---|
2 | from Numeric import zeros, Float, array |
---|
3 | from gaussPivot import * |
---|
4 | from analytical_prescription import * |
---|
5 | from parameter import * |
---|
6 | import os, time, csv, pprint |
---|
7 | from domain_sudi import * |
---|
8 | from config import g, epsilon |
---|
9 | from rootsearch import * |
---|
10 | from bisect_function import * |
---|
11 | |
---|
12 | #Analytical computations################################################################# |
---|
13 | def root_g(a,b,t): |
---|
14 | dx = 0.01 |
---|
15 | def g(u): |
---|
16 | return u + 2.0*A*pi/T*sin(2.0*pi/T*(t+u)) |
---|
17 | while 1: |
---|
18 | x1,x2 = rootsearch(g,a,b,dx) |
---|
19 | if x1 != None: |
---|
20 | a = x2 |
---|
21 | root = bisect(g,x1,x2,1) |
---|
22 | else: |
---|
23 | break |
---|
24 | return root |
---|
25 | def shore(t): |
---|
26 | a = -1.0 |
---|
27 | b = 1.0 |
---|
28 | #dx = 0.01 |
---|
29 | u = root_g(a,b,t) |
---|
30 | xi = -0.5*u*u + A*cos(2.0*pi/T*(t+u)) |
---|
31 | position = 1.0 + xi |
---|
32 | return position, u |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | #Numerical computations################################################################### |
---|
38 | def newtonRaphson2(f,q,tol=1.0e-13): ##1.0e-9 may be too large |
---|
39 | for i in range(50): |
---|
40 | h = 1.0e-4 ##1.0e-4 may be too large. |
---|
41 | n = len(q) |
---|
42 | jac = zeros((n,n),Float) |
---|
43 | if 1.0+q[0]-x<0.0: |
---|
44 | print "PROBLEM OCCURS.......... 1.0+q[0]-x=",1.0+q[0]-x |
---|
45 | q[0] = x-1.0 +0.001 |
---|
46 | f0 = f(q) |
---|
47 | for i in range(n): |
---|
48 | temp = q[i] |
---|
49 | q[i] = temp + h |
---|
50 | f1 = f(q) |
---|
51 | q[i] = temp |
---|
52 | jac[:,i] = (f1 - f0)/h |
---|
53 | if sqrt(dot(f0,f0)/len(q)) < tol: return q |
---|
54 | dq = gaussPivot(jac,-f0) |
---|
55 | q = q + dq |
---|
56 | if sqrt(dot(dq,dq)) < tol*max(max(abs(q)),1.0): return q |
---|
57 | print 'Too many iterations' |
---|
58 | |
---|
59 | def elevation(X): |
---|
60 | N = len(X) |
---|
61 | z = zeros(N,Float) |
---|
62 | for i in range(N): |
---|
63 | z[i] = (h_0/L)*X[i] - h_0 |
---|
64 | return z |
---|
65 | |
---|
66 | def height(X): |
---|
67 | N = len(X) |
---|
68 | z = zeros(N,Float) |
---|
69 | for i in range(N): |
---|
70 | z[i] = h_0 - (h_0/L)*X[i] |
---|
71 | return z |
---|
72 | |
---|
73 | def velocity(X): |
---|
74 | N = len(X) |
---|
75 | return zeros(N,Float) |
---|
76 | |
---|
77 | boundary = { (0,0): 'left',(N-1,1): 'right'} |
---|
78 | |
---|
79 | domain = Domain(points,boundary) |
---|
80 | domain.order = 2 |
---|
81 | domain.set_timestepping_method('rk2') |
---|
82 | domain.set_CFL(1.0) |
---|
83 | domain.beta = 1.0 |
---|
84 | domain.set_limiter("minmod") |
---|
85 | |
---|
86 | #def wu_at_O_by_formal_expansion(t): |
---|
87 | # W1 = j0(4*pi/T)*cos(2*pi*t/T) |
---|
88 | # U1 = -j1(4*pi/T)*sin(2*pi*t/T) |
---|
89 | # |
---|
90 | # gu = -2*pi/T*j0(4*pi/T)*sin(2*pi*t/T) |
---|
91 | # gw = -2*pi/T*j1(4*pi/T)*cos(2*pi*t/T) |
---|
92 | # fu = gw |
---|
93 | # fw = gu + j1(4*pi/T)*sin(2*pi*t/T) |
---|
94 | # W2 = gu*U1 + gw*W1 - 0.5*U1**2 |
---|
95 | # U2 = fu*U1 + fw*W1 |
---|
96 | # |
---|
97 | # w = W1*A + W2*A*A |
---|
98 | # u = U1*A + U2*A*A |
---|
99 | # return w,u |
---|
100 | |
---|
101 | def wu_at_O_by_fixed_point_iteration(t): |
---|
102 | w1 = A*j0(4.0*pi/T)*cos(2.0*pi*t/T) |
---|
103 | u1 = -1.0*A*j1(4.0*pi/T)*sin(2.0*pi*t/T) |
---|
104 | |
---|
105 | #w2 = -0.5*u1**2.0 + A*j0(4.0*pi*(w1+1.0)**0.5/T)*cos(2.0*pi*(u1+t)/T) |
---|
106 | #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) |
---|
107 | |
---|
108 | #w3 = -0.5*u2**2.0 + A*j0(4.0*pi*(w2+1.0)**0.5/T)*cos(2.0*pi*(u2+t)/T) |
---|
109 | #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) |
---|
110 | |
---|
111 | #w4 = -0.5*u3**2.0 + A*j0(4.0*pi*(w3+1.0)**0.5/T)*cos(2.0*pi*(u3+t)/T) |
---|
112 | #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) |
---|
113 | |
---|
114 | #w5 = -0.5*u4**2.0 + A*j0(4.0*pi*(w4+1.0)**0.5/T)*cos(2.0*pi*(u4+t)/T) |
---|
115 | #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) |
---|
116 | return w1,u1 |
---|
117 | |
---|
118 | def f_SUDI(t): |
---|
119 | timing = t*sqrt(g*h_0)/L |
---|
120 | w, u = wu_at_O_by_fixed_point_iteration(timing) |
---|
121 | wO = w*h_0 |
---|
122 | uO = u*sqrt(g*h_0) |
---|
123 | zO = -h_0 |
---|
124 | hO = wO - zO |
---|
125 | pO = uO * hO |
---|
126 | #[ 'stage', 'xmomentum', 'elevation', 'height', 'velocity'] |
---|
127 | return [wO, pO, zO, hO, uO] |
---|
128 | |
---|
129 | def func(q): #Here q=(w, u) |
---|
130 | f = zeros(2,Float) |
---|
131 | f[0] = q[0] + 0.5*q[1]**2.0 - A*j0(4.0*pi/T*(1.0+q[0]-x)**0.5)*cos(2.0*pi/T*(tim+q[1])) |
---|
132 | f[1] = q[1] + A*j1(4.0*pi/T*(1.0+q[0]-x)**0.5)*sin(2.0*pi/T*(tim+q[1]))/(1+q[0]-x)**0.5 |
---|
133 | return f |
---|
134 | |
---|
135 | |
---|
136 | Ver = domain.vertices |
---|
137 | n_V = len(Ver) |
---|
138 | AnalitW_V = zeros((n_V,2), Float) |
---|
139 | AnalitP_V = zeros((n_V,2), Float) |
---|
140 | AnalitZ_V = zeros((n_V,2), Float) |
---|
141 | AnalitH_V = zeros((n_V,2), Float) |
---|
142 | AnalitU_V = zeros((n_V,2), Float) |
---|
143 | |
---|
144 | T1 = Time_boundary(domain,f_SUDI) |
---|
145 | D2 = Dirichlet_boundary([(h_0/L)*Ver.flat[len(Ver.flat)-1]-h_0, 0.0, (h_0/L)*Ver.flat[len(Ver.flat)-1]-h_0, 0.0, 0.0]) |
---|
146 | domain.set_boundary({'left':T1,'right':D2}) |
---|
147 | |
---|
148 | domain.set_quantity('height',height) |
---|
149 | domain.set_quantity('elevation',elevation) |
---|
150 | domain.set_quantity('velocity',velocity) |
---|
151 | |
---|
152 | Cen = domain.centroids |
---|
153 | n_C = len(Cen) |
---|
154 | AnalitW_C = zeros(n_C, Float) |
---|
155 | AnalitP_C = zeros(n_C, Float) |
---|
156 | AnalitZ_C = zeros(n_C, Float) |
---|
157 | AnalitH_C = zeros(n_C, Float) |
---|
158 | AnalitU_C = zeros(n_C, Float) |
---|
159 | |
---|
160 | waktu = Tp/6.0 |
---|
161 | WAKTU = 14.0*Tp |
---|
162 | yieldstep = finaltime = waktu |
---|
163 | t0 = time.time() |
---|
164 | counter=1 |
---|
165 | |
---|
166 | shorelines_numerical_sudi = zeros(int(WAKTU/waktu), Float) |
---|
167 | time_instants = zeros(int(WAKTU/waktu), Float) |
---|
168 | |
---|
169 | |
---|
170 | while finaltime < WAKTU+0.001: |
---|
171 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
172 | domain.write_time() |
---|
173 | time_instants[counter-1] = domain.time |
---|
174 | |
---|
175 | Stage = domain.quantities['stage'] |
---|
176 | Momentum = domain.quantities['xmomentum'] |
---|
177 | Elevation = domain.quantities['elevation'] |
---|
178 | Height = domain.quantities['height'] |
---|
179 | Velocity = domain.quantities['velocity'] |
---|
180 | |
---|
181 | StageV = Stage.vertex_values |
---|
182 | MomV = Momentum.vertex_values |
---|
183 | ElevationV = Elevation.vertex_values |
---|
184 | HeightV = Height.vertex_values |
---|
185 | VelV = Velocity.vertex_values |
---|
186 | |
---|
187 | StageC = Stage.centroid_values |
---|
188 | MomC = Momentum.centroid_values |
---|
189 | ElevationC = Elevation.centroid_values |
---|
190 | HeightC = Height.centroid_values |
---|
191 | VelC = Velocity.centroid_values |
---|
192 | |
---|
193 | |
---|
194 | #######Numerical C-G at centroids######## |
---|
195 | table = zeros((n_C,6),Float) |
---|
196 | for r in range(n_C): |
---|
197 | for c in range(6): |
---|
198 | if c==0: |
---|
199 | table[r][c] = Cen[r] |
---|
200 | elif c==1: |
---|
201 | table[r][c] = StageC[r] |
---|
202 | elif c==2: |
---|
203 | table[r][c] = MomC[r] |
---|
204 | elif c==3: |
---|
205 | table[r][c] = ElevationC[r] |
---|
206 | elif c==4: |
---|
207 | table[r][c] = HeightC[r] |
---|
208 | else: |
---|
209 | table[r][c] = VelC[r] |
---|
210 | |
---|
211 | outname = "%s%04i%s%f%s" %("900_w1u1_Cen_numerical_", counter, "_", domain.time, ".csv") |
---|
212 | outfile = open(outname, 'w') |
---|
213 | writer = csv.writer(outfile) |
---|
214 | for row in table: |
---|
215 | writer.writerow(row) |
---|
216 | outfile.close() |
---|
217 | |
---|
218 | |
---|
219 | #######Numerical C-G at vertices######## |
---|
220 | table = zeros((len(Ver.flat),6),Float) |
---|
221 | for r in range(len(Ver.flat)): |
---|
222 | for c in range(6): |
---|
223 | if c==0: |
---|
224 | table[r][c] = Ver.flat[r] |
---|
225 | elif c==1: |
---|
226 | table[r][c] = StageV.flat[r] |
---|
227 | elif c==2: |
---|
228 | table[r][c] = MomV.flat[r] |
---|
229 | elif c==3: |
---|
230 | table[r][c] = ElevationV.flat[r] |
---|
231 | elif c==4: |
---|
232 | table[r][c] = HeightV.flat[r] |
---|
233 | else: |
---|
234 | table[r][c] = VelV.flat[r] |
---|
235 | |
---|
236 | outname = "%s%04i%s%f%s" %("900_w1u1_Ver_numerical_", counter, "_", domain.time, ".csv") |
---|
237 | outfile = open(outname, 'w') |
---|
238 | writer = csv.writer(outfile) |
---|
239 | for row in table: |
---|
240 | writer.writerow(row) |
---|
241 | outfile.close() |
---|
242 | |
---|
243 | |
---|
244 | #######Numerical shoreline######## |
---|
245 | for s in range(2*n_V): |
---|
246 | heiL = HeightV.flat[s] |
---|
247 | momR = MomV.flat[s+1] |
---|
248 | if heiL >= 1e-6: |
---|
249 | if abs(momR)==0.0: #<1e-15: |
---|
250 | break |
---|
251 | shorelines_numerical_sudi[counter-1] = Ver.flat[s+1] |
---|
252 | |
---|
253 | |
---|
254 | #######Setting for the next loop######## |
---|
255 | counter = counter+1 |
---|
256 | finaltime = finaltime + waktu |
---|
257 | |
---|
258 | |
---|
259 | ########Saving the numerical shoreline######### |
---|
260 | table = zeros((int(WAKTU/waktu), 2),Float) |
---|
261 | for r in range(int(WAKTU/waktu)): |
---|
262 | for c in range(2): |
---|
263 | if c==0: |
---|
264 | table[r][c] = time_instants[r] |
---|
265 | else: |
---|
266 | table[r][c] = shorelines_numerical_sudi[r] |
---|
267 | outname = "%s" %("900_w1u1_shore_numerical.csv") |
---|
268 | outfile = open(outname, 'w') |
---|
269 | writer = csv.writer(outfile) |
---|
270 | for row in table: |
---|
271 | writer.writerow(row) |
---|
272 | outfile.close() |
---|
273 | |
---|
274 | |
---|