Skip to content
70 changes: 69 additions & 1 deletion tests/test_mismatch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
from pvlib import singlediode as _singlediode
from pvlib.mismatch import _setup_currents, _singlediode_mismatch
from pvlib.mismatch import (_setup_currents, _singlediode_mismatch,
_v_from_i_mismatch)


def test__setup_currents():
Expand Down Expand Up @@ -35,3 +36,70 @@ def test__singlediode_mismatch_isc_voc():
IL, Io, Rs, Rsh, nNsVth, neg_v_limit=neg_v_limit)
assert np.isclose(isc[0], expected_isc)
assert np.isclose(voc[0], expected_voc.sum())


def test__singlediode_mismatch_five_points():
# Expected values from SunSolve (K. McIntosh, private communcication,
# Sept. 2026). pvlib calculations are corroborated by comparison with
# the SunSolve calculations.
IL = np.array([[6.], [4.]])
Io = np.array([[1e-9], [5e-8]])
Rs = np.array([[0.5], [0.7]])
Rsh = np.array([[2000.], [1200.]])
nNsVth = np.array([[2.5], [2.9]])
neg_v_limit = -5.

expected = {
'isc': np.array([5.9960000000000004]),
'voc': np.array([109.01309999999999]),
'imp': np.array([3.8311999999999999]),
'vmp': np.array([92.014600000000002]),
'pmp': np.array([352.52499999999998]),
'ff': np.array([0.53932586979593367]),
}

isc, voc, imp, vmp = _singlediode_mismatch(
IL, Io, Rs, Rsh, nNsVth, neg_v_limit=neg_v_limit)
pmp = imp * vmp
ff = pmp / (isc * voc)

np.testing.assert_allclose(isc, expected['isc'], atol=1e-3, rtol=0.)
np.testing.assert_allclose(voc, expected['voc'], atol=5e-3, rtol=0.)
np.testing.assert_allclose(imp, expected['imp'], atol=2e-3, rtol=0.)
np.testing.assert_allclose(vmp, expected['vmp'], atol=5e-2, rtol=0.)
np.testing.assert_allclose(pmp, expected['pmp'], atol=2e-2, rtol=0.)
np.testing.assert_allclose(ff, expected['ff'], atol=1e-5, rtol=0.)


def test__v_from_i_mismatch_corroboration():
# Expected values from SunSolve (K. McIntosh, private communication,
# Sept. 2026) and from Xyce (N. Jost, private communication, Sept. 2026).
IL = np.array([[6.], [4.]])
Io = np.array([[1e-9], [5e-8]])
Rs = np.array([[0.5], [0.7]])
Rsh = np.array([[2000.], [1200.]])
nNsVth = np.array([[2.5], [2.9]])
neg_v_limit = -5.

currents = np.array([1., 1.5, 2., 2.5, 3., 3.5,
4., 4.5, 5., 5.5, 6.])
sunsolve_voltages = np.array([
106.51, 105.10899999999999, 103.554,
101.76300000000001, 99.56,
96.376000000000005, 48.706000000000003,
45.524999999999999, 44.24,
42.194000000000003])
xyce_voltages = np.array([
106.53001420000001, 105.1284256,
103.57315919999999, 101.7831631,
99.5802625, 96.3959485,
48.7152532, 45.5353417,
44.2505189, 42.2050763, -8.])

voltages = _v_from_i_mismatch(
currents, IL, Io, Rs, Rsh, nNsVth, neg_v_limit=neg_v_limit)

np.testing.assert_allclose(
voltages[:, sunsolve_voltages.shape[1]], keith_voltages,
atol=5e-3, rtol=0.)
np.testing.assert_allclose(voltages, xyce_voltages, atol=2e-2, rtol=0.)