-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add lambertw_pvlib #2723
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
base: main
Are you sure you want to change the base?
Add lambertw_pvlib #2723
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -23,9 +23,12 @@ Bug fixes | |||||
|
|
||||||
| Enhancements | ||||||
| ~~~~~~~~~~~~ | ||||||
| * Use ``k`` and ``cap_adjustment`` from :py:func:`pvlib.pvsystem.Array.module_parameters` in :py:func:`pvlib.pvsystem.PVSystem.pvwatts_dc` | ||||||
| * Use ``k`` and ``cap_adjustment`` from :py:func:`pvlib.pvsystem.Array.module_parameters` | ||||||
| in :py:func:`pvlib.pvsystem.PVSystem.pvwatts_dc` | ||||||
| (:issue:`2714`, :pull:`2715`) | ||||||
|
|
||||||
| * Add :py:func:`pvlib.tools.lambertw_pvlib` to speed up calculations when using | ||||||
| LambertW single diode equation methods. | ||||||
| (:discussion:`2720`, :pull:`2723`) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
defined here: https://github.com/pvlib/pvlib-python/blob/main/docs/sphinx/source/conf.py#L331-L342
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although I suggest we hold off on a "what's new" note until the follow-up PR so that it can describe the speed-up in I-V calculations rather than a private function. |
||||||
|
|
||||||
| Documentation | ||||||
| ~~~~~~~~~~~~~ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -544,3 +544,60 @@ def astm_e1036(v, i, imax_limits=(0.75, 1.15), vmax_limits=(0.75, 1.15), | |||||||||||||||||||||||||||
| result['mp_fit'] = mp_fit | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _log_lambertw(logx): | ||||||||||||||||||||||||||||
| r'''Computes W(x) starting from log(x). | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||
| logx : numeric | ||||||||||||||||||||||||||||
| Log(x) of | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||
| numeric | ||||||||||||||||||||||||||||
| Lambert's W(x) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ''' | ||||||||||||||||||||||||||||
| # handles overflow cases, but results in nan for x <= 1 | ||||||||||||||||||||||||||||
| w = logx - np.log(logx) # initial guess, w = log(x) - log(log(x)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for _ in range(0, 3): | ||||||||||||||||||||||||||||
| # Newton's. Halley's is not substantially faster or more accurate | ||||||||||||||||||||||||||||
| # because f''(w) = -1 / (w**2) is small for large w | ||||||||||||||||||||||||||||
| w = w * (1. - np.log(w) + logx) / (1. + w) | ||||||||||||||||||||||||||||
| return w | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _lambertw_pvlib(x): | ||||||||||||||||||||||||||||
| r'''Lambert's W function principal branch, :math:`W_0(x)`, for :math:`x` | ||||||||||||||||||||||||||||
| real valued. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||
| x : np.array | ||||||||||||||||||||||||||||
| Must be real numbers. | ||||||||||||||||||||||||||||
|
Comment on lines
+579
to
+580
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will requiring |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||
| np.array | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ''' | ||||||||||||||||||||||||||||
| w = np.full_like(x, np.nan) | ||||||||||||||||||||||||||||
| small = x <= 10 | ||||||||||||||||||||||||||||
| # for large x, solve 0 = f(w) = w + log(w) - log(x) using Newton's | ||||||||||||||||||||||||||||
| w[~small] = _log_lambertw(np.log(x[~small])) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # w will contain nan for these numbers due to log(w) = log(log(x)) | ||||||||||||||||||||||||||||
| # for small x, solve 0 = g(w) = w * exp(w) - x using Halley's method | ||||||||||||||||||||||||||||
| if any(small): | ||||||||||||||||||||||||||||
| z = x[small] | ||||||||||||||||||||||||||||
| g = np.log(x[small] + 1) - np.log(np.log(x[small] + 1) + 1) | ||||||||||||||||||||||||||||
| for _ in range(0, 3): | ||||||||||||||||||||||||||||
| expg = np.exp(g) | ||||||||||||||||||||||||||||
| g = g - (g*expg - z) * (g + 1) / \ | ||||||||||||||||||||||||||||
| (expg * (g + 1)**2 - 0.5*(g + 2)*(expg*g - z)) | ||||||||||||||||||||||||||||
|
Comment on lines
+596
to
+600
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Eliminating redundant calculations improves speed by another ~15% or so (for |
||||||||||||||||||||||||||||
| w[small] = g | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return w | ||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.