GalerkinToolkit / GalerkinToolkit.jl

FEM toolbox with automatic code generation, HPC support, and more.
MIT License
23 stars 0 forks source link

GLMakie with transparent background #133

Open Cmurilochem opened 1 week ago

Cmurilochem commented 1 week ago

@fverdugo and @raar1. While investigating this possibility I came across the following issue in the official Makie.jl repo: https://github.com/MakieOrg/Makie.jl/issues/4007

It seems indeed that there is currently a bug in GLMakie which prevents one to make figures with transparent background.

I tested it with GalerkinToolkit example:

import GalerkinToolkit as GT
import PartitionedSolvers as PS
import ForwardDiff
import GLMakie
using LinearAlgebra

function ex()
    domain = (0,1,0,1)
    cells = (10,10)
    mesh = GT.cartesian_mesh(domain,cells;simplexify=true)
    dirichlet_tag = "dirichlet"
    GT.label_boundary_faces!(mesh;physical_name=dirichlet_tag)
    Ω = GT.interior(mesh)
    Γd = GT.boundary(mesh;physical_names=[dirichlet_tag])
    k = 1
    V = GT.lagrange_space(Ω,k;dirichlet_boundary=Γd)
    uhd = GT.dirichlet_field(Float64,V)
    g = GT.analytical_field(x->0,Ω)
    f = GT.analytical_field(x->1,Ω)
    GT.interpolate_dirichlet!(g,uhd)
    dΩ = GT.measure(Ω,2*k)
    ∇ = ForwardDiff.gradient
    a(u,v) = GT.∫( x->∇(u,x)⋅∇(v,x), dΩ)
    l(v) = GT.∫( x->v(x)*f(x), dΩ)
    p = GT.linear_problem(uhd,a,l)
    s = PS.LinearAlgebra_lu(p)
    s = PS.solve(s)
    uh = GT.solution_field(uhd,s)

    bgcolor = :black
    fig = Figure(; size = (1500, 1500), backgroundcolor=bgcolor)

    scene = LScene(
       fig[1, 1],
       show_axis=true,
       scenekw = (
           lights=[],
           backgroundcolor=bgcolor,
           clear=true,
           transparency=true,
           ),
    )

    GLMakie.plot!(scene,Ω;color=uh,strokecolor=:black)
    fig
end

ex()

test

But if I set bgcolor = :transparent, The final figure is just a blank white canvas as described in https://github.com/MakieOrg/Makie.jl/issues/4007

The only work around this I could find is to use CairoMakie, like so:

import GalerkinToolkit as GT
import PartitionedSolvers as PS
import ForwardDiff
import CairoMakie
using LinearAlgebra

function ex()
    domain = (0,1,0,1)
    cells = (10,10)
    mesh = GT.cartesian_mesh(domain,cells;simplexify=true)
    dirichlet_tag = "dirichlet"
    GT.label_boundary_faces!(mesh;physical_name=dirichlet_tag)
    Ω = GT.interior(mesh)
    Γd = GT.boundary(mesh;physical_names=[dirichlet_tag])
    k = 1
    V = GT.lagrange_space(Ω,k;dirichlet_boundary=Γd)
    uhd = GT.dirichlet_field(Float64,V)
    g = GT.analytical_field(x->0,Ω)
    f = GT.analytical_field(x->1,Ω)
    GT.interpolate_dirichlet!(g,uhd)
    dΩ = GT.measure(Ω,2*k)
    ∇ = ForwardDiff.gradient
    a(u,v) = GT.∫( x->∇(u,x)⋅∇(v,x), dΩ)
    l(v) = GT.∫( x->v(x)*f(x), dΩ)
    p = GT.linear_problem(uhd,a,l)
    s = PS.LinearAlgebra_lu(p)
    s = PS.solve(s)
    uh = GT.solution_field(uhd,s)

    bgcolor = :transparent
    fig = Figure(; size = (1500, 1500), backgroundcolor=bgcolor)

    scene = LScene(
       fig[1, 1],
       show_axis=true,
       scenekw = (
           lights=[],
           backgroundcolor=bgcolor,
           clear=true,
           transparency=true,
           ),
    )

    CairoMakie.plot!(scene,Ω;color=uh,strokecolor=:black)
    fig

end

ex()

test

I am not sure if this is an options for us ? If yes, I need to investigate how to include a LScene into our custom GalerkinToolkit Makie plots which for what I could experience so far is not so straightforwards as I initially thought.

fverdugo commented 1 week ago

Hi @Cmurilochem Thanks for the update.

CairoMakie can be a solution.

Does CairoMakie also work for 3d plots? and for creating animations?

Cmurilochem commented 5 days ago

Hi @Cmurilochem Thanks for the update.

CairoMakie can be a solution.

Does CairoMakie also work for 3d plots? and for creating animations?

Hi @fverdugo. It does make 2D and 3D plots, although it looks like the quality of these latter is slightly lower than GLMakie. However, as CairoMakie is expected to generate png-like figures, they are not iterative. The good thing is that we can use both packages at our convenience.

I will show you latter today how the documentation would look like with transparent plots done with CairoMakie.

See you soon.