Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit c71b336

Browse files
committed
Make sure to reset state of stateful functions when reconstructing operator
1 parent 7e4ac73 commit c71b336

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

test/test_jaches_products.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ end
2525
# Define state-dependent functions for operator tests
2626

2727
include("update_coeffs_testutils.jl")
28-
f = WrapFunc(_f, ones(N), 1.0, 1.0)
29-
g = WrapFunc(_g, ones(N), 1.0, 1.0)
30-
h = WrapFunc(_h, ones(N), 1.0, 1.0)
28+
f = WrapFunc(_f, 1.0, 1.0)
29+
g = WrapFunc(_g, 1.0, 1.0)
30+
h = WrapFunc(_h, 1.0, 1.0)
3131

3232
###
3333

@@ -80,6 +80,7 @@ cache4 = ForwardDiff.Dual{typeof(ForwardDiff.Tag(Nothing, eltype(x))), eltype(x)
8080
@info "JacVec"
8181

8282
L = JacVec(f, x, 1.0, 1.0)
83+
update_coefficients!(f, x, 1.0, 1.0)
8384
@test L * x auto_jacvec(f, x, x)
8485
@test L * v auto_jacvec(f, x, v)
8586
@test mul!(dy, L, v) auto_jacvec(f, x, v)
@@ -89,6 +90,7 @@ update_coefficients_for_test!(L, v, 3.0, 4.0)
8990
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*auto_jacvec(f,x,v) + b*_dy
9091

9192
L = JacVec(f, x, 1.0, 1.0; autodiff = AutoFiniteDiff())
93+
update_coefficients!(f, x, 1.0, 1.0)
9294
@test L * x num_jacvec(f, x, x)
9395
@test L * v num_jacvec(f, x, v)
9496
@test mul!(dy, L, v)num_jacvec(f, x, v) rtol=1e-6
@@ -105,15 +107,20 @@ gmres!(out, L, v)
105107
x = rand(N)
106108
v = rand(N)
107109
L = HesVec(g, x, 1.0, 1.0, autodiff = AutoFiniteDiff())
108-
@test L * x num_hesvec(g, x, x)
109-
@test L * v num_hesvec(g, x, v)
110+
update_coefficients!(g, x, 1.0, 1.0)
111+
@test L * x num_hesvec(g, x, x) rtol=1e-2
112+
num_hesvec(g, x, x)
113+
@test L * v num_hesvec(g, x, v) rtol=1e-2
110114
@test mul!(dy, L, v)num_hesvec(g, x, v) rtol=1e-2
111115
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*num_hesvec(g,x,v) + b*_dy rtol=1e-2
112116
update_coefficients_for_test!(L, v, 3.0, 4.0)
113117
@test mul!(dy, L, v)num_hesvec(g, v, v) rtol=1e-2
114118
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*num_hesvec(g,x,v) + b*_dy rtol=1e-2
115119

116120
L = HesVec(g, x, 1.0, 1.0)
121+
update_coefficients!(g, x, 1.0, 1.0)
122+
numauto_hesvec(g, x, x)
123+
num_hesvec(g, x, x)
117124
@test L * x numauto_hesvec(g, x, x)
118125
@test L * v numauto_hesvec(g, x, v)
119126
@test mul!(dy, L, v)numauto_hesvec(g, x, v) rtol=1e-8
@@ -129,6 +136,7 @@ x = rand(N)
129136
v = rand(N)
130137

131138
L = HesVec(g, x, 1.0, 1.0; autodiff = AutoZygote())
139+
update_coefficients!(g, x, 1.0, 1.0)
132140
@test L * x autoback_hesvec(g, x, x)
133141
@test L * v autoback_hesvec(g, x, v)
134142
@test mul!(dy, L, v)autoback_hesvec(g, x, v) rtol=1e-8
@@ -145,23 +153,26 @@ gmres!(out, L, v)
145153
x = rand(N)
146154
v = rand(N)
147155
L = HesVecGrad(h, x, 1.0, 1.0; autodiff = AutoFiniteDiff())
156+
update_coefficients!(h, x, 1.0, 1.0)
157+
update_coefficients!(g, x, 1.0, 1.0)
148158
@test L * x num_hesvec(g, x, x) rtol=1e-2
149159
@test L * v num_hesvec(g, x, v) rtol=1e-2
150160
@test mul!(dy, L, v)num_hesvec(g, x, v) rtol=1e-2
151161
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*num_hesvec(g,x,v)+b*_dy rtol=1e-2
152162
update_coefficients_for_test!(L, v, 3.0, 4.0)
163+
update_coefficients!(g, x, 3.0, 4.0)
153164
@test mul!(dy, L, v)num_hesvec(g, v, v) rtol=1e-2
154165
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*num_hesvec(g,x,v)+b*_dy rtol=1e-2
155166

156167
L = HesVecGrad(h, x, 1.0, 1.0)
168+
update_coefficients!(h, x, 1.0, 1.0)
169+
update_coefficients!(g, x, 1.0, 1.0)
157170
@test L * x autonum_hesvec(g, x, x)
158171
@test L * v numauto_hesvec(g, x, v)
159172
@test mul!(dy, L, v)numauto_hesvec(g, x, v) rtol=1e-8
160173
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*numauto_hesvec(g,x,v)+b*_dy rtol=1e-8
161174
update_coefficients_for_test!(L, v, 3.0, 4.0)
175+
update_coefficients!(g, x, 3.0, 4.0)
162176
@test mul!(dy, L, v)numauto_hesvec(g, v, v) rtol=1e-8
163177
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*numauto_hesvec(g,x,v)+b*_dy rtol=1e-8
164-
165-
out = similar(v)
166-
gmres!(out, L, v)
167178
#

test/update_coeffs_testutils.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Utilities for testing update coefficient behaviour with state-dependent (i.e. dependent on u/p/t) functions
22

3-
mutable struct WrapFunc{F,U,P,T}
3+
mutable struct WrapFunc{F,P,T}
44
func::F
5-
u::U
65
p::P
76
t::T
87
end
@@ -13,9 +12,8 @@ function (w::WrapFunc)(v, u)
1312
lmul!(w.p * w.t, v)
1413
end
1514

16-
update_coefficients(w::WrapFunc, u, p, t) = WrapFunc(w.func, u, p, t)
15+
update_coefficients(w::WrapFunc, u, p, t) = WrapFunc(w.func, p, t)
1716
function update_coefficients!(w::WrapFunc, u, p, t)
18-
w.u = u
1917
w.p = p
2018
w.t = t
2119
end

0 commit comments

Comments
 (0)