How do the formula of electrostatic energy change from vacuum to with matter?

I know that, in vacuum, the electrostic energy is:

$$U_E = \frac12 \int \rho(\mathbf r)\varphi(\mathbf r) d^3\mathbf{r}$$

But I don't know how to pass to the matter version? The formula would be the same, but what is $$\rho(\mathbf r)$$ now?, Is it free charge or total charge (free plus bound charge) density?

P.D.: I am studying classical electrodynamics

• @Elborito it is hard to be more precise, since it depends on what is $\rho$ and $\varphi$ in your equation. Yours is just general equation for electrostatic energy. Jun 7, 2020 at 6:27
• And if you have a polarization density $\mathbf P$ no dependent of the electric field $\mathbf E$, what do you suggest? Jun 7, 2020 at 15:06
• @BorisValderrama in this case, you have to evaluate the work (and if the contribution is conservative, the energy) of that polarization field. As an example, let's assume you have a prescribed "a priori" uniform polarization $\mathbf{P}^0$. You evaluate that contribution to work when you build the configuration of the system. The electric field due to this polarization is $\mathbf{E}^0 = -\frac{\mathbf{P^0}}{\epsilon_0}$, the elementary work reads $dW = -q \frac{\mathbf{P^0}}{\epsilon_0} \cdot d\mathbf{r} = - q d \left(\frac{\mathbf{P^0}}{\epsilon_0} \cdot \mathbf{r} \right) = q d \Phi^0$ Aug 25, 2022 at 13:05