# Recipe: to the Moon

You design the burns; Dynamik propagates, checks and draws them. `compute {fn:"trajectory"}` integrates a craft under the Earth (with J2), the Moon and the Sun from JPL DE440s through impulsive burns, on every plan. It answers `ephemeris` (movers, drawn with the Moon where JPL puts it) and `events` (a table: start, each burn's delta-v, the closest approach to the Moon, the orbit after each event, states about the Earth or the Moon), and a `summary` with the burns, `closest_to_moon` (with its `b_plane`), `final_orbit` and `moon_at_start_icrf`.

## One trajectory

```
compute {project, fn:"trajectory", id:"starship", args:{name:"Starship", start, duration, step:60,
  orbit:{periapsis_altitude:200, apoapsis_altitude:200, inclination:28.5, raan, arg_periapsis, true_anomaly:0},
  burns:[{name:"TLI", time, about:"earth", dv:[3135,0,0]},
         {name:"LOI", at:"periapsis", about:"moon", to_altitude:100}]}}
```

- `orbit` angles are ICRF (equator and equinox of J2000); altitudes above 6,378.137 km or 1,737.4 km. Or give `state` {x, y, z, vx, vy, vz} km, km/s, ICRF, about `center`.
- `dv` is m/s along V (velocity relative to `about`), N (orbit normal), B (V x N). `to_altitude` makes the velocity horizontal with the speed that puts the opposite apsis there: the burn's own altitude makes it circular.
- `at:"periapsis"` burns at the next periapsis about its body inside that body's sphere (the Moon within 66,100 km). Give the parking orbit's perigee to a TLI with `at:"periapsis", about:"earth"`.

## Design, then target

1. The Moon at arrival: `compute {fn:"trajectory", keep:false, args:{start:<arrival>, duration:60, orbit:{periapsis_altitude:200}}}`, read `summary.moon_at_start_icrf`.
2. In your sandbox, two-body: the parking plane through the Moon's arrival direction for your inclination (solve for raan); the transfer ellipse from perigee whose time to the Moon's distance is your time of flight; TLI = v_perigee(transfer) - v_perigee(parking); the TLI point is the arrival direction turned back by the arrival true anomaly (argument of latitude for `arg_periapsis` or the parking phase).
3. Programs are internal-only. On Starter iterate with `compute {project,fn:"trajectory",id:"probe",draw:false,args}` and read `summary.closest_to_moon.b_plane` (B.T, B.R km, v_inf). Reuse that id, then remove it: saved computes avoid scratch's 120 CPU-second daily limit and its large samples inflating model context and credit reservations. Polar lunar orbit: B.T = 0 and |B.R| = r_p sqrt(1 + 2 mu_moon / (r_p v_inf^2)), mu_moon 4,902.8. Controls: TLI V delta-v and parking `raan`. Finite differences once (0.2 m/s, 0.01 deg), then Broyden; 4 to 6 runs a craft.
4. Keep it: the same `args` with an `id`, without `keep:false`; use `step:60` for low lunar orbits, whose two-hour revolution disappears at hour-long steps. Update with `compute {project,id,args}`, not `edits.args`. It draws `outputs.ephemeris.layer` about the Earth and `outputs.moon.layer` about the Moon; name both with `layer_put`.

## Read back and check

Read `data_read {of:"output:<id>.events"}` for every saved craft, even when compute gives a summary: each burn's `dv_m_s`, `total_dv_m_s`, and the orbit after it. Check before you answer:

- TLI from a circular LEO to the Moon's distance is about 3.1 km/s (vis-viva: sqrt(mu(2/r - 1/a)) - sqrt(mu/r)); from the perigee of an elliptical parking orbit, less.
- LOI at 100 km from v_inf 0.8 to 1.0 km/s: sqrt(v_inf^2 + 2 mu_moon / r) - sqrt(mu_moon / r), about 0.85 km/s. Into a large ellipse, a few hundred m/s.
- Arrival meets the requested deadline; no `impact` row. Check the final lunar orbit as well as insertion: a predicted perilune below the surface needs a different target ellipse or plane, not a claim of a lasting orbit.
- Say what is not modelled: the Moon's gravity field (a 100 km orbit drifts by kilometres a day), solar radiation pressure, finite burns. An ellipse of an NRHO's size is not an NRHO: call it NRHO-sized.

## Draw and answer

One computation per craft in one project. Earth and Moon layers use their own frames. Overview: `view_set {camera:{pose:{frame:"earth/inertial", lat:<90 - inclination>, lon, altitude_km:1100000}}, clock:{time:<arrival>}}`; wrap longitude to -180 through 180. Lunar orbit: `frame:"moon/inertial"`, about 6,000 to 20,000 km, looking along its orbit normal; `layer_put` its Moon layer's `style:{path:"tracks",trail:{behind:120,ahead:120}}` to see a low orbit near insertion. Render, then answer with each craft's burn table (time, body, delta-v, orbit after), totals, lunar orbit and `url`.
