Skip to content

Commit

Permalink
fix preconditioner field
Browse files Browse the repository at this point in the history
  • Loading branch information
ranocha committed Mar 21, 2024
1 parent d53e26b commit 8b5dc54
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
28 changes: 18 additions & 10 deletions src/mprk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ function build_mprk_matrix(P, sigma, dt)
for I in CartesianIndices(P)
if !iszero(P[I])
dtP = dt*P[I]
dtP = dt*P[I]
M[I] = -dtP / sigma[I[2]]
d[I[2]] += dtP
end
end
for i in eachindex(d)
for i in eachindex(d)
M[i,i] = 1.0 + d[i]/sigma[i]
end
return M
Expand Down Expand Up @@ -105,13 +105,23 @@ The modified Patankar-Euler method requires the special structure of a
Applied Numerical Mathematics 47.1 (2003): 1-30.
[DOI: 10.1016/S0168-9274(03)00101-6](https://doi.org/10.1016/S0168-9274(03)00101-6)
"""
struct MPE{F, P} <: OrdinaryDiffEqAlgorithm
struct MPE{F} <: OrdinaryDiffEqAlgorithm
linsolve::F
precs::P
end

function MPE(; linsolve = nothing, precs = (I, I))
MPE(linsolve, precs)
function MPE(; linsolve = nothing)
MPE(linsolve)
end

# TODO: Think about adding preconditioners to the MPE algorithm
# This hack is currently required to make OrdinaryDiffEq.jl happy...
function Base.getproperty(mpe::MPE, f::Symbol)
# preconditioners
if f === :precs
return Returns((nothing, nothing))
else
return getfield(mpe, f)
end
end

#@cache
Expand Down Expand Up @@ -140,10 +150,8 @@ function alg_cache(alg::MPE, u, rate_prototype, ::Type{uEltypeNoUnits},
recursivefill!(weight, false)

linprob = LinearProblem(P, _vec(linsolve_tmp); u0 = _vec(tmp))
Pl, Pr = wrapprecs(alg.precs(P, nothing, u, p, t, nothing, nothing, nothing,
nothing)..., weight, tmp)
linsolve = init(linprob, alg.linsolve, alias_A = true, alias_b = true,
Pl = Pl, Pr = Pr, assumptions = LinearSolve.OperatorAssumptions(true))
assumptions = LinearSolve.OperatorAssumptions(true))

MPECache(u, uprev, tmp, zero(rate_prototype), zero(rate_prototype), P, zero(u),
linsolve_tmp, linsolve, weight)
Expand Down Expand Up @@ -189,7 +197,7 @@ function perform_step!(integrator, cache::MPEConstantCache, repeat_step = false)

# solve linear system
linprob = LinearProblem(M, uprev)
sol = solve(linprob, alg.linsolve, Pl = alg.precs[1], Pr = alg.precs[2],
sol = solve(linprob, alg.linsolve,
alias_A = false, alias_b = false,
assumptions = LinearSolve.OperatorAssumptions(true))
u = sol.u
Expand Down
7 changes: 4 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Aqua: Aqua

@testset "PositiveIntegrators.jl tests" begin
@testset "Aqua.jl" begin
# We do not test ambiguities since we get a lot of
# We do not test ambiguities since we get a lot of
# false positives from dependencies
Aqua.test_all(PositiveIntegrators;
ambiguities = false,)
Expand Down Expand Up @@ -169,8 +169,9 @@ using Aqua: Aqua
cmd = Base.julia_cmd()
examples_dir = abspath(joinpath(pkgdir(PositiveIntegrators), "examples"))
examples = ["01_example_proddest.jl",
"02_example_mpe.jl",
"03_example_mprk22.jl"]
"02_example_mpe.jl",
"03_example_mprk22.jl",
"04_example_problemlibrary.jl"]

@testset "Example $ex" for ex in examples
@info "Testing examples" ex
Expand Down

0 comments on commit 8b5dc54

Please sign in to comment.