WhenEvent and Resetting of Variable in PDE when operation succeeds [closed]

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.

• You can't use usol until NDSolveValue ends. So, you certainly can't use it inside NDSolveValue Feb 6, 2016 at 4:56
• @Dr.belisarius So, how do I proceed? I am sorry but I do not have a clue. What you say makes sense. Feb 6, 2016 at 4:58
• Probably include an auxiliary function whose derivative is u and include that equation inside NDSolve. I'm not sure exactly how this would work. I might think on it. Feb 6, 2016 at 5:05
• I think if you just change usol to u inside NIntegrate, it should work. Feb 6, 2016 at 20:45
• I'm voting to close this question as off-topic because it was solved in a comment Feb 22, 2016 at 3:20