Well, we have the value of pressure at a specific depth so I thought I could try and integrate to find the force and calculate the work done by gravity to see if I get the same thing if I used change in potential energy
What I did with like this, Assume a rectangular tube for simplicity. Make the reference point as following.
https://i.imgur.com/TwInjDb.png
Make an expression for the pressure of the lower tube in terms of H2 (##\rho g (H2-y)##) and for the upper tube (##\rho g (H2-y)##), I will neglect static pressure as I only want to find the work done by gravity. Since there is no change in energy for the middle part where the fluid goes up, it must be on the sides and we can calculate the external work done.
Integrate for the lower tube From y = 0 to y = H1 if the width of the tube is L to get the total force. I get
##F_T = \int_0^{H_1} \rho g (H_2 - y) L \, dy = L(\rho g H_2 H_1 - \frac{\rho g H^2_1}{2} ) ##
integrate for the lower tube From y = h2 to y = H2
##F_T= \int_{h_2}^{H_2} \rho g (H_2 - y) L \, dy = L \rho g \frac{(H_2 -h_2)^2}{2}##
Net external work, (##d_1## is the distance the fluid moves in the lower tube, ##d_2## is the distance the fluid moves in the upper tube)
## W_{ext} = L(\rho g H_2 H_1 - \frac{\rho g H^2_1}{2} ) d_1 - L \rho g \frac{(H_2 -h_2)^2}{2} d_2 ##
If we divide by V to get the work done per unit volume, we get:
## \rho g (H_2 -\frac{H_1}{2} ) - \rho g (\frac{H_2 -h_2}{2})##
rearranging gives,
## \rho g \frac{H_2+h_2}{2} - \rho g \frac{H_1}{2} ##
Which is the exact value of the change in potential energy but it is supposed to be negative because it is the work done. I honestly don't know if this is supposed to work. So it seems this method will only work if only the pressure increases as we go up so we can get negative work. For example, in a vertical tube (where the fluid is going down) to get the same thing as using the change in potential energy we have to say that the pressure decreases with pgh which is a bit counter intuitive...
Sorry if I didnt write all the steps, Hopefully you get what I was trying to do since the start of the thread. Many thanks in advance.