I'm reading a textbook example on the finite element method:
$\nabla^T[D(x,y,z)\nabla u] - a(x,y,z)u + f = 0 $ in R
$\partial R= \partial R_1 \bigcup \partial R_2$, $\partial R_1 \bigcap \partial R_2 \neq \varnothing$
u=r(x,y,z) on $\partial R_1$
$D \nabla u \centerdot n = -p(x,y,z)u + q(x,y,z)$ on $\partial R_2$
where $n$ is a unit outward normal vector, and $D(x,y,z) > 0$ and $a(x,y,z),p(x,y,z),$ and $q(x,y,z)\ge0$.
I understand how to obtain the weak formulation using the test function $v$:
$\iiint\limits_R [-D\nabla u\centerdot \nabla v -auv + fv]dV + \iint\limits_{\partial R_2}[-puv+qv]dA$, where dV and dA are the volume and area differentials.
I'm looking for a bound on the Galerkin method error. The book I'm reading seems to use an energy functional of the form:
$F(u) = \iiint\limits_R \{ \frac{1}{2}D|\nabla u|^2 + \frac{1}{2}au^2 - fu \}dV + \iint\limits_{\partial R_2} \{ pu^2 - qu \} dA$.
I'm not sure exactly how this energy functional was derived. It almost looks like the weak formulation integrated once with respect to u, then integrated over the respective domains. But I don't think this is true for the $|\nabla u|^2$ term. How could I have come up with this appropriate energy functional on my own? What guidelines can I follow, in general, to obtain this functional for numerical error analysis?