My question is why can we exchange the variation and gradient in the following equation that I found in some paper: $$\int \phi \, \delta(-\vec{\nabla}\cdot \vec{P})\, dV = \int \nabla\phi (\delta \vec{P})\, dV.$$
Is there some derivative rule that I am not familiar with or is it the peculiarity of the situation?
Here is some background: A variation on the form of electrostatic energy of a polarization density field $\vec{P}$ can be written like this
$$\delta E=\int \phi \,\delta\rho\, dV.$$
Here, $\phi$ is the electric potential and $\rho$ is bound charge density. From Gauss' law we can write the dipole charge density with the polarization field,
$$\rho=-\vec{\nabla}\cdot \vec{P}.$$
Inserting this into the first equation we get
$$\int \phi \, \delta(-\vec{\nabla}\cdot \vec{P})\, dV = \int \nabla\phi (\delta \vec{P})\, dV.$$
Edit 1:
There seems to be a divergence theorem at work here. First we see that $$\delta(\vec{\nabla}\cdot \vec{P})=\vec{\nabla} \cdot \delta \vec{P}.$$
Our original integral can now be rewritten, so that
$$\int \phi \, \delta(-\vec{\nabla}\cdot \vec{P})\, dV = -\int \phi \, \delta(\vec{\nabla}\cdot \vec{P})\, dV = -\int \phi \,(\vec{\nabla} \cdot \delta \vec{P})\, dV.$$
The product rule for multiplication by scalar gives us
$$\vec{\nabla}\cdot(\phi \,\delta \vec{P} )=(\nabla \phi)\cdot \delta\vec{P}+\phi( \vec{\nabla}\cdot \delta \vec{P}),$$
so the last integral can again be rewritten so that
$$-\int \phi \,(\vec{\nabla} \cdot \delta \vec{P})\, dV=-\left(\int[\vec{\nabla}\cdot(\phi \,\delta \vec{P} )-(\nabla \phi)\cdot \delta\vec{P}]\, dV \right).$$
The divergence theorem can be applied to the first integral on the right hand side of the last equation, so that
$$\int\vec{\nabla}\cdot(\phi \,\delta \vec{P} )\, dV=\oint_S(\phi \,\delta \vec{P} )\cdot \vec{n}\, dS.$$
If the last integral is equal to zero, which seems to be the case here, than the final expression is
$$\int \phi \, \delta(-\vec{\nabla}\cdot \vec{P})\, dV=\int \nabla\phi (\delta \vec{P})\, dV.$$
The only thing to figure out now is why the surface integral should be equal to zero.