Skip to content

Prevent v_oc to be negative in _lambertw #1782

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

Merged
merged 4 commits into from
Jun 27, 2023
Merged
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
5 changes: 5 additions & 0 deletions docs/sphinx/source/whatsnew/v0.10.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Enhancements
Bug fixes
~~~~~~~~~

* Prevent small negative values of `v_oc` in :py:func:`pvlib.singlediode._lambertw`
which result from accumulated roundoff error. (:issue:`1780`, :issue:`1673`, :pull:`1782`)


Testing
~~~~~~~
Expand All @@ -71,3 +74,5 @@ Contributors
* Adam R. Jensen (:ghuser:`AdamRJensen`)
* Echedey Luis (:ghuser:`echedey-ls`)
* Cliff Hansen (:ghuser:`cwhanse`)
* Cédric Leroy (:ghuser:`cedricleroy`)
* Jean-Baptiste Pasquier (:ghuser:`pasquierjb`)
7 changes: 7 additions & 0 deletions pvlib/singlediode.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,13 @@ def _lambertw(photocurrent, saturation_current, resistance_series,
# Compute open circuit voltage
v_oc = _lambertw_v_from_i(0., **params)

# Set small elements <0 in v_oc to 0
if isinstance(v_oc, np.ndarray):
v_oc[(v_oc < 0) & (v_oc > -1e-12)] = 0.
elif isinstance(v_oc, (float, int)):
if v_oc < 0 and v_oc > -1e-12:
v_oc = 0.

# Find the voltage, v_mp, where the power is maximized.
# Start the golden section search at v_oc * 1.14
p_mp, v_mp = _golden_sect_DataFrame(params, 0., v_oc * 1.14, _pwr_optfcn)
Expand Down
13 changes: 13 additions & 0 deletions pvlib/tests/test_singlediode.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ def test_singlediode_precision(method, precise_iv_curves):
assert np.allclose(pc['i_xx'], outs['i_xx'], atol=1e-6, rtol=0)


def test_singlediode_lambert_negative_voc():

# Those values result in a negative v_oc out of `_lambertw_v_from_i`
x = np.array([0., 1.480501e-11, 0.178, 8000., 1.797559])
outs = pvsystem.singlediode(*x, method='lambertw')
assert outs['v_oc'] == 0

# Testing for an array
x = np.array([x, x]).T
outs = pvsystem.singlediode(*x, method='lambertw')
assert np.array_equal(outs['v_oc'], [0, 0])


@pytest.mark.parametrize('method', ['lambertw'])
def test_ivcurve_pnts_precision(method, precise_iv_curves):
"""
Expand Down