Skip to content

Commit bba8e72

Browse files
committed
add some tests
1 parent 3991f12 commit bba8e72

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

lib/NonlinearSolveBase/src/solve.jl

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,23 @@ problems.
4545
https://docs.sciml.ai/SciMLSensitivity/stable/
4646
"""
4747
function solve(prob::AbstractNonlinearProblem, args...; sensealg = nothing,
48-
u0 = nothing, p = nothing, wrap = Val(true), kwargs...)
48+
u0 = nothing, p = nothing, wrap = Val(true), verbose = NonlinearVerbosity(), kwargs...)
4949
if sensealg === nothing && haskey(prob.kwargs, :sensealg)
5050
sensealg = prob.kwargs[:sensealg]
5151
end
5252

53+
if verbose isa Bool
54+
# @warn "Using `true` or `false` for `verbose` is being deprecated. Please use a `NonlinearVerbosity` type to specify verbosity settings.
55+
# For details see the verbosity section of the common solver options documentation page."
56+
if verbose
57+
verbose = NonlinearVerbosity()
58+
else
59+
verbose = NonlinearVerbosity(None())
60+
end
61+
elseif verbose isa AbstractVerbosityPreset
62+
verbose = NonlinearVerbosity(verbose)
63+
end
64+
5365
if haskey(prob.kwargs, :alias_u0)
5466
@warn "The `alias_u0` keyword argument is deprecated. Please use a NonlinearAliasSpecifier, e.g. `alias = NonlinearAliasSpecifier(alias_u0 = true)`."
5567
alias_spec = NonlinearAliasSpecifier(alias_u0 = prob.kwargs[:alias_u0])
@@ -85,6 +97,7 @@ function solve(prob::AbstractNonlinearProblem, args...; sensealg = nothing,
8597
args...;
8698
alias_u0 = alias_u0,
8799
originator = SciMLBase.ChainRulesOriginator(),
100+
verbose,
88101
kwargs...))
89102
else
90103
solve_up(prob,
@@ -94,6 +107,7 @@ function solve(prob::AbstractNonlinearProblem, args...; sensealg = nothing,
94107
args...;
95108
alias_u0 = alias_u0,
96109
originator = SciMLBase.ChainRulesOriginator(),
110+
verbose,
97111
kwargs...)
98112
end
99113
end
@@ -165,15 +179,27 @@ end
165179

166180
function init(
167181
prob::AbstractNonlinearProblem, args...; sensealg = nothing,
168-
u0 = nothing, p = nothing, kwargs...)
182+
u0 = nothing, p = nothing, verbose = NonlinearVerbosity(), kwargs...)
169183
if sensealg === nothing && has_kwargs(prob) && haskey(prob.kwargs, :sensealg)
170184
sensealg = prob.kwargs[:sensealg]
171185
end
172186

187+
if verbose isa Bool
188+
# @warn "Using `true` or `false` for `verbose` is being deprecated. Please use a `NonlinearVerbosity` type to specify verbosity settings.
189+
# For details see the verbosity section of the common solver options documentation page."
190+
if verbose
191+
verbose = NonlinearVerbosity()
192+
else
193+
verbose = NonlinearVerbosity(None())
194+
end
195+
elseif verbose isa AbstractVerbosityPreset
196+
verbose = NonlinearVerbosity(verbose)
197+
end
198+
173199
u0 = u0 !== nothing ? u0 : prob.u0
174200
p = p !== nothing ? p : prob.p
175201

176-
init_up(prob, sensealg, u0, p, args...; kwargs...)
202+
init_up(prob, sensealg, u0, p, args...; verbose, kwargs...)
177203
end
178204

179205
function init_up(prob::AbstractNonlinearProblem,

test/verbosity_tests.jl

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,83 @@
118118
prob, verbose = NonlinearVerbosity(threshold_state = SciMLLogging.InfoLevel()))
119119

120120
@test cache.verbose.threshold_state == SciMLLogging.InfoLevel()
121-
end
121+
122+
f(u, p) = u .* u .- 2
123+
prob = NonlinearProblem(f, [1.0, 1.0])
124+
125+
@testset "solve with Bool verbose" begin
126+
# Test verbose = true works (default verbosity)
127+
sol1 = solve(prob, verbose = true)
128+
@test sol1.retcode == ReturnCode.Success
129+
130+
# Test verbose = false silences all output
131+
@test_logs min_level=0 sol2=solve(prob, verbose = false)
132+
end
133+
134+
@testset "solve with Preset verbose" begin
135+
# Test verbose = SciMLLogging.Standard() works
136+
sol1 = solve(prob, verbose = SciMLLogging.Standard())
137+
@test sol1.retcode == ReturnCode.Success
138+
139+
# Test verbose = SciMLLogging.None() silences output
140+
@test_logs min_level=0 sol2=solve(prob, verbose = SciMLLogging.None())
141+
142+
# Test verbose = SciMLLogging.Detailed() works
143+
sol3 = solve(prob, verbose = SciMLLogging.Detailed())
144+
@test sol3.retcode == ReturnCode.Success
145+
146+
# Test verbose = SciMLLogging.All() works
147+
sol4 = solve(prob, verbose = SciMLLogging.All())
148+
@test sol4.retcode == ReturnCode.Success
149+
150+
# Test verbose = SciMLLogging.Minimal() works
151+
sol5 = solve(prob, verbose = SciMLLogging.Minimal())
152+
@test sol5.retcode == ReturnCode.Success
153+
end
154+
155+
@testset "init with Bool verbose" begin
156+
# Test verbose = true converts to NonlinearVerbosity()
157+
cache1 = init(prob, verbose = true)
158+
@test cache1.verbose isa NonlinearVerbosity
159+
@test cache1.verbose.threshold_state == SciMLLogging.WarnLevel()
160+
161+
# Test verbose = false converts to NonlinearVerbosity(None())
162+
cache2 = init(prob, verbose = false)
163+
@test cache2.verbose isa NonlinearVerbosity
164+
@test cache2.verbose.threshold_state isa SciMLLogging.Silent
165+
@test cache2.verbose.non_enclosing_interval isa SciMLLogging.Silent
166+
end
167+
168+
@testset "init with Preset verbose" begin
169+
# Test verbose = SciMLLogging.Standard() converts to NonlinearVerbosity(Standard())
170+
cache1 = init(prob, verbose = SciMLLogging.Standard())
171+
@test cache1.verbose isa NonlinearVerbosity
172+
@test cache1.verbose.threshold_state == SciMLLogging.WarnLevel()
173+
174+
# Test verbose = SciMLLogging.None() converts to NonlinearVerbosity(None())
175+
cache2 = init(prob, verbose = SciMLLogging.None())
176+
@test cache2.verbose isa NonlinearVerbosity
177+
@test cache2.verbose.threshold_state isa SciMLLogging.Silent
178+
179+
# Test verbose = SciMLLogging.Detailed()
180+
cache3 = init(prob, verbose = SciMLLogging.Detailed())
181+
@test cache3.verbose isa NonlinearVerbosity
182+
@test cache3.verbose.linear_verbosity isa SciMLLogging.Detailed
183+
184+
# Test verbose = SciMLLogging.All()
185+
cache4 = init(prob, verbose = SciMLLogging.All())
186+
@test cache4.verbose isa NonlinearVerbosity
187+
@test cache4.verbose.threshold_state == SciMLLogging.InfoLevel()
188+
189+
# Test verbose = SciMLLogging.Minimal()
190+
cache5 = init(prob, verbose = SciMLLogging.Minimal())
191+
@test cache5.verbose isa NonlinearVerbosity
192+
@test cache5.verbose.alias_u0_immutable isa SciMLLogging.Silent
193+
end
194+
195+
@testset "init then solve with converted verbose" begin
196+
# Ensure the converted verbose works through the full solve pipeline
197+
cache = init(prob, verbose = false)
198+
@test_logs min_level=0 sol=solve!(cache)
199+
end
200+
end

0 commit comments

Comments
 (0)