Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/sphinx/source/whatsnew/v0.16.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Deprecations

Bug fixes
~~~~~~~~~
* :py:func:`~pvlib.pvsystem.singlediode` with ``method='lambertw'`` no longer
emits ``RuntimeWarning: invalid value encountered in divide`` when the open
circuit voltage is zero, e.g. with zero photocurrent. (:issue:`2671`)


Enhancements
Expand Down Expand Up @@ -42,4 +45,5 @@ Maintenance

Contributors
~~~~~~~~~~~~
* Jakub Krasuski (:ghuser:`Blizzeq`)

14 changes: 10 additions & 4 deletions pvlib/singlediode.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,16 +908,22 @@ def _lambertw(photocurrent, saturation_current, resistance_series,
# remove try/except when scipy>=1.15, and golden mean is retired
try:
from scipy.optimize.elementwise import find_minimum
# left negative to insure strict inequality
init = (-1., 0.8*v_oc, v_oc)
# Set up initial value x1 < x2 < x3 to find vmp.
# Set x2 to 0.8*v_oc. Set x1=-1, negative to insure strict inequality
# x1 < x2. Set right to 1.0 where v_oc = 0 to ensure
# x2 < x3, otherwise set x3 = np.abs(v_oc).
# Where v_oc = 0 the minimizer returns a tiny positive v_mp, so
# v_mp and p_mp are set to 0 there to keep v_mp <= v_oc (GH 2671).
voc_zero = v_oc == 0
init = (-1., 0.8*v_oc, np.where(voc_zero, 1., np.abs(v_oc)))
res = find_minimum(_vmp_opt, init,
args=(params['photocurrent'],
params['saturation_current'],
params['resistance_series'],
params['resistance_shunt'],
params['nNsVth'],))
v_mp = res.x
p_mp = -1.*res.f_x
v_mp = np.where(voc_zero, 0., res.x)[()]
p_mp = np.where(voc_zero, 0., -1.*res.f_x)[()]
Comment on lines +925 to +926

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these necessary? If they are an attempt to clean up output for a user, I'd prefer to preserve negative v_mp, p_mp if only to detect numerical problems, or problems with input parameters.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They only apply where v_oc == 0 exactly, so negative v_mp or p_mp from other inputs pass through unchanged. I added them after your comment from Sep 22: with x3 = 1 the minimizer returns v_mp = 9.9e-17 at v_oc = 0, which is above v_oc. p_mp is already 0 there. I can drop both and assert v_mp close to 0 in the test if you prefer the raw value.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I understand now.

except ModuleNotFoundError:
# switch to old golden section method
p_mp, v_mp = _golden_sect_DataFrame(params, 0., v_oc * 1.14,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_singlediode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
testing single-diode methods using JW Bishop 1988
"""

import warnings

import numpy as np
import pandas as pd
import scipy
Expand Down Expand Up @@ -191,6 +193,26 @@ def test_singlediode_lambert_negative_voc(mocker):
assert_array_equal(outs["v_oc"], [0, 0])


def test_singlediode_lambert_zero_voc():
"""With v_oc = 0 the bracket passed to find_minimum collapsed to
x2 == x3 and scipy raised a RuntimeWarning. See issue #2671.
"""
pytest.importorskip("scipy", minversion="1.15")
with warnings.catch_warnings():
warnings.simplefilter("error")
out = pvsystem.singlediode(0., 1e-12, 0.5, np.inf, 1.8,
method="lambertw")
outs = pvsystem.singlediode(np.array([0., 5.]), 1e-12, 0.5, np.inf,
1.8, method="lambertw")
assert out["v_oc"] == 0
assert out["v_mp"] == 0
assert out["p_mp"] == 0
assert outs["v_mp"][0] == 0
assert outs["p_mp"][0] == 0
assert 0 < outs["v_mp"][1] < outs["v_oc"][1]
assert outs["p_mp"][1] > 0


@pytest.mark.parametrize('method', ['lambertw', 'brentq', 'newton',
chandrupatla])
def test_v_from_i_i_from_v_precision(method, precise_iv_curves):
Expand Down
Loading