-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample6.py
139 lines (106 loc) · 4.03 KB
/
example6.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
# Copyright (C) 2017, Sigvald Marholm and Diako Darian
#
# This file is part of ConstantBC.
#
# ConstantBC is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# ConstantBC is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ConstantBC. If not, see <http://www.gnu.org/licenses/>.
from dolfin import *
from mshr import *
import numpy as np
from numpy.linalg import norm
import matplotlib.pyplot as plt
from ConstantBC import *
from itertools import count
monitor_convergence = False
orders = [1,2]
resolutions = [1,2,3,4]#,5,6]#,7,8]
allow_extrapolation = False
rho = Expression("100*x[1]", degree=1)
rho = Constant(0)
hmins = []
errors = []
for resolution in resolutions:
print("Order: {}, resolution: {} (reference)".format(order,resolution))
fname = "mesh/pentagon_and_square_in_rectangle_res"+str(resolution)
mesh, bnd, num_objects = load_mesh(fname)
V = FunctionSpace(mesh, "Lagrange", 4)
phi = TrialFunction(V)
psi = TestFunction(V)
bcs = [DirichletBC(V, Constant(i), bnd, i+1) for i in range(3)]
lhs = dot(grad(phi), grad(psi)) * dx
rhs = rho*psi * dx
A = assemble(lhs)
b = assemble(rhs)
for bc in bcs:
bc.apply(A, b)
phi_ref = Function(V)
solver = PETScKrylovSolver('gmres','hypre_amg')
solver.parameters['absolute_tolerance'] = 1e-14
solver.parameters['relative_tolerance'] = 1e-10 #e-12
solver.parameters['maximum_iterations'] = 100000
solver.parameters['monitor_convergence'] = monitor_convergence
solver.set_operator(A)
solver.solve(phi_ref.vector(), b)
n = FacetNormal(mesh)
dss = Measure("ds", domain=mesh, subdomain_data=bnd)
charge1 = assemble(dot(grad(phi), n) * dss(2, degree=1))
charge2 = assemble(dot(grad(phi), n) * dss(3, degree=1))
total_charge = charge1 + charge2
vsources = [[0,1,1]]
vsources = [[-1,0,1],[-1,1,2]]
# vsources = []
# hmins.append(mesh.hmin())
hmins.append(mesh.hmax())
errors.append([])
for order in orders:
print("Order: {}, resolution: {}".format(order,resolution))
V = FunctionSpace(mesh, "Lagrange", order)
phi = TrialFunction(V)
psi = TestFunction(V)
bc_e = DirichletBC(V, Constant(0), bnd, 1)
# objects = [ObjectBC(V, bnd, 2+i) for i in range(num_objects)]
# circuit = Circuit(V, bnd, objects, vsources)
objects = [DirichletBC(V, Constant(i), bnd, i+1) for i in range(3)]
objects[0].charge = charge1
objects[1].charge = charge2
lhs = dot(grad(phi), grad(psi)) * dx
rhs = rho*psi * dx
# Do not use assemble_system()
A = assemble(lhs)
b = assemble(rhs)
bc_e.apply(A, b)
# A, b = circuit.apply(A, b)
for o in objects:
o.apply(A, b)
phi = Function(V)
# phi.set_allow_extrapolation(allow_extrapolation)
solver = PETScKrylovSolver('gmres','hypre_amg')
solver.parameters['absolute_tolerance'] = 1e-14
solver.parameters['relative_tolerance'] = 1e-10 #e-12
solver.parameters['maximum_iterations'] = 100000
solver.parameters['monitor_convergence'] = monitor_convergence
solver.set_operator(A)
solver.solve(phi.vector(), b)
error = errornorm(phi_ref, phi)
errors[-1].append(error)
for order in orders:
x = np.array(hmins)
y = np.array(errors)[:,order-1]
p = plt.loglog(x, y, '-o', label=order)
color = p[0].get_color()
plt.loglog(x, y[0]*(x/x[0])**order, ':', color=color)
plt.grid()
plt.xlabel('hmin')
plt.ylabel('L_2 norm error')
plt.title('Convergence')
plt.legend(loc='lower right')
plt.show()