From 362f85d86687099b945bf68e2c99282c0dbf1f8d Mon Sep 17 00:00:00 2001 From: JordiManyer Date: Wed, 13 Nov 2024 14:50:36 +1100 Subject: [PATCH] Added EmbeddedCollections --- scripts/Embedded/MWEs/updateable_trians.jl | 47 ++++----------- src/Embedded/Embedded.jl | 1 + src/Embedded/EmbeddedCollections.jl | 28 +++++++++ src/GridapTopOpt.jl | 4 +- test/seq/EmbeddedCollectionsTests.jl | 59 +++++++++++++++++++ ...sts.jl => EmbeddedDifferentiationTests.jl} | 2 +- test/seq/runtests.jl | 5 +- 7 files changed, 105 insertions(+), 41 deletions(-) create mode 100644 src/Embedded/EmbeddedCollections.jl create mode 100644 test/seq/EmbeddedCollectionsTests.jl rename test/seq/{EmbeddedTests.jl => EmbeddedDifferentiationTests.jl} (99%) diff --git a/scripts/Embedded/MWEs/updateable_trians.jl b/scripts/Embedded/MWEs/updateable_trians.jl index 652f28d..c1f1604 100644 --- a/scripts/Embedded/MWEs/updateable_trians.jl +++ b/scripts/Embedded/MWEs/updateable_trians.jl @@ -16,36 +16,6 @@ end φ(r) = interpolate(x->sqrt((x[1]-0.5)^2+(x[2]-0.5)^2)-r,V_φ) # Circle -struct EmbeddedTriangulationCollection - func::Function - trians::Dict{Symbol,Triangulation} - bgmodel::DiscreteModel - cutgeo::EmbeddedDiscretization -end - -function EmbeddedTriangulationCollection( - func::Function,bgmodel::DiscreteModel,φ0 -) - geo = DiscreteGeometry(φ0,bgmodel) - cutgeo = cut(bgmodel,geo) - trians = Dict(pairs(func(cutgeo))) - return EmbeddedTriangulationCollection(func,trians,bgmodel,cutgeo) -end - -function update!(c::EmbeddedTriangulationCollection,φh) - geo = DiscreteGeometry(φh,c.bgmodel) - cutgeo = cut(c.bgmodel,geo) - trians = func(cutgeo) - for (key,value) in pairs(trians) - c.trians[key] = value - end - return c -end - -function Base.getindex(c::EmbeddedTriangulationCollection,key) - return c.trians[key] -end - order = 1 model = generate_model(2,10) @@ -56,19 +26,22 @@ reffe = ReferenceFE(lagrangian,Float64,order) V_φ = TestFESpace(model,reffe) φ0 = φ(0.2) -Ωs = EmbeddedTriangulationCollection(model,φ0) do cutgeo +Ωs = EmbeddedCollection(model,φ0) do cutgeo + Ω = Triangulation(cutgeo,PHYSICAL_IN) + Γ = EmbeddedBoundary(cutgeo) (; - :Ωin => Triangulation(cutgeo,PHYSICAL_IN), - :Ωout => Triangulation(cutgeo,PHYSICAL_OUT), - :Γ => EmbeddedBoundary(cutgeo) + :Ω => Ω, + :Γ => Γ, + :dΩ => Measure(Ω,2*order), + :dΓ => Measure(Γ,2*order) ) end -area(Ωs) = sum(∫(1.0)*Measure(Ωs[:Ωin],2)) -contour(Ωs) = sum(∫(1.0)*Measure(Ωs[:Γ],2)) +area(Ωs) = sum(∫(1.0)*Ωs[:dΩ]) +contour(Ωs) = sum(∫(1.0)*Ωs[:dΓ]) for r in 0.2:0.1:0.5 - update!(Ωs,φ(r)) + update_collection!(Ωs,φ(r)) println(" >> Radius: $r") println(" >> Area: $(area(Ωs)) (expected: $(π*r^2))") println(" >> Contour: $(contour(Ωs)) (expected: $(2π*r))") diff --git a/src/Embedded/Embedded.jl b/src/Embedded/Embedded.jl index 4edefd7..ced074c 100644 --- a/src/Embedded/Embedded.jl +++ b/src/Embedded/Embedded.jl @@ -1,3 +1,4 @@ include("DifferentiableTriangulations.jl") include("SubFacetBoundaryTriangulations.jl") +include("EmbeddedCollections.jl") diff --git a/src/Embedded/EmbeddedCollections.jl b/src/Embedded/EmbeddedCollections.jl new file mode 100644 index 0000000..5c31128 --- /dev/null +++ b/src/Embedded/EmbeddedCollections.jl @@ -0,0 +1,28 @@ + +struct EmbeddedCollection + generate :: Function + objects :: Dict{Symbol,Any} + bgmodel :: Union{<:DiscreteModel,<:DistributedDiscreteModel} + + function EmbeddedCollection( + generate::Function,bgmodel,φ0 + ) + geo = DiscreteGeometry(φ0,bgmodel) + cutgeo = cut(bgmodel,geo) + objects = Dict{Symbol,Any}(pairs(generate(cutgeo))) + new(generate,objects,bgmodel) + end +end + +(c::EmbeddedCollection)(φh) = update_collection!(c,φh) + +function update_collection!(c::EmbeddedCollection,φh) + geo = DiscreteGeometry(φh,c.bgmodel) + cutgeo = cut(c.bgmodel,geo) + merge!(c.objects,pairs(c.generate(cutgeo))) + return c +end + +function Base.getindex(c::EmbeddedCollection,key) + return c.objects[key] +end diff --git a/src/GridapTopOpt.jl b/src/GridapTopOpt.jl index c19784f..aa25bca 100644 --- a/src/GridapTopOpt.jl +++ b/src/GridapTopOpt.jl @@ -52,8 +52,8 @@ export evaluate_derivatives! include("Embedded/Embedded.jl") export DifferentiableTriangulation -export SubFacetBoundaryTriangulation -export SubFacetSkeletonTriangulation +export SubFacetBoundaryTriangulation, SubFacetSkeletonTriangulation +export EmbeddedCollection, update_collection! include("Utilities.jl") export SmoothErsatzMaterialInterpolation diff --git a/test/seq/EmbeddedCollectionsTests.jl b/test/seq/EmbeddedCollectionsTests.jl new file mode 100644 index 0000000..8c6e867 --- /dev/null +++ b/test/seq/EmbeddedCollectionsTests.jl @@ -0,0 +1,59 @@ +module EmbeddedCollectionsTests +using Test + +using GridapTopOpt +using Gridap + +using GridapEmbedded +using GridapEmbedded.LevelSetCutters +using Gridap.Geometry, Gridap.FESpaces, Gridap.CellData, Gridap.Adaptivity + +function generate_model(D,n) + domain = (D==2) ? (0,1,0,1) : (0,1,0,1,0,1) + cell_partition = (D==2) ? (n,n) : (n,n,n) + base_model = UnstructuredDiscreteModel((CartesianDiscreteModel(domain,cell_partition))) + ref_model = refine(base_model, refinement_method = "barycentric") + model = ref_model.model + return model +end + +function main() + order = 1 + model = generate_model(2,40) + + reffe = ReferenceFE(lagrangian,Float64,order) + V_φ = TestFESpace(model,reffe) + + φ(r) = interpolate(x->sqrt((x[1]-0.5)^2+(x[2]-0.5)^2)-r,V_φ) # Circle + + φ0 = φ(0.2) + Ωs = EmbeddedCollection(model,φ0) do cutgeo + Ω = Triangulation(cutgeo,PHYSICAL_IN) + Γ = EmbeddedBoundary(cutgeo) + (; + :Ω => Ω, + :Γ => Γ, + :dΩ => Measure(Ω,2*order), + :dΓ => Measure(Γ,2*order) + ) + end + + area(Ωs) = sum(∫(1.0)*Ωs[:dΩ]) + contour(Ωs) = sum(∫(1.0)*Ωs[:dΓ]) + + for r in 0.2:0.1:0.5 + update_collection!(Ωs,φ(r)) + A = area(Ωs) + C = contour(Ωs) + println(" >> Radius: $r") + println(" >> Area: $(A) (expected: $(π*r^2))") + println(" >> Contour: $(C) (expected: $(2π*r))") + @test abs(A - π*r^2) < 1e-3 + @test abs(C - 2π*r) < 1e-3 + println("---------------------------------") + end +end + +main() + +end \ No newline at end of file diff --git a/test/seq/EmbeddedTests.jl b/test/seq/EmbeddedDifferentiationTests.jl similarity index 99% rename from test/seq/EmbeddedTests.jl rename to test/seq/EmbeddedDifferentiationTests.jl index 2769fff..2c4dc7a 100644 --- a/test/seq/EmbeddedTests.jl +++ b/test/seq/EmbeddedDifferentiationTests.jl @@ -1,4 +1,4 @@ -module EmbeddedTests +module EmbeddedDifferentiationTests using Test using GridapTopOpt diff --git a/test/seq/runtests.jl b/test/seq/runtests.jl index ca48f4f..cea7a33 100644 --- a/test/seq/runtests.jl +++ b/test/seq/runtests.jl @@ -10,6 +10,9 @@ using Test @time @testset "Inverter - HPM" begin include("InverterHPMTests.jl") end @time @testset "PZMultiFieldRepeatingState - ALM" begin include("PZMultiFieldRepeatingStateTests.jl") end @time @testset "JLD2SaveLoad" begin include("JLD2SaveLoad.jl") end -@time @testset "Embedded" begin include("EmbeddedTests.jl") end +@time @testset "Embedded" begin + include("EmbeddedDifferentiationTests.jl") + include("EmbeddedCollectionsTests.jl") +end end # module \ No newline at end of file