Skip to content

Commit

Permalink
Minor cleanup in nonlinear solvers.
Browse files Browse the repository at this point in the history
  • Loading branch information
michakraus committed Jul 12, 2024
1 parent 081ec3b commit 660b656
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/nonlinear/abstract_newton_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ cache(solver::AbstractNewtonSolver) = solver.cache
config(solver::AbstractNewtonSolver) = solver.config
status(solver::AbstractNewtonSolver) = solver.status
jacobian(solver::AbstractNewtonSolver) = solver.jacobian
linearsolver(solver::AbstractNewtonSolver) = solver.linear
linesearch(solver::AbstractNewtonSolver) = solver.linesearch

compute_jacobian!(s::AbstractNewtonSolver, x, f::Callable) = compute_jacobian!(s, x, f, missing)
compute_jacobian!(s::AbstractNewtonSolver, x, f::Callable, ::Missing) = s.jacobian(s.cache.J, x, f)
Expand Down
8 changes: 4 additions & 4 deletions src/nonlinear/newton_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ end

function solver_step!(x, f, forj, s::NewtonSolver)
# shortcuts
rhs = s.cache.rhs
δ = s.cache.δx
rhs = cache(s).rhs
δ = cache(s).δx

# update Newton solver cache
update!(s, x)
Expand All @@ -30,14 +30,14 @@ function solver_step!(x, f, forj, s::NewtonSolver)
compute_jacobian!(s, x, forj)

# factorize linear solver
factorize!(s.linear, s.cache.J)
factorize!(linearsolver(s), cache(s).J)

# compute RHS
f(rhs, x)
rmul!(rhs, -1)

# solve J δx = -f(x)
ldiv!(δ, s.linear, rhs)
ldiv!(δ, linearsolver(s), rhs)

# apply line search
α = s.linesearch(linesearch_objective(f, jacobian(s), cache(s)))
Expand Down
10 changes: 5 additions & 5 deletions src/nonlinear/quasi_newton_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ end

function solver_step!(x, f, forj, s::QuasiNewtonSolver)
# shortcuts
rhs = s.cache.rhs
δ = s.cache.δx
rhs = cache(s).rhs
δ = cache(s).δx

# update Newton solver cache
update!(s, x)

# compute Jacobian and factorize
if mod(s.status.i-1, s.refactorize) == 0
if mod(status(s).i-1, s.refactorize) == 0
compute_jacobian!(s, x, forj)
factorize!(s.linear, s.cache.J)
factorize!(linearsolver(s), cache(s).J)
end

# compute RHS
f(rhs, x)
rmul!(rhs, -1)

# solve J δx = -f(x)
ldiv!(δ, s.linear, rhs)
ldiv!(δ, linearsolver(s), rhs)

# apply line search
α = s.linesearch(linesearch_objective(f, jacobian(s), cache(s)))
Expand Down

0 comments on commit 660b656

Please sign in to comment.