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

Add support for 1D physical space cases #75

Merged
merged 7 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
````

<p align="center">
<img src="_readme/images/1d_Fig1.png" width="500"/>
</p>

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)
````

<p align="center">
<img src="_readme/images/1d_Fig2.png" width="500"/>
</p>

### 2D Plots

Expand Down
Binary file added _readme/images/1d_Fig1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _readme/images/1d_Fig2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 19 additions & 4 deletions src/recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading