Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/sphinx/source/whatsnew/v0.16.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -83,4 +87,5 @@ Contributors
* :ghuser:`bfs2021`
* :ghuser:`xiajiahang`
* Jakub Krasuski (:ghuser:`Blizzeq`)
* :ghuser:`G26karthik`
* Dylan Pulver (:ghuser:`dylanpulver`)
40 changes: 28 additions & 12 deletions pvlib/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,27 @@ def schlick(aoi):
return iam


def _schlick_diffuse_sky(surface_tilt):
# Eq 4 in [2] of schlick_diffuse
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
)
# As surface_tilt approaches 180 degrees, the sum in parentheses above and
# (1 + cosB) both approach zero, so float64 rounding errors in the sum are
# magnified by the division. Within 0.3 degrees of 180, return the first
# three terms of the Taylor series of Eq 4 about 180 degrees instead. At
# 0.3 degrees from 180, both are accurate to about 1e-8 (relative).
h = np.radians(180 - surface_tilt)
series = h * (4800 - 2025*np.pi*h + 4048*h**2) / (540*np.pi)
return np.where(np.abs(180 - surface_tilt) < 0.3, series, cuk)


def schlick_diffuse(surface_tilt):
r"""
Determine the incidence angle modifiers (IAM) for sky diffuse and
Expand Down Expand Up @@ -894,6 +915,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
-------
Expand Down Expand Up @@ -947,19 +969,13 @@ 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)

cug = np.where(surface_tilt < 1e-6, 0, cug)
# relative transmittance of ground-reflected radiation by PV cover.
# Eq 6 in [2] is mathematically equal to Eq 4 evaluated at
# 180 - surface_tilt. Computing it that way avoids the rounding problem
# Eq 6 has near 0 degrees.
cug = _schlick_diffuse_sky(180 - surface_tilt)

# respect input types:
if np.isscalar(surface_tilt):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,29 @@ def test_schlick_diffuse():
rtol=1e-6)


def test_schlick_diffuse_near_0_and_180():
# Eq 4 near 180 degrees and Eq 6 near 0 degrees lose precision in
# float64; expected values were computed from them 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)

surface_tilt = np.array([0, 1e-4, 1e-3, 0.01, 0.1, 0.5])
expected_ground = np.array([0, 4.93826018180e-6, 4.93815737467e-5,
4.93712941647e-4, 4.92686113684e-3,
2.44073643107e-2])
actual_iam = _iam.schlick_diffuse(surface_tilt)
assert_allclose(actual_iam['ground'], expected_ground, 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}),
Expand Down