1
$\begingroup$

Bug introduced after 10.0, in or before 10.3, persisting through 13.2.1


The exact problem I am dealing with has already been asked and answered here.

Unfortunately the solution no longer seems to work. In fact when I run the solution

equation = x'[t] + (x[t] - λ[t]) == 0;

sol = NDSolveValue[{equation, x[0] == 0, λ[0] == 1, 
                    WhenEvent[x'[t] == 0.25, λ[t] -> x[t]]}, 
                   x, {t, 0, 5}, DiscreteVariables -> {λ}]

I get nothing back. There is no error indication. Mathematica simply stops after a single integration step and returns nothing. I have already checked that the solution works fine if I simply replace in the condition inside WhenEvent with x[t] == 0.25 instead of the first derivative.

My speculation is that at some point someone decided to remove the possibility of using a WhenEvent with a condition that depends on a discrete variable when the action affects the discrete variable itself. Is there some workaround to this? It is unavoidable that my condition must have x'[t] in it.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

The code works fine in v9.0.1, but behaves as OP describes at least since v12.3.1. I believe this is a bug related to pre-processing, because

equation = x'[t] + (x[t] - λ[t]) == 0;
sol = NDSolveValue[{equation, x[0] == 0, λ[0] == 1, 
   WhenEvent[x'[t] == 0.25, λ[t] -> x[t]]}, x, {t, 0, 5}, 
  DiscreteVariables -> {λ}, SolveDelayed -> True]

fixes the problem. (SolveDelayed is red, but don't worry. If you don't like it, use the equivalent Method -> {EquationSimplification -> Residual} instead. )

$\endgroup$
2
  • $\begingroup$ Thanks for the proposal! I will give that a shot. I think the method "Residual" doesn't use Parallelization automatically right? I am generally using "ExplicitRungeKutta" because that makes the calculation 8-fold faster. The example above was just a toy example, In reality I am evolving thousands of coupled equations each of which has thousands of terms. Any tips for doing this calculation faster? I tried replacing the derivative using the eoms and that also seems to work but at a cost of speed. $\endgroup$
    – pip
    Commented Feb 28, 2023 at 8:30
  • $\begingroup$ @pip AFAIK parallelization is not that relevant here. Currently the ODE solver of NDSolve doesn't parallelize, either. What's parallelized is the basic arithmetic operation when the system is large. See discussion in mathematica.stackexchange.com/q/208784/1871 Also, generally the ExplicitRungeKutta method isn't that efficient according to my (limited) experience, but who knows. For large system, compilation is often a good choice, but that's relatively advanced topic, see e.g. mathematica.stackexchange.com/q/208762/1871 $\endgroup$
    – xzczd
    Commented Feb 28, 2023 at 8:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.