Skip to content

Commit

Permalink
added basic restrict/mask functions and tests (#118)
Browse files Browse the repository at this point in the history
* added basic restrict/mask functions and tests

* applied feedback

* fixed spaces

* Update restrictions.jl

* update docstring
  • Loading branch information
quffaro authored Oct 21, 2024
1 parent 81e763d commit 471c35d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/CombinatorialSpaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ include("DiscreteExteriorCalculus.jl")
include("MeshInterop.jl")
include("FastDEC.jl")
include("Meshes.jl")
include("restrictions.jl")

@reexport using .SimplicialSets
@reexport using .DiscreteExteriorCalculus
Expand Down
30 changes: 30 additions & 0 deletions src/restrictions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CombinatorialSpaces, CombinatorialSpaces.SimplicialSets

export restrict, mask!

""" restrict(sd::HasDeltaSet, func::Function, form)
restrict a form to a subset of the points.
# Arguments:
`sd`: the mesh,
`func`: a function that chooses the submesh indices corresponding to the boundary
`form`: the vector you want to restrict
"""
restrict(sd::HasDeltaSet, func::Function, form) = form[func(sd)]

restrict(indices, form) = form[indices]

""" mask(sd::HasDeltaSet, func::Function, form, values)
Masks a form to values on a subset of the points.
# Arguments:
`sd`: the mesh
`func`: function that chooses the submesh indices corresponding to the boundary
`form`: the vector you want to restrict and
`values:` is the vector you want to replace with
"""
mask!(sd::HasDeltaSet, func::Function, form, values) = setindex!(form, values, func(sd))

mask!(indices, form, values) = setindex!(form, values, indices)
26 changes: 26 additions & 0 deletions test/restrictions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CombinatorialSpaces
using GeometryBasics: Point3
Point3D = Point3{Float64}

rect′ = loadmesh(Rectangle_30x10());
rect = EmbeddedDeltaDualComplex2D{Bool,Float64,Point3D}(rect′);
subdivide_duals!(rect, Barycenter());

left_wall_idxs(sd) = begin
min_y = minimum(p -> p[2], sd[:point])
findall(p -> abs(p[2] - min_y) sd[1,:length]+1e-4, sd[:point])
end

# 0-form
zero_form = 2*ones(nv(rect));
idxlen = length(left_wall_idxs(rect))
max = Float64(length(zero_form) + 1)

@test restrict(left_wall_idxs(rect), zero_form) == fill(2.0, idxlen)
@test restrict(rect, left_wall_idxs, zero_form) == fill(2.0, idxlen)

copy_zero_form = copy(zero_form);
mask!(rect, left_wall_idxs, copy_zero_form, fill(max, idxlen));

@test copy_zero_form[copy_zero_form .== max] == fill(max, idxlen)

4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ end
include("Backends.jl")
end

@testset "Restrictions" begin
include("restrictions.jl")
end

end

0 comments on commit 471c35d

Please sign in to comment.