Stabilization of numerical solutions of boundary value problems exploiting invariants