# How can I use WhenEvent to stop the integration? [duplicate]

I have a questions about stopping the integration. I calculate the function as the following. It gives the message: "At NN=58.7035438......, the step size is effectively zero; singularity or stiff system suspected.". I want to stop the numerical integration when the step size is effectively zero. I know there is a function called WhenEvent. But, there are no items specifying the step size. Any ideas for solving this? (If there are ideas other than using WhenEvent, it is also welcomed. ) The code is the following. Thank you very much for your sincere help.

(* Define the numerical solving *)
numsol[M_, TRinitial_, Tiinitial_, TRinitialrate_, Tiinitialrate_, \[Alpha]_, ABS\[Mu]_, \[Iota]1_, KK_, Nini_, Nend_] :=NDSolve[{(*Equation of motion of TR[NN]*)
(ParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN],(*CC*) (ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) TR''[
NN] - (ParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN],(*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) (TR'[NN]/(2 M^2))*
(SigmaPrimeSquare[M, \[Alpha], TR[NN], TR'[NN],
Ti'[NN]]) + (ParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN], (*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) (Christ111[
TR[NN]] TR'[NN] TR'[NN] +
Christ112[TR[NN]] TR'[NN] Ti'[NN] +
Christ121[TR[NN]] Ti'[NN] TR'[NN] +
Christ122[TR[NN]] Ti'[NN] Ti'[NN]) + (3) (ParametrizedVVReal[
M, \[Alpha], Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN], (*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) TR'[
NN] + (3 M^2 -
0.5 (SigmaPrimeSquare[M, \[Alpha], TR[NN], TR'[NN],
Ti'[NN]])) (InvMetric[M, \[Alpha], TR[NN]][[1, 1]]*
DTRParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[NN]] +
InvMetric[M, \[Alpha], TR[NN]][[1, 2]]*
DTiParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[NN]]) == 0,
(*Equation of motion of Ti[
NN]*)
(ParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN],(*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) Ti''[
NN] - (ParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN],(*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) (Ti'[NN]/(2 M^2))*
(SigmaPrimeSquare[M, \[Alpha], TR[NN], TR'[NN],
Ti'[NN]]) + (ParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN],(*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) (Christ211[
TR[NN]] TR'[NN] TR'[NN] +
Christ212[TR[NN]] TR'[NN] Ti'[NN] +
Christ221[TR[NN]] Ti'[NN] TR'[NN] +
Christ222[TR[NN]] Ti'[NN] Ti'[NN]) + (3) (ParametrizedVVReal[
M, \[Alpha], Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[
NN],(*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] -
3) \[Iota]1))^(\[Alpha] - 1)]) Ti'[
NN] + (3 M^2 -
0.5 (SigmaPrimeSquare[M, \[Alpha], TR[NN], TR'[NN],
Ti'[NN]])) (InvMetric[M, \[Alpha], TR[NN]][[2, 1]]*

DTRParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[NN]] +
InvMetric[M, \[Alpha], TR[NN]][[2, 2]]*
DTiParametrizedVVReal[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[NN]]) == 0,
(*Equation of motion of TSS*)

TSS'[NN] ==
BBeta[M, \[Alpha], Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[NN], TR'[NN],
Ti'[NN],(*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] - 3) \[Iota]1))^(\[Alpha] -
1)] TSS[NN],
(* Equation of motion of TRS *)

TRS'[NN] ==
AAlpha[M, \[Alpha],
Sqrt[\[Iota]1^2 + (KK*\[Iota]1)^2]*ABS\[Mu]/2,
ABS\[Mu], \[Iota]1*ABS\[Mu]^2, (KK*\[Iota]1) ABS\[Mu]^2,
TR[NN], Ti[NN], TR'[NN],
Ti'[
NN],(*CC*)(ABS\[Mu]^2)*(\[Iota]1/(\[Alpha]*
M))*(((\[Alpha]^2 - 7 \[Alpha] +
4) M)/(-\[Alpha] (\[Alpha] - 3) \[Iota]1))^(\[Alpha] -
1)] TSS[NN],
(*Boundary condition*)
TR[Nini] == TRinitial,
Ti[Nini] == Tiinitial, TR'[Nini] == TRinitialrate,
Ti'[Nini] == Tiinitialrate, TSS[Nini] == 1, TRS[Nini] == 0},
{TR[NN], Ti[NN], TR'[NN], Ti'[NN], TSS[NN], TSS'[NN], TRS[NN],TRS'[NN]}, {NN, Nini, Nend}, AccuracyGoal -> 10, PrecisionGoal -> 10, Method -> {"StiffnessSwitching", Method -> {"Extrapolation", Automatic}}(*,WhenEvent[MaxSteps\[Rule] 1000,"StopIntegration"]*)];

(* a function of Plotting TR and Ti by using the above numsol *)

FieldValuesPlot[M_, TRinitial_, Tiinitial_, TRinitialrate_,
Tiinitialrate_, \[Alpha]_, ABS\[Mu]_, \[Iota]1_, KK_, Nini_, Nend_, color_] :=
Plot[Evaluate[{TR[NN], Ti[NN]} /.
numsol[M, TRinitial, Tiinitial, TRinitialrate,
Tiinitialrate, \[Alpha], ABS\[Mu], \[Iota]1, KK, Nini,
Nend]], {NN, Nini, Nend}, PlotRange -> {{Nini, Nend}, {-5, 200}},
AxesLabel -> {"N",
"\!$$\*SubscriptBox[\(T$$, $$R,I$$]\)/\!$$\*SubscriptBox[\(M$$, \$$pl$$]\)"}, BaseStyle -> {FontSize -> 30},
PlotLegends -> {"\!$$\*SubscriptBox[\(T$$, $$R$$]\)[N]",
"\!$$\*SubscriptBox[\(T$$, $$I$$]\)[N]"},
PlotStyle -> {{color, Thick}, {color, Dashed}}];

(* Final output using the field plot *)
FieldValuesPlot[1, 70, 85, 10^(-5), 10^(-5), 4, 3*10^(-4), 25, -5, 0, 60, Red]

• 1. Please provide a minimal example. 2. Why do you need WhenEvent to stop calculation? When ndsz warning pops up, the integration will stop automatically. 3. Is the system supposed to be stiff at some point? If the answer is no, probably you should double check the underlying model. Commented Oct 9, 2019 at 8:34
• Thank you very much for your answer. Since I want to pick the value of NN such that the step size is effectively zero, I want to stop the process once the step size tending to zero. How can I do so? (It does not matter whether I use WhenEvent or not, I just want to pick up the NN value such that the step size is very small. ) Commented Oct 9, 2019 at 13:03
• As mentioned above, for initial value problem, once ndsz warning pops up, NDSolve stops working automatically. As to the extraction of NN at the end time, have a look at the linked post. Also, you can extract the end time from the InterpolationFunction. You can check this post for more information. Commented Oct 9, 2019 at 13:50