Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

pymrm.particles.Sphere

Back to module page · Back to alphabetical overview

Signature

Sphere(center, radius)

Summary

Sphere (any dimension; in 2-D this is a disk — see Circle).

Documentation

Fully analytic: exact level function, normals, and segment intersections.

Source

View on GitHub

class Sphere(Particle):
    """Sphere (any dimension; in 2-D this is a disk — see :class:`Circle`).

    Fully analytic: exact level function, normals, and segment intersections.
    """

    def __init__(self, center, radius):
        super().__init__(center)
        self.radius = float(radius)

    def level_body(self, coords):
        return np.linalg.norm(np.asarray(coords, dtype=float), axis=-1) - self.radius

    def normal_body(self, coords):
        return np.asarray(coords, dtype=float)

    def bounding_box_body(self):
        r = self.radius
        return tuple((-r, r) for _ in range(self.ndim))

    def intersect(self, p0, p1):
        p0 = np.asarray(p0, dtype=float)
        p1 = np.asarray(p1, dtype=float)
        d = p1 - p0
        m = p0 - self.position
        a = np.sum(d * d, axis=-1)
        b = np.sum(d * m, axis=-1)
        c = np.sum(m * m, axis=-1) - self.radius**2
        disc = np.sqrt(np.maximum(b * b - a * c, 0.0))
        t1 = (-b - disc) / a
        t2 = (-b + disc) / a
        # Endpoints straddle the surface: entering picks the first root,
        # exiting (p0 inside) the second.
        return np.where(c > 0.0, t1, t2)

Members

__init__(center, radius)

View source on GitHub

bounding_box(pad = 0.0)

World axis-aligned bounding box ((lo, hi), ...) per axis.

View source on GitHub

bounding_box_body()

Body-frame bounding box ((lo, hi), ...) per axis.

View source on GitHub

intersect(p0, p1)

Surface crossing fraction t on the segments p0 → p1.

p0/p1 are (n, ndim) batches whose endpoints straddle the surface (level(p0) and level(p1) of opposite sign); returns t in (0, 1) with level(p0 + t (p1 - p0)) == 0. Default: vectorised bisection on level; shapes with closed-form intersections override this.

View source on GitHub

level(coords)

Signed level function at world coords shaped (..., ndim).

View source on GitHub

level_body(coords)

Signed level function at body-frame coords shaped (..., ndim).

View source on GitHub

normal(coords)

Outward (solid→fluid) unit normal at world surface points.

View source on GitHub

normal_body(coords)

Gradient direction of level_body (finite differences).

View source on GitHub

to_body(coords)

View source on GitHub

vec_to_world(vecs)

View source on GitHub

__slots__

View source on GitHub