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

Commit 6cd7300

Browse files
committed
Remove update_coefficients_for_test!, test L(v, u, p, t)
1 parent b5e2fe3 commit 6cd7300

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

test/test_jaches_products.jl

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using SparseDiffTools, ForwardDiff, FiniteDiff, Zygote, IterativeSolvers
22
using LinearAlgebra, Test
3-
import SciMLOperators: update_coefficients, update_coefficients!
43

54
using Random
65
Random.seed!(123)
@@ -85,22 +84,28 @@ update_coefficients!(f, x, 1.0, 1.0)
8584
@test L * v auto_jacvec(f, x, v)
8685
@test mul!(dy, L, v) auto_jacvec(f, x, v)
8786
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*auto_jacvec(f,x,v) + b*_dy
88-
update_coefficients_for_test!(L, v, 3.0, 4.0)
87+
update_coefficients!(L, v, 3.0, 4.0)
88+
update_coefficients!(f, v, 3.0, 4.0)
8989
@test mul!(dy, L, v) auto_jacvec(f, v, v)
9090
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*auto_jacvec(f,x,v) + b*_dy
91+
update_coefficients!(f, v, 5.0, 6.0)
92+
@test L(dy, v, 5.0, 6.0) auto_jacvec(f,x,v)
9193

9294
L = JacVec(f, x, 1.0, 1.0; autodiff = AutoFiniteDiff())
9395
update_coefficients!(f, x, 1.0, 1.0)
9496
@test L * x num_jacvec(f, x, x)
9597
@test L * v num_jacvec(f, x, v)
9698
@test mul!(dy, L, v)num_jacvec(f, x, v) rtol=1e-6
9799
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*num_jacvec(f,x,v) + b*_dy rtol=1e-6
98-
update_coefficients_for_test!(L, v, 3.0, 4.0)
100+
update_coefficients!(L, v, 3.0, 4.0)
101+
update_coefficients!(f, v, 3.0, 4.0)
99102
@test mul!(dy, L, v)num_jacvec(f, v, v) rtol=1e-6
100103
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*num_jacvec(f,x,v) + b*_dy rtol=1e-6
104+
update_coefficients!(f, v, 5.0, 6.0)
105+
@test L(dy, v, 5.0, 6.0) num_jacvec(f,x,v) rtol=1e-6
101106

102107
out = similar(v)
103-
gmres!(out, L, v)
108+
@test_nowarn gmres!(out, L, v)
104109

105110
@info "HesVec"
106111

@@ -113,9 +118,12 @@ num_hesvec(g, x, x)
113118
@test L * v num_hesvec(g, x, v) rtol=1e-2
114119
@test mul!(dy, L, v)num_hesvec(g, x, v) rtol=1e-2
115120
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*num_hesvec(g,x,v) + b*_dy rtol=1e-2
116-
update_coefficients_for_test!(L, v, 3.0, 4.0)
121+
update_coefficients!(L, v, 3.0, 4.0)
122+
update_coefficients!(g, v, 3.0, 4.0)
117123
@test mul!(dy, L, v)num_hesvec(g, v, v) rtol=1e-2
118124
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b) a*num_hesvec(g,x,v) + b*_dy rtol=1e-2
125+
update_coefficients!(g, v, 5.0, 6.0)
126+
@test L(dy, v, 5.0, 6.0) num_hesvec(g,x,v) rtol=1e-2
119127

120128
L = HesVec(g, x, 1.0, 1.0)
121129
update_coefficients!(g, x, 1.0, 1.0)
@@ -125,9 +133,12 @@ num_hesvec(g, x, x)
125133
@test L * v numauto_hesvec(g, x, v)
126134
@test mul!(dy, L, v)numauto_hesvec(g, x, v) rtol=1e-8
127135
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*numauto_hesvec(g,x,v)+b*_dy rtol=1e-8
128-
update_coefficients_for_test!(L, v, 3.0, 4.0)
136+
update_coefficients!(L, v, 3.0, 4.0)
137+
update_coefficients!(g, v, 3.0, 4.0)
129138
@test mul!(dy, L, v)numauto_hesvec(g, v, v) rtol=1e-8
130139
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*numauto_hesvec(g,x,v)+b*_dy rtol=1e-8
140+
update_coefficients!(g, v, 5.0, 6.0)
141+
@test L(dy, v, 5.0, 6.0) numauto_hesvec(g,x,v) rtol=1e-2
131142

132143
out = similar(v)
133144
gmres!(out, L, v)
@@ -141,9 +152,12 @@ update_coefficients!(g, x, 1.0, 1.0)
141152
@test L * v autoback_hesvec(g, x, v)
142153
@test mul!(dy, L, v)autoback_hesvec(g, x, v) rtol=1e-8
143154
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*autoback_hesvec(g,x,v)+b*_dy rtol=1e-8
144-
update_coefficients_for_test!(L, v, 3.0, 4.0)
155+
update_coefficients!(L, v, 3.0, 4.0)
156+
update_coefficients!(g, v, 3.0, 4.0)
145157
@test mul!(dy, L, v)autoback_hesvec(g, v, v) rtol=1e-8
146158
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*autoback_hesvec(g,x,v)+b*_dy rtol=1e-8
159+
update_coefficients!(g, v, 5.0, 6.0)
160+
@test L(dy, v, 5.0, 6.0) autoback_hesvec(g,x,v) rtol=1e-2
147161

148162
out = similar(v)
149163
gmres!(out, L, v)
@@ -159,20 +173,26 @@ update_coefficients!(g, x, 1.0, 1.0)
159173
@test L * v num_hesvec(g, x, v) rtol=1e-2
160174
@test mul!(dy, L, v)num_hesvec(g, x, v) rtol=1e-2
161175
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*num_hesvec(g,x,v)+b*_dy rtol=1e-2
162-
update_coefficients_for_test!(L, v, 3.0, 4.0)
163-
update_coefficients!(g, x, 3.0, 4.0)
176+
for op in (L, g, h) update_coefficients!(op, v, 3.0, 4.0) end
164177
@test mul!(dy, L, v)num_hesvec(g, v, v) rtol=1e-2
165178
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*num_hesvec(g,x,v)+b*_dy rtol=1e-2
179+
update_coefficients!(g, v, 5.0, 6.0)
180+
@test L(dy, v, 5.0, 6.0) num_hesvec(g,x,v) rtol=1e-2
166181

167182
L = HesVecGrad(h, x, 1.0, 1.0)
168-
update_coefficients!(h, x, 1.0, 1.0)
169183
update_coefficients!(g, x, 1.0, 1.0)
184+
update_coefficients!(h, x, 1.0, 1.0)
170185
@test L * x autonum_hesvec(g, x, x)
171186
@test L * v numauto_hesvec(g, x, v)
172187
@test mul!(dy, L, v)numauto_hesvec(g, x, v) rtol=1e-8
173188
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*numauto_hesvec(g,x,v)+b*_dy rtol=1e-8
174-
update_coefficients_for_test!(L, v, 3.0, 4.0)
175-
update_coefficients!(g, x, 3.0, 4.0)
189+
for op in (L, g, h) update_coefficients!(op, v, 3.0, 4.0) end
176190
@test mul!(dy, L, v)numauto_hesvec(g, v, v) rtol=1e-8
177191
dy=rand(N);_dy=copy(dy);@test mul!(dy,L,v,a,b)a*numauto_hesvec(g,x,v)+b*_dy rtol=1e-8
192+
update_coefficients!(g, v, 5.0, 6.0)
193+
update_coefficients!(h, v, 5.0, 6.0)
194+
@test L(dy, v, 5.0, 6.0) num_hesvec(g,x,v) rtol=1e-2
195+
196+
out = similar(v)
197+
gmres!(out, L, v)
178198
#

test/test_vecjac_products.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ f = WrapFunc(_f, 1.0, 1.0)
2020

2121
L = VecJac(f, x, 1.0, 1.0)
2222
update_coefficients!(L, v, 3.0, 4.0)
23+
update_coefficients!(f, v, 3.0, 4.0)
2324
actual_vjp = Zygote.jacobian(f, x)[1]' * v
2425
@test L * v actual_vjp
26+
update_coefficients!(f, v, 5.0, 6.0)
27+
actual_vjp2 = Zygote.jacobian(f, x)[1]' * v
28+
@test L(copy(v), v, 5.0, 6.0) actual_vjp2
29+
2530
L = VecJac(f, x, 1.0, 1.0; autodiff = AutoFiniteDiff())
2631
update_coefficients!(L, v, 3.0, 4.0)
32+
update_coefficients!(f, v, 3.0, 4.0)
2733
@test L * v actual_vjp
34+
update_coefficients!(f, v, 5.0, 6.0)
35+
@test L(copy(v), v, 5.0, 6.0) actual_vjp2
2836
#

test/update_coeffs_testutils.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Utilities for testing update coefficient behaviour with state-dependent (i.e. dependent on u/p/t) functions
1+
import SciMLOperators: update_coefficients, update_coefficients!
2+
3+
# Wrapper function for testing update coefficient behaviour with state-dependent (i.e. dependent on u/p/t) functions
24

35
mutable struct WrapFunc{F,P,T}
46
func::F
@@ -16,11 +18,4 @@ update_coefficients(w::WrapFunc, u, p, t) = WrapFunc(w.func, p, t)
1618
function update_coefficients!(w::WrapFunc, u, p, t)
1719
w.p = p
1820
w.t = t
19-
end
20-
21-
# Helper function for testing correct update coefficients behaviour of operators
22-
function update_coefficients_for_test!(L, u, p, t)
23-
update_coefficients!(L, u, p, t)
24-
# Force function hiding inside L to update. Should be a null-op if previous line works correctly
25-
update_coefficients!(L.op.f, u, p, t)
2621
end

0 commit comments

Comments
 (0)