---
title: "Capstone: balance a microgrid for a day"
heading: A solar and battery microgrid serving a steady load, the day's energy balance, how much the battery must carry through the night, simulating the battery hour by hour, and finding out whether a given battery can keep the lights on day after day
eyebrow: D1 · Lesson 9 · Capstone
xp: 80
---

::: hook
An island, a remote village or a field hospital runs on its own microgrid: solar panels, a battery and a diesel generator as backup. The question its designer must answer is simple to state: will this system keep the lights on through the night, every night, without burning diesel? It depends on how much energy the panels make, how much the load uses, how much the battery can hold, and the losses each time energy goes in and out. This capstone pulls together the energy thinking of lessons 7 and 8 and checks it with an hour-by-hour simulation.
:::

::: play
- A microgrid's solar output (a half-sine from 6:00 to 18:00 peaking at $P_\text{pk}$) and a steady load L.
- Where solar exceeds the load, the surplus charges the battery; where it falls short, the battery covers the gap.
- The readouts give the day's energies: whether the panels make enough in total, and how much the battery must carry from evening to morning.
:::

```sim plot
view: graph
x: t = 0..24
y: P = -30..70
slider: k = 20..120 step 5 = 60
slider: L = 5..40 step 1 = 15
plot: k*sin(pi*(t - 6)/12)*step(t - 6)*step(18 - t) | samples: 300 | color: ch4 | label: solar, kW
plot: L + 0*t | dashed | color: ch3 | label: load, kW
plot: k*sin(pi*(t - 6)/12)*step(t - 6)*step(18 - t) - L | samples: 300 | label: surplus (positive) or deficit, kW
readout: daily solar energy, kWh = k*24/pi | digits: 4
readout: daily load energy, kWh = 24*L | digits: 4
```

::: predict
## A 15 kW load runs all day from solar peaking at 60 kW and a 200 kWh battery with 95% efficiency each way. The panels make about 460 kWh a day against 360 kWh of load. Can the 200 kWh battery keep the lights on every night?

- [ ] Yes: the panels make more than enough energy
- [x] No: the battery is too small to carry the evening and night
- [ ] Yes, with energy to spare every night
- [ ] No: the panels don't make enough energy

::: reveal
The energy balance is fine, but timing isn't. Solar falls below 15 kW at about 17:00 and doesn't exceed it again until about 7:00, so the battery must supply about 14 hours × 15 kW = 210 kWh, or 221 kWh at its terminals after discharge losses, more than its 200 kWh. The simulation below shows it running dry just before dawn, about 2 kWh short.
:::
:::

::: nameit
## Energy balance

Over a day, solar energy must at least cover the load plus the losses on the energy that passes through the battery. For a half-sine solar profile peaking at $P_\text{pk}$, daily energy is $P_\text{pk} \times 24/\pi$ kWh (12 hours with an average of $2/\pi$ of the peak). A load L draws 24L kWh.

## Battery size

The battery must carry the load through the hours when solar is below it. If those hours add up to T and the discharge efficiency is $\eta_d$, the battery needs at least $LT/\eta_d$ of usable energy, plus margin for cloudy days. A battery that fills by midday wastes (curtails) the rest of the surplus.

## Sustainable cycling

A daily cycle repeats only if the battery ends the day with at least the charge it started with. The simulation below checks that, together with any unmet energy (which the diesel must supply) and curtailed surplus.
:::

::: example
## How long must the battery carry the load in the predict question, and what usable capacity does that need?

::: solution
1. **When solar meets the load.** Solar equals the 15 kW load when
   $$60\sin(\pi(t - 6)/12) = 15$$
   that is, when the sine is 0.25:
   $$\begin{aligned} t &= 6 + (12/\pi) \times 0.253 \\ &= 6.97\ \text{h} \end{aligned}$$
   and again at t = 17.03 h.
2. **Hours on the battery.** From 17:00 to 7:00 is about 14 h.
3. **Usable capacity.**
   $$\begin{aligned} E &= 14 \times 15/0.95 \\ &= 221\ \text{kWh} \end{aligned}$$
4. **Check in the sim.** A 300 kWh battery starting each midnight at 110 kWh covers the whole day and ends at 185 kWh. The 200 kWh battery fills by midday and ends every day at 97.7 kWh, which isn't enough for the next night.

**Answer:** the battery carries the load for about 14 hours, which needs about 221 kWh of usable capacity.
:::
:::

::: example
## The microgrid's 60 kW peak of half-sine solar against a steady 15 kW load: what are the daily solar energy and the daily load?

::: solution
1. **Daily solar energy.**
   $$60 \times 24/\pi = 458\ \text{kWh}$$
2. **Daily load.**
   $$15 \times 24 = 360\ \text{kWh}$$
3. **Surplus.** There's a 98 kWh surplus on a clear day, before battery losses take their share of the energy that passes through storage.

**Answer:** the panels make 458 kWh a day, and the load uses 360 kWh.
:::
:::

::: example
## On a cloudy day the panels make 30% of their clear-day output. How much must the diesel generator supply, and how much fuel does it burn at 0.3 L/kWh?

::: solution
1. **Cloudy-day solar.**
   $$0.3 \times 458.4 = 137.5\ \text{kWh}$$
2. **Diesel energy.** Against 360 kWh of load:
   $$360 - 137.5 = 222.5\ \text{kWh}$$
   more if the battery starts the day low.
3. **Fuel.**
   $$222.5 \times 0.3 = 67\ \text{L}$$
4. **Sizing.** A few cloudy days a month set the fuel budget, which is why microgrids size the battery and the diesel together.

**Answer:** the diesel must supply 222.5 kWh and burns about 67 L.
:::
:::

::: check
kind: python

## Write `simulate_day`: run the microgrid's battery hour by hour.

```python starter
import numpy as np

def simulate_day(load, solar, cap, soc0, eta_c=0.95, eta_d=0.95):
    # Hourly microgrid: load and solar are 24 average powers in kW (energy per hour in kWh).
    # Each hour: surplus = solar - load. A surplus charges the battery (storing surplus*eta_c,
    # up to cap); whatever doesn't fit is curtailed. A deficit draws deficit/eta_d from the
    # battery, down to empty; whatever can't be covered is unmet (the diesel's job).
    # Return (soc_end, unmet_kWh, curtailed_kWh).
    return soc0, 0.0, 0.0

t = np.arange(24) + 0.5
solar = np.where((t > 6) & (t < 18), 60*np.sin(np.pi*(t - 6)/12), 0.0)
load = np.full(24, 15.0)
print(simulate_day(load, solar, 200.0, 100.0))
```

```python solution
import numpy as np

def simulate_day(load, solar, cap, soc0, eta_c=0.95, eta_d=0.95):
    soc, unmet, curtailed = float(soc0), 0.0, 0.0
    for L, S in zip(load, solar):
        x = S - L
        if x >= 0:
            store = min(x*eta_c, cap - soc)
            soc += store
            curtailed += x - store/eta_c
        else:
            need = -x/eta_d
            take = min(need, soc)
            soc -= take
            unmet += (need - take)*eta_d
    return soc, unmet, curtailed

t = np.arange(24) + 0.5
solar = np.where((t > 6) & (t < 18), 60*np.sin(np.pi*(t - 6)/12), 0.0)
load = np.full(24, 15.0)
print(simulate_day(load, solar, 200.0, 100.0))
```

```python tests
t = np.arange(24) + 0.5
solar = np.where((t > 6) & (t < 18), 60*np.sin(np.pi*(t - 6)/12), 0.0)
load = np.full(24, 15.0)
soc, unmet, cur = simulate_day(load, solar, 0.0, 0.0)
assert abs(unmet - np.sum(np.maximum(load - solar, 0))) < 1e-9 and abs(cur - np.sum(np.maximum(solar - load, 0))) < 1e-9, "With no battery, unmet energy is every deficit and curtailment every surplus."
soc, unmet, cur = simulate_day(load, solar, 1e6, 1e5, 1.0, 1.0)
assert unmet == 0 and cur == 0 and abs(soc - (1e5 + np.sum(solar - load))) < 1e-6, "A huge lossless battery should simply absorb the net energy."
soc, unmet, cur = simulate_day(load, solar, 200.0, 100.0)
ins = np.sum(np.maximum(solar - load, 0)) - cur
outs = np.sum(np.maximum(load - solar, 0)) - unmet
assert abs((soc - 100.0) - (0.95*ins - outs/0.95)) < 1e-9, "Energy must balance: the change in charge is what went in times eta_c minus what came out over eta_d."
assert abs(soc - 97.7174) < 1e-3 and abs(unmet - 2.1684) < 1e-3 and abs(cur - 83.4884) < 1e-3, f"A 200 kWh battery starting at 100 kWh should end at 97.72 kWh with 2.17 kWh unmet and 83.49 kWh curtailed; got {soc:.2f}, {unmet:.2f}, {cur:.2f}."
soc, unmet, cur = simulate_day(load, solar, 300.0, 110.0)
assert unmet == 0 and cur == 0 and abs(soc - 184.7489) < 1e-3, f"A 300 kWh battery starting at 110 kWh should cover everything and end at 184.75 kWh; got {soc:.2f}, {unmet:.2f}, {cur:.2f}."
```

::: right
Right. Your simulation conserves energy, and it shows the 200 kWh battery running 2.2 kWh short before dawn while a 300 kWh one carries the whole day.
:::
:::

::: check
given: k = 30 | 60 | 100
answer: k*24/pi
unit: kWh
tolerance: 0.5%

## Solar output follows a half-sine from 6:00 to 18:00 peaking at {k} kW. How much energy does it make in a day?

::: right
Right. $P_\text{pk} \times 24/\pi$ = {answer} kWh.
:::

::: misses
- [k*12] The average over the 12 hours is $2/\pi$ of the peak.
- [any] Not yet. Integrate the half-sine.
:::
:::

::: check
given: L = 10 | 15 | 25; T = 12 | 14 | 16; e = 0.9 | 0.95
answer: L*T/e
unit: kWh
tolerance: 0.5%

## A {L} kW load must run from the battery for {T} hours with discharge efficiency {e}. What usable capacity does the battery need?

::: right
Right. $LT/\eta_d$ = {answer} kWh.
:::

::: misses
- [L*T] Discharge losses mean the battery must hold more than the load uses.
- [any] Not yet. Energy delivered over efficiency.
:::
:::

::: check
given: L = 10 | 20 | 30; c = 0.85 | 0.9
answer: L*24/c*pi/24
unit: kW
tolerance: 0.5%

## Ignoring timing, what solar peak (half-sine, 6:00 to 18:00) makes enough energy for a {L} kW constant load if all of the load's energy passes through a battery of round-trip efficiency {c}?

::: right
Right. $P_\text{pk} = \pi \times 24L/(24\eta)$ = {answer} kW, an upper bound since daytime load can be served directly.
:::

::: misses
- [any] Not yet. Set the daily solar energy equal to the load's energy over η.
:::
:::

::: check
kind: parts
given: P = 40 | 60 | 100; L = 10 | 15 | 25

## A microgrid's solar follows a half-sine peaking at {P} kW over 12 hours; the load is a steady {L} kW.

::: part
answer: P*24/pi
unit: kWh
tolerance: 0.5%

### What is the daily solar energy?

::: misses
- [P*12] The average of a half-sine is $2/\pi$ of its peak.
- [any] $P_\text{pk} \times 24/\pi$.
:::
:::

::: part
answer: P*24/pi - 24*L
unit: kWh
tolerance: 0.5%

### What surplus or shortfall (negative) is there over the day, before battery losses?

::: misses
- [any] Solar less 24L.
:::
:::
:::

::: check
kind: parts
given: L = 10 | 15 | 25; T = 12 | 14 | 16; e = 90 | 95

## The microgrid's {L} kW load must be carried by the battery for {T} hours each night, with {e}% discharge efficiency.

::: part
answer: L*T/(e/100)
unit: kWh
tolerance: 0.5%

### What usable capacity does that need?

::: misses
- [L*T] Discharge losses mean the battery gives up more than the load uses.
- [any] $LT/\eta_d$.
:::
:::

::: part
answer: L*T/(e/100)/0.8
unit: kWh
tolerance: 0.5%

### What nameplate capacity, if only 80% may be used?

::: misses
- [any] Usable ÷ 0.8.
:::
:::
:::

::: explain
## Why can a microgrid run short at night even when its panels make more energy than the load uses each day?

::: model
The daily energy balance only says the panels produce enough in total; it says nothing about when. Solar arrives in the middle of the day, while the load continues through the evening and night, so the energy for those hours must be stored, and a battery can only shift as much as it can hold. If the battery fills by midday, the rest of the surplus is curtailed and wasted, and if its capacity is smaller than the night's energy need plus losses, it runs dry before sunrise no matter how much surplus there was. Sizing a microgrid means matching the battery to the longest stretch the load must be carried, then checking that the cycle repeats.
:::
:::

::: practice

::: check
given: k = 5 | 10 | 50 | 100 | 250 | 500
answer: k*24/pi
unit: kWh
tolerance: 0.5%

## Daily solar energy, peak {k} kW?

::: misses
- [any] $24P_\text{pk}/\pi$.
:::
:::

::: check
given: L = 1 | 100; T = 8 | 18; e = 0.8 | 0.98
answer: L*T/e
unit: kWh
tolerance: 0.5%

## Battery for {L} kW over {T} h, η = {e}?

::: misses
- [any] $LT/\eta_d$.
:::
:::
:::
