[guided]The goal of this step is to isolate the exact square that later appears in Perelman's monotonicity formula. For each $t\in[0,T]$, let $S_g:M\to\mathbb{R}$ denote the scalar curvature of $g(t)$, let $\operatorname{Ric}_g$ denote the Ricci curvature of $g(t)$, let $\nabla$ denote the Levi-Civita connection of $g(t)$, let $\operatorname{Hess}_{g} f$ denote the Hessian of $f(\cdot,t)$, let $\Delta_g=\operatorname{div}_g\nabla$ denote the Laplace-Beltrami operator, and let $d\mu_g$ denote the Riemannian volume measure. Define $Q:M\times[0,T]\to\mathbb{R}$ by
\begin{align*}
Q(x,t)=S_{g(t)}(x)+|\nabla f(x,t)|_{g(t)}^2.
\end{align*}
Define the functional $\mathcal F:[0,T]\to\mathbb{R}$ by
\begin{align*}
\mathcal F(t)=\int_MQ(\cdot,t)u(\cdot,t)\,d\mu_{g(t)}.
\end{align*}
Because $g$, $f$, and $u$ are smooth in space and time on compact $M$, all curvature quantities and time derivatives below are smooth and bounded. This justifies differentiating under the integral sign. Since $M$ is closed, the Laplace-Beltrami operator is self-adjoint with no boundary contribution. Using $\partial_t(u\,d\mu_g)=-\Delta_gu\,d\mu_g$, we get
\begin{align*}
\frac{d}{dt}\mathcal F(t)=\int_M(\partial_tQ-\Delta_gQ)u\,d\mu_g.
\end{align*}
The scalar-curvature evolution under Ricci flow gives
\begin{align*}
\partial_tS_g=\Delta_gS_g+2|\operatorname{Ric}_g|_g^2.
\end{align*}
The metric variation $\partial_tg=-2\operatorname{Ric}_g$ gives
\begin{align*}
\partial_t|\nabla f|_g^2=2\operatorname{Ric}_g(\nabla f,\nabla f)+2\langle\nabla f,\nabla\partial_tf\rangle_g.
\end{align*}
Bochner's identity gives
\begin{align*}
\Delta_g|\nabla f|_g^2=2\langle\nabla f,\nabla\Delta_gf\rangle_g+2|\operatorname{Hess}_{g} f|_g^2+2\operatorname{Ric}_g(\nabla f,\nabla f).
\end{align*}
Combining these three formulas cancels the two Ricci-gradient terms and yields
\begin{align*}
\partial_tQ-\Delta_gQ=2|\operatorname{Ric}_g|_g^2-2|\operatorname{Hess}_{g} f|_g^2+2\langle\nabla f,\nabla(\partial_tf-\Delta_gf)\rangle_g.
\end{align*}
Now substitute the equation for $f$,
\begin{align*}
\partial_tf-\Delta_gf=-2\Delta_gf+|\nabla f|_g^2-S_g+\frac{n}{2\tau}.
\end{align*}
The final summand in the displayed equation is spatially constant, so its gradient is zero. Therefore
\begin{align*}
\int_M\langle\nabla f,\nabla(\partial_tf-\Delta_gf)\rangle_gu\,d\mu_g
=-2\int_M\langle\nabla f,\nabla\Delta_gf\rangle_gu\,d\mu_g
+2\int_M\operatorname{Hess}_{g} f(\nabla f,\nabla f)u\,d\mu_g
-\int_M\langle\nabla f,\nabla S_g\rangle_gu\,d\mu_g.
\end{align*}
We next compute the first and third terms. Since $u$ has the displayed form, its spatial gradient is
\begin{align*}
\nabla u=-u\nabla f.
\end{align*}
Integrating Bochner's identity against $u$ and using self-adjointness of $\Delta_g$ on closed $M$ gives
\begin{align*}
\int_M\frac12\Delta_g|\nabla f|_g^2u\,d\mu_g
=-\int_M\frac12\langle\nabla|\nabla f|_g^2,\nabla u\rangle_g\,d\mu_g.
\end{align*}
Since $\nabla u=-u\nabla f$, this equals
\begin{align*}
\int_M\operatorname{Hess}_{g} f(\nabla f,\nabla f)u\,d\mu_g.
\end{align*}
Thus Bochner's identity implies
\begin{align*}
\int_M\langle\nabla f,\nabla\Delta_gf\rangle_gu\,d\mu_g
=\int_M\operatorname{Hess}_{g} f(\nabla f,\nabla f)u\,d\mu_g
-\int_M|\operatorname{Hess}_{g} f|_g^2u\,d\mu_g
-\int_M\operatorname{Ric}_g(\nabla f,\nabla f)u\,d\mu_g.
\end{align*}
For the scalar-curvature term, use the contracted Bianchi identity
\begin{align*}
\operatorname{div}_g\operatorname{Ric}_g=\frac12\nabla S_g.
\end{align*}
Applying the [divergence theorem](/theorems/3614) on the closed manifold $M$ to the vector field $u\operatorname{Ric}_g(\nabla f,\cdot)^{\sharp}$ gives
\begin{align*}
0
=\int_M\operatorname{div}_g\left(u\operatorname{Ric}_g(\nabla f,\cdot)^{\sharp}\right)\,d\mu_g.
\end{align*}
Expanding the divergence gives
\begin{align*}
0
=\int_M\langle\nabla u,\operatorname{Ric}_g(\nabla f,\cdot)^{\sharp}\rangle_g\,d\mu_g
+\int_Mu\langle\operatorname{div}_g\operatorname{Ric}_g,\nabla f\rangle_g\,d\mu_g
+\int_Mu\langle\operatorname{Ric}_g,\operatorname{Hess}_{g} f\rangle_g\,d\mu_g.
\end{align*}
Using $\nabla u=-u\nabla f$ and the contracted Bianchi identity, this becomes
\begin{align*}
0
=-\int_M\operatorname{Ric}_g(\nabla f,\nabla f)u\,d\mu_g
+\frac12\int_M\langle\nabla S_g,\nabla f\rangle_gu\,d\mu_g
+\int_M\langle\operatorname{Ric}_g,\operatorname{Hess}_{g} f\rangle_gu\,d\mu_g.
\end{align*}
Solving this identity for the scalar-curvature integral yields
\begin{align*}
\int_M\langle\nabla S_g,\nabla f\rangle_gu\,d\mu_g
=2\int_M\operatorname{Ric}_g(\nabla f,\nabla f)u\,d\mu_g
-2\int_M\langle\operatorname{Ric}_g,\operatorname{Hess}_{g} f\rangle_gu\,d\mu_g.
\end{align*}
Substituting the Bochner and Bianchi evaluations into the expanded weighted gradient term, the terms involving $\operatorname{Hess}_{g} f(\nabla f,\nabla f)$ cancel and the terms involving $\operatorname{Ric}_g(\nabla f,\nabla f)$ cancel. The remaining terms are
\begin{align*}
\int_M\langle\nabla f,\nabla(\partial_tf-\Delta_gf)\rangle_gu\,d\mu_g
=2\int_M\left(|\operatorname{Hess}_{g} f|_g^2+\langle\operatorname{Ric}_g,\operatorname{Hess}_{g} f\rangle_g\right)u\,d\mu_g.
\end{align*}
Substituting this identity into the derivative of $\mathcal F$ gives
\begin{align*}
\frac{d}{dt}\mathcal F(t)
=2\int_M|\operatorname{Ric}_g|_g^2u\,d\mu_g
-2\int_M|\operatorname{Hess}_{g} f|_g^2u\,d\mu_g
+4\int_M\left(|\operatorname{Hess}_{g} f|_g^2+\langle\operatorname{Ric}_g,\operatorname{Hess}_{g} f\rangle_g\right)u\,d\mu_g.
\end{align*}
Collecting the three terms gives
\begin{align*}
\frac{d}{dt}\mathcal F(t)
=2\int_M\left(|\operatorname{Ric}_g|_g^2+2\langle\operatorname{Ric}_g,\operatorname{Hess}_{g} f\rangle_g+|\operatorname{Hess}_{g} f|_g^2\right)u\,d\mu_g.
\end{align*}
The last integrand is the metric square of the symmetric $2$-tensor $\operatorname{Ric}_g+\operatorname{Hess}_{g} f$, so
\begin{align*}
\frac{d}{dt}\mathcal F(t)=2\int_M|\operatorname{Ric}_g+\operatorname{Hess}_{g} f|_g^2u\,d\mu_g.
\end{align*}[/guided]