A few weeks ago I asked here about a Julia simulator for small off-grid villages. One piece of it is a plain ODE with four stocks: a battery, a water tank, a food store, a compute buffer. Each flow is clipped with min/max so nothing can take out more than a stock holds or put in more than fits. That makes the right-hand side continuous, but with a bend a little below capacity, where the cap on inflow starts to bite.
On one day the battery should have charged up to full. Instead it went down, even though there was more sun than demand, and ended at 2814.6 kWh instead of 3000, with retcode Success. As far as I can tell, while the battery charges at a steady rate the solution is a straight line, the error estimate is close to zero, and Tsit5 keeps stretching the step until one step covers most of the day. That step starts below the bend and its later stages land past full, where inflow is cut off, so it pulls the battery down and the error estimate doesn't notice.
Here's the day on its own:
using OrdinaryDiffEq # wrong on OrdinaryDiffEq 7.8.1; on 6.107.0 + OrdinaryDiffEqCore 3.1.0 this day is fine (other days fail there)
τ = 0.05 # days over which a stock can fill or empty at its bounds
caps = [3000.0, 3e5, 9e7, 7200.0]; ins = [1821.6416596454678, 6e4, 9e5, 3600.0]
dems = [1500.0, 45000.0, 750000.0, 2400.0]; loss = [0.001, 0.002, 0.0005, 0.0]
function rate(u, cap, in, dem)
out = max(0.0, min(dem, in + u/τ)) # can't take out more than there is
return max(0.0, min(in, out + (cap - u)/τ)) - out # can't put in more than fits
end
f!(du, u, p, t) = (du .= rate.(u, caps, ins, dems .+ loss .* max.(0.0, u))) # in-place; out-of-place passes on this day
u0 = [2854.0445204092402, 299999.9999999993, 7.775874075072253e7, 7199.999999999989] # every digit matters: 3e5 for the tank passes
sol = solve(ODEProblem(f!, u0, (0.0, 1.0)), Tsit5(); reltol=1e-6, abstol=1e-6)
println(sol.retcode, ", battery at end of day: ", sol.u[end][1]) # should be ~3000
This day is right on the edge, though. Change the last digit of the tank's start value, or write f as out-of-place, and this day comes out at 3000 while some other day in the batch goes wrong instead. The 2814.6 comes from a Mac with an Apple chip (aarch64), while the same code under x86 emulation (Rosetta) gives 3000, and I haven't tried it on a native x86 machine or worked out why it changes (fused multiply-add is my only suspect so far). Across my whole batch it's 9 days out of about 1.2 million, so if you run it and get 3000, that's expected and doesn't mean it's fixed.
I tried two fixes. Capping dtmax at τ fixes that day and held up across a big batch of runs, but it roughly triples the steps over a simulated year. A ContinuousCallback on each bend, with an empty affect! that only makes the step stop there and no dtmax, also fixes that day and costs about the same as doing nothing. But in the bigger batch it still misses a few days, some about as badly as no fix at all. On the days I looked at, the bend condition had the same sign at the start and at the end of the long step.
So is dtmax the right answer for a right-hand side like this, or is there a better way to tell the solver where the bends are when I don't know ahead of time when a stock will hit them? I'm not sure whether that's a bug, a known limit of error control on non-smooth problems, or just me using the solver wrong.