Upload ex6 and ex7
This commit is contained in:
parent
1885d64c0a
commit
6c2d96ff4d
44 changed files with 15291 additions and 0 deletions
127
ex6/ex_6C.py
Normal file
127
ex6/ex_6C.py
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import adaptivity_schemes
|
||||
|
||||
np.set_printoptions(precision=2)
|
||||
|
||||
|
||||
def Solve_6C(mesh, p):
|
||||
N = len(mesh) - 1 # number of elements
|
||||
|
||||
A = np.zeros((N + 1, N + 1))
|
||||
|
||||
for i in range(1, N + 1):
|
||||
h = mesh[i] - mesh[i - 1]
|
||||
|
||||
a_11 = 1./h - p/2.
|
||||
a_12 = -1./h + p/2.
|
||||
a_21 = -1./h - p/2.
|
||||
a_22 = 1./h + p/2.
|
||||
|
||||
A[i - 1, i - 1] += a_11
|
||||
A[i - 1, i] += a_12
|
||||
A[i, i - 1] += a_21
|
||||
A[i, i] += a_22
|
||||
|
||||
#print("A =\n", A)
|
||||
|
||||
|
||||
# take dirichlet data into account
|
||||
u_g = np.zeros(N + 1)
|
||||
u_g[0] = 0
|
||||
u_g[N] = 1
|
||||
#print("u_g =\n", u_g)
|
||||
|
||||
# remove first and last row of A
|
||||
A_g = A[1:N, :]
|
||||
#print("A_g =\n", A_g)
|
||||
|
||||
# assemble RHS with dirichlet data
|
||||
f = -A_g.dot(u_g)
|
||||
#print(f)
|
||||
|
||||
# matrix for the inner nodes (excluding nodes with dirichlet bcs)
|
||||
A_0 = A[1:N, 1:N]
|
||||
#print(A_0)
|
||||
|
||||
# solve for u_0 (free dofs)
|
||||
u_0 = np.linalg.solve(A_0, f)
|
||||
|
||||
# assemble "u = u_0 + u_g"
|
||||
u = np.concatenate([[0], u_0, [1]])
|
||||
|
||||
return u
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
p = 70
|
||||
######### h-adaptivity ##########
|
||||
N = 5 # number of elements
|
||||
mesh = np.linspace(0, 1, N + 1)
|
||||
u = Solve_6C(mesh, p)
|
||||
|
||||
plt.plot(mesh, u, '-o')
|
||||
plt.grid()
|
||||
plt.xlabel('x')
|
||||
plt.ylabel('u_h(x)')
|
||||
plt.title("h-adaptivity")
|
||||
|
||||
|
||||
N_vec = ["0 refinements, " + str(N) + " elements"]
|
||||
refinements = 4 # number of refinements
|
||||
for i in range(refinements):
|
||||
mesh = adaptivity_schemes.adapt_h(mesh, u, 0.7)
|
||||
u = Solve_6C(mesh, p)
|
||||
plt.plot(mesh, u, '-o')
|
||||
|
||||
N_vec.append(str(i + 1) + " refinements, " + str(len(mesh) - 1) + " elements")
|
||||
|
||||
|
||||
# plot exact solution
|
||||
x = np.linspace(0, 1, 50)
|
||||
plt.plot(x, (np.exp(p*x) - 1.)/(np.exp(p) - 1.))
|
||||
N_vec.append("exact")
|
||||
|
||||
plt.legend(N_vec)
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
########## r-adaptivity ##########
|
||||
N = 10
|
||||
mesh = np.linspace(0, 1, N + 1)
|
||||
u = Solve_6C(mesh, p)
|
||||
plt.plot(mesh, u, '-o')
|
||||
title = "r-adaptivity with " + str(N) + " elements"
|
||||
plt.title(title)
|
||||
|
||||
adaptations_vec = ["0 adaptations"]
|
||||
adaptations = 4 # number of iterations
|
||||
for i in range(adaptations):
|
||||
mesh = adaptivity_schemes.adapt_r(mesh, u)
|
||||
u = Solve_6C(mesh, p)
|
||||
plt.plot(mesh, u, '-o')
|
||||
|
||||
adaptations_vec.append(str(i + 1) + " adaptations")
|
||||
|
||||
|
||||
# plot exact solution
|
||||
x = np.linspace(0, 1, 50)
|
||||
plt.plot(x, (np.exp(p*x) - 1.)/(np.exp(p) - 1.))
|
||||
adaptations_vec.append("exact")
|
||||
|
||||
|
||||
plt.legend(adaptations_vec)
|
||||
plt.xlabel('x')
|
||||
plt.ylabel('u_h(x)')
|
||||
plt.grid()
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue