# WhenEvent detects only the first event

My code is based on the suggestion in this discussion by Michael E2 And when I'm using the same definitions the WhenEvent function locates only the first time the event occurs.

slopeα = 0.01; Tend = 10; L = 1;
us[m_, P_] := (1 + Tanh[m (P)])/2;
α1 = 1; α2 = 8; elementNum = 25; ΔPsnap = 0.25;
maxval[if_: InterpolatingFunction[___][x_]] := Max[if /. {x -> "ValuesOnGrid"}];
fnew[if : InterpolatingFunction[___][x_]] := ConstantArray[1, Length[if /. x -> "Grid"]];
opts = Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> (elementNum - 1)}};
Pup = 0.7; t0 = 1;
α1 = 1; α2 = 8;
sol1 = NDSolve[{D[P[x, t], t] - α[x, t] D[P[x, t], {x, 2}] == 0, P[x, 0] == 0,
P[0, t] == UnitStep[t - t0], P[L, t] == 0, α[x, 0] == α2,
D[α[x, t], t] == 0, WhenEvent[maxval[P[x, t]] >= Pup, Print[t]]},
{P[x, t], α[x, t]}, {t, 0, Tend}, {x, 0, L}, opts];
Manipulate[Plot[{P[x, t] /. sol1} /. {t -> tt}, {x, 0, L},
PlotRange -> {{0, L}, {0, 1}}, ImageSize -> Medium], {tt, 0, Tend}]

and the print output is 2.20493 although the value of $$P[x,t]$$ remains higher than $$Pup$$ for at all times.

Can't find the reason for this issue. Thanks a lot, Ofek.

• WhenEvent only detects the change from False->Trueof the condition ! – Ulrich Neumann Jan 4 at 10:41
• Oh, I'm surprised I've missed that, Thanks!! Maybe you know a way to "force" WhenEvent to check every step? – Ofek Peretz Jan 4 at 10:43
• The WhenEvent in your simulation doesn't affect NDSolve, so you could check the conditions after the simulation is completed!? – Ulrich Neumann Jan 4 at 10:47
• I have excluded the part that this effect takes place since its too long and maybe have some errors itself, so, unfortunately, that's now the case. – Ofek Peretz Jan 4 at 10:49
• If I understand your problem right the ode "changes" when the event occurs? If so you could define a DiscreteVariable ->switch and switch between the different ode's. – Ulrich Neumann Jan 4 at 11:05