Summary
In this article, I will be explaining in full detail (at a level understandable for everyone) my algorithm for electric vehicle charging stations, which determines a load management allocation which is safe, and distributes current optimally in proportion to the power of devices. In a seperate article, I'll also provide an alternative algorithm which allocates amperage in a manner which is safe and instead distributes current to devices such that each charger delivers as equal an amount of power as possible. Note this solves a single iteration of dynamic load management. If you don't know what any of that means, all will be explained.
Context: The Charging Problem
In the rapidly growing electric vehicle industry, the importance of accessible and reliable charging infrastructure is vital to a complete transition from combustion vehicles. Charging, whether it be infrastructure, batteries, the capacities of chargers, is often cited as the biggest problem holding back electric vehicles from reaching the
'tipping point' in regards to mass adoption. A great resource that adequately describes the current problem surrounding electric vehicle charging infrastructure can be found
here.
My current place of work, EVUp, aims at providing solutions to these problems, in particular via
EVUp Charge. EVUp Charge is a software I've contributed to since Feb 2022 and led since Oct 2022, that provides deep functionality for
OCPP electric vehicle chargers, while also connecting drivers to their nearest charger. For those who own chargers, the software enables monetisation, authorisation and various monitoring features for each charger. It also offers
dynamic load balancing (I'll elaborate on the details of what that is soon) for charging stations, which is what brought me to this problem.
Preliminaries
To elaborate further on load balancing, and why it is so important, there are a few concepts I have to explain first. Unless specified otherwise, I aim to make the majority of the articles on this website accessible to everyone, in terms of prerequisite knowledge - this is one of them! This preliminary concepts section should quickly explain from a beginner level some core background ideas I myself actually had to learn to understand dynamic load balancing.
Electricity and Circuits
Firstly, some entry-level details about electricity and circuits I had to teach myself (and hopefully, I can teach you) to grasp the problem are necessary. What I will explain here are the short and simple concepts - hopefully I've made the level of detail accessible for those unfamiliar, as I had to do so for myself.
I (and many) often find it easier to think of a circuit as water flowing through pipework, instead of electrons flowing through wires, known usefully as the
Hydraulic Analogy. I'm going to describe these concepts solely using the analogy, as it will be sufficient for what we're doing.
Firstly,
amperage , also known as current, is the unit which describes the rate at which electrical charge (lots of electrons) flow through a circuit. We can think of this as the volume of water flowing though some pipework. Meanwhile,
voltage we can think of as the difference in water pressure between two points of the pipework. What this actually corresponds to is the difference in electrical potential (energy required to move electrons) between two points. I found the illustration below to be particularly helpful.
Finally,
wattage, or power, is simply the result of multiplying amperage and voltage. In the context of the Hydraulic Analogy, I like to think of it as the volume of water multiplied by the rate of flow, therefore being useful as a measure of the amount of water being delivered through the pipework over time. Bringing it back to electricity, wattage generally is used to describe the rate at which something is receiving electricity, particularly in the context of charging a car.
There are two more key terms to introduce regarding circuits which are series and parallel. A circuit connected in series can be thought of as pipework consisting of a single pipe, while circuits running in parallel can be thought of as a pipe which splits off into different branches at any point.
One thing worth noting which continues our analogy, is that for any circuit which runs in parallel, splitting the pipework into multiple different branches would mean the volume of water in each branch will be a portion of what it was before the pipework branched off - it has to be right? But the pressure in those branches would stay the same - we are still pumping through water at the same rate. Similarly, in a parallel circuit, when the circuit splits into multiple directions, the amperage too splits across those branches - while the voltage stays the same. This is known as Kirchhoff's Law. It might sound confusing, but it's quite intuitive - think of a raging river which splits into different streams, the volume in each stream is less, but they're all still moving as fast.
Power Delivery
With this, there are also two forms that power can be delivered in, AC (alternating current) or DC (direct current). AC power is equivalent to water quickly oscillating back and forth in a pipe, while DC is equivalent to water being constantly pushed through a pipe (more intuitively). When I first heard this I thought, if AC power is going back and forth, how are devices powered by AC not constantly switching on and off then? Well AC power is significantly faster than DC (so much so that it is used in all industrial grids), and this change in direction is often so fast it does not matter. See
this resource here for a great short explanation.
One final but important concept worth noting is that circuits which use AC power may use
Single Phase or Three Phase power. Single phase electricity is the delivery of simple AC power as we know it so far, so it alternates direction and as a result, voltage follows a single sine wave as the "high pressure" points are always alternating (hence the term alternating current or AC power, and the analogy of water oscillating in a pipe). The problem with this is that half the time, the current is travelling away from the direction you want it to go, which is inefficient. All industrial electrical grids (that deliver power to your home) account for this by using standard 3 phase power, where instead of one alternating current, it cleverly delivers 3 alternating currents simultaneously, but they are evenly spaced across time to deliver 3 times the power. See below:
As a result, many buildings will have electricity delivered in across 3
phases, each phase being known as L1, L2 and L3. One confusing question I first asked myself when I heard this, if a device draws power from all 3 phases and overall needs 100 watts, does it draw 100 watts from each or 33.3 watts from each? As it happens, a
3 phase device looking to draw 100 watts will draw a maximum of 100 watts from each L1, L2 and L3. This is because of how the phases are evenly spaced out to have the least overlap possible (as seen in the graph above), never delivering more than 100 watts to the device if 100 watts is delivered at the peak of each phase.
Electric Vehicle Chargers
As for electric vehicle chargers themselves, all you really need to know is that they are an outlet, often with many ports to charge from (exactly like an plug in your wall really). Some chargers use AC power, some use DC power. Since electrical power supplied from the grid is delivered in AC (as mentioned), DC power chargers will convert the AC power they receive into DC power inside them. I don't claim to know how - fortunately that won't be relevant. The charging devices themselves will either draw power from 3 phases or just one.
For those wondering, we can actually set the amperage drawn by each connector from the grid. We do this using
OCPP, the standard which many EV smart charger communications abide by. However, a disclaimer that whatever amperage we set with OCPP will result the charger in drawing that amount from
all connected phases.
For those unfamiliar with all these concepts, it can be a lot to take in, particularly coming from someone who didn't properly know any of this before confronting the problem. It might be worth a reread or checking out the associated links if anything is still unclear, but any further nitpicking details regarding electricity shouldn't be necessary to know, just the general overview provided.
What is Load Balancing/Management?
One of the key problems faced in the process of expanding EV charging infrastructure, is the power strain on
the electical grid and your the local property in doing so. With our constantly growing dependence on electricity, the stability of the grid is important for just about everything you can think of. As a result, power companies will often only allocate some specified amount of amperage to each property. Since electric vehicle chargers draw extensive amounts of power, most properties with multiple chargers installed are likely to draw more amperage from the grid than their local power company has allocated them. As a result, electric vehicle chargers frequently cause power outages (when the load drawn by the property exceeds what it is allocated, tripping the local
circuit breaker - which I'm sure we've all experienced before) which can be obviously problematic for a variety of reasons.
This is particularly relevant for properties which intend on having chargers installed, but don't have their electrical infrastructure primed to be a charging station, which is the case for many places currently looking at installing multiple chargers - city councils, apartments, parking lots etc. Dynamic Load Balancing (DLB), also known as Dynamic Load Management (DLM), is the localised solution to this problem. It connects a group of chargers to a software which ensures they won't exceed the allocated amperage of the property, as to be safe. The "dynamic" part of DLB refers to whenever a charger starts or stops being used, or the allocated amperage changes, the software will recalculate what amount of amperage each charger is allocated, or rather, can safely draw from the home circuit. Fortunately this part is easy, as that just ultimately involves sending the updates to a computer, and the computer sending them back to the chargers. The "load management" part refers to how we actually calculate this, and is more involved - figuring out how this should work will be the main focus of this article.
One thing I should mention is that this article will focus on finding a load management algorithm which distributes power in a manner which is safe and uses as much amperage as possible. In the next article, this will be taken a step further and we will aim to find an algorithm which, on top of those two things, ensures the load management system finds the most evenly distributed amount of wattage possible, as to guarantee each charger provides the most closely equal amount of power possible to those charging - granting fairness. But for now forget about that, we just want something that is safe and makes the most of the limited amperage the power company has given us.
A lot of people (including myself at first glance, being formerly unfamiliar with the workings of circuits) initially assume the solution could be as straight forward as taking the amperage of the circuit breaker, dividing it by the number of active chargers, setting each charger to draw that amount, and moving on. Basic division, even yielding an evenly distributed allocation. However 2 major limitations are faced that convolute the problem. Believe it or not, considering these limitations makes the solution much more complicated than mere division.
Problem 1
The first limitation is the mixing of three phase and single phase devices (assuming a location uses 3 phase power). When allocating amperage to devices, we have to consider whether the allocation exceeds capacity of
any of the 3 phases, rather than a single source. This is only a problem when putting single phase chargers on 3 phase property circuits, because if you look at the
3 phase graph again, one of these peaks will be imbalanced with the others when we have an additional device connected to it. If we have just 3 phase devices or just single phase devices, this wouldn't be an issue because we wouldnt have to consider each phase seperately - we could effectively ignore two of them.
Problem 2
Another limitation emerges when considering the circuits of the devices and ports themselves. Often, each charger will have a couple of ports, which get allocated some share of the amperage given to the charger. The ports themselves have a maximum amperage they can safely use to charge a car, and on top of that, the devices themselves usually have a maximum circuit amperage which too must not be exceeded. We see now that on top of respecting the phase capacities, as we need to respect also the devices' circuit capacities.
So we have a bit to think about. Whenever I get faced with a problem like this, the first thing I like to think is what information do we need? To even consider solving these load management problems we will always definitely need the maximum amperage to allocate to all the chargers (for example, the limit set by the circuit breaker), as well the devices at the station - including what phases they are attached to, what their maximum amperage is, what connectors they have and what the maximum amperage of their connectors.
Breaking Down The Problem
Personally, I think we still need to break this whole thing down into something more digestible. How this will be solved is still fairly out of sight right now, in my opinion. Maybe for the sake of thinking about it a bit more before jumping in, we should list out a set of aims, and also a set of restrictions/assumptions for this problem, so the outline of what we're trying to do can all be in one place.
Assumptions
We aim to:
- a) Allocate each active charging port a maximal amount of power
- b) Respect all amperage capacities
- c) Distribute the current proportional to how much each device requires
Then the restrictions/assumptions we have are:
- Devices are connected in parallel, therefore Kirchhoff's Law applies
- Hence, the devices connected to a phase can't draw more current than the phase supplies
- Each device has a maximal circuit amperage to be respected
- Each port of a device also has a seperate maximal amperage to respectected
- Each AC phase from the grid supplies the same amount amperage to the station
- There are no "2 phase" devices, just 3 phase and single phase - this is the case in practice
- There can be any number of devices. A device can have any number of ports
- Every port can readily have it's maximum amperage set
One more step I always think is absolutely necessary (especially in the context of creating algorithms) is to find a visual representation for any possible instance of the problem, in this case an 'instance of the problem' referring to a station. We want to represent a given station setup, which includes all the necessary information. A lot of people claim to be visual learners - you can benefit a lot from creating your own visual instead of struggling to find one, like so;
Example: Location A
The obvious initial choice is to represent this as a tree, since it does appear we have these 'layers' of dependency - in the sense that a device has multiple "child" ports which is distributes its amperage to, and phases have multiple "child" devices with the same relationship.
As an example, we consider a Location A which has 40 amps supplied by the grid and 3 chargers which are:
- A 3 phase device with max amperage 32A. Two active ports both with max amperage also 32A.
- A single phase device (connected to L2) with max amperage 12A. One sole active port with max amperage 10A
- A single phase device (connected to L3) with max amperage 22A, an inactive port with max amperage 22A, an active port with max amperage 22A.
I should acknowledge that it is, in practice, unlikely a charger will have ports with different max amperages (in fact, they usually have the same max as the device). But we need to extend our model to all cases to ensure it will generalise well in the caprice of the real world. That being said, with my basic understanding of circuits there may be factors I haven't considered - my goal here though is to create a model of the situation, and find a solution to that.
Nonetheless, describing Location A in the way we just have is in itself an example of why we need to represent this somehow else, writing it out is clearly verbose. We can instead model that example easily by representing it with the following diagram:
Note that this representation does not intend to (or adequately) represent the circuitry of the system, but rather the dependencies and restrictions on amperage in allocating a safe amount - essentially all the pieces relevant to the safe DLM problem. With this way of representing a location, therefore granting a data structure (the tree) for a location, we can finally move on to solving the problem. In a way it's given us an entry point, because now we realise we just need to think about how devices and phases should safely distribute their amperage as independent object - breaking it down one more step.
Solving The Model
We now need to use this tree structure to figure out what the optimal safe allocation is, or rather, an algorithm that does so.
As a starting point, the algorithm we derive should obviously involve allocating 0 amps to innactive chargers, but then allocating the rest of the power such that the proportion given to devices which require more power is more than those which need less. There needs to also be no remaining current to allocate, such as what would happen for example if we just set every connector to draw 1A, a probably-safe but inefficient amount.
The "I Gotta Ask My Dad" Algorithm
While the details of how this algorithm is implemented are only really interpretable by programmers or computer scientists, like much of these articles I want to explain it in a way that demonstrates the discrete thinking involved in coming up with such a method, as to be accessible to all - so I found an analogy we're all familiar with.
For those with siblings, or even cousins perhaps, we've all been in a position where you both want a certain amount of something, let's say it's cake, but there's only so much to go around. One sibling says "I want 2 slices of cake", and the other says "I want 4 slices of cake". Their parents then distribute what's available, in proportion to how much each sibling wants. If there's 3 slices of cake available, the parent would give 1 slice to sibling 1, and 2 slices to sibling 2 - distributing in proportion, exactly like what we want to do with our amperage.
This algorithm uses the exact same idea. Each active connector/port wants to draw as much amperage as possible, but before it does, it has to ask the parent node in dependency tree, then can use what it's given by that node. I'll give an example of the algorithm run on location A, as I run through the steps, which are as follows:
- Each active connector wants to use as much amperage as possible, but first it has to "ask it's dad". For each device, make a list of what amperage each of it's connectors will maximally draw, for a connector not being used, add 0 to the list - equivalent to "how much cake the sibling wants"
- Dad needs to figure out what proportion each child should get. Calculate the total amperage all the sibling connectors are asking for. If this is more than the devices maximum amperage, then for each number in that list, multiply it by (the devices maximum amperage / the total amperage you just calculated) - now each connector's proportional share is determined. But wait, it shouldn't tell the connectors yet, now the device has to "ask it's dad" whether it can draw the amount it needs to give to it's children.
- Now the phases (grandparents) have the hard task of figuring out what to give each device. They'll need to communicate about whether they're setting a limit due to too much strain caused by any attached devices, since some of them share a device. It doesn't work quite the same as previously because the device is asking multiple parents what it may draw instead - the relationship between the layers is different so we need a new approach.
- To start, the devices should make clear what current they are requesting - "asking their dad(s)" for the number of slices of cake they need to give to their children. Each phase should again make a list of what all their devices are asking for. What the grandparent phases will first need to do is handle the 3 phase devices, as they are asking all their parents for the same amount, but they will need to communicate about how much they can each give.
- Now the way we handle 3 phase devices first, is if any of the lists exceed what the phase can offer, we should again perform the task of recalculating the proportion. However, when the proportion of a 3 phase device is recalculated, be sure to let the other grandparents know, by updating them in the other lists with the minimum of the results. Note that in phase 3, values are rounded down and should stay that way
- Now we've got a pretty reasonable allocation - it is certainly safe, which is fantastic. However, the fact that 3 phase devices take the minimum result means that there might be some leftover current in a given phase which it can allow single phase devices to take! If we don't handle this, we've violated the aim of the algorithm being optimal (that is, making the most of the available current).
- What we want to do now then, is go through each phase one final time. For each phase we will do the following. Firstly, set the single phase devices back to the amount they requested, and make note of what the single phase requests sum to. Then, take the grid amperage, and subtract what we allocated to any 3 phase devices - to find what is remaining to allocate. Then, again if the sum of requests if larger, multiply each single phase device by (that result / the sum of their requests from the previous step). For our example, this will only make a difference on L2, can you see why? The others are full or have no single phase devices.
- That step is the most confusing visually. Maybe try it a few times with other examples solely following the explanation, if it doesn't make sense yet. Alas, we finally have an optimised and proportional amperage which exceeds no limits. Now the grandparents can tell the parents how much they can have. The parents should then recalculate the proportions they will give their children, if necessary.
- Then finally the parents can give the children what they asked for, finishing our big family argument.
For location A, the following gif summarises the entire process visually as a nice final product:
For any computer scientists concerned, with a basic operation of adjusting the amperage allocation of a node, this algorithm with m devices and n ports has time complexity O(n+m), which also must be the theoretical minimal as this could not be possibly solved without checking the amperage of each node once. A short proof by contradiction that O(n+m) is the minimal runtime complexity:
- Suppose the model is solvable in less than O(n+m)
- Then with n+m nodes with restraints, you must have not checked a restraint
- Therefore if a restraint was not checked, the allocation is not certainly safe, violating the aim, so there can be no solution faster than O(n+m). QED - Quite Easily Done
Note in practice it definitely may be able to be sped up, as the number of basic operations is actually 2n+6m in the worst case (n when "asking the device", 6m at most when "asking the phases", n when readjusting the connectors based on what the phase gave the device)
Conclusion
I was motivated to share this algorithm (as well as the upcoming one) as I was unable to find any EV load management algorithms online so created these from scratch myself, essentially looking to solve that problem for others of not being able to find a solution. Hopefully this was instructive, if anything was unclear feel free to comment.
While this algorithm may not be considered abstract enough to handle for example 2 phase devices, this is deliberate. This article intends to set the concrete groundwork for the slightly more involved computer-sciency algorithms in the next articles, one of which achieves a more final goal of finding the allocation which instead distributes power as evenly as possible amongst connectors - ensuring every person charging is getting as fair a share of the station's electricity as the next. The other of which will abstract away from load management, and provide a solution to all general distribution problems like in this arrangement.
If you intend on using this in practice, there are other considerations I highly recommend you also take into account, which I won't cover here but they have simple solutions. These include what order to send the connector reallocations in, such that it is safe. Because what if you increase one connector allocation before decreasing others? The circuit breaker trips - a hint to how you should handle this, there is a field in the source code DLBConnector type that can be sorted. Also how to treat device disconnections (what if it disconnects but starts drawing it's maximum?), you might have to make some assumptions to guarantee safety. It is also recommended you include some buffer on the grid amperage, for safety sake. Finally, remember this is just one iteration of dynamic load management - this algorithm would run every time the available grid amperage updates or the connector activity changes.