SimLab Architecture¶
This document outlines the high-level architecture of the SimLab framework, explaining its components, design principles, and how they interact.
Overview¶
SimLab is designed around a modular, extensible architecture that allows for:
- Consistent interface across different simulation types
- Dynamic discovery and instantiation of simulation models
- Easy extension with new simulation types
- Clear separation of concerns between components
The architecture follows object-oriented principles, with a focus on:
- Inheritance: All simulators inherit from a common base class
- Abstraction: Complex simulation logic is encapsulated in specific classes
- Polymorphism: Common interface across different simulator types
- Encapsulation: Internal simulation state is managed by each simulator
Core Components¶
Base Simulation¶
At the heart of SimLab is the BaseSimulation
abstract class, which defines the common interface for all simulations:
BaseSimulation
├── run_simulation()
├── reset()
├── get_parameters_info()
└── _initialize_random_generators()
This class ensures all simulators provide a consistent interface and handles common functionality like random number generation.
Registry System¶
The SimulatorRegistry
provides dynamic discovery and instantiation of simulation models:
SimulatorRegistry
├── register()
├── unregister()
├── get()
├── list_simulators()
├── create()
└── load_simulator_from_path()
The registry allows simulations to be referenced by name, decoupling the code that uses simulations from the specific simulation implementations.
Simulation Categories¶
SimLab organizes simulations into several categories, each with its own specialized base class:
BaseSimulation
├── BasicSimulation
│ ├── StockMarketSimulation
│ ├── ResourceFluctuationsSimulation
│ └── ProductPopularitySimulation
├── DiscreteEventSimulation
│ └── QueueingSimulation
├── StatisticalSimulation
│ ├── MonteCarloSimulation
│ └── MarkovChainSimulation
├── AgentBasedSimulation
├── SystemDynamicsSimulation
├── NetworkSimulation
├── EcologicalSimulation
│ └── PredatorPreySimulation
└── DomainSpecificSimulation
├── EpidemiologicalSimulation
├── CellularAutomatonSimulation
└── SupplyChainSimulation
Directory Structure¶
The project follows a clear directory structure:
sim_lab/
├── __init__.py
├── cli/
│ ├── __init__.py
│ └── main.py
├── config/
│ ├── __init__.py
│ └── settings.py
├── core/
│ ├── __init__.py
│ ├── base_simulation.py
│ ├── registry.py
│ └── [specific simulation files]
├── tui/
│ ├── __init__.py
│ └── app.py
├── utils/
│ ├── __init__.py
│ ├── io.py
│ └── validation.py
├── viz/
│ ├── __init__.py
│ └── plots.py
└── web/
├── __init__.py
└── app.py
- core/: Contains the simulation models and core functionality
- cli/: Command-line interface
- tui/: Text-based user interface
- web/: Web-based interface
- config/: Configuration settings
- utils/: Utility functions
- viz/: Visualization components
Interfaces¶
SimLab provides multiple interfaces for interacting with simulations:
- Python API: Direct use in Python code
- CLI: Command-line interface
- TUI: Text-based user interface
- Web: Web-based interface
Each interface uses the same underlying simulation models, providing different ways to access the functionality.
Design Patterns¶
SimLab uses several design patterns:
- Factory Pattern: The registry acts as a factory for creating simulations
- Singleton Pattern: The registry is a singleton
- Decorator Pattern: Registration is done through decorators
- Strategy Pattern: Different simulation algorithms implement the same interface
- Observer Pattern: Some simulations use observers to track state changes
Extension Points¶
SimLab can be extended in several ways:
- New Simulation Types: Create a new class inheriting from
BaseSimulation
- Custom Visualizations: Add new visualization methods
- Additional Interfaces: Create new interfaces to the simulation models
- Plugins: Develop plugins that can be loaded at runtime
Data Flow¶
A typical data flow through the system:
- User selects a simulation type and parameters
- Interface code creates a simulation instance through the registry
- Simulation runs and generates results
- Results are processed, visualized, or exported
Dependencies¶
SimLab has minimal external dependencies:
- NumPy: For numerical operations
- Matplotlib (optional): For visualization
- Rich (optional): For TUI
- Flask (optional): For web interface
Future Architecture Directions¶
Planned architectural improvements:
- Plugin System: More formal plugin architecture
- Distributed Simulations: Support for distributed computing
- Cloud Integration: Deployment to cloud environments
- Real-time Visualization: Live updating visualizations
- Interoperability: Better integration with other tools