Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions interfaces/cython/cantera/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# This file is part of Cantera. See License.txt in the top-level directory or
# at https://cantera.org/license.txt for license and copyright information.

from ._cantera import *
from ._utils import __version__, __sundials_version__, __git_commit__
from .composite import *
from .liquidvapor import *
from .onedim import *
from .utils import *
from .data import *
import cantera.interrupts # Helps with standalone packaging (PyInstaller etc.)

import os
import sys
from pathlib import Path
import warnings

from ._cantera import * # import Cython module members
from ._cantera import (
__git_commit__, __sundials_version__, __version__,
)

from .composite import (
DustyGas, Interface, Quantity, Solution, SolutionArray,
)
from .data import list_data_files
from .liquidvapor import (
CarbonDioxide, Heptane, Hfc134a, Hydrogen, Methane, Nitrogen, Oxygen, Water,
)
from .onedim import (
BurnerFlame, CounterflowDiffusionFlame, CounterflowPremixedFlame,
CounterflowTwinPremixedFlame, FlameBase, FreeFlame, ImpingingJet,
)
from .utils import add_module_directory, import_phases


import cantera.interrupts # Helps with standalone packaging (PyInstaller etc.)

warnings.filterwarnings('default', module='cantera')
add_directory(Path(__file__).parent / "data")
add_directory('.') # Move current working directory to the front of the path

# Python interpreter used for converting mechanisms
if 'PYTHON_CMD' not in os.environ:
os.environ['PYTHON_CMD'] = sys.executable

del os
del sys
del Path
del warnings
92 changes: 66 additions & 26 deletions interfaces/cython/cantera/_cantera.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,86 @@
#cython: language_level=3
#distutils: language=c++

import sys
import importlib
import importlib.abc
import importlib.util
import sys as _sys
import importlib.abc as _abc
import importlib.machinery as _machinery
import importlib.util as _util

# Chooses the right init function
# See https://stackoverflow.com/a/52714500
class CythonPackageMetaPathFinder(importlib.abc.MetaPathFinder):
class _CythonPackageMetaPathFinder(_abc.MetaPathFinder):
def __init__(self, name_filter):
super().__init__()
self.name_filter = name_filter

def find_spec(self, fullname, path, target=None):
if fullname.startswith(self.name_filter):
# use this extension-file but PyInit-function of another module:
loader = importlib.machinery.ExtensionFileLoader(fullname, __file__)
return importlib.util.spec_from_loader(fullname, loader)
loader = _machinery.ExtensionFileLoader(fullname, __file__)
return _util.spec_from_loader(fullname, loader)


# Inject custom finder/loaders into sys.meta_path:
sys.meta_path.append(CythonPackageMetaPathFinder("cantera."))
_sys.meta_path.append(_CythonPackageMetaPathFinder("cantera."))

# Import the contents of the individual .pyx files
from ._utils import *
from ._onedim import *
from .solutionbase import *
from .delegator import *
from .func1 import *
from .kinetics import *
from .mixture import *
from .reaction import *
from .reactionpath import *
from .reactor import *
from .speciesthermo import *
from .thermo import *
from .transport import *
from .units import *
from .yamlwriter import *
from .constants import *
from .jacobians import *
from ._onedim import (
AxisymmetricFlow, Boundary1D, Domain1D, FlowBase, FreeFlow, Inlet1D, Outlet1D,
OutletReservoir1D, ReactingSurface1D, Sim1D, Surface1D, SymmetryPlane1D,
UnstrainedFlow,
)
from ._utils import (
AnyMap, CanteraError,
__git_commit__, __sundials_version__, __version__,
add_directory, appdelete, debug_mode_enabled, get_data_directories, hdf_support,
make_deprecation_warnings_fatal, print_stack_trace_on_segfault,
suppress_deprecation_warnings, suppress_thermo_warnings, use_legacy_rate_constants,
use_sparse,
)
from .constants import (
avogadro, boltzmann, electron_charge, electron_mass, epsilon_0, faraday,
gas_constant, light_speed, one_atm, permeability_0, planck, stefan_boltzmann,
)
from .delegator import extension
from .func1 import Func1, Tabulated1
from .jacobians import (
AdaptivePreconditioner, BandedJacobian, EigenSparseDirectJacobian,
EigenSparseJacobian, SystemJacobian,
)
from .kinetics import Kinetics, InterfaceKinetics
from .mixture import Mixture
from .reaction import (
Arrhenius, ArrheniusRate, ArrheniusRateBase, BlowersMaselRate, ChebyshevRate,
CustomRate, ElectronCollisionPlasmaRate, ExtensibleRate, ExtensibleRateData,
FalloffRate, InterfaceArrheniusRate, InterfaceBlowersMaselRate,
InterfaceRateBase, LinearBurkeRate, LindemannRate, PlogRate, Reaction,
ReactionRate, SriRate, StickRateBase, StickingArrheniusRate,
StickingBlowersMaselRate, ThirdBody, TroeRate, TsangRate, TwoTempPlasmaRate,
)
from .reactionpath import ReactionPathDiagram
from .reactor import (
ConnectorNode, ConstPressureMoleReactor, ConstPressureReactor,
ExtensibleConstPressureMoleReactor, ExtensibleConstPressureReactor,
ExtensibleIdealGasConstPressureMoleReactor,
ExtensibleIdealGasConstPressureReactor, ExtensibleIdealGasMoleReactor,
ExtensibleIdealGasReactor, ExtensibleMoleReactor, ExtensibleReactor,
FlowDevice, FlowReactor, IdealGasConstPressureMoleReactor,
IdealGasConstPressureReactor, IdealGasMoleReactor, IdealGasReactor,
MassFlowController, MoleReactor, PressureController, Reactor, ReactorBase,
ReactorNet, ReactorSurface, Reservoir, Valve, Wall, WallBase,
)
from .solutionbase import SolutionArrayBase
from .speciesthermo import (
ConstantCp, Mu0Poly, Nasa9PolyMultiTempRegion, NasaPoly2, ShomatePoly2,
SpeciesThermo,
)
from .thermo import (
Element, InterfacePhase, PureFluid, Species, ThermoModelMethodError, ThermoPhase,
)
from .transport import DustyGasTransport, GasTransportData, Transport
from .units import UnitStack, UnitSystem, Units
from .yamlwriter import YamlWriter


# Custom finder/loader no longer needed, so remove it
sys.meta_path.pop()
_sys.meta_path.pop()
8 changes: 4 additions & 4 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# at https://cantera.org/license.txt for license and copyright information.
from __future__ import annotations

from ._cantera import *
import numpy as np
import csv as _csv
import importlib.metadata
import warnings

from ._cantera import (
DustyGasTransport, InterfaceKinetics, InterfacePhase, Kinetics, PureFluid,
SolutionArrayBase, ThermoPhase, Transport,
)

_pandas = None
def _import_pandas():
Expand Down
7 changes: 4 additions & 3 deletions interfaces/cython/cantera/liquidvapor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# This file is part of Cantera. See License.txt in the top-level directory or
# at https://cantera.org/license.txt for license and copyright information.

from . import PureFluid, _cantera
from .thermo import PureFluid
from .transport import Transport


def Water(backend='Reynolds'):
Expand Down Expand Up @@ -41,7 +42,7 @@ def Water(backend='Reynolds'):
:ct:`WaterSSTP` and :ct:`WaterTransport` in the Cantera C++ source
code documentation.
"""
class WaterWithTransport(_cantera.Transport, PureFluid):
class WaterWithTransport(Transport, PureFluid):
__slots__ = ()

if backend == 'Reynolds':
Expand All @@ -51,7 +52,7 @@ class WaterWithTransport(_cantera.Transport, PureFluid):
return WaterWithTransport('liquidvapor.yaml', 'liquid-water-IAPWS95',
transport_model='water')

raise KeyError("Unknown backend '{}'".format(backend))
raise KeyError(f"Unknown backend {backend!r}")


def Nitrogen():
Expand Down
7 changes: 5 additions & 2 deletions interfaces/cython/cantera/onedim.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import warnings
import numpy as np

from ._cantera import *
from ._onedim import (
AxisymmetricFlow, FreeFlow, Inlet1D, Outlet1D, ReactingSurface1D, Sim1D,
Surface1D, SymmetryPlane1D, UnstrainedFlow,
)
from ._utils import CanteraError, __git_commit__, __version__, hdf_support
from .composite import Solution, SolutionArray
from . import __version__, __git_commit__, hdf_support


class FlameBase(Sim1D):
Expand Down
8 changes: 5 additions & 3 deletions interfaces/cython/cantera/reactor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import warnings
import numbers as _numbers
import numpy as np
from cython.operator cimport dereference as deref

from .thermo cimport *
from ._utils cimport pystr, stringify, comp_map, py_to_anymap, anymap_to_py
from ._utils import *
from ._utils import CanteraError
from .delegator cimport *
from .drawnetwork import *
from .drawnetwork import (
draw_flow_controllers, draw_reactor, draw_reactor_net, draw_surface, draw_walls,
)

cdef class ReactorBase:
"""
Expand Down
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/speciesthermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
cimport numpy as np
import numpy as np

from ._utils cimport *
from .constants import *
from ._utils cimport anymap_to_py, py_to_anymap
from .constants import gas_constant

# These match the definitions in speciesThermoTyeps.h
cdef int SPECIES_THERMO_CONSTANT_CP = 1
Expand Down
3 changes: 2 additions & 1 deletion interfaces/cython/cantera/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import os
import inspect as _inspect

from . import Solution, add_directory
from ._utils import add_directory
from .composite import Solution


def import_phases(filename, phase_names):
Expand Down
Loading