I am setting up a rather complicated function that i wish to solve as an ODE. Preferably i want to use a solver which is accessible in most python environments so i am trying to use scipy.integrate.ode
.
The Problem is that my function produces a lot of values which i want to extract at each iteration.
Here is an example: I want to get someValues
at each completed iteration of r.integrate()
and save them somewhere:
from scipy.integrate import ode
def f(t, y):
someValues = [10,5,4]
return -1
y0 = 1
r = ode(f)
r.set_initial_value(y0, 0)
dt = 0.1
while r.successful() and r.y[0] >= 0:
r.integrate(r.t + dt)
# print(someValues)
However the solver forbids f()
from returning additional values.
Since it is not necessarily clear how the solver will acces the function, it is also not a good idea to just push it out to a global variable from inside f()
.
While it is possible to recalculate each step of f()
, returning all values, after the solver is done, it would be preferrable to do it in one run for performance.