What is the derivation for the fluid dynamics continuity equation In my Fluid Dynamics module I have begun learning about mass flux. In my most recent lecture I have been presented with the following text and equation:
For a general fluid in some volume $V$, enclosed by the surface $S$, the conservation of mass is expressed through the continuity equation (integral form):
$$\iiint_V \frac{\partial\rho}{\partial t}\text{d}V=-\iint_S \rho\vec{v}\cdot\hat{n}\text{d}S$$
where $\rho(\vec{x})=\rho$.
I do not understand how these two integrals are equal to one another and would appreciate if anybody would be able to help me understand.
 A: The first thing to note is that this is a physical law, not a mathematical identity. Those integrals are certainly not equal for arbitrary $\rho$ and $\mathbf v$. So we need to think about what those integrals are saying.
The first integral adds up the rate of change of fluid density at each point in the volume. Note that because of the linearity of the derivative, we can pull it outside the integral to get
$$
\iiint_V \frac{\partial \rho}{\partial t}d^3V = \frac{d}{dt}\iiint_V\rho d^3V = \frac{dM_{inside}}{dt},
$$
where $M_{inside}$ is the total mass of fluid inside the volume.
The second integral is adding up $\rho \mathbf v$, which is the flow rate of mass density in the fluid. It integrates this flow rate over the boundary of the volume, so it's finding the total rate at which fluid mass is leaving the volume. If you're having trouble seeing this, picture a tube of cross section $d^2S$ flowing through the surface at velocity $\mathbf v$. During a time interval $dt$, a fluid element of length $\mathbf {v}\cdot\hat{\mathbf{n}}dt$ passes through the surface. This element has mass $d^3M_{outflow} = \rho \mathbf {v}\cdot \hat{\mathbf{n}}d^2Sdt$, so the rate of mass flow out of that element of the surface is $d^3M_{outflow}/dt = \rho\mathbf v\cdot\hat{\mathbf n}d^2S$. Integrate all the surface elements and you get the total mass flow out of the volume:
$$
\iint_{\partial V}\rho\mathbf v\cdot\hat{\mathbf n}d^2S = \frac{dM_{outflow}}{dt}.
$$
Now, for mass to be conserved, the rate of mass leaving the volume must be equal to the rate at which the mass of the volume is decreasing. That is,
$$
\frac{d M_{inside}}{dt} = -\frac{dM_{outflow}}{dt}.
$$
Substituting our integrals gives
$$
\iiint_V \frac{\partial \rho}{\partial t}d^3V = -\iint_{\partial V}\rho\mathbf v\cdot\hat{\mathbf n}d^2S.
$$
