Skip to content

Test counters jprod jtprod #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/src/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 45 additions & 0 deletions test/nlp/counters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
35 changes: 35 additions & 0 deletions test/nls/counters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading