Make a variable as a global variable within a function

From here:

  • Double check if you’re doing anything that violates assumptions of being an ODE. As an ODE, the right-hand side f function should always give you the same result: u' = f(u,p,t) needs to be uniquely defined. These issues are just fundamental to the mathematics: if you do these things in f , then f no longer defines an ODE so of course it cannot be solved!
  • If you put randomness into f , then the adaptivity will think your ODE is being solved at a high error because the derivative keeps changing, and therefore it will fail and hit dtmin trying to reduce the randomness to zero (if you do need randomness, use an SDE or RODE solver).
  • If your f function is modifying u , then calling f with different stepsizes is not deterministic and the solver is likely to fail. If you need to do this, you should be using a callback.
  • If your f function is caching values from a previous step, just think about what this means. If you change dt , then you’re changing f . In that sense, u' is no longer defined since it’s now dependent on how it’s being solved! Even worse, adaptive ODE solvers do not always move forwards in time: sometimes they try t + dt1 before trying t + dt2 and choosing what to do. So if you’re assuming that the cached values are from the last step, that’s not actually the case: those values can be coming from a fake (too incorrect according to error estimates) future!