# Solving Partial Differential Equations with Finite Elements

## Introduction

The aim of this tutorial is to give an introductory overview of the finite element method (FEM) as it is implemented in NDSolve. The notebook introduces finite element method concepts for solving partial differential equations (PDEs). First, typical workflows are discussed. The setup of regions, boundary conditions, and equations is followed by the solution of the PDE with NDSolve. The visualization and animation of the solution is then introduced, and some theoretical aspects of the finite element method are presented. Classical PDEs such as the Poisson and Heat equations are discussed. Coupled PDEs are also introduced with an example from structural mechanics.

### Why Finite Elements?

Explicit closed-form solutions for partial differential equations (PDEs) are rarely available. The finite element method (FEM) is a technique to solve partial differential equations numerically. It is important for at least two reasons. First, the FEM is able to solve PDEs on almost any arbitrarily shaped region. Second, the method is well suited for use on a large class of PDEs. While it is almost always possible to conceive better methods for a specific PDE on a specific region, the finite element method performs quite well for a large class of PDEs. In summary, the finite element method is important since it can deal with:

### What Is Needed for a Finite Element Analysis

To solve partial differential equations with the finite element method, three components are needed:

This section deals with partial differential equations and their boundary conditions. Finite element meshes can be generated with ToElementMesh. A treatise on the generation of finite element meshes can be found in "Element Mesh Generation".

### The Scope of the Finite Element Method as Implemented in NDSolve

The current version of the implementation of the finite element method supports the following features:

- linear variable coefficient Dirichlet conditions, generalized Neumann (Robin) boundary values, and a mixture of these boundary conditions are possible

The following sections provide an overview of this functionality.

For convenient use of the finite element functionality, load the finite element package.

In[67]:= |

## Regions

To perform a finite element analysis, a region over which the partial differential equation is to be solved needs to be defined.

Rectangular regions may be specified using {v,v_{min},v_{max}} for each of the spatial independent variables.

Regions of arbitrary shape may be specified using the notation vars∈Ω, where Ω is a region so that RegionQ[Ω] gives True.

One way to specify a nonrectangular region is by using Boolean predicates. A predicate is a function that either returns True or False. Since the term *predicate* has many meanings in mathematical literature, the terminology *Boolean predicate* is used to emphasize that the predicate returns a Boolean. To describe regions, Boolean predicates can be used as in functions like RegionPlot and RegionPlot3D, for example.

This tutorial concentrates on solving partial differential equations with the finite element method, without emphasis on the creation of regions and meshes. More detailed information on this topic can be found in "Element Mesh Generation".

## Classical Partial Differential Equations

### The Coefficient Form of Partial Differential Equations

What types of equations can be solved with the finite element method as implemented in NDSolve? Consider a single linear partial differential equation in :

The PDE is defined in . Here is the dependent variable for which a solution is sought. The coefficients , , , and are scalars; , , and are vectors; and is an × matrix.

What follows are some well-known PDEs and their corresponding coefficients. To illustrate the generality of (1), the components that are relevant to a specific equation are black, while the non-relevant components are gray.

The Laplace equation simply contains a diffusive term:

To model Poisson's equation, only a small modification is needed; add a load term :

Helmholtz's equation adds a reaction term :

Convection-diffusion-reaction type equations are another common class of PDEs. Compared to the previous examples, these have an additional convection term :

The PDEs considered so far are stationary, i.e. they have no time dependence. The heat equation adds time dependence to the Poisson equation. It has the following form:

Similarly, the wave equation is given as:

Equation (1) provides the components for modeling a range of different phenomena, since it provides spatial derivatives up to order 2.

### Poisson’s Equation with Dirichlet Conditions

The Poisson equation is to be solved over a region with Dirichlet boundary conditions. First, a region needs to be defined where the equation will be solved. Then the equation and boundary conditions are defined. Finally, the equation is solved over the region.

One way to specify a region is by using Boolean predicates.

Next, define a Poisson operator.

In[7]:= |

The third step is to set up the boundary conditions. The first Dirichlet boundary condition enforces the dependent variable to a value of 0 wherever the evaluation of x0∧8≤y≤10 yields True on the boundary of the region . In this case, this is the upper-left part of the boundary. The second Dirichlet boundary condition specifies the value of the dependent variable whenever yields True on the boundary of region .

In[8]:= |

In the solution step, NDSolve needs the PDE, the boundary conditions, and the region.

Internally, the region is converted to a finite element mesh and a finite element method is used to solve the equation automatically.

To visualize the solution, a contour plot can be used.

### Partial Differential Equations and Boundary Conditions

Two primary types of boundary conditions are used. Dirichlet boundary conditions prescribe a condition on the dependent variable of value or at a part of the boundary of :

On the other hand, generalized Neumann boundary values, also known as Robin values, specify a value . The value prescribes a flux over the outward normal on some part of the boundary:

The finite element method is based on the weak form of the differential equation. This form is obtained by taking equation (1), multiplying it by a so-called test function , and integrating over the region :

This process is done internally. The integrand in the boundary integral in (11) can be replaced with (9) and yields

specifies the value of the boundary integral integrand (11) in the weak form and thus the name NeumannValue. Other boundary conditions are conceivable, but currently not implemented.

In most cases, Dirichlet boundary conditions need not be associated with a particular equation and can be specified independent of the equation. They are specified as equations. Generalized Neumann values, on the other hand, are specified by giving a value, since the equation satisfied is implicit in the value. Neumann values are mathematically tied to the PDE.

For practical reasons, the NeumannValue is part of the equation. Consider a system of coupled PDEs. One would like to be able to unambiguously specify any given Neumann value to any given single PDE of that system of PDEs. It is not possible to derive (unambiguously) from the Neumann value with which PDE equation the value should be associated. Making a NeumannValue part of a PDE equation solves this problem. By making the NeumannValue part of the equation, the association of the Neumann value with the PDE equation becomes unambiguous.

{op[u[x_{1},…]] Γ_{N},Γ_{D}} | partial differential equation with differential operator op, Neumann boundary values Γ_{N}, and Dirichlet boundary condition equations Γ_{D} |

{op_{1}[u[x_{1},…],v[x_{1},…],…] Γ_{N1},op_{2}[u[x_{1},…],v[x_{1},…],…] Γ_{N2},…,Γ_{D}} | system of coupled partial differential equation operators op_{j}, Neumann boundary values Γ_{Nj}, and Dirichlet boundary condition equations Γ_{D} |

Specifying partial differential equations with boundary conditions.

Both DirichletCondition and NeumannValue also need as a second argument a predicate describing the location on the boundary where the conditions/values are to be applied.

A partial differential equation typically needs at least one Dirichlet boundary condition on some part of the region to be uniquely solvable. For this reason, Dirichlet boundary conditions are also called essential boundary conditions.

The use of generalized Neumann values arises from the boundary integral from the weak form, and so they are also commonly referred to as natural conditions.

A pure Neumann boundary equation is a generalized Neumann boundary equation (9) where :

If is also not specified, then a Neumann zero boundary value is automatically implied ():

Note that when the Neumann value is zero, the term drops out in (12). This is typically a case where there is no flux or stress at the boundary. When no boundary conditions are explicitly specified for a part of the region boundary , a Neumann zero value is assumed.

### Poisson’s Equation with Neumann Values

Consider the solution of Poisson's equation with generalized Neumann boundary values.

In[18]:= |

Poisson's equation is given as:

The corresponding Neumann boundary equation is:

Note how the normal derivative of the Neumann-type equation relates to the PDE. In the Neumann boundary equation, one specifies the values of and on the right-hand side. The left-hand side is implicitly specified though the PDE. If the coefficient in the PDE is given as an identity matrix, then the coefficient on the left-hand side of the Neumann equation is that same coefficient .

The Neumann boundary value is specified though the values and . Typically when , one speaks of a Neumann boundary value, and in the case , one speaks of a generalized Neumann or Robin boundary value. It is common to use the term Neumann boundary condition for both the generalized Neumann and Neumann boundary value.

In[20]:= |

In[21]:= |

It is important to observe that the constants in the term of the Neumann boundary equation exactly match the constants in from the weak form of the PDE (11). Because these terms are required to match, NeumannValue only requires the user to enter the right-hand side of the boundary equation. For example, to enter the boundary equation , it suffices to enter NeumannValue[1,…]. The correct expression for the left-hand side of the boundary equation (i.e. ) is implied by the term in the weak form of the PDE (12). This close relation of the Neumann boundary value and the PDE is further accommodated by making the NeumannValue part of the equation.

In the previous examples, not all boundaries had boundary conditions explicitly attributed; for those parts of the boundaries, the Neumann zero condition is used.

### Partial Differential Equations with Variable Coefficients

The PDE coefficients may depend on space and time. Here indicates the temporal variable and the spatial variables:

The example of a shielded microstrip illustrates the effect of variable coefficients. It is an electrostatics application that can be modeled with a Poisson equation where the coefficient is the relative permittivity and depends on space:

A geometry is constructed where the boundary separates two materials. In each material, has a different value.

First, a boundary mesh is created. For more information about creating element meshes, please consult ToBoundaryMesh.

The conversion of the boundary mesh to an element mesh is done with ToElementMesh.

Next, the PDE is set up. The load term of the PDE is the charge density divided by the vacuum permittivity :

The lower part of the geometry is silicon, while the upper part is vacuum. Silicon has a relative permittivity of about 11.7, while vacuum has a value of 1.

The charge density and vacuum permittivity are given as follows.

In[31]:= |

The potential is set to 0 wherever . A second potential is set to 1000 in the inner strip.

In[34]:= |

In many cases when there are explicit discontinuities in the PDE coefficients, NDSolve automatically includes these in the mesh it generates to solve the problem. The discontinuity at is automatically detected for this example.

As can be seen, the original input region did not have an internal boundary at the discontinuity, but during the mesh generation process the internal line along the discontinuity was added.

### Heat Equation

The heat equation is a time-dependent Poisson equation

where the dependent variable depends on the spatial coordinates and time .

Both Dirichlet boundary conditions and Neumann boundary values may also depend on time.

The overall procedure to solve PDEs remains the same: a region needs to be specified and a PDE with boundary conditions needs to be set up.

In[42]:= |

The dependent variable now depends on time and space.

In[43]:= |

Also, the boundary conditions may depend on space and time.

In[44]:= |

In[45]:= |

To solve first-order time-dependent problems, NDSolve needs an initial condition and a time region.

In[46]:= |

Note that it is more efficient to convert the mesh once and use the discretized mesh during the plotting instead of rediscretizing the region for every animation frame.

### Wave Equation

The wave equation is a second-order time-dependent equation:

In[48]:= |

In[49]:= |

In[50]:= |

The wave equation is a second-order time-dependent PDE, and initial conditions and the derivative of the initial condition must be specified.

In[51]:= |

In[52]:= |

To make an animation of the resulting InterpolatingFunction, use Plot3D and Animate.

### Formal Partial Differential Equations

In the preceding example, the PDE is set up by making use of Laplacian. The exact same PDE can be formulated with Div and Grad. To do so, use the coefficient in . Here is an × matrix where is the number of dimensions in .

To prevent evaluation, Inactive can be used.

The formal version of the PDE allows you to easily modify the coefficient matrix .

One scenario where the use of Inactive can be important is when modeling the relation between a PDE and a Neumann value. Consider the following PDE:

The corresponding Neumann boundary equation is

Consider a PDE where is the identity matrix and is given by . Specifying the PDE without the use of Inactive will cause the expression to evaluate.

Note, however, that the evaluation changes the PDE to the following:

In[63]:= |

Even though the PDEs are equivalent, the corresponding Neumann values are not:

For the second PDE, the Neumann value modeled is the following:

To prevent evaluation and model a Neumann value of the following form, the use of Inactivate is needed.

In[66]:= |

A more in-depth discussion of formal PDEs can be found in "Finite Element Method Usage Tips".

Also, note that while it is possible to specify a coefficient as a function, this deprives the internal code of the ability to optimize the coefficient integration. In this specific case, the coefficient is constant, and with the optimization, the coefficient is only integrated once. A black box function would need continuous evaluations over the region.

## Systems of Partial Differential Equations

### The Coefficient Form of Systems of Partial Differential Equations

Consider a system of two equations with dependent variables and :

For a region in , each is an × matrix, and each of , , and are vectors.

In this case, the generalized Neumann equations are

### One-Dimensional Coupled Partial Differential Equation

The following example is set up such that the parameters are easy to modify. First, the region is specified.

The PDE has two dependent variables and . The coupling is between two diffusion operators. Note that since this is a one-dimensional system, each coefficient is a 1×1 matrix, which is equivalent to a number.

In[69]:= |

Each equation has its own set of boundary conditions. In the most general form, Dirichlet boundary conditions are now systems of conditions:

The current implementation does not support cross-coupling in Dirichlet boundary conditions. The terms and cannot be given.

Neumann values specify a system of the following form:

Next, the boundary conditions are set up.

In[72]:= |

In[74]:= |

In[76]:= |

In[78]:= |

In[80]:= |

Here is the analytical solution of the system of PDEs computed with DSolve.

Note how in the DSolve formulation of the system of equations the generalized Neumann boundary equations have to be given explicitly, and how their coefficients match those in the PDE.

To compare the analytical result to the numeric result, make a plot of the difference between the two.

This example also makes apparent why the NeumannValue is part of the equation. The equations for the Neumann value are given by:

There is no way to know from the Neumann value with which equation it should be associated. Making the Neumann value part of the equation disambiguates with which equation the Neumann value is associated.

### Structural Mechanics

In structural mechanics, the deformation of objects under load is computed. Structural mechanics provides good examples of coupled PDEs. As an example region, consider a beam five units long and one unit high.

The PDE that models the physics is a plane stress relation and is valid for thin objects. Here, "thin" means thin relative to the other dimensions of the object. The plane stress formulation depends on two dependent variables and the independent variables. The two dependent variables give the component-wise contribution of the deformed object. As material data, the Young's modulus and the Poisson ratio need to be specified.

In[84]:= |

Furthermore, a boundary load may be specified. In this case, the boundary load applies at the right edge of the beam and a force of unit is applied in the negative direction.

On the left-hand side of the region, both dependent variables are held fixed with 0 displacement.

In[86]:= |

In[87]:= |

In order to show the deformed beam, an element mesh must be created for the beam at rest, which is deformed by the interpolating functions given.

More information about the usage of ElementMeshDeformation can be found in "Element Mesh Visualization".

The following contour plots show the deformation of the beam. The dependent represents the deformation in the direction. It can be seen that the top-right part of the beam experiences a shift in the positive direction. The lower-right part is moved in the negative direction.

The contour plot of the dependent represents the deformation in the direction. The right-hand side of the beam undergoes a deformation in the negative direction.

Note that the color scale of the two contour plots is not the same.

For completeness, a PDE that models the physics of a plane strain relation is presented. The plane strain formulation is, in contrast to the plane stress formulation, valid for thick objects. Here, "thick" means thick relative to the other dimensions of the object. The plane strain formulation depends on two dependent variables and the independent variables. The two dependent variables give the component-wise contribution of the deformed object. As material data, the Young's modulus and the Poisson ratio need to be specified.

In[92]:= |

The boundary conditions this time are prescribed displacements on both sides. On the left-hand side of the region, both dependent variables are held fixed with 0 displacement. On the right-hand side, a displacement of one unit in the direction of negative axes is given and the displacement on the direction is held fixed.

In[94]:= |

In[95]:= |

In order to show the deformed beam, an element mesh must be created for the beam at rest, which is deformed by the interpolating functions given.

The following contour plots show the additive deformation of the beam. The dependent represents the deformation in the direction, and the dependent represents the deformation in the direction.

### Fluid flow

A linear model of a fluid flow is Stokes's equation. In this example, a flow through a narrowing tube is analyzed.

In[101]:= |

Here represents the fluid's velocity in the direction and represents the velocity in the direction. stands for the pressure in the fluid.

In[102]:= |

The inflow velocity in the direction is prescribed with the boundary conditions. This is done by specifying an inflow profile for the dependent variable on the left-hand side of the region. On the bottom and upper part of the region, a no-flow condition is specified. This is done by setting both the and components on those parts to 0. An outflow condition is set on the right-hand side of the domain. Here the pressure is arbitrarily set to 0.

In[103]:= |

A stable solution can be found if the velocities are interpolated with a higher order than the pressure. NDSolve allows an interpolation order for each dependent variable to be specified.

In[104]:= |

An explanation of the usage of the finite element method option interpolation order is given in "Finite Element Method Usage Tips".