Numba jitclass: parent class type as a class member

Viewed 1084

Consider the class dof, and its child particleGroup:

class dof():
    def getVar(self):
        return self.var

spec = [
    ('var', float64[:]),
    ('C_s', float64[:]),          
]
@jitclass(spec)
class particleGroup(dof):
    def __init__(self, partRelease):
        self.var = np.array([partRelease.Q, partRelease.M[0], partRelease.M[1], partRelease.M[2], partRelease.B, partRelease.x[0], partRelease.x[1], partRelease.x[2], 0])
        self.C_s = np.copy(partRelease.C_s)
    def getC(self):
        return self.C_s

The details of partRelease object are not important.

The class dof is a python class as numba does not support inheritance of a jitclass. Thus, a deferred_type cannnot be used on it.

dof can be the parent of different child classes. I want the dof class to be used in another class, where I would be able to call the parent function getVar().

My question: how to store an instance of this class (dof type or any of the child types) in another class? Which type to specify in the jitclass specification?

Then: Even if I can specify the parent class type, I suspect that the call to a python class will be expensive. Is there another way to do polymorphism efficiently, e.g. using templates instead of inheritance?

1 Answers
Related