The physics is explained in fitting GW170817 and the trade-off between ejecta mass and speed. This guide is the recipe.
What the authors published
Villar and colleagues compiled the ultraviolet, optical and infrared photometry of GW170817 from 18 papers into one table, which they distribute with the arXiv source of their paper. Villar and colleagues, arXiv:1710.11576v2, §2 and Table 3 They ask that any use of the table cite the original papers as well as their compilation. Our CSV keeps their reference key for every row so that each point can be credited.
All of this is also in tutorial-inputs.json. Schlafly and Finkbeiner, Table 6
Units
Time is in days since the merger, magnitudes are AB and not corrected for extinction, mass is in solar masses, speed in units of c, opacity in cm2g−1, temperature in kelvin and distance in megaparsecs. Inside the code everything is converted to CGS. The model evaluates each filter at one effective wavelength; that approximation is part of what the extra scatter absorbs.
Step 1: get the table and check it
Keep the download in its own empty folder and the scripts in another.
mkdir -p kilonova-fit/author-data kilonova-fit/scripts
cd kilonova-fit/author-data
curl -L -o villar-source.tar.gz https://arxiv.org/e-print/1710.11576
tar -xzf villar-source.tar.gz
shasum -a 256 phot_table.dat
The digest should be:
a2ec8677f60d096a1347d82f5628377fd206de5596909c161a79d68c70bc6e92 phot_table.dat
The e-print link serves the newest version of the source. If the authors ever post a new version, the table may change. Stop and compare before trusting anything below if the digest differs.
Step 2: extract the fixed subset
Save extract_gw170817_photometry.py in the scripts folder and run it on the table:
cd ../scripts
python3 extract_gw170817_photometry.py ../author-data/phot_table.dat
It writes gw170817-photometry.csv and prints both digests. The output should match our CSV, digest 6e9777...52fc. It spans 0.452 to 25.44 days.
Step 3: fix the software
python3 -m venv .venv
. .venv/bin/activate
pip install numpy==2.0.2 scipy==1.13.1
The versions are listed in requirements.txt. We used Python 3.9.6. The fit script writes the versions it ran with into its output.
Step 4: run the fits and the checks
Save kilonova_model.py, fit_two_component.py, check_independent_limits.py and tutorial-inputs.json next to the CSV, then:
python3 fit_two_component.py --check
python3 check_independent_limits.py
With --check, the fit script recomputes everything and compares it with a saved fit-outputs.json in the same folder, allowing a relative difference of 2×10−3. Without the flag it overwrites that file. The second script writes independent-checks.json.
What the fit script does
- It builds each component from the radioactive heating law, the thermalization fit of Barnes and colleagues, Arnett's diffusion integral with the factor 2/td, and a blackbody photosphere with a temperature floor.
- It adds the components' flux in each band, converts to AB magnitudes at 39.5Mpc, and adds Milky Way extinction.
- It minimises −lnL, alternating a bounded least-squares step at fixed scatter with a one-dimensional search over the scatter.
- It starts from Villar and colleagues' published values and from 16 fixed pseudo-random points, with seed 170817, and keeps every distinct optimum.
- It repeats the fit for four variants and maps −lnL over a grid of red mass and speed, re-fitting the other parameters at each grid point.
Results
The local errors come from the curvature at the optimum and ignore the other branches. Villar and colleagues report 0.023, 0.256c and 3983K for blue and 0.050, 0.149c, 3.65 and 1151K for red, with scatter 0.256. Villar and colleagues, Table 2 The two sets of numbers are different optima of closely related likelihoods; see the failure cases below.
The independent checks pass. Arnett's integral agrees with a direct Radau solution of the energy equation within 4.3×10−4. Energy is conserved to 5×10−5. The light-curve peaks satisfy L=Q to 2×10−4. For pure t−1.3 heating the peak falls at 0.7087td, the value found by quadrature. The radioactive energy from one to ten days matches the closed form within 10−5. A blackbody spectrum integrated over frequency returns its luminosity.
Failure cases and how they are handled
- The printed light-curve integral. Villar and colleagues' eq. 3 has the kernel t/td instead of 2t/td2. As printed it has units of energy and depends on the time unit; for our best red component it is 3.48 times L with time in days. We use 2t/td2 and check it against the energy equation.
- The printed photosphere law. Eqs. 4 and 5 put σSB2 inside the fourth root but σSB in the receded radius. With the square every temperature would be divided by 0.0868, about 4000K becoming 46000K. We use σSB throughout.
- The printed likelihood. Eq. 6 does not normalise the Gaussian with variance σi2+σ2. We use the normalised form. Constant terms do not move the best parameters, but the normalisation does affect the fitted scatter.
- Several optima. The 17 starts land in four distinct optima, at 63.02, 64.90, 82.13 and 137.28. Ten of the 17 starts end at 82.13 and only three reach the best optimum, so a fit with a single start is likely to stop short of it.
- A blue speed outside the thermalization table. The best blue speed, 0.385c, exceeds the table's 0.3c, so its coefficients are held at the edge values. The fit is valid for this model but the blue speed should not be read as a measurement.
- Extinction convention. Schlafly and Finkbeiner's coefficients multiply the original SFD reddening, 0.1228. Using the recalibrated 0.1056 with the same coefficients under-corrects by 14 percent. That variant changes −lnL by only 0.94 and the red mass by 0.0002.
- Rejected models. One component, no temperature floor, and a red opacity forced to 10 are all worse by more than 200 in BIC. Their outputs are kept in the file for comparison.
- The published parameters in our setup. With σ refitted they give −lnL=106.32 and leave the late K band about 0.20 magnitudes too bright. We have not separated the effects of data selection, likelihood normalisation and the authors' MCMC sampling.
What this tutorial does not show
The fit uses only detections and no upper limits, a spherical geometry, one grey opacity per component and blackbody spectra. Villar and colleagues preferred a three-component model, and an asymmetric version changed their masses by about half. Opacities of real r-process mixtures depend on wavelength and change with time. Tanaka and colleagues, §4.2 The numbers here therefore show what this model can and cannot do with these data. They do not measure the ejecta. The Lab draws its curves with a TypeScript port of the same model, which agrees with the Python reference within 3×10−4 magnitudes. Compare its default with the data in the kilonova light curves Lab.
Try it in the Lab
Keep the default, which uses the published Villar et al. values, and look at the K points after ten days: the model curve runs about 0.2 magnitudes brighter than the data. Open the kilonova light-curve Lab