Linear Reaction-Diffusion Equations

Next, we write a specialised solver for solving linear reaction-diffusion equations. What we produce in this section can also be accessed in FiniteVolumeMethod.LinearReactionDiffusionEquation.

Mathematical Details

To start, let's give the mathematical details. The problems we will be solving take the form

\[\pdv{u}{t} = \div\left[D(\vb x)\grad u\right] + f(\vb x)u.\]

We want to turn this into an equation of the form $\mathrm d\vb u/\mathrm dt = \vb A\vb u + \vb b$ as usual. This takes the same form as our diffusion equation example, except with the extra $f(\vb x)u$ term, which just adds an extra $f(\vb x)$ term to the diagonal of $\vb A$. See the previois sections for further mathematical details.

Implementation

Let us now implement the solver. For constructing $\vb A$, we can use FiniteVolumeMethod.triangle_contributions! as in the previous sections, but we will need an extra function to add $f(\vb x)$ to the appropriate diagonals. We can also reuse apply_dirichlet_conditions!, apply_dudt_conditions, and boundary_edge_contributions! from the diffusion equation example. Here is our implementation.

using FiniteVolumeMethod, SparseArrays, OrdinaryDiffEq, LinearAlgebra, SciMLOperators
const FVM = FiniteVolumeMethod
function linear_source_contributions!(
        A, mesh, conditions, source_function, source_parameters)
    for i in each_solid_vertex(mesh.triangulation)
        if !FVM.has_condition(conditions, i)
            x, y = get_point(mesh, i)
            A[i, i] += source_function(x, y, source_parameters)
        end
    end
end
function linear_reaction_diffusion_equation(mesh::FVMGeometry,
        BCs::BoundaryConditions,
        ICs::InternalConditions = InternalConditions();
        diffusion_function,
        diffusion_parameters = nothing,
        source_function,
        source_parameters = nothing,
        initial_condition,
        initial_time = 0.0,
        final_time)
    conditions = Conditions(mesh, BCs, ICs)
    n = DelaunayTriangulation.num_solid_vertices(mesh.triangulation)
    Afull = zeros(n + 1, n + 1)
    A = @views Afull[begin:(end - 1), begin:(end - 1)]
    b = @views Afull[begin:(end - 1), end]
    _ic = vcat(initial_condition, 1)
    FVM.triangle_contributions!(
        A, mesh, conditions, diffusion_function, diffusion_parameters)
    FVM.boundary_edge_contributions!(
        A, b, mesh, conditions, diffusion_function, diffusion_parameters)
    linear_source_contributions!(A, mesh, conditions, source_function, source_parameters)
    FVM.apply_dudt_conditions!(b, mesh, conditions)
    FVM.apply_dirichlet_conditions!(_ic, mesh, conditions)
    FVM.fix_missing_vertices!(A, b, mesh)
    Af = sparse(Afull)
    prob = ODEProblem(MatrixOperator(Af), _ic, (initial_time, final_time))
    return prob
end
linear_reaction_diffusion_equation (generic function with 2 methods)

If you go and look back at the diffusion_equation function from the diffusion equation example, you will see that this is essentially the same function except we now have linear_source_contributions! and source_function and source_parameters arguments.

Let's now test this function. We consider the problem

\[\pdv{T}{t} = \div\left[10^{-3}x^2y\grad T\right] + (x-1)(y-1)T, \quad \vb x \in [0,1]^2,\]

with $\grad T \vdot\vu n = 1$ on the boundary.

using DelaunayTriangulation
tri = triangulate_rectangle(0, 1, 0, 1, 150, 150, single_boundary = true)
mesh = FVMGeometry(tri)
BCs = BoundaryConditions(mesh, (x, y, t, u, p) -> one(x), Neumann)
diffusion_function = (x, y, p) -> p.D * x^2 * y
diffusion_parameters = (D = 1e-3,)
source_function = (x, y, p) -> (x - 1) * (y - 1)
initial_condition = [x^2 + y^2 for (x, y) in DelaunayTriangulation.each_point(tri)]
final_time = 8.0
prob = linear_reaction_diffusion_equation(mesh, BCs;
    diffusion_function, diffusion_parameters,
    source_function, initial_condition, final_time)
ODEProblem with uType Vector{Float64} and tType Float64. In-place: true
Non-trivial mass matrix: false
timespan: (0.0, 8.0)
u0: 22501-element Vector{Float64}:
 0.0
 4.504301608035674e-5
 0.00018017206432142696
 0.00040538714472321063
 0.0007206882572857079
 ⋮
 1.9601369307688843
 1.9733345344804287
 1.986622224224134
 2.0
 1.0
sol = solve(prob, Tsit5(); saveat = 2)
retcode: Success
Interpolation: 1st order linear
t: 5-element Vector{Float64}:
 0.0
 2.0
 4.0
 6.0
 8.0
u: 5-element Vector{Vector{Float64}}:
 [0.0, 4.504301608035674e-5, 0.00018017206432142696, 0.00040538714472321063, 0.0007206882572857079, 0.0011260754020089186, 0.0016215485788928425, 0.0022071077879374803, 0.0028827530291428314, 0.0036484843025088956  …  1.8955002026935723, 1.9082473762443133, 1.921084635827215, 1.9340119814422772, 1.9470294130895003, 1.9601369307688843, 1.9733345344804287, 1.986622224224134, 2.0, 1.0]
 [5.392972715241144e-10, 0.0003283969313676684, 0.00129607599906578, 0.002877290944573624, 0.005046982038098887, 0.007780761845897449, 0.011054900287088434, 0.01484630998784744, 0.01913253192748716, 0.023891721371031457  …  1.8576012589880757, 1.866918752086122, 1.8756974896164813, 1.884763887437324, 1.8917905879259964, 1.9028816755063147, 1.902358803429383, 1.9299909658674956, 1.8825037936504083, 1.0]
 [7.916735417539227e-9, 0.0023942552773780646, 0.009323370071034572, 0.02042192008254055, 0.035343899135842786, 0.05376187635010655, 0.07536617230272592, 0.09986406790939059, 0.12697904481386743, 0.1564500561223995  …  1.850913610507159, 1.8585916000623353, 1.866489589989134, 1.8731272794777782, 1.8825835384209193, 1.884116496741212, 1.905311415327487, 1.8762173442198424, 1.9784909526935537, 1.0]
 [8.716291359760387e-8, 0.01745587228824834, 0.06706791816304747, 0.1449467115730241, 0.24751160642335812, 0.3714705521119457, 0.5138009456452586, 0.6717316202228357, 0.8427259076096095, 1.0244657148949758  …  1.8560527056615184, 1.8629729344642032, 1.8696298851466997, 1.8768276952756286, 1.8824053751208285, 1.8921110896830133, 1.8912408569478099, 1.9171352613986457, 1.8728687859761977, 1.0]
 [8.530436664276907e-7, 0.1272660020066095, 0.4824543028839804, 1.0287720968254628, 1.733305483676706, 2.566682072979201, 3.5027585194801465, 4.518332688313138, 5.592878646428266, 6.7083028021327875  …  1.8700526286528898, 1.876184724018738, 1.882506506235186, 1.8884430769360296, 1.8956204930445557, 1.899792158934437, 1.9118930626125588, 1.904218911920958, 1.9487418307432145, 1.0]
using CairoMakie
fig = Figure(fontsize = 38)
for j in eachindex(sol.u)
    ax = Axis(fig[1, j], width = 600, height = 600,
        xlabel = "x", ylabel = "y",
        title = "t = $(sol.t[j])")
    tricontourf!(ax, tri, sol.u[j], levels = 0:0.1:1, extendlow = :auto,
        extendhigh = :auto, colormap = :turbo)
    tightlimits!(ax)
end
resize_to_layout!(fig)
fig
Example block output

Here is how we could convert this into an FVMProblem. Note that the Neumann boundary conditions are expressed as $\grad T\vdot\vu n = 1$ above, but for FVMProblem we need them in the form $\vb q\vdot\vu n = \ldots$. For this problem, $\vb q=-D\grad T$, which gives $\vb q\vdot\vu n = -D$.

_BCs = BoundaryConditions(mesh, (x, y, t, u, p) -> -p.D(x, y, p.Dp), Neumann;
    parameters = (D = diffusion_function, Dp = diffusion_parameters))
fvm_prob = FVMProblem(
    mesh,
    _BCs;
    diffusion_function = let D=diffusion_function
        (x, y, t, u, p) -> D(x, y, p)
    end,
    diffusion_parameters = diffusion_parameters,
    source_function = let S=source_function
        (x, y, t, u, p) -> S(x, y, p) * u
    end,
    final_time = final_time,
    initial_condition = initial_condition
)
fvm_sol = solve(fvm_prob, Tsit5(), saveat = 2.0)
retcode: Success
Interpolation: 1st order linear
t: 5-element Vector{Float64}:
 0.0
 2.0
 4.0
 6.0
 8.0
u: 5-element Vector{Vector{Float64}}:
 [0.0, 4.504301608035674e-5, 0.00018017206432142696, 0.00040538714472321063, 0.0007206882572857079, 0.0011260754020089186, 0.0016215485788928425, 0.0022071077879374803, 0.0028827530291428314, 0.0036484843025088956  …  1.882843115174992, 1.8955002026935723, 1.9082473762443133, 1.921084635827215, 1.9340119814422772, 1.9470294130895003, 1.9601369307688843, 1.9733345344804287, 1.986622224224134, 2.0]
 [5.392972715241145e-10, 0.0003283969313676687, 0.0012960759990657785, 0.0028772909445736283, 0.005046982038098886, 0.0077807618458974465, 0.011054900287088444, 0.014846309987847438, 0.01913253192748718, 0.02389172137103146  …  1.848063591209066, 1.8576012582625903, 1.866918754272051, 1.8756974833133426, 1.884763904846072, 1.8917905417452563, 1.9028817939540141, 1.9023585053824097, 1.929991723462379, 1.882501747575437]
 [7.916735417539232e-9, 0.002394255277378066, 0.009323370071034566, 0.020421920082540596, 0.03534389913584277, 0.05376187635010658, 0.07536617230272596, 0.09986406790939079, 0.12697904481386746, 0.15645005612239946  …  1.8429322427231214, 1.850913609981875, 1.8585916016450457, 1.8664895854253847, 1.8731272920824804, 1.8825835049840252, 1.8841165825025752, 1.9053111995283225, 1.8762178927519777, 1.978489471244963]
 [8.716291359760385e-8, 0.017455872288248346, 0.06706791816304748, 0.1449467115730244, 0.24751160642335823, 0.37147055211194546, 0.513800945645259, 0.6717316202228366, 0.8427259076096097, 1.0244657148949752  …  1.849136053821626, 1.856052707046336, 1.8629729302916949, 1.869629897178183, 1.8768276620456872, 1.8824054632709744, 1.8921108635891568, 1.8912414258621113, 1.917133815295843, 1.872872691539465]
 [8.530436664276906e-7, 0.12726600200660956, 0.4824543028839805, 1.0287720968254663, 1.7333054836767066, 2.5666820729791993, 3.5027585194801465, 4.518332688313149, 5.592878646428262, 6.70830280213278  …  1.8638903357449805, 1.8700526280569532, 1.8761847258143374, 1.8825065010575694, 1.8884430912361823, 1.895620455110088, 1.8997922562315015, 1.9118928177863976, 1.904219534235772, 1.9487401500259915]

Using the Provided Template

The above code is implemented in LinearReactionDiffusionEquation in FiniteVolumeMethod.jl.

prob = LinearReactionDiffusionEquation(mesh, BCs;
    diffusion_function, diffusion_parameters,
    source_function, initial_condition, final_time)
sol = solve(prob, Tsit5(); saveat = 2)
retcode: Success
Interpolation: 1st order linear
t: 5-element Vector{Float64}:
 0.0
 2.0
 4.0
 6.0
 8.0
u: 5-element Vector{Vector{Float64}}:
 [0.0, 4.504301608035674e-5, 0.00018017206432142696, 0.00040538714472321063, 0.0007206882572857079, 0.0011260754020089186, 0.0016215485788928425, 0.0022071077879374803, 0.0028827530291428314, 0.0036484843025088956  …  1.8955002026935723, 1.9082473762443133, 1.921084635827215, 1.9340119814422772, 1.9470294130895003, 1.9601369307688843, 1.9733345344804287, 1.986622224224134, 2.0, 1.0]
 [5.392972715241144e-10, 0.0003283969313676684, 0.00129607599906578, 0.002877290944573624, 0.005046982038098887, 0.007780761845897449, 0.011054900287088434, 0.01484630998784744, 0.01913253192748716, 0.023891721371031457  …  1.8576012589880757, 1.866918752086122, 1.8756974896164813, 1.884763887437324, 1.8917905879259964, 1.9028816755063147, 1.902358803429383, 1.9299909658674956, 1.8825037936504083, 1.0]
 [7.916735417539227e-9, 0.0023942552773780646, 0.009323370071034572, 0.02042192008254055, 0.035343899135842786, 0.05376187635010655, 0.07536617230272592, 0.09986406790939059, 0.12697904481386743, 0.1564500561223995  …  1.850913610507159, 1.8585916000623353, 1.866489589989134, 1.8731272794777782, 1.8825835384209193, 1.884116496741212, 1.905311415327487, 1.8762173442198424, 1.9784909526935537, 1.0]
 [8.716291359760387e-8, 0.01745587228824834, 0.06706791816304747, 0.1449467115730241, 0.24751160642335812, 0.3714705521119457, 0.5138009456452586, 0.6717316202228357, 0.8427259076096095, 1.0244657148949758  …  1.8560527056615184, 1.8629729344642032, 1.8696298851466997, 1.8768276952756286, 1.8824053751208285, 1.8921110896830133, 1.8912408569478099, 1.9171352613986457, 1.8728687859761977, 1.0]
 [8.530436664276907e-7, 0.1272660020066095, 0.4824543028839804, 1.0287720968254628, 1.733305483676706, 2.566682072979201, 3.5027585194801465, 4.518332688313138, 5.592878646428266, 6.7083028021327875  …  1.8700526286528898, 1.876184724018738, 1.882506506235186, 1.8884430769360296, 1.8956204930445557, 1.899792158934437, 1.9118930626125588, 1.904218911920958, 1.9487418307432145, 1.0]

Here is a benchmark comparison of LinearReactionDiffusionEquation versus FVMProblem.

using BenchmarkTools
using Sundials
@btime solve($prob, $CVODE_BDF(linear_solver = :GMRES); saveat = $2);
  48.360 ms (1087 allocations: 1.58 MiB)
@btime solve($fvm_prob, $CVODE_BDF(linear_solver = :GMRES); saveat = $2);
  163.686 ms (83267 allocations: 90.84 MiB)

Just the code

An uncommented version of this example is given below. You can view the source code for this file here.

using FiniteVolumeMethod, SparseArrays, OrdinaryDiffEq, LinearAlgebra, SciMLOperators
const FVM = FiniteVolumeMethod
function linear_source_contributions!(
        A, mesh, conditions, source_function, source_parameters)
    for i in each_solid_vertex(mesh.triangulation)
        if !FVM.has_condition(conditions, i)
            x, y = get_point(mesh, i)
            A[i, i] += source_function(x, y, source_parameters)
        end
    end
end
function linear_reaction_diffusion_equation(mesh::FVMGeometry,
        BCs::BoundaryConditions,
        ICs::InternalConditions = InternalConditions();
        diffusion_function,
        diffusion_parameters = nothing,
        source_function,
        source_parameters = nothing,
        initial_condition,
        initial_time = 0.0,
        final_time)
    conditions = Conditions(mesh, BCs, ICs)
    n = DelaunayTriangulation.num_solid_vertices(mesh.triangulation)
    Afull = zeros(n + 1, n + 1)
    A = @views Afull[begin:(end - 1), begin:(end - 1)]
    b = @views Afull[begin:(end - 1), end]
    _ic = vcat(initial_condition, 1)
    FVM.triangle_contributions!(
        A, mesh, conditions, diffusion_function, diffusion_parameters)
    FVM.boundary_edge_contributions!(
        A, b, mesh, conditions, diffusion_function, diffusion_parameters)
    linear_source_contributions!(A, mesh, conditions, source_function, source_parameters)
    FVM.apply_dudt_conditions!(b, mesh, conditions)
    FVM.apply_dirichlet_conditions!(_ic, mesh, conditions)
    FVM.fix_missing_vertices!(A, b, mesh)
    Af = sparse(Afull)
    prob = ODEProblem(MatrixOperator(Af), _ic, (initial_time, final_time))
    return prob
end

using DelaunayTriangulation
tri = triangulate_rectangle(0, 1, 0, 1, 150, 150, single_boundary = true)
mesh = FVMGeometry(tri)
BCs = BoundaryConditions(mesh, (x, y, t, u, p) -> one(x), Neumann)
diffusion_function = (x, y, p) -> p.D * x^2 * y
diffusion_parameters = (D = 1e-3,)
source_function = (x, y, p) -> (x - 1) * (y - 1)
initial_condition = [x^2 + y^2 for (x, y) in DelaunayTriangulation.each_point(tri)]
final_time = 8.0
prob = linear_reaction_diffusion_equation(mesh, BCs;
    diffusion_function, diffusion_parameters,
    source_function, initial_condition, final_time)

sol = solve(prob, Tsit5(); saveat = 2)

using CairoMakie
fig = Figure(fontsize = 38)
for j in eachindex(sol.u)
    ax = Axis(fig[1, j], width = 600, height = 600,
        xlabel = "x", ylabel = "y",
        title = "t = $(sol.t[j])")
    tricontourf!(ax, tri, sol.u[j], levels = 0:0.1:1, extendlow = :auto,
        extendhigh = :auto, colormap = :turbo)
    tightlimits!(ax)
end
resize_to_layout!(fig)
fig

_BCs = BoundaryConditions(mesh, (x, y, t, u, p) -> -p.D(x, y, p.Dp), Neumann;
    parameters = (D = diffusion_function, Dp = diffusion_parameters))
fvm_prob = FVMProblem(
    mesh,
    _BCs;
    diffusion_function = let D=diffusion_function
        (x, y, t, u, p) -> D(x, y, p)
    end,
    diffusion_parameters = diffusion_parameters,
    source_function = let S=source_function
        (x, y, t, u, p) -> S(x, y, p) * u
    end,
    final_time = final_time,
    initial_condition = initial_condition
)
fvm_sol = solve(fvm_prob, Tsit5(), saveat = 2.0)

prob = LinearReactionDiffusionEquation(mesh, BCs;
    diffusion_function, diffusion_parameters,
    source_function, initial_condition, final_time)
sol = solve(prob, Tsit5(); saveat = 2)

This page was generated using Literate.jl.