You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
irradiance.aoi can return NaN when module orientation is perfectly aligned with solar position #1185
Describe the bug
I was playing with a dual-axis tracking mount with #1176 and found that when the modules are perfectly aligned with the sun (i.e. AOI should be exactly zero), floating point round-off can result in aoi projection values slightly greater than one, resulting in NaN aoi. This only happens for some perfectly-aligned inputs (for example tilt=zenith=20, azimuth=180 returns aoi=0 as expected).
notable that pyephem essentially clips based on aoi projection.
I found this stackexchange answer after some googling. I must have made a mistake because I couldn't get that formula to work. But the other answer does work (see page 15 of the linked reference for original source).
Cool, thanks! The problem was a missing sqrt. Here's an updated version with some additional tweaks so that it works for arrays in addition to scalars:
Testing the three functions with many random inputs (where solar position and module orientation are parallel) shows that the two methods from that SO post reliably return aoi=zero while the current pvlib function returns nan for ~3.7% of inputs. The downside is that the two SO functions are half as fast as the current pvlib function. Avoiding redundant conversions to radians (by not using cosd and sind) squeak a bit of extra speed but not much.
I don't love cutting execution speed in half, but it's still pretty fast. Ok with me to use one of these functions instead of np.clip. @cwhanse?
Edit to add: using this wouldn't fix the underlying issue of irradiance.aoi_projection returning values > 1. Is aoi_projection useful for anything other than calculating aoi?
I've been using clip for a long time. All these trig functions are numerical approximations and converting to and from radians doesn't help. I also use vector representations frequently, but I think it is entirely possible that these variations have other corner cases where they fail. In clip I trust. :)
Activity
wholmgren commentedon Mar 10, 2021
notable that pyephem essentially clips based on aoi projection.
I found this stackexchange answer after some googling. I must have made a mistake because I couldn't get that formula to work. But the other answer does work (see page 15 of the linked reference for original source).
I don't know what the best approach is, but maybe you can dig at it more.
kandersolar commentedon Mar 11, 2021
Cool, thanks! The problem was a missing sqrt. Here's an updated version with some additional tweaks so that it works for arrays in addition to scalars:
Click to expand!
Testing the three functions with many random inputs (where solar position and module orientation are parallel) shows that the two methods from that SO post reliably return aoi=zero while the current pvlib function returns nan for ~3.7% of inputs. The downside is that the two SO functions are half as fast as the current pvlib function. Avoiding redundant conversions to radians (by not using cosd and sind) squeak a bit of extra speed but not much.
Output:
I don't love cutting execution speed in half, but it's still pretty fast. Ok with me to use one of these functions instead of
np.clip. @cwhanse?Edit to add: using this wouldn't fix the underlying issue of
irradiance.aoi_projectionreturning values > 1. Isaoi_projectionuseful for anything other than calculatingaoi?cwhanse commentedon Mar 11, 2021
I'll take slow, correct and reliable over fast, mostly correct and not reliable.
mikofski commentedon Mar 11, 2021
do a benchmark before you merge this so we can document when the performance hit occured
cwhanse commentedon Mar 11, 2021
I have a mild preference for the
aoiversion, it is somewhat more readable and looks more likely to be further optimized.adriesse commentedon Mar 17, 2021
I've been using clip for a long time. All these trig functions are numerical approximations and converting to and from radians doesn't help. I also use vector representations frequently, but I think it is entirely possible that these variations have other corner cases where they fail. In clip I trust. :)