I want to calculate the following integral: $$S = \int\vec{a}\cdot\nabla(\nabla\cdot\vec{a})\,\text{d}V$$
I tried to calculate the integral of the $i$-th term, i. e., $\int a_i\partial_i(\nabla\cdot\vec{a})\,\text{d}V$, but I got to this expression $$S = \int a_i\partial_i(\partial_ja_j)\,\text{d}V=a_i\partial_ja_j-\int(\partial_ia_i)(\partial_ja_j)\,\text{d}V$$ where the first term on the right side turns to be a vector. What am I doing wrong? I know that the result is $$S = \int\vec{a}\cdot\nabla(\nabla\cdot\vec{a})\,\text{d}V=\text{something}-\int(\nabla\cdot\vec{a})^2\,\text{d}V$$ so I guess that the only problem with my calculation is on that first term.
If you apply the divergence theorem (or integration by parts), you get that $$ S = \int_V a \cdot \nabla(\nabla \cdot a) dV = \int_{\partial V} (\nabla \cdot a) a \cdot n dS - \int_V (\nabla \cdot a)^2 dV, $$ where $n$ denotes a normalized pointing outward normal vector orthogonal to the boundary $\partial V$.