From a03e2c5f75337e8363ae692b6e3392784d8baa7e Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Fri, 26 Feb 2021 21:32:39 -0800 Subject: [PATCH 1/9] use horner's method in disc --- pvlib/irradiance.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 3ec6b213f9..feed595755 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1402,24 +1402,22 @@ def _disc_kn(clearness_index, airmass, max_airmass=12): am = np.minimum(am, max_airmass) # GH 450 - # powers of kt will be used repeatedly, so compute only once - kt2 = kt * kt # about the same as kt ** 2 - kt3 = kt2 * kt # 5-10x faster than kt ** 3 - - bools = (kt <= 0.6) - a = np.where(bools, - 0.512 - 1.56*kt + 2.286*kt2 - 2.222*kt3, - -5.743 + 21.77*kt - 27.49*kt2 + 11.56*kt3) - b = np.where(bools, - 0.37 + 0.962*kt, - 41.4 - 118.5*kt + 66.05*kt2 + 31.9*kt3) - c = np.where(bools, - -0.28 + 0.932*kt - 2.048*kt2, - -47.01 + 184.2*kt - 222.0*kt2 + 73.81*kt3) + # Use Horner's method to compure polynomials efficiently + bools = (kt <= 0.6) + a = np.where(bools, + 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), + -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) + b = np.where(bools, + 0.37 + 0.962*kt, + 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) + c = np.where(bools, + -0.28 + kt*(0.932 - 2.048*kt), + -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) + delta_kn = a + b * np.exp(c*am) - Knc = 0.866 - 0.122*am + 0.0121*am**2 - 0.000653*am**3 + 1.4e-05*am**4 + Knc = 0.866 + am*(-0.122 + am*(0.0121 + am*(-0.000653 + 1.4e-05*am))) Kn = Knc - delta_kn return Kn, am From bf00382fb0c233b1b5aea57e1401b2cd1243112f Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Mon, 13 Mar 2023 12:42:46 -0700 Subject: [PATCH 2/9] Update v0.9.5.rst update whatsnew --- docs/sphinx/source/whatsnew/v0.9.5.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/sphinx/source/whatsnew/v0.9.5.rst b/docs/sphinx/source/whatsnew/v0.9.5.rst index c5d246310f..127b406ed1 100644 --- a/docs/sphinx/source/whatsnew/v0.9.5.rst +++ b/docs/sphinx/source/whatsnew/v0.9.5.rst @@ -26,13 +26,14 @@ Enhancements differentiable and bounded between zero and one. (:pull:`1179`) * Add ``model='gueymard2003'``, the airmass model used for REST and REST2, to :py:func:`~pvlib.atmosphere.get_relative_airmass`. (:pull:`1655`) - - * Added an optional ``model`` parameter to :py:func:`pvlib.bifacial.infinite_sheds.get_irradiance` and :py:func:`pvlib.bifacial.infinite_sheds.get_irradiance_poa` to enable use of the hay-davies sky diffuse irradiance model instead of the default isotropic model. (:pull:`1668`) +* Use [Horner's Method](https://en.wikipedia.org/wiki/Horner%27s_method) + to evaluate polynomials in :py:func:`~pvlib.irradiance.disc`, may + decrease runtime by 20%. (:issue:`1180`, :pull:`1183`) Bug fixes ~~~~~~~~~ From 5018e3b96004f57edea5e9b4c5fdfb80eaf40509 Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Mon, 13 Mar 2023 14:56:08 -0700 Subject: [PATCH 3/9] fix stickler, indentation in disc --- pvlib/irradiance.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 4a1576f85e..5859a3e380 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1482,17 +1482,16 @@ def _disc_kn(clearness_index, airmass, max_airmass=12): am = np.minimum(am, max_airmass) # GH 450 # Use Horner's method to compure polynomials efficiently - bools = (kt <= 0.6) - a = np.where(bools, - 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), - -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) - b = np.where(bools, - 0.37 + 0.962*kt, - 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) - c = np.where(bools, - -0.28 + kt*(0.932 - 2.048*kt), - -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) - + bools = (kt <= 0.6) + a = np.where(bools, + 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), + -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) + b = np.where(bools, + 0.37 + 0.962*kt, + 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) + c = np.where(bools, + -0.28 + kt*(0.932 - 2.048*kt), + -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) delta_kn = a + b * np.exp(c*am) From c87427b694c8cd1f060592cbe0f48379a2b5ed8e Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Mon, 13 Mar 2023 16:35:10 -0700 Subject: [PATCH 4/9] fix underindentation stickler issue in disc --- pvlib/irradiance.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 5859a3e380..082370de3c 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1484,14 +1484,14 @@ def _disc_kn(clearness_index, airmass, max_airmass=12): # Use Horner's method to compure polynomials efficiently bools = (kt <= 0.6) a = np.where(bools, - 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), - -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) + 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), + -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) b = np.where(bools, - 0.37 + 0.962*kt, - 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) + 0.37 + 0.962*kt, + 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) c = np.where(bools, - -0.28 + kt*(0.932 - 2.048*kt), - -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) + -0.28 + kt*(0.932 - 2.048*kt), + -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) delta_kn = a + b * np.exp(c*am) From d1cf1c188717f90d1899b96bab923fff3ea61048 Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Tue, 14 Mar 2023 22:56:26 -0700 Subject: [PATCH 5/9] use is_clear instead of bools in disc --- pvlib/irradiance.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 082370de3c..e9164000ee 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1481,15 +1481,15 @@ def _disc_kn(clearness_index, airmass, max_airmass=12): am = np.minimum(am, max_airmass) # GH 450 - # Use Horner's method to compure polynomials efficiently - bools = (kt <= 0.6) - a = np.where(bools, + is_clear = (kt <= 0.6) + # Use Horner's method to compute polynomials efficiently + a = np.where(is_clear, 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) - b = np.where(bools, + b = np.where(is_clear, 0.37 + 0.962*kt, 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) - c = np.where(bools, + c = np.where(is_clear, -0.28 + kt*(0.932 - 2.048*kt), -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) From a91f17727cb2596cfecb73075dd55f4d39075998 Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Wed, 15 Mar 2023 16:15:21 -0700 Subject: [PATCH 6/9] fix stickler indentation complaint for disc --- pvlib/irradiance.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index e9164000ee..1cc7e23aa4 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1483,13 +1483,16 @@ def _disc_kn(clearness_index, airmass, max_airmass=12): is_clear = (kt <= 0.6) # Use Horner's method to compute polynomials efficiently - a = np.where(is_clear, + a = np.where( + is_clear, 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) - b = np.where(is_clear, + b = np.where( + is_clear, 0.37 + 0.962*kt, 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) - c = np.where(is_clear, + c = np.where( + is_clear, -0.28 + kt*(0.932 - 2.048*kt), -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) From b80a1dfd9b142ed2fcb8fcce74da4b7f6bbecca6 Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Wed, 15 Mar 2023 16:17:37 -0700 Subject: [PATCH 7/9] fix stickler indent complaint in disc again --- pvlib/irradiance.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 1cc7e23aa4..995dc8ad52 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1484,17 +1484,17 @@ def _disc_kn(clearness_index, airmass, max_airmass=12): is_clear = (kt <= 0.6) # Use Horner's method to compute polynomials efficiently a = np.where( - is_clear, - 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), - -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) + is_clear, + 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), + -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) b = np.where( - is_clear, - 0.37 + 0.962*kt, - 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) + is_clear, + 0.37 + 0.962*kt, + 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) c = np.where( - is_clear, - -0.28 + kt*(0.932 - 2.048*kt), - -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) + is_clear, + -0.28 + kt*(0.932 - 2.048*kt), + -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) delta_kn = a + b * np.exp(c*am) From 76d0ee64c4821a57b24736b56c3a819a77093a09 Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Wed, 15 Mar 2023 17:53:35 -0700 Subject: [PATCH 8/9] change is_clear to is_cloudy, fix hyperlink for whatsnew rst --- docs/sphinx/source/whatsnew/v0.9.5.rst | 2 +- pvlib/irradiance.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/sphinx/source/whatsnew/v0.9.5.rst b/docs/sphinx/source/whatsnew/v0.9.5.rst index 127b406ed1..d9415c2038 100644 --- a/docs/sphinx/source/whatsnew/v0.9.5.rst +++ b/docs/sphinx/source/whatsnew/v0.9.5.rst @@ -31,7 +31,7 @@ Enhancements :py:func:`pvlib.bifacial.infinite_sheds.get_irradiance_poa` to enable use of the hay-davies sky diffuse irradiance model instead of the default isotropic model. (:pull:`1668`) -* Use [Horner's Method](https://en.wikipedia.org/wiki/Horner%27s_method) +* Use `Horner's Method`_ to evaluate polynomials in :py:func:`~pvlib.irradiance.disc`, may decrease runtime by 20%. (:issue:`1180`, :pull:`1183`) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 995dc8ad52..83527c7604 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1481,18 +1481,18 @@ def _disc_kn(clearness_index, airmass, max_airmass=12): am = np.minimum(am, max_airmass) # GH 450 - is_clear = (kt <= 0.6) + is_cloudy = (kt <= 0.6) # Use Horner's method to compute polynomials efficiently a = np.where( - is_clear, + is_cloudy, 0.512 + kt*(-1.56 + kt*(2.286 - 2.222*kt)), -5.743 + kt*(21.77 + kt*(-27.49 + 11.56*kt))) b = np.where( - is_clear, + is_cloudy, 0.37 + 0.962*kt, 41.4 + kt*(-118.5 + kt*(66.05 + 31.9*kt))) c = np.where( - is_clear, + is_cloudy, -0.28 + kt*(0.932 - 2.048*kt), -47.01 + kt*(184.2 + kt*(-222.0 + 73.81*kt))) From 7cb274a82a773d92bd99be43e871a475bd4d0f5c Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Wed, 15 Mar 2023 19:17:21 -0700 Subject: [PATCH 9/9] Update v0.9.5.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix link to Horner’s method --- docs/sphinx/source/whatsnew/v0.9.5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/sphinx/source/whatsnew/v0.9.5.rst b/docs/sphinx/source/whatsnew/v0.9.5.rst index d9415c2038..8eed7b369e 100644 --- a/docs/sphinx/source/whatsnew/v0.9.5.rst +++ b/docs/sphinx/source/whatsnew/v0.9.5.rst @@ -31,7 +31,7 @@ Enhancements :py:func:`pvlib.bifacial.infinite_sheds.get_irradiance_poa` to enable use of the hay-davies sky diffuse irradiance model instead of the default isotropic model. (:pull:`1668`) -* Use `Horner's Method`_ +* Use `Horner's Method `_ to evaluate polynomials in :py:func:`~pvlib.irradiance.disc`, may decrease runtime by 20%. (:issue:`1180`, :pull:`1183`)