CalebBell / chemicals

chemicals: Chemical database of Chemical Engineering Design Library (ChEDL)
MIT License
186 stars 36 forks source link

numba_vectorized reports TypeError #28

Closed jgostick closed 2 years ago

jgostick commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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.