You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Speed up Brent how-methods in single-diode using Scipy Cython Optimize #974
As described in #968singlediode.bishop88_i_from_v() and singlediode.bishop88_v_from_i() can be very slow using how method=brentq
Solution
Scipy made changes to access Brent using Cython which can be compiled without the GIL and with prange, a Cython command that parallelizes long loops. Compiled code is already typically very fast, so this could result in 10-100x speed improvements. Here's a gist with a Jupyter notebook benchmark that demonstrates about 30x reduction in runtime, but YMMV.
Alternatives
An alternative to Brent is Newton which is already vectorized in Scipy using NumPy arrays. A Newton how-method is already available in singlediode but Newton is unbounded, and so it's not guaranteed to converge like Brent, which is a bounded search method.
write a custom vectorized bisection method - actually there already is one, I think Rob Andrews may have authored it in pvlib.tools: _golden_sect_DataFrame, I think it's a golden search algorithm, not as fast as Brent, but still very good. Should we test it? Might need modification to work because I think it was specifically written for solving LambertW problems.
wait til next summer and make this a GSoC project 😉
Additional context
There might be some unpleasant hurdles:
how do you maintain cython code in your repo?
this should work fine for conda-forge & pvlib Anaconda channels, but what about PyPI users? We don't have a binary distribution on PyPI,
The closest experience we've had is with the NREL SPA algorithm, which we've sort of abandoned because the NumPy & numba versions are so much faster and easier
would we consider making binaries for deployment on multiple platforms like Windows (win32) & Mac OS X (darwin)
would we make Linux users build their own wheels or figure out how to get involved with manylinux?
I am involved with another project that has a cython module. I am setting up cibuildwheel with GH Actions for it and am happy with it so far -- it seems to make the PyPI binary distribution process pretty painless, at least for the three major OSs, though I only have a few hours of experience with it at this point. If we start using cython in pvlib-python at some point, cibuildwheel seems like a good way to keep PyPI users from having to compile things themselves, and I'll volunteer to do the CI fiddling to get it set up.
I installed a nightly scipy wheel (1.12.0.dev0+1691.26d4738) to compare it with brentq and newton in bishop88_i_from_v and found that it is faster than brentq by 1-2 orders of magnitude, and less than an order of magnitude away from newton:
I would be quite pleased to drop our custom golden mean search function in favor of a vectorized scipy function that guarantees convergence (given that a root is in the bounded interval). Would solve issues in #1757
Activity
kandersolar commentedon Oct 26, 2020
I am involved with another project that has a cython module. I am setting up cibuildwheel with GH Actions for it and am happy with it so far -- it seems to make the PyPI binary distribution process pretty painless, at least for the three major OSs, though I only have a few hours of experience with it at this point. If we start using cython in pvlib-python at some point, cibuildwheel seems like a good way to keep PyPI users from having to compile things themselves, and I'll volunteer to do the CI fiddling to get it set up.
kandersolar commentedon Sep 14, 2023
SciPy is adding a vectorized bounded root finder that is accessible directly from python: https://github.com/scipy/scipy/blob/0cec3111f825019598a28b9a03858866d6908d39/scipy/optimize/_zeros_py.py#L1399
I installed a nightly scipy wheel (
1.12.0.dev0+1691.26d4738) to compare it withbrentqandnewtoninbishop88_i_from_vand found that it is faster thanbrentqby 1-2 orders of magnitude, and less than an order of magnitude away fromnewton:Benchmark script
bishop88_i_from_v patch
Not sure when a public function for this root finding method will appear in scipy's API, but anyway it is something to keep an eye on.
cwhanse commentedon Sep 14, 2023
I would be quite pleased to drop our custom golden mean search function in favor of a vectorized scipy function that guarantees convergence (given that a root is in the bounded interval). Would solve issues in #1757
adriesse commentedon Sep 15, 2023
This will be useful for reverse transposition as well.