Water hammer is the result of: