|
| 1 | +# SimLab Architecture |
| 2 | + |
| 3 | +This document outlines the high-level architecture of the SimLab framework, explaining its components, design principles, and how they interact. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +SimLab is designed around a modular, extensible architecture that allows for: |
| 8 | + |
| 9 | +1. Consistent interface across different simulation types |
| 10 | +2. Dynamic discovery and instantiation of simulation models |
| 11 | +3. Easy extension with new simulation types |
| 12 | +4. Clear separation of concerns between components |
| 13 | + |
| 14 | +The architecture follows object-oriented principles, with a focus on: |
| 15 | + |
| 16 | +- **Inheritance**: All simulators inherit from a common base class |
| 17 | +- **Abstraction**: Complex simulation logic is encapsulated in specific classes |
| 18 | +- **Polymorphism**: Common interface across different simulator types |
| 19 | +- **Encapsulation**: Internal simulation state is managed by each simulator |
| 20 | + |
| 21 | +## Core Components |
| 22 | + |
| 23 | +### Base Simulation |
| 24 | + |
| 25 | +At the heart of SimLab is the `BaseSimulation` abstract class, which defines the common interface for all simulations: |
| 26 | + |
| 27 | +``` |
| 28 | +BaseSimulation |
| 29 | +├── run_simulation() |
| 30 | +├── reset() |
| 31 | +├── get_parameters_info() |
| 32 | +└── _initialize_random_generators() |
| 33 | +``` |
| 34 | + |
| 35 | +This class ensures all simulators provide a consistent interface and handles common functionality like random number generation. |
| 36 | + |
| 37 | +### Registry System |
| 38 | + |
| 39 | +The `SimulatorRegistry` provides dynamic discovery and instantiation of simulation models: |
| 40 | + |
| 41 | +``` |
| 42 | +SimulatorRegistry |
| 43 | +├── register() |
| 44 | +├── unregister() |
| 45 | +├── get() |
| 46 | +├── list_simulators() |
| 47 | +├── create() |
| 48 | +└── load_simulator_from_path() |
| 49 | +``` |
| 50 | + |
| 51 | +The registry allows simulations to be referenced by name, decoupling the code that uses simulations from the specific simulation implementations. |
| 52 | + |
| 53 | +### Simulation Categories |
| 54 | + |
| 55 | +SimLab organizes simulations into several categories, each with its own specialized base class: |
| 56 | + |
| 57 | +``` |
| 58 | +BaseSimulation |
| 59 | +├── BasicSimulation |
| 60 | +│ ├── StockMarketSimulation |
| 61 | +│ ├── ResourceFluctuationsSimulation |
| 62 | +│ └── ProductPopularitySimulation |
| 63 | +├── DiscreteEventSimulation |
| 64 | +│ └── QueueingSimulation |
| 65 | +├── StatisticalSimulation |
| 66 | +│ ├── MonteCarloSimulation |
| 67 | +│ └── MarkovChainSimulation |
| 68 | +├── AgentBasedSimulation |
| 69 | +├── SystemDynamicsSimulation |
| 70 | +├── NetworkSimulation |
| 71 | +├── EcologicalSimulation |
| 72 | +│ └── PredatorPreySimulation |
| 73 | +└── DomainSpecificSimulation |
| 74 | + ├── EpidemiologicalSimulation |
| 75 | + ├── CellularAutomatonSimulation |
| 76 | + └── SupplyChainSimulation |
| 77 | +``` |
| 78 | + |
| 79 | +## Directory Structure |
| 80 | + |
| 81 | +The project follows a clear directory structure: |
| 82 | + |
| 83 | +``` |
| 84 | +sim_lab/ |
| 85 | +├── __init__.py |
| 86 | +├── cli/ |
| 87 | +│ ├── __init__.py |
| 88 | +│ └── main.py |
| 89 | +├── config/ |
| 90 | +│ ├── __init__.py |
| 91 | +│ └── settings.py |
| 92 | +├── core/ |
| 93 | +│ ├── __init__.py |
| 94 | +│ ├── base_simulation.py |
| 95 | +│ ├── registry.py |
| 96 | +│ └── [specific simulation files] |
| 97 | +├── tui/ |
| 98 | +│ ├── __init__.py |
| 99 | +│ └── app.py |
| 100 | +├── utils/ |
| 101 | +│ ├── __init__.py |
| 102 | +│ ├── io.py |
| 103 | +│ └── validation.py |
| 104 | +├── viz/ |
| 105 | +│ ├── __init__.py |
| 106 | +│ └── plots.py |
| 107 | +└── web/ |
| 108 | + ├── __init__.py |
| 109 | + └── app.py |
| 110 | +``` |
| 111 | + |
| 112 | +- **core/**: Contains the simulation models and core functionality |
| 113 | +- **cli/**: Command-line interface |
| 114 | +- **tui/**: Text-based user interface |
| 115 | +- **web/**: Web-based interface |
| 116 | +- **config/**: Configuration settings |
| 117 | +- **utils/**: Utility functions |
| 118 | +- **viz/**: Visualization components |
| 119 | + |
| 120 | +## Interfaces |
| 121 | + |
| 122 | +SimLab provides multiple interfaces for interacting with simulations: |
| 123 | + |
| 124 | +1. **Python API**: Direct use in Python code |
| 125 | +2. **CLI**: Command-line interface |
| 126 | +3. **TUI**: Text-based user interface |
| 127 | +4. **Web**: Web-based interface |
| 128 | + |
| 129 | +Each interface uses the same underlying simulation models, providing different ways to access the functionality. |
| 130 | + |
| 131 | +## Design Patterns |
| 132 | + |
| 133 | +SimLab uses several design patterns: |
| 134 | + |
| 135 | +1. **Factory Pattern**: The registry acts as a factory for creating simulations |
| 136 | +2. **Singleton Pattern**: The registry is a singleton |
| 137 | +3. **Decorator Pattern**: Registration is done through decorators |
| 138 | +4. **Strategy Pattern**: Different simulation algorithms implement the same interface |
| 139 | +5. **Observer Pattern**: Some simulations use observers to track state changes |
| 140 | + |
| 141 | +## Extension Points |
| 142 | + |
| 143 | +SimLab can be extended in several ways: |
| 144 | + |
| 145 | +1. **New Simulation Types**: Create a new class inheriting from `BaseSimulation` |
| 146 | +2. **Custom Visualizations**: Add new visualization methods |
| 147 | +3. **Additional Interfaces**: Create new interfaces to the simulation models |
| 148 | +4. **Plugins**: Develop plugins that can be loaded at runtime |
| 149 | + |
| 150 | +## Data Flow |
| 151 | + |
| 152 | +A typical data flow through the system: |
| 153 | + |
| 154 | +1. User selects a simulation type and parameters |
| 155 | +2. Interface code creates a simulation instance through the registry |
| 156 | +3. Simulation runs and generates results |
| 157 | +4. Results are processed, visualized, or exported |
| 158 | + |
| 159 | +## Dependencies |
| 160 | + |
| 161 | +SimLab has minimal external dependencies: |
| 162 | + |
| 163 | +- **NumPy**: For numerical operations |
| 164 | +- **Matplotlib** (optional): For visualization |
| 165 | +- **Rich** (optional): For TUI |
| 166 | +- **Flask** (optional): For web interface |
| 167 | + |
| 168 | +## Future Architecture Directions |
| 169 | + |
| 170 | +Planned architectural improvements: |
| 171 | + |
| 172 | +1. **Plugin System**: More formal plugin architecture |
| 173 | +2. **Distributed Simulations**: Support for distributed computing |
| 174 | +3. **Cloud Integration**: Deployment to cloud environments |
| 175 | +4. **Real-time Visualization**: Live updating visualizations |
| 176 | +5. **Interoperability**: Better integration with other tools |
0 commit comments