How can I calculate the flux inside this shape? 
Here's a picture from my multivariable calculus textbook. The question is in a chapter about the divergence theorem. I'm going to sum it here (sorry for my bad English):
A curved pipe $S$ is illustrated. The boundary of $S$ is a pair of circles $C_1$ and $C_2$. The circle $C_1$ has radius $3$ and is contained in the plane $y=0$ and $C_2$ has radius $1$ and is contained inside the plane $z=4$. The volume of $S$ is $12$. Calculate the flux of the field $F=g(y,z)i+8j-(2z+3)k$, where $g$ has continuous partial derivatives.
I took the divergence of $F$, which gave me ${\dfrac{d}{dx}}g-2$. Using the divergence theorem to find the flux, I then multiplied this result by the volume, $12$. The answer of the problem is $83π-24$. I got the $-24$ by multiplying $-2$ by $12$, but where does the $83π$ come from? Thanks!
 A: Let's call the lateral surface of the pipe $S$ and the surface at the ends $S_1$ and $S_2$ corresponding to the circles $C_1$ and $C_2$. You should note that the union of $S$, $S_1$, and $S_2$ will construct a closed surface. Let us call this closed surface $\partial \Omega$. Then the divergence theorem can be applied to the volume $\Omega$ surrounded by the closed surface $\partial \Omega$. Hence, we can write
$$\begin{align}
\int_{\Omega} \nabla \cdot F dV &= \oint_{\partial \Omega} F.dS \\
\int_{\Omega} \nabla \cdot F dV &= \int_{S} F.dS + \int_{S_1} F.dS + \int_{S_2} F.dS \\
\end{align}$$ 
and hence
$$ \int_{S} F.dS = \int_{\Omega} \nabla \cdot F dV - \int_{S_1} F.dS - \int_{S_2} F.dS $$
which finally leads to
$$ \int_{S} F.dS = (-2 \cdot 12) - (-8 \cdot \pi \cdot 3^2) - (-11 \cdot \pi \cdot 1^2) = -24 +72 \pi+11 \pi= 83 \pi-24 $$
A: If you complete surface $S$ by two diskes $D_1$, $D_2$ whose boundaries are $C_1$ and $C_2$, it will be the boundary $\partial X$ for some sort of cylindrical region $X$. Divergence theorem tell us
$$\left( \int_{S} + \int_{D_1} + \int_{D_2} \right) F\cdot dA 
= \int_{S\cup D_1 \cup D_2} F\cdot dA = \int_{\partial X} F\cdot dA\\
= \int_X \nabla\cdot F dV = -2\verb/Vol/(X) = -2\cdot 12 = -24\tag{*1}$$
Based on what has been shown in the picture.
On $D_1$, the outward pointing normal of $\partial X$ is pointing in the $-ve$ $y$-direction.
Its contribution to the flux is
$$\int_{D_1} F\cdot dA = (-1) 8\; \verb/Area/(D_1) = -8 (\pi 3^2) = -72\pi$$
On $D_2$, the outward pointing normal of $\partial X$ is pointing in the $+ve$ $x$-direction.
Its contribution to the flux is (notice $z$ is a constant on $D_2$ )
$$\int_{D_2} F\cdot dA = -(2*4+3)\; \verb/Area/(D_2) = -11(\pi 1^2) = -11\pi$$
Substitute these two expression into $(*1)$, you get
$$\int_{S} F\cdot dA - 72\pi - 11\pi = -24
\quad\iff\quad\int_{S} F\cdot dA = 83\pi - 24$$
A: To use the divergence theorem, your surface must be closed. Here, it is not the case. One way of working around this is to write
$$
\phi = \iint_S \vec{F}\cdot d\vec{S}= \iint_S \vec{F}\cdot d\vec{S}+ 
\iint_{S_1} \vec{F}\cdot d\vec{S}+ \iint_{S_2} \vec{F}\cdot d\vec{S}-
 \iint_{S_1} \vec{F}\cdot d\vec{S}- \iint_{S_2} \vec{F}\cdot d\vec{S},
$$
where $S_1$ and $S_2$ are the discs that close the pipe.
By Chasles' theorem, you can put the three first terms together:
$$
\phi = \iint_{S\cup S_1 \cup S_2} \vec{F}\cdot d\vec{S}-
 \iint_{S_1} \vec{F}\cdot d\vec{S}- \iint_{S_2} \vec{F}\cdot d\vec{S}
$$
And this is a good idea because you now have a closed surface ($S\cup S_1 \cup S_2$) on which you can use the divergence theorem:
$$
\phi =\iiint_E div\vec{F}\; dV - \iint_{S_1} \vec{F}\cdot d\vec{S}- \iint_{S_2} \vec{F}\cdot d\vec{S},
$$
with $div\vec{F}=0+0-2$. 
Since $S_1$ and $S_2$ are discs in the planes $y=0$ and $z=4$, they have unitary normal vectors oriented outwards  equal to $(0,-1,0)$ and $(0,0,1)$. It follows that 
$$
\phi = -2 \text{Volume}(E) -\iint_{S_1} -8 dS - \iint_{S_2} -(2z+3) \;dS\\
=-2 \text{Volume}(E)+8 \text{Area}(S_1)+\iint_{S_2}2z+3 \;dS\\
=-24+72\pi +\iint_{S_2}2z+3 \;dS
$$
Now for the last integral, $z=4$ on $S_2$, so it is equal to $11\text{Area}(S_2)=11\pi$. It follows that 
$$
\phi=-24+72\pi+11\pi=-24+83\pi.
$$
Note.
 You can also do this with the Stoke's theorem (do you see why?)! It is a good exercise to show that it will give you the exact same equations…
