Skip to content

Adjoint status ​

This page marks the current boundary of the adjoint implementation: what can be run and tested today, and which combinations are deliberately rejected. The LinRood adjoint and TM5-style inversion scaffold are implemented and covered by the core test suite, but real-data parity with TM5-4DVAR remains a validation gap.

For a first introduction, start with Learning adjoints and inversions and the executed footprint tutorial. This page is the support reference, not the starting lesson.

What is shipped ​

Forward operators (unchanged) ​

The forward transport model — advection (four schemes: UpwindScheme, SlopesScheme, PPMScheme, LinRoodPPMScheme), convection (CMFMC + TM5), implicit vertical diffusion (Beljaars–Viterbo / Holtslag–Boville Kz fields), and surface flux sources — is implemented for the runtime's supported topology/backend combinations and covered by the test suite documented in Conservation budgets.

Tape, checkpoint, and reverse pass ​

src/Adjoints/ ships a full discrete-adjoint pipeline for the cubed-sphere split-sweep path, with a separate kernel-level pipeline for LinRoodPPMScheme integrated through cs_surface_emission_footprint.

The shipped pieces, all exported through AtmosTransport.Adjoints:

SurfaceWhat it does
cs_surface_emission_footprintForward → tape → reverse pass driver for CS, returning dJ/dE_t at every surface step
cs_surface_emission_footprint_from_seedSame, but accepts an explicit final dJ/drm seed for arbitrary observation operators
cs_surface_flux_jacobianBatches layer/column objectives on one recorded tape (one reverse walk per objective), aggregates per-step footprints into named user windows
cs_surface_flux_4dvar4D-Var cost / gradient with named controls, scalar observations sampled at one step or over a time window (layer, column-mean, point samples placed in the column, and averaging-kernel columns), diagonal background; one forward run, which samples the observations and records the tape, and one reverse pass per gradient, whatever the number of observations (single-sweep gradient, observation operators, averaging-kernel columns)
cs_surface_flux_4dvar_optimizeDependency-free gradient-descent driver for the above
cs_surface_flux_4dvar_solveProduction driver with optimizer choice (CSGradientDescent / CSLBFGS)

The adjoint supports the following advection schemes (full union in CSAdjointSupportedScheme):

  • UpwindScheme()

  • SlopesScheme(NoLimiter())

  • PPMScheme(NoLimiter())

  • PPMScheme(MonotoneLimiter()) and PPMScheme(CW84Limiter()) — via the limiter branches of the base trajectory, recorded on the tape (one byte per cell and sweep; what the tape stages)

  • LinRoodPPMScheme(5) and LinRoodPPMScheme(7), with either vertical sweep: FV3's kord = 8 profile (vertical = "fv3_kord8", the configured default, or "fv3_kord8_signed") or upwind (vertical = "upwind"). The FV3 sweeps are transposed at the limiter branches of the recorded state; see the adjoint of the FV3 vertical sweep

The split-sweep PPMScheme with the FV3 vertical profile has no adjoint; neither do limited SlopesScheme and NoAdvection. The per-step air-mass reset of advection-off runs ([run] air_mass_update = "step", their default) has no adjoint record either; the setup guard names both. require_adjoint_support(scheme) returns a supported scheme and otherwise throws an error that names the missing piece. The inversion driver applies it to its [advection] table; a Lin–Rood table without vertical selects fv3_kord8 there as in forward runs.

The supporting kernel adjoints in src/Operators/Advection/linrood_adjoint_*.jl are transposition-tested (test/core/test_linrood_kernel_adjoints.jl) and finite-difference VJP-tested via single-panel and cross-panel halo compositions. The full LinRood footprint, including its two Upwind Z half-sweeps, matches central differences to about 1e-11 relative on a smooth base field. test/core/test_cs_linrood_fv3_adjoint.jl asserts 1e-9 with upward vertical flux and a surface-layer objective, the case most sensitive to which air mass the Z records hold. Every tape records the air mass entering a Z sweep, because the upwind adjoint coefficient F / m_donor uses the pre-sweep donor mass; the FV3 profile's records also hold the tracer mass entering the sweep. test/core/test_cs_linrood_fv3_adjoint.jl checks the FV3 vertical sweeps' transpose in 256-bit arithmetic and the Lin–Rood footprints with them against central differences (1e-9, upward and downward flux, ORD 5 and 7, both profiles), and Full/Stride parity.

The opt-in test/diagnostic/test_cs_transport_adjoint_gpu.jl also exercises transporting PPM and LinRood footprints on CUDA with scalar indexing disabled. It checks Float32/Float64 CPU/GPU agreement, Float64 directional finite differences, and full/stride checkpoint parity, plus the single-panel recording and reverse wrappers. LinRood copy-back and halo-gradient updates run on the array backend; the tape path does not require host scalar reads.

The CS reverse pass also supports the default, unclamped/full-column/unmerged forms of TM5Convection, CMFMCConvection, and CMFMCMatrixConvection. IFSConvection is forward-only; adjoint setups reject it. CMFMC and CMFMC-matrix have column adjoint-identity tests (for CMFMC also the transpose of the forward column matrix, sub-cloud mixing included, in test_cmfmc_ras_cloud_base.jl); the TM5/CMFMC footprint paths are checked against finite-difference emission gradients in test_cs_ppm_adjoint_footprint.jl.

The step plan: the adjoint replays production ​

The forward model the reverse pass transposes is the production step by construction (owner decision D31). A StepPlan (Operators/StepPlan) describes a run's step in four phases — window advance, window start, every step, window end — with the physics cadence, the binary window contract and the air-mass reset mode resolved once. TransportModel.step! and DrivenSimulation execute it; the adjoint replays the same plan with the production operators on a single-tracer state (CSStepSchedule; for a driven run CSStepSchedule(sim)) and records its tape through the recorder hooks of the production step (AbstractStepRecorder; production passes NoStepRecorder(), whose hooks are no-ops). Consequences:

  • physics_cadence = :window (the driven-run default under a binary window contract): convection is recorded once per met window, after the window's last step and the air-mass reset to the window's end mass, over the window length, with the window's forcing and (CMFMC) the CFL sub-step count of the forward call. Formerly the adjoint applied convection every step (finding OF-114).

  • A configuration without surface sources has the center V(Δt); footprints and 4D-Var always carry a surface-flux operator (zero rates allowed), and cs_surface_emission_footprint_from_seed(...; surface_flux = false) gives the initial-condition sensitivity of a no-emission run (initial_adjoint). Formerly the replay always built the coupled center V(Δt/2)SV(Δt/2) (finding OF-115).

  • The replay carries production's persistent operator state (the Kahan compensation of the surface source, the convection and Kz/Dkg diffusion ledger carries, the CMFMC CFL cache), and checkpoints save it, so the forward replay, the tape recording and every checkpoint restart equal the production run bit for bit.

  • require_adjoint_support(plan, ops) (also require_adjoint_support(sim)) rejects, before recording, every stage or operator without a record and an adjoint (adjoint_support(x) is the trait), naming it.

  • cs_adjoint_inputs(sim; tracer) captures the adjoint's inputs from a production DrivenSimulation (the face fluxes of every step as production refreshes them, one copy per met window that the window's steps share, or one per step when they are interpolated within the window; per-window convection forcing, diffusion layer thickness and Kz, emission rates, the schedule, a fresh convection workspace). Capture rejects initial nonzero compensation or carries and valid CMFMC CFL caches, including models continued across binaries at iteration zero. CSStepSchedule(sim) validates the production operators and retains chemistry. Reset windows and targets use the model backend. The fresh convection workspace is allocated on that backend too, including the cell-area metrics copied from the host mesh. Diffusion records carry the forward's clock, so time-varying Kz fields are transposed at the times production evaluated them.

test/core/test_cs_adjoint_step_plan_parity.jl is the parity matrix: Upwind, unlimited slopes, monotone PPM and Lin–Rood (plus CW84 PPM, unlimited PPM and Lin–Rood ORD 5 with upwind vertical) under :window and :substep cadence, with and without the binary window contract, TM5, CMFMC and CMFMC-matrix convection, both diffusion couplings, diffusion without surface sources and both reset modes, also with continuous windows whose shared reset targets give the footprints of separate copies bit for bit. Each case compares the forward replay, the tape recording and a restart from a checkpoint inside a met window with the production DrivenSimulation (== in one thread), Full/Stride footprints with each other, the captured fluxes with production's at every step (one copy per met window; shared and per-step copies give the same footprints) and, on a subset, footprints and the initial-condition sensitivity with central differences of the production simulation (Upwind ∼10−14, monotone PPM 4×10−12, Lin–Rood 5×10−11 relative).

Memory of the captured inputs ​

With the default residency = :device the captured inputs stay on the device for the whole 4D-Var period (a streamed HostCSWindowSource, below, keeps them in host memory instead). Per met window they are one set of face fluxes, the convection forcing, the diffusion operator (its Kz panels) and layer thickness, and the reset targets; per step there are only references. Two duplicates are removed at capture, both without changing any result bit:

  • Diffusion. The adjoint takes the per-window layer thickness (diffusion_layer_thickness, six (Nc, Nc, Nz) panels, read only). The replay owns its diffusion workspace and one scratch for every reverse solve; each reverse kernel overwrites a column of the scratch before it reads it. Formerly every window held a full DiffusionWorkspace (factors, layer thickness, exchange, elimination), four times the data.

  • Reset targets. With the window contract and a mass reset the plan resets to window w's end mass m + dm and, on entering window w + 1, to its start mass. CSStepSchedule(sim) stores one array for both when they are equal bit for bit, halos included (_window_reset_targets); otherwise two copies. Sharing is exact because the replay writes a target only when a reset refreshes its halos from its interior (fill_panel_halos!(…; dir = 1) rewrites every halo cell), so both resets install what separate copies would. In the MERRA-2 binaries measured every interior window boundary of a daily binary was bitwise continuous; days captured separately (one cs_adjoint_inputs per binary) keep two arrays at the day boundary.

At C90 L72 Float32 a window held 178.5 MB of device inputs before (fluxes 48.3, convection 28.4, Kz 14.0, diffusion workspace 56.0, reset targets 31.9) and holds 121.3 MB now (layer thickness 14.0; reset targets 16.6 on average over a day). Seven C90 days of inputs fell from 27.97 to 19.01 GiB, a 31-day C48 Lin–Rood 4D-Var now fits on a 46 GiB GPU, and C90 Lin–Rood fits 11 days; costs and gradients are unchanged bit for bit on one CPU thread. The measurements are in docs/memos/2026-11-08_adjoint_input_dedup.md.

How the replay reads the met: window sources ​

The replay reads every met input of a step through the step's met window (CSWindowMet): the window's face fluxes, convection forcing, diffusion operator and layer thickness, and reset targets. It acquires the window from a window source (acquire_window!) at the window's first step, or at a checkpoint restart inside the window, and returns it with release_window!:

  • an untaped forward run (and the counting run that sizes a tape) releases its window when it acquires the next one;

  • a recording run keeps the windows it reads until the driver has released the records that reference them. Before the segmented driver walks a re-recorded segment, it checks that every window the segment holds is still the one it acquired (require_current; otherwise a StaleWindowSlotError).

Two sources ship:

  • ResidentCSWindowSource (the default). The entry points build it from their per-step arguments, so every window stays in memory and releasing one does nothing. It groups the steps by window once (one flux state per window, or one per step when the fluxes change within the window; a flux_scale ≠ 1 is applied once per set) and validates each window once. The convection forcing, the diffusion operator and the layer thickness must be constant within a met window, as production refreshes them at the window's first step. Results are the same bit for bit as with the per-step vectors the source replaced.

  • HostCSWindowSource, a streamed source. cs_adjoint_inputs(sim; residency = :host, device_slots = 2, host_budget_bytes) builds it: each completed window's captured met (the same arrays the resident capture keeps) is copied to host memory and its device copies are freed; the reset targets are kept on the host too. Pass it as met_source with nothing for the three per-step flux vectors (the diffusion and convection-forcing keywords come from the source; the schedule carries no reset targets). The source keeps device_slots windows on the device. Acquiring a window that no slot holds refills the least recently used slot without a lease, in place, from the host copy; a slot is synchronized before its last lease ends, so a refill never overwrites data a queued kernel still reads. Every refill increments the slot's generation, which require_current compares. Loads are synchronous (prefetching is a later step). One source serves any number of calls (every evaluation of an optimization); close(source) frees its slots.

A streamed source accepts only MetWindowCheckpoint (plain, split, or as the AutoTape constraint) with StateTape: each re-recorded segment references one met window. FullCheckpoint, StrideCheckpoint (fixed K, or with a resident tail whose records outlive their windows' leases), AirMassTape (held across evaluations) and an unconstrained AutoTape() are an ArgumentError before the forward run, also for a cost-only evaluation. The setup checks read the source's window layout instead of acquiring every window, and a Lin–Rood tape is sized from window 1 and the longest later window: a Lin–Rood step has a fixed record structure, so each step takes the layout of the counted step of its class (window 1 or later, first or last step of its window); a class those windows lack falls back to counting every step. Recording still throws TapeReservationExceeded if a step needed more.

Exactness: the slots hold copies of the resident arrays and the replay runs the same operators on them, so costs, simulated values, footprints and gradients equal the resident inputs' bit for bit on one CPU thread (test/core/test_cs_window_source.jl, and on two days of MERRA-2 C24 meteorology for the PPM and Lin–Rood schemes).

Host memory per C90 L72 Float32 window is about 135 MB (face fluxes 48.3, convection forcing 28.4, Kz panels 14.0 plus the operator's host Kz staging copy 14.0, layer thickness 14.0, reset targets 16.6 on average): about 3.2 GB per day, so a month of windows (about 100 GB) does not fit a process budget of 60 GB on a shared host. Device memory holds the slots (about 0.3 GB for two at C90) instead of every window.

The tape: AdjointTape ​

Every reverse-pass entry point (cs_surface_emission_footprint, cs_surface_emission_footprint_from_seed, cs_surface_flux_jacobian, cs_surface_flux_4dvar, cs_surface_flux_4dvar_optimize) takes one tape keyword, defaulting to AutoTape(). It resolves and reserves one concrete tape before recording. An explicit AdjointTape(; content, storage, checkpoint) fixes three typed axes; its constructor defaults are listed below (src/Tape/AdjointTape.jl):

AxisChoicesDefault
content: what each record stages, how long a recording servesStateTape() (what each operator's reverse reads of this forward run; recorded by every forward run), AirMassTape() (the air mass only; recorded once per call or optimization)StateTape()
storage: where the staged states liveDeviceCSTapeStorage(), PinnedHostCSTapeStorage(), MmapCSTapeStorage(dir)DeviceCSTapeStorage()
checkpoint: how much of the run is taped at onceFullCheckpoint(), StrideCheckpoint(K), MetWindowCheckpoint()FullCheckpoint()

Every storage and schedule is valid for the split-sweep schemes; the Lin–Rood tape supports device storage only; AirMassTape() takes FullCheckpoint() and a scheme whose reverse reads the air mass only (below). The 4D-Var driver reads the same choices from [adjoint.tape] (scripts/inversions/cs_4dvar.jl).

What the tape stages ​

The reverse of a split-sweep step reads, per operator, the air mass that entered it in the forward run (the sweeps rebuild each face coefficient from the donor air mass, the diffusion and convection solves and the air-mass reset read it too) and, for a monotone or CW84 PPM sweep, the limiter branches the forward took. A record stages each of these once (StateTape, the recorders in src/Adjoints/Footprint/TapeRecorder.jl):

RecordStagesShares
sweep (X, Y, Z)the air mass entering it; limited PPM: one byte of limiter branches per cell—
palindrome center (diffusion)the air mass at the centerthe reverse half's first Z sweep reads the same slot
convection—the next step's first X sweep's slot, or the old air mass of a following :preserve_vmr reset; its own slot only before a :preserve_tracer_mass reset or at the end of a recording
:preserve_vmr resetthe old and the target air mass—
Lin–Rood recordsevery state they read (unchanged; device copies outside the pool for the horizontal operator)nothing

The sharing is exact, not an approximation:

  • The center's air mass is the buffer the first reverse-half Z sweep enters with: the midpoint writes only the tracer (surface fluxes, the diffusion of the mixing ratio), and no halo fill runs in between.

  • Convection leaves the air mass unchanged, and its reverse (_apply_cs_convection_adjoint!: CMFMC, TM5 and the CMFMC matrix, which delegates to TM5) reads interior columns only. Until the next sweep, only the step-start halo fill writes the air mass, and only its halos; an air-mass reset is recorded before it runs, so its old air mass equals the convection's, halos included.

  • For fixed limiter branches the limited PPM face flux is linear in the tracer mass, and its derivative depends on the tracer mass only through those branches. With ci=ri/max(mi,mfloor), the face flux for F≥0 is

    f=αr1+α(1−α)s1,s1=m1[(qR−c1)−κ1],α=clamp(F/m1,0,1),

    where the edges qL,qR of cell 1 are the fourth-order edge (monotone) or CW84 eq. 1.7 with minmod slopes, reset by the monotonicity constraints (CW84 eqs. 1.10), and κ1=αb0 the CW84 curvature term (zero for the monotone limiter). Every piece is linear in c once the minmod branch of each slope (zero, centred, forward, backward) and the profile branch of each cell (extremum, left edge reset, right edge reset, unchanged) are fixed; m and F give the rest. So the tape keeps one byte per cell and sweep (PPMLimiterCodes, bits 0–1 the profile branch, bits 2–3 the CW84 minmod branch), computed when the record is made from the staged state with the predicates and values the reverse used to evaluate on the tracer mass, each cell's stencil clamped to its axis as the reverse clamps it (y at the padded panel edges, z at the column ends; the minmod is zero whenever two of its cells are equal, so the clamped stencil of a cell gives the code of every face that reads it). The reverse kernels (_ppm_face_coeffs) apply the derivative of the recorded branch in the operation order of the previous tracer-mass reverse; on one CPU thread the gradients are bit for bit those of the tracer-mass tape (the bitwise gate below; 0 of 1.6 million random faces differed in a direct comparison, all branches taken). One fused launch computes the codes of a sweep's six panels.

Tape per palindrome pass, with S the bytes of one air-mass state (6(Nc+2Hp)2Nz floats) and f the float size: 6S for the linear schemes and 6S(1+1/f) for limited PPM, plus 2S per :preserve_vmr reset and S per convection record with no later air mass to share (the last one of a recording, one before a :preserve_tracer_mass reset). cs_tape_byte_estimate counts it, and the mmap tier reserves it, with the recorder's own hooks (_CSCountingRecorder). On the C90 MERRA-2 day of the benchmarks (Float32, 48 steps, S = 15.93 MB, CPU size accounting of the recorded tape):

scheme, physicsbefore: states, MB per step (GB per day)now: states, MB per step (GB per day)
upwind, slopes, PPM; no physics6, 95.6 (21.5)6, 95.6 (21.5)
upwind, slopes, PPM; convection + diffusion8, 127.4 (28.7)6.02, 95.9 (21.6): −25 %
monotone, CW84 PPM; no physics12, 191.1 (43.0)7.5, 119.4 (26.9): −38 %
monotone, CW84 PPM; convection + diffusion14, 223.0 (50.2)7.52, 119.8 (27.0): −46 %

States are air-mass-state equivalents (a code panel counts 1/4); a day is 225.14 steps. CMFMC convection and diffusion every step, CFL subcycles of the replay (one pass per step on this day). The counted bytes equal the bytes of a recorded device tape (first 4 steps), and cs_tape_byte_estimate equals the count.

The record of a limited PPM sweep makes one code launch in place of six tracer-mass copies, and the center and convection records make none in place of six copies each: per step with convection and diffusion, 84 staging launches become 42 for limited PPM and 48 become 36 for the linear schemes. The reverse pass launches the same kernels as before, reading one byte per cell instead of the tracer mass.

AirMassTape() is for the schemes whose reverse reads the air mass only, tape_dependence(scheme) isa AirMassDependence: UpwindScheme(), SlopesScheme(NoLimiter()) and PPMScheme(NoLimiter()), whose sweeps are linear in the tracer mass; the reverse of every other operator with an adjoint (convection, diffusion, surface fluxes, resets) reads only the air mass, the met forcing and the clock. Its tape does not depend on the tracer or the emissions, so the first forward run of a call records it and every later forward run of the call (the later evaluations of cs_surface_flux_4dvar_optimize) runs without a recorder and walks it. Before a reuse the call recomputes the forward run's identity and compares it with the recording's: the grid, the number of taped steps, the met windows (window, step length and clock of every step), the scheme, step plan and operators, and content checksums of the met inputs (initial air mass, face fluxes, convection forcing, diffusion layer thickness, and the interiors of the reset targets, whose halos a reset refreshes in place; a position-weighted wrapping sum of bit patterns, the same on any backend); and the tape pool's generation. Device reductions collect all checksums into one result vector with one host transfer per evaluation. The checksums detect permutations but remain a within-runtime guard, not a collision-free fingerprint. Holding the tape also rejects records with tracer payloads. A difference is an ArgumentError naming it. Limited PPM and Lin–Rood (TracerDependence) with AirMassTape() are an ArgumentError before any forward run, as is AirMassTape() with StrideCheckpoint or MetWindowCheckpoint (their windows are re-recorded from tracer-dependent checkpoints in every reverse pass). On one CPU thread (C32 L20 Float64, 8 steps, upwind with TM5 convection and diffusion, a three-evaluation optimization, before the position-weighted checksum change) there was no measurable difference with a CPU device-array tape (min-of-three −0.4 %, within run-to-run spread), and an mmap evaluation took 1.5 % less time; the costs were the same bit for bit. GPU timings remain pending. With flux_scale ≠ 1, the held tape retains the first evaluation's scaled flux copies throughout the optimization; later evaluations allocate another set, so peak scaled-flux memory is twice one set.

Checkpoint schedules ​

PolicyMemoryRecompute
FullCheckpointO(N) state snapshotsnone — fastest reverse pass
StrideCheckpoint(K)one K-step window plus N/K checkpointsone extra forward run
StrideCheckpoint(K; resident_tail = n, snapshots = PinnedHostCSTapeStorage())the resident tail, one head window and host tracer/air-mass checkpointsre-records the head only
MetWindowCheckpoint(; split_steps = 0, snapshots = PinnedHostCSTapeStorage())the longest met window (or split_steps-bounded piece) plus one checkpoint per segment, tracer/air mass on the host by defaultone extra forward run

MetWindowCheckpoint places its checkpoints at the met-window boundaries of the step schedule (CSStepSchedule.steps_per_window, variable in real binaries: 2–3 steps per window at C24, 5–6 at C48 and 8–11 at C90 in the first week of December 2021 of the MERRA-2 binaries), so no segment straddles two met windows and a segment's tape refers to one window's met inputs only, the property a streamed reverse pass needs. split_steps > 0 cuts a window of n steps into cld(n, split_steps) pieces of nearly equal length, never across a window boundary; a run that ends inside a met window ends its last segment there. It uses the StrideCheckpoint driver with a different segment list, built in one pass from the met window of every step. The pool reserves the largest per-type count over the segments; there is no resident tail. Explicit AdjointTape(; checkpoint = MetWindowCheckpoint(...)) and AutoTape(; checkpoint = MetWindowCheckpoint(...)) (sized and checked against the budget, then reserved) are supported; unconstrained AutoTape() does not consider it, so its choices are unchanged. On one CPU thread the 4D-Var cost, simulated values and gradient equal FullCheckpoint's bit for bit on 2 days of C24 MERRA-2 binaries (ppm_linear and linrood7, CMFMC convection and Holtslag–Boville diffusion), as do the bitwise gate's variants.

The schedules share one driver per policy for every scheme family; a checkpoint is a replay snapshot (tracer and air mass with halos plus the persistent operator state), so a restart inside a met window runs that window's remaining steps and window-end stages exactly as the recording run. StrideCheckpoint records each window once and walks it for every adjoint start (all objectives of a Jacobian). On one CPU thread every schedule and tape storage reproduces FullCheckpoint with device storage bit for bit; the bitwise gate below checks it. There is no logarithmic-memory schedule: the bisection RevolveCheckpoint was removed (it measured 2.0–2.1× the time of StrideCheckpoint with no lower device peak); an optimal binomial schedule will be added when a case needs it.

Tape storage and its pools ​

The tape records can live on the device, in pinned host memory, or on disk. AutoTape() tries device storage first; supplied fields constrain selection. Each call reserves its runtime pools from the resolved storage (src/Tape/TapeStorage.jl, HostTapeArena.jl, MmapTapeStorage.jl), records every tape of the call into it (each stride window rewinds it and reuses its memory), and releases it explicitly when it returns: device arrays go back to the GPU pool and pinned host memory to the system then, not at a later garbage collection. cs_surface_flux_4dvar_optimize keeps one pool for all its evaluations. A record read after its pool was rewound or released throws a StaleTapeSlotError. The pinned-host pool reserves its host memory and registers its chunks with the GPU driver before recording, so staging a state copies into memory that is already pinned. Staging and reads are synchronous copies; the read caches are reserved too.

Measured on an L40S (C90 MERRA-2, 48 steps of 7.5 min, convection and diffusion, Float32, single runs comparing the pool-reuse implementation against the earlier per-window tapes, before AutoTape reservation):

beforeafter pool reuse
device peak, StrideCheckpoint, upwind / monotone PPM7.13 / 11.38 GiB2.35 / 3.03 GiB
device peak, 4-evaluation optimization, device FullCheckpoint / StrideCheckpoint (upwind)22.5 / 22.5 GiB7.3 / 2.9 GiB
pinned-host re-record of a tape into its pool29–48 ms/step9.7–13.9 ms/step (≈ 24 GB/s device-to-host)
pinned-host footprint or evaluation, StrideCheckpoint / FullCheckpoint—50–56 % / 4–19 % less time
optimization evaluation, mmap / pinned-host FullCheckpoint—55–56 % / 42–44 % less time
single device-tape call—0–2 % more time (the tape is freed inside the call)

The on-disk tier needs a scratch directory; there is no default (a C90 day of PPM tape is tens of GB, more than a typical /tmp). Before recording, the call sizes the largest tape from the binary schedule or one cached CFL/Lin–Rood counting replay, checks that dir has that much space plus 10 %, and reserves an anonymous file with fallocate. The file is mapped once, reused within its reservation, and unmapped and closed when the call returns. An entry point raises TapeReservationError if an explicit reservation fails; Auto tries another permitted candidate. Recording beyond the reservation raises TapeReservationExceeded. Reserved extents on ext4 or XFS protect writes against a full-disk SIGBUS; file systems that cannot reserve in-place writes (including ZFS) are rejected by name. Tapes are not persisted.

Bitwise gate of the tape ​

test/core/test_cs_adjoint_tape_bitwise.jl runs a pairwise set of ten configurations in a single-threaded subprocess (test/helpers/cs_adjoint_tape_harness.jl): upwind, unlimited slopes and PPM, monotone and CW84 PPM, Lin–Rood, Float64 and Float32, TM5, CMFMC and CMFMC-matrix convection at substep and window cadence, both diffusion couplings, every air-mass reset mode and CFL subcycles. The fields are rough (random values, spikes, exact zeros and ties, sign changes; fluxes from a random streamfunction per panel plus noise; nonzero emission rates), and the test requires every branch of the monotone profile limiter and of the CW84 minmod slopes to occur in the limiter codes of the recorded tape, per axis and next to the panel seams. Footprints, initial adjoints, seeded and multi-objective footprints (the Jacobian also under StrideCheckpoint, whose windows serve both objectives), the 4D-Var cost, simulated values and gradient (with a co-sample after the last assimilated step) and a two-iteration gradient-descent optimization (whose evaluations share one tape pool) of every storage × schedule variant (including MetWindowCheckpoint with and without split met windows, with host and device snapshots, on met windows of 2, 3 and 2 steps), and for the linear schemes of AirMassTape() (the optimization's later evaluations reuse the first one's tape), must equal the default tape's reference bit for bit, and the reference must equal the SHA-1 digests recorded from main on the reference host (test/fixtures/cs_adjoint_tape_digests.toml). Main recorded the tracer mass of every limited sweep, so the gate holds the limiter codes, the shared center and convection slots and the reused air-mass tape to the gradients of the uncompacted tape. Two of the configurations also evaluate every observation operator of the 4D-Var (a time window, placed points by height and by pressure, averaging-kernel columns with the rebin and the hat projection, a cell average, a column the surface-pressure screen leaves out, so that the reverse pass starts before the end of the tape, and a co-sample after it), with background terms and in the preconditioned space. Two others take their inputs from cs_adjoint_inputs of a production DrivenSimulation with rough met windows, so the gate also covers the capture: each met window's shared fluxes under CFL subcycles, full-window flux storage scaled per window, CMFMC sub-steps and per-window diffusion layer thickness, and under the binary window contract with a window-end reset. The full matrix (50 configurations, two Lin–Rood ones and the two captured ones) is the opt-in test/diagnostic/test_cs_adjoint_tape_bitwise_full.jl. test/core/test_cs_tape_compaction.jl checks which records share a slot, that the counting recorder and cs_tape_byte_estimate count the recorded tape exactly, and the bytes per step.

Inversion scaffold ​

src/Adjoints/Inversion/ ships:

ModuleSurface
Covariance.jlDiagonalCSCovariance, IsotropicGaussianCSCovariance, apply_B_half!, apply_B_half_adjoint!
Preconditioning.jlapply_preconditioner!, LinearOptimType, LogNormalOptimType
Optimizer.jlAbstractCSOptimizer, CSGradientDescent, CSLBFGS (via multiple-dispatch surfaces) and their shared stop rules
Jacobian.jlFootprint-Jacobian assembly for batched observation operators
CostGradient.jl4D-Var cost / gradient evaluator with preconditioning
Observations.jl, ObservationColumns.jl, ObservationBinding.jl, ObservationsIO.jlTyped observation surface (time sampling, placed point objectives, column meteorology, stencils) + on-disk schema v2
DeparturesIO.jlDepartures-file IO

These are all on CI. Full inversion tests: test_cs_4dvar_single_sweep.jl, test_cs_4dvar_placed_observations.jl, test_cs_gradient_checks.jl, test_cs_lbfgs.jl, test_cs_optimizer_stop_rules.jl, test_cs_inversion_driver.jl, test_cs_inversion_truth_recovery.jl, test_cs_iteration_log.jl, test_cs_observations_io.jl, test_cs_observation_binding.jl, test_cs_departures_io.jl, test_cs_covariance.jl, test_cs_preconditioning.jl, test_cs_optimizer_dispatch.jl.

Single-sweep 4D-Var gradient ​

cs_surface_flux_4dvar computes the observation term of the cost and its gradient with one forward run and one reverse pass, whatever the number of observations, as TM5-4DVAR does: the forward run that samples the observations also records the tape the reverse pass walks.

Observations as linear functionals. An observation i is sampled after the model steps s∈Si of its time sampling (one step, or the steps of a time window with weights ωis summing to 1; see observation operators), after convection: the state runtime output and the forward observation sampler see. The meteorology is fixed, so the air mass m of every step is known before any tracer is transported, and the observation operator is a fixed sparse linear functional of the tracer masses rs at those steps,

Hi(x)=ci+∑s∈Siωis∑ewisers[e],

with weights from the replayed air mass at each s: w=1/max(m,ϵ) on one cell for CSLayerMeanObjective and for the layer a CSPlacedPointObjective reads at that step (ϵ the machine epsilon, the floor of its evaluation), w=1/∑kmk on every level of a column for CSColumnMeanObjective, and ci=0; an averaging-kernel column (CSRetrievalColumnObjective) has wk=Hk/max(mk,ϵ) on every level and its prior term as the offset ci (averaging-kernel columns). Its gradient ∂Hi/∂rs is ωiswis. An instantaneous observation (one step, ω=1) gives exactly the functional it had before time windows existed.

One reverse sweep. Write Ms for the tangent-linear model of step s (the transport palindrome with the surface-flux center, then convection) and λs for the adjoint state at the end of step s. The cost Jobs=12∑i((Hi(x)−yi)/σi)2 has the gradient of a sum, so all observations share one recursion,

λN=fN,λs=Ms+1Tλs+1+fs,fs=∑i:s∈SiHi(x)−yiσi2ωiswis,

run from the last assimilated step N back to the first step. Every recorder ends each step with an end-of-step marker (_CSStepEndRecord, after the convection record); the reverse walk adds fs there, before it transposes that step's convection. This is the TM5-4DVAR structure: its forcing enters just before the adjoint of the operator after which the forward run sampled. Where the walk reaches the midpoint of step t (the palindrome center, where the step's surface emissions enter) it reads Δtλ as ∂Jobs/∂Et, and each control window sums its steps.

The forcing entries are grouped by step, panel and cell; the injection kernel gives each cell one work item, which adds the cell's entries in input order, so the injection is deterministic without atomics.

One forward run. The forcing needs every Hi(x), so the reverse pass can only start after the forward run; but the tape does not depend on the forcing. The forward run that samples the observations therefore records what the tape's checkpoint schedule needs for its reverse pass, through the last step of an observation with assimilate = true (co-samples after it run the forward but are not taped): the whole tape with FullCheckpoint, the state at every K-step boundary with StrideCheckpoint(K). After the forcing is assembled, the reverse pass starts from zero at the last assimilated step N, which the surface-pressure screen of averaging-kernel columns may move before the end of the tape (the screen only removes observations); the walk then starts at that step's end-of-step marker. With FullCheckpoint one evaluation therefore costs one forward run and one reverse pass (previously a forward run for H(x) and a second, taped, forward run); StrideCheckpoint adds the re-recording of its head windows, as for a footprint. CFL-driven schedules and Lin–Rood add one cached sizing replay per runtime; binary split-sweep schedules use their closed-form size, including for mmap storage. The gradients, the cost and the simulated values are bit for bit those of the two-run evaluation (test/core/test_cs_adjoint_tape_bitwise.jl). evaluation = CSCostOnly() runs the forward run alone, without a tape, for callers that need the cost or the simulated values but not the gradient. Steps after the last assimilated observation do not affect the cost and get a zero gradient. tape chooses how the tape is stored. test/core/test_cs_4dvar_single_sweep.jl checks the gradient against one footprint per observation (the previous algorithm, rebuilt in the test) for upwind, unlimited slopes and PPM, monotone PPM and Lin–Rood with diffusion of both surface-flux couplings, TM5 and CMFMC convection and the Full and Stride checkpoint schedules, and for CW84-limited PPM and Lin–Rood with the FV3 vertical sweep (10−12 relative in Float64, 10−5 in Float32), counts tape reads to confirm one recording and one reverse pass per gradient, counts the replayed steps to confirm one forward run per evaluation, and counts the pool's opens, rewinds and releases to confirm one pool per optimization.

cs_surface_flux_jacobian likewise records its tape (with StrideCheckpoint each window) once and walks it once per objective; each walk is the one a separate cs_surface_emission_footprint call makes.

Observation operators: point samples in the column and time windows ​

Observation files use schema v2 (schemas/cs_observations_v2.toml): Float64 coordinates, a kind ("point" for an in-situ sample, "column" for a column average), the vertical fields elevation_m, intake_height_m, altitude_m and pressure_pa (NaN when a record does not give them), a time_window_s, a quality flag and a model_sigma. bind_to_mesh turns each record into a CSObservation; its sigma is σ=(σobs2+σmod2)1/2.

Cell. The record binds to the cell that contains its point (the grid-box stencil), found with the forward observation sampler's locator, so a record is sampled and inverted in the same cell. Float32 coordinates (schema v1) could put a point within rounding of a cell edge into the neighbouring cell; v1 files are rejected.

Vertical placement. A point record becomes a CSPlacedPointObjective, placed once by the forward sampler's rule (Observations.resolve_placement) with the vertical_coordinates of the placement settings, in preference order (default height above ground, then pressure):

  • by its height above the true ground h: intake_height_m, else altitude_m − elevation_m, else altitude_m minus a DEM elevation at the point. h is measured up from the model surface, whatever the model surface height: the model ground lies above or below the true ground, and a mountain station belongs in the model's lowest layers, not at its absolute altitude. Absolute altitudes are an explicit option (absolute_altitude_above_agl_m);

  • by its pressure pobs (a total pressure).

A point record that gives none of the coordinates in use is unplaced: an error, or dropped and counted with unplaced_records = "skip". A column record without a retrieval block becomes a CSColumnMeanObjective; one with a block becomes an averaging-kernel column.

The layer is chosen at every sampled state s from that state's replayed dry air mass mk (kg per cell, k=1 at the top) and the column meteorology (CSObservationColumnMet: model-top pressure ptop, gravity g, layer temperature Tk, specific humidity qk), with the forward sampler's function Observations.sample_column_layer!. With the cell area A, the total interface pressures (from the dry air mass and the humidity) and the hypsometric interface heights above the model surface are

p1/2=ptop,pk+1/2=pk−1/2+gmkA(1−qk),zN+1/2=0,zk−1/2=zk+1/2+RdTk(1+cqk)gln⁡pk+1/2pk−1/2,

with the virtual temperature Tk(1+cqk), c=Rv/Rd−1 (VIRTUAL_TEMPERATURE_FACTOR, 0.61) (pressure_basis = "total", the default). With pressure_basis = "dry", for meteorology without humidity, both are built from the dry interfaces pk+1/2d=pk−1/2d+gmk/A and Tk: an approximation whose heights are about 2 % low in a humid boundary layer (finding OF-117). A record placed by height reads the layer with zk+1/2≤h<zk−1/2 (the lowest layer for h≤0); a record placed by pressure reads the layer with pk−1/2≤pobs<pk+1/2 of the same basis. The sampled value is the dry mixing ratio of that layer, hs=rs,ks/max(ms,ks,ϵ). The layer depends only on the meteorology, not on the tracer, so it is fixed in an evaluation and the functional weight 1/max(ms,ks,ϵ) is the exact derivative of the evaluated value: every vertical coordinate enters the adjoint by construction.

Time sampling. Step s covers Is=[t0+(s−1)Δt,t0+sΔt) and is represented by the state at its end. A record without a window samples the step whose interval contains its time t (ω=1). A record with a window W>0 samples

ωs=|Is∩[t−W/2,t+W/2]∩[t0,t0+NΔt)|∑s′|Is′∩[t−W/2,t+W/2]∩[t0,t0+NΔt)|,H=∑sωshs:

steps fully inside the window weigh Δt, a step partly inside weighs its overlap, and the window is truncated at the run's ends and renormalised. A window narrower than a step inside one interval is the instantaneous sample. Whether a record is in the run is decided by its time t (out_of_range_policy); ClampOutOfRange keeps a record outside the run: the part of its window inside the run, truncated and renormalised, or the first or last step when it has no window or the window misses the run (departure rows mark such records clamped). Window records need the run length (nsteps), which bounds their steps.

This time sampling differs from the forward observation sampler's: the sampler takes its samples at met-window ends (a linear blend of the two ends around the record time, or the nearest end), while the inversion takes the state at the end of the model step containing the record time (up to one step Δt after it) or the Δt-weighted mean over its window. Both place a sample in the same layer of the same state and read the same value there; for an arbitrary record time they sample different states.

CS4DVarResult.stencils returns, per observation, the cell, the sampled steps and weights, the level read at each step, the placement code and the height or pressure that placed it; build_departure_set joins them to the records (by id) in schema-v2 departure files (schemas/cs_departures_v2.toml).

Relation to TM5-4DVAR. Matched: the sample is taken right after a fixed operator (here the end of the step, after convection) and its forcing enters just before that operator's transpose, in one reverse sweep; windows are Δt/ΣΔt means with weights fixed at binding; the horizontal stencil is fixed per observation (TM5 also offers slopes and trilinear stencils; ours is the grid box, a type that can gain a trilinear form for both the sampler and the inversion); the vertical stencil is chosen from the same meteorology forward and in reverse. Different: points are placed by their height above the true ground measured from the model surface (TM5 compares sea-level altitudes with heights on the model orography and never samples layer 1); pressure placement is in the adjoint too (TM5's adjoint uses the altitude only, so its gradient of pressure-coordinate points is wrong); a step partly inside a window counts its overlap (TM5 counts the half steps fully inside); the basis is dry; and records carry their flag, elevation, intake height and pressure.

test/core/test_cs_4dvar_placed_observations.jl checks parity with the forward sampler on one state (same layer and value through build_observation_sampler in nearest-window mode, records sampled at the window end that closes their model step, and through bind_to_mesh + cs_surface_flux_4dvar), a 10 m intake against a 5 km aircraft sample, total against dry pressure placement, layers that follow the replayed air mass step by step, a window observation's value and gradient against the Δt-weighted sum of its single-step observations (10−12), the single sweep against per-observation footprints for placed and window observations (10−12, upwind, monotone PPM and Lin–Rood, TM5 convection, Full and Stride checkpoints), and central finite differences of the cost.

Averaging-kernel columns ​

A retrieved column-averaged dry mole fraction (OCO-2, GOSAT, TROPOMI, TCCON) is not the column mean of the true profile: the retrieval weights the atmosphere with its averaging kernel and fills in its prior where it has little sensitivity. A column record that carries a retrieval block (schema v2, schemas/cs_observations_v2.toml) is compared with the model through that retrieval's own vertical description, with the operator of TM5-4DVAR (deps/tm5-cy3-4dvar/base/f2py/src/averaging_kernels.F90, the OCO-2 routine applyaveragingkernel and the rebin inv_rebin). A block describes either layer means (retrieval_mapping = "layer_rebin", this section) or values on levels with the profile linear between them ("hat_levels", the OCO-2/OCO-3 retrievals; see level retrievals). A layer block gives the retrieval's N layers from the surface up:

  • interfaces p1r>p2r>⋯>pN+1r≥0 (Pa, total pressure), with p1r=psr the retrieval surface pressure and pN+1r=ptr its top;

  • pressure weights hi≥0 with ∑ihi=1 (checked to 10−4: a deficit δ would bias the column by δX);

  • the column averaging kernel ai, normalized, so that ∂X/∂xi=hiai on the retrieval layers;

  • the prior profile xa,i (dry mole fraction, mol mol⁻¹; files state the unit, ppm and ppb are converted). The operator adds it to the model's dry mole fractions, so the record value and errors and the transported tracer are in mol mol⁻¹ too: such a file states a mole-fraction unit on value, value_sigma and model_sigma (converted to mol mol⁻¹), and a record must be an absolute column average, a value in (0, 1] with errors not above it;

  • optionally retrieval_profile_sigma, an uncertainty σi of the model profile on the retrieval layers (mol mol⁻¹), for the model-error term below.

bind_to_mesh turns such a record into a CSRetrievalColumnObjective. At every sampled state the model's interface pressures pk+1/2 of its cell are built as for pressure placement: total pressures from the replayed dry air mass and the specific humidity, pk+1/2=pk−1/2+gmk/(A(1−qk)) (pressure_basis = "total", the default; retrieval pressures are total pressures), or the dry interfaces ("dry"). Model surface and top are ps=pNz+1/2 and pt=p1/2. Then (Observations.retrieval_column_weights!):

  1. Stretch. The model interfaces are mapped linearly onto the retrieval's pressure range, the model surface onto the retrieval surface and the model top onto the retrieval top:
sk+1/2=μpk+1/2+c,μ=psr−ptrps−pt,c=12[(psr−μps)+(ptr−μpt)].
  1. Rebin. Each retrieval layer takes the pressure-weighted mean of the stretched model layers it overlaps:
Rik=|[sk−1/2,sk+1/2]∩[pi+1r,pir]|pir−pi+1r,(Rx)i=∑kRikxk,

with the outermost retrieval interfaces taken from the stretched model column (TM5's choice; they equal psr and ptr to rounding). Every row of R sums to 1 and ∑iRik(pir−pi+1r)=sk+1/2−sk−1/2: the rebin conserves the column integral.

  1. Column.
X=∑ihiai(Rx)i+∑ihi(1−ai)xa,i=∑kHkxk+c,Hk=∑ihiaiRik,

on the model's dry mixing ratios xk=rk/max(mk,ϵ).

The pressures depend on the meteorology only, so X is linear in the tracer mass with weights wk=Hk/max(mk,ϵ) on every level of the cell and the offset c (_linear_functional is the exact derivative of the evaluation; a window record takes the ωs-weighted sum over its steps, each step with its own pressures). The adjoint forcing is TM5's per-layer H⋅(X−y)/σ2.

What the stretch does physically. The stretch keeps every model layer's share of the column, (sk+1/2−sk−1/2)/(psr−ptr)=(pk+1/2−pk−1/2)/(ps−pt): it moves the model column onto the retrieval's pressure range without adding or removing air, and it absorbs both a surface-pressure mismatch Δps=ps−psr and a different top. With a=1 and weights normalized to the retrieval column the operator returns the model's pressure-weighted column mean for any Δps. A mismatch changes only where the model's layers sit against the vertical structure of the kernel. Where the model surface lies below the sounding's true ground (Δps>0, smoothed model orography under a mountain sounding), the model's boundary layer is compressed onto the retrieval's lowest layers instead of being cut off below the retrieval surface, which would remove boundary-layer air, and its enhancements, from the column; a 30 hPa mismatch moves every interface by about 3 % of its pressure. This is why the default is no limit, as in TM5. The same holds at the top: when the retrieval top lies well below the model top (a retrieval grid that stops at 10 hPa, say), the stretch packs the whole model column, stratosphere included, into the retrieval's pressure range, as TM5 does, so X is a full-column mean of the model, not a partial column above the retrieval top.

Surface-pressure mismatch policy. max_surface_pressure_mismatch_pa (bind_to_mesh keyword; [observations.placement] key of the inversion driver; default no limit) bounds |Δps|, the time-weighted model minus retrieval surface pressure over the sampled steps, with the model surface pressure of the stretch's pressure basis (with "dry", Δps also contains the vapour weight, about 0.5 % of the surface pressure). A record above the limit follows the unplaced policy: with unplaced_records = "error" (default) the 4D-Var stops and names the records; with "skip" the record is reported (simulated value, residual, its stencil with assimilated = false) but adds nothing to the cost or the gradient, and the number of such records is logged. The model surface pressure is known only at the sampled states, so the check runs in the 4D-Var's forward replay; it depends on the meteorology only, so the same records are left out in every iteration.

Model-error term. When the record gives a model-profile uncertainty σi (retrieval_profile_sigma), the observation error is σ2=σobs2+σmod2+∑i(hiaiσi)2 (Observations.retrieval_model_error_variance): the operator's weights on the retrieval layers, squared, times the profile variance, the layers taken independent. It depends on the record only, not on the state or the meteorology, so it is fixed when the record is bound and the cost stays quadratic in every iteration (TM5 freezes its σmod at the first iteration for the same reason). It is analogous to, not the same as, TM5's term: TM5's CH4 TROPOMI routine (averaging_kernels.F90, line 669) evaluates ∑kHk2σk2 with the weights H=(ha)R on the model layers and the spread of the modelled profiles, and its OCO-2 routine adds a column model error instead. The input is an uncertainty of the model profile (representation or transport error). Do not pass the retrieval's a-priori uncertainty: the smoothing error it causes is already represented by the averaging kernel.

Diagnostics. The stencil of an averaging-kernel column (CSObservationStencil, placement code RETRIEVAL_COLUMN_PLACEMENT_CODE = −3, level 0) carries Δps, the model profile on the retrieval layers Rx (time-weighted) and assimilated; departure files add them with the profile model sigma (schemas/cs_departures_v2.toml).

Relation to TM5-4DVAR. Matched: the stretch (including its averaging of the two offsets), the overlap matrix of inv_rebin with its outer interfaces, the column and its per-layer forcing; the test reproduces values computed with TM5's Fortran on fixed columns to 10−14 (the stretched interfaces exactly; H can differ in the last bit from the Fortran's matmul summation order). Different:

  • the operator is in the model, a linear functional of the tracer mass at each sampled step (a window record averages the operator over its steps), where TM5 writes the Δt-averaged profile and surface pressure to a track file and applies the kernel once in Python;

  • dry basis: x is the dry mixing ratio and the interfaces are total pressures rebuilt from the dry air mass and the humidity; TM5 uses moist rm/m and AT+BTps of its moist surface pressure;

  • the pressure weights come from the record and must sum to 1; TM5's OCO-2 routine computes hi=Δpi/psr, which leaves out the column above a retrieval top above 0 Pa;

  • an explicit mismatch policy, where TM5's OCO-2 path has none and its CH4 TROPOMI path sets a huge σ above a tolerance (the same effect as "skip");

  • a rebin that fails TM5's checks (possible only for a retrieval layer thinner than the stretch's rounding) is an error, not a silent zero departure;

  • the model-profile uncertainty is given on the retrieval layers by the record; TM5's CH4 TROPOMI path uses the spread of the modelled profile on model layers from its track file.

test/core/test_cs_4dvar_retrieval_columns.jl checks agreement with TM5's Fortran (reference values computed with averaging_kernels.F90 itself, with the model surface below and above the retrieval's and the retrieval top above and below the model top) and with a line-by-line transcription on random columns, conservation by the rebin, the pressure-weighted and air-mass-weighted column means for a=1 on the model's levels, the schema block (units, ordering, NaN padding, errors), the single sweep against a seeded footprint per observation and step (10−12; upwind, monotone PPM, Lin–Rood, TM5 convection, Full and Stride checkpoints), central finite differences with window records, the mismatch policy, and the departure diagnostics.

Level retrievals (OCO-2, OCO-3): the hat projection ​

The OCO-2 and OCO-3 retrievals (Lite files and the v11 MIP 10-second files) give their prior and column averaging kernel on 20 levels, σj=pj/ps from 10−4 (top) to 1 (surface), "defined on layer boundaries": the retrieval's state vector holds the profile at the levels and the profile is linear in pressure between them. Their pressure weights are the integrals of the levels' hat functions φj (φj(pj)=1, falling linearly to 0 at the neighbouring levels): the trapezoid rule, half-thickness end levels, with a dry-air correction. A record whose block has retrieval_mapping = "hat_levels" (Observations.HatLevelProjection) stores the 20 level pressures, weights hj, kernel aj and prior xa,j surface first, and the model column is projected onto the hats.

Projection. The model interfaces are stretched as for layer retrievals, the model surface onto the bottom level and the model top onto the top level (sk±1/2, each layer keeping its share of the column). The level values cj of the model profile are its L2 (Galerkin) projection onto the hats in dry air: the piecewise-linear profile ∑jcjφj closest to the model profile, weighted by dry air,

∑iMjici=bj,Mji=∫φjφidpdry,bj=∫φjxdpdry=∑kBjkxk,Bjk=mksk+1/2−sk−1/2∫sk−1/2sk+1/2φj(p)dp,

each stretched layer's dry air mk spread evenly over its pressure thickness; M is tridiagonal (neighbouring hats overlap), so P=M−1B costs one tridiagonal solve per column and step. The column is the operator of the layer case with P in place of the rebin R:

X=Xa+∑jhjaj(cj−xa,j)=∑kHkxk+c,H=(h∘a)TM−1B,

with h, a, xa and the prior column Xa from the file (the MIP protocol's eq. 2; the kernel is already normalized: it is not multiplied by h). Xa is the file's xco2_apriori, carried by the converter and the forward sampler's reader (retrieval_prior_column in observation files); a record without one uses Xa=∑jhjxa,j, the form of the layer case. The 10-s averages of the weights and of the prior were taken separately, so the two differ: by up to about 0.01 ppm (−0.012 ppm for sounding 20210804153946), a shift of every simulated column. X is still linear in the tracer: the 4D-Var functional is exact as before, with weights Hk/max(mk,ϵ) built from the replayed air mass and pressures of each sampled step.

Why the projection. The basis contains the constants (the hats sum to 1 over the stretched column), so the projection preserves the column integral: the rows of P sum to 1, and with the hat integrals of the model's dry air as weights, hjm=∑kBjk/∑kmk, the projected profile has the model's dry column mean exactly, ∑jhjmcj=∑kmkxk/∑kmk. The file's weights are the same integrals with the retrieval's own humidity, so the column is conserved with them to the difference of the two dry-air corrections. And a projection reproduces every profile its basis can represent: a model profile that is piecewise linear on the levels, the retrieval's own representation, gives its level values back exactly (to the discretization of the model's layer means, second order in the layer thickness), so the kernel is applied to the level values it was derived for. A model profile equal to the retrieval's prior gives the prior column, to the projection's discretization: on 960 OCO-2 records of 2019-07-01, with the prior sampled as the layer means of a fine column aligned with the levels (760 layers), X−Xa is −7e-6 ppm (5–95 %: −9e-6 to −4e-6). On a GEOS L72 column, whose layers do not line up with the levels, |X−Xa|=|∑jhjaj(cj−xa,j)| ranges from 1e-5 to 2.1e-3 ppm and single level values cj differ from xa,j by up to 0.57 ppm (independent review): the level values carry the model's coarse vertical sampling, the column much less. The hat average ∫φjx/∫φj (M lumped onto its diagonal), which is not a projection, puts that column −0.0185 ppm low (−0.021 to −0.012), because the prior is curved in the stratosphere; for a +10 ppm boundary layer below σ=0.95 it is 0.018 ppm below the projection. P, the inverse of the consistent mass matrix times B, has negative entries in general (about −0.21 to −0.23 in every row on a GEOS L72 column), which a linear operator allows; the column weights H stayed positive on OCO-2 records (on L72, minkHk was 0.23–0.39 of their mean).

The top level. The top level sits at σ=10−4 (10 Pa), not at

  1. The stretch maps the whole model column, from its top (about 1 Pa for

GEOS, 0 for ERA5) to its surface, onto [10−4ps,ps], every layer keeping its share, so nothing is cut off: the model stratosphere above 53 hPa falls under the top half-hat (h≈0.026) and the model's air above 10 Pa (10−4 of the column) is squeezed into it. The weights sum to 1 over the levels.

TM5-4DVAR's 19 layers. Observations.tm5_layer_column builds TM5-4DVAR's layer form of the same record (proj/tracer/CO2/py/Observations.py, 7016–7052; Satellite.py, 707–717): the 20 levels as the interfaces of 19 layers, the kernel and prior interpolated to the layer midpoints, the weights Δpi, then the layer operator (rebin R). TM5 divides the thicknesses by ps, so its weights sum to 1−10−4; this mode normalizes them to 1 (TM5's column is 0.9999 times this one). It is for comparisons with TM5-4DVAR co-samples (converter mapping = Observations.LayerRebin() or --mapping tm5, forward sampler averaging_kernel_mapping = "tm5"). For a +10 ppm boundary layer below σ=0.95 on the prior, the mode is 0.009 ppm above the hat projection (5–95 %: +0.002 to +0.017; 960 records of 2019-07-01).

The MIP point formula. The MIP protocol writes the column as X=Xprior+∑jhjaj(cj−cprior,j) with cj the model profile at the levels, without saying how to transform the model to them. Point values are not column-conserving: for a +10 ppm boundary layer below σ=0.95 (ΔX = +0.49 ppm) the point form is 0.232 ppm below the hat projection (5–95 %: 0.230–0.236 ppm; 960 records of 2019-07-01). At c=xa, TM5's unnormalized weights put its column 0.033 ppm below the point form. Neither point values nor boxes centred on the levels are offered.

Tests. test/core/test_retrieval_hat_projection.jl: the projection matrix against an independent construction (exact integration of the hats and their products), rows summing to 1, column conservation with the hat integrals, a piecewise-linear profile reproduced at every level, ends and kinks included, within Δmax|v′|/N2 for N layers per interval (second order), a model profile equal to the prior giving the prior column within 1e-5 ppm (synthetic and real MIP records), TM5 mode against a transcription of TM5's Python, level records in schema v2. test/core/test_cs_4dvar_oco2_mip.jl: the single sweep against a seeded footprint per observation and step for level columns, windows and TM5-mode records (10−12, upwind and monotone PPM), and central finite differences.

OCO-2 MIP records: converter, co-sampling, cell averages ​

Converter. read_oco2_mip_observations(path; ...) reads an OCO-2 or OCO-3 v11 MIP 10-second file (OCO2_b11.2_10sec_GOOD_r2/r3.nc4, OCO3_b11_10sec_GOOD_r2.nc4) into schema-v2 column records with a level block, and convert_oco2_mip_observations(path, output; partition = :day) writes one file per day ({YYYYMMDD}) or month ({YYYYMM}):

bash
julia --project=. scripts/preprocessing/oco2_mip_to_observations.jl \
    OCO2_b11.2_10sec_GOOD_r2.nc4 'obs/oco2_mip_{YYYYMMDD}.nc' \
    --start 2019-01-01 --stop 2019-02-01 --data-types LN,LG,OG --assimilate-flags 1

sounding_id → id; date[1:6] → date_components (truncated to the second, as TM5 reads it); latitude, longitude; kind = "column"; altitude → elevation_m; time_window_s = 0; xco2_2019_scale, xco2_uncertainty and model_error (or a constant, --model-sigma) → value, value_sigma, model_sigma (ppm → mol/mol); the levels (σ⋅ps, hPa → Pa), weights, kernel and prior (ppm → mol/mol), turned surface first, and the prior column xco2_apriori. Every record of the time range is written: flag = 0 when its assimilate_flag is in the experiment's set (--assimilate-flags, 1 by default) and its data type too (--data-types: LN, LG, OG by default; SAM names OCO-3's snapshot area maps, data_type = 0), else flag = 1 + assimilate_flag (1 evaluation-only scenes, 2 assimilable but outside the data types, 3 withheld). instrument_type is <instrument>_<data type> (OCO2_OG); the files record the source file, the options and the mapping as root attributes. OCO-2 and OCO-3 sounding ids can coincide, so keep the instruments in separate sets.

Forward-only co-sampling. The MIP needs co-samples of every record, withheld and evaluation-only ones included. bind_to_mesh(...; flagged_records = "cosample") (CosampleFlaggedRecords; the inversion driver's [observations].flagged_records) binds records with a nonzero flag as co-samples, CSObservation(...; assimilate = false): the forward replay simulates them, CS4DVarResult reports their simulated values, residuals and stencils (assimilated = false), but they add nothing to the cost or the gradient, are not screened by the surface-pressure limit, and do not extend the taped run: the cost is bit for bit that without them, and the reverse pass receives bit-identical forcing and tape length (the gradient is bit for bit that without them single-threaded; with several threads the adjoint kernels are not bit-reproducible from one call to the next, at about 10−22 of the largest entry, with or without co-samples). Departure files carry each row's flag (build_departure_set), so one file holds the co-samples of all records. The default ("drop") is unchanged.

Cell averages. About 59 % of the assimilated OCO-2 10-s records share a C90 cell with another record of the same orbit (1.43 per orbit and cell, about 9 s apart). cell_averaging = CellAverage(; time_tolerance_s = 60, correlation = 0) ([observations.cell_averaging], mode = "average") averages the assimilated column records of one tracer, cell and step sampling that lie within time_tolerance_s of the earliest record of their group. With wi=1/σi2 (value_sigma):

y¯=∑iwiyi∑iwi,H¯=∑iwiHi∑iwi,σmeas2=∑i∑jwiwjρijσiσj(∑iwi)2,σ¯2=σmeas2+∑iwiσmod,i2∑iwi,

ρii=1 and ρij= correlation between distinct spans (0: the within-span correlation is already in σi); the model error (model_sigma and the profile term) is added once, not reduced. H¯ is a CSCellAverageObjective, exact for the linear operators: its gradient is the w-weighted combination of the members'. The members stay as co-samples, so departures keep one row per record, with the average's cell_average_id, and the average is a row of its own, the term of the cost (placement code −4, cell_average_id its number 1, 2, …, a row id above every record id of the observation set, y¯, H¯x, σmeas in value_sigma, the model error in model_sigma, ρ in cell_average_correlation): over the assimilated rows, ∑ normalized_departure² =2Jobs. Averaging needs max_surface_pressure_mismatch_pa = Inf (a CSCellAverageObjective built by hand is checked the same way). The alternative, inflate_cell_model_error(set, mesh, t_start, dt; ...) (mode = "inflate_model_error"), keeps the records and multiplies each record's model_sigma² by the number k of records of its group; the profile-error term of an averaging-kernel column is not inflated.

Forward sampler. An oco2_lite source of [output.observations] with averaging_kernel_tracers = ["co2"] (opt-in) also reads each sounding's retrieval (Observations.read_oco2_retrieval_columns) and writes co2_ak_column, the same operator (averaging_kernel_mapping = "hat" or "tm5") on the sampled profile and the interface pressures of pressure_basis. Without the key the output is unchanged. The test checks that, in nearest-window mode, the sampler's value equals the 4D-Var's simulated value of the same record converted to schema v2 (10−13), and that the default sounding file is identical.

What is not yet shipped ​

ItemNotes
Logarithmic-memory checkpointingOnly FullCheckpoint, StrideCheckpoint(K) and MetWindowCheckpoint() ship; the bisection Revolve was removed. An optimal binomial (Griewank–Walther) schedule will be added when a case needs log-memory snapshots (met streaming).
Optimized/clamped convection adjointsCMFMC clamp = true and TM5/CMFMC-matrix collaborative, truncated, or merged solves are rejected by the footprint API until their exact branches are taped and transposed.
TM5-4DVAR cross-validationSynthetic truth-recovery via test_cs_inversion_truth_recovery.jl is on CI; a side-by-side parity run against TM5-4DVAR on real data has not been published.
Inversion driver on transport binariesscripts/inversions/cs_4dvar.jl binds observation files only on its synthetic meteorology (synthetic_hydrostatic); a 4D-Var on transport-binary meteorology is assembled with the Julia API (bind_to_mesh, CSObservationColumnMet, cs_surface_flux_4dvar).
Tangent-linear modelForward TL is not exposed as a separate driver. If you need it, use the reverse pass plus identity seeding.

Adjoint-readiness in the forward design ​

Three concrete forward-design choices that pay off in the adjoint:

  • Vertical diffusion — generic Kz kernels expose the tridiagonal coefficients (a, b, c) and transpose the backward-Euler solve with its mass/VMR scaling. The precomputed Dkg mass path exposes the retention fractions of two conservative bidiagonal solves. Its reverse applies those passes' transposes in reverse order, preserving a constant total-mass gradient exactly for positive carrier masses.

  • Convection (CMFMC + TM5) — apply! takes a ConvectionForcing carrier explicitly so the operator does not call current_time internally; this keeps the operator pure-functional in the time variable. The TM5 reverse pass rebuilds the same per-column matrix and solves with the transposed LU factors.

  • Advection — the adjoint composes each operator's transpose in reverse order. The palindrome helps organize that reversal, but forward transport is not its own inverse. Stored states supply the limiter branches and reconstructed-face sensitivities needed by the reverse pass.

How to use the adjoint today ​

The maintained command-line path is the inversion driver:

bash
julia --project=. scripts/inversions/cs_4dvar.jl \
    config/inversions/example_synthetic.toml

That synthetic-only configuration owns observations, controls, covariance and optimizer choices, and an [advection] table that is parsed like a forward run's (limiter = "none", the unlimited PPM, is accepted only here). Unknown tables and keys are errors. [observations] takes inline entries or a schema-v2 observation file, whose point records [observations.placement] places in the column of the synthetic_hydrostatic meteorology (the keys of [output.observations] placement, parsed by the same code), and [adjoint.tape] defaults to automatic selection (AutoTape); storage and checkpointing accept "auto", or constrain storage to "device", "pinned_host", "mmap" and checkpointing to "full", "stride". stride and resident_tail accept "auto" or an integer; snapshots is "device" or "pinned_host". budget_gb overrides the measured device tape/snapshot budget and its safety margin. scratch_dir enables mmap with automatic storage and is required for explicit mmap storage. content = "state" | "air_mass" selects the content axis. The resolved pools and snapshots are reserved before recording. The binary-window size uses the per-window step schedule, while CFL and Lin–Rood counts are cached once per runtime. See Tape and checkpointing for candidate order, memory headroom, typed reservation errors and logging. It does not expose diffusion, convection or real-binary settings. For the lower-level Julia API, start with the footprint tutorial, then test/core/test_cs_inversion_truth_recovery.jl for a larger inversion assembly. Pass tape = AdjointTape(; checkpoint = StrideCheckpoint(K)) to fix a K-step re-recording schedule. resident_tail = n retains the final n steps recorded by the forward run; snapshots = PinnedHostCSTapeStorage() moves the head's tracer/air-mass checkpoints to host memory. MetWindowCheckpoint() re-records one met window at a time (split_steps = k bounds a segment to k steps). Lin–Rood tapes currently require DeviceCSTapeStorage(); split-sweep schemes also support PinnedHostCSTapeStorage() and MmapCSTapeStorage(dir).