11# Sparsity Detection
2+ """
3+ BVPJacobianAlgorithm(diffmode = missing; nonbc_diffmode = missing, bc_diffmode = missing)
4+
5+ Select the automatic differentiation backends used to form boundary value problem
6+ Jacobians.
7+
8+ For two-point problems, `diffmode` is used for the whole residual. For standard
9+ multi-point problems, `nonbc_diffmode` is used for the differential equation residual and
10+ `bc_diffmode` is used for the boundary condition residual. Passing `diffmode` fills both
11+ specialized fields unless they are supplied explicitly.
12+ """
213@concrete struct BVPJacobianAlgorithm
314 bc_diffmode
415 nonbc_diffmode
516 diffmode
617end
718
19+ """
20+ __materialize_jacobian_algorithm(nlsolve, jac_alg)
21+
22+ Normalize a user supplied AD backend or Jacobian algorithm into a `BVPJacobianAlgorithm`.
23+ When `jac_alg` is not supplied, the nonlinear solver's `jacobian_ad` field is used if it
24+ exists.
25+ """
826@inline __materialize_jacobian_algorithm (_, alg:: BVPJacobianAlgorithm ) = alg
927@inline __materialize_jacobian_algorithm (_, alg:: ADTypes.AbstractADType ) = BVPJacobianAlgorithm (alg)
1028@inline __materialize_jacobian_algorithm (:: Nothing , :: Nothing ) = BVPJacobianAlgorithm ()
@@ -30,6 +48,11 @@ function Base.show(io::IO, alg::BVPJacobianAlgorithm)
3048 return print (io, " )" )
3149end
3250
51+ """
52+ __any_sparse_ad(ad_or_jac_alg) -> Bool
53+
54+ Return whether an AD backend or `BVPJacobianAlgorithm` contains an `AutoSparse` backend.
55+ """
3356@inline __any_sparse_ad (:: AutoSparse ) = true
3457@inline function __any_sparse_ad (jac_alg:: BVPJacobianAlgorithm )
3558 return __any_sparse_ad (jac_alg. bc_diffmode) ||
@@ -104,6 +127,11 @@ function concrete_jacobian_algorithm(jac_alg::BVPJacobianAlgorithm, prob_type, p
104127 return BVPJacobianAlgorithm (bc_diffmode, nonbc_diffmode, diffmode)
105128end
106129
130+ """
131+ __default_sparse_ad(x_or_type)
132+
133+ Choose the default sparse AD backend for an input value or element type.
134+ """
107135@inline function __default_sparse_ad (x:: AbstractArray{T} ) where {T}
108136 return isbitstype (T) ? __default_sparse_ad (T) : __default_sparse_ad (first (x))
109137end
@@ -136,15 +164,33 @@ end
136164 )
137165end
138166
167+ """
168+ __default_coloring_algorithm(diffmode)
169+
170+ Return the sparse matrix coloring algorithm associated with `diffmode`, or the package
171+ default when none is specified.
172+ """
139173@inline __default_coloring_algorithm (_) = GreedyColoringAlgorithm ()
140174@inline __default_coloring_algorithm (diffmode:: AutoSparse ) = isnothing (diffmode) ?
141175 GreedyColoringAlgorithm () :
142176 diffmode. coloring_algorithm
177+
178+ """
179+ __default_sparsity_detector(diffmode)
180+
181+ Return the sparsity detector associated with `diffmode`, or the package default when none
182+ is specified.
183+ """
143184@inline __default_sparsity_detector (_) = TracerLocalSparsityDetector ()
144185@inline __default_sparsity_detector (diffmode:: AutoSparse ) = isnothing (diffmode) ?
145186 TracerLocalSparsityDetector () :
146187 diffmode. sparsity_detector
147188
189+ """
190+ __default_nonsparse_ad(x_or_type)
191+
192+ Choose the default dense AD backend for an input value or element type.
193+ """
148194@inline function __default_nonsparse_ad (x:: AbstractArray{T} ) where {T}
149195 return isbitstype (T) ? __default_nonsparse_ad (T) : __default_nonsparse_ad (first (x))
150196end
@@ -160,6 +206,12 @@ function concretize_jacobian_algorithm(alg, prob)
160206 return alg
161207end
162208
209+ """
210+ __needs_diffcache(ad_or_jac_alg) -> Bool
211+
212+ Return whether the AD backend needs a `PreallocationTools.DiffCache` during residual or
213+ Jacobian evaluation.
214+ """
163215@inline __needs_diffcache (:: AutoForwardDiff ) = true
164216@inline __needs_diffcache (:: AutoPolyesterForwardDiff ) = true
165217@inline __needs_diffcache (ad:: AutoSparse ) = __needs_diffcache (ADTypes. dense_ad (ad))
170222 __needs_diffcache (jac_alg. nonbc_diffmode)
171223end
172224
225+ """
226+ __maybe_allocate_diffcache(x, chunksize, jac_alg)
227+
228+ Allocate a `DiffCache` for `x` when `jac_alg` requires one; otherwise return `x`.
229+ """
173230function __maybe_allocate_diffcache (x, chunksize, jac_alg)
174231 return __needs_diffcache (jac_alg) ?
175232 DiffCache (x, chunksize; warn_on_resize = false ) : x
@@ -179,9 +236,26 @@ function __maybe_allocate_diffcache(x::DiffCache, chunksize)
179236end
180237
181238# DiffCache
239+ """
240+ DiffCacheNeeded
241+
242+ Trait value indicating that a Jacobian backend needs a `DiffCache`.
243+ """
182244struct DiffCacheNeeded end
245+
246+ """
247+ NoDiffCacheNeeded
248+
249+ Trait value indicating that a Jacobian backend does not need a `DiffCache`.
250+ """
183251struct NoDiffCacheNeeded end
184252
253+ """
254+ __cache_trait(ad_or_jac_alg)
255+
256+ Return `DiffCacheNeeded()` or `NoDiffCacheNeeded()` for an AD backend or
257+ `BVPJacobianAlgorithm`.
258+ """
185259@inline __cache_trait (:: AutoForwardDiff ) = DiffCacheNeeded ()
186260@inline __cache_trait (ad:: AutoSparse ) = __cache_trait (ADTypes. dense_ad (ad))
187261@inline function __cache_trait (jac_alg:: BVPJacobianAlgorithm )
0 commit comments