I have had success in using WhenEvent
to reset or change a variable within NDSolve
with ordinary differential equations. My working, functioning MWE is:
Clear[usol, u, t, x, λ];
usol = NDSolveValue[
{
D[u[t, x], t] == λ[t, x] D[u[t, x], x, x],
u[0, x] == 0,
u[t, 0] == Sin[t],
u[t, 5] == 0,
λ[0, x] == 1,
WhenEvent[t > 5, λ[t, x] -> 1.25*λ[t, x]]
},
u,
{t, 0, 10},
{x, 0, 5},
DiscreteVariables -> λ]
Plot3D[usol[t, x], {t, 0, 10}, {x, 0, 5}, PlotRange -> All]
NIntegrate[usol[1, x], {x, 0, 5}]) (*This evaluates to 0.669665*)
However, I would like to use WhenEvent
to perform an operation such as StopIntegration
or changing a variable when a calculation such as area under the curve represented by the dependent variable attains a certain value. For this, I tried:
Clear[usol, u, t, x, λ];
usol = NDSolveValue[
{
D[u[t, x], t] == λ[t, x] D[u[t, x], x, x],
u[0, x] == 0,
u[t, 0] == Sin[t],
u[t, 5] == 0,
λ[0, x] == 1,
WhenEvent[NIntegrate[usol[t, x], {x, 0, 5}] > 0.1, end = t;
"StopIntegration"]
},
u,
{t, 0, 10},
{x, 0, 5},
DiscreteVariables -> λ]
However, I am beset with a slew of error messages in this latter case. The error message starts with:
The integrand usol[0,x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,5}}
The other question here on WhenEvent
and NDSolve
for partial differential equations did not have similar errors.
I am not sure what is going wrong; the operation NIntegrate[usol[t, x], {x, 0, 5}] < 1
should have "ended" the NDSolve Integration at t=1 at the least as in my functioning MWE.
usol
untilNDSolveValue
ends. So, you certainly can't use it insideNDSolveValue
$\endgroup$u
and include that equation insideNDSolve
. I'm not sure exactly how this would work. I might think on it. $\endgroup$usol
tou
insideNIntegrate
, it should work. $\endgroup$