SciML / diffeqpy

Solving differential equations in Python using DifferentialEquations.jl and the SciML Scientific Machine Learning organization
MIT License
508 stars 39 forks source link

Accessing `EnsembleSolution` elements #105

Open Skumin opened 1 year ago

Skumin commented 1 year ago

I'm trying to use diffeqpy to simulate some trajectories of the geometric Brownian motion but I'm having some issues accessing the elements of the solution.

My code is the following:

from diffeqpy import de
import numpy as np

def gbm(A0, mu, sigma, t0, t1, dt=0.01, trajectories=1):
    def f(du, u, p, t):
        du[0] = u[0] * p[0]

    def g(du, u, p, t):
        du[0] = u[0] * p[1]

    prob = de.SDEProblem(f, g, [A0], (t0, t1), [mu, sigma])
    eprob = de.EnsembleProblem(prob)

    return de.solve(
        eprob,
        de.SRIW1(),
        de.EnsembleThreads(),
        dt=dt,
        adaptive=False,
        trajectories=trajectories,
    )

res = gbm(100.0, 0.05, 0.2, 0.0, 1.0, trajectories=5)

In Julia, I would do simply res[1] to get the first path. I could then also do res[1](x) to get the value of that path at some arbitrary x in the domain of the solution. Can I do this in Python somehow?

When I try res[0] in Python, I get an error:

>>> res[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'PyCall.jlwrap' object is not subscriptable

I know that I could bypass this partly by using saveat=np.arange(t0, t1 + dt, dt), for example, in the call to de.solve, but that requires knowing x in advance.

ChrisRackauckas commented 8 months ago

res.u[i] works, the indexing doesn't seem to carry over for some reason.