Skip to content

Commit 1932b51

Browse files
committed
Add MeanValueRestriction
1 parent 5b86cbe commit 1932b51

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

examples/Example252_NSEPlanarLatticeFlow.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ function exact_error!(result, u, qpinfo)
8383
return result .= result .^ 2
8484
end
8585

86-
function main(; μ = 0.001, nrefs = 5, reconstruct = true, Plotter = nothing, kwargs...)
86+
function main(;
87+
μ = 0.001,
88+
nrefs = 5,
89+
reconstruct = true,
90+
Plotter = nothing,
91+
use_LM_restrictions = true,
92+
kwargs...
93+
)
8794

8895
## problem description
8996
PD = ProblemDescription()
@@ -96,6 +103,11 @@ function main(; μ = 0.001, nrefs = 5, reconstruct = true, Plotter = nothing, kw
96103
assign_operator!(PD, NonlinearOperator(kernel_nonlinear!, [id_u, grad(u), id(p)]; params = [μ], kwargs...))
97104
assign_operator!(PD, LinearOperator(f!(μ), [id_u]; kwargs...))
98105
assign_operator!(PD, InterpolateBoundaryData(u, u!; regions = 1:4))
106+
if use_LM_restrictions
107+
assign_restriction!(PD, MeanValueRestriction(p; value = 0))
108+
else
109+
assign_operator!(PD, FixDofs(p; dofs = [1]))
110+
end
99111

100112
## grid
101113
xgrid = uniform_refine(grid_unitsquare(Triangle2D), nrefs)
@@ -106,10 +118,12 @@ function main(; μ = 0.001, nrefs = 5, reconstruct = true, Plotter = nothing, kw
106118
## solve
107119
sol = solve(PD, FES; kwargs...)
108120

109-
## move integral mean of pressure
110-
pintegrate = ItemIntegrator([id(p)])
111-
pmean = sum(evaluate(pintegrate, sol)) / sum(xgrid[CellVolumes])
112-
view(sol[p]) .-= pmean
121+
if !use_LM_restrictions
122+
## move integral mean of pressure
123+
pintegrate = ItemIntegrator([id(p)])
124+
pmean = sum(evaluate(pintegrate, sol)) / sum(xgrid[CellVolumes])
125+
view(sol[p]) .-= pmean
126+
end
113127

114128
## error calculation
115129
ErrorIntegratorExact = ItemIntegrator(exact_error!, [id(u), id(p)]; quadorder = 4, params = [μ], kwargs...)
@@ -127,7 +141,9 @@ end
127141

128142
generateplots = ExtendableFEM.default_generateplots(Example252_NSEPlanarLatticeFlow, "example252.png") #hide
129143
function runtests() #hide
130-
L2errorU, plt = main(; nrefs = 4) #hide
144+
L2errorU, _ = main(; use_LM_restrictions = false, nrefs = 4) #hide
145+
@test L2errorU 0.010616923333947861 #hide
146+
L2errorU, _ = main(; use_LM_restrictions = true, nrefs = 4) #hide
131147
@test L2errorU 0.010616923333947861 #hide
132148
return nothing #hide
133149
end #hide

src/ExtendableFEM.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ include("common_operators/reduction_operator.jl")
128128
include("restrictions.jl")
129129
include("common_restrictions/coupled_dofs_restriction.jl")
130130
export CoupledDofsRestriction
131+
include("common_restrictions/mean_value_restriction.jl")
132+
export MeanValueRestriction
131133

132134
include("problemdescription.jl")
133135
export ProblemDescription

src/common_restrictions/coupled_dofs_restriction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function CoupledDofsRestriction(matrix::AbstractMatrix)
2020
end
2121

2222

23-
function assemble!(R::CoupledDofsRestriction, SC; kwargs...)
23+
function assemble!(R::CoupledDofsRestriction, sol, SC; kwargs...)
2424

2525
# extract all col indices
2626
_, J, _ = findnz(R.coupling_matrix)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
struct MeanValueRestriction{T, Tv} <: AbstractRestriction
2+
unknown::Unknown
3+
value::T
4+
linear_operator
5+
parameters::Dict{Symbol, Any}
6+
end
7+
8+
9+
"""
10+
$(TYPEDSIGNATURES)
11+
12+
Restrict the mean value of a scalar unknown to a certain value.
13+
14+
An additional operator can be applied to the unknown before the mean value is restricted.
15+
"""
16+
function MeanValueRestriction(u::Unknown; value::T = 0, operator = Identity, Tv = Float64) where {T}
17+
linear_operator = LinearOperator([(u, operator)]; store = true, Tv)
18+
return MeanValueRestriction{T, Tv}(u, value, linear_operator, Dict{Symbol, Any}(:name => "MeanValueRestriction"))
19+
end
20+
21+
22+
function assemble!(R::MeanValueRestriction{T, Tv}, sol, SC; kwargs...) where {T, Tv}
23+
24+
n = length(SC.b.entries)
25+
26+
# type and shape of the rhs
27+
b = deepcopy(SC.b)
28+
fill!(b.entries, 0)
29+
30+
assemble!(nothing, b, sol, R.linear_operator, SC; assemble_rhs = true, kwargs...)
31+
32+
R.parameters[:matrix] = reshape(b.entries, n, 1)
33+
R.parameters[:rhs] = Tv[R.value]
34+
35+
return nothing
36+
end

src/solvers.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function solve_linear_system!(A, b, sol, soltemp, residual, linsolve, unknowns,
227227
## assemble restrctions
228228
if !SC.parameters[:initialized]
229229
for restriction in PD.restrictions
230-
@timeit timer "$(restriction.parameters[:name])" assemble!(restriction, SC; kwargs...)
230+
@timeit timer "$(restriction.parameters[:name])" assemble!(restriction, sol, SC; kwargs...)
231231
end
232232
end
233233
end
@@ -287,9 +287,9 @@ function solve_linear_system!(A, b, sol, soltemp, residual, linsolve, unknowns,
287287
end
288288

289289
b_block = BlockVector(zeros(Tv, total_size), block_sizes)
290-
b_block[Block(1)] = b_unrestricted
291290

292-
u_unrestricted = linsolve.u
291+
b_block[Block(1)] = b_unrestricted
292+
u_unrestricted = @views linsolve.u[1:block_sizes[1]]
293293
u_block = BlockVector(zeros(Tv, total_size), block_sizes)
294294
u_block[Block(1)] = u_unrestricted
295295

@@ -333,7 +333,8 @@ function solve_linear_system!(A, b, sol, soltemp, residual, linsolve, unknowns,
333333
# Solve linear system
334334
push!(stats[:matrix_nnz], nnz(linsolve.A))
335335
@timeit timer "solve! call" begin
336-
blocked_Δx = LinearSolve.solve!(linsolve)
336+
blocked_result = LinearSolve.solve!(linsolve)
337+
blocked_Δx = blocked_result.u
337338
end
338339

339340
# extract the solution / dismiss the lagrange multipliers

0 commit comments

Comments
 (0)