Skip to content

Commit adb85be

Browse files
Merge pull request #185 from gridap/create_trial_fe_space_out_of_distributed_cell_field
Necessary changes required to be able to instantiate a TrialFESpace with an instance of DistributedCellField
2 parents d7e2d44 + f0d75a3 commit adb85be

File tree

4 files changed

+108
-56
lines changed

4 files changed

+108
-56
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
### Added
11+
12+
- New overloads for the `TrialFESpace` constructor where the data to be imposed is passed as a `DistributedCellField`. Since PR[#183](https://github.com/gridap/GridapDistributed.jl/pull/183).
13+
814
## [0.4.10] - 2025-09-29
915

1016
### Added

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ BlockArrays = "1"
2121
CircularArrays = "1.4.0"
2222
FillArrays = "1"
2323
ForwardDiff = "0.10, 1"
24-
Gridap = "0.19.4"
24+
Gridap = "0.19.6"
2525
LinearAlgebra = "1"
2626
MPI = "0.16, 0.17, 0.18, 0.19, 0.20"
2727
PartitionedArrays = "0.3.3"

src/FESpaces.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,21 @@ function FESpaces.TrialFESpace!(f::DistributedSingleFieldFESpace,fun)
414414
DistributedSingleFieldFESpace(spaces,f.gids,f.trian,f.vector_type,f.metadata)
415415
end
416416

417+
function FESpaces.TrialFESpace(f::DistributedSingleFieldFESpace,cf::DistributedCellField)
418+
spaces = map(local_views(f),local_views(cf)) do s, field
419+
TrialFESpace(s,field)
420+
end
421+
DistributedSingleFieldFESpace(spaces,f.gids,f.trian,f.vector_type,f.metadata)
422+
end
423+
424+
function FESpaces.TrialFESpace(cf::DistributedCellField,f::DistributedSingleFieldFESpace)
425+
spaces = map(local_views(f),local_views(cf)) do s, field
426+
TrialFESpace(s,field)
427+
end
428+
DistributedSingleFieldFESpace(spaces,f.gids,f.trian,f.vector_type,f.metadata)
429+
end
430+
431+
417432
function FESpaces.HomogeneousTrialFESpace(f::DistributedSingleFieldFESpace)
418433
spaces = map(f.spaces) do s
419434
HomogeneousTrialFESpace(s)
@@ -478,6 +493,16 @@ function FESpaces.interpolate_dirichlet!(
478493
FEFunction(f,free_values,dirichlet_values)
479494
end
480495

496+
function FESpaces.interpolate_dirichlet!(
497+
u::DistributedCellField, free_values::AbstractVector,
498+
dirichlet_values::AbstractArray{<:AbstractVector},
499+
f::DistributedSingleFieldFESpace)
500+
map(local_views(u), f.spaces,local_views(free_values),dirichlet_values) do u,V,fvec,dvec
501+
interpolate_dirichlet!(u,fvec,dvec,V)
502+
end
503+
FEFunction(f,free_values,dirichlet_values)
504+
end
505+
481506
function FESpaces.interpolate_everywhere(u, f::DistributedSingleFieldFESpace)
482507
free_values = zero_free_values(f)
483508
dirichlet_values = get_dirichlet_dof_values(f)

test/FESpacesTests.jl

Lines changed: 76 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -85,66 +85,87 @@ function main(distribute,parts,das)
8585
model = CartesianDiscreteModel(ranks,parts,domain,cells)
8686
Ω = Triangulation(model)
8787
Γ = Boundary(model)
88+
= Measure(Ω,3)
89+
90+
function _interpolation_tests(u,model,Ω,Γ,output)
91+
reffe = ReferenceFE(lagrangian,Float64,1)
92+
V = TestFESpace(model,reffe,dirichlet_tags="boundary")
93+
U = TrialFESpace(u,V)
94+
V2 = FESpace(Ω,reffe)
95+
@test get_vector_type(V) <: PVector{<:Vector}
96+
@test get_vector_type(U) <: PVector{<:Vector}
97+
@test get_vector_type(V2) <: PVector{<:Vector}
98+
99+
free_values_partition = map(partition(V.gids)) do indices
100+
ones(Float64,local_length(indices))
101+
end
102+
103+
free_values = PVector(free_values_partition,partition(V.gids))
104+
fh = FEFunction(U,free_values)
105+
zh = zero(U)
106+
uh = interpolate(u,U)
107+
eh = u - uh
108+
109+
uh_dir = interpolate_dirichlet(u,U)
110+
free_values = zero_free_values(U)
111+
dirichlet_values = get_dirichlet_dof_values(U)
112+
uh_dir2 = interpolate_dirichlet!(u,free_values,dirichlet_values,U)
113+
114+
uh_everywhere = interpolate_everywhere(u,U)
115+
dirichlet_values0 = zero_dirichlet_values(U)
116+
uh_everywhere_ = interpolate_everywhere!(u,free_values,dirichlet_values0,U)
117+
eh2 = u - uh_everywhere
118+
eh2_ = u - uh_everywhere_
119+
120+
uh_everywhere2 = interpolate_everywhere(uh_everywhere,U)
121+
uh_everywhere2_ = interpolate_everywhere!(uh_everywhere,free_values,dirichlet_values,U)
122+
eh3 = u - uh_everywhere2
123+
124+
dofs = get_fe_dof_basis(U)
125+
cell_vals = dofs(uh)
126+
gather_free_values!(free_values,U,cell_vals)
127+
gather_free_and_dirichlet_values!(free_values,dirichlet_values,U,cell_vals)
128+
uh4 = FEFunction(U,free_values,dirichlet_values)
129+
eh4 = u - uh4
130+
131+
= Measure(Ω,3)
132+
cont = ( abs2(eh) )dΩ
133+
cont2 = ( abs2(eh2) )dΩ
134+
cont2_ = ( abs2(eh2_) )dΩ
135+
cont3 = ( abs2(eh3) )dΩ
136+
cont4 = ( abs2(eh4) )dΩ
137+
@test sqrt(sum(cont)) < 1.0e-9
138+
@test sqrt(sum(cont2)) < 1.0e-9
139+
@test sqrt(sum(cont2_)) < 1.0e-9
140+
@test sqrt(sum(cont3)) < 1.0e-9
141+
@test sqrt(sum(cont4)) < 1.0e-9
142+
143+
writevtk(Ω,joinpath(output,"Ω"), nsubcells=10,
144+
celldata=["err"=>cont[Ω]],
145+
cellfields=["uh"=>uh,"zh"=>zh,"eh"=>eh])
146+
147+
writevtk(Γ,joinpath(output,"Γ"),cellfields=["uh"=>uh])
148+
end
88149

150+
# Testing interpolation with a Julia function
89151
u((x,y)) = x+y
152+
_interpolation_tests(u,model,Ω,Γ,output)
153+
154+
# Testing interpolation with a DistributedCellField
155+
# The DistributedCellField MUST be defined in all cells
156+
# (i.e., owned + ghost) for the tests within
157+
# _interpolation_tests(...) to pass. The problematic part is
158+
# in the interpolation of the dirichlet values
159+
Ωghosts = Triangulation(with_ghost, model)
160+
fields = map(local_views(Ωghosts)) do Ω
161+
CellField(u,Ω)
162+
end
163+
u_cf = GridapDistributed.DistributedCellField(fields,Ωghosts)
164+
_interpolation_tests(u_cf,model,Ωghosts,Γ,output)
165+
90166
reffe = ReferenceFE(lagrangian,Float64,1)
91167
V = TestFESpace(model,reffe,dirichlet_tags="boundary")
92168
U = TrialFESpace(u,V)
93-
V2 = FESpace(Ω,reffe)
94-
@test get_vector_type(V) <: PVector{<:Vector}
95-
@test get_vector_type(U) <: PVector{<:Vector}
96-
@test get_vector_type(V2) <: PVector{<:Vector}
97-
98-
free_values_partition = map(partition(V.gids)) do indices
99-
ones(Float64,local_length(indices))
100-
end
101-
102-
free_values = PVector(free_values_partition,partition(V.gids))
103-
fh = FEFunction(U,free_values)
104-
zh = zero(U)
105-
uh = interpolate(u,U)
106-
eh = u - uh
107-
108-
uh_dir = interpolate_dirichlet(u,U)
109-
free_values = zero_free_values(U)
110-
dirichlet_values = get_dirichlet_dof_values(U)
111-
uh_dir2 = interpolate_dirichlet!(u,free_values,dirichlet_values,U)
112-
113-
uh_everywhere = interpolate_everywhere(u,U)
114-
dirichlet_values0 = zero_dirichlet_values(U)
115-
uh_everywhere_ = interpolate_everywhere!(u,free_values,dirichlet_values0,U)
116-
eh2 = u - uh_everywhere
117-
eh2_ = u - uh_everywhere_
118-
119-
uh_everywhere2 = interpolate_everywhere(uh_everywhere,U)
120-
uh_everywhere2_ = interpolate_everywhere!(uh_everywhere,free_values,dirichlet_values,U)
121-
eh3 = u - uh_everywhere2
122-
123-
dofs = get_fe_dof_basis(U)
124-
cell_vals = dofs(uh)
125-
gather_free_values!(free_values,U,cell_vals)
126-
gather_free_and_dirichlet_values!(free_values,dirichlet_values,U,cell_vals)
127-
uh4 = FEFunction(U,free_values,dirichlet_values)
128-
eh4 = u - uh4
129-
130-
= Measure(Ω,3)
131-
cont = ( abs2(eh) )dΩ
132-
cont2 = ( abs2(eh2) )dΩ
133-
cont2_ = ( abs2(eh2_) )dΩ
134-
cont3 = ( abs2(eh3) )dΩ
135-
cont4 = ( abs2(eh4) )dΩ
136-
@test sqrt(sum(cont)) < 1.0e-9
137-
@test sqrt(sum(cont2)) < 1.0e-9
138-
@test sqrt(sum(cont2_)) < 1.0e-9
139-
@test sqrt(sum(cont3)) < 1.0e-9
140-
@test sqrt(sum(cont4)) < 1.0e-9
141-
142-
143-
writevtk(Ω,joinpath(output,"Ω"), nsubcells=10,
144-
celldata=["err"=>cont[Ω]],
145-
cellfields=["uh"=>uh,"zh"=>zh,"eh"=>eh])
146-
147-
writevtk(Γ,joinpath(output,"Γ"),cellfields=["uh"=>uh])
148169

149170
# Assembly
150171
Ωass = Triangulation(das,model)

0 commit comments

Comments
 (0)