Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vpuri3 committed May 12, 2023
1 parent 61bcde0 commit e77e83f
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test/test_jaches_products.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using SparseDiffTools, ForwardDiff, FiniteDiff, Zygote, IterativeSolvers
using LinearAlgebra, Test
using SparseDiffTools: get_tag, DeivVecTag

using Random
Random.seed!(123)
N = 300

struct MyTag end

N = 300
x = rand(N)
v = rand(N)

Expand Down Expand Up @@ -104,6 +107,10 @@ _dy = copy(dy);
update_coefficients!(f, v, 5.0, 6.0)
@test L(dy, v, 5.0, 6.0) auto_jacvec(f, v, v)

# GMRES test
out = similar(v)
@test_nowarn gmres!(out, L, v)

L = JacVec(f, copy(x), 1.0, 1.0; autodiff = AutoFiniteDiff())
update_coefficients!(f, x, 1.0, 1.0)
@test L * x num_jacvec(f, x, x)
Expand All @@ -121,9 +128,16 @@ _dy = copy(dy);
update_coefficients!(f, v, 5.0, 6.0)
@test L(dy, v, 5.0, 6.0)num_jacvec(f, v, v) rtol=1e-6

# GMRES test
out = similar(v)
@test_nowarn gmres!(out, L, v)

# Tag test
L = JacVec(f, copy(x), 1.0, 1.0)
@test get_tag(L.op.cache[1]) === ForwardDiff.Tag{DeivVecTag, eltype(x)}
L = JacVec(f, copy(x), 1.0, 1.0; tag = MyTag())
@test get_tag(L.op.cache[1]) === ForwardDiff.Tag{MyTag, eltype(x)}

@info "HesVec"

L = HesVec(g, copy(x), 1.0, 1.0, autodiff = AutoFiniteDiff())
Expand Down Expand Up @@ -159,6 +173,7 @@ _dy = copy(dy);
update_coefficients!(g, v, 5.0, 6.0)
@test L(dy, v, 5.0, 6.0) numauto_hesvec(g, v, v)

# GMRES test
out = similar(v)
gmres!(out, L, v)

Expand All @@ -179,9 +194,16 @@ _dy = copy(dy);
update_coefficients!(g, v, 5.0, 6.0)
@test L(dy, v, 5.0, 6.0) autoback_hesvec(g, v, v)

# GMRES test
out = similar(v)
gmres!(out, L, v)

# Tag test
L = HesVec(g, copy(x), 1.0, 1.0; autodiff = AutoZygote())
@test get_tag(L.op.cache[1]) === ForwardDiff.Tag{DeivVecTag, eltype(x)}
L = HesVec(g, copy(x), 1.0, 1.0; autodiff = AutoZygote(), tag = MyTag())
@test get_tag(L.op.cache[1]) === ForwardDiff.Tag{MyTag, eltype(x)}

@info "HesVecGrad"

L = HesVecGrad(h, copy(x), 1.0, 1.0; autodiff = AutoFiniteDiff())
Expand All @@ -203,6 +225,10 @@ _dy = copy(dy);
update_coefficients!(g, v, 5.0, 6.0)
@test L(dy, v, 5.0, 6.0)num_hesvec(g, v, v) rtol=1e-2

# GMRES test
out = similar(v)
gmres!(out, L, v)

L = HesVecGrad(h, copy(x), 1.0, 1.0)
update_coefficients!(g, x, 1.0, 1.0)
update_coefficients!(h, x, 1.0, 1.0)
Expand All @@ -223,6 +249,7 @@ update_coefficients!(g, v, 5.0, 6.0)
update_coefficients!(h, v, 5.0, 6.0)
@test L(dy, v, 5.0, 6.0) numauto_hesvec(g, v, v)

# GMRES test
out = similar(v)
gmres!(out, L, v)

Expand All @@ -231,4 +258,10 @@ gmres!(out, L, v)
@test x x0
@test v v0

# Tag test
L = HesVecGrad(g, copy(x), 1.0, 1.0)
@test get_tag(L.op.cache[1]) === ForwardDiff.Tag{DeivVecTag, eltype(x)}
L = HesVecGrad(g, copy(x), 1.0, 1.0; tag = MyTag())
@test get_tag(L.op.cache[1]) === ForwardDiff.Tag{MyTag, eltype(x)}

#

0 comments on commit e77e83f

Please sign in to comment.