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

numba_vectorized reports TypeError #28

Closed
jgostick opened this issue Mar 30, 2021 · 3 comments
Closed

numba_vectorized reports TypeError #28

jgostick opened this issue Mar 30, 2021 · 3 comments

Comments

@jgostick
Copy link

jgostick commented Mar 30, 2021

Describe the bug
I got the numba_vectorized func working for my single component calcs, but now the functions based on mixing rules are now giving me grief.

Minimal Reproducible Example

numba_vectorized.Lindsay_Bromley([333, 333], [[0.2, 0.2], [0.8, 0.8]], [[1, 1], [1, 1]], [[1, 1], [1, 1]], [351, 370], [18, 33])

Additional context
TypeError: return type must be specified for object mode

@CalebBell
Copy link
Owner

Hi Jeff,
This one I believe is a limitation of numba's vectorize function. The scalar form works fine:

from chemicals import numba
import numpy as np
numba.Lindsay_Bromley(333.0, np.array([0.2, 0.8]),  np.array([1.0, 1.0]), np.array([1.0, 1.0]),  np.array([351.0, 370.0]), np.array([18.0, 33.0]))

The broadcasting rules aren't quite all implemented in Numba. I have known about this issue for a while but haven't ran into it before. I will need to look into this one. It may be possible to implement some sort of a wrapper.
Sincerely.
Caleb

@jgostick
Copy link
Author

Am I misunderstanding how a mixing rule is meant to work? I have two components and want want the mixture value for a range of compositions, so I would need to pass ys = [ [x1, x2, ...], [y1, y2, ...]]. Since I'm passing in a range of compositions, I should also be able to pass in a range for the other props too, correct? The way you've written it above, I can only use this function for a single composition ([x1, y1] = [0.2, 0.2]).

@CalebBell
Copy link
Owner

Hi Jeff,
Numba is very magic but it isn't smart enough to know how to pass in the compositions, thermal conductivities, and viscositires to the inner loop. Should it vectorize by passing in ys=[x1, x2] or ys=[x1, y1]? It can't tell and there isn't a set of hard-coded assumptions for this case. In a case like this, there is a mechanism to manually specify how to do this calculation:

import chemicals.numba
import numpy as np
from numba import guvectorize
from numba.types import int64, float64

@guvectorize([(float64[:], float64[:, :], float64[:, :],
               float64[:, :], float64[:], float64[:], float64[:])], '(n),(n,n),(n,n),(n,n),(n),(n)->(n)')
def better_Lindsay_Bromley(T, ys, ks, mus, Tbs, MWs, res):
    for i in range(T.shape[0]):
        res[i] = chemicals.numba.Lindsay_Bromley(T[i], ys[:, i], ks[:, i], mus[:, i], Tbs, MWs)
    
better_Lindsay_Bromley(np.array([333.0, 334.0]), np.array([[0.2, 0.2], [0.8, 0.8]]),  np.array([[1, 1], [1,1]]), 
                      np.array([[1, 1], [1, 1]]), np.array([351.0, 370.0]), np.array([18.0, 33.0]))

array([0.9969455 , 0.99694497])

I don't have a clear idea how to implement this in the library as a whole at this time, but it is a topic I am interested in for sure.
In the future numba may have better support for doing this automatically. For example the error you received originally is clearly not applicable in this case.

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