Skip to content

Commit e40af1e

Browse files
Monkey patch solver to test exception
1 parent 58e44e0 commit e40af1e

File tree

2 files changed

+32
-52
lines changed

2 files changed

+32
-52
lines changed

pvlib/pvsystem.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,18 +3052,14 @@ def max_power_point_mismatched(
30523052
"""
30533053
if i_mp_ic is None:
30543054
retry_ic = False
3055-
try:
3056-
i_mp_ic = max_power_point(
3057-
np.mean(photocurrent),
3058-
np.mean(saturation_current),
3059-
np.mean(resistance_series),
3060-
np.mean(resistance_shunt),
3061-
np.mean(nNsVth),
3062-
)["i_mp"]
3063-
except Exception as exc:
3064-
raise RuntimeError(
3065-
"unsuccessful determination of i_mp_ic"
3066-
) from exc
3055+
3056+
i_mp_ic = max_power_point(
3057+
np.mean(photocurrent),
3058+
np.mean(saturation_current),
3059+
np.mean(resistance_series),
3060+
np.mean(resistance_shunt),
3061+
np.mean(nNsVth),
3062+
)["i_mp"]
30673063
else:
30683064
retry_ic = True
30693065

@@ -3074,6 +3070,7 @@ def max_power_point_mismatched(
30743070
resistance_shunt,
30753071
nNsVth,
30763072
)
3073+
30773074
sol = scipy.optimize.minimize(
30783075
_negative_total_power, i_mp_ic, args=args, jac='3-point'
30793076
)

pvlib/tests/test_pvsystem.py

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from numpy import nan, array
66
import pandas as pd
77
import scipy.constants
8+
import scipy.optimize
89

910
import pytest
1011
from .conftest import (
@@ -13,7 +14,6 @@
1314
import unittest.mock as mock
1415

1516
from pvlib import inverter, pvsystem
16-
from pvlib import atmosphere
1717
from pvlib import iam as _iam
1818
from pvlib import irradiance
1919
from pvlib import spectrum
@@ -2648,48 +2648,31 @@ def test_max_power_point_mismatched(inputs):
26482648
print(result)
26492649

26502650

2651-
@pytest.mark.parametrize(
2652-
'inputs',
2653-
[
2654-
{
2655-
"photocurrent": -6.2, # This is bad.
2656-
"saturation_current": 1.0e-8,
2657-
"n": 1.1,
2658-
"resistance_series": 0.0001,
2659-
"resistance_shunt": 5000.0,
2660-
"Ns": 60,
2661-
"T": 25.0,
2662-
},
2663-
{
2664-
"photocurrent": -6.2, # This is bad.
2665-
"saturation_current": 1.0e-8,
2666-
"n": 1.1,
2667-
"resistance_series": 0.0001,
2668-
"resistance_shunt": 5000.0,
2669-
"Ns": 60,
2670-
"T": 25.0,
2671-
"i_mp_ic": 5.6,
2672-
},
2673-
]
2674-
)
2675-
def test_max_power_point_mismatched_exception(inputs, monkeypatch):
2676-
"""
2677-
Test errored max power point computation for mismatched devices in series.
2678-
"""
2679-
# Monkey patch the objective function to force thrown exception.
2651+
def test_max_power_point_mismatched_unsuccessful_solver(monkeypatch):
2652+
"""Test errored max power point computation where solver is unsuccessful."""
2653+
photocurrent = 6.2
2654+
saturation_current = 1.0e-8
2655+
resistance_series = 0.0001
2656+
resistance_shunt = 5000.0
2657+
n = 1.1
2658+
Ns = 60
2659+
T = 25.0
2660+
T_K = scipy.constants.convert_temperature(T, "Celsius", "Kelvin")
2661+
k_B_J_per_K = scipy.constants.value("Boltzmann constant")
2662+
q_C = scipy.constants.value("elementary charge")
2663+
nNsVth = n * Ns * k_B_J_per_K * T_K / q_C
2664+
2665+
def minimize_monkeypatched(*_, **__):
2666+
"""Return an unsuccessful solution from solver."""
2667+
return scipy.optimize.OptimizeResult(success=False)
2668+
2669+
# Monkey patch solver to return unsuccessfully.
26802670
monkeypatch.setattr(
2681-
"pvlib.pvsystem._negative_total_power", ZeroDivisionError
2671+
scipy.optimize,
2672+
"minimize",
2673+
minimize_monkeypatched,
26822674
)
26832675

2684-
photocurrent = inputs["photocurrent"]
2685-
saturation_current = inputs["saturation_current"]
2686-
resistance_series = inputs["resistance_series"]
2687-
resistance_shunt = inputs["resistance_shunt"]
2688-
q_C = scipy.constants.value("elementary charge")
2689-
k_B_J_per_K = scipy.constants.value("Boltzmann constant")
2690-
T_K = scipy.constants.convert_temperature(inputs["T"], "Celsius", "Kelvin")
2691-
nNsVth = inputs["n"] * inputs["Ns"] * k_B_J_per_K * T_K / q_C
2692-
26932676
with pytest.raises(RuntimeError) as e_info:
26942677
pvsystem.max_power_point_mismatched(
26952678
photocurrent,

0 commit comments

Comments
 (0)