Solution (source code)

= Solution

Put $h=u-\phi-C$. Then $h=0$ on $S$, and the constant does not change a <gradient>, so $\nabla u=\nabla\phi+\nabla h$. Expand the unnormalized <Dirichlet energy>:
$$
\int_V|\nabla u|^2\,dV
=\int_V|\nabla\phi|^2\,dV
+2\int_V\nabla\phi\cdot\nabla h\,dV
+\int_V|\nabla h|^2\,dV.
$$
The cross term is zero by <Green's first identity>:
$$
\int_V\nabla\phi\cdot\nabla h\,dV
=\int_Sh\,\partial_n\phi\,dS-\int_Vh\Delta\phi\,dV=0.
$$
Thus the useful stronger identity is
$$
\boxed{\int_V|\nabla u|^2\,dV-\int_V|\nabla\phi|^2\,dV
=\int_V|\nabla(u-\phi-C)|^2\,dV\geq0.}
$$
Equality holds exactly when $h$ has zero <gradient>. It is then constant on every <connected component> and zero on that component’s boundary, hence $h=0$. Therefore \b[equality holds precisely when $u=\phi+C$ throughout $V$.] This is the <Dirichlet principle>; an additive boundary constant does not alter the minimizing energy. The energy here omits the optional factor $1/2$, which does not affect minimizers or the inequality.