Skip to content

Use explicit finite difference instead of scipy.misc.derivative in pvsyst_temperature_coeff #1674

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 3 commits into from
Feb 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
2 changes: 2 additions & 0 deletions docs/sphinx/source/whatsnew/v0.9.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Bug fixes
* When using ``utc_time_range`` with :py:func:`pvlib.iotools.read_ecmwf_macc`,
the time index subset is now selected with ``nearest`` instead of ``before``
and ``after`` for consistency with ``cftime>=1.6.0``. (:issue:`1609`, :pull:`1656`)
* :py:func:`~pvlib.ivtools.sdm.pvsyst_temperature_coeff` no longer raises
a scipy deprecation warning (and is slightly more accurate) (:issue:`1644`, :pull:`1674`)


Testing
Expand Down
6 changes: 4 additions & 2 deletions pvlib/ivtools/sdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
from scipy import constants
from scipy import optimize
from scipy.special import lambertw
from scipy.misc import derivative

from pvlib.pvsystem import calcparams_pvsyst, singlediode, v_from_i
from pvlib.singlediode import bishop88_mpp

from pvlib.ivtools.utils import rectify_iv_curve, _numdiff
from pvlib.ivtools.sde import _fit_sandia_cocontent

from pvlib.tools import _first_order_centered_difference


CONSTANTS = {'E0': 1000.0, 'T0': 25.0, 'k': constants.k, 'q': constants.e}

Expand Down Expand Up @@ -1344,5 +1345,6 @@ def maxp(temp_cell, irrad_ref, alpha_sc, gamma_ref, mu_gamma, I_L_ref,
I_o_ref, R_sh_ref, R_sh_0, R_s, cells_in_series, R_sh_exp, EgRef,
temp_ref)
pmp = maxp(temp_ref, *args)
gamma_pdc = derivative(maxp, temp_ref, args=args)
gamma_pdc = _first_order_centered_difference(maxp, x0=temp_ref, args=args)

return gamma_pdc / pmp
11 changes: 11 additions & 0 deletions pvlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,14 @@ def _degrees_to_index(degrees, coordinate):
index = int(np.around(index))

return index


EPS = np.finfo('float64').eps # machine precision NumPy-1.20
DX = EPS**(1/3) # optimal differential element


def _first_order_centered_difference(f, x0, dx=DX, args=()):
# simple replacement for scipy.misc.derivative, which is scheduled for
# removal in scipy 1.12.0
df = f(x0+dx, *args) - f(x0-dx, *args)
return df / 2 / dx