Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8a07281
use rstar capi for cell location
leo-collins Feb 17, 2026
85ae5e9
add logging
leo-collins Feb 17, 2026
5169948
remove allgather logs
leo-collins Feb 17, 2026
3b96b1e
update comments
leo-collins Feb 23, 2026
281c971
fix cached property
leo-collins Feb 23, 2026
e8da836
merge logging
leo-collins Feb 24, 2026
ab58a9b
more logs
leo-collins Feb 25, 2026
8f7bd74
more logging
leo-collins Feb 25, 2026
dcc3052
adapt to new API
leo-collins Mar 4, 2026
1ffbc5d
rebase
leo-collins Mar 10, 2026
94d0165
spatial index in 1D
leo-collins Mar 9, 2026
37a8a3f
partial fix
leo-collins Mar 10, 2026
c7de8cd
fix test
leo-collins Mar 10, 2026
e5722a0
tidy up cython
leo-collins Mar 10, 2026
613c5f1
fixes
leo-collins Mar 12, 2026
d1b9295
renaming and use `firedrake_rtree` package
leo-collins Mar 20, 2026
c291d33
firedrake-rtree fixes
leo-collins Mar 20, 2026
dfe8df1
remove unused stuff from rtree cython
leo-collins Mar 20, 2026
ebd96dc
free ids in error case
leo-collins Mar 20, 2026
470c1be
add firedrake-rtree dependency
leo-collins Mar 20, 2026
c7eadf4
remove unnecessary stuff from petschdr
leo-collins Mar 20, 2026
b6dc90d
fix function at
leo-collins Mar 20, 2026
4f17f7a
misc tidying
leo-collins Mar 20, 2026
13d273a
fixes
leo-collins Mar 20, 2026
6af9f5f
fixup mesh.py
leo-collins Mar 20, 2026
add7f7d
add rtree libspatialindex back in
leo-collins Mar 20, 2026
fe32bd8
add spatialindex to supermesh extension
leo-collins Mar 24, 2026
7457c9b
add extra runtime path
leo-collins Mar 24, 2026
06f65d3
lowercase r
leo-collins Mar 24, 2026
1ade5a8
fix
leo-collins Mar 25, 2026
cbf72d4
libsupermesh fix
leo-collins Mar 25, 2026
970dde5
remove library
leo-collins Mar 26, 2026
6e6f3e3
docs fixes
leo-collins Mar 26, 2026
eb6604c
remove rtree import from setup.py
leo-collins Mar 26, 2026
2a959d1
docs fix
leo-collins Mar 26, 2026
0840c77
use libsupermesh branch for docs
leo-collins Mar 26, 2026
695d241
installation and workflow
leo-collins Mar 26, 2026
a219492
review suggestions
leo-collins Apr 2, 2026
e352521
merge conflicts
leo-collins Apr 2, 2026
5395ca6
fix docstrings
leo-collins Apr 2, 2026
8b2cfbe
lint
leo-collins Apr 2, 2026
d82c3b1
Apply suggestions from code review
leo-collins Apr 16, 2026
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
103 changes: 103 additions & 0 deletions firedrake/cython/rtree.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# cython: language_level=3

cimport numpy as np
import numpy as np
import ctypes
import cython
from libc.stddef cimport size_t
from libc.stdint cimport uintptr_t, uint32_t

cdef extern from "rtree-capi.h":
ctypedef enum RTreeError:
Success
NullPointer
InvalidDimension

ctypedef struct RTreeH:
pass

RTreeError rtree_bulk_load(
RTreeH **tree,
const double *mins,
const double *maxs,
const size_t *ids,
size_t n,
uint32_t dim
)

RTreeError rtree_free(RTreeH *tree)


cdef class RTree(object):
"""Python class for holding an Rtree."""

cdef RTreeH* tree

def __cinit__(self, uintptr_t tree_handle):
self.tree = <RTreeH*>0
if tree_handle == 0:
raise RuntimeError("invalid tree handle")
self.tree = <RTreeH*>tree_handle

def __dealloc__(self):
if self.tree != <RTreeH*>0:
rtree_free(self.tree)

@property
def ctypes(self):
"""Returns a ctypes pointer to the rtree."""
return ctypes.c_void_p(<uintptr_t> self.tree)


@cython.boundscheck(False)
@cython.wraparound(False)
def build_from_aabb(np.ndarray[np.float64_t, ndim=2, mode="c"] coords_min,
np.ndarray[np.float64_t, ndim=2, mode="c"] coords_max,
np.ndarray[np.npy_uintp, ndim=1, mode="c"] ids = None):
"""Builds rtree from two arrays of shape (n, dim) containing the coordinates
of the lower and upper corners of n axis-aligned bounding boxes, and an
optional array of shape (n,) containing integer ids for each box.

Parameters
----------
coords_min : numpy.ndarray
Lower corner coordinates of the bounding boxes, with shape `(n, dim)`.
coords_max : numpy.ndarray
Upper corner coordinates of the bounding boxes, with shape `(n, dim)`.
ids : numpy.ndarray
Optional integer ids for each box, with shape `(n,)`. If not provided,
defaults to `0, 1, ..., n-1`.

Returns
-------
RTree
An RTree object containing the Rtree.
"""
cdef:
RTreeH* rtree
size_t n
size_t dim
RTreeError err

if coords_min.shape[0] != coords_max.shape[0] or coords_min.shape[1] != coords_max.shape[1]:
Comment thread
connorjward marked this conversation as resolved.
raise ValueError("coords_min and coords_max must have the same shape")

n = <size_t>coords_min.shape[0]
dim = <size_t>coords_min.shape[1]
if ids is None:
ids = np.arange(n, dtype=np.uintp)
elif ids.shape[0] != n:
raise ValueError("Mismatch between number of boxes and number of ids")

err = rtree_bulk_load(
&rtree,
<const double*>coords_min.data,
<const double*>coords_max.data,
<const size_t*>ids.data,
n,
dim
)
if err != Success:
raise RuntimeError("rtree_bulk_load failed")

return RTree(<uintptr_t>rtree)
110 changes: 0 additions & 110 deletions firedrake/cython/spatialindex.pyx

This file was deleted.

47 changes: 0 additions & 47 deletions firedrake/cython/spatialindexinc.pxi

This file was deleted.

4 changes: 2 additions & 2 deletions firedrake/evaluate.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ struct Function {
PetscScalar *f;
PetscInt *f_map;

/* Spatial index */
void *sidx;
/* rtree */
void *rtree;

/*
* TODO:
Expand Down
15 changes: 6 additions & 9 deletions firedrake/function.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import rtree
import firedrake_rtree
import sys
import ufl
import warnings
Expand All @@ -12,7 +12,6 @@
from ctypes import POINTER, c_int, c_double, c_void_p
from collections.abc import Collection
from numbers import Number
from pathlib import Path
from functools import partial, cached_property
from typing import Tuple

Expand Down Expand Up @@ -44,7 +43,7 @@ class _CFunction(ctypes.Structure):
("coords_map", POINTER(as_ctypes(IntType))),
("f", c_void_p),
("f_map", POINTER(as_ctypes(IntType))),
("sidx", c_void_p)]
("rtree", c_void_p)]


class CoordinatelessFunction(ufl.Coefficient):
Expand Down Expand Up @@ -545,7 +544,7 @@ def _constant_ctypes(self):
def _ctypes(self):
mesh = extract_unique_domain(self)
c_function = self._constant_ctypes
c_function.sidx = mesh.spatial_index and mesh.spatial_index.ctypes
c_function.rtree = mesh.rtree and mesh.rtree.ctypes

# Return pointer
return ctypes.pointer(c_function)
Expand Down Expand Up @@ -588,7 +587,7 @@ def _at(self, arg, *args, **kwargs):
:kwarg tolerance: Tolerence to use when checking if a point is
in a cell. Default is the ``tolerance`` provided when
creating the :func:`~.Mesh` the function is defined on.
Changing this from default will cause the spatial index to
Changing this from default will cause the rtree to
be rebuilt which can take some time.
"""
# Shortcut if function space is the R-space
Expand Down Expand Up @@ -877,15 +876,13 @@ def make_c_evaluate(function, c_name="evaluate", ldargs=None, tolerance=None):

if ldargs is None:
ldargs = []
libspatialindex_so = Path(rtree.core.rt._name).absolute()
lsi_runpath = f"-Wl,-rpath,{libspatialindex_so.parent}"
ldargs += [str(libspatialindex_so), lsi_runpath]
ldargs += [firedrake_rtree.get_lib_filename(), f"-Wl,-rpath,{firedrake_rtree.get_lib()}"]
dll = compilation.load(
src, "c",
cppargs=[
f"-I{path.dirname(__file__)}",
f"-I{sys.prefix}/include",
f"-I{rtree.finder.get_include()}",
f"-I{firedrake_rtree.get_include()}",
*petsctools.get_petsc_dirs(prefix="-I", subdir="include"),
],
ldargs=ldargs,
Expand Down
Loading
Loading