Simple diffusion with varying temperature#
This is an extension of the simple MMS example with a non-homogenous temperature gradient.
We will only consider diffusion of hydrogen in a unit square domain \(\Omega\) at steady state with an homogeneous diffusion coefficient \(D\).
We will assume a temperature gradient of \(T = 300 + x\) over the domain \(\Omega\) so the diffusivity coefficient \(D = D_0 \exp\left[-\frac{E_D}{k_B T}\right]\).
Moreover, a Dirichlet boundary condition will be assumed on the boundaries \(\partial \Omega \).
The problem is therefore:
The exact solution for mobile concentration is:
Injecting (9) in (8), we obtain the expressions of \(S\) and \(c_0\):
We can then run a FESTIM model with these values and compare the numerical solution with \(c_\mathrm{exact}\).
FESTIM code#
Comparison with exact solution#
computed_solution = H.solution
E_l2 = error_L2(computed_solution, exact_solution)
exact_solution_function = dolfinx.fem.Function(computed_solution.function_space)
exact_solution_function.interpolate(exact_solution)
E_max = np.max(np.abs(exact_solution_function.x.array - computed_solution.x.array))
print(f"L2 error: {E_l2:.2e}")
print(f"Max error: {E_max:.2e}")
L2 error: 8.75e-03
Max error: 1.57e-05
import pyvista
from dolfinx.plot import vtk_mesh
pyvista.start_xvfb()
pyvista.set_jupyter_backend("html")
def get_u_grid(computed_solution: dolfinx.fem.Function, label: str):
u_topology, u_cell_types, u_geometry = vtk_mesh(computed_solution.function_space)
u_grid = pyvista.UnstructuredGrid(u_topology, u_cell_types, u_geometry)
u_grid.point_data[label] = computed_solution.x.array.real
u_grid.set_active_scalars(label)
return u_grid
u_grid_mobile = get_u_grid(computed_solution, "c_mobile")
u_grid_mobile_exact = get_u_grid(exact_solution_function, "c_mobile_exact")
u_plotter = pyvista.Plotter(shape=(1, 2))
u_plotter.subplot(0, 0)
u_plotter.add_mesh(u_grid_mobile, show_edges=True)
u_plotter.view_xy()
u_plotter.subplot(0, 1)
u_plotter.add_mesh(u_grid_mobile_exact, show_edges=True)
u_plotter.view_xy()
if not pyvista.OFF_SCREEN:
u_plotter.show()
else:
figure = u_plotter.screenshot("simple_temperature.png")
/home/docs/checkouts/readthedocs.org/user_builds/festim-vv-report/conda/festim-2/lib/python3.11/site-packages/pyvista/plotting/utilities/xvfb.py:48: PyVistaDeprecationWarning: This function is deprecated and will be removed in future version of PyVista. Use vtk-osmesa instead.
warnings.warn(
Compute convergence rates#
It is also possible to compute how the numerical error decreases as we increase the number of cells. By iteratively refining the mesh, we find that the error exhibits a second order convergence rate. This is expected for this particular problem as first order finite elements are used.