-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
834 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"""Potential assembly.""" | ||
|
||
import typing | ||
import numpy as np | ||
import numpy.typing as npt | ||
from bempp._bempprs import lib as _lib, ffi as _ffi | ||
from bempp.function_space import FunctionSpace | ||
from ndelement.reference_cell import ReferenceCellType | ||
from enum import Enum | ||
from _cffi_backend import _CDataBase | ||
from scipy.sparse import coo_matrix | ||
|
||
_dtypes = { | ||
0: np.float32, | ||
1: np.float64, | ||
} | ||
_ctypes = { | ||
np.float32: "float", | ||
np.float64: "double", | ||
} | ||
|
||
|
||
def _dtype_value(dtype): | ||
"""Get the u8 identifier for a data type.""" | ||
for i, j in _dtypes.items(): | ||
if j == dtype: | ||
return i | ||
raise TypeError("Invalid data type") | ||
|
||
|
||
class OperatorType(Enum): | ||
"""Operator type.""" | ||
|
||
SingleLayer = 0 | ||
DoubleLayer = 1 | ||
|
||
|
||
class PotentialAssembler(object): | ||
"""Potential assembler.""" | ||
|
||
def __init__(self, rs_assembler: _CDataBase, owned: bool = True): | ||
"""Initialise.""" | ||
self._rs_assembler = rs_assembler | ||
self._owned = owned | ||
|
||
def __del__(self): | ||
"""Delete.""" | ||
if self._owned: | ||
_lib.free_potential_assembler(self._rs_assembler) | ||
|
||
def set_quadrature_degree(self, cell: ReferenceCellType, degree: int): | ||
"""Set the (non-singular) quadrature degree for a cell type.""" | ||
if not _lib.potential_assembler_has_quadrature_degree(self._rs_assembler, cell.value): | ||
raise ValueError(f"Invalid cell type: {cell}") | ||
_lib.potential_assembler_set_quadrature_degree(self._rs_assembler, cell.value, degree) | ||
|
||
def quadrature_degree(self, cell: ReferenceCellType) -> int: | ||
"""Get the (non-singular) quadrature degree for a cell type.""" | ||
if not _lib.potential_assembler_has_quadrature_degree(self._rs_assembler, cell.value): | ||
raise ValueError(f"Invalid cell type: {cell}") | ||
return _lib.potential_assembler_quadrature_degree(self._rs_assembler, cell.value) | ||
|
||
def set_batch_size(self, batch_size: int): | ||
"""Set the batch size.""" | ||
_lib.potential_assembler_set_batch_size(self._rs_assembler, batch_size) | ||
|
||
@property | ||
def batch_size(self) -> int: | ||
"""Get the batch size.""" | ||
return _lib.potential_assembler_batch_size(self._rs_assembler) | ||
|
||
@property | ||
def dtype(self): | ||
"""Data type.""" | ||
return _dtypes[_lib.potential_assembler_dtype(self._rs_assembler)] | ||
|
||
@property | ||
def _ctype(self): | ||
"""C data type.""" | ||
return _ctypes[self.dtype] | ||
|
||
|
||
def create_laplace_assembler( | ||
operator_type: OperatorType, dtype: typing.Type[np.floating] = np.float64 | ||
): | ||
"""Create a Laplace assembler.""" | ||
return PotentialAssembler( | ||
_lib.laplace_potential_assembler_new(operator_type.value, _dtype_value(dtype)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
import numpy as np | ||
from ndelement.reference_cell import ReferenceCellType | ||
from bempp.assembly.potential import OperatorType, create_laplace_assembler | ||
from bempp.function_space import function_space | ||
from ndgrid.shapes import regular_sphere | ||
from ndelement.ciarlet import create_family, Family, Continuity | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"otype", | ||
[ | ||
OperatorType.SingleLayer, | ||
OperatorType.DoubleLayer, | ||
], | ||
) | ||
def test_create_assembler(otype): | ||
a = create_laplace_assembler(otype) | ||
|
||
assert a.quadrature_degree(ReferenceCellType.Triangle) != 3 | ||
a.set_quadrature_degree(ReferenceCellType.Triangle, 3) | ||
assert a.quadrature_degree(ReferenceCellType.Triangle) == 3 | ||
with pytest.raises(ValueError): | ||
a.set_quadrature_degree(ReferenceCellType.Interval, 3) | ||
with pytest.raises(ValueError): | ||
a.quadrature_degree(ReferenceCellType.Interval) | ||
|
||
assert a.batch_size != 4 | ||
a.set_batch_size(4) | ||
assert a.batch_size == 4 | ||
|
||
assert a.dtype == np.float64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
//! Assembly of potential operators | ||
mod assemblers; | ||
pub(crate) mod cell_assemblers; | ||
mod integrands; | ||
pub mod integrands; | ||
|
||
pub use assemblers::PotentialAssembler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.