I haven't seen your particular PDEs, but I don't think your can numerically evaluate the target functions in the NDSolve
scope, as the numerical solutions (expressed as InterpolatingFunction
objects) hasn't been constructed.
If it were me, I may consider doing the plotting afterward:
sol = NDSolve[{
y''[t] == -1, y[0] == 1, y'[0] == 0,
WhenEvent[
y[t] == 0,
tEnd = t; "StopIntegration"
]
}, y, {t, 0, 2}];
Plot[y[t] /. sol, {t, 0, tEnd}]
Update:
I think I overlooked the question, and the real difficulty here might be related to the numerical method of lines, which is the way how NDSolve
deals with this kind of PDEs.
According to the documentation, with the NML, the directions of boundary-value problem are considered as space dimensions, and the direction of initial-value problem is considerd as time dimension. All space dimensions are discretized, but the numerical integrations are only taken along the time dimension. So it's many lines start from some points in the space space at time zero, and evolve over time, thus the internal integrations are all one-dimensional and the "StopIntegration"
action can only be taken along time. This is the reason you got the error in your comment.
One possible (and only partial) solution is specifying all space variables in WhenEvent
, leaving only the time variable. Here is a simple demonstration:
Module[{sol},
sol = NDSolve[{
D[u[t, x], t] == D[u[t, x], x, x],
u[0, x] == 0,
u[t, 0] == Sin[t], u[t, 5] == 0,
(* the space variable x, i.e. #, is fixed value: *)
WhenEvent[u[t, #] - .1 == 0,
tEnd = t; "StopIntegration"
]
},
u, {t, 0, 10}, {x, 0, 5}];
Plot3D[
Evaluate[-(#/10) + u[t, x] /. sol], {t, 0, tEnd}, {x, 0, 5},
PlotRange -> All,
PlotStyle -> ColorData["Rainbow"][1 - Rescale[#, {0, 5}]],
Mesh -> False]
] & /@ Range[0, 5, .5] //
Show[#, AxesLabel -> (Style[#, 20, Italic, Bold] & /@ {t, x, u}), Lighting -> "Neutral"] &

Method -> {"EventLocator", "Event" -> phi[x,t]==0}]
$\endgroup$WhenEvent
is the new way to do it, introduced in v9. It should do everything that EventLocator did and more. Sorry, no time to check the details right now ... $\endgroup$t
axis (Mathematica uses the method of lines), so asking whenf[x,t]==0
is not a well defined question. I guess you meanf[x,t]==0
for allx
, but that is unlikely to happen. So the condition should probably be something else. $\endgroup$