-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add scipy.optimize.elementwise.find_root
(method='chandrupatla'
) to bishop88 functions
#2498
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
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f65583b
add method='chandrupatla' for bishop88 functions
kandersolar c7ba903
update tests
kandersolar 0b5cc71
whatsnew
kandersolar 01f7d56
doc tweaks
kandersolar 3aa8acd
test tweak
kandersolar 394cc58
try out skipping chandrupatla on py3.9
kandersolar 99e3ae9
fix py3.9 skips
kandersolar a720411
Apply suggestions from code review
kandersolar 106712b
more edits from review
kandersolar d4cd028
docs fixes
kandersolar ca6e4b8
Apply suggestions from code review
kandersolar 26377f8
Merge branch 'main' into chandrupatla
kandersolar 5652342
Merge remote-tracking branch 'upstream/main' into chandrupatla
kandersolar dfb02f8
move whatsnew entry to v0.13.2
kandersolar b9a6e64
fix test and lint issues
kandersolar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,13 +109,13 @@ | |
(a-Si) modules that is the product of the PV module number of series | ||
cells :math:`N_{s}` and the builtin voltage :math:`V_{bi}` of the | ||
intrinsic layer. [V]. | ||
breakdown_factor : float, default 0 | ||
breakdown_factor : numeric, default 0 | ||
fraction of ohmic current involved in avalanche breakdown :math:`a`. | ||
Default of 0 excludes the reverse bias term from the model. [unitless] | ||
breakdown_voltage : float, default -5.5 | ||
breakdown_voltage : numeric, default -5.5 | ||
reverse breakdown voltage of the photovoltaic junction :math:`V_{br}` | ||
[V] | ||
breakdown_exp : float, default 3.28 | ||
breakdown_exp : numeric, default 3.28 | ||
avalanche breakdown exponent :math:`m` [unitless] | ||
gradients : bool | ||
False returns only I, V, and P. True also returns gradients | ||
|
@@ -162,12 +162,11 @@ | |
# calculate temporary values to simplify calculations | ||
v_star = diode_voltage / nNsVth # non-dimensional diode voltage | ||
g_sh = 1.0 / resistance_shunt # conductance | ||
if breakdown_factor > 0: # reverse bias is considered | ||
cwhanse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
brk_term = 1 - diode_voltage / breakdown_voltage | ||
brk_pwr = np.power(brk_term, -breakdown_exp) | ||
i_breakdown = breakdown_factor * diode_voltage * g_sh * brk_pwr | ||
else: | ||
i_breakdown = 0. | ||
|
||
brk_term = 1 - diode_voltage / breakdown_voltage | ||
brk_pwr = np.power(brk_term, -breakdown_exp) | ||
i_breakdown = breakdown_factor * diode_voltage * g_sh * brk_pwr | ||
|
||
i = (photocurrent - saturation_current * np.expm1(v_star) # noqa: W503 | ||
- diode_voltage * g_sh - i_recomb - i_breakdown) # noqa: W503 | ||
v = diode_voltage - i * resistance_series | ||
|
@@ -177,18 +176,14 @@ | |
grad_i_recomb = np.where(is_recomb, i_recomb / v_recomb, 0) | ||
grad_2i_recomb = np.where(is_recomb, 2 * grad_i_recomb / v_recomb, 0) | ||
g_diode = saturation_current * np.exp(v_star) / nNsVth # conductance | ||
if breakdown_factor > 0: # reverse bias is considered | ||
cwhanse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
brk_pwr_1 = np.power(brk_term, -breakdown_exp - 1) | ||
brk_pwr_2 = np.power(brk_term, -breakdown_exp - 2) | ||
brk_fctr = breakdown_factor * g_sh | ||
grad_i_brk = brk_fctr * (brk_pwr + diode_voltage * | ||
-breakdown_exp * brk_pwr_1) | ||
grad2i_brk = (brk_fctr * -breakdown_exp # noqa: W503 | ||
* (2 * brk_pwr_1 + diode_voltage # noqa: W503 | ||
* (-breakdown_exp - 1) * brk_pwr_2)) # noqa: W503 | ||
else: | ||
grad_i_brk = 0. | ||
grad2i_brk = 0. | ||
brk_pwr_1 = np.power(brk_term, -breakdown_exp - 1) | ||
brk_pwr_2 = np.power(brk_term, -breakdown_exp - 2) | ||
brk_fctr = breakdown_factor * g_sh | ||
grad_i_brk = brk_fctr * (brk_pwr + diode_voltage * | ||
-breakdown_exp * brk_pwr_1) | ||
grad2i_brk = (brk_fctr * -breakdown_exp # noqa: W503 | ||
* (2 * brk_pwr_1 + diode_voltage # noqa: W503 | ||
* (-breakdown_exp - 1) * brk_pwr_2)) # noqa: W503 | ||
grad_i = -g_diode - g_sh - grad_i_recomb - grad_i_brk # di/dvd | ||
grad_v = 1.0 - grad_i * resistance_series # dv/dvd | ||
# dp/dv = d(iv)/dv = v * di/dv + i | ||
|
@@ -247,12 +242,14 @@ | |
breakdown_exp : float, default 3.28 | ||
avalanche breakdown exponent :math:`m` [unitless] | ||
method : str, default 'newton' | ||
Either ``'newton'`` or ``'brentq'``. ''method'' must be ``'newton'`` | ||
Either ``'newton'``, ``'brentq'``, or ``'chandrupatla'`` (requires | ||
scipy 1.15 or greater). ''method'' must be ``'newton'`` | ||
if ``breakdown_factor`` is not 0. | ||
method_kwargs : dict, optional | ||
Keyword arguments passed to root finder method. See | ||
:py:func:`scipy:scipy.optimize.brentq` and | ||
:py:func:`scipy:scipy.optimize.newton` parameters. | ||
:py:func:`scipy:scipy.optimize.brentq`, | ||
:py:func:`scipy:scipy.optimize.newton`, and | ||
:py:func:`scipy:scipy.optimize.elementwise.find_root` for parameters. | ||
kandersolar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``'full_output': True`` is allowed, and ``optimizer_output`` would be | ||
returned. See examples section. | ||
|
@@ -333,6 +330,30 @@ | |
vd = newton(func=lambda x, *a: fv(x, voltage, *a), x0=x0, | ||
fprime=lambda x, *a: bishop88(x, *a, gradients=True)[4], | ||
args=args, **method_kwargs) | ||
elif method == 'chandrupatla': | ||
try: | ||
from scipy.optimize.elementwise import find_root | ||
except ModuleNotFoundError as e: | ||
# TODO remove this when our minimum scipy version is >=1.15 | ||
msg = ( | ||
"method='chandrupatla' requires scipy v1.15 or greater. " | ||
"Select another method, or update your version of scipy. " | ||
f"({str(e)})" | ||
) | ||
raise ImportError(msg) | ||
|
||
voc_est = estimate_voc(photocurrent, saturation_current, nNsVth) | ||
shape = _shape_of_max_size(voltage, voc_est) | ||
vlo = np.zeros(shape) | ||
vhi = np.full(shape, voc_est) | ||
bounds = (vlo, vhi) | ||
kwargs_trimmed = method_kwargs.copy() | ||
kwargs_trimmed.pop("full_output", None) # not valid for find_root | ||
|
||
result = find_root(fv, bounds, args=(voltage, *args), **kwargs_trimmed) | ||
vd = result.x | ||
if method_kwargs.get('full_output'): | ||
vd = (vd, result) # mimic the other methods | ||
else: | ||
raise NotImplementedError("Method '%s' isn't implemented" % method) | ||
|
||
|
@@ -388,7 +409,8 @@ | |
breakdown_exp : float, default 3.28 | ||
avalanche breakdown exponent :math:`m` [unitless] | ||
method : str, default 'newton' | ||
Either ``'newton'`` or ``'brentq'``. ''method'' must be ``'newton'`` | ||
Either ``'newton'``, ``'brentq'``, or ``'chandrupatla'`` (requires | ||
scipy 1.15 or greater). ''method'' must be ``'newton'`` | ||
|
||
if ``breakdown_factor`` is not 0. | ||
method_kwargs : dict, optional | ||
kandersolar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Keyword arguments passed to root finder method. See | ||
|
@@ -474,6 +496,29 @@ | |
vd = newton(func=lambda x, *a: fi(x, current, *a), x0=x0, | ||
fprime=lambda x, *a: bishop88(x, *a, gradients=True)[3], | ||
args=args, **method_kwargs) | ||
elif method == 'chandrupatla': | ||
try: | ||
from scipy.optimize.elementwise import find_root | ||
except ModuleNotFoundError as e: | ||
# TODO remove this when our minimum scipy version is >=1.15 | ||
msg = ( | ||
"method='chandrupatla' requires scipy v1.15 or greater. " | ||
kandersolar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"Select another method, or update your version of scipy. " | ||
f"({str(e)})" | ||
) | ||
raise ImportError(msg) | ||
kandersolar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
shape = _shape_of_max_size(current, voc_est) | ||
vlo = np.zeros(shape) | ||
vhi = np.full(shape, voc_est) | ||
bounds = (vlo, vhi) | ||
kwargs_trimmed = method_kwargs.copy() | ||
kwargs_trimmed.pop("full_output", None) # not valid for find_root | ||
kandersolar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
result = find_root(fi, bounds, args=(current, *args), **kwargs_trimmed) | ||
vd = result.x | ||
if method_kwargs.get('full_output'): | ||
vd = (vd, result) # mimic the other methods | ||
else: | ||
raise NotImplementedError("Method '%s' isn't implemented" % method) | ||
|
||
|
@@ -526,7 +571,8 @@ | |
breakdown_exp : numeric, default 3.28 | ||
avalanche breakdown exponent :math:`m` [unitless] | ||
method : str, default 'newton' | ||
Either ``'newton'`` or ``'brentq'``. ''method'' must be ``'newton'`` | ||
Either ``'newton'``, ``'brentq'``, or ``'chandrupatla'`` (requires | ||
scipy 1.15 or greater). ''method'' must be ``'newton'`` | ||
if ``breakdown_factor`` is not 0. | ||
method_kwargs : dict, optional | ||
Keyword arguments passed to root finder method. See | ||
|
@@ -611,6 +657,31 @@ | |
vd = newton(func=fmpp, x0=x0, | ||
fprime=lambda x, *a: bishop88(x, *a, gradients=True)[7], | ||
args=args, **method_kwargs) | ||
elif method == 'chandrupatla': | ||
try: | ||
from scipy.optimize.elementwise import find_root | ||
except ModuleNotFoundError as e: | ||
# TODO remove this when our minimum scipy version is >=1.15 | ||
msg = ( | ||
"method='chandrupatla' requires scipy v1.15 or greater. " | ||
"Select another method, or update your version of scipy. " | ||
f"({str(e)})" | ||
) | ||
raise ImportError(msg) | ||
|
||
vlo = np.zeros_like(photocurrent) | ||
vhi = np.full_like(photocurrent, voc_est) | ||
kwargs_trimmed = method_kwargs.copy() | ||
kwargs_trimmed.pop("full_output", None) # not valid for find_root | ||
|
||
result = find_root(fmpp, | ||
(vlo, vhi), | ||
args=args, | ||
**kwargs_trimmed) | ||
vd = result.x | ||
if method_kwargs.get('full_output'): | ||
vd = (vd, result) # mimic the other methods | ||
|
||
else: | ||
raise NotImplementedError("Method '%s' isn't implemented" % method) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.