  We present a unified method, based on convex optimization, for managing the
power produced and consumed by a network of devices over time. We start with
the simple setting of optimizing power flows in a static network, and then
proceed to the case of optimizing dynamic power flows, i.e., power flows that
change with time over a horizon. We leverage this to develop a real-time
control strategy, model predictive control, which at each time step solves a
dynamic power flow optimization problem, using forecasts of future quantities
such as demands, capacities, or prices, to choose the current power flow
values. Finally, we consider a useful extension of model predictive control
that explicitly accounts for uncertainty in the forecasts. We mirror our
framework with an object-oriented software implementation, an open-source
Python library for planning and controlling power flows at any scale. We
demonstrate our method with various examples. Appendices give more detail about
the package, and describe some basic but very effective methods for
constructing forecasts from historical data.
