question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scipy.optimize.root as a nonlinear solver

See original GitHub issue

#118 referred to the translation of one of the FEniCS tutorial examples into scikit-fem https://github.com/gdmcbain/fenics-tuto-in-skfem/tree/master/05_poisson_nonlinear. As discussed there, it’s a nonlinear Poisson equation like docs/examples/ex10.py.

In ex10, Newton iteration is coded from scratch. I wondered whether it would be possible to use a canned implementation such as scipy.optimize.root. This was done.

Actually in the scikit-fem translation, the optional Jacobian is not passed to root; it solves in a reasonable time (4 seconds) without for this very small problem. It shouldn’t be that hard to add the Jacobian (by hand as suggested in #27 or perhaps using SymPy #118).

Would ex10 be better for using a canned nonlinear solver? I tend to prefer to encapsulate algorithms like Newton iteration than recode them each time.

SciPy is already a dependency.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kinnalacommented, Feb 16, 2019

Assumed that scipy implements it in a reasonable fashion (with regards to the sparsity etc.), I don’t mind that the example is modified.

0reactions
gdmcbaincommented, Jun 5, 2020

Following the (offline) discussion of the ‘action’ of a bilinear form (Kirby & Logg, ‘Finite element variational forms’) and then the tantalizing last paragraph of Hannukainen & Jantunen about the then planned future work on ‘matrix free implementation of the finite element assembly’ (did they ever get to that?), I was reminded of ‘Jacobian-free Newton–Krylov methods’ and took another look at this using root with method='krylov'. It works quite nicely on ex10: just omit the BilinearForm and replace the for-loop with

def residual(u: np.ndarray) -> np.ndarray:
    res = asm(rhs, basis, w=basis.interpolate(u))
    res[D] = 0.
    return res

x = root(residual, x, method='krylov', options={'disp': True}).x

Complete file: ex10_jfnk.py.txt

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimization and root finding (scipy.optimize)
SciPy optimize provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints. It includes solvers for nonlinear ...
Read more >
scipy.optimize.root — SciPy v1.9.3 Manual
The solution represented as a OptimizeResult object. Important attributes are: x the solution array, success a Boolean flag indicating if the algorithm ...
Read more >
scipy.optimize.fsolve — SciPy v1.9.3 Manual
Return the roots of the (non-linear) equations defined by func(x) = 0 given a starting estimate. Parameters. funccallable f(x, *args). A function that...
Read more >
Optimization and root finding - Numpy and Scipy Documentation
Minimize a function using a nonlinear conjugate gradient algorithm. fmin_bfgs(f, x0[, fprime, args, gtol, norm, ...]) Minimize a function using the BFGS ...
Read more >
Optimization and root finding (scipy.optimize)
Find a root of a function, using Broyden's second Jacobian approximation. Large-scale nonlinear solvers: newton_krylov(F, xin[, iter, rdiff, method, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found