This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example4-neumann.py
191 lines (142 loc) · 5.17 KB
/
example4-neumann.py
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
#!/usr/bin/env python3
# @author [email protected]
# @author [email protected]
# @date 10/05/2019
import numpy as np
import sys
import matplotlib.pyplot as plt
import matplotlib
from cycler import cycler
matplotlib.rcParams["text.usetex"] = True
matplotlib.rcParams["font.size"] = 12
matplotlib.rcParams['text.latex.preamble'] = [
r'\usepackage{xfrac}']
eps=0.1
#############################################################################
# Solve the system
#############################################################################
def solve(M,f):
return np.linalg.solve(M,f)
#############################################################################
# Exact solution
#############################################################################
def exactSolution(x):
return x-((np.exp(-(1-x)/eps)-np.exp(-1/eps))/(1-np.exp(-1/eps)))
#############################################################################
# Loading
#############################################################################
def force(x):
return np.exp((x-1)/eps)/(eps*eps*(1-np.exp(-1/eps)))
#############################################################################
# Boundary value
#############################################################################
def boundary(x):
return 1-np.exp((x-1)/eps)/(eps*(1-np.exp(-1/eps)))
#############################################################################
# Assemble the stiffness matrix for the varibale horizon model (VHM)
#############################################################################
def VHM(n,h):
MVHM = np.zeros([n,n])
MVHM[0][0] = 1.
MVHM[1][0] = -8.
MVHM[1][1] = 16.
MVHM[1][2] = -8.
for i in range(2,n-2):
MVHM[i][i-2] = -1.
MVHM[i][i-1] = -4.
MVHM[i][i] = 10.
MVHM[i][i+1] = -4.
MVHM[i][i+2] = -1.
MVHM[n-2][n-1] = -8.
MVHM[n-2][n-2] = 16.
MVHM[n-2][n-3] = -8.
MVHM[n-1][n-1] = 12.*h
MVHM[n-1][n-2] = -16.*h
MVHM[n-1][n-3] = 4.*h
MVHM *= 1./(8.*h*h)
return MVHM
#############################################################################
# Assemble the stiffness matrix for the local linear elasticity model (LLEM)
#############################################################################
def LLEM(n,h):
MLLEM = np.zeros([n,n])
MLLEM[0][0] = 1
for i in range(1,n-1):
MLLEM[i][i-1] = -2
MLLEM[i][i] = 4
MLLEM[i][i+1] = -2
MLLEM[n-1][n-1] = 3*h
MLLEM[n-1][n-2] = -4*h
MLLEM[n-1][n-3] = h
MLLEM *= 1./(2.*h*h)
return MLLEM
#############################################################################
#Computation
#############################################################################
print("n,h,VHM")
markers = ['s','o','x','.']
size= [2,4,8,10]
for i in range(5,10):
n = np.power(2,i)
h = 1./n
nodes = n+1
x = np.linspace(0,1.,nodes)
load = force(x)
load[len(x)-1] = boundary(1)
u=np.linalg.solve(VHM(nodes,h),load)
uLLEM=np.linalg.solve(LLEM(nodes,h),load)
print(str(n)+","+str(h)+","+str(max(((u[1:len(u)-2]-exactSolution(x)[1:len(u)-2])/exactSolution(x)[1:len(u)-2])))+","+str(max(((uLLEM[1:len(u)-2]-exactSolution(x)[1:len(u)-2])/exactSolution(x)[1:len(u)-2]))))
if i > 6:
plt.plot(x,u-exactSolution(x),label="$\delta=\sfrac{1}{2^{"+str(int(n/2))+"}}$", marker=markers[i-6], c="black",markevery=size[i-6],ls='')
plt.title(r"Example with $\epsilon=$"+str(eps)+" Solution using VHM")
plt.xlabel("x")
plt.ylabel('Error in displacement')
plt.xscale('linear')
plt.yscale('linear')
plt.grid()
plt.legend()
plt.savefig("VHM-Error-eps-"+str(eps)+"-neumann-Solution.pdf",bbox_inches='tight')
plt.cla()
monochrome = (cycler('color', ['k']) * cycler('linestyle',['--',':','-.']))
ax = plt.gca()
ax.set_prop_cycle(monochrome)
for i in [5,6,7]:
n = np.power(2,i)
h = 1./n
nodes = n+1
x = np.linspace(0,1.,nodes)
load = force(x)
load[len(x)-1] = boundary(1)
u=np.linalg.solve(VHM(nodes,h),load)
plt.plot(x,u,label="$\delta=\sfrac{1}{2^{"+str(int(n/2))+"}}$")
plt.plot(x,exactSolution(x),label=r"$\underline{u}(x)$",color="black",ls="-")
plt.xlabel("x")
plt.ylabel('Displacement $u$')
plt.xscale('linear')
plt.yscale('linear')
plt.title(r"Convergence Study with $\epsilon=$"+str(eps)+" Solution using VHM")
plt.grid()
plt.legend()
plt.savefig("VHM-convergence-eps-"+str(eps)+"-neumann-Solution.pdf",bbox_inches='tight')
plt.cla()
monochrome = (cycler('color', ['k']) * cycler('linestyle',['--',':','-.']))
ax = plt.gca()
ax.set_prop_cycle(monochrome)
for i in [5,6,7]:
n = np.power(2,i)
h = 1./n
nodes = n+1
x = np.linspace(0,1.,nodes)
load = force(x)
load[len(x)-1] = boundary(1)
u=np.linalg.solve(LLEM(nodes,h),load)
plt.plot(x,u,label="$\delta=\sfrac{1}{2^{"+str(int(n/2))+"}}$")
plt.plot(x,exactSolution(x),label=r"$\underline{u}(x)$",color="black",ls="-")
plt.xlabel("x")
plt.ylabel('Displacement $u$')
plt.xscale('linear')
plt.yscale('linear')
plt.title(r"Convergence Study with $\epsilon=$"+str(eps)+" Solution using VHM")
plt.grid()
plt.legend()
plt.savefig("LLEM-convergence-eps-"+str(eps)+"-neumann-Solution.pdf",bbox_inches='tight')