Skip to content

Commit

Permalink
Fix cffi_utils for abscence of petsc4py (#3415)
Browse files Browse the repository at this point in the history
* Fix cffi_utils for non abscence of petsc4py

* Readd KeyError catch

* Apply suggestions

* Ruff
  • Loading branch information
schnellerhase committed Sep 18, 2024
1 parent 56d870e commit e23ff5e
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions python/dolfinx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ctypes as _ctypes
import os
import pathlib
import warnings

import numpy as np

Expand Down Expand Up @@ -195,25 +196,29 @@ def set_vals(A: int,
ffi.from_buffer(rows(data), mode)
"""

# Register numba complex types with cffi
import cffi as _cffi

_ffi = _cffi.FFI()

try:
import cffi as _cffi
import numba as _numba
import numba.core.typing.cffi_utils as _cffi_support

_ffi = _cffi.FFI()
# Register complex types
_cffi_support.register_type(_ffi.typeof("float _Complex"), _numba.types.complex64)
_cffi_support.register_type(_ffi.typeof("double _Complex"), _numba.types.complex128)
except (ImportError, KeyError):

except KeyError:
pass
except ImportError:
warnings.warn(
"Could not import numba, so cffi/numba complex types were not registered.",
ImportWarning,
)

# Define CFFI ABI functions for PETSc calls.
try:
from petsc4py import PETSc as _PETSc

import cffi as _cffi

_ffi = _cffi.FFI()
_lib_cffi = _ffi.dlopen(str(get_petsc_lib()))

_CTYPES = {
Expand Down Expand Up @@ -248,5 +253,10 @@ def set_vals(A: int,
"""See PETSc `MatSetValuesBlockedLocal
<https://petsc.org/release/manualpages/Mat/MatSetValuesBlockedLocal>`_
documentation."""
except (ImportError, KeyError):
except KeyError:
pass
except ImportError:
warnings.warn(
"Could not import petsc4py, so cffi/PETSc ABI mode interface was not created.",
ImportWarning,
)

0 comments on commit e23ff5e

Please sign in to comment.