Using FEniCS to generate sampling matrix

For solving inverse problems, we usually need to sample a function at some discrete points. Considering the following problem
d = \mathcal{F}u + \epsilon,
with \mathcal{F} := \mathcal{S} \cdot F. Here F represents a partial differential equation or other forward operator, \mathcal{S} is a sampling operator can be defined as
\mathcal{S}(f) = (f(x_1), f(x_2), \cdots, f(x_{N_{d}}))^{T}.

Usually, for linear operator F, it can be discretized as a matrix also denoted as F. In the following, we provide a strategy to generate the sampling matrix \mathcal{S}. For finite element method, a function can be expanded by a series of basis functions
u = \sum_{i=1}^{N}u_{i}e_{i}.
Then we have
\left[\begin{array}{c} u(x_1) \\ \vdots \\ u(x_{N_d}) \end{array}\right] = \left[\begin{array}{ccc} e_{1}(x_1) & \cdots & e_{N}(x_1) \\ \vdots & & \vdots \\ e_{1}(x_{N_d}) & \cdots & e_{N}(x_{N_{d}}) \end{array}\right] \left[\begin{array}{c} u_1 \\ \vdots \\ u_N \end{array}\right]
Hence, the sampling matrix can be obtained by the basis functions evaluated at sampling points. Notice that
u(x_1) = \sum_{i = 1}^{N}u_{i}e_{i}(x_{1}),
so if we take u_1 = 1 and u_i = 0 for i=2,3,\cdots, N, we find
u(x_1) = e_{1}(x_1).
Based on this analysis, we can generate a function as follow:

  1. generate a function u in FEniCS;
  2. take each component of u.vector() to be 1 and other components to be 0, and evaluate the function u at the required point.

In the following, we provide a program. The parameter V can be taken as

V = fe.FunctionSpace(mesh, 'P', 1)

The parameter points can be taken as

points = [(0.5,0.5), (0.5, 0.6)]

The program:

import numpy as np
import fenics as fe

def measureMatrix(V, points):
    len_points = len(points)
    u = fe.Function(V)
    u.vector()[:] = 0
    u_vec = u.vector()[:]
    len_u = len(u_vec)
    sampleMatrix = np.zeros((len_points, len_u))
    
    for i in range(len_points):
        for j in range(len_u):
            u_vec[j] = 1
            u.vector()[:] = u_vec
            sampleMatrix[(i, j)] = u(points[i])
            u_vec[:] = 0
    
    return sampleMatrix

The following program illustrates the effectiveness of the above program.

import fenics as fe
import numpy as np

mesh = fe.UnitSquareMesh(10, 10)
V = fe.FunctionSpace(mesh, 'P', 1)

u_D = fe.Expression('1+x[0]*x[0]+2*x[1]*x[1]', degree=2)

def boundary(x, on_boundary):
    return on_boundary

bc = fe.DirichletBC(V, u_D, boundary)

u = fe.TrialFunction(V)
v = fe.TestFunction(V)
f = fe.Constant(-6.0)
a = fe.dot(fe.grad(u), fe.grad(v))*fe.dx
L = f*v*fe.dx

u = fe.Function(V)
fe.solve(a == L, u, bc)

points = []
for i in range(100):
    points.append((0.5, i/100.0))
    
S = measureMatrix(V, points)
val1 = S@u.vector()[:]
val2 = [u(i) for i in points]
print(np.linalg.norm(val1-val2))
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,196评论 0 10
  • 去年今日父安在, 全家团圆乐相融。 今日难见父影踪, 心生悲戚泪眼胧。
    姝瑾儿阅读 4,210评论 17 27
  • 我拒绝每个勇敢的人跑向我 因为我在这片干净的岁月里 等你过来 冲我笑 无论多久 无论多久 我一边在说着 无论多久我...
    哀慕熙荣阅读 1,346评论 0 1
  • 昨天,收到一则简信,信件内容是:好久没看你出文了,好久出文呀…… 其实...台妹PKGIRL不是消失阿!是去生二胎...
    庄13台妹PKGIRL阅读 11,016评论 107 79

友情链接更多精彩内容