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

MakiePlotter rewrite with ShaderAbstractions.Buffer #69

Merged
merged 13 commits into from
Mar 23, 2023
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Modified
- Removed unnecessary extra dispatches for three-dimensional case ([#56][github-56]).
- function barrier for `transfer_solution` such that its closer to type groundedness ([#68][github-68]).
- `MakiePlotter` holds now `ShaderAbstractions.Buffer`s ([#69][github-69])
- triangles are now `Buffer`s with Observables
koehlerson marked this conversation as resolved.
Show resolved Hide resolved
- triangle coords are now `Buffers`s with Observables
- remove overcomplicated ternary operators by `begin end` expressions ([#69][github-69])
koehlerson marked this conversation as resolved.
Show resolved Hide resolved
- remove unused functions ([#69][github-69])
### Fixed
- Renamed `Crincle` to `Crinkle` ([#56][github-56]).

Expand Down Expand Up @@ -46,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[github-65]: https://github.com/Ferrite-FEM/FerriteViz.jl/pull/65
[github-63]: https://github.com/Ferrite-FEM/FerriteViz.jl/pull/63
[github-68]: https://github.com/Ferrite-FEM/FerriteViz.jl/pull/68
[github-69]: https://github.com/Ferrite-FEM/FerriteViz.jl/pull/69

[Unreleased]: https://github.com/Ferrite-FEM/FerriteViz.jl/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/Ferrite-FEM/FerriteViz.jl/compare/v0.2.0...v0.1.4
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ version = "0.2.0"

[deps]
Ferrite = "c061ca5d-56c9-439f-9c0e-210fe06d3992"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
ShaderAbstractions = "65257c39-d410-5151-9873-9b3e5be5013e"
Tensors = "48a634ad-e948-5137-8d70-aa71f2a747f4"

[compat]
Expand Down
2 changes: 2 additions & 0 deletions src/FerriteViz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module FerriteViz
using Makie
using Tensors
import Ferrite
import GeometryBasics
import ShaderAbstractions
import LinearAlgebra

abstract type AbstractPlotter end
Expand Down
115 changes: 83 additions & 32 deletions src/makieplotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ function update!(plotter::MakiePlotter, u::Vector)
Makie.notify(plotter.u)
end

function Makie.convert_arguments(P::Type{<:Makie.Mesh}, plotter::MakiePlotter)
return Makie.convert_arguments(P,plotter.physical_coords,visible(plotter))
end

"""
solutionplot(plotter::MakiePlotter; kwargs...)
solutionplot(dh::AbstractDofHandler, u::Vector; kwargs...)
Expand Down Expand Up @@ -45,13 +41,32 @@ end

function Makie.plot!(SP::SolutionPlot{<:Tuple{<:MakiePlotter}})
plotter = SP[1][]
solution = @lift($(SP[:field])===:default ? reshape(transfer_solution(plotter,$(plotter.u); field_idx=1, process=$(SP[:process])), num_vertices(plotter)) : reshape(transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(SP[:field])), process=$(SP[:process])), num_vertices(plotter)))
u_matrix = @lift($(SP[:deformation_field])===:default ? zeros(0,3) : transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(SP[:deformation_field])), process=identity))
coords = @lift($(SP[:deformation_field])===:default ? plotter.physical_coords : plotter.physical_coords .+ ($(SP[:deformation_scale]) .* $(u_matrix)))
solution = @lift begin
if $(SP[:field])===:default
reshape(transfer_solution(plotter,$(plotter.u); field_idx=1, process=$(SP[:process])), num_vertices(plotter))
else
reshape(transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(SP[:field])), process=$(SP[:process])), num_vertices(plotter))
end
end
u_matrix = @lift begin
if $(SP[:deformation_field])===:default
Ferrite.getdim(plotter.dh.grid) > 2 ? Point3f[Point3f(0,0,0)] : Point2f[Point2f(0,0)]
else
#TODO remove convert
convert(Vector{Point{Ferrite.getdim(plotter.dh.grid),Float32}},Makie.to_vertices(transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(SP[:deformation_field])), process=identity)))
end
end
@lift begin
if $(SP[:deformation_field])===:default
plotter.physical_coords_mesh[1:end] = plotter.physical_coords
else
plotter.physical_coords_mesh[1:end] = plotter.physical_coords .+ ($(SP[:deformation_scale]) .* $(u_matrix))
end
end
mins = @lift(minimum($solution))
maxs = @lift(maximum($solution))
SP[:colorrange] = @lift(isapprox($mins,$maxs) ? ($mins,1.01($maxs)) : ($mins,$maxs))
return Makie.mesh!(SP, coords, visible(plotter), color=solution, shading=SP[:shading], scale_plot=SP[:scale_plot], colormap=SP[:colormap], transparent=SP[:transparent])
return Makie.mesh!(SP, plotter.mesh, color=solution, shading=SP[:shading], scale_plot=SP[:scale_plot], colormap=SP[:colormap], transparent=SP[:transparent])
end

"""
Expand Down Expand Up @@ -86,13 +101,25 @@ end
function Makie.plot!(CP::CellPlot{<:Tuple{<:MakiePlotter{dim},Vector}}) where dim
plotter = CP[1][]
qp_values = CP[2][]
u_matrix = @lift($(CP[:deformation_field])===:default ? zeros(0,3) : transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(CP[:deformation_field])), process=identity))
coords = @lift($(CP[:deformation_field])===:default ? plotter.physical_coords : plotter.physical_coords .+ ($(CP[:deformation_scale]) .* $(u_matrix)))
u_matrix = @lift begin
if $(CP[:deformation_field])===:default
Point3f[Point3f(0,0,0)]
else
convert(Vector{Point{Ferrite.getdim(plotter.dh.grid),Float32}},Makie.to_vertices(transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(CP[:deformation_field])), process=identity)))
end
end
coords = @lift begin
if $(CP[:deformation_field])===:default
plotter.physical_coords_mesh[1:end] = plotter.physical_coords
else
plotter.physical_coords_mesh[1:end] = plotter.physical_coords .+ ($(CP[:deformation_scale]) .* $(u_matrix))
end
end
mins = minimum(qp_values)
maxs = maximum(qp_values)
CP[:colorrange] = @lift(isapprox($mins,$maxs) ? ($mins,1.01($maxs)) : ($mins,$maxs))
solution = @lift(reshape(transfer_scalar_celldata(plotter, qp_values; process=$(CP[:process])), num_vertices(plotter)))
return Makie.mesh!(CP, coords, visible(plotter), color=solution, shading=CP[:shading], scale_plot=CP[:scale_plot], colormap=CP[:colormap], transparent=CP[:transparent])
return Makie.mesh!(CP, plotter.mesh, color=solution, shading=CP[:shading], scale_plot=CP[:scale_plot], colormap=CP[:colormap], transparent=CP[:transparent])
end

"""
Expand Down Expand Up @@ -145,17 +172,29 @@ function Makie.plot!(WF::Wireframe{<:Tuple{<:MakiePlotter{dim}}}) where dim
# u_matrix = @lift($(WF[:deformation_field])===:default ? zeros(0,3) : transfer_solution(plotter; field_idx=Ferrite.find_field(plotter.dh,$(WF[:deformation_field])), process=identity))
# coords = @lift($(WF[:deformation_field])===:default ? plotter.physical_coords : plotter.physical_coords .+ ($(WF[:scale]) .* $(u_matrix)))
#original representation
nodal_u_matrix = @lift($(WF[:deformation_field])===:default ? zeros(0,3) : dof_to_node(plotter.dh, $(WF[1][].u); field=Ferrite.find_field(plotter.dh,$(WF[:deformation_field]))))
gridnodes = @lift($(WF[:deformation_field])===:default ? plotter.gridnodes : plotter.gridnodes .+ ($(WF[:deformation_scale]) .* $(nodal_u_matrix)))
nodal_u_matrix = @lift begin
if $(WF[:deformation_field])===:default
Point3f[Point3f(0,0,0)]
else
convert(Vector{Point{Ferrite.getdim(plotter.dh.grid),Float32}},Makie.to_vertices(dof_to_node(plotter.dh, $(WF[1][].u); field=Ferrite.find_field(plotter.dh,$(WF[:deformation_field])))))
end
end
gridnodes = @lift begin
if $(WF[:deformation_field])===:default
plotter.gridnodes
else
plotter.gridnodes .+ ($(WF[:deformation_scale]) .* $(nodal_u_matrix))
end
end
lines = @lift begin
dim > 2 ? (lines = Point3f[]) : (lines = Point2f[])
for cell in Ferrite.getcells(plotter.dh.grid)
boundaryentities = dim < 3 ? Ferrite.faces(cell) : Ferrite.edges(cell)
append!(lines, [$gridnodes[e,:] for boundary in boundaryentities for e in boundary])
append!(lines, [$gridnodes[e] for boundary in boundaryentities for e in boundary])
end
lines
end
nodes = @lift($(WF[:plotnodes]) ? $(gridnodes) : zeros(Float32,0,3))
nodes = @lift($(WF[:plotnodes]) ? $(gridnodes) : Point3f[Point3f(0,0,0)])
#plot cellsets
cellsets = plotter.dh.grid.cellsets
cellset_to_value = Dict{String,Int}()
Expand All @@ -170,20 +209,32 @@ function Makie.plot!(WF::Wireframe{<:Tuple{<:MakiePlotter{dim}}}) where dim
end
end
end
u_matrix = @lift($(WF[:deformation_field])===:default ? zeros(0,3) : transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(WF[:deformation_field])), process=identity))
coords = @lift($(WF[:deformation_field])===:default ? plotter.physical_coords : plotter.physical_coords .+ ($(WF[:deformation_scale]) .* $(u_matrix)))
u_matrix = @lift begin
if $(WF[:deformation_field])===:default
Point3f[Point3f(0,0,0)]
else
Makie.to_vertices(transfer_solution(plotter,$(plotter.u); field_idx=Ferrite.find_field(plotter.dh,$(WF[:deformation_field])), process=identity))
end
end
coords = @lift begin
if $(WF[:deformation_field])===:default
plotter.physical_coords_mesh[1:end] = plotter.physical_coords
else
plotter.physical_coords_mesh[1:end] = plotter.physical_coords .+ ($(WF[:deformation_scale]) .* $(u_matrix))
end
end
colorrange = isempty(cellset_to_value) ? (0,1) : (0,maximum(values(cellset_to_value)))
cellset_u = reshape(transfer_scalar_celldata(plotter, cellset_u; process=identity), num_vertices(plotter))
Makie.mesh!(WF, coords, visible(plotter), color=cellset_u, shading=false, scale_plot=false, colormap=:darktest, visible=WF[:cellsets])
Makie.mesh!(WF, plotter.mesh, color=cellset_u, shading=false, scale_plot=false, colormap=:darktest, visible=WF[:cellsets])
#plot the nodes
Makie.scatter!(WF,gridnodes,markersize=WF[:markersize], color=WF[:color], visible=WF[:visible])
#set up nodelabels
nodelabels = @lift $(WF[:nodelabels]) ? ["$i" for i in 1:size($gridnodes,1)] : [""]
nodepositions = @lift $(WF[:nodelabels]) ? [dim < 3 ? Point2f(row) : Point3f(row) for row in eachrow($gridnodes)] : (dim < 3 ? [Point2f((0,0))] : [Point3f((0,0,0))])
nodepositions = @lift $(WF[:nodelabels]) ? $gridnodes : (dim < 3 ? Point2f[Point2f((0,0))] : Point3f[Point3f((0,0,0))])
#set up celllabels
celllabels = @lift $(WF[:celllabels]) ? ["$i" for i in 1:Ferrite.getncells(plotter.dh.grid)] : [""]
cellpositions = @lift $(WF[:celllabels]) ? [midpoint(cell,$gridnodes) for cell in Ferrite.getcells(plotter.dh.grid)] : (dim < 3 ? [Point2f((0,0))] : [Point3f((0,0,0))])
Makie.text!(WF,nodelabels, position=nodepositions, textsize=WF[:textsize], offset=WF[:offset],color=WF[:nodelabelcolor])
Makie.text!(WF,nodepositions, text=nodelabels, fontsize=WF[:textsize], offset=WF[:offset],color=WF[:nodelabelcolor])
Makie.text!(WF,celllabels, position=cellpositions, textsize=WF[:textsize], color=WF[:celllabelcolor], align=(:center,:center))
#plot edges (3D) /faces (2D) of the mesh
Makie.linesegments!(WF,lines,color=WF[:color], linewidth=WF[:strokewidth], visible=WF[:visible])
Expand All @@ -193,15 +244,16 @@ end
function Makie.plot!(WF::Wireframe{<:Tuple{<:Ferrite.AbstractGrid{dim}}}) where dim
grid = WF[1][]
coords = [Ferrite.getcoordinates(node)[i] for node in Ferrite.getnodes(grid), i in 1:dim]
coords = Makie.to_vertices(coords)
dim > 2 ? (lines = Point3f[]) : (lines = Point2f[])
for cell in Ferrite.getcells(grid)
boundaryentities = dim < 3 ? Ferrite.faces(cell) : Ferrite.edges(cell)
append!(lines, [coords[e,:] for boundary in boundaryentities for e in boundary])
append!(lines, [coords[e] for boundary in boundaryentities for e in boundary])
end
nodes = @lift($(WF[:plotnodes]) ? coords : zeros(Float32,0,3))
nodes = @lift($(WF[:plotnodes]) ? coords : Point3f[Point3f(0,0,0)])
Makie.scatter!(WF,nodes,markersize=WF[:markersize], color=WF[:color])
nodelabels = @lift $(WF[:nodelabels]) ? ["$i" for i in 1:size(coords,1)] : [""]
nodepositions = @lift $(WF[:nodelabels]) ? [dim < 3 ? Point2f(row) : Point3f(row) for row in eachrow(coords)] : (dim < 3 ? [Point2f((0,0))] : [Point3f((0,0,0))])
nodepositions = @lift $(WF[:nodelabels]) ? coords : (dim < 3 ? Point2f[Point2f((0,0))] : Point3f[Point3f((0,0,0))])
celllabels = @lift $(WF[:celllabels]) ? ["$i" for i in 1:Ferrite.getncells(grid)] : [""]
cellpositions = @lift $(WF[:celllabels]) ? [midpoint(cell,coords) for cell in Ferrite.getcells(grid)] : (dim < 3 ? [Point2f((0,0))] : [Point3f((0,0,0))])
#cellsetsplot
Expand All @@ -222,7 +274,7 @@ function Makie.plot!(WF::Wireframe{<:Tuple{<:Ferrite.AbstractGrid{dim}}}) where
plotter = MakiePlotter(dh,cellset_u)
cellset_u = reshape(transfer_scalar_celldata(plotter, cellset_u; process=identity), num_vertices(plotter))
colorrange = isempty(cellset_to_value) ? (0,1) : (0,maximum(values(cellset_to_value)))
Makie.mesh!(WF, plotter.physical_coords, visible(plotter), color=cellset_u, shading=false, scale_plot=false, colormap=:darktest, visible=WF[:cellsets])
Makie.mesh!(WF, plotter.mesh, color=cellset_u, shading=false, scale_plot=false, colormap=:darktest, visible=WF[:cellsets])
Makie.text!(WF,nodelabels, position=nodepositions, textsize=WF[:textsize], offset=WF[:offset],color=WF[:nodelabelcolor])
Makie.text!(WF,celllabels, position=cellpositions, textsize=WF[:textsize], color=WF[:celllabelcolor], align=(:center,:center))
Makie.linesegments!(WF,lines,color=WF[:color], strokewidth=WF[:strokewidth])
Expand Down Expand Up @@ -256,8 +308,10 @@ function Makie.plot!(SF::Surface{<:Tuple{<:MakiePlotter{2}}})
plotter = SF[1][]
field = @lift($(SF[:field])===:default ? 1 : Ferrite.find_field(plotter.dh,$(SF[:field])))
solution = @lift(reshape(transfer_solution(plotter, $(plotter.u); field_idx=$(field), process=$(SF[:process])), num_vertices(plotter)))
points = @lift([Point3f(coord[1], coord[2], $(solution)[idx]) for (idx, coord) in enumerate(eachrow(plotter.physical_coords))])
return Makie.mesh!(SF,points, visible(plotter), color=solution, scale_plot=SF[:scale_plot], shading=SF[:shading], colormap=SF[:colormap])
coords = @lift begin
Point3f[Point3f(coord[1], coord[2], $(solution)[idx]) for (idx, coord) in enumerate(plotter.physical_coords)]
end
return Makie.mesh!(SF, coords, plotter.vis_triangles, color=solution, scale_plot=SF[:scale_plot], shading=SF[:shading], colormap=SF[:colormap])
end

"""
Expand Down Expand Up @@ -294,17 +348,15 @@ function Makie.plot!(AR::Arrows{<:Tuple{<:MakiePlotter{dim}}}) where dim
@assert Ferrite.getfielddim(plotter.dh,field[]) > 1
solution = @lift(transfer_solution(plotter, $(plotter.u); field_idx=$(field), process=identity))
if dim == 2
ps = [Point2f(i) for i in eachrow(plotter.physical_coords)]
ns = @lift([Vec2f(i) for i in eachrow($(solution))])
lengths = @lift($(AR[:color])===:default ? $(AR[:process]).($(ns)) : ones(length($(ns)))*$(AR[:color]))
elseif dim == 3
ps = [Point3f(i) for i in eachrow(plotter.physical_coords)]
ns = @lift([Vec3f(i) for i in eachrow($(solution))])
lengths = @lift($(AR[:color])===:default ? $(AR[:process]).($(ns)) : ones(length($(ns)))*$(AR[:color]))
else
error("Arrows plots are only available in dim ≥ 2")
end
Makie.arrows!(AR, ps, ns, arrowsize=AR[:arrowsize], colormap=AR[:colormap], color=lengths, lengthscale=AR[:lengthscale])
Makie.arrows!(AR, plotter.physical_coords, ns, arrowsize=AR[:arrowsize], colormap=AR[:colormap], color=lengths, lengthscale=AR[:lengthscale])
end

"""
Expand Down Expand Up @@ -477,9 +529,8 @@ end

function ferriteviewer(plotter::MakiePlotter, data::Vector{Vector{T}}) where T
fig = ferriteviewer(plotter)
timeslider = labelslider!(fig, "timestep n:", 1:length(data); format = x->"$x", sliderkw = Dict(:snap=>false))
fig[2,1] = timeslider.layout
@lift(FerriteViz.update!(plotter,data[$(timeslider.slider.value)]))
sg = SliderGrid(fig[2,1], (label="timestep n:", range=1:length(data), format = x->"$x"))
@lift(FerriteViz.update!(plotter,data[$(sg.sliders[1].value)]))
display(fig)
end

Expand Down
Loading