Skip to content

Point sources

The generic signed and complex source

WeightedPointSource is gragra's generic source type: a table of positions (N, 3) and a matching array of weights (N,), where weights are mass elements in kilograms that may be negative or complex. Use it whenever your source is not simply a collection of positive real masses — a density perturbation δρ, a phase-carrying element of a plane-wave source, or any signed/complex quantity.

from gragra import WeightedPointSource

source = WeightedPointSource(
    positions_m=[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]],
    weights_kg=[2.0, -1.5],
)
assert source.positions_m.shape == (2, 3)
assert source.weights_kg[0] == 2.0
assert source.weights_kg[1] == -1.5

Positive-real-mass convenience wrappers

PointMass (a single mass) and PointMassCloud (many masses) are convenience wrappers for positive real masses only — not a separate source abstraction. Both reject non-positive or non-finite mass at construction, and both convert internally to a WeightedPointSource via as_weighted_source() before any calculation happens. If your source can be negative or complex, use WeightedPointSource directly — PointMass/PointMassCloud will never accept it.

"""Newtonian acceleration from a single point mass -- a complete first run.

A 1 kg point mass sits at the origin and the observation point is 1 m away
along x. The closed-form answer is a = -G_SI * x_hat, and the checks below
are exact equalities rather than tolerance comparisons: at r = 1 m the
geometric kernel evaluates to exactly -1 in IEEE-754 double precision, so the
only arithmetic left is a single multiplication by G_SI.
"""

import numpy as np

from gragra import G_SI, PointMass, point_acceleration

source = PointMass(mass_kg=1.0, position_m=[0.0, 0.0, 0.0])

# Low-level observables take source-frame SI coordinates in meters.
target_xyz_m = [[1.0, 0.0, 0.0]]
acceleration_m_s2 = point_acceleration(source, target_xyz_m)
print("acceleration at (1, 0, 0) m:", acceleration_m_s2, "m/s^2")

# One observation point, three vector components. The component axis is last,
# so the shape is (number of observation points, 3).
assert acceleration_m_s2.shape == (1, 3)
assert acceleration_m_s2.dtype == np.float64

# The acceleration points from the observation point towards the mass -- here
# along -x -- and its magnitude is G M / r^2 = G_SI.
assert acceleration_m_s2[0, 0] == -G_SI
assert acceleration_m_s2[0, 1] == 0.0
assert acceleration_m_s2[0, 2] == 0.0

# Arrays returned by an observable are read-only. Copy before modifying.
assert acceleration_m_s2.flags.writeable is False

# Doubling the distance divides the magnitude by four. Both magnitudes are
# exact multiples of G_SI by a power of two, so the ratio is exactly 4.
farther = point_acceleration(source, [[2.0, 0.0, 0.0]])
assert acceleration_m_s2[0, 0] / farther[0, 0] == 4.0

Observation points

High-level TargetPoints stores (N, 3) length Quantities, unique IDs and a Frame; pass it to a model's .at(targets) method. Low-level numerical observables accept bare source-frame SI coordinate arrays, including a single (3,) point promoted to (1, 3) for convenience. They do not infer units, IDs or frame transforms from those arrays.

What the source weights mean

Prescribed motion

Finite-body mesh comparison uses signed WeightedPointSource terms for the bulk and interface mass perturbations, plus a separate positive mass cloud with per-cell displacement. The bulk and interface terms are summed before comparison; the displaced mass cloud is an alternative representation, not an additional source to superpose.

PrescribedPointTrajectory and PrescribedRigidTransform describe sampled external motion. Their observable functions evaluate only the supplied frames; they do not interpolate a trajectory, generate motion, or solve dynamics.

weights_kg is always a mass element — a point mass, or the mass element of a discretized density perturbation (δm = δρ·dV). It is never a pre-integrated transfer coefficient or a geometric weighting factor such as (n̂·d)·dS — those live in a separate abstraction used by the coherent and surface-source machinery (see Volume and surface sources and Coherent responses and cross statistics), not in weights_kg.

Validation and error behaviour

  • A WeightedPointSource position array and high-level TargetPoints input must be a strict 2-D table; passing (3,) raises ValueError.
  • PointMass/PointMassCloud reject non-positive or non-finite mass.
  • A source point coincident with a target point (r == 0) with softening_m == 0 raises ValueError naming the offending index pair; see Chunking, softening and tolerances.
  • All stored arrays (positions_m, weights_kg) are read-only after construction — see Arrays, dtypes and axes.
"""dtype rules, axis order, shape validation and the read-only contract.

Every assertion here restates a convention that the rest of the site relies
on, so a change to any of them breaks this file rather than only the prose.
"""

import numpy as np

from gragra import (
    PointMass,
    WeightedPointSource,
    point_acceleration,
    point_gravity_gradient,
)

# Coordinates are float64 tables of shape (N, 3): one row per source point,
# the component axis last.
positions = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
real_source = WeightedPointSource(positions_m=positions, weights_kg=[1.0, -1.0])
assert real_source.positions_m.dtype == np.float64
assert real_source.positions_m.shape == (2, 3)

# Weights keep the kind of number they were given: real stays float64, complex
# is promoted to complex128. Coordinates are float64 either way.
assert real_source.weights_kg.dtype == np.float64
complex_source = WeightedPointSource(
    positions_m=positions, weights_kg=[1.0 + 0.5j, -1.0]
)
assert complex_source.weights_kg.dtype == np.complex128
assert complex_source.positions_m.dtype == np.float64

# Low-level numeric observables consume bare source-frame SI target arrays.
# The output dtype follows the weights, never the coordinates.
targets_xyz_m = [[0.0, 2.0, 0.0], [0.0, 0.0, 3.0]]
assert point_acceleration(real_source, targets_xyz_m).dtype == np.float64
assert point_acceleration(complex_source, targets_xyz_m).dtype == np.complex128

# A vector observable ends in one component axis of length 3, a tensor
# observable in two. The observation-point axis stays in front of them.
assert point_acceleration(real_source, targets_xyz_m).shape == (2, 3)
assert point_gravity_gradient(real_source, targets_xyz_m).shape == (2, 3, 3)

# Stored arrays are defensive copies, so a later edit of the caller's array
# cannot reach inside the frozen source ...
mutable_input = np.array([[4.0, 0.0, 0.0]])
frozen_source = WeightedPointSource(positions_m=mutable_input, weights_kg=[2.0])
mutable_input[0, 0] = 99.0
assert frozen_source.positions_m[0, 0] == 4.0
assert frozen_source.positions_m.flags.writeable is False
assert real_source.weights_kg.flags.writeable is False

# ... and the arrays an observable hands back are read-only too.
assert point_acceleration(real_source, targets_xyz_m).flags.writeable is False

# A table of points has shape (N, 3). A bare (3,) array means "one point", and
# low-level observable functions accept that shorthand. Source dataclasses
# remain strict so a vector cannot be mistaken for a one-column table.
single = point_acceleration(PointMass(1.0, [0.0, 0.0, 0.0]), [5.0, 0.0, 0.0])
assert single.shape == (1, 3)
try:
    WeightedPointSource(positions_m=[5.0, 0.0, 0.0], weights_kg=[1.0])
except ValueError as error:
    assert "(N, 3)" in str(error)
else:
    raise AssertionError("a (3,) array must not be accepted as source table")

Status and verification

WeightedPointSource

Status & verification

Status Stable
Verified against invariance property; closed-form analytic solution
Agreement Exact — sign flip and complex dtype promotion are checked as exact equalities, not tolerances
Test evidence tests/test_point_sources.py::test_weighted_point_source_valid_real, tests/test_point_sources.py::test_weighted_point_source_valid_complex, tests/test_point_sources.py::test_stored_arrays_read_only_and_defensive_copy, tests/test_observables.py::test_weighted_source_sign_flip, tests/test_observables.py::test_weighted_source_complex_output_dtype
Conditions positions_m strict (N, 3) float64; weights_kg (N,), real or complex
Not verified Behaviour for weight magnitudes outside the ranges the test suite exercises
Known limits weights_kg is a mass element only — see What the source weights mean
Scope none

PointMass

Status & verification

Status Stable
Verified against closed-form analytic solution; invariance property
Agreement Exact at the unit-mass, unit-distance fixed point
Test evidence tests/test_point_sources.py::test_point_mass_valid, tests/test_point_sources.py::test_point_mass_invalid, tests/test_point_sources.py::test_point_mass_conversion, tests/test_observables.py::test_acceleration_analytic_value
Conditions mass_kg > 0 and finite; position_m shape (3,)
Not verified none beyond the conditions above
Known limits Positive real mass only — it is a convenience wrapper, not the generic source type
Scope none

PointMassCloud

Status & verification

Status Stable
Verified against invariance property (equals the sum of individual PointMass contributions)
Agreement Exact
Test evidence tests/test_point_sources.py::test_point_mass_cloud_valid, tests/test_point_sources.py::test_point_mass_cloud_invalid, tests/test_observables.py::test_point_mass_cloud_equals_sum
Conditions masses_kg all positive and finite; positions_m strict (N, 3)
Not verified none beyond the conditions above
Known limits Positive real mass only
Scope none

TargetPoints

Status & verification

Status Stable
Verified against invariance property
Agreement Exact — shape, units, IDs and owned read-only coordinates are checked as behavioural assertions
Test evidence tests/high_level/test_inputs.py::test_target_ids_are_nonempty_unique_strings_matching_count, tests/high_level/test_inputs.py::test_quantity_units_and_finite_inputs_are_required, tests/high_level/test_inputs.py::test_input_arrays_are_owned_even_if_original_and_returned_views_are_mutated
Conditions Length Quantity with shape (N, 3), unique non-empty IDs, and an explicit Frame
Not verified none beyond the conditions above
Known limits Low-level numerical observables use source-frame SI coordinate arrays and do not consume this high-level container
Scope Target coordinates and identity only