Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use 2d-array of 'u0'? #18

Closed
kimyoungjin06 opened this issue Sep 27, 2022 · 4 comments
Closed

Can I use 2d-array of 'u0'? #18

kimyoungjin06 opened this issue Sep 27, 2022 · 4 comments

Comments

@kimyoungjin06
Copy link

I want to use the 2d-shape initial values of the multiple-particles system. But, I think I cannot use the slicing or indexing.

def swing_lsoda(network):
    """
    rhs = swing_cover(network)
    funcptr = rhs.address
    ...
    data = np.array([1.0])
    usol, success = lsoda(funcptr, u0, t_eval, data = data)
    """
    @nb.cfunc(lsoda_sig) # network
    def rhs(t, u, du, p): # p0=m, p1=gamma, p2=P, p3=K
        # u is 2d-array 
        Interaction = p[3] * SinIntE(network, u[0])
        du[0] = u[1] #Thetas of nodes
        du[1] = 1/p[0]*(p[2] - p[1]*u[1] - Interaction) #Omega 

How can I try it?

@Nicholaswogan
Copy link
Owner

u0 can not be a 2d array. But you can flatten the 2D array, then reshape it in the rhs function. For example

@cfunc(lsoda_sig)
def rhs(t, u, du, p):
    u_2D = nb.carray(u, (1,2))
    # rest of function goes here
    du[0] = u_2D[0,0]-u_2D[0,0]*u_2D[0,1]
    du[1] = u_2D[0,0]*u_2D[0,1]-u_2D[0,1]*p[0]

funcptr = rhs.address
u0_2d = np.array([[5.,0.8]])
u0 = u0_2d.flatten()
data = np.array([1.0])
t_eval = np.linspace(0.0,50.0,1000)

usol, success = lsoda(funcptr, u0, t_eval, data = data)

@kimyoungjin06
Copy link
Author

@Nicholaswogan Perfect! ;)

Thanks a lot!
I think this is so beginner level in the numba, but I also put in the first step in the numba.

@kimyoungjin06
Copy link
Author

kimyoungjin06 commented Oct 4, 2022

@Nicholaswogan

I have an additional question.

Can I not use slicing?

def swing_lsoda(network, N):
    rhs = swing_cover(network)
    funcptr = rhs.address
    ...
    data = np.array([1.0])
    usol, success = lsoda(funcptr, u0, t_eval, data = data)

    @nb.cfunc(lsoda_sig) # network
    def rhs(t, u, du, p): # p0=m, p1=gamma, p2=P, p3=K
        # u_2D = nb.carray(u, (N,2))
        Interaction = p[3] * SinIntE(network, u[:N])
        du[0] = u[N:] #Thetas of nodes
        du[1] = 1/p[0]*(p[2] - p[1]*u[N:] - Interaction) #Omegas of nodes

@Nicholaswogan
Copy link
Owner

Nicholaswogan commented Oct 4, 2022

You can slice, but only after you have converted u to a numpy array with nb.carray

@nb.cfunc(lsoda_sig) # network
def rhs(t, u, du, p): 
    u_2D = nb.carray(u,(N,2))
    # `u_2D` can be sliced, but `u` can not be
    u_1D = nb.carray(u,(N*2))
    # `u_1D` can be sliced as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants