/
mixedBoundary.py
87 lines (64 loc) · 2.71 KB
/
mixedBoundary.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
import numpy as np
from numpy import sqrt
import matplotlib.pylab as plt
import meshes as msh
import FEM
def main():
lu = 1.548888; # value of l(sol)
f = lambda x, y: (1.0);
Nspacings = 9; # number of mesh widths to test (in powers of sqrt(2)) -> CHANGE BACK TO 10!!!!
h0 = 0.3; # maximum value of mesh width
h = [None]*Nspacings; # vector for mesh widths (set values)
dof = np.zeros(Nspacings); # vector for mesh widths (true values)
dof_reg = np.zeros(Nspacings); # vector for mesh widths (true values)
discret_err_bgm = [None]*Nspacings; # vector for discretization errors for linear FEM
discret_err_reg = [None]*Nspacings; # vector for discretization errors for quadratic FEM
# regular mesh
for j in range(Nspacings):
h = h0*2.0**(-j/2.0);
[p, t, be, bd]=msh.create_msh("regular", h)
# LINEAR FINITE ELEMENTS
# Compute the discretized system:
A_lin = FEM.stiffness(p, t); # stiffness-matrix
F_lin = FEM.load(p, t, 3, f); # load-vector
# degrees of freedom
dof_reg[j] = p.shape[0]
# solve system with Dirichtlet boundary conditions
u_n = FEM.solve_d0(p, t, bd, A_lin, F_lin)
# LINEAR FINITE ELEMENTS
lun_lin = np.dot(u_n,F_lin); # value of l(u_n)
en_lin = lu-sqrt(abs(lun_lin)); # energy norm of error vector
discret_err_reg[j] = en_lin;
print
print "energy error with the regular mesh"
print discret_err_reg
# background mesh
for j in range(Nspacings):
#[p, t, be, bd]=msh.create_msh_bgm("bgmesh"+str(j))
[p, t, be, bd]=msh.read_gmsh("bgmesh"+str(j)+".msh")
# LINEAR FINITE ELEMENTS
# Compute the discretized system:
A_lin = FEM.stiffness(p, t); # stiffness-matrix
F_lin = FEM.load(p, t, 3, f); # load-vector
# degrees of freedom
dof[j] = p.shape[0]
# solve system with Dirichtlet boundary conditions
u_n = FEM.solve_d0(p, t, bd, A_lin, F_lin)
# LINEAR FINITE ELEMENTS
lun_lin = np.dot(u_n,F_lin); # value of l(u_n)
en_lin = lu-sqrt(abs(lun_lin)); # energy norm of error vector
discret_err_bgm[j] = en_lin;
print
print "energy error with the background mesh"
print discret_err_bgm
# ploting energy error distribution
plt.loglog(dof_reg, discret_err_reg,':*', label='energy error - regular mesh')
plt.loglog(dof, discret_err_bgm,':*', label='energy error - bgm')
plt.title('energy error depending on degrees of freedom')
plt.legend(loc=1)
plt.show()
# Visualization of the solution
FEM.plot(p, t, u_n); # approximated solution
plt.show()
if __name__ == '__main__':
main()