diff --git a/docs/sphinx/source/whatsnew/v0.16.2.rst b/docs/sphinx/source/whatsnew/v0.16.2.rst index fb3ff54f76..5d803161fe 100644 --- a/docs/sphinx/source/whatsnew/v0.16.2.rst +++ b/docs/sphinx/source/whatsnew/v0.16.2.rst @@ -21,6 +21,10 @@ 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``, 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`) @@ -83,4 +87,5 @@ Contributors * :ghuser:`bfs2021` * :ghuser:`xiajiahang` * Jakub Krasuski (:ghuser:`Blizzeq`) +* :ghuser:`G26karthik` * Dylan Pulver (:ghuser:`dylanpulver`) diff --git a/pvlib/iam.py b/pvlib/iam.py index cc9c8daae8..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 @@ -894,6 +916,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 ------- @@ -947,19 +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) - 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] - - # 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-6, 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 0287fd8fd2..47ea4952ed 100644 --- a/tests/test_iam.py +++ b/tests/test_iam.py @@ -462,6 +462,24 @@ def test_schlick_diffuse(): rtol=1e-6) +def test_schlick_diffuse_upside_down(): + # 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 + 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}),