Architecture¶
Overview of Software Architecture¶
RAPID is implemented as a Python package with a modular architecture that separates river network processing, parameter handling, routing computations, and I/O operations. The system follows a pipeline architecture where data flows through distinct processing stages.
High-Level System Architecture¶
RAPID follows a simple 4-stage pipeline:
- Configuration - Parse YAML namelist
- Input Processing - Read files and build network matrices
- Routing Engine - Perform matrix-based Muskingum routing
- Output Generation - Write NetCDF result files
Each stage is detailed below with its internal components and data flow.
Detailed Sub-System Architectures¶
Input Processing Pipeline¶
Routing Engine Pipeline¶
Output Generation Pipeline¶
System Components¶
Core Modules¶
Configuration Management¶
nml_cfg.py
- YAML namelist parser and validator that loads configuration files and validates required parameters
River Network Processing¶
con_vec.py
- Reads river connectivity data from CSV filesbas_vec.py
- Loads basin/sub-basin river ID mappingshsh_tbl.py
- Creates hash tables for efficient river ID lookupsnet_mat.py
- Constructs network connectivity matriceschk_ids.py
- Validates river ID consistency between datasetschk_top.py
- Verifies upstream-downstream topology
Parameter Management¶
k_x_vec.py
- Loads Muskingum k and x parameters for each river reachccc_mat.py
- Computes Muskingum coefficient matrices (C1, C2, C3)
Routing Engine¶
rte_mat.py
- Constructs routing matrices for the linear systemmus_rte.py
- Performs matrix-based Muskingum routing calculationsstp_cor.py
- Handles time step correspondence between input data and routing
I/O Operations¶
Qex_mdt.py
- Reads external inflow metadata and dataQou_new.py
- Creates output discharge filesQfi_new.py
- Creates final state files
Main Application¶
_rapid2.py
- Main entry point that orchestrates the entire routing workflow
Data Flow Architecture¶
The RAPID system follows this processing pipeline:
- Configuration Loading: Parse YAML namelist and validate parameters
- Network Setup: Load river connectivity and create network matrices
- Parameter Loading: Read Muskingum parameters and create coefficient matrices
- System Matrix Assembly: Construct linear system matrices for routing
- Input Processing: Load external inflow data and initial conditions
- Validation: Check ID consistency and network topology
- Output Setup: Create output files with proper metadata
- Routing Loop: Perform time-stepping Muskingum routing
- Results Storage: Save discharge time series and final states
Mathematical Foundation¶
Core Algorithm Overview¶
Mathematical Components¶
RAPID implements a matrix-based version of the Muskingum routing method:
- Network Matrix (N): Represents upstream-downstream river connections
- Coefficient Matrices (C1, C2, C3): Derived from Muskingum k and x parameters
- Linear System: Sparse matrix equation solved at each routing time step
- Time Stepping: Iterative solution through multiple sub-timesteps for stability
Key Equation¶
At each time step, RAPID solves: N × Q_new = C1 × Q_prev + C2 × Q_ext + C3 × Q_prev
Where:
- Q_new = Updated discharge values
- Q_prev = Previous time step discharge
- Q_ext = External lateral inflow
Extension Points¶
Adding New Input Formats¶
- Extend I/O modules to support additional file formats
- Implement new metadata parsers following existing patterns
Custom Routing Methods¶
- Modify
mus_rte.py
or create new routing modules - Adjust matrix construction in
rte_mat.py
for new methods
Enhanced Validation¶
- Add new validation modules following
chk_*.py
patterns - Integrate additional consistency checks into the main workflow
Design Patterns¶
Functional Programming¶
- Each module provides pure functions with clear inputs/outputs
- Minimal side effects and state management
- Extensive use of NumPy arrays and SciPy sparse matrices
Pipeline Architecture¶
- Clear separation of concerns between processing stages
- Data transformation through sequential module calls
- Error handling at module boundaries
Configuration-Driven¶
- All file paths and parameters specified in YAML configuration
- No hardcoded paths or parameters in the codebase
- Flexible input/output file specification
Technology Stack¶
Core Dependencies¶
- NumPy: Array operations and numerical computations
- SciPy: Sparse matrix operations and linear algebra
- NetCDF4: Scientific data file I/O
- PyYAML: Configuration file parsing
Development Tools¶
- Type Hints: Full typing support for maintainability
- Docstrings: Comprehensive function documentation with examples
- Testing: Unit tests using doctest examples
For detailed API documentation and implementation details, please refer to the source code and the individual module docstrings.