-
Notifications
You must be signed in to change notification settings - Fork 2
/
ellipticCauchyPro.py
210 lines (166 loc) · 8.04 KB
/
ellipticCauchyPro.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import fenics as fe
import numpy as np
import matplotlib.pyplot as plt
"""
This script used for evaluating Cauchy-type problem described in
B. Jin and J. Zou, Hierarchical Bayesian inference for ill-posed
problems via variational method, Journal of Computational Physics,
229, 2010, 7317-7343.
In Subsection 5.1, there are detailed descriptions of the problem.
The specific settings are following descriptions in Subsection 5.2 (first paragraph):
(1) Neumann and Dirichlet boundary conditions,
(2) inaccessible and accessible part of the boundary
"""
# used for generate H
class GeneH(object):
def __init__(self, para):
# para = [nx, ny, FunctionSpace, degree, ...]
self.nx, self.ny = para['mesh_N'][0], para['mesh_N'][1]
self.mesh = fe.UnitSquareMesh(self.nx, self.ny)
self.Vu = fe.FunctionSpace(self.mesh, 'P', para['P'])
self.Vc = fe.FunctionSpace(self.mesh, 'P', para['P'])
self.sol = []
self.al = fe.Constant(1.0)
self.f = fe.Constant(0.0)
self.q = fe.Constant(0.0)
self.theta = []
self.mE = 0
def eva(self, num):
# construct basis functions, Set num points corresponding to num basis functions
basPoints = np.linspace(0, 1, num)
dx = basPoints[1] - basPoints[0]
aa, bb, cc = -dx, 0.0, dx
for x_p in basPoints:
self.theta.append(fe.interpolate(fe.Expression(
'x[0] < a || x[0] > c ? 0 : (x[0] >=a && x[0] <= b ? (x[0]-a)/(b-a) : 1-(x[0]-b)/(c-b))',
degree=2, a=aa, b=bb, c=cc), self.Vc))
aa, bb, cc = aa+dx, bb+dx, cc+dx
u_trial, u_test = fe.TrialFunction(self.Vu), fe.TestFunction(self.Vu)
left = fe.inner(self.al*fe.nabla_grad(u_trial), fe.nabla_grad(u_test))*fe.dx
right = self.f*u_test*fe.dx
def boundaryD(x, on_boundary):
return on_boundary and fe.near(x[1], 1.0)
for i in range(num):
uH = fe.Function(self.Vu)
bcD = fe.DirichletBC(self.Vu, self.theta[i], boundaryD)
left_m, right_m = fe.assemble_system(left, right, bcD)
fe.solve(left_m, uH.vector(), right_m)
self.sol.append(uH)
def gene(self, points_m):
num = len(self.sol)
ma = np.zeros((len(points_m), num))
for i in range(num):
ma[:, i] = [self.sol[i](points) for points in points_m]
return np.array(ma)
def geneM(self, funT):
num = len(self.theta)
m = funT(np.linspace(0, 1, num), np.ones(num))
return m
def setBasis(self, m):
self.mE = m
def basisFun(self, x):
num = len(self.mE)
return np.sum([self.mE[i]*self.theta[i](x, 1) for i in range(num)])
def calBasisFun(self, x):
vbasisFun = np.vectorize(self.basisFun)
return vbasisFun(x)
#return np.array([self.basisFun(dian) for dian in x])
# used for generate U_H
class GeneUH(object):
def __init__(self, para):
self.nx, self.ny = para['mesh_N'][0], para['mesh_N'][1]
self.mesh = fe.UnitSquareMesh(self.nx, self.ny)
self.Vu = fe.FunctionSpace(self.mesh, 'P', para['P'])
self.Vc = fe.FunctionSpace(self.mesh, 'P', para['P'])
self.al = fe.Expression(para['alpha'], degree=5)
self.q1 = fe.interpolate(fe.Expression(para['q1'], degree=5), self.Vc)
self.q2 = fe.interpolate(fe.Expression(para['q2'], degree=5), self.Vc)
self.q3 = fe.interpolate(fe.Expression(para['q3'], degree=5), self.Vc)
self.f = fe.Expression(para['f'], degree=5)
self.theta = fe.Constant(0.0)
self.u = fe.Function(self.Vu)
def eva(self):
# construct solutions corresponding to the basis functions
class BoundaryX0(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[0], 0.0)
class BoundaryX1(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[0], 1.0)
class BoundaryY0(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[1], 0.0)
class BoundaryY1(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[1], 1.0)
boundaries = fe.MeshFunction('size_t', self.mesh, self.mesh.topology().dim()-1)
boundaries.set_all(0)
bc0, bc1, bc2, bc3 = BoundaryX0(), BoundaryX1(), BoundaryY0(), BoundaryY1()
bc0.mark(boundaries, 1)
bc1.mark(boundaries, 2)
bc2.mark(boundaries, 3)
bc3.mark(boundaries, 4)
domains = fe.MeshFunction("size_t", self.mesh, self.mesh.topology().dim())
domains.set_all(0)
dx = fe.Measure('dx', domain=self.mesh, subdomain_data=domains)
ds = fe.Measure('ds', domain=self.mesh, subdomain_data=boundaries)
u_trial, u_test = fe.TrialFunction(self.Vu), fe.TestFunction(self.Vu)
left = fe.inner(fe.nabla_grad(u_trial), fe.nabla_grad(u_test))*dx
right = self.f*u_test*dx + (self.q1*u_test*ds(1) + self.q2*u_test*ds(2) + self.q3*u_test*ds(3))
bcD = fe.DirichletBC(self.Vu, self.theta, boundaries, 4)
left_m, right_m = fe.assemble_system(left, right, bcD)
fe.solve(left_m, self.u.vector(), right_m)
def gene(self, points_m):
uH = np.array([self.u(points) for points in points_m])
return uH
# calculate the full solutions
def calTrueSol(para):
nx, ny = para['mesh_N'][0], para['mesh_N'][1]
mesh = fe.UnitSquareMesh(nx, ny)
Vu = fe.FunctionSpace(mesh, 'P', para['P'])
Vc = fe.FunctionSpace(mesh, 'P', para['P'])
al = fe.Constant(para['alpha'])
f = fe.Expression(para['f'], degree=5)
q1 = fe.interpolate(fe.Expression(para['q1'], degree=5), Vc)
q2 = fe.interpolate(fe.Expression(para['q2'], degree=5), Vc)
q3 = fe.interpolate(fe.Expression(para['q3'], degree=5), Vc)
theta = fe.interpolate(fe.Expression(para['q4'], degree=5), Vc)
class BoundaryX0(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[0], 0.0)
class BoundaryX1(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[0], 1.0)
class BoundaryY0(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[1], 0.0)
class BoundaryY1(fe.SubDomain):
def inside(self, x, on_boundary):
return on_boundary and fe.near(x[1], 1.0)
boundaries = fe.MeshFunction('size_t', mesh, mesh.topology().dim()-1)
boundaries.set_all(0)
bc0, bc1, bc2, bc3 = BoundaryX0(), BoundaryX1(), BoundaryY0(), BoundaryY1()
bc0.mark(boundaries, 1)
bc1.mark(boundaries, 2)
bc2.mark(boundaries, 3)
bc3.mark(boundaries, 4)
domains = fe.MeshFunction("size_t", mesh, mesh.topology().dim())
domains.set_all(0)
bcD = fe.DirichletBC(Vu, theta, boundaries, 4)
dx = fe.Measure('dx', domain=mesh, subdomain_data=domains)
ds = fe.Measure('ds', domain=mesh, subdomain_data=boundaries)
u_trial, u_test = fe.TrialFunction(Vu), fe.TestFunction(Vu)
u = fe.Function(Vu)
left = fe.inner(al*fe.nabla_grad(u_trial), fe.nabla_grad(u_test))*dx
right = f*u_test*dx + (q1*u_test*ds(1) + q2*u_test*ds(2) + q3*u_test*ds(3))
left_m, right_m = fe.assemble_system(left, right, bcD)
fe.solve(left_m, u.vector(), right_m)
return u
class MeasurePoints(object):
def __init__(self, num):
# num is the number of measurment points
yy = np.linspace(0, 1, np.floor(num/2))
xxl, xxr = yy*0.0, yy*0.0+1
self.points_m = np.vstack((np.transpose([xxl, yy]), np.transpose([xxr, yy])))
#self.points_m = np.vstack((np.transpose([xxr, yy]), np.flip(np.transpose([xxl, yy]), -1)))
self.num = len(self.points_m)