diff --git a/docs/src/tools.md b/docs/src/tools.md index 6923d1f1..b08d1b34 100644 --- a/docs/src/tools.md +++ b/docs/src/tools.md @@ -60,6 +60,9 @@ grad(nlp, rand(2)) sum_counters(nlp) ``` +!!! note "Counter testing" + All function evaluation counters are properly tested for both direct function calls and sparsity structure-based calls. For example, `jprod!` and `jtprod!` counters are verified to increment correctly whether called with `jprod!(nlp, x, v, Jv)` or with the sparsity structure format `jprod!(nlp, rows, cols, vals, v, Jv)`. + ## Querying problem type There are some utility functions for querying the problem type: diff --git a/test/nlp/counters.jl b/test/nlp/counters.jl index ba5a7d63..842e36d7 100644 --- a/test/nlp/counters.jl +++ b/test/nlp/counters.jl @@ -20,6 +20,51 @@ @test sum_counters(nlp) == 0 end +@testset "Counters for jprod and jtprod using jacobian sparsity structure" begin + nlp = SimpleNLPModel() + x = nlp.meta.x0 + v = ones(nlp.meta.nvar) + w = ones(nlp.meta.ncon) + Jv = similar(w) + Jtw = similar(v) + + # Test counters for jprod! with sparsity structure + reset!(nlp) + initial_jprod_count = neval_jprod(nlp) + jprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), v, Jv) + @test neval_jprod(nlp) == initial_jprod_count + 1 + + # Test counters for jtprod! with sparsity structure + reset!(nlp) + initial_jtprod_count = neval_jtprod(nlp) + jtprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), w, Jtw) + @test neval_jtprod(nlp) == initial_jtprod_count + 1 + + # Test counters for jprod_nln! with sparsity structure + reset!(nlp) + initial_jprod_nln_count = neval_jprod_nln(nlp) + jprod_nln!(nlp, jac_nln_structure(nlp)..., jac_nln_coord(nlp, x), v, Jv[2:2]) + @test neval_jprod_nln(nlp) == initial_jprod_nln_count + 1 + + # Test counters for jtprod_nln! with sparsity structure + reset!(nlp) + initial_jtprod_nln_count = neval_jtprod_nln(nlp) + jtprod_nln!(nlp, jac_nln_structure(nlp)..., jac_nln_coord(nlp, x), w[2:2], Jtw) + @test neval_jtprod_nln(nlp) == initial_jtprod_nln_count + 1 + + # Test counters for jprod_lin! with sparsity structure + reset!(nlp) + initial_jprod_lin_count = neval_jprod_lin(nlp) + jprod_lin!(nlp, jac_lin_structure(nlp)..., jac_lin_coord(nlp, x), v, Jv[1:1]) + @test neval_jprod_lin(nlp) == initial_jprod_lin_count + 1 + + # Test counters for jtprod_lin! with sparsity structure + reset!(nlp) + initial_jtprod_lin_count = neval_jtprod_lin(nlp) + jtprod_lin!(nlp, jac_lin_structure(nlp)..., jac_lin_coord(nlp, x), w[1:1], Jtw) + @test neval_jtprod_lin(nlp) == initial_jtprod_lin_count + 1 +end + if VERSION ≥ VersionNumber(1, 7, 3) @testset "Allocations for NLP counters" begin nlp = SimpleNLPModel() diff --git a/test/nls/counters.jl b/test/nls/counters.jl index 7bb149db..2a48d283 100644 --- a/test/nls/counters.jl +++ b/test/nls/counters.jl @@ -27,6 +27,41 @@ @test sum_counters(nls) == 0 end +@testset "Counters for jprod and jtprod using jacobian sparsity structure" begin + nls = SimpleNLSModel() + x = nls.meta.x0 + v = ones(nls.meta.nvar) + w = ones(nls.meta.ncon) + Jv = similar(w) + Jtw = similar(v) + + # Test counters for jprod! with sparsity structure + reset!(nls) + initial_jprod_count = neval_jprod(nls) + jprod!(nls, jac_structure(nls)..., jac_coord(nls, x), v, Jv) + @test neval_jprod(nls) == initial_jprod_count + 1 + + # Test counters for jtprod! with sparsity structure + reset!(nls) + initial_jtprod_count = neval_jtprod(nls) + jtprod!(nls, jac_structure(nls)..., jac_coord(nls, x), w, Jtw) + @test neval_jtprod(nls) == initial_jtprod_count + 1 + + # Test counters for jprod_residual! with sparsity structure + w_residual = ones(nls.nls_meta.nequ) + Jv_residual = similar(w_residual) + reset!(nls) + initial_jprod_residual_count = neval_jprod_residual(nls) + jprod_residual!(nls, jac_structure_residual(nls)..., jac_coord_residual(nls, x), v, Jv_residual) + @test neval_jprod_residual(nls) == initial_jprod_residual_count + 1 + + # Test counters for jtprod_residual! with sparsity structure + reset!(nls) + initial_jtprod_residual_count = neval_jtprod_residual(nls) + jtprod_residual!(nls, jac_structure_residual(nls)..., jac_coord_residual(nls, x), w_residual, Jtw) + @test neval_jtprod_residual(nls) == initial_jtprod_residual_count + 1 +end + if VERSION ≥ VersionNumber(1, 7, 3) @testset "Allocations for NLS counters" begin nls = SimpleNLSModel()