Note
Go to the end to download the full example code.
Dirichlet condition for infinite horizon kernel
The following example can be found at examples/example_InfHorizonDirichlet.py in the PyNucleus repository.
We want to solve a problem with infinite horizon fractional kernel
where
where
import numpy as np
import matplotlib.pyplot as plt
from PyNucleus import meshFactory, dofmapFactory, kernelFactory, functionFactory, solverFactory
Construct a mesh for
radius = 1.0
mesh = meshFactory('disc', n=8, radius=radius)
for _ in range(4):
mesh = mesh.refine()
Get an indicator function for
OmegaIndicator = functionFactory('radialIndicator', 0.5*radius-1e-6)
dm = dofmapFactory('P1', mesh, OmegaIndicator)
dmBC = dm.getComplementDoFMap()
plt.figure()
dm.plot(printDoFIndices=False)
plt.figure()
dmBC.plot(printDoFIndices=False)
Set up the kernel, rhs and known solution.
s = 0.75
kernel = kernelFactory('fractional', dim=mesh.dim, s=s, horizon=np.inf)
rhs = functionFactory('constant', 1.)
uex = functionFactory('solFractional', dim=mesh.dim, s=s, radius=radius)
Assemble the linear system
Set up a solver for
A_OmegaOmega = dm.assembleNonlocal(kernel, matrixFormat='H2')
A_OmegaOmegaI = dm.assembleNonlocal(kernel, dm2=dmBC)
f = dm.assembleRHS(rhs)
g = g = dmBC.interpolate(uex)
solver = solverFactory('lu', A=A_OmegaOmega, setup=True)
Efficiency of assembly with 2 DoFMaps is bad.
Compute FE solution and error between FE solution and interpolation of analytic expression
u_Omega = dm.zeros()
solver(f-A_OmegaOmegaI*g, u_Omega)
u = u_Omega.augmentWithBoundaryData(g)
err = u.dm.interpolate(uex) - u
Plot FE solution and error
plt.figure()
plt.title('FE solution')
u.plot(flat=True)
plt.figure()
plt.title('error')
err.plot(flat=True)
<matplotlib.collections.PolyCollection object at 0x7fbedb9fda90>
Total running time of the script: (0 minutes 10.188 seconds)