2

I am using the cantera module in python, specifically trying to extend the class cantera.Solution using inheritance. The base class can be called like this:

gas = cantera.Solution("gri30.ct")

Where "gri30.cti" is the file name of the chemistry model to be used.

I am trying to make a new class, SolutionExtended which inherits from cantera.Solution. Here is the class definition:

class SolutionExtended(cantera.Solution):
    def __init__(self):
        print("Beginning of SolutionExtended.__init__")
        
        super().__init__("gri30.cti")
        
        print("End of SolutionExtended.__init__")

When instantiating the SolutionExtended object, I get this error:

Traceback (most recent call last):

  File "E:\Projects\Coursework\Cantera\Code\test.py", line 15, in <module>
    gas = SolutionExtended()

  File "interfaces\cython\cantera\base.pyx", line 63, in cantera._cantera._SolutionBase.__cinit__

ValueError: Arguments are insufficient to define a phase

This is the same error that is thrown when you try to instantiate the parent class cantera.Solution without any input arguments (gas = cantera.Solution()).

Additionally, neither of the print statements actually print.

This leads me to thinking that cantera.Solution.__init__ is being called before SolutionExtended.__init__, which fails because no input arguments are supplied. I am by no means a python expert, however, so this may be completely off.

Can anyone tell me what is going on here? How would I make the SolutionExtended initialization work without requiring the chemistry model to be explicitly input?

I'm pretty stumped by this, so I really appreciate any help. Thanks!

Here is my full code:

import cantera

gas = cantera.Solution('gri30.cti')
gas()

class SolutionExtended(cantera.Solution):
    def __init__(self):
        print("Beginning of SolutionExtended.__init__")
        
        super().__init__("gri30.cti")
        
        print("End of SolutionExtended.__init__")

        
gas = SolutionExtended()
gas()

Link to the cantera package website: https://cantera.org/

Bryan M
  • 21
  • 1
  • 2
    *”base.pyx, `_SolutionBase.__cinit__`”* — These are hints that this is not a plain class with a simple `__init__`, but at the very least a custom `__new__`, but more likely even deeper customizations in C. In fact there’s not even a guarantee that `__init__` will even be called; it’s just what default class constructors do. – deceze Nov 17 '21 at 18:16
  • @deceze That would seem to explain it. Is there a way to call the `Solution` class's constructor with an input within the `SolutionExtended` class's definition? – Bryan M Nov 17 '21 at 18:25
  • @Bryan M deceze is correct, this behavior is due to the presence of the `__cinit__` function. Probably the easiest thing to do is to avoid subclassing altogether and have a `ct.Solution` instance be an instance attribute on your extended class. – darthbith Feb 14 '22 at 17:34
  • Does this answer your question? [Superclass \_\_init\_\_ overrides subclass constructor](https://stackoverflow.com/questions/64168584/superclass-init-overrides-subclass-constructor) – darthbith Feb 14 '22 at 17:38

0 Answers0