Skip to content

LennisNgugi/SolarPanelSimulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Solar Panel Energy Simulation (C++)

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.

Features

  • 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_ptr for automatic memory management.

How It Works

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 Intensity

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.

Cloud Factor

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.


Example Output

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.

Project Structure

main.cpp
│
├── SolarPanel class
│   ├── sunlightIntensity()
│   ├── cloudFactor()
│   └── output()
│
└── main()
    ├── Creates solar panels
    ├── Simulates 24 hours
    ├── Prints hourly production
    └── Prints total daily energy

C++ Concepts Demonstrated

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)

Building

Compile using a C++17 compatible compiler.

GCC / MinGW

g++ -std=c++17 main.cpp -o SolarSimulation

Run

Linux/macOS:

./SolarSimulation

Windows:

SolarSimulation.exe

Customisation

You can easily modify the simulation:

Change the number of panels

panels.push_back(make_unique<SolarPanel>(0.4));

Add more panels by inserting additional lines.

Change panel size

SolarPanel(0.4)

Examples:

Rating Description
0.30 300 W panel
0.40 400 W panel
0.55 550 W panel

Change weather conditions

Modify:

uniform_real_distribution<>(0.5, 1.0);

Example:

uniform_real_distribution<>(0.8, 1.0);

This simulates consistently sunny weather.

Possible Improvements

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.

Learning Objectives

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

License

This project is released under the MIT License. Feel free to use, modify and learn from it.

About

C++ simulation that models the hourly energy production of a small solar panel system over a 24-hour period.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages