A C++ simulation that models the hourly energy production of a small solar panel system over a 24-hour period.
The program demonstrates object-oriented programming concepts, smart pointers, random number generation and mathematical modelling using the C++ Standard Library.
- Simulates a full 24-hour day.
- Models daylight using a smooth sine wave.
- Simulates changing weather with random cloud cover.
- Supports multiple solar panels.
- Calculates hourly energy production.
- Displays total daily energy generated.
- Uses
std::unique_ptrfor automatic memory management.
Each SolarPanel object has a maximum power rating measured in kilowatts (kW).
For every hour of the day the program calculates:
Energy Output = Panel Power × Sunlight Intensity × Cloud Factor
Sunlight follows a sine wave:
- 00:00 – 05:59
- No sunlight
- 06:00 – 18:00
- Output increases until midday
- Peaks around 12:00
- Gradually decreases toward sunset
- 19:00 – 23:59
- No sunlight
This creates a realistic daylight curve.
Weather is simulated using a random value between:
- 1.0 = clear sky
- 0.5 = heavy cloud
Each panel receives its own random cloud value every hour and creating natural variation in production.
Hour | Energy Output (kWh)
--------------------------
0 | 0.000
1 | 0.000
...
9 | 0.421
10 | 0.592
11 | 0.731
12 | 0.782
...
23 | 0.000
The Total Energy: 6.487 kWh
Since cloud cover is random and every output produces slightly different results.
main.cpp
│
├── SolarPanel class
│ ├── sunlightIntensity()
│ ├── cloudFactor()
│ └── output()
│
└── main()
├── Creates solar panels
├── Simulates 24 hours
├── Prints hourly production
└── Prints total daily energy
This project demonstrates:
- Classes and objects
- Encapsulation
- Private helper functions
- Constructors
- Smart pointers (
std::unique_ptr) - Dynamic memory management
std::vector- Random number generation (
std::mt19937) - Mathematical functions (
std::sin) - Loops and iteration
- Formatted console output (
iomanip)
Compile using a C++17 compatible compiler.
g++ -std=c++17 main.cpp -o SolarSimulationLinux/macOS:
./SolarSimulationWindows:
SolarSimulation.exeYou can easily modify the simulation:
panels.push_back(make_unique<SolarPanel>(0.4));Add more panels by inserting additional lines.
SolarPanel(0.4)Examples:
| Rating | Description |
|---|---|
| 0.30 | 300 W panel |
| 0.40 | 400 W panel |
| 0.55 | 550 W panel |
Modify:
uniform_real_distribution<>(0.5, 1.0);Example:
uniform_real_distribution<>(0.8, 1.0);This simulates consistently sunny weather.
Future enhancements could include:
- Seasonal sunlight variation.
- Real sunrise and sunset times.
- Battery storage simulation.
- Solar inverter efficiency.
- Multiple weather profiles.
- CSV file output export.
- Monthly or yearly simulations.
- Real weather API integration (open-meteo).
- CSV file import instead of simulating it with sine wave (MIDAS Open (Met Office) or PVGIS).
- Graphical charts of power generation.
This project is intended as a beginner-to-intermediate C++ exercise covering:
- Object-Oriented Programming (OOP)
- Smart pointers
- Random number generation
- Mathematical modelling
- Clean code organisation
- Basic simulation techniques
This project is released under the MIT License. Feel free to use, modify and learn from it.