-
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 1 commit
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
- Loading branch information
There are no files selected for viewing
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,17 +639,18 @@ 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) | ||
|
||
params = (photocurrent, saturation_current, resistance_series, | ||
resistance_shunt, nNsVth) | ||
|
||
# Compute maximum power point quantities | ||
imp_est = 0.8 * photocurrent | ||
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 |
||
v_mp, saturation_current, photocurrent) | ||
|
@@ -679,6 +679,75 @@ def _lambertw(photocurrent, saturation_current, resistance_series, | |
return out | ||
|
||
|
||
def _w_psi(i, il, io, rsh, 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) | ||
rsh : numeric | ||
Shunt resistance (Ohm) | ||
a : numeric | ||
The product n*Ns*Vth (V). | ||
|
||
Returns | ||
------- | ||
lambertwterm : numeric | ||
The value of W(psi) | ||
|
||
''' | ||
gsh = 1. / rsh | ||
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, rsh, a): | ||
wma = _w_psi(i, il, io, rsh, a) | ||
f = (il + io - i) * rsh - i * rs - a * wma | ||
fprime = -rs - rsh / (1 + wma) | ||
return -f / fprime | ||
|
||
|
||
def _imp_zero(i, il, io, rs, rsh, a): | ||
return _imp_est(i, il, io, rs, rsh, a) - i | ||
|
||
|
||
def _imp_zero_prime(i, il, io, rs, rsh, a): | ||
wma = _w_psi(i, il, io, rsh, a) | ||
f = (il + io - i) * rsh - i * rs - a * wma | ||
fprime = -rs - rsh / (1 + wma) | ||
fprime2 = -rsh**2. / a * wma / (1 + wma)**3. | ||
return f / fprime**2. * fprime2 - 2. | ||
|
||
|
||
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.