% Cross Flow Calculator or "cfc"
%
% By: Ashby McCort               Tyler Jeppesen 
%     amccort@gmail.com          phoenixflame9@yahoo.com 
%  
% The following code is designed to find the pipe length necessary to heat 
% or cool a fluid flowing through a circular pipe with another fluid in
% cross flow acoss the pipe. 
%
% 
% List of Required Initial Conditions:
% 
% Exterior Fluid(1)
% Interior Fluid(2)
% Average Enterance Temperature of Fluid2
% Desired Exit Temperature of Fluid2
% Estimate of the Average Pipe Surface Temperature
% Average Temperature of Fluid1
% Mass Flow Rate of Fluid2
% Pipe Outter and Inner Diameters
% Averager Velocity of fluid flow over the pipe
% 
% Other necessary thermal properties of the two fluids required for 
% calculations will be determined using the FLPROP Calculator courtesy of 
% Tom Aimes and Mark Cutler (see below for explanation of 'flprop' 
% requirements and limitations).
% 
% 
% Constraints of these Calculations:
%
% Assumptions: Pipe has circular crossection, Pipe is Smooth, Flow Inside
% pipe is Steady, Fluid inside pipe is incompressible, Heat Convection is
% Constant across the pipe, Neglect Natural Connective Effects, Neglect
% Radioactive Effects.
% 
% The situation is for one fluid flowing inside pipe and one cross flow
% fluid outside pipe. The pipe itself can have a nominal thickness (or 
% be considered thin just type in the same value for the inner and outter
% diameters) and a pipe material with a known thermal conductivity.
% 
% 
% Explanation of Thermal Property Calculations from the function "flprop":
% 
% FLPROP Interpolates and returns thermophysical properties 
%        of various fluids
%
% By: Tom Ames                   Mark Cutler
%    thomaslames@gmail.com       markjcutler@gmail.com
% 
% flprops calculates and returns various fluid properties using the syntax:
% [returned properties] = flprops(fluid,temp,property list)
% where fluid is a character array representing the desired fluid, temp is
% the temperatue at which the user would like to get the property values,
% and property list is a comma seperated list of the properties to be
% returned.  The properties are returned as a [1xn] matrix where n 
% represents the number of properties requested.  The values in the matrix 
% will be in the same order that the user entered them.  The following 
% fluid types are supported:
%
%       Gases at atmospheric pressure:
%           Air:                'air'
%           Ammonia:            'nh3'
%           Carbon dioxide:     'co2'
%           Carbon monoxide:    'co'
%           Helium:             'he'
%           Hydrogen:           'h2'
%           Nitrogen:           'n2'
%           Oxygen:             'o2'
%           Water Vapor:        'steam'
%
%       Saturated liquids:
%           Unused Engine Oil:  'oil'
%           Ethylene Glycol:    'eth_gly'
%           Glycerin:           'glyc'
%           Refrigerant-132a:   'r132a'
%           Refrigerant-22:     'r22'
%           Mercury:            'hg'
%           Water:              'water'
%
%The properties supported for both gases at STP and saturated fluids are:
%           density:                'rho'
%           specific heat:          'cp'
%           viscosity:              'mu'
%           kinematic viscosity:    'nu'
%           thermal conductivity:   'k'
%           thermal diffusivity:    'alpha'
%           Prandlt number:         'Pr'
%           expansion coefficient:  'beta'
%
% Values for saturated liquid and vapor mixtures of water are also 
% available.
% 
% To access these values the following syntax is used:
%      [returned properties]=flprops('water',temp,property list,options)
% where options can be of any of the following forms:
%
%           'satvap': returns property values for a satured vapor at the
%               user defined temperature.
%           'satliq': returns property values for a satured liquid at the
%               user defined temperature.
%           'mixture',option2: returns property values for a satured
%               liquid/vapor mixture at the user defined temperature.  The
%               quality of the mixture is calculated using any one of the
%               following user defined values for a property.  Only one
%               should be used, and and it should follow the format shown
%               below:
%                   'quality',value: user knows quality
%                   'v',value: user knows specific volume
%                   'cp',value: user knows specific heat
%                   'mu',value: user knows viscosity
%                   'k',value: user knows thermal conductivity
%                   'Pr',value: user knows Prandlt number
%
%Supported properties are for liquid/vapor mixtures of water are:
%           density:                'rho'
%           specific heat:          'cp'
%           viscosity:              'mu'
%           kinematic viscosity:    'nu'
%           thermal conductivity:   'k'
%           thermal diffusivity:    'alpha'
%           Prandlt number:         'Pr'
%           pressure:               'p'
%           surface tension:        'sigma'
%           heat of vaporizate:     'hfg'
%
%Properties have the following units:
%       Temperature:                    K
%       Density 'rho':                  kg/m^3
%       Specific Heat 'cp':             J/(kg*K)
%       Dynamic Viscosity 'mu':         N*s/m^2
%       Kinematic Viscosity 'nu':       m^2/s
%       Thermal Conductivity 'k':       W/(m*K)
%       Thermal Diffusivity 'alpha':    m^2/s
%       Prandlt number 'Pr':            unitless
%       Pressure 'p':                   Pa
%       Surface tension 'sigma':        N/m
%       Heat of Vaporization 'hfg':     J/kg
%       Expansion Coefficient 'beta':   1/K
% 
% 
% All Heat Tranfer Principles used in these calculations can be found in
% 
% "Fundamental Heat and Mass Transfer, 6th Edition."  
%  
% Call in Initial Conditions/Begin Calcuations of Exterior Fluids:

clc;
clear all;

fluid1 = input('Exterior fluid(see "help cfc" for proper abreviations): ','s');

fluid2 = input('Interior fluid(see "help cfc" for proper abreviations): ','s');

Tmi = input('Fluid flow enterance temperature(degrees C): ');

Tmo = input('Desired fluid flow exiting temperature(degrees C): ');

Ts = input('Guess of Average Surface temperature(degrees C): ');

Tinf = input('Temperature of the Exterior fluid(degrees C): ');

% Calculation of film temperature to find Thermal Properties
Tf = (Ts+Tinf)/2+273.15;

Tm = (Tmi+Tmo)/2+273.15;

% Finding thermal properties using flprops:
[nu Pr k] = flprop(fluid1,Tf,'nu','Pr','k');

[mu2 nu2 Pr2 k2 cp2] = flprop(fluid2,Tm,'mu','nu','Pr','k','cp');

mdot = input('Interior Mass Flow Rate(kg/s): ');

Do = input('Pipe Outter Diameter(m): ');

ro = Do/2;

Di = input('Pipe Inner Diameter(m): ');

ri = Di/2;

if Do/Di==1

    kp = 1;
else
    kp = input('Thermal Conductivity of pipe wall material: ');
end

V = input('Average velocity of fluid flow outside of the pipe(m/s): ');

fluid1

disp('nu ='); disp(nu);

disp('Pr ='); disp(Pr);

disp('k ='); disp(k);


fluid2

disp('mu2 ='); disp(mu2);

disp('nu2 ='); disp(nu2);

disp('Pr2 ='); disp(Pr2);

disp('k2 ='); disp(k2);

disp('cp2 ='); disp(cp2);


%Exterior Flow Calculations:
ReD = (V*Do)/nu;

disp('ReD');

disp(ReD);

%Calculating Nusselt# using Churchill Method:
Nu = 0.3+((0.62*ReD^0.5*Pr^(1/3))/((1+(0.4/Pr)^(2/3))^(0.25)))*(1+(ReD/282000)^(5/8))^(4/5);

disp('Nu ='); disp(Nu);

h = Nu*(k/Do);

disp('h ='); disp(h);


%Calculations of Interior Fluid Flow:
Rei = (4*mdot)/(pi*mu2*Di);

disp('Rei =');
disp(Rei);

%Laminar Vs. Turbulent
if Rei < 2300
    Nui = 4.36;
    
else
    %Calculating the Nusselt# using Gnielinski Method:
    f = 1/(0.79*log(Rei)-1.64)^2;
    Nui = ((f/8)*(Rei-1000)*Pr2)/(1+12.7*(f/8)^0.5*(Pr2^(2/3)-1));
end

disp('Nui =');
disp(Nui);

hi = Nui*(k2/Do);

disp('hi ='); disp(hi);


%Final Calculation steps for length:
q = mdot*cp2*(Tmi-Tmo);

U = 1/((1/h)+(ri/kp)*log(ro/ri)+(ri/ro)*(1/hi));

disp('U =');

disp(U);

deltaTlm = ((Tmi-Tinf)-(Tmo-Tinf))/(log((Tmi-Tinf)/(Tmo-Tinf)));

disp('deltaTlm');

disp(deltaTlm);

x = q/(U*pi*Di*deltaTlm);

disp('Determined Pipe Length(m):'); disp(x);

Tscalculated = Tm - q*(1/(hi*x*pi*Di) + 1/(2*pi*kp*x)*log((Do/2)/(Di/2)));

Tscalculated = Tscalculated - 273.15;

disp('Calculated Average Surface Temperature(degrees C):');

disp(Tscalculated);





