Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

Commit 2d6f096

Browse files
author
Jordi
committed
Solutions Diffusion Class
Solution to exercises worked during the class.
1 parent 5b5ff38 commit 2d6f096

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
"""
3+
Solution of a 1D Poisson equation: -u_xx = f
4+
Domain: [0,1]
5+
BC: u(0) = u(1) = 0
6+
with f = (3*x + x^2)*exp(x)
7+
8+
Analytical solution: -x*(x-1)*exp(x)
9+
10+
Finite differences (FD) discretization: second-order diffusion operator
11+
12+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13+
Change boundary conditions
14+
15+
"""
16+
__author__ = 'Jordi Vila-Pérez'
17+
__email__ = 'jvilap@mit.edu'
18+
19+
20+
import numpy as np
21+
import matplotlib.pyplot as plt
22+
import matplotlib.animation as animation #We have to load this
23+
from math import pi
24+
%matplotlib qt
25+
plt.close()
26+
27+
"Number of points"
28+
N = 8
29+
Dx = 1/N
30+
x = np.linspace(0,1,N+1)
31+
32+
"Dirichlet boundary condition at x=0, x=1"
33+
u0 = 0
34+
35+
"System matrix and RHS term"
36+
A = (1/Dx**2)*(2*np.diag(np.ones(N+1)) - np.diag(np.ones(N),-1) - np.diag(np.ones(N),1))
37+
F = (3*x + x**2)*np.exp(x)
38+
39+
"Boundary condition at x=0"
40+
A[0,:] = np.concatenate(([1], np.zeros(N)))
41+
F[0] = u0
42+
43+
"Boundary condition at x=0"
44+
A[N,:] = np.concatenate((np.zeros(N),[1]))
45+
F[N] = u0
46+
47+
"Solution of the linear system AU=F"
48+
u = np.linalg.solve(A,F)
49+
ua = -x*(x-1)*np.exp(x)+u0
50+
51+
"Plotting solution"
52+
plt.plot(x,ua,'-r',linewidth=2,label='$u_a$')
53+
plt.plot(x,u,':ob',linewidth=2,label='$\widehat{u}$')
54+
plt.legend(fontsize=12,loc='upper left')
55+
plt.grid()
56+
plt.axis([0, 1,u0, u0+0.5])
57+
plt.xlabel("x",fontsize=16)
58+
plt.ylabel("u",fontsize=16)
59+
60+
"Compute error"
61+
error = np.max(np.abs(u-ua))
62+
print("Linf error u: %g\n" % error)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
"""
3+
Solution of a 1D Poisson equation: -u_xx = f
4+
Domain: [0,1]
5+
BC: u(0) = 0, u'(1) = 0
6+
with f = 2*(2*x^2 + 5*x - 2)*exp(x)
7+
8+
Analytical solution: 2*x*(3-2*x)*exp(x)
9+
10+
Finite differences (FD) discretization: second-order diffusion operator
11+
12+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13+
Change boundary conditions (Neumann first and second-order)
14+
15+
"""
16+
__author__ = 'Jordi Vila-Pérez'
17+
__email__ = 'jvilap@mit.edu'
18+
19+
20+
import numpy as np
21+
import matplotlib.pyplot as plt
22+
import matplotlib.animation as animation #We have to load this
23+
from math import pi
24+
%matplotlib qt
25+
plt.close()
26+
27+
"Number of points"
28+
N = 8
29+
Dx = 1/N
30+
x = np.linspace(0,1,N+1)
31+
32+
"Dirichlet boundary condition at x=0"
33+
u0 = 0
34+
35+
"Order of Neumann boundary condition approximation"
36+
order = 1
37+
38+
"System matrix and RHS term"
39+
A = (1/Dx**2)*(2*np.diag(np.ones(N+1)) - np.diag(np.ones(N),-1) - np.diag(np.ones(N),1))
40+
F = 2*(2*x**2 + 5*x - 2)*np.exp(x)
41+
42+
"Boundary condition at x=0"
43+
A[0,:] = np.concatenate(([1], np.zeros(N)))
44+
F[0] = u0
45+
46+
if order<2:
47+
"Boundary condition at x=0"
48+
A[N,:] = np.concatenate((np.zeros(N-1),[-1, 1]))
49+
F[N] = 0
50+
else:
51+
"Boundary condition at x=0"
52+
A[N,:] = (1/Dx)*np.concatenate((np.zeros(N-2),[1/2, -2, 3/2]))
53+
F[N] = 0
54+
55+
"Solution of the linear system AU=F"
56+
u = np.linalg.solve(A,F)
57+
u = u[0:N+1]
58+
ua = 2*x*(3-2*x)*np.exp(x)+u0
59+
60+
"Plotting solution"
61+
plt.plot(x,ua,'-r',linewidth=2,label='$u_a$')
62+
plt.plot(x,u,':ob',linewidth=2,label='$\widehat{u}$')
63+
plt.legend(fontsize=12,loc='upper left')
64+
plt.grid()
65+
plt.xlabel("x",fontsize=16)
66+
plt.ylabel("u",fontsize=16)
67+
68+
"Compute error"
69+
error = np.max(np.abs(u-ua))
70+
print("Linf error u: %g\n" % error)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
"""
3+
Solution of a 1D Poisson equation: -u_xx = f
4+
Domain: [0,1]
5+
BC: u(0) = 0, u'(1) = 0
6+
with f = 2*(2*x^2 + 5*x - 2)*exp(x)
7+
8+
Analytical solution: 2*x*(3-2*x)*exp(x)
9+
10+
Finite differences (FD) discretization: second-order diffusion operator
11+
12+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13+
Change boundary conditions (Neumann first and second-order)
14+
15+
"""
16+
__author__ = 'Jordi Vila-Pérez'
17+
__email__ = 'jvilap@mit.edu'
18+
19+
20+
import numpy as np
21+
import matplotlib.pyplot as plt
22+
import matplotlib.animation as animation #We have to load this
23+
from math import pi
24+
%matplotlib qt
25+
plt.close()
26+
27+
"Number of points"
28+
N = 16
29+
Dx = 1/N
30+
x = np.linspace(0,1,N+1)
31+
32+
"Dirichlet boundary condition at x=0"
33+
u0 = 0
34+
35+
"Order of Neumann boundary condition approximation"
36+
order = 2
37+
38+
if order<2:
39+
"System matrix and RHS term"
40+
A = (1/Dx**2)*(2*np.diag(np.ones(N+1)) - np.diag(np.ones(N),-1) - np.diag(np.ones(N),1))
41+
F = 2*(2*x**2 + 5*x - 2)*np.exp(x)
42+
43+
"Boundary condition at x=0"
44+
A[0,:] = np.concatenate(([1], np.zeros(N)))
45+
F[0] = u0
46+
47+
"Boundary condition at x=0"
48+
A[N,:] = np.concatenate((np.zeros(N-1),[-1, 1]))
49+
F[N] = 0
50+
else:
51+
"System matrix and RHS term"
52+
A = (1/Dx**2)*(2*np.diag(np.ones(N+2)) - np.diag(np.ones(N+1),-1) - np.diag(np.ones(N+1),1))
53+
xN = np.concatenate((x, [x[N]+Dx]))
54+
F = 2*(2*xN**2 + 5*xN - 2)*np.exp(xN)
55+
56+
"Boundary condition at x=0"
57+
A[0,:] = np.concatenate(([1], np.zeros(N+1)))
58+
F[0] = u0
59+
60+
"Boundary condition at x=0"
61+
A[N+1,:] = (0.5/Dx)*np.concatenate((np.zeros(N-1),[-1, 0, 1]))
62+
F[N+1] = 0
63+
64+
"Solution of the linear system AU=F"
65+
u = np.linalg.solve(A,F)
66+
u = u[0:N+1]
67+
ua = 2*x*(3-2*x)*np.exp(x)+u0
68+
69+
"Plotting solution"
70+
plt.plot(x,ua,'-r',linewidth=2,label='$u_a$')
71+
plt.plot(x,u,':ob',linewidth=2,label='$\widehat{u}$')
72+
plt.legend(fontsize=12,loc='upper left')
73+
plt.grid()
74+
plt.xlabel("x",fontsize=16)
75+
plt.ylabel("u",fontsize=16)
76+
77+
"Compute error"
78+
error = np.max(np.abs(u-ua))
79+
print("Linf error u: %g\n" % error)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
Solution of a 1D Poisson equation in spherical coordinates: -u_rr - (2/r)*u_r = f
3+
Domain: [r0,r0+1] IMPORTANT r>0
4+
BC: u(r0) = u0, u'(r0+1) = 0
5+
with f = 2*(2*A(rt)/r + B(rt))*erp(rt), being A(rt) = 2*rt^2 + rt - 3, and B(rt) = 2*rt^2 + 5*rt -2
6+
and rt = r-r0
7+
8+
Analytical solution: 2*(rt)*(3-2*(rt))*exp(rt) + u0
9+
10+
Finite differences (FD) discretization: second-order daccuracy
11+
12+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13+
Introduce first-order derivative with second-order accuracy
14+
15+
"""
16+
__author__ = 'Jordi Vila-Pérez'
17+
__email__ = 'jvilap@mit.edu'
18+
19+
20+
import numpy as np
21+
import matplotlib.pyplot as plt
22+
import matplotlib.animation as animation #We have to load this
23+
from math import pi
24+
%matplotlib qt
25+
plt.close()
26+
27+
"Number of points"
28+
N = 16
29+
r0 = 13
30+
Dr = 1/N
31+
r = np.linspace(0,1,N+1) + r0
32+
rN = np.concatenate((r, [r[N]+Dr]))
33+
rNt = rN-r0
34+
35+
"Dirichlet boundary condition at r0"
36+
u0 = 1
37+
38+
"Time parameters"
39+
dt = 1/24
40+
time = np.arange(0,3+dt,dt)
41+
nt = np.size(time)
42+
43+
"Initialize solution U"
44+
U = np.zeros((N+2,nt))
45+
U[:,0] = rNt*(3-2*rNt)*np.exp(rNt)+u0
46+
47+
"Solution at each time step"
48+
for it in range(0,nt-1):
49+
"System matrir and RHS term"
50+
A = (1/Dr**2)*(2*np.diag(np.ones(N+2)) - np.diag(np.ones(N+1),-1) - np.diag(np.ones(N+1),1))
51+
"First-order term"
52+
A = A + (1/Dr)*(np.diag(1/rN[1:N+2],-1) - np.diag(1/rN[0:N+1],1))
53+
54+
"New source term"
55+
Ar = 2*rNt**2 + rNt - 3
56+
Br = 2*rNt**2 + 5*rNt - 2
57+
F = 2*(2*Ar/rN + Br)*np.exp(rNt)*np.abs(np.cos(pi*time[it+1]))
58+
59+
"Time derivative terms"
60+
A = A + (1/dt)*np.diag(np.ones(N+2))
61+
F = F + U[:,it]/dt
62+
63+
"Boundary condition at r=0"
64+
A[0,:] = np.concatenate(([1], np.zeros(N+1)))
65+
F[0] = u0
66+
67+
"Boundary condition at r=0"
68+
A[N+1,:] = (0.5/Dr)*np.concatenate((np.zeros(N-1),[-1, 0, 1]))
69+
F[N+1] = 0
70+
71+
"Solution of the linear system AU=F"
72+
u = np.linalg.solve(A,F)
73+
U[:,it+1] = u
74+
75+
"Animation of the results"
76+
fig = plt.figure()
77+
ax = plt.axes(xlim =(r0, r0+1),ylim =(u0,u0+5))
78+
myAnimation, = ax.plot([], [],':ob',linewidth=2)
79+
plt.grid()
80+
plt.xlabel("x",fontsize=16)
81+
plt.ylabel("u",fontsize=16)
82+
83+
def animate(i):
84+
plt.plot(r,U[0:N+1,i])
85+
myAnimation.set_data(r, U[0:N+1,i])
86+
return myAnimation,
87+
88+
anim = animation.FuncAnimation(fig,animate,frames=range(1,nt),blit=True)
-48.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)