Observables¶
The observable layer¶
The three functions on this page are gragra's public entry points for
Newtonian gravity: potential, acceleration and gravity-gradient tensor. Each
dispatches to a G-free kernel contract and
multiplies the result by G_SI — that multiplication is the only thing
this layer does beyond argument normalization and dispatch (see
What gragra computes). All four accept a
chunk_size and (where relevant) a softening_m and a backend keyword;
see Chunking, softening and tolerances and
Compute backends. The "numpy" backend is the reference
implementation and the default; the other backends are experimental
performance selectors checked for agreement against it.
Potential¶
point_potential(source, targets, *, softening_m=0.0, chunk_size=None)
returns the scalar gravitational potential, shape (M,), unit J/kg. Sign
convention: K_Φ = −1/r_eff, negative for a positive mass.
"""Smallest end-to-end check that an installation actually works.
Imports the package, confirms one public name and the value of the
gravitational constant, then reproduces the closed-form potential of a point
mass. If this runs, the install is sound.
"""
import gragra
from gragra import G_SI, PointMass, point_potential
# The public surface is importable and carries the names the docs use.
assert "point_potential" in gragra.__all__
assert "point_acceleration" in gragra.__all__
# The gravitational constant is a declared CODATA value, not a fitted one.
assert G_SI == 6.67430e-11
# One mass, one observation point, one closed-form answer:
# Phi = -G M / r, which at M = 1 kg and r = 1 m is exactly -G_SI.
potential_j_kg = point_potential(PointMass(1.0, [0.0, 0.0, 0.0]), [[1.0, 0.0, 0.0]])
assert potential_j_kg.shape == (1,)
assert potential_j_kg[0] == -G_SI
print("gragra reproduces Phi = -G M / r at r = 1 m:", potential_j_kg[0], "J/kg")
Acceleration¶
point_acceleration(source, targets, *, softening_m=0.0, chunk_size=None, backend="numpy")
returns the vector gravitational acceleration, shape (M, 3), unit m/s².
Sign convention: K_a = Δ/r_eff³ with Δ = r_source − r_target, so the
result points from the observation point back towards the source.
"""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
Gravity gradient tensor¶
point_gravity_gradient(source, targets, *, softening_m=0.0, chunk_size=None)
returns the tensor ∂aᵢ/∂xⱼ, shape (M, 3, 3), unit s⁻². Sign convention:
K_T = 3ΔΔ/r_eff⁵ − I/r_eff³, differentiated with respect to the
observation coordinate. At the fixed point softening_m=0, source at the
origin, target at (1, 0, 0): T = G_SI · diag(2, −1, −1), and
tr(T) = 0 — the vacuum Laplace-equation property, which holds only
for zero softening (see Chunking, softening and tolerances).
"""The three kernel sign conventions, pinned at one exact fixed point.
Source: 1 kg at the origin. Observation point: (1, 0, 0) m. Softening: zero.
At r = 1 m every geometric factor is exactly 0, +-1 or +-2 in IEEE-754 double
precision, so each check below is an exact equality and fails on a sign error
of any magnitude -- which a default-tolerance comparison would not, because
NumPy's default atol of 1e-8 is larger than the entire quantity being tested.
"""
import numpy as np
from gragra import (
G_SI,
PointMass,
point_acceleration,
point_gravity_gradient,
point_potential,
)
source = PointMass(mass_kg=1.0, position_m=[0.0, 0.0, 0.0])
target = [[1.0, 0.0, 0.0]]
# K_Phi = -1 / r_eff, so the potential of a positive mass is negative.
potential_j_kg = point_potential(source, target)
assert potential_j_kg.shape == (1,)
assert potential_j_kg[0] == -G_SI
# K_a = Delta / r_eff**3 with Delta = r_source - r_target, so the acceleration
# points from the observation point towards the source: along -x here.
acceleration_m_s2 = point_acceleration(source, target)
assert acceleration_m_s2[0, 0] == -G_SI
assert (acceleration_m_s2[0, 1:] == 0.0).all()
# K_T = 3 Delta Delta / r_eff**5 - I / r_eff**3, differentiated with respect
# to the *observation* coordinate. On the separation axis it is diag(2, -1, -1)
# and the tensor axes are the two trailing ones.
gradient_s2 = point_gravity_gradient(source, target, softening_m=0.0)
assert gradient_s2.shape == (1, 3, 3)
assert (gradient_s2[0] == G_SI * np.diag([2.0, -1.0, -1.0])).all()
# With zero softening the tensor is trace-free exactly, not approximately.
assert np.trace(gradient_s2[0]) == 0.0
Status and verification¶
point_potential¶
Status & verification
| Status | Stable |
| Verified against | closed-form analytic solution; independent numerical oracle (tests/oracles/) |
| Agreement | Exact at the unit-mass, unit-distance fixed point |
| Test evidence | tests/test_observables.py::test_potential_analytic_value, tests/test_kernel_contract.py::test_kernel_vs_observable_real |
| Conditions | softening_m >= 0; chunk_size any positive int or None |
| Not verified | Agreement outside the tested fixed points and configurations |
| Known limits | none beyond Chunking, softening and tolerances |
| Scope | none |
point_acceleration¶
Status & verification
| Status | Stable |
| Verified against | closed-form analytic solution; invariance property (chunk-size, backend equivalence) |
| Agreement | Exact at the unit-mass, unit-distance fixed point; rtol=1e-15 chunk-size invariance |
| Test evidence | tests/test_observables.py::test_acceleration_analytic_value, tests/test_observables.py::test_inverse_square_scaling, tests/test_observables.py::test_superposition_symmetric_sources, tests/test_kernel_contract.py::test_chunk_size_invariance |
| Conditions | softening_m >= 0; any backend; chunk_size any positive int or None |
| Not verified | Error detection beyond the listed fixed points and configurations; the zero/sign-flip mutation controls cover only their named checks |
| Known limits | none beyond Chunking, softening and tolerances |
| Scope | none |
point_gravity_gradient¶
Status & verification
| Status | Stable |
| Verified against | closed-form analytic solution; invariance property |
| Agreement | Tensor-sign fixed point matches to rtol=1e-14; at zero softening the trace is bounded by 32 ε max(abs(T)) for the tested configuration |
| Test evidence | tests/test_observables.py::test_tensor_sign_convention, tests/test_observables.py::test_trace_free_gradient, tests/test_observables.py::test_numerical_derivatives |
| Conditions | softening_m >= 0; trace-free property holds only at softening_m == 0 |
| Not verified | Agreement outside the tested fixed points |
| Known limits | With softening_m > 0, tr(T) is not zero — see Chunking, softening and tolerances |
| Scope | none |