Skip to content

Commit b30c808

Browse files
authoredDec 7, 2023
Merge pull request #140 from gridap/redistribute-multifield
Redistribute for MultiField
2 parents 0bfb7c6 + f736a0a commit b30c808

File tree

6 files changed

+363
-29
lines changed

6 files changed

+363
-29
lines changed
 

‎NEWS.md

+6
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+
- Added redistribution for MultiFieldFESpaces. Since PR [#140](https://github.com/gridap/GridapDistributed.jl/pull/140).
13+
814
## [0.3.5] - 2023-12-04
915

1016
### Added

‎src/Adaptivity.jl

+124-29
Original file line numberDiff line numberDiff line change
@@ -260,35 +260,114 @@ function _get_cell_dof_ids_inner_space(s::TrialFESpace)
260260
_get_cell_dof_ids_inner_space(s.space)
261261
end
262262

263-
function get_redistribute_free_values_cache(fv_new::Union{PVector,Nothing},
264-
Uh_new::Union{DistributedSingleFieldFESpace,Nothing},
265-
fv_old::Union{PVector,Nothing},
266-
dv_old::Union{AbstractArray,Nothing},
267-
Uh_old::Union{DistributedSingleFieldFESpace,Nothing},
268-
model_new,
269-
glue::RedistributeGlue;
270-
reverse=false)
263+
function redistribute_fe_function(uh_old::Union{DistributedSingleFieldFEFunction,Nothing},
264+
Uh_new::Union{DistributedSingleFieldFESpace,Nothing},
265+
model_new,
266+
glue::RedistributeGlue;
267+
reverse=false)
268+
271269
old_parts, new_parts = get_old_and_new_parts(glue,Val(reverse))
272-
cell_dof_values_old = i_am_in(old_parts) ? map(scatter_free_and_dirichlet_values,local_views(Uh_old),local_views(fv_old),dv_old) : nothing
273-
cell_dof_ids_new = i_am_in(new_parts) ? map(_get_cell_dof_ids_inner_space, local_views(Uh_new)) : nothing
274-
caches = get_redistribute_cell_dofs_cache(cell_dof_values_old,cell_dof_ids_new,model_new,glue;reverse=reverse)
275-
return caches
270+
cell_dof_values_old = i_am_in(old_parts) ? map(get_cell_dof_values,local_views(uh_old)) : nothing
271+
cell_dof_ids_new = i_am_in(new_parts) ? map(_get_cell_dof_ids_inner_space,local_views(Uh_new)) : nothing
272+
cell_dof_values_new = redistribute_cell_dofs(cell_dof_values_old,cell_dof_ids_new,model_new,glue;reverse=reverse)
273+
274+
# Assemble the new FEFunction
275+
if i_am_in(new_parts)
276+
free_values, dirichlet_values = Gridap.FESpaces.gather_free_and_dirichlet_values(Uh_new,cell_dof_values_new)
277+
free_values = PVector(free_values,partition(Uh_new.gids))
278+
uh_new = FEFunction(Uh_new,free_values,dirichlet_values)
279+
return uh_new
280+
else
281+
return nothing
282+
end
283+
end
284+
285+
for T in [:DistributedSingleFieldFESpace,:DistributedMultiFieldFESpace]
286+
@eval begin
287+
_get_fe_type(::$T,::Nothing) = $T
288+
_get_fe_type(::Nothing,::$T) = $T
289+
_get_fe_type(::$T,::$T) = $T
290+
end
276291
end
277292

278293
function redistribute_free_values(fv_new::Union{PVector,Nothing},
279-
Uh_new::Union{DistributedSingleFieldFESpace,Nothing},
294+
Uh_new::Union{DistributedSingleFieldFESpace,DistributedMultiFieldFESpace,Nothing},
280295
fv_old::Union{PVector,Nothing},
281296
dv_old::Union{AbstractArray,Nothing},
282-
Uh_old::Union{DistributedSingleFieldFESpace,Nothing},
297+
Uh_old::Union{DistributedSingleFieldFESpace,DistributedMultiFieldFESpace,Nothing},
283298
model_new,
284299
glue::RedistributeGlue;
285300
reverse=false)
286-
287301
caches = get_redistribute_free_values_cache(fv_new,Uh_new,fv_old,dv_old,Uh_old,model_new,glue;reverse=reverse)
288302
return redistribute_free_values!(caches,fv_new,Uh_new,fv_old,dv_old,Uh_old,model_new,glue;reverse=reverse)
289303
end
290304

305+
function get_redistribute_free_values_cache(fv_new,Uh_new,
306+
fv_old,dv_old,Uh_old,
307+
model_new,glue::RedistributeGlue;
308+
reverse=false)
309+
T = _get_fe_type(Uh_new,Uh_old)
310+
get_redistribute_free_values_cache(T,fv_new,Uh_new,fv_old,dv_old,Uh_old,model_new,glue;reverse=reverse)
311+
end
312+
313+
function get_redistribute_free_values_cache(::Type{DistributedSingleFieldFESpace},
314+
fv_new,Uh_new,
315+
fv_old,dv_old,Uh_old,
316+
model_new,glue::RedistributeGlue;
317+
reverse=false)
318+
old_parts, new_parts = get_old_and_new_parts(glue,Val(reverse))
319+
cell_dof_values_old = i_am_in(old_parts) ? map(scatter_free_and_dirichlet_values,local_views(Uh_old),local_views(fv_old),dv_old) : nothing
320+
cell_dof_ids_new = i_am_in(new_parts) ? map(_get_cell_dof_ids_inner_space, local_views(Uh_new)) : nothing
321+
caches = get_redistribute_cell_dofs_cache(cell_dof_values_old,cell_dof_ids_new,model_new,glue;reverse=reverse)
322+
return caches
323+
end
324+
325+
function get_redistribute_free_values_cache(::Type{DistributedMultiFieldFESpace},
326+
fv_new,Uh_new,
327+
fv_old,dv_old,Uh_old,
328+
model_new,glue::RedistributeGlue;
329+
reverse=false)
330+
old_parts, new_parts = get_old_and_new_parts(glue,Val(reverse))
331+
332+
if i_am_in(old_parts)
333+
Uh_old_i = Uh_old.field_fe_space
334+
fv_old_i = map(i->restrict_to_field(Uh_old,fv_old,i),1:num_fields(Uh_old))
335+
dv_old_i = dv_old
336+
else
337+
nfields = num_fields(Uh_new) # The other is not Nothing
338+
Uh_old_i = [nothing for i = 1:nfields]
339+
fv_old_i = [nothing for i = 1:nfields]
340+
dv_old_i = [nothing for i = 1:nfields]
341+
end
342+
343+
if i_am_in(new_parts)
344+
Uh_new_i = Uh_new.field_fe_space
345+
fv_new_i = map(i->restrict_to_field(Uh_new,fv_new,i),1:num_fields(Uh_new))
346+
else
347+
nfields = num_fields(Uh_old) # The other is not Nothing
348+
Uh_new_i = [nothing for i = 1:nfields]
349+
fv_new_i = [nothing for i = 1:nfields]
350+
end
351+
352+
caches = map(Uh_new_i,Uh_old_i,fv_new_i,fv_old_i,dv_old_i) do Uh_new_i,Uh_old_i,fv_new_i,fv_old_i,dv_old_i
353+
get_redistribute_free_values_cache(DistributedSingleFieldFESpace,fv_new_i,Uh_new_i,fv_old_i,dv_old_i,Uh_old_i,model_new,glue;reverse=reverse)
354+
end
355+
356+
return caches
357+
end
358+
291359
function redistribute_free_values!(caches,
360+
fv_new,Uh_new,
361+
fv_old,dv_old,Uh_old,
362+
model_new,
363+
glue::RedistributeGlue;
364+
reverse=false)
365+
T = _get_fe_type(Uh_new,Uh_old)
366+
redistribute_free_values!(T,caches,fv_new,Uh_new,fv_old,dv_old,Uh_old,model_new,glue;reverse=reverse)
367+
end
368+
369+
function redistribute_free_values!(::Type{DistributedSingleFieldFESpace},
370+
caches,
292371
fv_new::Union{PVector,Nothing},
293372
Uh_new::Union{DistributedSingleFieldFESpace,Nothing},
294373
fv_old::Union{PVector,Nothing},
@@ -310,24 +389,40 @@ function redistribute_free_values!(caches,
310389
return fv_new
311390
end
312391

313-
function redistribute_fe_function(uh_old::Union{DistributedSingleFieldFEFunction,Nothing},
314-
Uh_new::Union{DistributedSingleFieldFESpace,Nothing},
315-
model_new,
316-
glue::RedistributeGlue;
317-
reverse=false)
392+
function redistribute_free_values!(::Type{DistributedMultiFieldFESpace},
393+
caches,
394+
fv_new::Union{PVector,Nothing},
395+
Uh_new::Union{DistributedMultiFieldFESpace,Nothing},
396+
fv_old::Union{PVector,Nothing},
397+
dv_old::Union{AbstractArray,Nothing},
398+
Uh_old::Union{DistributedMultiFieldFESpace,Nothing},
399+
model_new,
400+
glue::RedistributeGlue;
401+
reverse=false)
318402

319403
old_parts, new_parts = get_old_and_new_parts(glue,Val(reverse))
320-
cell_dof_values_old = i_am_in(old_parts) ? map(get_cell_dof_values,local_views(uh_old)) : nothing
321-
cell_dof_ids_new = i_am_in(new_parts) ? map(_get_cell_dof_ids_inner_space,local_views(Uh_new)) : nothing
322-
cell_dof_values_new = redistribute_cell_dofs(cell_dof_values_old,cell_dof_ids_new,model_new,glue;reverse=reverse)
323404

324-
# Assemble the new FEFunction
405+
if i_am_in(old_parts)
406+
Uh_old_i = Uh_old.field_fe_space
407+
fv_old_i = map(i->restrict_to_field(Uh_old,fv_old,i),1:num_fields(Uh_old))
408+
dv_old_i = dv_old
409+
else
410+
nfields = num_fields(Uh_new) # The other is not Nothing
411+
Uh_old_i = [nothing for i = 1:nfields]
412+
fv_old_i = [nothing for i = 1:nfields]
413+
dv_old_i = [nothing for i = 1:nfields]
414+
end
415+
325416
if i_am_in(new_parts)
326-
free_values, dirichlet_values = Gridap.FESpaces.gather_free_and_dirichlet_values(Uh_new,cell_dof_values_new)
327-
free_values = PVector(free_values,partition(Uh_new.gids))
328-
uh_new = FEFunction(Uh_new,free_values,dirichlet_values)
329-
return uh_new
417+
Uh_new_i = Uh_new.field_fe_space
418+
fv_new_i = map(i->restrict_to_field(Uh_new,fv_new,i),1:num_fields(Uh_new))
330419
else
331-
return nothing
420+
nfields = num_fields(Uh_old) # The other is not Nothing
421+
Uh_new_i = [nothing for i = 1:nfields]
422+
fv_new_i = [nothing for i = 1:nfields]
423+
end
424+
425+
map(Uh_new_i,Uh_old_i,fv_new_i,fv_old_i,dv_old_i,caches) do Uh_new_i,Uh_old_i,fv_new_i,fv_old_i,dv_old_i,caches
426+
redistribute_free_values!(DistributedSingleFieldFESpace,caches,fv_new_i,Uh_new_i,fv_old_i,dv_old_i,Uh_old_i,model_new,glue;reverse=reverse)
332427
end
333428
end

‎src/MultiField.jl

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ function MultiField.restrict_to_field(
7171
PVector(values,partition(gids))
7272
end
7373

74+
function FESpaces.zero_dirichlet_values(f::DistributedMultiFieldFESpace)
75+
map(zero_dirichlet_values,f.field_fe_space)
76+
end
77+
7478
function FESpaces.FEFunction(
7579
f::DistributedMultiFieldFESpace,x::AbstractVector,isconsistent=false)
7680
free_values = change_ghost(x,f.gids;is_consistent=isconsistent,make_consistent=true)

0 commit comments

Comments
 (0)
Please sign in to comment.