# mehrantsi/flybrain architecture

> Architecture diagram of the GitHub repository mehrantsi/flybrain, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mehrantsi/flybrain
- Repository: https://github.com/mehrantsi/flybrain

## Overview

FlyBrain Engine is a connectome-based neural simulator with a native Rust/Metal runtime, a MuJoCo embodiment and world, and a browser simulation. The principal workflow advances a connectome pack, converts sensory input into neural events, reads neural motor activity, and applies it to the embodied fly and world. Python provides an independent verification lane and offline pack compilation. The README documents major capabilities; connections below are limited to those it states or that the sampled source index supports.

Main entry points: [`rust/src/world_sim.rs`](https://github.com/mehrantsi/flybrain/blob/master/rust/src/world_sim.rs) (World simulation), [`rust/src/brain_bridge.rs`](https://github.com/mehrantsi/flybrain/blob/master/rust/src/brain_bridge.rs) (Brain bridge), [`rust/src/metal_engine.rs`](https://github.com/mehrantsi/flybrain/blob/master/rust/src/metal_engine.rs) (Rust Metal engine), [`rust/src/pack.rs`](https://github.com/mehrantsi/flybrain/blob/master/rust/src/pack.rs) (Connectome pack)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_neural["Neural runtime"]
  node_pack[("Connectome pack<br/>[pack.rs]")]
  node_parameters["Model parameters<br/>[parameters.rs]"]
  node_metal["Rust Metal engine<br/>[metal_engine.rs]"]
  node_browserengine["Browser engine<br/>[browser_engine.rs]"]
  node_npy["NumPy arrays<br/>[npy.rs]"]
end

subgraph group_embodiment["World embodiment"]
  node_world["World simulation<br/>[world_sim.rs]"]
  node_worldcore["MuJoCo world<br/>[world.rs]"]
  node_bridge["Brain bridge<br/>[brain_bridge.rs]"]
  node_flight["Flight dynamics<br/>[flight.rs]"]
  node_aero["Aerodynamics<br/>[aerodynamics.rs]"]
end

subgraph group_sensing["Sensory and behavior"]
  node_behavior["Behavior modes<br/>[behavior.rs]"]
  node_flightbehavior["Flight behavior<br/>[flight_behavior.rs]"]
  node_foraging["Foraging control<br/>[foraging.rs]"]
  node_odor["Odor guidance<br/>[odor_guidance.rs]"]
  node_olfaction["Olfaction<br/>[olfaction.rs]"]
  node_cnsodor["CNS odor decoder<br/>[cns_olfaction.rs]"]
  node_neuralio["Neural I/O mapping<br/>[neural_io.rs]"]
  node_retina["Binocular retina<br/>[retina.rs]"]
  node_stimulus["Neural stimulus<br/>[stimulus.rs]"]
end

subgraph group_browser["Browser runtime"]
  node_webgpu["WebGPU engine<br/>[neural-engine.js]"]
  node_webapp["Browser application<br/>[app.js]"]
  node_scene["3D scene<br/>[scene.js]"]
  node_worker["Simulation worker"]
end

subgraph group_python["Python verification"]
  node_pythonref["Python reference<br/>[reference.py]"]
  node_mlx["MLX engine<br/>[mlx_engine.py]"]
  node_compiler["Connectome compiler<br/>[pack.py]"]
end

node_user(("Simulation user"))
node_pythonuser(("Verification user"))

node_user -.->|"runs simulation"| node_world
node_world -->|"streams sensory events"| node_bridge
node_bridge -->|"advances neural runtime"| node_metal
node_bridge -->|"resolves neural I/O"| node_neuralio
node_bridge -->|"uses odor decoder"| node_cnsodor
node_bridge -->|"uses olfaction"| node_olfaction
node_metal -->|"loads connectome"| node_pack
node_metal -->|"uses parameters"| node_parameters
node_metal -.->|"accepts stimuli"| node_stimulus
node_pack -->|"reads arrays"| node_npy
node_world -->|"uses world"| node_worldcore
node_world -->|"uses flight dynamics"| node_flight
node_flight -->|"uses aerodynamics"| node_aero
node_world -->|"uses behavior modes"| node_behavior
node_world -->|"uses flight behavior"| node_flightbehavior
node_world -->|"updates foraging"| node_foraging
node_foraging -->|"uses odor guidance"| node_odor
node_odor -->|"uses CNS decoder"| node_cnsodor
node_world -->|"uses retina"| node_retina
node_user -.->|"opens browser simulation"| node_webapp
node_webapp -->|"renders scene"| node_scene
node_webapp -.->|"starts worker"| node_worker
node_worker -->|"runs neural engine"| node_webgpu
node_browserengine -->|"loads connectome"| node_pack
node_browserengine -->|"uses parameters"| node_parameters
node_pythonuser -.->|"runs reference"| node_pythonref
node_pythonuser -.->|"runs verification"| node_mlx
node_compiler -.->|"writes runtime packs"| node_pack
node_pythonref -->|"uses connectome data"| node_compiler
node_mlx -->|"uses connectome data"| node_compiler

click node_world "https://github.com/mehrantsi/flybrain/blob/master/rust/src/world_sim.rs"
click node_worldcore "https://github.com/mehrantsi/flybrain/blob/master/rust/src/world.rs"
click node_bridge "https://github.com/mehrantsi/flybrain/blob/master/rust/src/brain_bridge.rs"
click node_flight "https://github.com/mehrantsi/flybrain/blob/master/rust/src/flight.rs"
click node_aero "https://github.com/mehrantsi/flybrain/blob/master/rust/src/aerodynamics.rs"
click node_behavior "https://github.com/mehrantsi/flybrain/blob/master/rust/src/behavior.rs"
click node_flightbehavior "https://github.com/mehrantsi/flybrain/blob/master/rust/src/flight_behavior.rs"
click node_foraging "https://github.com/mehrantsi/flybrain/blob/master/rust/src/foraging.rs"
click node_odor "https://github.com/mehrantsi/flybrain/blob/master/rust/src/odor_guidance.rs"
click node_olfaction "https://github.com/mehrantsi/flybrain/blob/master/rust/src/olfaction.rs"
click node_cnsodor "https://github.com/mehrantsi/flybrain/blob/master/rust/src/cns_olfaction.rs"
click node_neuralio "https://github.com/mehrantsi/flybrain/blob/master/rust/src/neural_io.rs"
click node_retina "https://github.com/mehrantsi/flybrain/blob/master/rust/src/retina.rs"
click node_stimulus "https://github.com/mehrantsi/flybrain/blob/master/rust/src/stimulus.rs"
click node_pack "https://github.com/mehrantsi/flybrain/blob/master/rust/src/pack.rs"
click node_parameters "https://github.com/mehrantsi/flybrain/blob/master/rust/src/parameters.rs"
click node_metal "https://github.com/mehrantsi/flybrain/blob/master/rust/src/metal_engine.rs"
click node_browserengine "https://github.com/mehrantsi/flybrain/blob/master/rust/src/browser_engine.rs"
click node_npy "https://github.com/mehrantsi/flybrain/blob/master/rust/src/npy.rs"
click node_webgpu "https://github.com/mehrantsi/flybrain/blob/master/web/neural-engine.js"
click node_webapp "https://github.com/mehrantsi/flybrain/blob/master/web/app.js"
click node_scene "https://github.com/mehrantsi/flybrain/blob/master/web/scene.js"
click node_worker "https://github.com/mehrantsi/flybrain/blob/master/web/simulation-worker.js"
click node_pythonref "https://github.com/mehrantsi/flybrain/blob/master/src/flybrain/reference.py"
click node_mlx "https://github.com/mehrantsi/flybrain/blob/master/src/flybrain/mlx_engine.py"
click node_compiler "https://github.com/mehrantsi/flybrain/blob/master/src/flybrain/pack.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pack,node_parameters,node_metal,node_browserengine,node_npy,node_user,node_pythonuser toneBlue
class node_world,node_worldcore,node_bridge,node_flight,node_aero toneAmber
class node_behavior,node_flightbehavior,node_foraging,node_odor,node_olfaction,node_cnsodor,node_neuralio,node_retina,node_stimulus toneMint
class node_webgpu,node_webapp,node_scene,node_worker toneRose
class node_pythonref,node_mlx,node_compiler toneIndigo
```

## Components

### Neural runtime

- [Connectome pack](https://github.com/mehrantsi/flybrain/blob/master/rust/src/pack.rs): `rust/src/pack.rs`
- [Model parameters](https://github.com/mehrantsi/flybrain/blob/master/rust/src/parameters.rs): `rust/src/parameters.rs`
- [Rust Metal engine](https://github.com/mehrantsi/flybrain/blob/master/rust/src/metal_engine.rs): `rust/src/metal_engine.rs`
- [Browser engine](https://github.com/mehrantsi/flybrain/blob/master/rust/src/browser_engine.rs): `rust/src/browser_engine.rs`
- [NumPy arrays](https://github.com/mehrantsi/flybrain/blob/master/rust/src/npy.rs): `rust/src/npy.rs`

### World embodiment

- [World simulation](https://github.com/mehrantsi/flybrain/blob/master/rust/src/world_sim.rs): `rust/src/world_sim.rs`
- [MuJoCo world](https://github.com/mehrantsi/flybrain/blob/master/rust/src/world.rs): `rust/src/world.rs`
- [Brain bridge](https://github.com/mehrantsi/flybrain/blob/master/rust/src/brain_bridge.rs): `rust/src/brain_bridge.rs`
- [Flight dynamics](https://github.com/mehrantsi/flybrain/blob/master/rust/src/flight.rs): `rust/src/flight.rs`
- [Aerodynamics](https://github.com/mehrantsi/flybrain/blob/master/rust/src/aerodynamics.rs): `rust/src/aerodynamics.rs`

### Sensory and behavior

- [Behavior modes](https://github.com/mehrantsi/flybrain/blob/master/rust/src/behavior.rs): `rust/src/behavior.rs`
- [Flight behavior](https://github.com/mehrantsi/flybrain/blob/master/rust/src/flight_behavior.rs): `rust/src/flight_behavior.rs`
- [Foraging control](https://github.com/mehrantsi/flybrain/blob/master/rust/src/foraging.rs): `rust/src/foraging.rs`
- [Odor guidance](https://github.com/mehrantsi/flybrain/blob/master/rust/src/odor_guidance.rs): `rust/src/odor_guidance.rs`
- [Olfaction](https://github.com/mehrantsi/flybrain/blob/master/rust/src/olfaction.rs): `rust/src/olfaction.rs`
- [CNS odor decoder](https://github.com/mehrantsi/flybrain/blob/master/rust/src/cns_olfaction.rs): `rust/src/cns_olfaction.rs`
- [Neural I/O mapping](https://github.com/mehrantsi/flybrain/blob/master/rust/src/neural_io.rs): `rust/src/neural_io.rs`
- [Binocular retina](https://github.com/mehrantsi/flybrain/blob/master/rust/src/retina.rs): `rust/src/retina.rs`
- [Neural stimulus](https://github.com/mehrantsi/flybrain/blob/master/rust/src/stimulus.rs): `rust/src/stimulus.rs`

### Browser runtime

- [WebGPU engine](https://github.com/mehrantsi/flybrain/blob/master/web/neural-engine.js): `web/neural-engine.js`
- [Browser application](https://github.com/mehrantsi/flybrain/blob/master/web/app.js): `web/app.js`
- [3D scene](https://github.com/mehrantsi/flybrain/blob/master/web/scene.js): `web/scene.js`
- [Simulation worker](https://github.com/mehrantsi/flybrain/blob/master/web/simulation-worker.js): `web/simulation-worker.js`

### Python verification

- [Python reference](https://github.com/mehrantsi/flybrain/blob/master/src/flybrain/reference.py): `src/flybrain/reference.py`
- [MLX engine](https://github.com/mehrantsi/flybrain/blob/master/src/flybrain/mlx_engine.py): `src/flybrain/mlx_engine.py`
- [Connectome compiler](https://github.com/mehrantsi/flybrain/blob/master/src/flybrain/pack.py): `src/flybrain/pack.py`

### Other components

- **Simulation user**
- **Verification user**

## Connections

- Simulation user → World simulation: runs simulation
- World simulation → Brain bridge: streams sensory events (evidence: `rust/src/world_sim.rs`)
- Brain bridge → Rust Metal engine: advances neural runtime (evidence: `rust/src/brain_bridge.rs`)
- Brain bridge → Neural I/O mapping: resolves neural I/O (evidence: `rust/src/brain_bridge.rs`)
- Brain bridge → CNS odor decoder: uses odor decoder (evidence: `rust/src/brain_bridge.rs`)
- Brain bridge → Olfaction: uses olfaction (evidence: `rust/src/brain_bridge.rs`)
- Rust Metal engine → Connectome pack: loads connectome (evidence: `rust/src/metal_engine.rs`)
- Rust Metal engine → Model parameters: uses parameters (evidence: `rust/src/metal_engine.rs`)
- Rust Metal engine → Neural stimulus: accepts stimuli (evidence: `rust/src/metal_engine.rs`)
- Connectome pack → NumPy arrays: reads arrays (evidence: `rust/src/pack.rs`)
- World simulation → MuJoCo world: uses world (evidence: `rust/src/world_sim.rs`)
- World simulation → Flight dynamics: uses flight dynamics (evidence: `rust/src/world_sim.rs`)
- Flight dynamics → Aerodynamics: uses aerodynamics (evidence: `rust/src/flight.rs`)
- World simulation → Behavior modes: uses behavior modes (evidence: `rust/src/world_sim.rs`)
- World simulation → Flight behavior: uses flight behavior (evidence: `rust/src/world_sim.rs`)
- World simulation → Foraging control: updates foraging (evidence: `rust/src/world_sim.rs`)
- Foraging control → Odor guidance: uses odor guidance (evidence: `rust/src/foraging.rs`)
- Odor guidance → CNS odor decoder: uses CNS decoder (evidence: `rust/src/odor_guidance.rs`)
- World simulation → Binocular retina: uses retina (evidence: `rust/src/world_sim.rs`)
- Simulation user → Browser application: opens browser simulation
- Browser application → 3D scene: renders scene (evidence: `web/app.js`)
- Browser application → Simulation worker: starts worker
- Simulation worker → WebGPU engine: runs neural engine (evidence: `web/simulation-worker.js`)
- Browser engine → Connectome pack: loads connectome (evidence: `rust/src/browser_engine.rs`)
- Browser engine → Model parameters: uses parameters (evidence: `rust/src/browser_engine.rs`)
- Verification user → Python reference: runs reference
- Verification user → MLX engine: runs verification
- Connectome compiler → Connectome pack: writes runtime packs
- Python reference → Connectome compiler: uses connectome data (evidence: `src/flybrain/reference.py`)
- MLX engine → Connectome compiler: uses connectome data (evidence: `src/flybrain/mlx_engine.py`)
