向量已被插值的矩阵向量乘法 - Python

Matrix vector multiplication where the vector has been interpolated - Python

我已经使用有限元法逼近了拉普拉斯方程 并因此将其转化为矩阵系统 AU = F 其中 A 是刚度矢量并求解 U(对我来说不是很重要问题)。

我现在得到了我的近似值 U,当我找到 AU 时,我应该得到向量 F(或至少类似),其中 F 是:

AU 给出了 x = 0 到 x = 1 的以下图(例如,对于 20 个节点):

然后我需要将 U 插值到更长的向量并找到 AU(对于更大的 A 也是如此,但不插值)。我通过以下方式对 U 进行插值:

U_inter = interp1d(x,U)
U_rich = U_inter(longer_x)

这似乎工作正常,直到我将它与更长的 A 矩阵相乘:

似乎每个尖峰都在 x 的一个节点(即原始 U 的节点)。有谁知道这可能是什么原因造成的?以下是我查找 A、U 和 F 的代码。

import numpy as np
import math
import scipy
from scipy.sparse import diags
import scipy.sparse.linalg
from scipy.interpolate import interp1d
import matplotlib
import matplotlib.pyplot as plt

def Poisson_Stiffness(x0):
    """Finds the Poisson equation stiffness matrix with any non uniform mesh x0"""

    x0 = np.array(x0)
    N = len(x0) - 1 # The amount of elements; x0, x1, ..., xN

    h = x0[1:] - x0[:-1]

    a = np.zeros(N+1)
    a[0] = 1 #BOUNDARY CONDITIONS
    a[1:-1] = 1/h[1:] + 1/h[:-1]
    a[-1] = 1/h[-1]
    a[N] = 1 #BOUNDARY CONDITIONS

    b = -1/h
    b[0] = 0 #BOUNDARY CONDITIONS

    c = -1/h
    c[N-1] = 0 #BOUNDARY CONDITIONS: DIRICHLET

    data = [a.tolist(), b.tolist(), c.tolist()]
    Positions = [0, 1, -1]
    Stiffness_Matrix = diags(data, Positions, (N+1,N+1))

    return Stiffness_Matrix

def NodalQuadrature(x0):
    """Finds the Nodal Quadrature Approximation of sin(pi x)"""

    x0 = np.array(x0)
    h = x0[1:] - x0[:-1]
    N = len(x0) - 1

    approx = np.zeros(len(x0))
    approx[0] = 0 #BOUNDARY CONDITIONS

    for i in range(1,N):
        approx[i] = math.sin(math.pi*x0[i])
        approx[i] = (approx[i]*h[i-1] + approx[i]*h[i])/2

    approx[N] = 0 #BOUNDARY CONDITIONS

    return approx

def Solver(x0):

    Stiff_Matrix = Poisson_Stiffness(x0)

    NodalApproximation = NodalQuadrature(x0)
    NodalApproximation[0] = 0

    U = scipy.sparse.linalg.spsolve(Stiff_Matrix, NodalApproximation)

    return U

x = np.linspace(0,1,10)
rich_x = np.linspace(0,1,50)
U = Solver(x)
A_rich = Poisson_Stiffness(rich_x)
U_inter = interp1d(x,U)
U_rich = U_inter(rich_x)
AUrich = A_rich.dot(U_rich)
plt.plot(rich_x,AUrich)
plt.show()

评论 1:

我添加了一个 Stiffness_Matrix = Stiffness_Matrix.tocsr() 语句以避免效率警告。有限元计算非常复杂,我必须打印出一些中间值才能确定发生了什么。

评论 2:

plt.plot(rich_x,A_rich.dot(Solver(rich_x)))剧情不错。您得到的噪声是内插 U_rich 与真实解之间差异的结果:U_rich-Solver(rich_x).

评论 3:

我认为您的代码没有问题。问题在于您可以通过这种方式测试插值。我对 FE 理论很陌生,但我认为你需要使用形状函数进行插值,而不是简单的线性插值。

评论 4:

凭直觉,A_rich.dot(U_rich) 你问的是,什么样的强迫 F 会产生 U_rich。与 Solver(rich_x) 相比,U_rich 具有平坦点,其值小于真实解的区域。 F 会产生什么?一个尖尖的,NodalQuadrature(x)x 点,但两者之间的值接近零。这就是你的情节所显示的。

更高阶的插值将消除平坦点,并产生更平滑的反向计算 F。但是你真的需要重新审视有限元理论。

您可能会发现它很有启发性

plt.plot(x,NodalQuadrature(x))
plt.plot(rich_x, NodalQuadrature(rich_x))

第二个图更平滑,但只有原来的 1/5 左右

最好看看:

plt.plot(rich_x,AUrich,'-*')  # the spikes
plt.plot(x,NodalQuadrature(x),'o')  # original forcing
plt.plot(rich_x, NodalQuadrature(rich_x),'+') # new forcing

在模型中,强制不是连续的,它是每个节点的一个值。节点越多 (rich_x),每个节点的幅度越小。