Skip to content

Commit 2803eb8

Browse files
Fix ZeroDivisionError in simple_efficiency when load_loss=0 (#2646)
* Fix division-by-zero in transformer.simple_efficiency * fix flake8 * Add whatsnew note for simple_efficiency division-by-zero fix * fixing docs * Update pvlib/transformer.py --------- Co-authored-by: Kevin Anderson <kevin.anderso@gmail.com>
1 parent f3e73ba commit 2803eb8

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

docs/sphinx/source/whatsnew/v0.15.1.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Deprecations
1414

1515
Bug fixes
1616
~~~~~~~~~
17-
17+
* Fix a division-by-zero condition in
18+
:py:func:`pvlib.transformer.simple_efficiency` when ``load_loss = 0``.
19+
(:issue:`2645`, :pull:`2646`)
1820

1921
Enhancements
2022
~~~~~~~~~~~~
@@ -42,4 +44,5 @@ Maintenance
4244

4345
Contributors
4446
~~~~~~~~~~~~
47+
* Aman Srivastava (:ghuser:`aman-coder03`)
4548

pvlib/transformer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ def simple_efficiency(
111111
b = 1
112112
c = no_load_loss - input_power_normalized
113113

114-
output_power_normalized = (-b + (b**2 - 4*a*c)**0.5) / (2 * a)
114+
# alternative form of the quadratic equation to avoid
115+
# divide-by-zero when a == 0
116+
disc = (b*b - 4*a*c)**0.5
117+
output_power_normalized = 2*c / (-b - disc)
115118

116119
output_power = output_power_normalized * transformer_rating
117120
return output_power

tests/test_transformer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pandas as pd
2+
import pytest
23

34
from numpy.testing import assert_allclose
45

@@ -58,3 +59,24 @@ def test_simple_efficiency_known_values():
5859
*args),
5960
rating,
6061
)
62+
63+
64+
@pytest.mark.parametrize(
65+
"input_power, no_load_loss, load_loss, transformer_rating, expected",
66+
[
67+
(1000.0, 0.01, 0.0, 1000.0, 990.0),
68+
],
69+
)
70+
def test_simple_efficiency_zero_load_loss(
71+
input_power, no_load_loss, load_loss, transformer_rating, expected
72+
):
73+
# for load_loss = 0, the model reduces to:
74+
# P_out = P_in - L_no_load * P_nom
75+
result = transformer.simple_efficiency(
76+
input_power=input_power,
77+
no_load_loss=no_load_loss,
78+
load_loss=load_loss,
79+
transformer_rating=transformer_rating,
80+
)
81+
82+
assert_allclose(result, expected)

0 commit comments

Comments
 (0)