From e83ec9bd186762165e57aae84759d8b77656e62a Mon Sep 17 00:00:00 2001 From: G26Karthik Date: Mon, 9 Nov 2026 00:25:22 +0530 Subject: [PATCH 1/3] Fix inf and nan from iam.schlick_diffuse at surface_tilt=180 Eq 4 for the sky term divides by (1 + cos(tilt)), which is 0/0 for a surface facing straight down. Return 0 (no visible sky) there, as is already done for the ground term at tilt 0. The ground term then follows from Eq 6 and equals 20/21, the sky value at tilt 0. --- docs/sphinx/source/whatsnew/v0.16.2.rst | 3 +++ pvlib/iam.py | 15 ++++++++++----- tests/test_iam.py | 11 +++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/docs/sphinx/source/whatsnew/v0.16.2.rst b/docs/sphinx/source/whatsnew/v0.16.2.rst index 5893d8bf46..d6aa056deb 100644 --- a/docs/sphinx/source/whatsnew/v0.16.2.rst +++ b/docs/sphinx/source/whatsnew/v0.16.2.rst @@ -21,6 +21,8 @@ Bug fixes is scalar (:issue:`2881`, :pull:`2882`) * :py:func:`pvlib.scaling.wvm` no longer returns all NaNs when ``positions`` contains a single site (:issue:`2895`, :pull:`2896`) +* :py:func:`pvlib.iam.schlick_diffuse` no longer returns ``inf`` and ``nan`` + for ``surface_tilt=180`` (:issue:`2939`) Enhancements ~~~~~~~~~~~~ @@ -80,3 +82,4 @@ Contributors * :ghuser:`bfs2021` * :ghuser:`xiajiahang` * Jakub Krasuski (:ghuser:`Blizzeq`) +* :ghuser:`G26karthik` diff --git a/pvlib/iam.py b/pvlib/iam.py index cc9c8daae8..8cda64b678 100644 --- a/pvlib/iam.py +++ b/pvlib/iam.py @@ -949,11 +949,16 @@ def schlick_diffuse(surface_tilt): # relative transmittance of sky diffuse radiation by PV cover: cosB = cosd(surface_tilt) sinB = sind(surface_tilt) - cuk = (2 / (np.pi * (1 + cosB))) * ( - (30/7)*np.pi - (160/21)*np.radians(surface_tilt) - (10/3)*np.pi*cosB - + (160/21)*cosB*sinB - (5/3)*np.pi*cosB*sinB**2 + (20/7)*cosB*sinB**3 - - (5/16)*np.pi*cosB*sinB**4 + (16/105)*cosB*sinB**5 - ) # Eq 4 in [2] + with np.errstate(divide='ignore', invalid='ignore'): + cuk = (2 / (np.pi * (1 + cosB))) * ( + (30/7)*np.pi - (160/21)*np.radians(surface_tilt) + - (10/3)*np.pi*cosB + (160/21)*cosB*sinB + - (5/3)*np.pi*cosB*sinB**2 + (20/7)*cosB*sinB**3 + - (5/16)*np.pi*cosB*sinB**4 + (16/105)*cosB*sinB**5 + ) # Eq 4 in [2] + + # a surface facing straight down sees no sky (Eq 4 is 0/0 there) + cuk = np.where(np.abs(surface_tilt - 180) < 1e-6, 0, cuk) # relative transmittance of ground-reflected radiation by PV cover: with np.errstate(divide='ignore', invalid='ignore'): # Eq 6 in [2] diff --git a/tests/test_iam.py b/tests/test_iam.py index 0287fd8fd2..7705167ae6 100644 --- a/tests/test_iam.py +++ b/tests/test_iam.py @@ -462,6 +462,17 @@ def test_schlick_diffuse(): rtol=1e-6) +def test_schlick_diffuse_upside_down(): + # facing down: no sky, and the ground term matches the sky term at 0 tilt + actual_iam = _iam.schlick_diffuse(np.array([0, 180 - 1e-9, 180])) + assert_allclose(actual_iam['sky'], [20 / 21, 0, 0]) + assert_allclose(actual_iam['ground'], [0, 20 / 21, 20 / 21]) + + actual_iam = _iam.schlick_diffuse(180.) + assert actual_iam['sky'] == 0 + assert_allclose(actual_iam['ground'], 20 / 21) + + @pytest.mark.parametrize('source,source_params,target,expected', [ ('physical', {'n': 1.5, 'K': 4.5, 'L': 0.004}, 'martin_ruiz', {'a_r': 0.174037}), From 078467bc16a442c7a436d6139e37dc2456612a54 Mon Sep 17 00:00:00 2001 From: G26Karthik Date: Thu, 12 Nov 2026 00:14:21 +0530 Subject: [PATCH 2/3] Widen the tilt cutoffs in schlick_diffuse to 1e-3 and state the tilt range --- pvlib/iam.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pvlib/iam.py b/pvlib/iam.py index 8cda64b678..1e3c1b6541 100644 --- a/pvlib/iam.py +++ b/pvlib/iam.py @@ -894,6 +894,7 @@ def schlick_diffuse(surface_tilt): surface_tilt : numeric Surface tilt angle measured from horizontal (e.g. surface facing up = 0, surface facing horizon = 90). [degrees] + Surface_tilt must be in the range [0, 180] Returns ------- @@ -958,13 +959,13 @@ def schlick_diffuse(surface_tilt): ) # Eq 4 in [2] # a surface facing straight down sees no sky (Eq 4 is 0/0 there) - cuk = np.where(np.abs(surface_tilt - 180) < 1e-6, 0, cuk) + cuk = np.where(np.abs(surface_tilt - 180) < 1e-3, 0, cuk) # relative transmittance of ground-reflected radiation by PV cover: with np.errstate(divide='ignore', invalid='ignore'): # Eq 6 in [2] cug = 40 / (21 * (1 - cosB)) - (1 + cosB) / (1 - cosB) * cuk - cug = np.where(surface_tilt < 1e-6, 0, cug) + cug = np.where(surface_tilt < 1e-3, 0, cug) # respect input types: if np.isscalar(surface_tilt): From 264c8954705e185e5195b434bda9bd299f031bf8 Mon Sep 17 00:00:00 2001 From: G26Karthik Date: Sat, 21 Nov 2026 21:32:43 +0530 Subject: [PATCH 3/3] Use the series of Eq 4 near 180 degrees in schlick_diffuse Near 180 degrees the bracket of Eq 4 cancels to about h**3 (h = 180 - tilt in radians) while 1 + cos(tilt) is about h**2 / 2, so float64 rounding in the bracket dominates: the result was off by 2% at 0.001 degrees from 180 and by a factor of 13 at 0.0001 degrees. Replace the cutoffs: within 0.3 degrees of 180 the sky term uses the first three terms of the series of Eq 4 in h, and the ground term (Eq 6) is computed as the sky term at 180 - tilt, which it equals exactly; this also avoids the cancellation in Eq 6 near 0. Against Eq 4 in 60-digit arithmetic the largest relative error of both terms is now below 1e-8. --- docs/sphinx/source/whatsnew/v0.16.2.rst | 4 ++- pvlib/iam.py | 44 +++++++++++++++---------- tests/test_iam.py | 15 ++++++--- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/docs/sphinx/source/whatsnew/v0.16.2.rst b/docs/sphinx/source/whatsnew/v0.16.2.rst index 5e1e437225..5d803161fe 100644 --- a/docs/sphinx/source/whatsnew/v0.16.2.rst +++ b/docs/sphinx/source/whatsnew/v0.16.2.rst @@ -22,7 +22,9 @@ Bug fixes * :py:func:`pvlib.scaling.wvm` no longer returns all NaNs when ``positions`` contains a single site (:issue:`2895`, :pull:`2896`) * :py:func:`pvlib.iam.schlick_diffuse` no longer returns ``inf`` and ``nan`` - for ``surface_tilt=180`` (:issue:`2939`) + for ``surface_tilt=180``, and is now accurate close to 0 and 180 degrees, + where the closed-form equations lose precision in floating point + (:issue:`2939`, :pull:`2942`) * Document the URL default :py:func:`pvlib.iotools.get_acis_station_data` actually uses. Its docstring named the GridData endpoint; should be StnData. (:pull:`2950`) diff --git a/pvlib/iam.py b/pvlib/iam.py index 1e3c1b6541..cfade83f41 100644 --- a/pvlib/iam.py +++ b/pvlib/iam.py @@ -864,6 +864,28 @@ def schlick(aoi): return iam +def _schlick_diffuse_sky(surface_tilt, down_angle): + # Eq 4 in [2] of schlick_diffuse; down_angle = 180 - surface_tilt is + # passed separately so that callers keep it exact near 0 + cosB = cosd(surface_tilt) + sinB = sind(surface_tilt) + with np.errstate(divide='ignore', invalid='ignore'): + cuk = (2 / (np.pi * (1 + cosB))) * ( + (30/7)*np.pi - (160/21)*np.radians(surface_tilt) + - (10/3)*np.pi*cosB + (160/21)*cosB*sinB + - (5/3)*np.pi*cosB*sinB**2 + (20/7)*cosB*sinB**3 + - (5/16)*np.pi*cosB*sinB**4 + (16/105)*cosB*sinB**5 + ) + # Near 180 degrees the bracket cancels to about h**3 while 1 + cosB is + # about h**2 / 2 (h = down_angle in radians), so float64 rounding in the + # bracket dominates. Use the series of Eq 4 in h there instead; at + # 0.3 degrees its truncation error and the rounding in Eq 4 are both + # about 5e-9 relative. + h = np.radians(down_angle) + cuk_near_180 = h * (4800 - 2025*np.pi*h + 4048*h**2) / (540*np.pi) + return np.where(np.abs(down_angle) < 0.3, cuk_near_180, cuk) + + def schlick_diffuse(surface_tilt): r""" Determine the incidence angle modifiers (IAM) for sky diffuse and @@ -948,24 +970,12 @@ def schlick_diffuse(surface_tilt): # weighting coefficient w set to 1.0 (so it is omitted) # relative transmittance of sky diffuse radiation by PV cover: - cosB = cosd(surface_tilt) - sinB = sind(surface_tilt) - with np.errstate(divide='ignore', invalid='ignore'): - cuk = (2 / (np.pi * (1 + cosB))) * ( - (30/7)*np.pi - (160/21)*np.radians(surface_tilt) - - (10/3)*np.pi*cosB + (160/21)*cosB*sinB - - (5/3)*np.pi*cosB*sinB**2 + (20/7)*cosB*sinB**3 - - (5/16)*np.pi*cosB*sinB**4 + (16/105)*cosB*sinB**5 - ) # Eq 4 in [2] - - # a surface facing straight down sees no sky (Eq 4 is 0/0 there) - cuk = np.where(np.abs(surface_tilt - 180) < 1e-3, 0, cuk) - - # relative transmittance of ground-reflected radiation by PV cover: - with np.errstate(divide='ignore', invalid='ignore'): # Eq 6 in [2] - cug = 40 / (21 * (1 - cosB)) - (1 + cosB) / (1 - cosB) * cuk + cuk = _schlick_diffuse_sky(surface_tilt, 180 - surface_tilt) - cug = np.where(surface_tilt < 1e-3, 0, cug) + # relative transmittance of ground-reflected radiation by PV cover. + # Eq 6 in [2] equals the sky term at 180 - surface_tilt; computing it + # that way avoids the cancellation in Eq 6 near 0 degrees. + cug = _schlick_diffuse_sky(180 - surface_tilt, surface_tilt) # respect input types: if np.isscalar(surface_tilt): diff --git a/tests/test_iam.py b/tests/test_iam.py index 7705167ae6..47ea4952ed 100644 --- a/tests/test_iam.py +++ b/tests/test_iam.py @@ -463,10 +463,17 @@ def test_schlick_diffuse(): def test_schlick_diffuse_upside_down(): - # facing down: no sky, and the ground term matches the sky term at 0 tilt - actual_iam = _iam.schlick_diffuse(np.array([0, 180 - 1e-9, 180])) - assert_allclose(actual_iam['sky'], [20 / 21, 0, 0]) - assert_allclose(actual_iam['ground'], [0, 20 / 21, 20 / 21]) + # near 180 degrees Eq 4 cancels in float64; expected values were + # computed from Eq 4 with 60-digit arithmetic + surface_tilt = np.array([179.5, 179.9, 179.99, 179.9999, 179.999999, 180]) + expected_sky = np.array([2.44073643107e-2, 4.92686113684e-3, + 4.93712941647e-4, 4.93826018196e-6, + 4.93827147824e-8, 0]) + actual_iam = _iam.schlick_diffuse(surface_tilt) + assert_allclose(actual_iam['sky'], expected_sky, rtol=1e-8) + # the ground term at tilt b equals the sky term at 180 - b + actual_iam = _iam.schlick_diffuse(180 - surface_tilt) + assert_allclose(actual_iam['ground'], expected_sky, rtol=1e-8) actual_iam = _iam.schlick_diffuse(180.) assert actual_iam['sky'] == 0