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

Hi all,

After I read this page and see the animation

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.

There are a few packages you can use for solving the PDE. Makie is great for creating animations. Here’s an example, using FiniteVolumeMethod.jl for solving the PDE.

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

4 Likes

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

1 Like

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

1 Like

Julia Discourse is an amazing forum to learn

3 Likes

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 that is hopefully sufficient. For understanding how to make the animations, the Makie docs 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:

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:

1 Like

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

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?

1 Like

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:

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 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.

1 Like

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

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.

1 Like