%Aquires optimal Diameter and velocity (Average 7ft/s) for a given flowrate
clear, clc, close all %THIS TEMPLATE IS BASED ON SEGMENT B-C
syms D v2  
%FLOW RATE MAY BRANCH OFF SO YOU MAY HAVE TO EVALUATE THE SAME POINT TWO
%TIMES TO ATTAIN TWO DIFFERENT INLET PRESSURES FOR NEXT 
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IdealFlow = 320;  %THIS VALUE CAN BE CHANGED               %Gpm
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Flowrate(D,v2) = vpa(simplify(v2/(0.00222801./(pi*(D^2)/4))),2);      %Gpm
ezplot(Flowrate == IdealFlow, [0 2 3 10]); grid on; grid minor;           %optimizes velocity and diameter from 3-10 ft/s and 0-2 ft diameter
hold on; yline(7); xlabel('\bf Diameter in ft'); ylabel('\bf velocity ft/s') %Plots a line at average velocity. Record velocity and Diameter and use for next part
%% Calculate Exit Pressure drop: Full Flow
%Calculates Pressure at Exit Point. This is the same format as above but does not include Pressure term from the lots.
clear, clc, close all                                                        %Everything is in STANDARD ENGLISH UNITS excpet for pressure (psig)
dens = 62.4183; u = 9.52564*10^-4; gc = 32.174; e = 0.00015; %Given, dens - density, u - dynamic viscosity,  gc -gravitaty, e -absolute roughness
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%ANYTHING HERE CAN BE AND SHOULD BE CHANGED
 D = .360113;                  %ft                                %Aquire Inner Diamter of pipe from Step 1
 v1 = 7;                        %ft/s                                %aquire velocity from Step 1
 InletPressure = 70       %psig                               %Total Pressure based on full flow. This is where ExitPressure = NewPressure the next time around
 n = 5;                          %# of lots                         %number of lots
 RequiredPressure =70; %psig                               %This is the pressure required per lot
 z = 0;                           %ft                                   %Change in elevation
 K = 6*0.4;                   %Dimensionless               %Friction coefficients
 L = 11359;                  %ft                                  %Length of Pipe
 %---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 PumpPressure = 0;                                              %This allows us to find a pressure Difference and solve for PumpPressure to minimize DELP
 PressureLots = n*RequiredPressure;                    %
 Re1 = dens*v1*D/u;                                        %Solves For Reynold's number
 A1 = (2.457*log((1/((7/Re1)^.9+0.27*(e/D)))))^16;%Churchhill Coefficients
 B1 = (37530/Re1)^16;
 Ff1= 2*((8/Re1)^12 + 1/((A1+B1)^1.5))^(1/12);    %Fanning Friction
 Flowrate = v1/(0.00222801./(pi*(D^2)/4))             %gpm
 ExitPressure = (InletPressure + PumpPressure - PressureLots) - dens*(z+v1^2/gc*(2*Ff1*L/D + K/2))/144;  %In Psig
 TotalWorkPump = abs(PumpPressure)*0.0005834*Flowrate;                                                                      %HHP
 DELP = abs(InletPressure - ExitPressure);                                                                                                %Psig
 PumpPressure = PumpPressure + DELP                                                                                                   %Psig
 Flowrate = v1/(0.00222801./(pi*(D^2)/4));                                                                                                  %gpm
 ExitPressure = (InletPressure + PumpPressure - PressureLots) - dens*(z+v1^2/gc*(2*Ff1*L/D + K/2))/144   %In Psig
 TotalWorkPump = abs(PumpPressure)*0.0005834*Flowrate;                                                                      %HHP
 DELP = abs(InletPressure - ExitPressure)                                                                                                 %Psig
 HHP =  0.0005834*Flowrate*PumpPressure         %This is Horspower for pump                                         %HP
 %% Calculate Exit Pressure: Static
%Calculates Pressure at Exit Point. This is the same format as above but does not include Pressure term from the lots.
clear, clc, close all                                                        %Everything is in STANDARD ENGLISH UNITS excpet for pressure (psig)
dens = 62.4183; u = 9.52564*10^-4; gc = 32.174; e = 0.00015; %Given, dens - density, u - dynamic viscosity,  gc -gravitaty, e -absolute roughness
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%HERE YOU CANNOT ADD OR REMOVE PUMPS BASED ON PREVIOUS SECTION. CAN STILL MANIPULATE PRESSURE
%ANYTHING HERE CAN BE CHANGED
 D = .360113;               %ft                                  %Aquire Inner Diamter of pipe from Step 1
 v1 = 7;                        %ft/s                                %aquire velocity from Step 1
 InletPressure = 70       %psig                               %Total Pressure based on full flow. This is where ExitPressure = NewPressure the next time around
 z = 0;                          %ft                                  %Change in elevation
 K = 6*0.4;                   %Dimensionless               %Friction coefficients
 L = 11359;                  %ft                                  %Length of Pipe
 %---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 PumpPressure = 0;                                              %This allows us to find a pressure Difference and solve for PumpPressure to minimize DELP
 Re1 = dens*v1*D/u;                                         %Solves For Reynold's number
 A1 = (2.457*log((1/((7/Re1)^.9+0.27*(e/D)))))^16;%Churchhill Coefficients
 B1 = (37530/Re1)^16;
 Ff1= 2*((8/Re1)^12 + 1/((A1+B1)^1.5))^(1/12);    %Fanning Friction
 Flowrate = v1/(0.00222801./(pi*(D^2)/4))             %gpm
 ExitPressure = (InletPressure + PumpPressure) - dens*(z+v1^2/gc*(2*Ff1*L/D + K/2))/144; %In Psig
 TotalWorkPump = abs(PumpPressure)*0.0005834*Flowrate;                                               %HHP
 DELP = abs(InletPressure - ExitPressure);                                                                         %Psig
 PumpPressure = PumpPressure + DELP                                                                            %Psig
 Flowrate = v1/(0.00222801./(pi*(D^2)/4));                                                                           %gpm
 ExitPressure = (InletPressure + PumpPressure) - dens*(z+v1^2/gc*(2*Ff1*L/D + K/2))/144   %In Psig
 TotalWorkPump = abs(PumpPressure)*0.0005834*Flowrate;                                               %HHP
 DELP = abs(InletPressure - ExitPressure)                                                                          %Psig
  HHP =  0.0005834*Flowrate*PumpPressure        %This is Horspower for pump                  %HP