Skip to content

Commit

Permalink
Compute CPPs of DistributedCartesianDiscreteModels
Browse files Browse the repository at this point in the history
  • Loading branch information
ericneiva committed Jul 11, 2024
1 parent 7aca032 commit b899e02
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 44 deletions.
47 changes: 47 additions & 0 deletions src/AlgoimUtils/AlgoimUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ using Gridap.Geometry
using PartitionedArrays
using GridapDistributed
using GridapDistributed: DistributedDiscreteModel
using GridapDistributed: DistributedCartesianDiscreteModel
using GridapDistributed: DistributedTriangulation
using GridapDistributed: DistributedMeasure
using GridapDistributed: DistributedCellField
import GridapDistributed: local_views

using GridapEmbedded.Interfaces
using GridapEmbedded.Interfaces: Simplex
Expand Down Expand Up @@ -81,6 +83,17 @@ function AlgoimCallLevelSetFunction(φ::CellField,∇φ::CellField)
AlgoimCallLevelSetFunction{typeof(φ),typeof(∇φ),typeof(cache_φ),typeof(cache_∇φ)}(φ,∇φ,cache_φ,cache_∇φ)
end

struct DistributedAlgoimCallLevelSetFunction{A<:AbstractArray{<:AlgoimCallLevelSetFunction}} <: GridapType
levelsets::A
end

local_views(a::DistributedAlgoimCallLevelSetFunction) = a.levelsets

function AlgoimCallLevelSetFunction::DistributedCellField,∇φ::DistributedCellField)
levelsets = map((v,g)->AlgoimCallLevelSetFunction(v,g),local_views(φ),local_views(∇φ))
DistributedAlgoimCallLevelSetFunction(levelsets)
end

function normal(phi::AlgoimCallLevelSetFunction,x::AbstractVector{<:Point},cell_id::Int=1)
map(xi->normal(phi,xi,cell_id),x)
end
Expand Down Expand Up @@ -294,6 +307,40 @@ function compute_closest_point_projections(model::CartesianDiscreteModel,
fill_cpp_data(φ,partition,xmin,xmax,cppdegree,trim,limitstol)
end

function compute_closest_point_projections(model::DistributedCartesianDiscreteModel,
φ::AlgoimCallLevelSetFunction;
cppdegree::Int=2,
trim::Bool=false,
limitstol::Float64=1.0e-8)
cpps = map(local_views(model)) do m
cdesc = get_cartesian_descriptor(m)
partition = Int32[cdesc.partition...]
xmin = cdesc.origin
xmax = xmin + Point(cdesc.sizes .* partition)
fill_cpp_data(φ,partition,xmin,xmax,cppdegree,trim,limitstol)
end
node_gids = get_face_gids(model,0)
PVector(cpps,partition(node_gids)) |> consistent! |> wait
cpps
end

function compute_closest_point_projections(model::DistributedCartesianDiscreteModel,
φ::DistributedAlgoimCallLevelSetFunction;
cppdegree::Int=2,
trim::Bool=false,
limitstol::Float64=1.0e-8)
cpps = map(local_views(model),local_views(φ)) do m,f
cdesc = get_cartesian_descriptor(m)
partition = Int32[cdesc.partition...]
xmin = cdesc.origin
xmax = xmin + Point(cdesc.sizes .* partition)
fill_cpp_data(f,partition,xmin,xmax,cppdegree,trim,limitstol)
end
node_gids = get_face_gids(model,0)
PVector(cpps,partition(node_gids)) |> consistent! |> wait
cpps
end

function compute_closest_point_projections(fespace::FESpace,
φ::AlgoimCallLevelSetFunction,
order::Int;
Expand Down
43 changes: 26 additions & 17 deletions test/AlgoimUtilsTests/ClosestPointTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,50 @@ using Test
using CxxWrap
using Gridap
using GridapEmbedded
using PartitionedArrays
using GridapDistributed

using Gridap.Geometry: get_node_coordinates

const IN = -1
const OUT = 1
const CUT = 0

function run_case(degree)
function run_case(distribute,parts,degree)

xmin = Point(-1.1,-1.1,-1.1)
xmax = Point(1.1,1.1,1.1)
partition = Int32[16,16,16]
ranks = distribute(LinearIndices((prod(parts),)))
cells = Int32[16,16,16]

domain = (-1.1,1.1,-1.1,1.1,-1.1,1.1)
bgmodel = CartesianDiscreteModel(domain,partition)
bgmodel = CartesianDiscreteModel(ranks,parts,domain,cells)
Ω = Triangulation(bgmodel)

reffeᵠ = ReferenceFE(lagrangian,Float64,1)
V = TestFESpace(Ω,reffeᵠ,conformity=:L2)

f(x) = (x[1]*x[1]/(0.5*0.5)+x[2]*x[2]/(0.5*0.5)+x[3]*x[3]/(0.5*0.5)) - 1.0
function gradf(x::V) where {V}
V([2.0*x[1]/(0.5*0.5),2.0*x[2]/(0.5*0.5),2.0*x[3]/(0.5*0.5)])
end

fₕ = interpolate_everywhere(f,V)
phi = AlgoimCallLevelSetFunction(fₕ,(fₕ))

coords = fill_cpp_data(phi,partition,xmin,xmax,degree)
@test maximum(f.(coords)) < 1.0e-4
# coords = map(local_views(bgmodel)) do t
# _p = reduce(vcat,collect(get_node_coordinates(t)))
# [ Point(_p[i]...) for i in 1:length(_p) ]
# end
cpps = compute_closest_point_projections(bgmodel,phi,cppdegree=degree)
# map(coords,cpps,ranks) do c,p,i
# writevtk(c,"cpps_$i",nodaldata=["f"=>f.(p)])
# end

end
partials = map(cpps) do c
maximum(f.(c))
end
maxf = reduce(max,partials,init=typemin(Float64))
partials = map(cpps) do c
minimum(f.(c))
end
minf = reduce(min,partials,init=typemax(Float64))
@show maxf,minf

run_case(2)
run_case(3)
run_case(4)
run_case(5)
run_case(-1)
end

end # module
23 changes: 17 additions & 6 deletions test/AlgoimUtilsTests/PoissonAlgoimTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using GridapEmbedded.Interfaces

function run_poisson(domain,cells,order,solution_degree)

φ(x) = x[1]-x[2]
∇φ(x) = VectorValue(1.0,-1.0,0.0)
φ(x) = x[1]-x[2]-1.0
∇φ(x) = VectorValue(1.0,-1.0)
phi = AlgoimCallLevelSetFunction(φ,∇φ)

u(x) = sum(x)^solution_degree
Expand All @@ -21,15 +21,26 @@ function run_poisson(domain,cells,order,solution_degree)

degree = order == 1 ? 3 : 2*order
vquad = Quadrature(algoim,phi,degree,phase=IN)
Ωᵃ,dΩᵃ = TriangulationAndMeasure(Ω,vquad)
Ωᵃ,dΩᵃ,is_a = TriangulationAndMeasure(Ω,vquad)
squad = Quadrature(algoim,phi,degree)
_,dΓ = TriangulationAndMeasure(Ω,squad)
_,dΓ,is_c = TriangulationAndMeasure(Ω,squad)
n_Γ = normal(phi,Ω)

aggregates = aggregate(model,is_a,is_c,IN)
@show aggregates

reffe = ReferenceFE(lagrangian,Float64,order)
V = TestFESpace(Ωᵃ,reffe,conformity=:H1,dirichlet_tags="boundary")
Vstd = TestFESpace(Ωᵃ,reffe,conformity=:H1,dirichlet_tags="boundary")

V = AgFEMSpace(Vstd,aggregates)
U = TrialFESpace(V,ud)

cell_to_dofs = get_cell_dof_ids(Vstd)
@show cell_to_dofs

cell_to_dofs = get_cell_dof_ids(V)
@show cell_to_dofs

# Nitsche method
γᵈ = 2.0*order^2
h = (domain[2]-domain[1])/cells[1]
Expand Down Expand Up @@ -59,6 +70,6 @@ function run_poisson(domain,cells,order,solution_degree)

end

run_poisson((-1.1,1.1,-1.1,1.1,-1.1,1.1),(8,8,8),1,1)
run_poisson((-1.1,1.1,-1.1,1.1),(6,6),1,1)

end # module
12 changes: 7 additions & 5 deletions test/DistributedTests/PoissonTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ end
function main_algoim(distribute,parts;
threshold=1,
n=4,
cells=(n,n),
cells=(n,n,n),
order=1,
solution_degree=1)

ranks = distribute(LinearIndices((prod(parts),)))

φ(x) = x[1]-x[2]-1.0
∇φ(x) = VectorValue(1.0,-1.0)
φ(x) = x[1]-x[2]+0.01
∇φ(x) = VectorValue(1.0,-1.0) # ,0.0)
phi = AlgoimCallLevelSetFunction(φ,∇φ)
# phi = AlgoimCallLevelSetFunction(
# x -> ( x[1]*x[1] + x[2]*x[2] + x[3]*x[3] ) - 1.0,
Expand All @@ -131,7 +131,7 @@ function main_algoim(distribute,parts;
f(x) = -Δ(u)(x)
ud(x) = u(x)

domain = (-1.1,1.1,-1.1,1.1)
domain = (-1.1,1.1,-1.1,1.1) # ,-1.1,1.1)
model = CartesianDiscreteModel(ranks,parts,domain,cells)

degree = order == 1 ? 3 : 2*order
Expand All @@ -152,6 +152,8 @@ function main_algoim(distribute,parts;
n_Γ = normal(phi,Ω)

writevtk(model,"bgmodel")
writevtk(Ω,"trian")
writevtk(Ωᵃ,"atrian")

# colors = map(color_aggregates,aggregates,local_views(model))
# gids = get_cell_gids(model)
Expand Down Expand Up @@ -203,7 +205,7 @@ function main_algoim(distribute,parts;

eₕ = u - uₕ

writevtk(Ωᵃ,"rtrian",cellfields=["uh"=>uₕ,"eh"=>eₕ])
# writevtk(Ωᵃ,"rtrian",cellfields=["uh"=>uₕ,"eh"=>eₕ])

l2(u) = (( ( u*u )dΩᵃ ))
h1(u) = (( ( u*u + (u)(u) )dΩᵃ ))
Expand Down
3 changes: 2 additions & 1 deletion test/DistributedTests/mpi/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function run_driver(procs,file,sysimage)
end
end

run_driver(4,"runtests_body.jl",sysimage)
run_driver(8,"runtests_body.jl",sysimage)
run_driver(1,"runtests_body.jl",sysimage) # Check that the degenerated case works
# run_driver(64,"runtests_body.jl",sysimage)

end # module
24 changes: 14 additions & 10 deletions test/DistributedTests/mpi/runtests_body.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PArrays = PartitionedArrays
using MPI

include("../PoissonTests.jl")
include("../AggregatesTests.jl")
# include("../AggregatesTests.jl")

if ! MPI.Initialized()
MPI.Init()
Expand All @@ -17,25 +17,29 @@ function all_tests(distribute,parts)
t = PArrays.PTimer(ranks,verbose=true)
PArrays.tic!(t)

PoissonTests.main(distribute,parts)
PoissonTests.main(distribute,(prod(parts),1),cells=(12,12),geometry=:remotes)
PoissonTests.main_algoim(distribute,parts,cells=(8,8,8),solution_degree=2)
# PoissonTests.main(distribute,(prod(parts),1),cells=(12,12),geometry=:remotes)
PArrays.toc!(t,"Poisson")

if prod(parts) == 4
DistributedAggregatesTests.main(distribute,parts)
end
PArrays.toc!(t,"Aggregates")
# if prod(parts) == 4
# DistributedAggregatesTests.main(distribute,parts)
# end
# PArrays.toc!(t,"Aggregates")

display(t)
end

if MPI.Comm_size(MPI.COMM_WORLD) == 4
if MPI.Comm_size(MPI.COMM_WORLD) == 8
with_mpi() do distribute
all_tests(distribute,(2,2,2))
end
elseif MPI.Comm_size(MPI.COMM_WORLD) == 64
with_mpi() do distribute
all_tests(distribute,(2,2))
all_tests(distribute,(4,4,4))
end
elseif MPI.Comm_size(MPI.COMM_WORLD) == 1
with_mpi() do distribute
all_tests(distribute,(1,1))
all_tests(distribute,(1,1,1))
end
else
MPI.Abort(MPI.COMM_WORLD,0)
Expand Down
20 changes: 15 additions & 5 deletions test/DistributedTests/sequential/PoissonTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ module PoissonTestsSeq
using PartitionedArrays
include("../PoissonTests.jl")
with_debug() do distribute
PoissonTests.main(distribute,(2,2))
PoissonTests.main(distribute,(1,4),cells=(4,8))
PoissonTests.main(distribute,(2,4),cells=(8,8))
PoissonTests.main(distribute,(4,1),cells=(12,12),geometry=:remotes)
end
# PoissonTests.main(distribute,(2,2))
# PoissonTests.main(distribute,(1,4),cells=(4,8))
# PoissonTests.main(distribute,(2,4),cells=(8,8))
# PoissonTests.main(distribute,(4,1),cells=(12,12),geometry=:remotes)
# PoissonTests.main_algoim(distribute,(2,1,2),cells=(4,4,4),solution_degree=2)
# PoissonTests.main_algoim(distribute,(4,1,2),cells=(8,8,8),solution_degree=2)
# PoissonTests.main_algoim(distribute,(1,1,4),cells=(8,8,8),solution_degree=2)
# PoissonTests.main_algoim(distribute,(2,2,2),cells=(8,8,8))
# failing case, probably due to empty subdomains, review dirichlet BCs
PoissonTests.main_algoim(distribute,(2,2),cells=(4,4))
PoissonTests.main_algoim(distribute,(4,4),cells=(16,16)) # passes
PoissonTests.main_algoim(distribute,(3,3),cells=(6,6))
# PoissonTests.main_algoim(distribute,(6,6),cells=(12,12)) # fails
# PoissonTests.main_algoim(distribute,(8,8),cells=(16,16)) # fails
end
end

0 comments on commit b899e02

Please sign in to comment.