A Byte of PhysicsLogo

Computational relativity

Many important predictions of relativity can be calculated exactly: a Lorentz boost, a freely moving particle, or an ideal black hole with perfect symmetry. Real systems such as two orbiting black holes, a merging pair of neutron stars, or collapsing matter usually have too little symmetry for a closed-form solution. Computational relativity, commonly called numerical relativity in the strong-gravity setting, turns Einstein's equations into a finite numerical initial-value problem.

The result is not simply a picture of curved spacetime. It is a solution tied to initial data, coordinates, gauge conditions, a finite grid, boundary conditions, an evolution formulation, and estimates of numerical error. A trustworthy run must report those choices and show that physically meaningful outputs remain stable when the numerical approximation is improved.

From one geometric equation to a time evolution

Einstein's field equation is

\[G_{\mu\nu}+\Lambda g_{\mu\nu}=\frac{8\pi G}{c^4}T_{\mu\nu}\]

The spacetime metric

\(g_{\mu\nu}\)

describes geometry. The Einstein tensor

\(G_{\mu\nu}\)

contains derivatives of that metric, and the stress-energy tensor

\(T_{\mu\nu}\)

describes matter and non-gravitational fields. The cosmological constant is \(\Lambda\).

The compact equation is covariant: it does not choose a preferred coordinate system. A computer, however, needs a sequence of states labeled by a time coordinate. A three-plus-one decomposition slices four-dimensional spacetime into three-dimensional spatial hypersurfaces. Each slice carries:

The lapse and shift are gauge choices. Changing them can make coordinate trajectories look very different without changing the underlying spacetime.

Evolution equations and constraints

The decomposition produces two kinds of equations. Evolution equations advance the state from one slice to the next. Constraint equations must hold on the initial slice and should remain satisfied during exact evolution. Numerical truncation and boundary errors cause nonzero constraint residuals, so their behavior is a central diagnostic.

In geometric units where \(G=c=1\), the Hamiltonian constraint has the schematic form

\[\mathcal H={}^{(3)}R+K^2-K_{ij}K^{ij}-16\pi\rho=0\]

and the momentum constraints can be written

\[\mathcal M^i=D_j\left(K^{ij}-\gamma^{ij}K\right)-8\pi S^i=0\]

Here \({}^{(3)}R\) is the spatial curvature scalar, \(K=\gamma^{ij}K_{ij}\), and \(\rho\) and \(S^i\) describe matter as seen by observers normal to the slice. These equations are shown to explain the workflow, not as a complete solver.

import sympy as sp

# Flat vacuum initial data: spatial curvature and extrinsic curvature vanish.
spatial_metric = sp.eye(3)
extrinsic_curvature = sp.zeros(3)
scalar_curvature, matter_energy_density = 0, 0
trace_k = sp.trace(extrinsic_curvature)
hamiltonian = scalar_curvature + trace_k**2 - sp.trace(extrinsic_curvature**2)
assert hamiltonian == 0 and matter_energy_density == 0
print("Flat-vacuum Hamiltonian constraint:", hamiltonian)
# This checks one exact initial state; it is not a general-relativity solver.

Why formulation matters

Writing the field equation in a mathematically equivalent form does not guarantee equally stable numerical evolution. Modern codes often use a Baumgarte–Shapiro–Shibata–Nakamura formulation, a generalized-harmonic formulation, or a related strongly hyperbolic system. These introduce evolved variables and sometimes constraint-damping terms designed to control numerical errors.

The formulation, gauge, and boundary treatment work together. A stable time integrator cannot rescue an ill-posed continuum system. Likewise, a well-posed formulation can still fail when a boundary injects constraint violations or a coordinate gauge stretches the grid until resolution is lost.

Building valid initial data

One cannot usually place two isolated black-hole metrics side by side and begin evolving them. The combined data must satisfy the constraint equations. Initial-data methods choose some freely specifiable fields and solve elliptic equations for the remaining fields. The choices encode physical assumptions about masses, spins, momentum, orbit, and unwanted initial gravitational radiation.

For matter systems, an equation of state and matter constraints are also needed. Different plausible neutron-star equations of state can produce physical differences larger than the remaining grid error. Numerical convergence does not remove uncertainty in the physical model.

Discretizing space and time

The continuous fields are stored on a finite representation: finite-difference grids, spectral elements or domains, finite-volume meshes for fluids, or a combination. Compact objects need high resolution nearby while emitted waves travel through a much larger region, so adaptive mesh refinement or multiple spectral domains are common.

An explicit evolution method must respect a stability restriction related to the Courant–Friedrichs–Lewy condition:

\[\Delta t\leq C_{\mathrm{CFL}}\frac{\Delta x}{{v_{\mathrm{max}}}}\]

The precise constant depends on the spatial scheme, time integrator, dimensionality, grid geometry, and characteristic speeds. Reducing spatial spacing without reducing the time step can therefore make a previously stable run unstable.

import numpy as np

spacings = np.array([0.1, 0.05, 0.025])
maximum_speed, courant = 1.0, 0.4  # Units with c=1.
time_step = courant * spacings.min() / maximum_speed
print("Step limited by finest spacing:", time_step)

Black holes on a grid

A black-hole singularity cannot be represented as an ordinary finite grid value. Two major strategies avoid evolving the singular point directly:

An apparent horizon can be located on individual slices, but an event horizon is a global object whose location depends on the future spacetime. Horizon mass, spin, area, and shape are therefore accompanied by definitions and numerical uncertainties rather than inferred from a dark region in a visualization.

Extracting gravitational waves

The metric components near a source are strongly coordinate-dependent. Gravitational radiation is commonly extracted on coordinate spheres at several radii using quantities such as the Newman–Penrose scalar \(\Psi_4\). Far from the source, it is related to the two gravitational-wave polarizations by

\[\Psi_4\approx\ddot h_+-i\ddot h_\times\]

up to sign and convention choices that must be stated. Recovering strain requires two integrations in time or a frequency-domain procedure, each of which needs careful treatment of integration constants and low-frequency drift.

Waveforms are extracted at several finite radii and extrapolated toward large radius, or computed with methods that reach future null infinity. Moving the extraction radius is an essential check because a waveform can appear converged on the grid while retaining near-zone or gauge contamination.

Measuring convergence

Suppose a method has leading error

\[Q(h)=Q_{\mathrm{exact}}+Ch^p+\mathcal O(h^{p+1})\]

where \(h\) is grid spacing and \(p\) is convergence order. Run the same physical problem at three resolutions \(h,\,h/r,\,h/r^2\). If the leading-error model applies, the differences satisfy approximately

\[\frac{Q(h)-Q(h/r)}{Q(h/r)-Q(h/r^2)}\approx r^p\]

so an observed order can be estimated:

\[p_{\mathrm{obs}}\approx\frac{\ln\left|\frac{Q(h)-Q(h/r)}{Q(h/r)-Q(h/r^2)}\right|}{\ln r}\]

The formal order printed in a method's documentation is not enough. Mesh interfaces, shocks, horizon treatment, and boundaries can lower the observed order. Compare waveform phase and amplitude, horizon quantities, conservation errors, and constraint norms—not only one visually smooth plot.

# A controlled second-order finite-difference example at the same probe.
steps = np.array([0.2, 0.1, 0.05])
results = np.sin(steps) / steps
coarse_change, fine_change = results[0] - results[1], results[1] - results[2]
order = np.log(abs(coarse_change / fine_change)) / np.log(2)
print("Observed derivative convergence order:", order)

Constraint norms need context

A code may record an \(L_2\) norm such as

\[\|\mathcal H\|_2=\sqrt{\frac{1}{V}\int_V|\mathcal H|^2\,dV}\]

but a single small number is not a proof of correctness. State the domain, normalization, excluded regions, units, and resolution. A residual can be small because the normalization is large, and a global norm can hide a severe local violation. Plot spatial maxima and convergence as well as integrated norms.

Constraint violations should remain controlled and decrease at the expected rate under refinement while physical observables converge. Constraint damping can reduce one diagnostic without correcting a bad matter model or boundary condition.

A reproducible evolution loop

# A scalar wave is a small test of a propagation scheme, not Einstein evolution.
N, length = 100, 1.0
x = np.arange(N) * length / N
dx, dt = length / N, 0.005
current = np.sin(2 * np.pi * x)
previous = np.sin(2 * np.pi * (x + dt))
for step in range(100):
    following = (
        2 * current
        - previous
        + (dt / dx) ** 2 * (np.roll(current, -1) - 2 * current + np.roll(current, 1))
    )
    previous, current = current, following
exact = np.sin(2 * np.pi * (x - 100 * dt))
print("Scalar-wave error after 0.5 time units:", np.max(abs(current - exact)))

A production code also needs parallel communication, restart consistency, provenance, deterministic or characterized reductions, and protection against silent hardware or storage faults. Those engineering details affect scientific reproducibility.

Validation hierarchy

Before trusting a binary merger, test increasingly demanding cases:

  1. Evolve flat Minkowski spacetime and verify that it stays flat apart from controlled numerical error.
  2. Evolve known analytic or semi-analytic solutions, such as gauge waves or a stationary single black hole, and compare against the expected result.
  3. Verify conservation laws and constraints on standard matter tests.
  4. Reproduce published benchmark problems.
  5. Compare independent formulations or codes where possible.
  6. Refine the mesh, move the outer boundary, vary the extraction radii, and alter gauge parameters without changing the intended physical result beyond the error budget.

These checks find different failures. A convergent answer can converge to the wrong continuum problem if the boundary or initial data are wrong. A small constraint residual can coexist with inaccurate waveform phase. Agreement between independent methods is especially valuable because they are less likely to share the same implementation error.

Coordinates, observables, and scope

Coordinate separation between two black-hole punctures is not automatically a physical distance. Coordinate merger time is not automatically an observable. Prefer quantities defined at infinity, quasi-local horizon measures with stated definitions, proper distances when meaningful, and invariant curvature scalars. When a coordinate-dependent diagnostic is useful for debugging, label it as such.

This lesson is a workflow introduction, not an implementation of a general-relativity solver. Full systems include tensor discretization, constraint-preserving boundaries, atmosphere treatments for fluids, magnetic fields, neutrino transport, equations of state, and many other specialized choices. The requested introductory textbooks establish relativity and its invariants; the Einstein Toolkit documentation supplies the specialized computational practices referenced here.

A worked Python notebook

Use a unit characteristic speed and Courant factor 0.4 to inspect refinement cost. The question for this calculation is precise: how does stable propagation time step change when we change grid spacing, while keeping the other choices fixed? The earlier sections explain the physical relationship. Here we turn one part of that relationship into a calculation that you can run, inspect, and change in Google Colab.

This scalar propagation estimate is not an evolution system for spacetime curvature. Keep that sentence beside the results. A formula can return a number outside its useful range, and a plotting library will usually draw that number without asking whether the physical assumptions still hold. The selected interval is from 0.01 to 0.2 scaled length units. It gives us a specific domain to discuss instead of leaving the graph's range to a software default.

Use the notebook link above the lesson, run the setup cell, and then run the cells in order. The code below is also included there. NumPy handles the arrays of numerical values, SymPy keeps the equation symbolic, and Matplotlib draws the results. If these libraries or notebook cells are unfamiliar, start with Your first physics notebook.

State the relationship before choosing a graph

In this small example, q stands for grid spacing, expressed in scaled length units. The output is stable propagation time step, expressed in scaled time units. The equation below uses the numerical values in those units. Any fixed coefficients therefore carry the units needed to make the relation consistent. A value supplied in another unit must be converted before it enters this expression.

\[f(q) = 0.4 q\]

This is a function of one changing input. It is not a statement that the physical system has only one variable. We deliberately hold the other inputs fixed so that one cause of variation is visible. Changing several inputs at once would make it harder to tell which change produced the output difference. Write down the held-fixed choices before trying a different experiment.

At 0.05 scaled length units, the expression gives 0.02 scaled time units. This is our reference calculation. It is a prediction of the stated example, not a new measurement. Its displayed digits help us compare implementations. They should not be read as a claim that physical inputs are known to six significant figures.

import numpy as np
import sympy as sp
import matplotlib.pyplot as plt

q = sp.Symbol("q", real=True)
expression = sp.sympify("0.4*q", locals={"q": q})
reference_input = 0.05
reference_output = float(expression.subs(q, reference_input))
print("Input:", reference_input, "scaled length units")
print("Predicted stable propagation time step:", reference_output, "scaled time units")

sp.Symbol declares a mathematical symbol. It does not assign a numerical measurement to that symbol. subs then replaces the symbol with our chosen input, and float converts the resulting symbolic number into a standard Python numerical value. Keeping these steps separate lets us inspect the equation before numerical substitution. It also prevents a later plot from becoming the only surviving record of the model.

Compare a few cases you can explain

Before drawing hundreds of points, calculate three. The lower endpoint gives 0.004 scaled time units. The midpoint, at 0.105 scaled length units, gives 0.042 scaled time units. The upper endpoint gives 0.08 scaled time units. Read these together with the assumptions: they describe different choices of grid spacing within the same example, with the other inputs held fixed.

These three values are a useful first check, but they do not establish the shape between them. Two endpoints can be equal even when the curve bends or oscillates in between. Likewise, a large change between endpoints does not tell us whether the relationship is linear. We need the equation and intermediate samples to answer those questions.

evaluate = sp.lambdify(q, expression, modules="numpy")
inputs = np.array([0.01, 0.10500000000000001, 0.2])
outputs = np.asarray(evaluate(inputs), dtype=float)
outputs = np.broadcast_to(outputs, inputs.shape)
for input_value, output_value in zip(inputs, outputs):
    print(
        f"{input_value:.6g} scaled length units -> "
        f"{output_value:.6g} scaled time units"
    )
assert np.all(np.isfinite(outputs))

lambdify turns the symbolic expression into a numerical function. We explicitly select NumPy as its numerical backend so it can accept an array. The same expression now has two useful forms: one for algebra and one for calculating many cases. The assertion checks that the chosen inputs produce finite outputs. It would catch an infinity or an undefined numerical result, but a finite value can still be physically inappropriate.

Make a graph that answers the question

The horizontal axis shows grid spacing; the vertical axis shows stable propagation time step. Every plotted point is a prediction of the same expression. We mark the reference case separately so that a reader can connect the earlier printed value to its place on the curve. The axis labels include units because a shape without a scale cannot tell us the size of a physical effect.

input_grid = np.linspace(0.01, 0.2, 301)
output_grid = np.asarray(evaluate(input_grid), dtype=float)
output_grid = np.broadcast_to(output_grid, input_grid.shape)
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(input_grid, output_grid, label="Stated model")
ax.scatter([reference_input], [reference_output], label="Reference case", zorder=3)
ax.set_xlabel("Grid spacing (scaled length units)")
ax.set_ylabel("Stable propagation time step (scaled time units)")
ax.legend()
ax.grid(alpha=0.25)
fig.tight_layout()
plt.show()

Look for a sign change, a turning point, or a region where the curve becomes steep. Connect each feature to the expression rather than to the colour of the line. If the curve has a minimum or maximum, changing the input in opposite directions may have similar effects on the output near that point. If it is nearly straight over a short interval, a local linear approximation may be useful even when the complete expression is nonlinear.

Increasing the number of samples makes the line smoother and may reveal features that a coarse grid missed. It does not improve the physical inputs or enlarge the model's validity range. For quantities that can take only discrete values, use the line as a guide to the relationship and interpret only the allowed input values. A plotting function does not enforce such restrictions for you.

Measure the local sensitivity

A derivative asks a more specific question than the whole graph: how quickly does stable propagation time step change near the reference input? Its units are the output unit divided by the input unit. For this calculation, the derivative at 0.05 scaled length units is 0.4 in those ratio units. Locally, the output increases as the input increases. This statement is local; the sign or size of the slope can be different elsewhere on the curve.

\[f'(q) = 0.4\]

For a small input change, multiply the derivative by that change to estimate the output change. Then compare that estimate with a fresh evaluation of the full expression. The distinction between these two calculations matters: the derivative gives a tangent approximation, while substitution evaluates the chosen expression at the new point.

slope_expression = sp.diff(expression, q)
slope_at_reference = float(slope_expression.subs(q, reference_input))
changed_input = 0.05095
linear_prediction = reference_output + slope_at_reference * (
    changed_input - reference_input
)
full_prediction = float(evaluate(changed_input))
print("Symbolic derivative:", slope_expression)
print("Tangent prediction:", linear_prediction, "scaled time units")
print("Full expression:", full_prediction, "scaled time units")
print(
    "Approximation difference:",
    full_prediction - linear_prediction,
    "scaled time units",
)

Here the changed input is 0.05095 scaled length units. The tangent estimate is 0.02038 scaled time units, while the full expression gives 0.02038 scaled time units. If the two values are close, it means the chosen input change is small enough for this local approximation at this point. It does not mean the entire model is linear. If the slope is zero, a quadratic or higher-order term may supply the leading change instead.

This is also the beginning of uncertainty propagation. If the input has a small stated standard uncertainty, the magnitude of the slope times that uncertainty estimates one contribution to output uncertainty. Do not automatically use the same rule for a wide interval, a discontinuity, a turning point with zero first derivative, or several correlated uncertain inputs. Those situations need more of the model than one slope.

Check the derivative with numerical differences

We can estimate the same slope from values on either side of the reference point. Subtract the lower output from the upper output and divide by twice the input step. Parentheses are essential: the entire output difference must be divided by the entire input interval. Without them, Python can execute a different mathematical expression while still producing a plausible number.

The calculation below uses three decreasing step sizes. Each estimate is compared with the symbolic derivative, so the reference is independent of the numerical difference formula. Near an endpoint, it is acceptable here to evaluate a small extension only because the displayed mathematical expression is smooth there; such a check does not assert that extrapolated values describe additional physical cases.

input_span = 0.19
for fraction in [1e-2, 1e-3, 1e-4]:
    step = input_span * fraction
    upper = float(evaluate(reference_input + step))
    lower = float(evaluate(reference_input - step))
    numerical_slope = (upper - lower) / (2 * step)
    difference = numerical_slope - slope_at_reference
    print(f"step={step:.6g}: slope={numerical_slope:.6g}, difference={difference:.6g}")

For a smooth function, reducing the step often reduces the approximation error at first. Very small steps can eventually make subtraction of nearly equal floating-point values less reliable. Therefore “smaller” is not an unlimited guarantee of “better.” Compare the observed behaviour over a useful range of steps instead of choosing the smallest number the computer can represent.

An absolute slope difference is useful when the expected slope is near zero. A relative difference would divide by a very small reference and could look enormous even when the absolute discrepancy is tiny. Away from zero, a relative difference can help compare quantities with very different scales. The tolerance should reflect the question and the numerical method, rather than a universal decimal threshold copied into every test.

Change the example and preserve the reasoning

Make one copy of the reference calculation before editing it. Choose a second value of grid spacing inside the stated interval, predict whether stable propagation time step will rise or fall, and then evaluate the expression. Place the prediction and the result in a text cell. If they disagree, identify whether the mistake was in the sign, the unit conversion, the algebra, or the assumption about how the curve behaves.

Next choose a shorter plotting interval around the reference point. Compare the local curve with its tangent approximation. Then widen the interval again. The difference shows why a useful local estimate need not describe the whole range. Keep the axis labels and numerical scale visible when comparing the plots; automatic axis limits can make two different effects appear equally large.

Finally, return to the physical limit stated at the start: This scalar propagation estimate is not an evolution system for spacetime curvature. Describe one change to the setup that would violate that limit. State what new input, equation, or observation would be needed before trusting an answer in the changed situation. This last step connects the Python experiment to the physics instead of treating successful execution as the only criterion for a useful result.

Before saving, restart the runtime and run all cells in order. A result that depends on an earlier, now-deleted input cell is not reproducible from the saved notebook. Keep unrounded values for later calculations, round only the reported output, and label any new data as measured, assumed, or generated. The graph and printed values should remain traceable to the same expression and the same choices of inputs.

Recover the output change by integrating the slope

There is another way to check that the expression and its derivative describe the same relationship. Add up the small output changes predicted by the slope across the chosen input interval. In the continuous limit, this integral equals the output at the upper endpoint minus the output at the lower endpoint. The result has units of scaled time units: the input unit in the integration cancels the input unit in the denominator of the slope.

This statement is about the mathematics of this particular relationship. It does not assign a new physical meaning to an arbitrary area under a graph. For example, an area under force versus distance represents work, while an area under a different pair of quantities may represent something else or have no standard name. Always carry the axis units through the calculation before naming an integral.

slope_function = sp.lambdify(q, slope_expression, modules="numpy")
endpoint_change = float(evaluate(0.2) - evaluate(0.01))
for points in [101, 201, 401]:
    grid = np.linspace(0.01, 0.2, points)
    slopes = np.asarray(slope_function(grid), dtype=float)
    slopes = np.broadcast_to(slopes, grid.shape)
    integrated_change = np.trapezoid(slopes, grid)
    print(
        points,
        "integrated change:",
        integrated_change,
        "endpoint change:",
        endpoint_change,
    )

The trapezoidal rule joins neighbouring slope samples with straight segments and adds their signed areas. A negative contribution remains negative; replacing every slope by its magnitude would calculate a different quantity. Increasing the sample count provides a practical refinement check. If the values do not settle toward the endpoint difference, first inspect the interval, singularities, and array ordering.

Do not conclude that two agreeing calculations prove the model is physically complete. Both use the same selected relationship, so an omitted effect would be omitted from both. Their agreement is evidence that the numerical tools implement that relationship consistently. Comparing with an independent experiment or a more appropriate physical model is a separate test.

Keep this comparison in the notebook when you change the expression. It is particularly useful after adding a coefficient, changing a sign convention, or converting an input unit. A code cell that produces a smooth graph can conceal these mistakes; a second calculation of the same output change gives you another place to notice them.

Sources


Share to: