# Modelling Battery Charging

May 08, 2022 ☕️ 3 min read

Modelling non-linear battery behavior with piecwise linear expressions

Modelling time dependent charging behavior of batteries is not new. The rise of global EV fleets makes it an interesting topic.

In the context of EV charging the most important charging objective is to have the car be charged to a target SOC (state of charge) at a pre-determined point in time, a.k.a. departure time.

This forms an important time constraint for the system.

The mathematical model of the charging and discharging behavior of a battery is straight forward.

# Model

## Indices

$H: \ \text{ Time Horizon }\\ t \in (1..50)\\$

## Parameters

$p_{min} = -25kW\\ p_{max} = 25kW\\ e_{min} = 0kWh\\ e_{max} = 100kWh\\ e_{start} = 10kWh\\ e_{tgt} = 100kWh\\ plugin = 5 \text{ (number of 15min interval) }\\ plugout = 48 \text{ (number of 15min interval) }\\ n_{discharge} = 1 \text{ (number of discharge events allowed)}\\$

## Variables

$e_t \in R^+\\ p_t = p_t^+ - p_t^- \\ z_{t} = \begin{cases} 1, \ \text{ p \lt 0 }\\ 0, \ \text{ p \gt 0 }\\ \end{cases}\\ d_t = \begin{cases} 1, \ \text{ discharging starts at begin of interval }\\ 0, \ \text{ else }\\ \end{cases}$

## Constraints

### Power

$p_t^+, p_t^- \ge 0 \\ p_t^- \le -p_{min}\\ p_t^+ \le p_{max} \\ p_{min} \le p_t \le p_{max}\\$

### Energy

$e_{min} \le e_t \le e_{max}\\ e_{plugin} = E_{plugin}\\ e_t = e_{t-1} + \frac{1}{4} p_{t-1}\\$

All optimization strategies need to ensure, that the car is ready when needed.

$e_t >= e_{tgt} \qquad \forall t \geq plugout\\$

## Optimization Objectives

It is interesting to look at different optimization objectives:

1. Time Optimization: (earliest possible, latest possible charging)
2. Feasibility: Car can be charged within available time
3. Cost: Car will be charge at minimum cost

# Battery Profile

Batteries have non-linear charging behavior. Especially high-power charging can show interesting profiles. Here is a simplified charging and discharging profile depending on the energy in the battery (SOC). The closer the SOC gets to 100% the lower the charging power will be.

This phenomenon is also called degradation.

### Step function as non-linearity

It is a well known modelling technique to approximate non-linear functions with piecewise linear functions.

Pyomo has out-of-the box support for this.

The above’s charging profile (upper boundary) can for example be modelled with this expression:

    # linearize non-linear P(E) battery behavior
model.pw_constraint_charging = Piecewise(
block.A, block.p_plus, block.e,
pw_pts=value(block.e_pw_charging),
pw_constr_type='UB',  # upper bound for power
f_rule=value(block.p_pw_charging),
pw_repn='CC',  # algorithm selection
)

This allows for almost arbitrary non-linear battery profiles and is limited only by the number for generated binary variables and associated solver capability.

# Solution

I solve here a very simple battery model over a charging period of 50x15min time intervals. The lower bound of the blue area delineates the plugin SOC and the upper bound is the target SOC.

The red area on the P(E) diagram defines the allowed power values, depending on battery energy.

Departure time is assumed to be $t=46$ and we want to leave with full battery $e_{tgt}=100$, of course.

## Cost Optimized

This scenario allows discharging the battery and selling the energy to the grid operator while still having the car ready at departure time.

Under the totally unrealistic assumption that we can sell energy for twice as much as we buy it, we can see that the battery discharges twice to the allowed minimum of 30% SOC and charges up again.

However, this scenario becomes more realistic when there are time periods where charging and discharging cost do have a real spread.

# Summary

Using piecewise linear functions as approximation of complex battery charging profiles can be a valid solution for modelling non-trivial charging behavior. Pyomo supports here with built-in capabilities and makes our lives as modellers bearable.

always be building, by sysid.