Diffusion of a Gaussian function

Author: Jørgen S. Dokken

Let us now solve a more interesting problem, namely the diffusion of a Gaussian hill. We take the initial value to be

(26)\[\begin{align} u_0(x,y)&= e^{-ax^2-ay^2} \end{align}\]

for \(a=5\) on the domain \([-2,2]\times[-2,2]\). For this problem we will use homogeneous Dirichlet boundary conditions (\(u_D=0\)).

The first difference from the previous problem is that we are not using a unit square. We create the rectangular domain with dolfinx.mesh.create_rectangle.

import numpy as np

from mpi4py import MPI
from petsc4py import PETSc

from dolfinx import fem
from dolfinx.mesh import CellType, create_rectangle, locate_entities_boundary

# Define temporal parameters
t = 0 # Start time
T = 2.0 # Final time
num_steps = 61     
dt = T / num_steps # time step size

# Define mesh
nx, ny = 50, 50
mesh = create_rectangle(MPI.COMM_WORLD, [np.array([-2, -2]), np.array([2, 2])], [nx, ny], CellType.triangle)
V = fem.FunctionSpace(mesh, ("CG", 1))

Note that we have used a much higher resolution that before to better resolve features of the solution. We also easily update the intial and boundary conditions. Instead of using a class to define the initial condition, we simply use a function

# Create initial condition
def initial_condition(x, a=5):
    return np.exp(-a*(x[0]**2+x[1]**2))
u_n = fem.Function(V)
u_n.name = "u_n"

# Create boundary condition
fdim = mesh.topology.dim - 1
boundary_facets = locate_entities_boundary(
    mesh, fdim, lambda x: np.full(x.shape[1], True, dtype=bool))
bc = fem.dirichletbc(PETSc.ScalarType(0), fem.locate_dofs_topological(V, fdim, boundary_facets), V)

Time-dependent output

To visualize the solution in an external program such as Paraview, we create a an XDMFFile which we can store multiple solutions in. The main advantage with an XDMFFile, is that we only need to store the mesh once, and can append multiple solutions to the same grid, reducing the storage space. The first argument to the XDMFFile is which communicator should be used to store the data. As we would like one output, independent of the number of processors, we use the COMM_WORLD. The second argument is the file name of the output file, while the third argument is the state of the file, this could be read ("r"), write ("w") or append ("a").

from dolfinx.io import XDMFFile
xdmf = XDMFFile(MPI.COMM_WORLD, "diffusion.xdmf", "w")

# Define solution variable, and interpolate initial solution for visualization in Paraview
uh = fem.Function(V)
uh.name = "uh"
xdmf.write_function(uh, t)

Variational problem and solver

As in the previous example, we prepare objects for time dependent problems, such that we do not have to recreate data-structures.

import ufl
u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
f = fem.Constant(mesh, PETSc.ScalarType(0))
a = u * v * ufl.dx + dt*ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx 
L = (u_n + dt * f) * v * ufl.dx

Preparing linear algebra structures for time dependent problems

We note that even if u_n is time dependent, we will reuse the same function for f and u_n at every time step. We therefore call dolfinx.fem.form to generate assembly kernels for the matrix and vector.

bilinear_form = fem.form(a)
linear_form = fem.form(L)

We observe that the left hand side of the system, the matrix \(A\) does not change from one time step to another, thus we only need to assemble it once. However, the right hand side, which is dependent on the previous time step u_n, we have to assemble it every time step. Therefore, we only create a vector b based on L, which we will reuse at every time step.

A = fem.assemble_matrix(bilinear_form, bcs=[bc])
b = fem.create_vector(linear_form)

Using petsc4py to create a linear solver

As we have already assembled a into the matrix A, we can no longer use the dolfinx.fem.LinearProblem class to solve the problem. Therefore, we create a linear algebra solver using PETSc, and assign the matrix A to the solver, and choose the solution strategy.

solver = PETSc.KSP().create(mesh.comm)

Visualization of time dependent problem using pyvista

We use the DOLFINx plotting functionality, which is based on pyvista to plot the solution at every \(15\)th time step. We would also like to visualize a colorbar reflecting the minimal and maximum value of \(u\) at each time step. We use the following convenience function plot_function for this:

import pyvista

from dolfinx.plot import create_vtk_topology
topology, cell_types = create_vtk_topology(mesh, mesh.topology.dim)
grid = pyvista.UnstructuredGrid(topology, cell_types, mesh.geometry.x)

def plot_function(t, uh):
    Create a figure of the concentration uh warped visualized in 3D at timet step t.
    p = pyvista.Plotter()
    # Update point values on pyvista grid
    grid.point_data[f"u({t})"] = uh.compute_point_values().real
    # Warp mesh by point values
    warped = grid.warp_by_scalar(f"u({t})", factor=1.5)

    # Add mesh to plotter and visualize in notebook or save as figure
    actor = p.add_mesh(warped)
    if not pyvista.OFF_SCREEN:
        figure_as_array = p.screenshot(f"diffusion_{t:.2f}.png")
        # Clear plotter for next plot
plot_function(0, uh)

Updating the solution and right hand side per time step

To be able to solve the variation problem at each time step, we have to assemble the right hand side and apply the boundary condition before calling solver.solve(b, uh.vector). We start by resetting the values in b as we are reusing the vector at every time step. The next step is to assemble the vector, calling dolfinx.fem.assemble(b, L) which means that we are assemble the linear for L(v) into the vector b. Note that we do not supply the boundary conditions for assembly, as opposed to the left hand side. This is because we want to use lifting to apply the boundary condition, which preserves symmetry of the matrix \(A\) if the bilinear form \(a(u,v)=a(v,u)\) without Dirichlet boundary conditions. When we have applied the boundary condition, we can solve the linear system and update values that are potentially shared between processors. Finally, before moving to the next time step, we update the solution at the previous time step to the solution at this time step.

for i in range(num_steps):
    t += dt

    # Update the right hand side reusing the initial vector
    with b.localForm() as loc_b:
    fem.assemble_vector(b, linear_form)
    # Apply Dirichlet boundary condition to the vector
    fem.apply_lifting(b, [bilinear_form], [[bc]])
    b.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)
    fem.set_bc(b, [bc])

    # Solve linear problem
    solver.solve(b, uh.vector)

    # Update solution at previous time step (u_n)
    u_n.x.array[:] = uh.x.array

    # Write solution to file
    xdmf.write_function(uh, t)
    # Plot every 15th time step
    if i % 15 == 0:
        plot_function(t, uh)


Animation with Paraview

We can also use Paraview to create an animation. We open the file in paraview with File->Open, and then press Apply in the properties panel.

Then, we add a time-annotation to the figure, pressing: Sources->Alphabetical->Annotate Time and Apply in the properties panel. It Is also a good idea to select an output resolution, by pressing View->Preview->1280 x 720 (HD).

Then finally, click File->Save Animation, and save the animation to the desired format, such as avi, ogv or a sequence of pngs. Make sure to set the framerate to something, sensible, in the range of \(5-10\) frames per second.