# Partial Differential Equation visualisation / animation for Heat Equation in Julia / Alternatives

**URL:** <https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [July 27, 2023, 8:09am UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145 "2023-07-27T08:09:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 27, 2023, 8:09am UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/1 "2023-07-27T08:09:50Z")

</div>

Hi all,

After I read this page and see the animation

> **[Partial differential equation](https://en.wikipedia.org/wiki/Partial_differential_equation)**
>
> In mathematics, a partial differential equation (PDE) is an equation which computes a function between various partial derivatives of a multivariable function.
> The function is often thought of as an "unknown" to be solved for, similar to how x is thought of as an unknown number to be solved for in an algebraic equation like x2 − 3x + 2 = 0. However, it is usually impossible to write down explicit formulas for solutions of partial differential equations. There is, correspondingly, a vast amount o...

[![](https://en.wikipedia.org/wiki/File:Heat.gif) ](https://en.wikipedia.org/wiki/File:Heat.gif)

I wonder, to those who might have created something like this, which Julia packages can be used to create animation for this PDE. Compute then plot and become animation. Then besides Julia, which Fortran library / C++ library can help for each of the languages, for those who might have experiences with those two languages.

Thanks.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 27, 2023, 9:03am UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/2 "2023-07-27T09:03:20Z")

</div>

There are a few packages you can use for solving the PDE. [Makie is great for creating animations](https://docs.makie.org/dev/documentation/animation/index.html#animations). Here’s an example, using [FiniteVolumeMethod.jl](https://github.com/DanielVandH/FiniteVolumeMethod.jl) for solving the PDE.

```julia
using CairoMakie, DelaunayTriangulation, FiniteVolumeMethod, OrdinaryDiffEq, LinearSolve

# Setup the geometry and initial condition
N = 500
L = 2.5
tri = triangulate_rectangle(-L, L, -L, L, N, N; single_boundary=true)
c, d, e, f, k, shift = 2.0, -1.0, 1.0, 0.5, 1.2, 10.0
Z = [(c^2 - (x / e - d)^2 - (y / f)^2)^2 + k * (c + d - x / e)^3 - shift for (x, y) in each_point(tri)]
@. Z = 1 - max(sign(Z), 0.0)

# Define the PDE  
geo = FVMGeometry(tri)
bc = BoundaryConditions(geo, Returns(0.0), :Dirichlet)
final_time = 0.25
prob = FVMProblem(geo, bc; initial_condition=Z, diffusion_function=Returns(1.0), final_time=final_time)

# Solve 
secs = 15.0
Δt = final_time / (24secs)
sol = solve(prob, TRBDF2(linsolve=KLUFactorization()), saveat=Δt)

# Plot 
j = Observable(1)

fig = Figure(fontsize=33)
ax = Axis3(fig[1, 1],
    xlabel=L"x", ylabel=L"y", zlabel=L"z",
    title=lift(_j -> L"t = %$(rpad(round(sol.t[_j], digits = 5), 7, '0'))", j),
    titlealign=:left) 
surface!(ax, LinRange(-L, L, N), LinRange(-L, L, N), @lift(reshape(sol.u[$j], N, N)))
# If you just want a 2D plot, use Axis instead and tricontourf!(ax, tri, @lift(sol.u[$j]))
record(fig, "heat_animation.mp4", eachindex(sol); framerate=24) do _j
    j[] = _j
end

```

[https://global.discourse-cdn.com/julialang/original/3X/1/3/138b1b40d63a10a5cee2979e240ce54a14289291.mp4](https://global.discourse-cdn.com/julialang/original/3X/1/3/138b1b40d63a10a5cee2979e240ce54a14289291.mp4)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 30, 2023, 9:48pm UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/3 "2023-07-30T21:48:51Z")

</div>

Wow it is like the wikipedia,

Can I get a reference book to learn about PDE and able to comprehend how to make the solution and illustration like this (the computation, the differential equations, etc) ?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 30, 2023, 9:50pm UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/4 "2023-07-30T21:50:01Z")

</div>

When I download the mp4 it is only `293 kb` but when I try to compile the Julia code, it took so long it never produces the mp4 with Julia 1.9.2

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 30, 2023, 9:58pm UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/5 "2023-07-30T21:58:40Z")

</div>

Julia Discourse is an amazing forum to learn

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 30, 2023, 11:29pm UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/6 "2023-07-30T23:29:02Z")

</div>

> Wow it is like the wikipedia,
> 
> Can I get a reference book to learn about PDE and able to comprehend how to make the solution and illustration like this (the computation, the differential equations, etc) ?
> 
> Thanks a lot

There are many books around for problems like this. You could search around for things like finite/numerical differences in PDEs. I’ve sometimes used “Numerical Methods for Fluid Dynamics” by Durran (2011) and “An Introduction to Computational Fluid Dynamics” by Versteeg and Malalasekera, but it’s quite a big field so you’d want to look around for yourself if you’re interested in this.

For the case here, I describe all the mathematical details for solving PDEs of the form \partial\_tu + \boldsymbol\nabla\boldsymbol\cdot \boldsymbol q = R [here](https://danielvandh.github.io/FiniteVolumeMethod.jl/dev/math/) that is hopefully sufficient. For understanding how to make the animations, the [Makie docs](https://docs.makie.org/dev/documentation/animation/index.html#animations) give this in good detail.

> When I download the mp4 it is only `293 kb` but when I try to compile the Julia code, it took so long it never produces the mp4 with Julia 1.9.2

The plotting with `surface!` itself takes quite a bit of time, it’s not actually making a huge file. With `N = 500`, there are 500^2 points to plot which is a bit much (`N` controls the accuracy). You can use `N = 100` and get pretty much the same quality in a significantly faster time. Another plot you might consider is just the 2D version with `tricontourf` which is extremely fast to compute:

```julia
fig = Figure(fontsize=33)
ax = Axis(fig[1, 1],
    xlabel=L"x", ylabel=L"y", 
    title=lift(_j -> L"t = %$(rpad(round(sol.t[_j], digits = 5), 7, '0'))", j),
    titlealign=:left) 
tricontourf!(ax, tri, @lift(sol.u[$j]))
record(fig, "heat_animation.mp4", eachindex(sol); framerate=24) do _j
    j[] = _j
end

```

This took about five seconds to run with `N = 100`.

With `N = 500`:

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 1, 2023, 2:03pm UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/7 "2023-08-01T14:03:13Z")

</div>

I am trying with N=500 and after 5 hours it has not even finished yet (maybe my GPU is not a hardcore one )

![Capture d’écran_2023-08-01_18-24-52](https://global.discourse-cdn.com/julialang/original/3X/c/7/c754af4e1f40a5781ecb19aa9e8e973a0a3158d1.png)

I will try `tricontourf` method after this.  
I have downloaded all the books you mentioned and even some that has topics around Finite differences or Fluid dynamics.

Is there a Navier-Stokes simulation from `FiniteVolumeMethod.jl`?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 1, 2023, 2:15pm UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/8 "2023-08-01T14:15:50Z")

</div>

Interesting. It shouldn’t take _that_ long. My computer is not that good to make that much difference (about ~10 minutes of computation for me). You could insert a `@show _j` to track the progress to see if it’s actually running anything:

```julia
record(fig, "heat_animation.mp4", eachindex(sol); framerate=24) do _j
    @show _j
    j[] = _j
end

```

but maybe it is just that slow.

`FiniteVolumeMethod.jl` could be applied to similar problems from Navier-Stokes, but it’s only for scalar equations and I think you want more sophisticated packages for that (depending on your application, it could even be fine). I would suggest you look [here](https://danielvandh.github.io/FiniteVolumeMethod.jl/dev/example_list/) to see the types of problems that can be easily solved.

For other packages, review the list here [GitHub - JuliaPDE/SurveyofPDEPackages: Survey of the packages of the Julia ecosystem for solving partial differential equations](https://github.com/JuliaPDE/SurveyofPDEPackages).

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 1, 2023, 8:50pm UTC](https://discourse.julialang.org/t/partial-differential-equation-visualisation-animation-for-heat-equation-in-julia-alternatives/102145/9 "2023-08-01T20:50:13Z")

</div>

I finally make it comes out quick with `N=50` only the size is 324 kb

[https://global.discourse-cdn.com/julialang/original/3X/6/8/68018ea2980e4ef6e63546a2cb968b030a9a20ec.mp4](https://global.discourse-cdn.com/julialang/original/3X/6/8/68018ea2980e4ef6e63546a2cb968b030a9a20ec.mp4)

Thanks for the `@show _j` suggestion. I like this `FiniteVolumeMethod` package I can learn about PDE from this, Navier-Stokes is said to be less understood till date by a lot of mathematicians.
