Take a Poisson equation $$ \begin{align*} -\Delta u &= 1\qquad\text{on $\Omega$} \\ u &= 0\qquad\text{on $\Gamma$} \end{align*} $$ where $\Omega = B_1(0,0)\setminus B_{0.3}(0.7,0)$, $B_r(x,y)$ is the ball with radius $r$ and center at $(x,y)$.
Use finite elements.
Your goal is to compute $\int_{\Omega} u$
Many mesh generators will fail to construct a mesh in that region, so consider two regions, $\Omega_1 = B_1(0,0)\setminus B_{0.3}(0.7-\epsilon,0)$ and $\Omega_2 = B_1(0,0)\setminus B_{0.3}(0.7+\epsilon,0)$ and show that the computed solutions (or integrals of the solutions) are close to each other for small values of $\epsilon$.
In [ ]:
Run a loop for all triangles and assemble the stiffness matrix
Correct rows and columns that correspond to Dirichlet boundary points
Note: use $\verb|scipy.sparse|$ to work with sparse matrices. Note that in the $\verb|scipy.sparse|$ the only sparse format that allows to change elements is lil.
In [ ]:
The RHS for the $i$-th node is $$f_i = \int_\Omega f \eta_i d\Omega .$$ To assemble it run a loop for all triangles and use quadrature rule from the lecture 4.
Note: do not forget to put zeros in the rows that correspond to Dirichlet boundary points.
In [ ]:
Check your code using the fact that for $\Omega = B_1(0,0)$ the solution of $$ \begin{align*} -\Delta u &= 1\qquad\text{on $\Omega$} \\ u &= 0\qquad\text{on $\Gamma$} \end{align*} $$ is $$ u = \frac{1-x^2 - y^2}{4} $$ Note: you can plot the solution using the following code
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
xi = np.linspace(-1, 1, 200)
yi = np.linspace(-1, 1, 200)
z = mlab.griddata(points[:, 0], points[:, 1], sol, xi, yi)
plt.contourf(xi, yi, z)
plt.colorbar()
In [ ]:
Note: $\int_{\Omega} u\, d \Omega$ is not just a sum over all elemets of the solution vector as $u(x,y) \approx \sum с_i \eta_i(x,y)$.
In [ ]:
In [1]:
from IPython.core.display import HTML
def css_styling():
styles = open("./styles/custom.css", "r").read()
return HTML(styles)
css_styling()
Out[1]: