neurophysik / jitcode

Just-in-Time Compilation for Ordinary Differential Equations
Other
62 stars 9 forks source link

set_parameters requires manual unpacking of list #18

Closed JohnGoertz closed 4 years ago

JohnGoertz commented 4 years ago

(JITCODE v. 1.4.0)

When specifying control parameters for an ODE through jitcode(...,control_pars=[...]), a list is passed containing all the symbols to act as control parameters. However, set_parameters doesn't accept a list as input.

Using the Lotka-Volterra oscillator as an example:

γ = se.Symbol('Y')#0.6
φ = 1.0
ω = 0.5
ν = 0.5

R,B = dynvars = [ y(i) for i in range(2) ]

lotka_volterra_diff = {
    B:  γ*B - φ*R*B,
    R: -ω*R + ν*R*B,
    }

ODE = jitcode(lotka_volterra_diff, control_pars=[γ])
ODE.set_integrator("dopri5")
ODE.set_parameters(*[0.6])
initial_state = { R: 0.2, B: 0.5 }
ODE.set_initial_value(initial_state,0.0)
times = np.arange(0.0,100,0.1)
values = { R: [], B: [] }
for time in times:
    ODE.integrate(time)
    for dynvar in dynvars:
        values[dynvar].append(ODE.y_dict[dynvar])

This works, because the list input to set_parameters is unpacked with *. However, if ODE.set_parameters([0.6]) is used instead, it fails with the ambiguous ValueError "Wrong input" (and ODE.check() gives no information). Given that list comprehensions are probably the easiest way to both specify the control params/values and to ensure their order remains consistent, it'd be good if their implementation was consistent between control_params and set_parameters. At least, the documentation should be updated to specify that a list input to set_parameters needs to be unpacked.

Wrzlprmft commented 4 years ago

This originates from aspiring an interface similar to scipy.integrate.ode. On the other hand, I see no problem with additionally allowing for a single iterable as an argument, so this is now implemented.

Thanks for requesting this.