-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework_3_final.py
More file actions
391 lines (257 loc) · 7.32 KB
/
Homework_3_final.py
File metadata and controls
391 lines (257 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python
# coding: utf-8
# ## AMATH Scientific Computing
# ### Homework-3
# #### Manjaree Binjolkar
# In[1]:
import numpy as np
import scipy.sparse
import matplotlib.pyplot as plt
import pdb
import numpy.matlib
import time
import copy
from matplotlib import animation, rc
from scipy.sparse.linalg import spsolve
from scipy.integrate import solve_ivp
from scipy.sparse.linalg import splu
from scipy.sparse import csr_matrix, triu, spdiags
# In[2]:
#conda install -c conda-forge ffmpeg
# ### Q1
# (a) Setup the matrix $A ≈ ∂x$ using second-order central differences and sparse
# storage.
#
# (b) Solve the advection equation with $c(x,t) = −0.5$, this corresponds to a constant velocity field. In other words, solve $ut= 0.5Au $.
# In[3]:
#define our initial condition
f = lambda x: np.exp(-(x-5)**2) #gaussian initial condition
L = 10
dx = 0.1
x = np.arange(-L,L,0.1)
#print(x)
N = len(x)
dt = 0.5 #step size
times = np.arange(0,10+dt, dt)
#print(times)
#np.shape(times)
#type(times)
#times
c = -0.5
#function for adevction equation
def advecPDE(t, x, A, c):
u_t = -c*A*x #time derivative
return u_t
#creating sparse matrix A
e = np.ones((N))
Bin = np.array([5*e, -5*e, -0*e, 5*e, -5*e])
d = np.array([-N+1, -1, 0, 1, N-1])
A = spdiags(Bin, d, N, N)
#print(A.toarray()[0:4,0:4])
#print(A.toarray())
A1 = copy.deepcopy(A.todense())
#print(A1)
#A = A/(dx*2) #from piazza discussion
#print(A.toarray())
#plt.spy(A)
y0 = f(x)
tic =time.time()
sol =solve_ivp(lambda t,x: advecPDE(t,x,A,c), [0,10], y0, t_eval = times)
toc = time.time()
#print(toc-tic)
#print('x = ',np.shape(x))
#print('y = ',np.shape(sol.y))
#print('t = ',np.shape(sol.t))
A2 = copy.deepcopy(sol.y)
#X, T = np.meshgrid(x,sol.t)
#fig, ax = plt.subplots(subplot_kw = {"projection":"3d"}, figsize =(25,10))
#surf = ax.plot_surface(X,T,sol.y.T, cmap='magma')
#ax.plot3D(x,0*x,f(x),'-r',linewidth=5)
#plt.xlabel('x')
#plt.ylabel('time')
#plt.show()
# In[4]:
#Check
#np.shape(A1)
#np.shape(A2)
# c) Solve the advection equation with
# $c(x,t) = −(1 + 2 sin(5t) −H(x −4))$ ,
# where
# $H(z) =
# 1, z > 0
# ;
# 0, z ≤0$
# In[5]:
#A.toarray()
#print(x)
#print(times)
# In[6]:
#define new function for c
def c_func(t,x):
c = 1+2*np.sin(5*t)-(np.heaviside((x-4), 0))
return c
def advecPDE_c(t, u, A, x):
u_t = np.multiply(c_func(t,x),A@u) #time derivative
return u_t
#print(y0)
tic = time.time()
sol = solve_ivp(lambda t,u : advecPDE_c(t, u, A, x), [0,10], y0, t_eval = times)
toc = time.time()
#print(toc-tic)
A3 = copy.deepcopy(sol.y)
#X, T = np.meshgrid(x,sol.t)
#fig, ax = plt.subplots(subplot_kw = {"projection":"3d"}, figsize =(25,10))
#surf = ax.plot_surface(X,T,sol.y.T, cmap='magma')
#ax.plot3D(x,0*x,f(x),'-r',linewidth=5)
#plt.xlabel('x')
#plt.ylabel('time')
#plt.show()
# In[7]:
#Check
#np.shape(A3)
# ### Q2
# (a) Use sparse-matrix storage (spdiags) to generate the three matrices $ A = r2 =
# @2
# x + @2
# y , B = @x, and C = @y$ which take derivative in two dimensions. Use
# the discretization of the domain and its transformation to a vector as in class.
# Use the second-order central di erence formula for each matrix. To make sure
# that A is nonsingular, set the rst element of A ( rst row, rst column) equal
# to 2 instead of the usual -4
# In[8]:
#setting up
m = 64
n = m*m
x = np.linspace(-L, L, m, endpoint = False)
#x = x[0:-1]
y = np.linspace(-L, L, m, endpoint = False)
#y =y[0:-1]
dx = x[1] - x[0]
e1 =np.ones((n))
l1 =np.concatenate((np.ones(m-1), np.array([0])))
Low1 = np.matlib.repmat(l1,1,m).reshape(n)
l2 =np.concatenate((np.array([1]), np.zeros(m-1)))
Low2 = np.matlib.repmat(l2,1,m).reshape(n)
Up1 = np.roll(Low1,1)
Up2 = np.roll(Low2, m-1)
# In[9]:
#Constructing A
Bin_A = np.array((e1, e1, Low2, Low1, -4*e1, Up1, Up2, e1, e1))
d_A = np.array([-(n-m), -m, -m+1, -1, 0, 1, m-1, m , (n-m)])
A = spdiags(Bin_A, d_A, n, n, format ='csc')
A[0,0] = 2
A = A/(dx**2)
#plt.spy(A)
#print(A.toarray())
A4 = copy.deepcopy(A.todense())
# In[10]:
#Constructing B
Bin_B = np.array((e1, -e1, e1, -e1))
d_B = np.array([-(m**2-m), -m, m, (m**2-m)])
B = spdiags(Bin_B, d_B, n, n, format ='csc')
B = B/(2*dx)
#print(np.shape(B))
#print(B.toarray()[4:16,4:16])
#plt.spy(B)
#print(B.toarray())
A5 = copy.deepcopy(B.todense())
# In[11]:
#Constructing C
Bin_C = np.array((Low2, -Low1, Up1, -Up2))
#d = np.array([ -(m-1), -1, 1, (m-1) ])
#d_C = np.array([ -3, -1, 1, 3 ])
d_C = np.array([ -(m-1), -1, 1, (m-1) ])
C = spdiags(Bin_C, d_C, n, n, format ='csc')
C = C/(dx*2)
#plt.spy(C)
#print(np.shape(C))
#print(C.toarray()[0:4,0:4])
#plt.spy(C)
#print(C.toarray())
A6 = copy.deepcopy(C.todense())
# In[12]:
#Check
#print(np.shape(A4))
#print(np.shape(A5))
#print(np.shape(A6))
# (b) Now we want to integrate the equations numerically in time. Start by dis-
# cretizing (2), using the matrices above, so that it is a linear problem.
# Then discretize (1) using the matrices created above and use the built-in RK45
# algorithm to step forward in time (your time step will include both solving
# and the ODE (1)). In each step you will need to solve the linear
# system $A~x =~b$. Do this in two separate ways:
#
# i. using Gaussian Elimination (A\b in Matlab or
# scipy.sparse.linalg.spsolve in python)
#
# ii. using LU decomposition. When using LU decomposition, make sure to
# create the matrices L; U; and P outside of the function and pass the decomposition into the function for a fast solve. If using python, use
# scipy.sparse.linalg.splu.
# In[13]:
#defining the initial values
nu = 0.001
L = 10
x = np.linspace(-L, L, 64, endpoint = False)
#x = x[0:-1]
y = np.linspace(-L, L, 64, endpoint = False)
#y = y[0:-1]
#making the meshgrid
yv, xv = np.meshgrid(x, y)
#setting up t
dt = 0.5
t = np.arange(0,4.5,dt)
#print(len(t))
#defining inital condition for omega
omega_0 = lambda x,y: np.exp(-2*(x**2)-(y**2)/20)
omega_0_val = omega_0(xv,yv).reshape(64*64)
#print(np.shape(omega_0_val))
#print(omega_0_val)
def rhs_ode_GE(t, omega, A, B, C):
#solving for A.psi = w using GE
#psi0 = spsolve(A,omega_0)
psi = spsolve(A,omega)
psi_x = B@psi
psi_y = C@psi
omega_x = B@omega
omega_y = C@omega
omega = nu*(A)@omega - np.multiply(psi_x,omega_y) + np.multiply(psi_y,omega_x)
return omega
def rhs_ode_LU(t, omega, A, B, C):
#solving for A.psi = w using GE
#psi0 = spsolve(A,omega_0)
plu = splu(A)
psi = plu.solve(omega)
psi_x = B@psi
psi_y = C@psi
omega_x = B@omega
omega_y = C@omega
omega = nu*(A)@omega - np.multiply(psi_x,omega_y) + np.multiply(psi_y,omega_x)
return omega
# In[14]:
#%%time
sol_GE = scipy.integrate.solve_ivp(lambda t,omega: rhs_ode_GE(t,omega, A, B, C), [0, 4],
omega_0_val, t_eval = np.arange(0, 4.5, 0.5))
A7 = copy.deepcopy(sol_GE.y.T)
# In[15]:
#%%time
sol_LU = scipy.integrate.solve_ivp(lambda t,omega: rhs_ode_LU(t,omega, A, B, C), [0, 4],
omega_0_val, t_eval = np.arange(0, 4.5, 0.5))
A8 = copy.deepcopy(sol_GE.y.T)
# In[16]:
A9 = copy.deepcopy(A8.reshape(9,64,64))
# In[17]:
#Check
#print(np.shape(A7))
#print(np.shape(A8))
#print(np.shape(A9))
# In[18]:
#plt.spy(C)
# In[23]:
#A6
# In[24]:
#A7
# In[25]:
#A8
# In[22]:
#print(omega_0_val)
# In[ ]: