Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 13be768

Browse files
Merge pull request #127 from JuliaDiffEq/myb/opnorm
Add `opnorm` to jacvec operators
2 parents c705255 + ebaa7f9 commit 13be768

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/jacvec_operators.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,23 @@ end
5656

5757
### Operator Implementation
5858

59-
mutable struct JacVecOperator{T,F,T1,T2,uType,P,tType} <: DiffEqBase.AbstractDiffEqLinearOperator{T}
59+
mutable struct JacVecOperator{T,F,T1,T2,uType,P,tType,O} <: DiffEqBase.AbstractDiffEqLinearOperator{T}
6060
f::F
6161
cache1::T1
6262
cache2::T2
6363
u::uType
6464
p::P
6565
t::tType
6666
autodiff::Bool
67+
ishermitian::Bool
68+
opnorm::O
6769

68-
function JacVecOperator{T}(f,p=nothing,t::Union{Nothing,Number}=nothing;autodiff=true) where T
70+
function JacVecOperator{T}(f,p=nothing,t::Union{Nothing,Number}=nothing;autodiff=true,ishermitian=false,opnorm=true) where T
6971
p===nothing ? P = Any : P = typeof(p)
7072
t===nothing ? tType = Any : tType = typeof(t)
71-
new{T,typeof(f),Nothing,Nothing,Any,P,tType}(f,nothing,nothing,nothing,nothing,nothing,autodiff)
73+
new{T,typeof(f),Nothing,Nothing,Any,P,tType,typeof(opnorm)}(f,nothing,nothing,nothing,nothing,nothing,autodiff,ishermitian)
7274
end
73-
function JacVecOperator{T}(f,u::AbstractArray,p=nothing,t::Union{Nothing,Number}=nothing;autodiff=true) where T
75+
function JacVecOperator{T}(f,u::AbstractArray,p=nothing,t::Union{Nothing,Number}=nothing;autodiff=true,ishermitian=false,opnorm=true) where T
7476
if autodiff
7577
cache1 = ForwardDiff.Dual{JacVecTag}.(u, u)
7678
cache2 = ForwardDiff.Dual{JacVecTag}.(u, u)
@@ -80,14 +82,16 @@ mutable struct JacVecOperator{T,F,T1,T2,uType,P,tType} <: DiffEqBase.AbstractDif
8082
end
8183
p===nothing ? P = Any : P = typeof(p)
8284
t===nothing ? tType = Any : tType = typeof(t)
83-
new{T,typeof(f),typeof(cache1),typeof(cache2),typeof(u),P,tType}(f,cache1,cache2,u,p,t,autodiff)
85+
new{T,typeof(f),typeof(cache1),typeof(cache2),typeof(u),P,tType,typeof(opnorm)}(f,cache1,cache2,u,p,t,autodiff,ishermitian,opnorm)
8486
end
8587
function JacVecOperator(f,u,args...;kwargs...)
8688
JacVecOperator{eltype(u)}(f,u,args...;kwargs...)
8789
end
88-
8990
end
9091

92+
LinearAlgebra.opnorm(L::JacVecOperator, p::Real=2) = L.opnorm
93+
LinearAlgebra.ishermitian(L::JacVecOperator) = L.ishermitian
94+
9195
Base.size(L::JacVecOperator) = (length(L.cache1),length(L.cache1))
9296
Base.size(L::JacVecOperator,i::Int) = length(L.cache1)
9397
function update_coefficients!(L::JacVecOperator,u,p,t)

src/matrixfree_operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mutable struct MatrixFreeOperator{F,N,S,O} <: AbstractMatrixFreeOperator{F}
66
opnorm::O
77
ishermitian::Bool
88
function MatrixFreeOperator(f::F, args::N;
9-
size=nothing, opnorm=nothing, ishermitian=false) where {F,N}
9+
size=nothing, opnorm=true, ishermitian=false) where {F,N}
1010
@assert (N <: Tuple && length(args) in (1,2)) "Arguments of a "*
1111
"MatrixFreeOperator must be a tuple with one or two elements"
1212
return new{F,N,typeof(size),typeof(opnorm)}(f, args, size, opnorm, ishermitian)

test/jacvec_operators.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ function lorenz(du,u,p,t)
5252
end
5353
u0 = [1.0;0.0;0.0]
5454
tspan = (0.0,100.0)
55-
ff = ODEFunction(lorenz,jac_prototype=JacVecOperator{Float64}(lorenz,u0))
56-
prob = ODEProblem(ff,u0,tspan)
57-
@test_broken sol = solve(prob,Rosenbrock23())
58-
@test_broken sol = solve(prob,Rosenbrock23(linsolve=LinSolveGMRES(tol=1e-10)))
59-
60-
ff = ODEFunction(lorenz,jac_prototype=JacVecOperator{Float64}(lorenz,u0,autodiff=false))
61-
prob = ODEProblem(ff,u0,tspan)
62-
@test_broken sol = solve(prob,Rosenbrock23())
63-
@test_broken sol = solve(prob,Rosenbrock23(linsolve=LinSolveGMRES(tol=1e-10)))
55+
ff1 = ODEFunction(lorenz,jac_prototype=JacVecOperator{Float64}(lorenz,u0))
56+
ff2 = ODEFunction(lorenz,jac_prototype=JacVecOperator{Float64}(lorenz,u0,autodiff=false))
57+
for ff in [ff1, ff2]
58+
prob = ODEProblem(ff,u0,tspan)
59+
@test solve(prob,TRBDF2()).retcode == :Success
60+
@test solve(prob,TRBDF2(linsolve=LinSolveGMRES(tol=1e-10))).retcode == :Success
61+
@test solve(prob,Exprb32()).retcode == :Success
62+
@test_broken sol = solve(prob,Rosenbrock23())
63+
@test_broken sol = solve(prob,Rosenbrock23(linsolve=LinSolveGMRES(tol=1e-10)))
64+
end

0 commit comments

Comments
 (0)