1
- mutable struct JacobianWrapper{fType, pType}
1
+ struct JacobianWrapper{fType, pType}
2
2
f:: fType
3
3
p:: pType
4
4
end
5
5
6
6
(uf:: JacobianWrapper )(u) = uf. f (u, uf. p)
7
7
(uf:: JacobianWrapper )(res, u) = uf. f (res, u, uf. p)
8
8
9
- struct ImmutableJacobianWrapper{fType, pType}
10
- f:: fType
11
- p:: pType
12
- end
13
-
14
- (uf:: ImmutableJacobianWrapper )(u) = uf. f (u, uf. p)
9
+ struct NonlinearSolveTag end
15
10
16
11
function sparsity_colorvec (f, x)
17
12
sparsity = f. sparsity
@@ -21,33 +16,35 @@ function sparsity_colorvec(f, x)
21
16
end
22
17
23
18
function jacobian_finitediff_forward! (J, f, x, jac_config, forwardcache, cache)
24
- (FiniteDiff. finite_difference_jacobian! (J, f, x, jac_config, forwardcache,
25
- dir = diffdir (cache));
19
+ (FiniteDiff. finite_difference_jacobian! (J, f, x, jac_config, forwardcache);
26
20
maximum (jac_config. colorvec))
27
21
end
28
22
function jacobian_finitediff! (J, f, x, jac_config, cache)
29
- (FiniteDiff. finite_difference_jacobian! (J, f, x, jac_config,
30
- dir = diffdir (cache));
23
+ (FiniteDiff. finite_difference_jacobian! (J, f, x, jac_config);
31
24
2 * maximum (jac_config. colorvec))
32
25
end
33
26
34
- function jacobian! (J:: AbstractMatrix{<:Number} , f, x:: AbstractArray{<:Number} ,
35
- fx:: AbstractArray{<:Number} , cache,
36
- jac_config)
37
- alg = unwrap_alg (cache, true )
27
+ function jacobian! (J:: AbstractMatrix{<:Number} , cache)
28
+ f = cache. f
29
+ uf = cache. uf
30
+ x = cache. u
31
+ fx = cache. fu
32
+ jac_config = cache. jac_config
33
+ alg = cache. alg
34
+
38
35
if alg_autodiff (alg)
39
- forwarddiff_color_jacobian! (J, f , x, jac_config)
36
+ forwarddiff_color_jacobian! (J, uf , x, jac_config)
40
37
# cache.destats.nf += 1
41
38
else
42
39
isforward = alg_difftype (alg) === Val{:forward }
43
40
if isforward
44
41
forwardcache = get_tmp_cache (cache, alg, unwrap_cache (cache, true ))[2 ]
45
- f (forwardcache, x)
42
+ uf (forwardcache, x)
46
43
# cache.destats.nf += 1
47
- tmp = jacobian_finitediff_forward! (J, f , x, jac_config, forwardcache,
44
+ tmp = jacobian_finitediff_forward! (J, uf , x, jac_config, forwardcache,
48
45
cache)
49
46
else # not forward difference
50
- tmp = jacobian_finitediff! (J, f , x, jac_config, cache)
47
+ tmp = jacobian_finitediff! (J, uf , x, jac_config, cache)
51
48
end
52
49
cache. destats. nf += tmp
53
50
end
57
54
function build_jac_config (alg, f:: F1 , uf:: F2 , du1, u, tmp, du2) where {F1, F2}
58
55
haslinsolve = hasfield (typeof (alg), :linsolve )
59
56
60
- if SciMLBase. has_jac (f) && # No Jacobian if has analytical solution
57
+ if ! SciMLBase. has_jac (f) && # No Jacobian if has analytical solution
61
58
((concrete_jac (alg) === nothing && (! haslinsolve || (haslinsolve && # No Jacobian if linsolve doesn't want it
62
59
(alg. linsolve === nothing || LinearSolve. needs_concrete_A (alg. linsolve))))) ||
63
60
(concrete_jac (alg) != = nothing && concrete_jac (alg))) # Jacobian if explicitly asked for
@@ -68,7 +65,7 @@ function build_jac_config(alg, f::F1, uf::F2, du1, u, tmp, du2) where {F1, F2}
68
65
_chunksize = get_chunksize (alg) === Val (0 ) ? nothing : get_chunksize (alg) # SparseDiffEq uses different convection...
69
66
70
67
T = if standardtag (alg)
71
- typeof (ForwardDiff. Tag (OrdinaryDiffEqTag (), eltype (u)))
68
+ typeof (ForwardDiff. Tag (NonlinearSolveTag (), eltype (u)))
72
69
else
73
70
typeof (ForwardDiff. Tag (uf, eltype (u)))
74
71
end
@@ -112,19 +109,23 @@ function jacobian_finitediff(f, x::AbstractArray, ::Type{diff_type}, dir, colorv
112
109
jac_prototype = jac_prototype)
113
110
return J, _nfcount (maximum (colorvec), diff_type)
114
111
end
115
- function jacobian (f, x, cache)
116
- alg = unwrap_alg (cache, true )
112
+ function jacobian (cache, f:: F ) where F
113
+ x = cache. u
114
+ alg = cache. alg
115
+ uf = cache. uf
117
116
local tmp
118
- if alg_autodiff (alg)
119
- J, tmp = jacobian_autodiff (f, x, cache. f, alg)
117
+
118
+ if DiffEqBase. has_jac (cache. f)
119
+ J = f. jac (cache. u, cache. p)
120
+ elseif alg_autodiff (alg)
121
+ J, tmp = jacobian_autodiff (uf, x, cache. f, alg)
120
122
else
121
123
jac_prototype = cache. f. jac_prototype
122
124
sparsity, colorvec = sparsity_colorvec (cache. f, x)
123
- dir = diffdir (cache)
124
- J, tmp = jacobian_finitediff (f , x, alg_difftype (alg), dir, colorvec, sparsity,
125
+ dir = true
126
+ J, tmp = jacobian_finitediff (uf , x, alg_difftype (alg), dir, colorvec, sparsity,
125
127
jac_prototype)
126
128
end
127
- cache. destats. nf += tmp
128
129
J
129
130
end
130
131
@@ -135,7 +136,7 @@ function jacobian_autodiff(f, x::AbstractArray, nonlinfun, alg)
135
136
maxcolor = maximum (colorvec)
136
137
chunk_size = get_chunksize (alg) === Val (0 ) ? nothing : get_chunksize (alg)
137
138
num_of_chunks = chunk_size === nothing ?
138
- Int (ceil (maxcolor / getsize (ForwardDiff. pickchunksize (maxcolor)))) :
139
+ Int (ceil (maxcolor / SparseDiffTools . getsize (ForwardDiff. pickchunksize (maxcolor)))) :
139
140
Int (ceil (maxcolor / _unwrap_val (chunk_size)))
140
141
(forwarddiff_color_jacobian (f, x, colorvec = colorvec, sparsity = sparsity,
141
142
jac_prototype = jac_prototype, chunksize = chunk_size),
0 commit comments