DEM: A variational treatment of dynamic systems