Repository navigation
Expand file tree
/
Copy pathexample_problem.py
More file actions
136 lines (87 loc) · 3.37 KB
/
Copy pathexample_problem.py
File metadata and controls
136 lines (87 loc) · 3.37 KB
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
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from mpi4py import MPI
from dolfinx import mesh
from dolfinx import fem
from petsc4py.PETSc import ScalarType
import ufl
def create_mesh(N):
domain = mesh.create_unit_square(MPI.COMM_SELF, N, N)
tdim = domain.topology.dim
fdim = tdim - 1
domain.topology.create_connectivity(fdim, tdim)
return domain
def create_riesz_problem(domain, V):
""" Create the problem f \mapsto u s.t. -\Delta u = f in \Omega, u = 0 on \partial\Omega.
Only the left-hand-side matrix portion is of interest here, for creating the preconditioner. """
# Create homogeneous Dirichlet boundary conditions.
boundary_facets = mesh.exterior_facet_indices(domain.topology)
boundary_dofs = fem.locate_dofs_topological(V, domain.topology.dim-1, boundary_facets)
bc = fem.dirichletbc(ScalarType(0), boundary_dofs, V)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
f = fem.Constant(domain, ScalarType(1))
a = ufl.dot( ufl.grad(u), ufl.grad(v) ) * ufl.dx
L = f * v * ufl.dx
problem = fem.petsc.LinearProblem(a, L, bcs=[bc])
return problem
def create_inhomogeneous_problem(domain, V, kappa, f):
# Create homogeneous Dirichlet boundary conditions.
boundary_facets = mesh.exterior_facet_indices(domain.topology)
boundary_dofs = fem.locate_dofs_topological(V, domain.topology.dim-1, boundary_facets)
bc = fem.dirichletbc(ScalarType(0), boundary_dofs, V)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
a = kappa * ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx
L = f * v * ufl.dx
problem = fem.petsc.LinearProblem(a, L, bcs=[bc])
return problem
N = 100
domain = create_mesh(N)
V = fem.FunctionSpace(domain, ("CG", 1))
x = ufl.SpatialCoordinate(domain)
f = fem.Constant(domain, ScalarType(1))
kappa = 6.0 + 5.0*ufl.sin(ufl.pi*( (x[0]-0.5)**2 + (x[1]-0.5)**2 ))
from bindings.petsc_to_scipy import unpack_problem
inh_problem = create_inhomogeneous_problem(domain, V, kappa, f)
A_csr, b = unpack_problem(inh_problem)
riesz_problem = create_riesz_problem(domain, V)
B_csr, _ = unpack_problem(riesz_problem)
if N <= 40:
M = np.zeros((b.shape[0], 2*b.shape[0]+1))
M[:,:b.shape[0]] = A_csr.todense()
M[:,b.shape[0]:-1] = B_csr.todense()
M[:,-1] = b
ims = plt.imshow(M / np.amax(M))
plt.colorbar(ims)
plt.figure()
plt.spy(M)
import pyamg
ml = pyamg.ruge_stuben_solver(B_csr)
B = ml.aspreconditioner(cycle='V')
class callback:
def __init__(self, name="Solver"):
self.k = 0
self.name = name
def __call__(self, xk):
self.k += 1
print(f"{self.name}: k={self.k}, ||r_k||={np.linalg.norm(A_csr@xk - b):.2e}")
return
print("With preconditioning: ")
u_np, info = sp.sparse.linalg.cg(A_csr, b, tol=1e-8, maxiter=30, M=B, callback=callback("CG w/ AMG precond."))
print("Error =", np.linalg.norm(A_csr @ u_np - b))
print()
print("Without preconditioning: ")
u_np_wp, info = sp.sparse.linalg.cg(A_csr, b, tol=1e-8, maxiter=30, M=None, callback=callback("CG w/ no precond."))
print("Error =", np.linalg.norm(A_csr @ u_np_wp - b))
print()
xx = domain.geometry.x
step = 1
if N > 40:
step = N**2 // 1000
ax = plt.figure().add_subplot(projection='3d')
ax.plot_trisurf(xx[::step,0], xx[::step,1], u_np[::step], cmap=plt.cm.viridis)
plt.show()