-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add mismatch.py and function to combine curves #1781
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
Draft
ajonesr
wants to merge
30
commits into
pvlib:main
Choose a base branch
from
ajonesr:mismatch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
06abfaa
Added mismatch.py
ajonesr 3159bc1
Make method in bishop functions explicit
ajonesr 76efbac
Move clipping to prepare_curves
ajonesr 586de8f
Change assert to ValueError
ajonesr 3038f86
Added docstrings
ajonesr c0e57da
Fixed missing parenthesis
ajonesr 0670a6f
Updated comments, fixed typo
ajonesr a3a135b
Updated imports
ajonesr c27f558
Updated .rst file
ajonesr 5dfda15
Updated docstrings
ajonesr 2f11a16
Update docstring of prepare_curves
ajonesr 3effafe
Update docstring of combine_curves
ajonesr b969581
Fixed spacing and clipped_voltage typo
ajonesr 7ab461c
Reverse order of currents array
ajonesr d87f9df
Add tests
ajonesr cf955a3
Fixed types, updated tests
ajonesr a014b2a
Updated whatsnew
ajonesr 1f5914a
Added contributor
ajonesr 94e923c
Update prepare_curves docstring
ajonesr 31d1188
Update prepare_curves docstring
ajonesr 0880f24
Update prepare_curves docstring
ajonesr e87b4aa
Update spacing
ajonesr 057cada
Update spacing
ajonesr 606de91
Add tests for ValueErrors
ajonesr baeac3f
Merge branch 'main' into mismatch
ajonesr ded4548
Update init
ajonesr 52b7455
Added example
ajonesr 2ca0cb0
Merge remote-tracking branch 'upstream/main' into mismatch
ajonesr 8cca7b7
Update whatsnew, example
ajonesr 17d738c
Merge branch 'main' of https://github.com/pvlib/pvlib-python into mis…
cwhanse 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
""" | ||
The `mismatch` module contains functions for combining curves in | ||
series using the single diode model. | ||
""" | ||
|
||
import numpy as np | ||
from pvlib.singlediode import bishop88_i_from_v, bishop88_v_from_i | ||
|
||
|
||
def prepare_curves(params, num_pts, breakdown_voltage=-0.5): | ||
""" | ||
Calculates currents and voltages on IV curves with the given | ||
parameters, using the single diode equation. Returns values | ||
in format needed for inputs to :func:`combine_curves`. | ||
|
||
Parameters | ||
---------- | ||
params : array-like | ||
An array of parameters representing a set of :math:`n` IV | ||
curves. The array should contain :math:`n` rows and five | ||
columns. Each row contains the five parameters needed for a | ||
single curve. The parameters should be in the following order: | ||
ajonesr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
photocurrent : numeric | ||
photo-generated current :math:`I_{L}` [A] | ||
|
||
saturation_current : numeric | ||
diode reverse saturation current :math:`I_{0}` [A] | ||
|
||
resistance_series : numeric | ||
series resistance :math:`R_{s}` [ohms] | ||
|
||
resistance_shunt : numeric | ||
shunt resistance :math:`R_{sh}` [ohms] | ||
|
||
nNsVth : numeric | ||
product of thermal voltage :math:`V_{th}` [V], diode | ||
ideality factor :math:`n`, and number of series cells | ||
:math:`N_{s}` [V] | ||
|
||
num_pts : int | ||
Number of points to compute for each IV curve. | ||
|
||
breakdown_voltage : float | ||
Vertical asymptote to use left of the y-axis. Any voltages that | ||
are smaller than ``breakdown_voltage`` will be replaced by it. | ||
|
||
Returns | ||
------- | ||
tuple | ||
currents : np.ndarray | ||
A 1D array of current values. Has shape (``num_pts``,). | ||
|
||
voltages : np.ndarray | ||
A 2D array of voltage values, where each row corresponds to | ||
a single IV curve. Has shape (:math:`n`, ``num_pts``), where | ||
:math:`n` is the number of IV curves passed in. | ||
|
||
Notes | ||
----- | ||
This function assumes a simplified reverse bias model. When using | ||
:func:`pvlib.singlediode.bishop88_v_from_i`, ``breakdown_factor`` is | ||
left at the default value, which excludes the reverse bias term from | ||
the model. Instead, any returned voltages that are less than | ||
ajonesr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``breakdown_voltage`` are replaced by it, yielding a vertical line | ||
at ``breakdown_voltage``. | ||
|
||
""" | ||
|
||
# in case params is a list containing scalars, add a dimension | ||
if len(np.shape(params)) == 1: | ||
params = params[np.newaxis,:] | ||
|
||
# get range of currents from 0 to max_isc | ||
max_isc = np.max(pvlib.singlediode.bishop88_i_from_v(0.0, *params.T, | ||
method='newton')) | ||
currents = np.linspace(0, max_isc, num=num_pts, endpoint=True) | ||
|
||
# prepare inputs for bishop88 | ||
bishop_inputs = np.array([[currents[idx]]*len(params) for idx in | ||
range(num_pts)]) | ||
# each row of bishop_inputs contains n copies of a single current | ||
# value, where n is the number of curves being added together | ||
# there is a row for each current value | ||
|
||
# get voltages for each curve | ||
# (note: expecting to vectorize for both the inputted currents and | ||
# the inputted curve parameters) | ||
# transpose result so each row contains voltages for a single curve | ||
voltages = pvlib.singlediode.bishop88_v_from_i(bishop_inputs, *params.T, | ||
method='newton').T | ||
|
||
# any voltages in array that are smaller than breakdown_voltage are | ||
# clipped to be breakdown_voltage | ||
voltages = np.clip(voltages, a_min=breakdown_voltage, a_max=None) | ||
|
||
return currents, voltages | ||
|
||
|
||
def combine_curves(currents, voltages): | ||
""" | ||
Combines IV curves in series. | ||
|
||
Parameters | ||
---------- | ||
currents : array-like | ||
A 1D array-like object. Its first element must be zero, and it | ||
should be increasing. | ||
|
||
ajonesr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
voltages : array-like | ||
A 2D array-like object. Each row corresponds to a single IV | ||
curve and contains the voltages for that curve that are | ||
associated to elements of ``currents``. Each row must be | ||
decreasing. | ||
|
||
Returns | ||
------- | ||
dict | ||
Contains the following keys: | ||
i_sc : scalar | ||
short circuit current of combined curve [A] | ||
|
||
v_oc : scalar | ||
open circuit voltage of combined curve [V] | ||
|
||
i_mp : scalar | ||
current at maximum power point of combined curve [A] | ||
|
||
v_mp : scalar | ||
voltage at maximum power point of combined curve [V] | ||
|
||
p_mp : scalar | ||
power at maximum power point of combined curve [W] | ||
|
||
i : np.ndarray | ||
currents of combined curve [A] | ||
|
||
v : np.ndarray | ||
voltages of combined curve [V] | ||
|
||
Notes | ||
----- | ||
If the combined curve does not cross the y-axis, then the last (and | ||
hence largest) current is returned for short circuit current. | ||
|
||
The maximum power point that is returned is the maximum power point | ||
of the dataset. Its accuracy will improve as more points are passed | ||
in. | ||
|
||
""" | ||
|
||
currents = np.asarray(currents) | ||
voltages = np.asarray(voltages) | ||
assert currents.ndim == 1 | ||
assert voltages.ndim == 2 | ||
|
||
# for each current, add the associated voltages of all the curves | ||
# in our setup, this means summing each column of the voltage array | ||
combined_voltages = np.sum(clipped_voltages, axis=0) | ||
|
||
# combined_voltages should now have same shape as currents | ||
assert np.shape(combined_voltages) == np.shape(currents) | ||
|
||
# find max power point (in the dataset) | ||
powers = currents*combined_voltages | ||
mpp_idx = np.argmax(powers) | ||
vmp = combined_voltages[mpp_idx] | ||
imp = currents[mpp_idx] | ||
pmp = powers[mpp_idx] | ||
|
||
# we're assuming voltages are decreasing, so combined_voltages | ||
# should also be decreasing | ||
if not np.all(np.diff(combined_voltages) < 0): | ||
raise ValueError("Each row of voltages array must be decreasing.") | ||
# get isc | ||
# np.interp requires second argument is increasing, so flip | ||
# combined_voltages and currents | ||
isc = np.interp(0., np.flip(combined_voltages), np.flip(currents)) | ||
|
||
# the first element of currents must be zero | ||
if currents[0] != 0: | ||
raise ValueError("First element of currents array must be zero.") | ||
# get voc | ||
voc = combined_voltages[0] | ||
|
||
return {'i_sc': isc, 'v_oc': voc, 'i_mp': imp, 'v_mp': vmp, 'p_mp': pmp, | ||
'i': currents, 'v': combined_voltages} | ||
|
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.