# AC62 GW170817 afterglow fitting package

The observations in this folder are real: a frozen slice of the uniform GW170817 afterglow
table of Makhathini et al. 2021. The light curves use the interactive Lab’s thin-shell model on a coarser angular grid.

## Run

    python3 reproduce.py          # recompute and compare with expected-outputs.json (exit 0 on pass)
    python3 reproduce.py --write  # recompute and overwrite expected-outputs.json

Standard library only. Tested with Python 3.12.12; a full run takes about three minutes.
The comparison allows max(0.01, 0.2 percent) on every number.

## Additional checks

    python3 held_bands.py --output held-bands-check.json
    python3 arrival_reference.py --output equal-arrival-check.json
    curl --fail --location https://arxiv.org/e-print/2006.02382v3 --output makhathini-v3.tar.gz
    python3 verify_observations.py --source-archive makhathini-v3.tar.gz --output source-check.json

The first command fits the radio detections and predicts the withheld optical and X-ray rows with
the broadband spectral index held fixed. The second solves eight arrival-time roots using
adaptive quadrature. Use the published paper archive only when you want to verify the frozen
data extraction.

## Files

| File | Role |
| --- | --- |
| afterglow_model.py | Thin-shell patch model: Huang, Dai & Lu (1999, arXiv:astro-ph/9906404v2) Eq. 10 for the Lorentz factor, Sari, Piran & Narayan (1998, arXiv:astro-ph/9712005v1) broken power-law synchrotron spectrum, Doppler boosting delta^3 per patch, both jets. |
| fit_jets.py | Chi-square on detections with asymmetric errors, upper-limit check, Nelder-Mead minimizer. |
| reproduce.py | Gaussian fit, top-hat fit and a Gaussian scan with the viewing angle held at 15, 20, 25, 30 and 35 degrees. |
| gw170817-frozen-slice.json | 46 rows (42 detections, 4 upper limits): VLA 3 GHz, HST F606W, Chandra/XMM-Newton 1 keV. |
| expected-outputs.json | Outputs of the last --write run. |
| provenance.json | Sources and SHA-256 checksums. |
| held_bands.py / held-band-results.json | Radio-only fits, optical and X-ray predictions, and the separate upper-limit checks. |
| arrival_reference.py / equal-arrival-reference.json | Independent quadrature and radius roots for eight angle, energy, density and time settings. |
| verify_observations.py / frozen-observation-audit.json | Every frozen field audited against the pinned primary table. |

## Units and conventions

- Days since merger, frequencies in Hz, flux densities in microjansky, cgs inside the model.
- Distance 40 Mpc; redshift neglected.
- Angles in degrees in the fit, radians inside the model. E_iso(theta) is the isotropic-equivalent
  energy per jet; the Gaussian profile is E0 exp(-theta^2 / 2 theta_c^2), truncated at 8 theta_c.
- Initial Lorentz factor 300; no lateral spreading; no synchrotron self-absorption; no inverse Compton.
- Fixed: p = 2.168 (from the measured spectral index -0.584), eps_e = 0.1, eps_B = 1e-4.
- Fitted: viewing angle, core angle, log10 E0, log10 n.

## Results (expected-outputs.json)

| Fit | theta_obs | theta_c | log10 E0 | log10 n | ratio | chi-square (42 detections) |
| --- | --- | --- | --- | --- | --- | --- |
| Gaussian | 20.94 | 3.11 | 52.292 | -2.98 | 6.734 | 53.68 |
| Top-hat | 20.07 | 8.14 | 51.926 | -3.445 | 2.466 | 204.23 |

Gaussian fits with theta_obs fixed at 15 to 35 degrees give chi-square 53.63 to 57.63 and a ratio
theta_obs / theta_c of 6.709 to 6.815.

## Radio-only fitting

| Model | Radio chi-square per detection | Held optical | Held X-ray |
| --- | --- | --- | --- |
| Gaussian | 1.37 | 1.17 | 1.36 |
| Top-hat | 6.56 | 1.76 | 4.51 |

Both fits keep the spectral index inferred from the published broadband data. This exercise
compares time evolution conditioned on that index.

## Limits

- This is a teaching fit. It does not fit eps_e, eps_B or p and reports no posterior intervals.
- No gravitational-wave inclination prior and no VLBI constraint are used.
- The model normalization differs from the Sari-Piran-Narayan analytic peak flux: the ratio is
  0.41 at day 3 and 0.27 at day 30 for a sphere. The two use different normalization
  conventions, and the analytic scalings assume Gamma >> 1 throughout.
- The same physics runs in the Lab's TypeScript engine on a finer grid; this Python code is a
  separate implementation for readers, not an independent scientific check.
