# APC Power Delivery Overhaul Thoughts on a re-engineering of Space Station 13 electrical delivery systems (primarily as they relate to the Area Power Controllers). ## Objectives of the Overhaul Current power systems have significant issues with their delivery architecture. Below, I'll detail the two I consider most notable. ### Additions of load to the power net never verify that the added load can actually be supplied. Machinery that consumes excess power (SMES and APC recharging) simply guesses whether enough power will be free on the tick. This leads to consumed power alternating wildly between exceeding and underutilizing the available amount, making the entire network volatile unless there's more than enough power to go around. The lack of any verification also causes other peripheral issues, such as any number of power sinks having no effect if overall supply exceeds each one's maximum drain rate. ### APC distribution malfunctions if load is asymmetrical. Unless there is a substantial excess of power, allocation of available grid power to each APC occurs based only on a fixed share called "perapc". If this share is lower than the amount the APC would otherwise consume, the remainder will ALWAYS be returned to the SMES, instead of going to other APCs whose draw exceeds that share. A grid flooded with power will ignore this per-APC limit, but under circumstances where power is merely adequate and a localized load temporarily exceeds available power, that per-APC limit will return, virtually guaranteeing a failure to supply the load despite the grid being able to satisfy a significant portion of it. ## Current System & Possible Alternatives ### Unverified Per-APC (current system) * On each reset, the powernet calculates a per-APC allotment based on a percentage of the upcoming tick's power. * APCs will attempt to cover their load by adding this share's load to the grid, regardless of whether that much power is available; if current overall availability exceeds last tick's load, they will add their entire load to the grid (again ignoring what's available) and recharge if that excess is maintained for several ticks. * Other machinery will recharge if current availability exceeds last tick's load. * On powernet reset, any remaining power after satisfying load (netexcess) will be reabsorbed into the SMES. ### Overhaul Basics The following changes are ones I consider core to any approach. All machinery that adds load to the grid will attempt to add its load to the grid in the order they're processed, and return a failure to supply if the attempted addition exceeds the remaining capacity. Machinery that considers itself low-priority (SMES, PTL, Nadir's transception array) will opt to apply load only if current availability exceeds last tick's load. On powernet reset, the powernet will attempt to allocate any remaining power after satisfying load (netexcess) to APCs which are not currently fully charged, offered proportionally based on the number of APCs which are connected to the grid and not fully charged. Any further excess is reabsorbed into SMES. This solves some problems on its own, but doesn't contend with load distribution issues. There are two approaches I've tried so far, of which the latter is my current approach in the PR. ### First Come, First Serve + Deference APCs, inside their own process as before, will try to satisfy their load from this available capacity in the order that they are processed. If the APC is above 80% capacity, it will consider half of its load "optional", and defer it to the netexcess charging phase, freeing up power to be routed to more heavily loaded APCs. **Advantages** * Achieves the goal of highly flexible distribution. * Computationally inexpensive - just adds the load to the grid, with a simple condition to perform basic load balancing. * Works within existing APC process architecture without significant architectural changes. **Disadvantages** * Deference threshold mechanic is unintuitive, and is somewhat of a "band-aid" for the next problem. * Due to distribution being based solely on processing order, any APCs at the end of the list will end up consistently losing power in low-power scenarios, meaning areas with relatively low power consumption will die more frequently for no particular reason. ### Grid-Side Iterative Allocation Approach used during first testmerge. APC load delivery is now prompted by the powernet itself, managed by the reset in the following sequence: * Functioning APCs connected to the powernet are added to a list and instructed to tally their load for the cycle (the first half of what used to be the APC process). * This list of APCs is sorted from the largest to smallest load currently being experienced by the APC. * All APCs receive a per-APC share of the capacity available at the time of powernet reset, reducing or zeroing out their load as appropriate. * APCs are then instructed in the previously-sorted order (largest to smallest load) to complete their cycle, expending cell power to cover the load and then reimbursing to the fullest possible extent with grid power (the second half of what used to be the APC process). **Advantages** * Provides a very robust power delivery system, by combining the guaranteed minimum of the current per-APC delivery system with new load-adapting capability. * Considerably more intuitive than FCFS+deference; to players, power is simply sent where it's needed, and highly loaded APCs will still be the ones to fail given insufficient grid power. **Disadvantages** * APCs in the "middle of the curve" (above the per-APC distribution, but not the most heavily loaded) can sometimes end up failing quicker than the most-loaded APCs, due to the emphasis on fully supplying the heaviest loads. * Significantly departs from existing power architecture; could introduce unforeseen consequences. Tried some things to get it to break (including rapidly cutting and mending the connection to an APC) and it seems to be reasonably resilient. **Notes for all Grid-Side Approaches** * As this moves APC satisfaction to the end of the cycle, all other sources of grid load added by machines will necessarily take priority. This isn't necessarily a bad thing, because "polite" loads (SMES, PTL, transception array internal capacitor) will avoid consuming power before the APCs are fully satisfied, and "assertive" loads (power sink, Oshan carousel) will consistently stop APCs from intaking power. ### Grid-Side Percentile Allocation Another powernet-managed variant. Current approach in the PR. APC load delivery is now prompted by the powernet itself, managed by the reset in the following sequence: * Functioning APCs connected to the powernet are added to a list, and their demand for the tick is tallied. * The power available for distribution to APCs is divided by the amount of draw necessary. This fraction or 1 (whichever is lower) becomes the "distribution percentage". * APCs are then instructed to complete their cycle, expending cell power to cover the load, then reimbursing it by an amount equal to load multiplied by the distribution percentage. **Advantages** * Ensures all APCs receive a proportional share of power distribution in both adequate and lacking power circumstances. * Likely the most intuitive approach so far, as it simply boils down to "if there's more draw than power, that percentage is how much it'll supply" for the end user. **Disadvantages** * In circumstances where grid power is moderate, this *may* have reduced momentary supply capability versus the iterative allocation approach. **Notes** * Sustained heavy loads in an area can cause supply to other APCs to dwindle, because the heavily-loaded APC will have a higher proportion and this approach lacks a guaranteed per-APC allocation. Whether this is a good or bad thing is debatable. * To fully simulate brown-outs, other direct-draw devices would likely have to be brought into the end-of-cycle distribution system. This isn't necessary for this approach to function, and should be very possible to add later, if desired. ## Thoughts on Grid Behavior To elaborate on the goal of these changes, I'll run through my thoughts on different power scenarios and how that interacts with current and proposed grid systems. ### Low Power Little or no power is available for the grid to distribute. APCs cannot reasonably have their load supplied, let alone receive an excess sufficient for charging. * Desired behavior: APCs lose power over time, with heavily-loaded APCs generally failing faster. * **Current** grid: Approximates that behavior, but fails to do so responsively. APCs with low load will continue to break even on charge, as their per-APC share will almost always still be sufficient to cover costs, while APCs on heavier loads will fail rapidly. Assuming at least one APC still has lower load than its per-APC share, 50% of ticks will have their load fully covered regardless of true availability if there are no other direct loads on the grid, due to per-APC share reclamation creating an "excess" every other tick, which the APCs will interpret as surplus. * **Iterative** grid: Approximates that behavior much more responsively, but in a manner slightly skewed towards highest load. Per-APC distribution will attempt to reclaim what it can to cover APCs' load, then the most heavily-loaded APCs will receive the highest priority after that distribution occurs; APCs with more moderate loads may end up losing power at a similar rate to the most heavily-loaded APCs because of this prioritization. Distribution will always use up to, but not in excess of, the available power. * **Percentile** grid: Matches that behavior fairly well. Low-load APCs will draw down a little, and heavily-loaded APCs will draw down a little more. Distribution will always use all available power, as with Iterative. ### Break-Even Power Available power roughly matches the sum of the loads each APC is experiencing under idle conditions; momentary loads (manufacturers, cell charging, telescience portals) will frequently cause load to exceed available power, but the general circumstance is close to breaking even. * Desired behavior: APCs will approximately maintain their level of charge. Highly-loaded APCs will trend slightly downwards, or heavily so during momentary loads. * **Current** grid: Does not match desired behavior. The per-APC allocation is not a minimum, but a mandatory distribution; any power that isn't consumed in the share is simply returned to SMES, leaving other APCs with slightly or moderately higher loads without supply they could be receiving and causing them to lose power fairly rapidly. Because of this returned-to-SMES portion, every other tick of loads that aren't fully supplied will make up this difference for free. * **Iterative** grid: Decently approximates desired behavior. The per-APC minimum allocation is distributed, then power reclaimed from that is given to the APCs experiencing highest load. If power doesn't fully cover load, moderately-loaded APCs may lose charge at a similar rate to heavily-loaded ones, but this difference is mitigated by the first allocation stage, and those heavily-loaded APCs will be prevented from failing to the extent the grid can manage. * **Percentile** grid: Solidly approximates desired behavior. APC distribution will typically be fairly homogenous; areas with frequent high load will deplete at a faster rate than low-load areas if overall charge is trending downward, as even though distribution is proportional through the percentile, recharging is distributed equally to all APCs that aren't full. ### Moderate Surplus Available power considerably exceeds the sum of the loads each APC is experiencing under idle conditions, resulting in a net surplus most of the time, but particularly heavy momentary loads can still cause overall load to exceed grid capacity. * Desired behavior: APCs will have their load covered well, with cells recharging at most times and momentary loads supplied to the extent the grid is able when they arise. * **Current** grid: Adequate in idle conditions, but stutters severely during momentary loads. Per-APC allocation is only consistently ignored when the grid is maintaining a continual excess; if load suddenly matches or exceeds available power, instead of yielding what power is possible, the grid will return to its fixed allocation, starving any heavily-loaded areas of power even if an amount of power that would cover the majority of the load is available. * **Iterative** grid: Good approximation of desired behavior. In addition to functioning well during surplus, momentary increases in load will be satisfied by the grid to the fullest extent it permits; small discrepancies between heavily-loaded and moderately-loaded APCs will be smoothed out by the recharging procedure. * **Percentile** grid: Near-perfect approximation of desired behavior. Momentary brownouts will most heavily affect the heavily-loaded APCs, as they ought to, and full-supply circumstances will work without issue.