Automatic Differentiation
The Observer{T} element type is only constrained to Real, so solar positions are differentiable with ForwardDiff.jl.
using SolarPositionusing Datesusing ForwardDiffdt = DateTime(2024, 6, 21, 12, 0, 0)grad = ForwardDiff.gradient( x -> solar_position(Observer(x[1], x[2]), dt, SPA(), NoRefraction()).elevation, [45.0, 10.0],)2-element Vector{Float64}:
-0.9209770850903365
-0.27557068658077494Time is a DateTime, not a number, so derivatives with respect to time are not available through this route.
A refraction model's own parameters are differentiable inputs too:
ForwardDiff.derivative( p -> solar_position(Observer(45.0, 10.0), dt, PSA(), HUGHES(p, 10.0)).apparent_elevation, 101325.0,) # degrees of apparent elevation per pascal6.732119552420794e-8Interpolated differentiates as well.
DifferentiationInterface
Because the differentiability comes from the code being generic rather than from any AD specific hooks, backend agnostic tooling composes with it out of the box. DifferentiationInterface.jl lets you write the gradient call once and swap the backend freely. Here the ForwardDiff and finite difference backends agree to the finite difference accuracy:
import DifferentiationInterface as DIimport FiniteDifff(x) = solar_position(Observer(x[1], x[2]), dt, SPA(), NoRefraction()).elevationg_ad = DI.gradient(f, DI.AutoForwardDiff(), [45.0, 10.0])g_num = DI.gradient(f, DI.AutoFiniteDiff(), [45.0, 10.0])(g_ad, maximum(abs.(g_ad .- g_num)))([-0.9209770850903365, -0.27557068658077494], 1.9860470074073433e-7)Example: optimizing a solar panel orientation
Gradient ascent on a plane-of-array irradiance proxy finds the best fixed tilt and azimuth for a site. The objective sums the cosine of the angle of incidence over one year of solar positions whenever the sun is up:
obs = Observer(45.0, 10.0)times = collect(DateTime(2024, 1, 1):Hour(1):DateTime(2024, 12, 31))year = solar_position(obs, times, PSA(), NoRefraction())function annual_irradiance(angles) tilt, panel_azimuth = angles total = zero(eltype(angles)) for p in year p.elevation > 0 || continue cos_aoi = cosd(p.zenith) * cosd(tilt) + sind(p.zenith) * sind(tilt) * cosd(p.azimuth - panel_azimuth) total += max(zero(cos_aoi), cos_aoi) end return totalendfunction optimize(x) for _ in 1:100 g = ForwardDiff.gradient(annual_irradiance, x) x = x + g / sqrt(sum(abs2, g)) end return xendbest = optimize([10.0, 150.0])round.(best; digits = 1)2-element Vector{Float64}:
40.9
181.5The result lands near the textbook rule of thumb for the site at 45°N: tilt a little below the latitude, facing just about due south, and it collects roughly 31 % more annual irradiance than a flat panel.
Example: a horizontal single axis tracker
A horizontal single axis tracker rotates the panel about a horizontal axis to follow the sun east to west:
function cos_aoi_tracked(p, axis_azimuth; limit = 60.0) R = clamp(atand(tand(p.zenith) * sind(p.azimuth - axis_azimuth)), -limit, limit) panel_azimuth = axis_azimuth + (R < 0 ? -90 : 90) return cosd(p.zenith) * cosd(R) + sind(p.zenith) * sind(abs(R)) * cosd(p.azimuth - panel_azimuth)enddaylight = [p for p in year if p.elevation > 0]tracked(axis) = sum(max(zero(axis), cos_aoi_tracked(p, axis)) for p in daylight)function optimize_axis(x) for i in 1:40 g = ForwardDiff.derivative(tracked, x) x += g / abs(g) * max(0.1, 40.0 / (i + 10)) end return xendaxis = optimize_axis(150.0)(axis = round(axis; digits = 1), gain_vs_fixed = round(tracked(axis) / annual_irradiance(best); digits = 3))(axis = 180.0, gain_vs_fixed = 1.383)We find that the optimum axis azimuth is 180°, so that the axis runs north to south.