#Units and Dimensions of Force
# Variable declaration
m = 3.                        #Mass of the body (lbm)
g_fps = 32.174                #Acceleration due to gravity in fps units (ft/s2)
confaclbmtolbf = 32.174       #Conversion factor from lbm to lbf(lbm.ft/lbf.s2)
confaclbmtog = 453.59         #Conversion factor from lbm to gram (g/lbm)
g_mks = 980.665               #Acceleration due to gravity in mks  units (cm/s2)
confaclbtokg = 2.2046         #Conversion factor from lbm to kg (kg/lbm)
g_SI = 9.80665                #Acceleration due to gravity in SI units (m/s2)
    # Data SI Units 
#Calculation
#(a)
    #Fa = m*g_fps*ga
Fa = m*g_fps/confaclbmtolbf
    #Fb = m*g_mks*mb
Fb = m*g_mks*confaclbmtog
    #Fc = m*g_si*gc
Fc = m*g_SI/confaclbtokg
#Result
print "(a) The force in lbf is ",round(Fa,4),"lbf"
print '(b) The force in dynes is %6.4e'%(Fb),"dyne"
print "(c) The force in Newton is ",round(Fc,2),"N"
print 'The answers are correct because of book uses rounded numbers'
#Pressure in Storage Tank
# Variable declaration
Ht = 3.66             #Height of the tank (m)
P  = 1.               #Pressure exerted  (atm)
Hto = 3.05            #Height of tank filled with oil (m)
Htw = 0.61            #Height of tank filled with water (m)
Rho_oil = 917.        #Density of oil (kg/m3)
Rho_H2O = 1000.       #Density of H2O (kg/m3)
g = 9.8               #Gravitational acceleration (m/s2)
    # Data  
Patm_psia = 14.696        #Atmospheric pressure (psia)
P_pa   = 101325.          #Atmospheric pressure (pa)
Ht_ft  = 12.              #Height of the tank (ft)
Hto_ft  = 10.             #Height of tank filled with oil (ft)
Htw_ft = 2.               #Height of tank filled with water (ft)
confacsitofps = 0.06243   #conversion factor for density(kg/m3) to (lbm/ft3)
confacft2toin2 = 1./144.  #Conversion factor (ft2) to (in2)  
# Calculation
    #In fps units
    #Pressure exerted by oil in fps units 
    #P1f = Hto_ft*Rho_oil*confacsitofps*confacft2toin2 + P_psia
P1f = (Hto_ft*Rho_oil*confacsitofps*confacft2toin2 + Patm_psia)   
    #P2f = Htw_ft*Rho_H2O*confacsitofps*confacft2toin2 + P1f
P2f=  Htw_ft*Rho_H2O*confacsitofps*confacft2toin2 + P1f    
    #Pressure exerted in SI units
#Pressure exerted by oil in SI units 
P1si =  Hto*Rho_oil*g + P_pa
#Pressure exerted by water in SI
P2si = Htw*Rho_H2O*g + P1si
Pgage = P2f - Patm_psia
#Result
print "Pressure exerted by oil in fps ",round(P1f,2),"psia"
print 'Pressure exerted by oil in SI %6.3e Pa'%(P1si)
print "Pressure exerted by water in fps ",round(P2f,2),"psia"
print 'Pressure exerted by H2O in SI  %6.3e Pa'%(P2si)
print 'Gage pressure at the bottom of tank %5.2f psig'%(Pgage)
print 'The answers are correct because of book uses rounded numbers'
#Conversion of a pressure to head of a fluid 
# Variable declaration
P = 101325.  #Standard atmospheric pressure (kN/m2)
# Data  
Rho_H2O = 1000.       #Density of H2O at 4 deg C (kg/m3)
Rho_Hg  = 13.5955     #Density of Hg at 0 deg C (g/cm3)
g = 9.80665           #Gravitational acceleration (m/s2)
Rho_H2O_cgs = 1.      #Density of H2O at 4 deg C (g/cm3)
    
    # Calculation
#Pressure head in terms of H2O
    # H_H2O = Pressure/(density of H2O*Gravitational acceleration)
H_H2O = P/(Rho_H2O*g)
    #H_Hg = Pressure/(density of Hg*Gravitational acceleration)
H_Hg = H_H2O*Rho_H2O_cgs/Rho_Hg
#Result
print "Pressure head of H2O ",round(H_H2O,2),"m of water at 4°C"
print "Pressure head of Hg ",round(H_Hg,3),"mmHg"
#Pressure Difference in a Manometer
# Variable declaration
R_cgs = 32.7    #Reading of manometer (cm)
# Data  
Rho_H2O = 1000.         #Density of H2O  (g/cm3)
Rho_Hg  = 13600.        #Density of Hg  (g/cm3)
g = 9.80665             #Gravitational acceleration (m/s2)
R_SI = 0.327            #Reading of manometer (m)    
    # Calculation
#Pressure difference in SI units
    #delP = R_SI*(Rho_Hg-Rho_H2O)*g
delP = R_SI*(Rho_Hg-Rho_H2O)*g   
#Result
print ' The pressure difference in SI units  %10.3e'%(delP),"N/m2"
print 'The answer is correct because of book uses rounded numbers'
#Molecular Transport of a Property at Steady State 
# Variable declaration
C1 = 0.0137    #Concentration at point 1 (property/m3)
C2 = 0.0072    #Concentration at point 2 (property/m3)
z1 = 0         #Distance at point 1 (m)
z2 = 0.4       #Distance at point 2 (m)
z = 0.2        #Midpoint   (m)
delta = 0.013  #Diffusivty (m2/s)
# Calculation
    #Calculation for part (a)
    #Flux = delta*(C1-C2)/(z2-z1)
Flux = delta*(C1-C2)/(z2-z1)
    #Calculation for part (c)
C = C1 +  Flux*(z1-z)/delta 
#Result
print '(a) The flux is  %6.3e property/s.m2'%(Flux)
print '(c) The concentration at the midpoint is %6.3e property/m3'%(C)
#Calculation of Shear Stress in a Liquid
# Variable declaration
confaccgstofps = 0.0020875
confaccgstosi = 1/10.
dely = 0.5        #The distance between the two plates (cm)
delVz = 10.       #The velocity change (cm/s)
u = 0.0177        #The viscosity of liquid (g/cm.s)    
#Calculation
   #Calculation for part (a)
    #Calculation of shear stress 
    #Tyz = u(delVz)/(dely)
Tyz_cgs = u*(delVz)/(dely)
    #Calculation of Shear Rate
    #Shear Rate = delVz/dely
Vyz_cgs = delVz/dely
    #Calculation for part (b)
Tyz_fps = Tyz_cgs*confaccgstofps
    #Calculation for part (c)
Tyz_SI = Tyz_cgs*confaccgstosi
#Result
print "(a)Shear stress in cgs is",round(Tyz_cgs,4),"dyne/cm2"
print "   Shear Rate in cgs is",round(Vyz_cgs,4),"1/s"
print '(b)Shear stress in fps %10.3e'%(Tyz_fps),"lbf/ft2"
print "   Shear Rate in fps is",round(Vyz_cgs,4),"1/s"
print "(c)Shear stress in SI is",round(Tyz_SI,4),"N/m2"
print "   Shear Rate in SI is",round(Vyz_cgs,4),"1/s"
#Reynolds Number in a pipe 
from math import pi
# Variable declaration
F = 10.                 #The flowrate of water (gal/min)
d = 2.067               #The inner diameter of pipe (in)
spg = 0.996             #specific gravity,M/M
d_SI = 0.0525           #Diameter in SI units (m)
mu = 0.8007             #Viscosity, cp
#Calculation English units
F_fps = F*(1./7.481)*(1./60)
d_fps = d/12
A = pi*d_fps**2/4
V = F_fps/A
mu_fps = mu*6.7197e-4    #convert in lbm/ft.s
rho = spg*62.43
Nre_fps = d_fps*V*rho/(mu_fps)
#Calculation SIh units
d_SI = d*2.54e-2
V_SI = V/3.2808
mu_SI = mu*1e-3         #convert in Pa.s
rho = spg*1000
Nre_SI = d_SI*V_SI*rho/(mu_SI)
#Result 
print 'The reynolds number in fps units is %6.3e'%(Nre_fps)
print 'The reynolds number in SI units is  %6.3e'%(Nre_SI)
print 'The answers are correct because book uses rounded numbers'
#Flow of a Crude Oil and Mass Balance
#Variable declaration
rho = 892.              #Density of Crude Oil kh/m3
F = 1.388e-3            #Total Volumetric flow rate m3/s
d1 = 2.067              #Diamete of pipe 1 in inches
d3 = 1.610              #Diamete of pipe 3 in inches
#Calculations
intom = 0.0254                 #conversion factor for in to meter
A1 = pi*(d1*intom)**2/4        #Area of pipe 1
A3 = pi*(d3*intom)**2/4        #Area of pipe 3
m1 = F*rho                     #Mass flow rate of Crude Oil through pipe 1
m3 = m1/2                      #Mass flow rate of Crude Oil through pipe 3
v1 = m1/(rho*A1)               #velocity of Crude Oil through pipe 1
v3 = m3/(rho*A3)               #velocity of Crude Oil through pipe 3
G1 = m1/A1                     #Mass flux of cride oil through pipe 1
#Result
print "(a) Mass flow rate of Crude Oil through pipe 1:",round(m1,3), "kg/s"
print "    Mass flow rate through pipe 3:",round(m3,3), "kg/s"
print "(b) Velocity of Crude Oil through pipe 1:",round(v1,3), "m/s"
print "    Velocity of Crude Oil through pipe 3:",round(v3,3), "m/s"
print "(c) Mass velocity of Crude Oil through pipe 1:",round(G1), "m/s"
#Energy Balance on Steam boiler
#Variable Declaration
Twin = 18.33         #Inlet temperature of water to boiler, degC
Pwin = 137.9         #Inlet pressure of water to boiler, kPa
Vwin = 1.52          #Inlet temperature of water to boiler, m/s
Hwin = 0.            #Water Inlet height, m
Hsout = 15.2         #Height of steam outlet above water inlet, m 
Psout = 137.9        #Outlet pressure of steam to boiler, kPa
Tsout = 148.9        #Inlet temperature of water to boiler, degC
Vsout = 9.14         #Inlet temperature of water to boiler, m/s
H1 = 76.97           #Entalpy of Incomming water to boiler, kJ/kg
H2 = 2771.4          #Entalpy of steam leaving boiler, kJ/kg
g = 9.80665          #Gravitational acceleration, m/s2
#Calcualtion
# Heat Added = Del PE + Del KE + Del Enthalpy
Q = (Hsout-Hwin)*g + (Vsout**2-Vwin**2)/2 + (H2-H1)*1000
#Result 
print 'Heat Added per kg of water %5.4e'%(Q),"J/kg"
print 'The answers are correct because of book uses rounded of numbers'
#Energy Balance on a flow system with a Pump
#Variable Declaration
F = 0.567            #Water pumped from the tank (m3/min.)
E1 = 7450.           #Energy supplied by the pump (W)
E2 = 1408000         #Energy lost in heat exchanger (W)
Rho = 968.5          #Density of water at 85 deg C (kg/m3)
Z1 = 0.              #Height of first second tank (m)
Z2 = 20.             #Height of second tank (m)
H1 = 355900          #Enthalpy of steam (kJ/kg)
delV = 0.            #The kintic energy changes
g = 9.80665          #Gravitational acceleration (m/s2)
#Calcualtion
m1 = F*Rho/60        #Water pumped from the tank (kg/s)
Ws = -E1/m1 
Q = -E2/m1           
    # H2 - H1 + 1/2*delV + g(Z2 - Z1) = Q - Ws
    # H2 = Q - Ws + H1 - 1/2*delV - g(Z2 - Z1)
H2 = Q - Ws + H1 - 0.5*delV - g*(Z2 - Z1)
#Result 
print 'Heat Added per kg of water %5.3e'%(H2), "J/kg"
print "The value of heat added corresponds to  48.41 °C"
#Energy Balance in flow Calorimeter  
#Variable Declaration
F = 0.3964           #Flowrate of water entering the calorimeter  (kg/min)
E1 = 19.630          #Heat added to the water for complete vaporization  (kW)
Z1 = 0.              #Height of first second tank (m)
Z2 = 0.              #Height of second tank (m)
delV = 0.            #The kintic energy changes
g = 9.80665          #Gravitational acceleration  (m/s2)
Ws = 0.              #Shaft work
H1 = 0               #Initial Enthalpy
P1 = 150.            #Pressure at point 2 (kPa)
#Calcualtion
M1 = F/60            #Flowrate of water entering the calorimeter  (kg/s)
    # Q = E1/M1
Q = E1/M1
    # H2 - H1 + 0.5*delV + g(Z2 - Z1) = Q - Ws
    # H2 = Q - Ws + H1 - 1/2*delV - g(Z2 - Z1)
H2 = Q - Ws + H1 - 0.5*delV - g*(Z2 - Z1)
#Result 
print "Heat Added per kg of water",round(H2), "J/kg"
print "The corresponding value of enthalpy from steam table is 2972.7 kJ/kg"
#Mechanical-Energy Balance on Pumping System
#Variable Declaration
P1 = 68900.            #The entrance pressure of fluid (N/m2)
E = 155.4              #Energy supplied to the fluid (J/kg)
P2 = 137800.           #The exit pressure of fluid (N/m2) 
Nre = 4000             #Reynolds number 
Rho_H2O = 998.         #Density of water (kg/m3)
z1 = 0                 #Height of entrance pipe (m)
z2 = 3.05              #Height of exit pipe (m)
alpha = 1              #For turbulent flow value of alpha
Ws = -155.4            #Mechanical energy added to the fluid (J/kg)
g = 9.806              #Gravitational accleration (m/s2)
#Calcualtion
    #(V1 - V2)/2*alpha = 0
    #F = -Ws + 0 + g*(z1-z2) + (P1 - P2)/Rho_H2O
F = -Ws + 0 + g*(z1-z2) + (P1 - P2)/Rho_H2O
#Result 
print "The frictional losses calculated are ",round(F,1), "J/kg"
#Pump Horsepower in Flow System
#Variable Declaration
F = 69.1              #Volume of liquid drawn by the pump (gal/min)
F_fps = F/(60*7.481)  #Volume of liquid drawn bby the pump (ft3/s)
Rho = 114.8           #Density of liquid (lbm/ft3)
A1 = 0.05134          #Cross sectional area of suction  pipe (in2)
A2 = 0.0233           #Cross sectional area of discharge pipe (in2)
z1 = 0.               #Height of suction line (m)
z2 = 50.              #Height of discharge pipe (m)
f = 10.0              #Frictional losses (ft.lbf/lbm)
n = 0.65              #Efficiency of the pump 
g = 32.174            #Gravitational accleration (m/s2)
gc = 32.174           #Conversion factor for mks to fps 
v1 = 0.               #Velocity of liquid in storage feed tank (m/s)
alpha = 1.            #Since the flow is turbulent 
fc = 550.             #Conversion factor from fps to horse power
z3 = 0.           
z4 = 0.
ga = 144.             #Conversion factor for area from ft2 to in2
#Calcualtion
    #v2 = flowrate/cross sectional area
v2 = F_fps/A2
    #(P1-P2)/Rho = 0    
    #Ws = (z1-z2)*g/gc + (v1*v1 - v2*v2)/(2*gc) + 0 + f       
Ws = (z1-z2)*g/gc + (v1*v1 - v2*v2)/(2*gc) + 0 - f
Wp = -Ws/n
m = F_fps*Rho 
P = m*Wp/fc
v3 = F_fps/A1
v4 = F_fps/A2 
    #delP = P4 - P3    
delP = Rho*((z4-z3)*g/gc +(v3*v3 - v4*v4)/(2*gc) - Ws - 0. )/ga
#Result  
print "Power required by the pump ",round(P,0), "hp"
print "Pressure developed is ",round(delP,0),"lbf/in2"
#Momentum Balance for Horizontal Nozzle 
from math import pi
#Variable Declaration
F = 0.03154             #Flowrate of water (m3/s)
d1 = 0.0635             #Dia of pipe at section 1 (m)
d2 = 0.0286             #Dia of pipe at section 2 (m)
Rho_H2O = 1000.         #Density of water (kg/m3)
P2 = 0.                 #Pressure (N/m2)
#Calcualtion
m = F*Rho_H2O
A1 = pi*d1**2/4
A2 = pi*d2**2/4
v1 = F/A1
v2 = F/A2
P1 = Rho_H2O*((v2**2 - v1**2)/2 + P2/Rho_H2O)
Rx = m*(v2-v1) + P2*A2 - P1*A1
#Result
print "The resultant force on the nozzle is",round(Rx,1), "N"
print 'The answer is correct because of book uses rounded numbers'
#Force of Free Jet on a Curved,Fixed Vane
from math import pi
#Variable Declaration
V = 30.5            #Velocity of jet stream of water (m/s)
Rho_H2O = 1000.     #Density of water (kg/m3)
d = 2.54e-2         #Diameter of pipe, m
alpha = 60          #Angle made by exit stream, deg
A = pi*d**2/4
m = V*A*Rho_H2O 
#Calculation
#Rx is the x component of the force
    #Rx = m*V*(cos60 - 1)
Rx = m*V*(cos(alpha*pi/180)-1)
#Ry is the y component of the force
    #Ry = m*V*(sin60)
Ry = m*V*sin(alpha*pi/180)
#Result
print "The force in x direction is ",round(-Rx,1),"N"
print "The force in y direction is ",round(-Ry,1),"N"
print 'The answers are correct because of book uses rounded numbers'
#Falling Film Velocity and Thickness
#Variable Declaration
d = 0.0017          #Thickness of the film (m)
Rho_oil = 820       #Density of oil (kg/m3)
mu = 0.20            #Viscosity of oil (Pa.s)
g = 9.806           #Gravitational acceleration (m/s2)
#Calculation
Gama = Rho_oil*Rho_oil*d**3*g/(3*mu)
Nre = 4*Gama/mu
Vz = Rho_oil*g*d**2/(3*mu)
#Result
print "The mass flowrate per unit width of the wall",round(Gama,6),"kg/s.m"
print "Reynolds number is",round(Nre,3)
print "The average velocity is",round(Vz,6),"m/s"
#Metering of Small Liquid flows 
from math import pi
#Variable Declaration
d = 0.00222          #Diameter of capillary (m)
l = 0.317            #Length of capillary (m)
Rho = 875            #Density of liquid (kg/m3)
mu = 0.00113         #Viscosity of liquid (pa.s)
h = 0.0655           #Heightof water (m)
Rho_H2O = 996        #Density of water (kg/m3)
g = 9.80665          #Gravitational acceleration (m/s2)
#Calculation
delP = h*Rho_H2O*g
    #v = delP*d*d/(32*mu*l)
    #Flow assumed to be laminar
v = delP*d**2/(32*mu*l)
F = v*pi*d**2/(4)
    #Checking whether the flow is laminar
Nre = d*v*Rho/(mu)
#Result
print 'The pressure drop over capillary is %3.0f N/m2'%(delP)
print 'The velocity through capillary is %4.3f m/s'%(v)
print 'The mass flowrate through the capillary is %5.3e m3/s'%(F)
print 'Reynolds number is %3.0f, Hence flow is laminar'%(Nre)
print 'The answers are correct because of book uses rounded numbers'
#Use of Friction Factor in Laminar Flow  
 
#Variable Declaration
d = 0.00222          #Diameter of capillary (m)
l = 0.317            #Length of capillary (m)
V = 0.275            #Velocity of liquid (m/s)
Rho = 875            #Density of liquid (kg/m3)
mu = 0.00113         #Viscosity of liquid (pa.s)
h = 0.0655           #Heightof water (m)
Rho_H2O = 996        #Density of water (kg/m3)
g = 9.80665          #Gravitational acceleration (m/s2)
#Calculation
Nre = d*V*Rho/(mu)
    #f = 16/Nre
f = 16/Nre
    #delp = 4*f*Rho*l*V*V/(2*D)
delp = 4*f*Rho*l*V**2/(2*d)
#Result
print 'Reynolds number is %3.0f, Hence flow is laminar'%(Nre)
print 'friction factor is %0.4f'%(f)
print 'The pressure drop through capillary is %3.0f N/m2'%(delp)
#Use of Friction Factor in Turbulent Flow  
#Variable Declaration
d = 0.0525           #Diameter of capillary (m)
l = 36.6             #Length of capillary (m)
V = 4.57             #Velocity of liquid (m/s)
Rho = 801.           #Density of liquid (kg/m3)
mu = 4.46            #Viscosity of liquid (cp)
g = 9.806            #Gravitational acceleration (m/s2)
e = 0.000046         #Equvivalent Roughness (m)
f = 0.0060           #Friction factor 
#Calculation
mu = mu*1e-3         #Convert Viscosity to  (kg/m.s)
Nre = d*V*Rho/(mu)
    #delp = 4*f*Rho*l*V*V/(2*D)
delp = 4*f*Rho*l*V**2/(2*d)
    #F = 4*f*l*V*V/(2*d)
F = 4*f*l*V**2/(2*d)
#Result
print 'Reynolds number is %3.0f, Hence flow is turbulent'%(Nre)
print 'friction factor from chart is %0.4f'%(f)
print "The friction loss is %3.1f J/kg" %(F)
print 'The answers are correct because of book uses rounded numbers, and varified using calculator'
#Trial and Error Solution to Calculate Pipe Diameter
from scipy.optimize import root
from sympy import *
from math import pi, log
#Variable Declaration
l = 305.              #Length of steel pipe (m)
mu = 1.55e-3          #Viscosity of fluid (kg/m.s)
Q = 150.0             #Rate of flow of fluid (gal/min)
g = 9.80665           #Gravitational acceleration, m/s2
ep = 4.6e-5           #Roughness of commercial steel pipe, m
rho = 1000.           #Density of water, kg/m3
z = 6.1               #Head available, m
#Calculation
Qsi = Q*(1./7.481)*(1./60)*0.02831      #Multipliers in equation are conversion factors for conversion to SI Units
Ff = z*g
er = 12.
x = Symbol('x')
D = 0.089
while er >= 0.01:
    epbyd = ep/D
    A = pi*D**2/4.
    u = Qsi/A 
    Re = D*u*rho/mu
    sol = solve(1/sqrt(x)+4*(log(epbyd/3.7,10)+1.255/(Re*sqrt(x))),x)
    f = sol[0]
    d = 4*f*l*u**2/(2*Ff)
    er = abs((D-d)/D)
    if er > 0.01:
        print 'Error %6.5f and diameter calculated %6.5f'%(er, d)
        D = float(raw_input("Not Converged...:Enter the value of guess diameter  "))
#Result
print '\nConverged...Diameter of the pipe is %4.3f m or %4.3f in'%(D,D*12./0.3048)
print 'The value calculated is accurate than answer in book\nbecause book reads the friction factor from chart whereas\nequations for friction factor are used in this'
#Flow of Gas in Line and Pressure Drop 
#Variable Declaration
d = 0.010             #Inside diameter of tube (m)
G = 9.                #Rate of feed (kg/s.m2)
l = 200.              #Length of Tube (m)
mu = 0.0000177        #Viscosity of liquid (Pa.s)
R = 8314.3            #Gas constant (J/kg.mol.K)
P1 = 202650.          #Pressure at entrance (Pa)
f = 0.0090            #Friction factor 
T1 = 25.              #Temperature at inlet (degC)
T2 = 298.15           #Temperature at outlet (degC)
M = 28.02             #Molecular weight of 1 kmol N2  (kmol)
#Calculation
Nre = d*G/mu
f = 0.079*Nre**(-0.25)   #for smooth pipes
P2 = sqrt(P1**(2) - 4*f*l*G**2*R*T2/(d*M))
   
#Result
print "for Reynolds Number ", round(Nre,2), "friction factor for smooth pipe is", round(f,6)
print 'The pressure at outlet %6.4e Pa, '%(P2)
print 'The answer is correct than the book because of book uses rounded numbers'
#Friction Losses and mechanical energy balance
#Variable Declaration
Q = 0.223                 #Desired flowrate of water (ft3/s)
Rho_fps = 60.52           #Density of water in fps units (lbm/m3)
u_fps = 0.000233          #Viscosity of water in fps units (lbm/ft.s)
D3 = 0.3353               #Diameter of the third pipe (ft)
D4 = 0.1722               #Diameter of the third pipe (ft)
A3 = 0.0884               #Area of third pipe (ft2)
A4 = 0.0233               #Area of third pipe (ft2)
g_fps = 32.174            #Gravitational acceleration in fps units (ft/s2)
e = 0.00015               #Roughness factor (ft)
f2 = 0.0047               #Fanning friction factor for 4-in pipe
f5 = 0.0048               #Fanning friction factor for 2-in pipe
delL_4 = 20.              #Length of 4-in pipe (ft)
delL_2 = 185.             #Length of 2-in pipe (ft)
delP = 0.                 #Since pressure at both end is atmspheric pressure 
Kf = 0.75
alpha = 1.
Ws = 0.                   #Shaft work (j)
#Calculation
V1 = 0.
V3 = Q/A3
V4 = Q/A4
    #
    #(1) Contraction losses at tank exit
#Kc = 0.55*(1-A3/A1)
Kc1 = 0.55
hc1 = Kc1*V3**2/(2*g_fps)
    #(2) Friction in 4-in pipe 
Nre2 = D3*V3*Rho_fps/(u_fps)
F2 = 4*f2*delL_4*V3**2/(D3*2*g_fps)
    #(3) Friction in 4-in elbow
hf = Kf*V3**2/(2*g_fps)
    #(4) Contraction loss from 4-in to 2-in pipe 
Kc4 = 0.55*(1-A4/A3)
hc4 = Kc4*V4**2/(2*g_fps)
    #(5)Friction in 2-in pipe 
Nre5 = D4*V4*Rho_fps/u_fps
F5 = 4*f5*delL_2*V4**2/(D4*2*g_fps)
    #(6) Friction in 2-in elbows
hc6 = 2*Kf*V4**2/(2*g_fps)
    #F = hc1 + F2 + hf + hc4 + F5 + hc6
F = hc1 + F2 + hf + hc4 + F5 + hc6
    #delH*g_fps = delP/Rho_fps + (V4**2 - V1**2)/(2*alpha*g_fps) + Ws + F
    #delH = (delP/Rho_fps + (V4**2 - V1**2)/(2*alpha*g_fps) + Ws + F)/g_fps
delH = (delP/Rho_fps + (V4**2 - V1**2)/(2*alpha*g_fps) + Ws + F) 
#Result
print "The height of water level above the discharge outlet ",round(delH,2),"ft"
print 'The answer is correct because of book uses rounded numbers'
#Friction Losses with pump in mechanical energy balance
#Variable Declaration
Q = 0.005                 #Desired flowrate of water (m3/s)
Rho = 998.2               #Density of water in fps units (kg/m3)
u = 0.001005              #Viscosity of water in fps units (Pa/s)
D1 = 0.1023               #Diameter of the third pipe (m)
A1 = 0.008291             #Area of pipe (m2)
g = 9.806                 #Gravitational acceleration in fps units (ft/s2)
e = 0.000046              #Roughness factor (ft)
f2 = 0.0051               #Fanning friction factor for 4-in pipe
z1 = 0.                   #Height of storage tank (m)
z2 = 15.                  #Height of elevated tank (m)
delL = 170.               #Length of 4-in pipe (ft)
delL_2 = 185.             #Length of 2-in pipe (ft)
delP = 0.                 #Since pressure at both end is atmospheric pressure 
delV2 = 0.
Kf = 0.75
Kex = 1.             
alpha = 1.
n = .65                   #Efficiency of the pump
#Calculation
V1 = Q/A1
Nre = D1*V1*Rho/(u)
    
    #(1) Contraction losses at tank exit
#Kc = 0.55*(1-A3/A1)
Kc1 = 0.55
hc1 = Kc1*V1**2/(2*alpha)
    #(2) Friction in straight pipe 
F2 = 4*f2*delL*V1**2/(D1*2)
    #(3) Friction in the two elbows
hf = 2*V1**2/(2)
    #(4) Expansion loss at tank entrance
hc4 = Kex*V1**2/(2)
    #F = hc1 + F2 + hf + hc4 
F = hc1 + F2 + hf + hc4
    #1*(delV2)/(2*alpha) + g*(z2 - z1) +delP/Rho + F + Ws = 0.
Ws = -(1*(delV2)/(2*alpha) + g*(z2 - z1) +delP/Rho + F)
    #m = Q*Rho
m = Q*Rho
    #Ws = -n*Wp
Wp = Ws/-n
    #P = m*Wp
P = m*Wp/1000
#Result
print "The power required by the pump is ",round(P,3),"kW"
#Entry Length for a Fluid in a Pipe 
#Variable Declaration
d = 0.010                #Diameter of tube (m)
V = 0.10                 #Velocity of water (m/s)
mu = 0.001005             #Viscosity of water (Pa.s)
Rho = 998.2              #Density of water (kg/m3)
#Calculation
Nre = d*V*Rho/mu
    #For laminar flow 
    #Le = 0.0575*Nre
Le_l = 0.0575*Nre*d
Le_t = 50*d  
#Result
print "Reynolds Number is",round(Nre,1)
print "For laminar flow the entry length is ",round(Le_l,3),"m"
print "For turbulent flow the entry length is ",round(Le_t,4),"m"
#Compressible Flow of a gas in a pipe line
from math import log, pi
#Variable Declaration
d = 1.016                #Diameter of pipe (m)
G = 2.077                #The molar flow rate of the gas (kg mol/s)
mu = 1.04e-5             #Viscosity of methane at 288.8 k (Pa.s)   
p2 = 170.3e3             #The pressure at the outlet (Pa)
L = 1.609e5              #The length of the pipe (m) 
R = 8314.34              #Gas constant (N.m/kg.mol.K)
T = 288.8                #Temperature of methane gas (K)
M = 16.                  #Molecular weight of methane 
epsilon = 4.6e-5         #Pipe roughness
#Calculation
A = pi*d**2/4
Gm = G*M/A
Nre = d*Gm/mu
f = 0.0027        #from friction factor chart
     #P1**2 - P2**2 = (4*f*L*(G_mass**2)*R*T)/(D*M) + (2*(G**2)*R*T*log(p1/P2)/16)
     #P1 = sqrt(P2**2 + (4*f*L*(G_mass**2)*R*T)/(D*M) + (2*(G**2)*R*T*log(p1/P2)/16))
er = 1.2
p1 = 500.e3
while er>0.001:
    p1c = sqrt(p2**2 + 4*f*L*Gm**2*R*T/(d*M) + (2*Gm**2*R*T*log(p1/p2)/M))
    er = abs(p1-p1c)/p1c
    p1 = p1c
#Result
print 'The final pressure is %10.4e Pa.'%(p1)
print 'The answers are correct because of book uses rounded of numbers'
#Maximum Flow for Compressible Flow of a Gas
#Variable Declaration
R = 8314.              #Gas constant ()
T = 288.8              #Temperature of fluid (K)
M = 16.
P2 = 170300.           #Pressure at discharge tube (Pa)
G = 41.          
#Calculation
Vmax = sqrt(R*T/M)
V2 = R*T*G/(P2*M)
#Result
print "The maximum velocity in the tube  is ",round(Vmax,1),"m/s"
print "The actual velocity is ",round(V2,2),"m/s"