Perihelion precession in the Schwarzschild spacetime
A massive particle on a bound orbit around a static mass.
- Class:
SchwarzschildPrecessionSystem - Import:
from skcomplex.systems.physics.celestial_mechanics import SchwarzschildPrecessionSystem - Field:
physics/celestial_mechanics - Topics:
general relativity,ode,second order,gravity,geodesics - References:
- Schwichtenberg 2020
- Satz 2022
- Schwarzschild 1916
- Einstein 1915
- Carroll 2004
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.physics.celestial_mechanics import SchwarzschildPrecessionSystem
trajectory = SchwarzschildPrecessionSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
semi_major_axis |
100.0 |
eccentricity |
0.5 |
mass |
1.0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | curved spacetime |
| Laws | rate |
| Randomness | deterministic |
| Domains | vector |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class SchwarzschildPrecessionSystem(BaseSystem):
"""A massive particle on a bound orbit around a static mass.
Its perihelion advances every turn.
The particle follows a timelike geodesic of the Schwarzschild spacetime `SchwarzschildSpace`, in units with `G = c =
1` and with its proper time as the time of the system. It starts at the periapsis `r₁ = a (1 - e)` of an orbit whose
apoapsis is `r₂ = a (1 + e)`, with the energy `E` and the angular momentum `L` for which both are turning points of
`E² = (1 - 2M/r) (1 + L²/r²) + (dr/dτ)²`. Unlike a Kepler ellipse, the orbit does not close: the periapsis advances
by `6 π M / (a (1 - e²))` per turn when the orbit is far from the horizon, or `6 π G M / (c² a (1 - e²))` in other
units, which accounts for the anomalous 43 arcseconds per century of Mercury. The system has the position
`particle.x = (t, r, theta, phi)` and the four-velocity `particle.x'`.
Args:
semi_major_axis:
The semi-major axis `a` of the orbit.
eccentricity:
The eccentricity `e` of the orbit, in `[0, 1)`.
mass:
The mass `M` of the central body.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Raises:
ValidationError:
The eccentricity is outside `[0, 1)`, or the orbit comes closer than the innermost stable circular orbit.
Examples:
>>> from skcomplex.systems.physics.celestial_mechanics import SchwarzschildPrecessionSystem
>>> round(SchwarzschildPrecessionSystem().prepare().system_.elements[0].x[1], 6)
50.0
"""
field = 'physics/celestial_mechanics'
topics = ('general relativity', 'ode', 'second order', 'gravity', 'geodesics')
references = ('Schwichtenberg 2020', 'Satz 2022', 'Schwarzschild 1916', 'Einstein 1915', 'Carroll 2004')
until = 20000.0
dt = 10.0
def __init__(
self,
semi_major_axis: float = 100.0,
eccentricity: float = 0.5,
*,
mass: float = 1.0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.semi_major_axis = semi_major_axis
self.eccentricity = eccentricity
self.mass = mass
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of a massive particle on a bound orbit around a static mass.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
if not 0 <= self.eccentricity < 1:
error_msg = f'The eccentricity is {self.eccentricity!r}. Give a value in [0, 1) for a bound orbit.'
raise ValidationError(error_msg)
periapsis, apoapsis = self.semi_major_axis * (1 - self.eccentricity), self.semi_major_axis * (
1 + self.eccentricity
)
if periapsis <= 6 * self.mass:
error_msg = (
f'The periapsis {periapsis} lies inside the innermost stable circular orbit at 6 M = {6 * self.mass}. '
'Give a larger semi-major axis or a smaller eccentricity.'
)
raise ValidationError(error_msg)
lapses = [1 - 2 * self.mass / radius for radius in (periapsis, apoapsis)]
if self.eccentricity == 0:
momentum_squared = self.mass * periapsis**2 / (periapsis - 3 * self.mass)
else:
momentum_squared = (lapses[1] - lapses[0]) / (lapses[0] / periapsis**2 - lapses[1] / apoapsis**2)
energy = math.sqrt(lapses[0] * (1 + momentum_squared / periapsis**2))
particle = GeodesicParticleElement(
'particle',
x=[0.0, periapsis, EQUATOR, 0.0],
values={"x'": [energy / lapses[0], 0.0, 0.0, math.sqrt(momentum_squared) / periapsis**2]},
)
space = SchwarzschildSpace(mass=self.mass)
geodesic = GeodesicInteraction('geodesic', target='particle', space=space)
self.system_ = System(
self.label,
elements=[particle],
interactions=[geodesic],
space=space,
solver=GEODESIC_SOLVER,
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
GeodesicParticleElement, fromElement
Interactions:
GeodesicInteraction, fromInteraction
class GeodesicParticleElement(Element):
"""Test particle at the spacetime position `x = (t, r, theta, phi)` near a mass.
The mass is central, and the particle has its four-velocity.
The position is a vector in the Schwarzschild or Boyer-Lindquist coordinates of the space, and its derivative `x'`
is the four-velocity, the derivative of the position by the proper time of a massive particle or by the affine
parameter of a light ray. The particle is drawn in the equatorial plane, at `(r cos phi, r sin phi)`.
Args:
label:
The label of the particle.
x:
The initial position `(t, r, theta, phi)`.
values:
Further values by name, such as the initial four-velocity `x'`, or `None`.
Examples:
>>> from skcomplex.systems.physics.celestial_mechanics import GeodesicParticleElement
>>> GeodesicParticleElement('particle', x=[0.0, 10.0, 1.5708, 0.0]).x
[0.0, 10.0, 1.5708, 0.0]
"""
visual = VisualSpec(
'bodies',
{
'positions': _get_equatorial_position,
'trail': None,
'marks': _get_central_mass,
},
)
def __init__(
self,
label: str,
x: npt.ArrayLike = (0.0, 0.0, 0.0, 0.0),
*,
values: Mapping[str, object] | None = None,
) -> None:
super().__init__(label, values=values)
self.x = x
class GeodesicInteraction(Interaction):
"""Geodesic equation `x''^mu = -Γ^mu_(alpha beta)(x) x'^alpha x'^beta` of a point.
The point is a point of a pseudo-Riemannian space. The state is a vector with one component per coordinate of
the space, the position `x^mu` of a test particle, whose derivative `x'^mu` is the tangent. The
time of the system is the affine parameter of the geodesic, the proper time of a timelike geodesic when the tangent
starts with `g_(mu nu) x'^mu x'^nu = -1`. A tangent that starts null, with `g_(mu nu) x'^mu x'^nu = 0`, follows a
light ray. Either norm is conserved along the geodesic, so it measures the error of a run. The Christoffel symbols
are those the space derives from its metric.
Args:
label:
The label of the interaction, unique among the components of its system.
target:
The path of the element or group whose state is the position.
state:
The name of the vector state that is the position.
space:
The pseudo-Riemannian space whose geodesics the point follows, usually the space of the system too.
Attributes:
space_:
The prepared space, whose Christoffel symbols the rate reads.
Raises:
ValidationError:
The state is not a name.
SpaceError:
The space is not a pseudo-Riemannian space, raised by `prepare`.
Examples:
>>> from sage.all__sagemath_symbolics import SR, matrix
>>> from skcomplex.core.components.elements import Element
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.physics.common.relativity import GeodesicInteraction
>>> from skcomplex.systems.physics.common.relativity import SpacetimeSpace
>>> r, phi = SR.var('r phi')
>>> plane = SpacetimeSpace(matrix(SR, [[1, 0], [0, r**2]]), (r, phi))
>>> line = GeodesicInteraction('line', target='particle', space=plane)
>>> particle = Element('particle', values={'x': [1.0, 0.0], "x'": [0.0, 1.0]})
>>> system = System(elements=[particle], interactions=[line], space=plane).prepare()
>>> trajectory = system.simulate(until=1.0, dt=0.001)
>>> [round(float(value), 3) for value in trajectory.get_values('particle.x')[-1]]
[1.414, 0.785]
"""
law_classes: ClassVar[tuple[type[BaseLaw], ...]] = (RateLaw,)
def __init__(
self,
label: str,
*,
target: Role,
state: str = 'x',
space: SpacetimeSpace | None = None,
) -> None:
super().__init__(label)
self.target = target
self.state = state
self.space = space
def _check_arguments(self) -> None:
"""Raise an error for an argument the interaction cannot take.
Raises:
SpaceError:
The space is not a pseudo-Riemannian space.
ValidationError:
The type declares states, the state is not a name, or the position has not one component per
coordinate of the space.
"""
super()._check_arguments()
if not isinstance(self.state, str) or not self.state.isidentifier():
error_msg = (
f'{type(self).__name__} {self.label!r} moves the state {self.state!r}. Give the name of a state.'
)
raise ValidationError(error_msg)
if not isinstance(self.space, SpacetimeSpace):
error_msg = (
f'{type(self).__name__} {self.label!r} moves a point along the geodesics of {self.space!r}. Give a '
'`SpacetimeSpace`.'
)
raise SpaceError(error_msg)
self.space_ = self.space.prepare()
def _prepare(self, context: SimpleNamespace) -> None:
"""Set `space_`, the prepared pseudo-Riemannian space, and the law of a binding.
It first raises an error for a state that is not a name.
Args:
context:
The namespace of the target and the time `t`.
"""
super()._prepare(context)
self.laws_ = self._get_geodesic_laws(context)
def _get_geodesic_laws(self, context: SimpleNamespace) -> list[BaseLaw]:
"""Return the rate law of the second derivative of the position.
It is minus the connection on the tangent.
Args:
context:
The namespace of the target and the time `t`.
Returns:
laws:
The rate law of the position.
Raises:
ValidationError:
The position has not one component per coordinate of the space.
"""
t = context.t
position = getattr(context.target, self.state)
components = list(position) if isinstance(position, FreeModuleElement) else [position]
coordinates = self.space_.coordinates_
if len(components) != len(coordinates):
error_msg = (
f'The position {self.state!r} of {self.label!r} has {len(components)} components for the '
f'{len(coordinates)} coordinates of the space. Give a vector state of the dimension of the space.'
)
raise ValidationError(error_msg)
at = dict(zip(coordinates, components, strict=True))
tangent = [SR(component).diff(t) for component in components]
indices = range(len(components))
rate = [
-sum(
(
SR(self.space_.christoffel_[mu][alpha][beta]).subs(at) * tangent[alpha] * tangent[beta]
for alpha in indices
for beta in indices
if not SR(self.space_.christoffel_[mu][alpha][beta]).is_zero()
),
SR(0),
)
for mu in indices
]
if isinstance(position, FreeModuleElement):
return [RateLaw(position.diff(t, 2), vector(SR, rate))]
return [RateLaw(SR(position).diff(t, 2), rate[0])]