Learning differentiable solvers for systems with hard constraints