Skip to content

Add memory-light online streamfunction mode (lbas-indexed)#18

Open
hdrake wants to merge 3 commits into
TRACMASS:mainfrom
hdrake:online-streamfunction-lowmem
Open

Add memory-light online streamfunction mode (lbas-indexed)#18
hdrake wants to merge 3 commits into
TRACMASS:mainfrom
hdrake:online-streamfunction-lowmem

Conversation

@hdrake

@hdrake hdrake commented Jun 23, 2026

Copy link
Copy Markdown

Summary

Adds a memory-light online streamfunction mode that indexes fluxes by killing zone (lbas) instead of by trajectory, sizing the flux arrays 0:maxlbas (~tens) instead of 0:ntracmax (millions). This removes the per-trajectory memory that causes out-of-memory failures when computing streamfunctions online for runs with millions of particles.

⚠️ Stacked on #17#16. The "Files changed" tab includes those ancestors until they merge — this PR's own change is the single commit "Add memory-light online streamfunction mode…".

How it works

lbas is only known once a trajectory terminates, so the new mode runs as a second pass:

  1. Pass 1 — a normal run produces _rerun.csv with the (ntrac, lbas) labels.
  2. Pass 2./runtracmass rerun with l_psi=.TRUE., l_offline=.FALSE., l_psi_rerun=.TRUE. loads the labels (via read_rerun) and accumulates fluxes live, binned by lbas.

Changes

  • New namelist flag l_psi_rerun (INIT_POSTPROCESS), default .FALSE.additive; legacy per-trajectory online accumulation is unchanged.
  • init_stream sizes the flux arrays 0:maxlbas in the new mode (legacy online still uses 0:ntracmax).
  • update_fluxes indexes the flux slot by the trajectory's lbas.
  • compute_stream skips the per-trajectory summation in the new mode; the new path reuses the offline per-zone integration.

Correctness

The new mode and legacy online both write fluxes(:,:,lbas) and integrate per zone, so they should produce identical streamfunctions. Verified byte-identical to legacy per-trajectory online on single- and multi-killzone configurations (and differs from offline only by the pre-existing online-vs-offline one-cell shift).

Depends on

🤖 Generated with Claude Code


Addresses #19.

hdrake and others added 3 commits June 22, 2026 19:04
The streamfunction flux arrays and their per-zone loops were hard-coded to
a fixed 21 kill zones, and the kill-zone (10) and tracer (10) capacities
were scattered as bare literals across many files (including per-project
kill_zones.F90). This generalizes those limits and sizes the streamfunction
allocation to the zones a run actually uses.

- Add named capacity constants in mod_precdef: MAXGEOZONES, MAXTRACERS, and
  MAXZONES = 1 + MAXTRACERS + MAXGEOZONES, used to dimension the kill-zone
  and tracer configuration arrays.
- Compute maxlbas (the actual streamfunction zone count) from the run:
  namelist geographic zones + subdomain wall zones + tracer kill zones,
  finalised in init_subdomain once exitType and all zones are known. The
  flux arrays (offline) and the compute_stream loop now size to maxlbas
  instead of a fixed 21.
- Derive runtime loop bounds and the subdomain "last 4" wall slots from
  SIZE() of the arrays, removing the hard-coded slot numbers (7-10) in
  mod_subdomain and the 1,10 loops in kill_zones.F90 (x5), mod_diffusion,
  and mod_init.
- Initialise ienw/iene/jens/jenn to 0 so undefined slots are well-defined.

Fixes a latent out-of-bounds when the configured zone count differs from
21, and an under-allocation when an exitType=2 run uses a subdomain (which
promotes exitType to 3 and adds geographic walls). Behaviour is otherwise
unchanged: streamfunction output now contains the actual number of zone
blocks, verified byte-identical to the previous output for the real zones.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
`./runtracmass rerun` could not re-read the trajectory labels it needs,
due to two issues in the rerun file path (both independent of
streamfunctions):

- The output-file prefix default (outDataFile = 'TRACMASS' when empty)
  was applied only inside open_outfiles, which runs AFTER read_rerun.
  So read_rerun looked for '_rerun.csv' instead of 'TRACMASS_rerun.csv'
  and reported "No rerun file", leaving every trajectory label unset.
  Apply the default in init_namelist, before it is first used.

- _rerun.csv is written comma-separated but read_rerun parsed it with a
  fixed "(I8,I3,I10)" format, mis-reading lbas. Use list-directed input.

With both fixes a rerun pass correctly re-seeds and re-integrates the
labelled trajectories (verified: exit positions identical to a normal
run).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Online streamfunction accumulation (l_psi=.TRUE., l_offline=.FALSE.)
allocated the flux arrays with a per-trajectory third dimension
(0:ntracmax) -- the source of out-of-memory failures for runs with
millions of particles. This adds an additive online mode that indexes
fluxes by killing zone (lbas) instead of by trajectory, sizing the
arrays 0:maxlbas (tens) so memory no longer scales with particle count.

Because lbas is only known once a trajectory terminates, the new mode
runs as a second pass: pass 1 produces _rerun.csv with the labels; pass
2 (./runtracmass rerun) loads them via read_rerun and accumulates fluxes
live, binned by lbas.

- New namelist flag l_psi_rerun (INIT_POSTPROCESS), default .FALSE.
- init_stream sizes the flux arrays 0:maxlbas in the new mode; legacy
  per-trajectory online still uses 0:ntracmax.
- update_fluxes indexes the flux slot by the trajectory's lbas (skipping
  trajectories whose zone is not yet known).
- compute_stream skips the per-trajectory summation in the new mode; the
  new path shares the offline per-zone integration.

Verified byte-identical to legacy per-trajectory online streamfunctions
on single- and multi-killzone configurations.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant