-
Notifications
You must be signed in to change notification settings - Fork 1.1k
WIP: Speed up singlediode._lambertw #1661
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?
Changes from 13 commits
c294e40
62a8996
a97b383
d84c83c
e6d0a33
e1aff50
d362d7b
063abf8
5a0c80b
9e88b91
91ecf1c
6098885
0ca692c
4da7d2f
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
ASV benchmarks for singlediode.py | ||
""" | ||
# slower MT implementation in numpy<=1.16 | ||
from numpy.random import RandomState | ||
# replace with below when ASV migrates to numpy>=1.17 | ||
# and replace 'rng.rang()' with 'rng()' | ||
# from numpy.random import Generator, MT19937 | ||
from pvlib import singlediode as _singlediode | ||
|
||
|
||
def b88(params): | ||
# for a fair comparison, need to also compute isc, voc, i_x and i_xx | ||
isc = _singlediode.bishop88_i_from_v(0., *params) | ||
voc = _singlediode.bishop88_v_from_i(0., *params) | ||
imp, vmp, pmp = _singlediode.bishop88_mpp(*params) | ||
ix = _singlediode.bishop88_i_from_v(vmp/2., *params) | ||
ixx = _singlediode.bishop88_i_from_v((voc + vmp)/2., *params) | ||
return imp, vmp, pmp, isc, voc, ix, ixx | ||
|
||
|
||
class SingleDiode: | ||
|
||
def setup(self): | ||
seed = 11471 | ||
rng = RandomState(seed) | ||
nsamples = 10000 | ||
il = 9. * rng.rand(nsamples) + 1. # 1.- 10. A | ||
io = 10**(-9 + 3. * rng.rand(nsamples)) # 1e-9 to 1e-6 A | ||
rs = 5. * rng.rand(nsamples) + 0.05 # 0.05 to 5.05 Ohm | ||
rsh = 10**(2 + 2 * rng.rand(nsamples)) # 100 to 10000 Ohm | ||
n = 1 + 0.7 * rng.rand(nsamples) # 1.0 to 1.7 | ||
# 72 cells in series, roughly 25C Tcell | ||
nNsVth = 72 * n * 0.025 | ||
self.params = (il, io, rs, rsh, nNsVth) | ||
|
||
def time_bishop88(self): | ||
b88(self.params) | ||
|
||
def time_lambertw(self): | ||
_singlediode._lambertw(*self.params) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
from functools import partial | ||
import numpy as np | ||
from pvlib.tools import _golden_sect_DataFrame | ||
|
||
from scipy.optimize import brentq, newton | ||
from scipy.special import lambertw | ||
|
@@ -640,16 +639,20 @@ def _lambertw(photocurrent, saturation_current, resistance_series, | |
v_oc = _lambertw_v_from_i(resistance_shunt, resistance_series, nNsVth, 0., | ||
saturation_current, photocurrent) | ||
|
||
params = {'r_sh': resistance_shunt, | ||
'r_s': resistance_series, | ||
'nNsVth': nNsVth, | ||
'i_0': saturation_current, | ||
'i_l': photocurrent} | ||
|
||
# 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) | ||
conductance_shunt = 1. / resistance_shunt | ||
il, io, rs, gsh, a = \ | ||
np.broadcast_arrays(photocurrent, saturation_current, | ||
resistance_series, conductance_shunt, nNsVth) | ||
|
||
# Compute maximum power point quantities | ||
params = (il, io, rs, gsh, a) | ||
imp_est = 0.8 * il | ||
args, i0 = _prepare_newton_inputs((), params, imp_est) | ||
i_mp = newton(func=_imp_zero, x0=i0, fprime=_imp_zero_prime, | ||
args=args) | ||
v_mp = _lambertw_v_from_i(resistance_shunt, resistance_series, nNsVth, | ||
i_mp, saturation_current, photocurrent) | ||
p_mp = i_mp * v_mp | ||
|
||
# Find Imp using Lambert W | ||
i_mp = _lambertw_i_from_v(resistance_shunt, resistance_series, nNsVth, | ||
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. I think this |
||
|
@@ -679,6 +682,105 @@ def _lambertw(photocurrent, saturation_current, resistance_series, | |
return out | ||
|
||
|
||
def _w_psi(i, il, io, gsh, a): | ||
''' Computes W(psi), where psi = io * rsh / a exp((il + io - i) * rsh / a) | ||
This term is part of the equation V=V(I) solving the single diode equation | ||
V = (il + io - i)*rsh - i*rs - a W(psi) | ||
|
||
Parameters | ||
---------- | ||
i : numeric | ||
Current (A) | ||
il : numeric | ||
Photocurrent (A) | ||
io : numeric | ||
Saturation current (A) | ||
gsh : numeric | ||
Shunt conductance (1/Ohm) | ||
a : numeric | ||
The product n*Ns*Vth (V). | ||
|
||
Returns | ||
------- | ||
lambertwterm : numeric | ||
The value of W(psi) | ||
|
||
''' | ||
with np.errstate(over='ignore'): | ||
argW = (io / (gsh * a) * | ||
np.exp((-i + il + io) / | ||
(gsh * a))) | ||
lambertwterm = np.array(lambertw(argW).real) | ||
|
||
idx_inf = np.logical_not(np.isfinite(lambertwterm)) | ||
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. This will cause both |
||
if np.any(idx_inf): | ||
# Calculate using log(argW) in case argW is really big | ||
logargW = (np.log(io) - np.log(gsh) - | ||
np.log(a) + | ||
(-i + il + io) / | ||
(gsh * a))[idx_inf] | ||
|
||
# Six iterations of Newton-Raphson method to solve | ||
# w+log(w)=logargW. The initial guess is w=logargW. Where direct | ||
# evaluation (above) results in NaN from overflow, 3 iterations | ||
# of Newton's method gives approximately 8 digits of precision. | ||
w = logargW | ||
for _ in range(0, 6): | ||
w = w * (1. - np.log(w) + logargW) / (1. + w) | ||
lambertwterm[idx_inf] = w | ||
return lambertwterm | ||
|
||
|
||
def _imp_est(i, il, io, rs, gsh, a): | ||
wma = _w_psi(i, il, io, gsh, a) | ||
f = (il + io - i) / gsh - i * rs - a * wma | ||
fprime = -rs - 1. / (gsh * (1. + wma)) | ||
return -f / fprime | ||
|
||
|
||
def _split_on_gsh(gsh): | ||
# Determine indices where 0 < Gsh requires implicit model solution | ||
idx_p = 0. < gsh | ||
# Determine indices where 0 = Gsh allows explicit model solution | ||
idx_z = 0. == gsh | ||
return idx_p, idx_z | ||
|
||
|
||
def _imp_zero(i, il, io, rs, gsh, a): | ||
''' Root of this function is at imp | ||
''' | ||
idx_p, idx_z = _split_on_gsh(gsh) | ||
res = np.full_like(i, np.nan, dtype=np.float64) | ||
|
||
if np.any(idx_z): | ||
# explicit solution for gsh=0 | ||
t = il[idx_z] + io[idx_z] - i[idx_z] | ||
res[idx_z] = i[idx_z] / t - np.log(t / io[idx_z]) | ||
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. When I derive this by hand I get an extra In the limit This can be rearranged to solve explicitly for Differentiating Hence: The derivative is zero at 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. You're correct, I dropped a sign change. |
||
if np.any(idx_p): | ||
res[idx_p] = _imp_est(i[idx_p], il[idx_p], io[idx_p], rs[idx_p], | ||
gsh[idx_p], a[idx_p]) - i[idx_p] | ||
return res | ||
|
||
|
||
def _imp_zero_prime(i, il, io, rs, gsh, a): | ||
''' Derivative of _imp_zero with respect to current i | ||
''' | ||
idx_p, idx_z = _split_on_gsh(gsh) | ||
res = np.full_like(i, np.nan, dtype=np.float64) | ||
if np.any(idx_z): | ||
# explicit solution for gsh=0 | ||
t = il[idx_z] + io[idx_z] - i[idx_z] | ||
res[idx_z] = 2. / t + i[idx_z] / t**2. | ||
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. Depending on whether my previous comment regarding a missing term is in error, this may also need an additional term |
||
if np.any(idx_p): | ||
wma = _w_psi(i[idx_p], il[idx_p], io[idx_p], gsh[idx_p], a[idx_p]) | ||
f = (il[idx_p] + io[idx_p] - i[idx_p]) / gsh[idx_p] - \ | ||
i[idx_p] * rs[idx_p] - a[idx_p] * wma | ||
fprime = -rs[idx_p] - 1. / (gsh[idx_p] * (1. + wma)) | ||
fprime2 = -1. / (gsh[idx_p]**2. * a[idx_p]) * wma / (1 + wma)**3. | ||
res[idx_p] = f / fprime**2. * fprime2 - 2. | ||
return res | ||
|
||
|
||
def _pwr_optfcn(df, loc): | ||
''' | ||
Function to find power from ``i_from_v``. | ||
|
Uh oh!
There was an error while loading. Please reload this page.