diff --git a/README.md b/README.md index ec37416..563602c 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,38 @@ using FileIO mkdir("models") mkdir("images") ```` +### 1D Plots +Let us consider a 1D triangulation Ω, a function `u` and the corresponding FE function `uh` constructed with Gridap +````julia +model = CartesianDiscreteModel((0,15),150) +Ω = Triangulation(model) +reffe = ReferenceFE(lagrangian, Float64, 1) +V = FESpace(model, reffe) +u=x->cos(π*x[1])*exp(-x[1]/10) +uh = interpolate(u, V) +```` + +The visualization of the function can be achieved as follows +````julia +fig=plot(uh) +save("images/1d_Fig1.png", fig) +```` + +

+ +

+ +We may also plot the function with a line and its value at the boundaries +````julia +Γ = BoundaryTriangulation(model) +fig=lines(Ω,u) +plot!(Γ,uh,color=:red) +save("images/1d_Fig2.png", fig) +```` + +

+ +

### 2D Plots diff --git a/_readme/images/1d_Fig1.png b/_readme/images/1d_Fig1.png new file mode 100644 index 0000000..6fd8131 Binary files /dev/null and b/_readme/images/1d_Fig1.png differ diff --git a/_readme/images/1d_Fig2.png b/_readme/images/1d_Fig2.png new file mode 100644 index 0000000..49f42fe Binary files /dev/null and b/_readme/images/1d_Fig2.png differ diff --git a/src/conversions.jl b/src/conversions.jl index fc17858..46f1091 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -178,3 +178,10 @@ end to_scalar(x) = x to_scalar(x::VectorValue) = norm(x) + +function to_point1D(trian::Triangulation{Dc,1}, uh::Any) where Dc + vds = first(visualization_data(trian, "", cellfields=["uh"=>uh])) + y = vds.nodaldata["uh"] + x = map(y->y[1], get_node_coordinates(vds.grid)) + x, y +end diff --git a/src/recipes.jl b/src/recipes.jl index 8edf803..0dc5547 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -109,7 +109,8 @@ function Makie.convert_arguments(t::Type{<:Union{Makie.Wireframe, Makie.Scatter} end # Set default plottype as mesh if argument is type Triangulation, i.e., mesh(Ω) == plot(Ω). -Makie.plottype(::Triangulation) = PlotGridMesh +Makie.plottype(::Triangulation{Dc,1}) where Dc = Makie.Scatter +Makie.plottype(::Triangulation{Dc,Dp}) where {Dc,Dp} = PlotGridMesh Makie.args_preferred_axis(t::Triangulation)= num_point_dims(t)<=2 ? Makie.Axis : Makie.LScene Makie.plottype(::PlotGrid) = PlotGridMesh Makie.args_preferred_axis(pg::PlotGrid)= num_point_dims(pg.Grid)<=2 ? Makie.Axis : Makie.LScene @@ -138,7 +139,8 @@ function Makie.plot!(p::MeshField{<:Tuple{Triangulation, Any}}) ) end -Makie.plottype(::Triangulation, ::Any) = MeshField +Makie.plottype(::Triangulation{Dc,1}, ::Any) where Dc = Makie.Scatter +Makie.plottype(::Triangulation{Dc,Dp}, ::Any) where {Dc,Dp} = MeshField function Makie.plot!(p::MeshField{<:Tuple{CellField}}) uh = p[1] @@ -154,8 +156,21 @@ function Makie.plot!(p::MeshField{<:Tuple{CellField}}) ) end -Makie.plottype(::CellField) = MeshField -Makie.args_preferred_axis(c::CellField)= num_point_dims(get_triangulation(c))<=2 ? Makie.Axis : Makie.LScene +function Makie.convert_arguments(::Union{Type{Makie.Lines},Type{Makie.ScatterLines},Type{Makie.Scatter}}, c::CellField) + trian=get_triangulation(c) + if num_point_dims(trian)==1 + return to_point1D(trian, c) + else + ArgumentError("This function requires a 1D CellField") + end +end + +function Makie.convert_arguments(::Union{Type{Makie.Lines},Type{Makie.ScatterLines},Type{Makie.Scatter}}, trian::Triangulation{Dc,1}, uh::Any=x->0.0) where Dc + return to_point1D(trian, uh) +end + +Makie.plottype(c::CellField) = Makie.plottype(get_triangulation(c),c) +Makie.args_preferred_axis(c::CellField)= Makie.args_preferred_axis(get_triangulation(c)) function Makie.point_iterator(pg::PlotGrid) UnstructuredGrid(pg.grid) |> to_dg_points diff --git a/test/runtests.jl b/test/runtests.jl index a7d99eb..2b855f0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,6 +21,27 @@ function savefig(f, suffix::String) return true end +@testset "Tests 1D" begin + model = CartesianDiscreteModel((0,15),90) + Ω = Triangulation(model) + reffe = ReferenceFE(lagrangian, Float64, 1) + V = FESpace(model, reffe) + u=x->cos(π*x[1])*exp(-x[1]/10) + uh = interpolate(u, V) + Γ = BoundaryTriangulation(model) + + @test savefig("1d_Fig1") do + fig = lines(Ω,u) + scatter!(uh,color=:red) + fig + end + @test savefig("1d_Fig2") do + fig=scatterlines(uh) + plot!(Γ,uh,color=:red) + fig + end +end + @testset "Tests 2D" begin domain = (0,1,0,1)