Skip to content

Replace SimpleDiGraph flowgraph with compact FlowGraph (~320 MB vs ~5 GB for 80M cells)#12

Open
Oddan wants to merge 4 commits into
SUrbAreafrom
feature/compact-flowgraph
Open

Replace SimpleDiGraph flowgraph with compact FlowGraph (~320 MB vs ~5 GB for 80M cells)#12
Oddan wants to merge 4 commits into
SUrbAreafrom
feature/compact-flowgraph

Conversation

@Oddan

@Oddan Oddan commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Introduces a custom FlowGraph struct (src/FlowGraph.jl) that stores forward adjacency as Vector{Int32} instead of Graphs.SimpleDiGraph
  • Memory reduction: ~320 MB for 80M cells vs ~5 GB for SimpleDiGraph
  • Precomputed topological order eliminates redundant topological_sort_by_dfs calls in watercourses
  • Lazy backward adjacency for upstream_dfs (UI-only path, built on first use)
  • Design rationale and full details in docs/flowgraph-redesign.md

Changed files

  • New: src/FlowGraph.jl — struct + downstream_cells, has_downstream, topological_order, upstream_dfs
  • src/TrapStructure.jl — field type flowgraph::FlowGraph
  • src/spillregions.jl — construct FlowGraph instead of SimpleDiGraph
  • src/watercourses.jltopological_order(g), downstream_cells(g, cell), updated _update_runoff! signature
  • src/fill_sequence/flow.jlhas_downstream / downstream_cells replace outneighbors
  • src/utils.jl — update _downstream_cell, upstream_area, reconstruct_spillfield, current_upstream_area
  • New: docs/flowgraph-redesign.md — design document

Test plan

  • Run existing test suite: julia --project test/runtests.jl
  • Test with a real terrain to verify flow accumulation matches previous results
  • Test flow_path_from and river tracing in the web interface
  • Test current_upstream_area (exercises the lazy backward adjacency)
  • Profile memory usage on a large terrain to confirm reduction

🤖 Generated with Claude Code

Oddan and others added 4 commits June 6, 2026 22:22
Introduces a custom FlowGraph (src/FlowGraph.jl) that stores forward adjacency
as Vector{Int32} (~320 MB for 80M cells) instead of Graphs.SimpleDiGraph
(~5 GB), with a precomputed topological order and lazy backward adjacency.

- New: src/FlowGraph.jl — struct + downstream_cells, has_downstream,
  topological_order, upstream_dfs API
- TrapStructure.flowgraph field changed from Graphs.SimpleDiGraph to FlowGraph
- spillregions.jl: build FlowGraph instead of SimpleDiGraph
- watercourses.jl: use topological_order(g) and downstream_cells(g, cell);
  update _update_runoff! signature to accept FlowGraph
- fill_sequence/flow.jl: replace outneighbors calls with has_downstream /
  downstream_cells
- utils.jl: update _downstream_cell, upstream_area, reconstruct_spillfield,
  and current_upstream_area call sites
- docs/flowgraph-redesign.md: design document with rationale and full details

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
topological_order returns Vector{Int32}, so cur_node is Int32 when
downstream_cells/has_downstream are called from watercourses. Widen
cell argument from ::Int to ::Integer in all three public functions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Test functions in basicTestFuns.jl were calling spillregions(field) without
destructuring, receiving the full (regions, flowgraph, bottomcells) tuple
instead of just the regions matrix. Add trailing comma to extract only the
regions matrix, matching the pattern already used in test_spillregions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Documents complications, correctness risks, and expected runtime impact
of changing regions::Matrix{Int64} and footprints::Vector{Vector{Int64}}
to Int32, for ~640 MB memory saving on 80M-cell terrains.

Co-Authored-By: Claude Sonnet 4.6 <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