3
$\begingroup$

This is a problem I noticed when trying to answer this question. Just consider the following toy example:

test = ParametricNDSolveValue[{x'[t] == 1, x[0] == a, WhenEvent[t == 1, Sow@a]}, 
   x, {t, 0, 2}, a];
test@1; // Reap
(* {Null, {{a$224638}}} *)

This behavior becomes annoying when one wants to group the extracted value using the parameter a as a tag i.e. the 2nd argument of Sow.

How to understand this behavior? Is it possible to Sow the parameter value inside WhenEvent? If not, what's the best workaround for it?

$\endgroup$
  • 2
    $\begingroup$ When test@1 is executed, at some point a Function is constructed and it appears the argument a$224638 is rewritten in the standard way by adding $; namely the new argument is a$224638$. Thus the value of a fails to be injected into Sow[a$224638]. WRI might consider it a bug, if you report it. $\endgroup$ – Michael E2 Jan 12 '18 at 15:20
4
$\begingroup$

Here is a workaround.

test = ParametricNDSolveValue[{x'[t] == 1, x[0] == a, b[0] == a, 
    WhenEvent[t == 1, Sow@b[t]]}, x, {t, 0, 2}, {a}, DiscreteVariables -> b];
Reap[test@1;][[2, 1, 1]]
(* 1. *)

and test@2 returns 2., as desired.

$\endgroup$
4
$\begingroup$

another workaround is to not use the parametric form:

test[a_] := NDSolveValue[{x'[t] == 1, x[0] == a, 
              WhenEvent[t == 1, Sow@a]}, x, {t, 0, 2}];
test[1]; // Reap

{Null, {{1}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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