Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault with two heatmaps and colorbar #711

Closed
dingraha opened this issue Sep 30, 2020 · 2 comments
Closed

Segfault with two heatmaps and colorbar #711

dingraha opened this issue Sep 30, 2020 · 2 comments

Comments

@dingraha
Copy link

I'm getting a segfault when trying to plot two heatmaps with a shared colorbar. Here's the script that segfaults:

module StreamwisePlots

using Makie
using AbstractPlotting.MakieLayout
using AbstractPlotting
import DelimitedFiles
import CairoMakie
CairoMakie.activate!()

function write_data(fname1, fname2)
    nx, ny = 600, 125
    x1 = range(0.0, 20.0, length=nx)
    x1 = reshape(x1, nx, 1)
    x1 = repeat(x1, 1, ny)
    y1 = range(-1.0, 1.0, length=ny)
    y1 = reshape(y1, 1, ny)
    y1 = repeat(y1, nx, 1)
    z_shjar = zeros(eltype(x1), (nx, ny))
    u1 = (sin.(0.5*pi.*x1).*cos.(2.0*pi.*y1)).^2
    v1 = 0.1.*sin.(0.5*pi.*x1).*cos.(2.0*pi.*y1)

    data = [x1[:] y1[:] z_shjar[:] u1[:] v1[:]]
    open(fname1, "w") do io
        DelimitedFiles.writedlm(io, data, ',')
    end

    nx, ny = 240, 140
    x = range(1.0, 25.0, length=nx)
    x = reshape(x, nx, 1)
    x = repeat(x, 1, ny)
    y = range(-1.0, 1.0, length=ny)
    y = reshape(y, 1, ny)
    y = repeat(y, nx, 1)
    z = zeros(eltype(x), (nx, ny))
    u_mean = (sin.(0.5*pi.*x).*cos.(2.0*pi.*y)).^2
    u_rms = 0.2.*sin.(0.5*pi.*x).*cos.(2.0*pi.*y)

    data = [x[:] y[:] z[:] u_mean[:] u_rms[:]]
    open(fname2, "w") do io
        DelimitedFiles.writedlm(io, data, ',')
    end
end

function read_data1(fname)
    # Get the experimental data.
    data = DelimitedFiles.readdlm(fname, ',', comments=true)
    jmax, imax = 600, 125
    data = reshape(data, jmax, imax, :)
    x1 = data[:, :, 1]
    y1 = data[:, :, 2]
    z1 = data[:, :, 3]
    u1 = data[:, :, 4]
    v1 = data[:, :, 5]

    # Make sure all the z coordinates are zero.
    if maximum(abs.(z1)) > 1e-10
        @warn "warning: data1 z coordinate does not appear to be zero"
    end

    # Check that the x coordinates repeat.
    diff = abs.(x1 .- x1[:, 1:1])
    if maximum(diff) > 1e-10
        @warn "warning: data1 x coordinate does not repeat as expected"
    else
        x1 = x1[:, 1]
    end

    # Check that the y coordinates repeat.
    diff = y1 .- y1[1:1, :]
    if maximum(diff) > 1e-10
        @warn "warning: data1 y coordinate does not repeat as expected"
    else
        y1 = y1[1, :]
    end

    return x1, y1, u1
end

function read_data2(fname)
    # Read in the data.
    data = DelimitedFiles.readdlm(fname, ',', comments=true)

    # Reshape the data into a friendly form.
    jmax, imax = 240, 140
    data = reshape(data, jmax, imax, :)
    x2 = data[:, :, 1]
    y2 = data[:, :, 2]
    z2 = data[:, :, 3]
    u2 = data[:, :, 4]
    v2 = data[:, :, 5]

    # Check that the x coordinates repeat as we expect.
    diff = maximum(abs.(x2 .- x2[:, 1:1]))
    if diff > 1e-8
        @warn "data2 x coordinate from $(fname) does not repeat as expected. diff = $(diff)"
    else
        x2 = x2[:, 1]
    end

    # Check that the y coordinates repeat as we expect.
    diff = maximum(abs.(y2 .- y2[1:1, :]))
    if diff > 1e-8
        @warn "data2 y coordinate from $(fname) does not repeat as expected. diff = $(diff)"
    else
        y2 = y2[1, :]
    end

    if maximum(abs.(z2)) > 1e-7
        @warn "data2 z coordinate from $(fname) does not appear to be zero"
    end

    return x2, y2, u2
end

function doit(x1, y1, u1, x2, y2, u2)
    mean_clims = (0.0, 1.0)

    outer_padding = 30
    scene, layout = layoutscene(outer_padding)

    # Plot the data.
    # @show typeof(x1), typeof(y1), typeof(u1)
    # @show size(x1), size(y1), size(u1)
    ax1 = layout[1, 1] = LAxis(scene)
    c1 = heatmap!(ax1, x1, y1, u1, colorrange=mean_clims, colormap=cgrad(:viridis, 10, categorical=true))

    # Plot the data.
    # @show typeof(x2), typeof(y2), typeof(u2)
    # @show size(x2), size(y2), size(u2)
    ax2 = layout[2, 1] = LAxis(scene)
    c2 = heatmap!(ax2, x2, y2, u2, colorrange=mean_clims, colormap=cgrad(:viridis, 10, categorical=true))

    linkaxes!(ax1, ax2)

    # https://jkrumbiegel.github.io/MakieLayout.jl/stable/laxis/#Controlling-data-aspect-ratios
    ax1.autolimitaspect = 1.0
    ax2.autolimitaspect = 1.0

    cbar = LColorbar(scene, c1)
    cbar.width = 30
    layout[1:2, 2] = cbar

    return scene
end


if ! isinteractive()
    write_data("foo.csv", "bar.csv")
    x1, y1, u1 = read_data1("foo.csv")
    x2, y2, u2 = read_data2("bar.csv")
    s = doit(x1, y1, u1, x2, y2, u2)
    save("foo_bar.pdf", s)
    save("foo_bar.png", s)
end

end  # module

This is with Julia 1.5.2 on RHEL:

julia> versioninfo()
Julia Version 1.5.2                                
Commit 539f3ce943* (2020-09-23 23:17 UTC)                                                 
Platform Info:                                                            
  OS: Linux (x86_64-pc-linux-gnu)                
  CPU: Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz                                          
  WORD_SIZE: 64                               
  LIBM: libopenlibm                  
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:                                                               
  JULIA_PROJECT = /home/dingraha/projects/julia_issues_and_tips/Makie_shared_colorbar_stackoverflow

The error is just

dingraha@GRSLR18080228 Makie_shared_colorbar_stackoverflow % julia plot_with_segfault.jl
zsh: segmentation fault (core dumped)  julia plot_with_segfault.jl                                                                     
dingraha@GRSLR18080228 Makie_shared_colorbar_stackoverflow % 

Any ideas on how to debug this? Also, if I remove the three lines related to the colorbar,

    cbar = LColorbar(scene, c1)
    cbar.width = 30
    layout[1:2, 2] = cbar

there's no segfault (but I don't get a colorbar in the plot).

Any ideas? Thanks!

@dingraha
Copy link
Author

Tried using the official build of 1.5.2, and I got a better error message. See attached.
plot_with_segfault_traceback.txt

@dingraha
Copy link
Author

dingraha commented Oct 1, 2020

Aha, OK: this issue gave me the idea to add

    tight_ticklabel_spacing!(ax1)
    tight_ticklabel_spacing!(ax2)

just before setting the axes autolimitaspect. That fixed the problem. Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant