HeatEquation_1D_FEniCS_matrix_forced#

class fenics_heat(c_nvars=128, t0=0.0, family='CG', order=4, refinements=1, nu=0.1, c=0.0)[source]#

Bases: Problem

Forced 1D heat equation with FEniCS and Dirichlet BCs, IMEX, with the mass matrix inverted in the right-hand side.

Example implementing the forced one-dimensional heat equation with Dirichlet boundary conditions

\[\frac{d u}{d t} = \nu \frac{d^2 u}{d x^2} + f\]

for \(x \in \Omega:=[0,1]\), where the forcing term \(f\) is defined by

\[f(x, t) = -\sin(\pi x) (\sin(t) - \nu \pi^2 \cos(t)).\]

For initial conditions with constant c and

\[u(x, 0) = \sin(\pi x) + c\]

the exact solution of the problem is given by

\[u(x, t) = \sin(\pi x)\cos(t) + c.\]

In this class the problem is implemented in the way that the spatial part is solved using FEniCS [1]. Hence, the problem is reformulated to the weak formulation

The part containing the forcing term is treated explicitly, where it is interpolated in the function space. The other part will be treated in an implicit way.

Parameters:
  • c_nvars (int, optional) – Spatial resolution, i.e., numbers of degrees of freedom in space.

  • t0 (float, optional) – Starting time.

  • family (str, optional) – Indicates the family of elements used to create the function space for the trail and test functions. The default is 'CG', which are the class of Continuous Galerkin, a synonym for the Lagrange family of elements, see [2].

  • order (int, optional) – Defines the order of the elements in the function space.

  • refinements (int, optional) – Denotes the refinement of the mesh. refinements=2 refines the mesh by factor \(2\).

  • nu (float, optional) – Diffusion coefficient \(\nu\).

  • c (float, optional) – Constant for the Dirichlet boundary condition \(c\).

Variables:
  • V (FunctionSpace) – Defines the function space of the trial and test functions.

  • M (scalar, vector, matrix or higher rank tensor) – Denotes the expression \(\int_\Omega u_t v\,dx\).

  • K (scalar, vector, matrix or higher rank tensor) – Denotes the expression \(- \nu \int_\Omega \nabla u \nabla v\,dx\).

  • g (Expression) – The forcing term \(f\) in the heat equation.

  • bc (DirichletBC) – Denotes the Dirichlet boundary conditions.

References

apply_mass_matrix(u)[source]#

Routine to apply mass matrix.

Parameters:

u (dtype_u) – Current values of the numerical solution.

Returns:

me (dtype_u) – The product \(M \vec{u}\).

dtype_f#

alias of rhs_fenics_mesh

dtype_u#

alias of fenics_mesh

eval_f(u, t)[source]#

Routine to evaluate both parts of the right-hand side of the problem.

Parameters:
  • u (dtype_u) – Current values of the numerical solution.

  • t (float) – Current time at which the numerical solution is computed.

Returns:

f (dtype_f) – The right-hand side divided into two parts.

eval_f_increment(base, delta, t)[source]#

Evaluate the right-hand side increment.

Parameters:
  • base (dtype_u) – The base state, unused: the implicit part is linear.

  • delta (dtype_u) – The correction.

  • t (float) – Physical time, accepted for interface compatibility.

Returns:

dtype_f – The increment, with a zero explicit part.

solve_system(rhs, factor, u0, t)[source]#

Dolfin’s linear solver for \((M - factor \cdot A) \vec{u} = \vec{rhs}\).

Parameters:
  • rhs (dtype_f) – Right-hand side for the nonlinear system.

  • factor (float) – Abbrev. for the node-to-node stepsize (or any other factor required).

  • u0 (dtype_u) – Initial guess for the iterative solver (not used here so far).

  • t (float) – Current time.

Returns:

u (dtype_u) – Solution.

solve_system_delta(r, factor, base, f_base, t)[source]#

Solve \(\delta - factor\,[f(w+\delta) - f(w)] = r\), i.e. \((M - factor\,K)\,\delta = M r\) with zero boundary data.

This is the piece linear_implicit=True cannot supply on this backend. That shortcut reuses the stock solve_system, which applies inhomogeneous Dirichlet data to whatever right-hand side it is handed, and a correction must carry zero boundary data. Applying bc_hom instead is the whole difference.

Without this the sweeper falls back to the substitution \(y = w + \delta\), which is exact but reads the level’s \(\mathcal{O}(1)\) state. That is merely no benefit while the level is at backend precision, and is a wrong answer once it is not – measured here as 1.4e-05 with the coarse level at float32.

Parameters:
  • r (dtype_u) – Right-hand side of the correction equation.

  • factor (float) – Implicit prefactor assembled by the sweeper.

  • base (dtype_u) – Base state, unused: the implicit operator is linear.

  • f_base (dtype_f) – f evaluated at base, unused for the same reason.

  • t (float) – Physical time, accepted for interface compatibility.

Returns:

dtype_u – The correction.

u_exact(t)[source]#

Routine to compute the exact solution at time \(t\).

Parameters:

t (float) – Time of the exact solution.

Returns:

me (dtype_u) – Exact solution.

class fenics_heat_mass(c_nvars=128, t0=0.0, family='CG', order=4, refinements=1, nu=0.1, c=0.0)[source]#

Bases: fenics_heat

Forced 1D heat equation with FEniCS and Dirichlet BCs, IMEX, with the mass matrix applied instead of inverted.

Example implementing the forced one-dimensional heat equation with Dirichlet boundary conditions

\[\frac{d u}{d t} = \nu \frac{d^2 u}{d x^2} + f\]

for \(x \in \Omega:=[0,1]\), where the forcing term \(f\) is defined by

\[f(x, t) = -\sin(\pi x) (\sin(t) - \nu \pi^2 \cos(t)).\]

For initial conditions with constant c and

\[u(x, 0) = \sin(\pi x) + c\]

the exact solution of the problem is given by

\[u(x, t) = \sin(\pi x)\cos(t) + c.\]

In this class the problem is implemented in the way that the spatial part is solved using FEniCS [3]. Hence, the problem is reformulated to the weak formulation

The forcing term is treated explicitly, and is expressed via the mass matrix resulting from the left-hand side term \(\int_\Omega u_t v\,dx\), and the other part will be treated in an implicit way.

Parameters:
  • c_nvars (int, optional) – Spatial resolution, i.e., numbers of degrees of freedom in space.

  • t0 (float, optional) – Starting time.

  • family (str, optional) – Indicates the family of elements used to create the function space for the trail and test functions. The default is 'CG', which are the class of Continuous Galerkin, a synonym for the Lagrange family of elements, see [4].

  • order (int, optional) – Defines the order of the elements in the function space.

  • refinements (int, optional) – Denotes the refinement of the mesh. refinements=2 refines the mesh by factor \(2\).

  • nu (float, optional) – Diffusion coefficient \(\nu\).

  • c (float, optional) – Constant for the Dirichlet boundary condition \(c\).

Variables:
  • V (FunctionSpace) – Defines the function space of the trial and test functions.

  • M (scalar, vector, matrix or higher rank tensor) – Denotes the expression \(\int_\Omega u_t v\,dx\).

  • K (scalar, vector, matrix or higher rank tensor) – Denotes the expression \(- \nu \int_\Omega \nabla u \nabla v\,dx\).

  • g (Expression) – The forcing term \(f\) in the heat equation.

  • bc (DirichletBC) – Denotes the Dirichlet boundary conditions.

  • bc_hom (DirichletBC) – Denotes the homogeneous Dirichlet boundary conditions, potentially required for fixing the residual

  • fix_bc_for_residual (boolean) – flag to indicate that the residual requires special treatment due to boundary conditions

References

eval_f(u, t)[source]#

Routine to evaluate both parts of the right-hand side.

Parameters:
  • u (dtype_u) – Current values of the numerical solution.

  • t (float) – Current time at which the numerical solution is computed.

Returns:

f (dtype_f) – The right-hand side divided into two parts.

fix_residual(res)[source]#

Applies homogeneous Dirichlet boundary conditions to the residual

Parameters:

res (dtype_u) – Residual

solve_system(rhs, factor, u0, t)[source]#

Dolfin’s linear solver for \((M - factor A) \vec{u} = \vec{rhs}\).

Parameters:
  • rhs (dtype_f) – Right-hand side for the nonlinear system.

  • factor (float) – Abbrev. for the node-to-node stepsize (or any other factor required).

  • u0 (dtype_u) – Initial guess for the iterative solver (not used here so far).

  • t (float) – Current time.

Returns:

u (dtype_u) – Solution.

class fenics_heat_mass_timebc(c_nvars=128, t0=0.0, family='CG', order=4, refinements=1, nu=0.1, c=0.0)[source]#

Bases: fenics_heat_mass

Forced 1D heat equation with FEniCS, IMEX with the mass matrix applied, and time-dependent Dirichlet BCs.

Example implementing the forced one-dimensional heat equation with time-dependent Dirichlet boundary conditions

\[\frac{d u}{d t} = \nu \frac{d^2 u}{d x^2} + f\]

for \(x \in \Omega:=[0,1]\), where the forcing term \(f\) is defined by

\[f(x, t) = -\cos(\pi x) (\sin(t) - \nu \pi^2 \cos(t)).\]

and the boundary conditions are given by

\[u(x, t) = \cos(\pi x)\cos(t).\]

The exact solution of the problem is given by

\[u(x, t) = \cos(\pi x)\cos(t) + c.\]

In this class the problem is implemented in the way that the spatial part is solved using FEniCS [5]. Hence, the problem is reformulated to the weak formulation

The forcing term is treated explicitly, and is expressed via the mass matrix resulting from the left-hand side term \(\int_\Omega u_t v\,dx\), and the other part will be treated in an implicit way.

Parameters:
  • c_nvars (int, optional) – Spatial resolution, i.e., numbers of degrees of freedom in space.

  • t0 (float, optional) – Starting time.

  • family (str, optional) – Indicates the family of elements used to create the function space for the trail and test functions. The default is 'CG', which are the class of Continuous Galerkin, a synonym for the Lagrange family of elements, see [6].

  • order (int, optional) – Defines the order of the elements in the function space.

  • refinements (int, optional) – Denotes the refinement of the mesh. refinements=2 refines the mesh by factor \(2\).

  • nu (float, optional) – Diffusion coefficient \(\nu\).

  • c (float, optional) – Constant \(c\) added to the exact solution and hence to the time-dependent Dirichlet boundary condition.

Variables:
  • V (FunctionSpace) – Defines the function space of the trial and test functions.

  • M (scalar, vector, matrix or higher rank tensor) – Denotes the expression \(\int_\Omega u_t v\,dx\).

  • K (scalar, vector, matrix or higher rank tensor) – Denotes the expression \(- \nu \int_\Omega \nabla u \nabla v\,dx\).

  • g (Expression) – The forcing term \(f\) in the heat equation.

  • bc (DirichletBC) – Denotes the time-dependent Dirichlet boundary conditions.

  • bc_hom (DirichletBC) – Denotes the homogeneous Dirichlet boundary conditions, potentially required for fixing the residual

  • fix_bc_for_residual (boolean) – flag to indicate that the residual requires special treatment due to boundary conditions

References

solve_system(rhs, factor, u0, t)[source]#

Dolfin’s linear solver for \((M - factor A) \vec{u} = \vec{rhs}\).

Parameters:
  • rhs (dtype_f) – Right-hand side for the nonlinear system.

  • factor (float) – Abbrev. for the node-to-node stepsize (or any other factor required).

  • u0 (dtype_u) – Initial guess for the iterative solver (not used here so far).

  • t (float) – Current time.

Returns:

u (dtype_u) – Solution.

u_exact(t)[source]#

Routine to compute the exact solution at time \(t\).

Parameters:

t (float) – Time of the exact solution.

Returns:

me (dtype_u) – Exact solution.