-
Notifications
You must be signed in to change notification settings - Fork 15
/
ZeroMeanDistributedFESpaces.jl
171 lines (151 loc) · 6.01 KB
/
ZeroMeanDistributedFESpaces.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
struct ZeroMeanDistributedFESpace{V} <: DistributedFESpace{V}
vector_type :: Type{V}
spaces :: DistributedData{<:FESpace}
gids :: DistributedIndexSet
vol_i :: DistributedData{Vector{Float64}}
vol :: DistributedData{Float64}
end
# Constructors
function Gridap.TrialFESpace(V::ZeroMeanDistributedFESpace,args...)
spaces = DistributedData(V.spaces) do part, space
TrialFESpace(space,args...)
end
ZeroMeanDistributedFESpace(V.vector_type,spaces,V.gids,V.vol_i,V.vol)
end
# U = TrialFESpace(f.space)
# ZeroMeanFESpace(U,f.vol_i,f.vol,f.constraint_style)
function Gridap.FESpaces.FEFunction(dV::ZeroMeanDistributedFESpace,x)
dfree_vals = x[dV.gids]
# IMPORTANT NOTE: we need to call collect below in order to duplicate the
# local portion of dfree_vals. When dfree_vals is of
# type MPIPETScDistributedVector, the Julia's GC can destroy
# the vector on which the entries of dfree_vals are ultimately
# stored when it goes out of scope.
funs = DistributedData(dV.spaces,dfree_vals) do part, V, free_vals
FEFunction(V,collect(free_vals))
end
dfuns=_generate_zero_mean_funs(dV,funs)
DistributedFEFunction(dfuns,x,dV)
end
function Gridap.FESpaces.EvaluationFunction(dV::ZeroMeanDistributedFESpace,x)
dfree_vals = x[dV.gids]
# IMPORTANT NOTE: we need to call collect below in order to duplicate the
# local portion of dfree_vals. When dfree_vals is of
# type MPIPETScDistributedVector, the Julia's GC can destroy
# the vector on which the entries of dfree_vals are ultimately
# stored when it goes out of scope.
funs = DistributedData(dV.spaces,dfree_vals) do part, V, free_vals
Gridap.FESpaces.EvaluationFunction(V,collect(free_vals))
end
dfuns=_generate_zero_mean_funs(dV,funs)
DistributedFEFunction(dfuns,x,dV)
end
function _generate_zero_mean_funs(dV::ZeroMeanDistributedFESpace, funs)
dpartial_sums_fixed_val=
DistributedData(dV.spaces, funs, dV.vol_i, dV.vol) do part, V, fun, vol_i, vol
if (get_val_parameter(V.remove_dof))
fv=get_free_values(fun)
dv=get_dirichlet_values(fun)
c=Gridap.FESpaces._compute_new_fixedval(fv,
dv,
vol_i,
vol,
V.dof_to_remove)
else
fv=get_free_values(fun)
c=-dot(fv,vol_i)/vol
end
c
end
partial_sums_fixed_val=gather(dpartial_sums_fixed_val)
fixed_val=sum(partial_sums_fixed_val)
comm=get_comm(dV)
dfixed_val=scatter_value(comm,fixed_val)
dfuns = DistributedData(dV.spaces, funs, dfixed_val) do part, V, fun, fixed_val
free_values=get_free_values(fun)
fv=apply(+,free_values, Fill(fixed_val,length(free_values)))
if (get_val_parameter(V.remove_dof))
dirichlet_values=get_dirichlet_values(fun)
dv = dirichlet_values .+ fixed_val
return FEFunction(V,fv,dv)
else
return FEFunction(V,fv)
end
end
end
function ZeroMeanDistributedFESpace(::Type{V};
model::DistributedDiscreteModel,
kwargs...) where V
function init_local_spaces(part,model)
lspace = FESpace(;model=model,kwargs...)
end
comm = get_comm(model)
spaces = DistributedData(init_local_spaces,comm,model.models)
dof_lid_to_remove = _compute_dof_lid_to_remove(model,spaces)
function init_local_spaces_with_dof_removed(part,lspace,dof_lid_to_remove)
Gridap.FESpaces.FESpaceWithDofPotentiallyRemoved(
lspace, dof_lid_to_remove != -1, dof_lid_to_remove)
end
spaces_dof_removed = DistributedData(init_local_spaces_with_dof_removed,
comm,
spaces,
dof_lid_to_remove)
order=Gridap.FESpaces._get_kwarg(:order,kwargs)
dvol_i, dvol = _setup_vols(model,spaces,order)
gids=_compute_distributed_index_set(model, spaces_dof_removed)
ZeroMeanDistributedFESpace(V,spaces_dof_removed,gids,dvol_i,dvol)
end
function _setup_vols(model,spaces,order)
comm = get_comm(model)
dvol_i_and_vol = DistributedData(model,spaces) do part, (model,gids), lspace
trian = Triangulation(model)
owned_trian = remove_ghost_cells(trian, part, gids)
owned_quad = CellQuadrature(owned_trian, order)
Gridap.FESpaces._setup_vols(lspace,owned_trian,owned_quad)
end
dvol_i=DistributedData(dvol_i_and_vol) do part, vol_i_and_vol
vol_i_and_vol[1]
end
dvol_partial_sums=DistributedData(dvol_i_and_vol) do part, vol_i_and_vol
vol_i_and_vol[2]
end
partial_sums=gather(dvol_partial_sums)
if (i_am_master(comm))
vol=sum(partial_sums)
end
dvol=scatter_value(comm,vol)
(dvol_i,dvol)
end
function _compute_dof_lid_to_remove(model,spaces)
dof_lids_candidates=DistributedData(model.gids,spaces) do part, cell_gids, lspace
n_free_dofs = num_free_dofs(lspace)
lid_to_n_local_minus_ghost=zeros(Int32,n_free_dofs)
cell_dofs=get_cell_dofs(lspace)
cell_dofs_cache = array_cache(cell_dofs)
for cell in 1:length(cell_dofs)
current_cell_dofs = getindex!(cell_dofs_cache,cell_dofs,cell)
is_local = (cell_gids.lid_to_owner[cell] == part)
for lid in current_cell_dofs
if (lid>0)
if (is_local)
lid_to_n_local_minus_ghost[lid] += 1
else
lid_to_n_local_minus_ghost[lid] -= 1
end
end
end
end
min_lid_only_local_cells=
findfirst(x->(x>0),lid_to_n_local_minus_ghost)
min_lid_only_local_cells==nothing ? -1 : min_lid_only_local_cells
end
comm = get_comm(model)
part_dof_lids_candidates = gather(dof_lids_candidates)
if i_am_master(comm)
first_proc = findfirst(x->(x!=-1), part_dof_lids_candidates)
for proc=first_proc+1:length(part_dof_lids_candidates)
part_dof_lids_candidates[proc]=-1
end
end
dof_lid_to_remove = scatter(comm,part_dof_lids_candidates)
end