Skip to content
Merged
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
19 changes: 15 additions & 4 deletions src/scifem/point_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
# SPDX-License-Identifier: MIT

from __future__ import annotations
from typing import TYPE_CHECKING

from packaging.version import Version
from mpi4py import MPI
from petsc4py import PETSc

if TYPE_CHECKING:
from petsc4py import PETSc

try:
from petsc4py import PETSc

_HAS_PETSC = True
except ImportError:
_HAS_PETSC = False

import dolfinx
import dolfinx.fem.petsc
import numpy as np
import numpy.typing as npt
import ufl
Expand Down Expand Up @@ -158,7 +167,7 @@ def compute_cell_contributions(self):
self._basis_values = basis_values

def apply_to_vector(
self, b: dolfinx.fem.Function | dolfinx.la.Vector | PETSc.Vec, recompute: bool = False
self, b: dolfinx.fem.Function | dolfinx.la.Vector | "PETSc.Vec", recompute: bool = False
):
"""Apply the point sources to a vector.

Expand Down Expand Up @@ -187,9 +196,11 @@ def apply_to_vector(
b.x.array[dofs] += values * self._magnitude
elif isinstance(b, dolfinx.la.Vector):
b.array[dofs] += values * self._magnitude
elif isinstance(b, PETSc.Vec):
elif _HAS_PETSC and isinstance(b, PETSc.Vec):
b.setValuesLocal(
dofs,
values * self._magnitude,
addv=PETSc.InsertMode.ADD_VALUES,
)
else:
raise TypeError(f"Unsupported vector type {type(b)}.")
Loading