%clear all
%clc


function[] = GUI_32(str)
% Author:  Arturo Leon
% % Date:  12/15/2013

clear all
clc

global Decide_flow_discharge;
global calculation;
global c_bisection;
global turbine_type;
global CL;
global AP_AN;
global eff_turb;
global eff_gener;
global Qplus_actual;
global Pplus_actual;
if nargin==0
    str = [];  % User called the GUI without and output name.  
elseif ~ischar(str)
    error('Only a string argument is valid.  See help.')
    calculation = 100;
elseif ~isvarname(str) % User passed something like:  0.?6#
    error('String must be a valid variable name.  See help.')
    calculation = 100;
end

S.STR = str;  % The name of the variable the user wishes to have in base.
S.CNT = 0;  % The number of times user pressed the pushbutton.
S.CHC = [];  % Holds the strings which represent the operations performed.
S.fh = figure('units','pixels',...
              'position',[300 100 950 860],...
              'menubar','none',...
              'name','Optimal flow discharge and optimal penstock diameter - Arturo Leon, Oregon State University',...                 
              'numbertitle','off',...
              'resize','on',...
              'deletefcn',{@fig_del,S});
          
S.fh.CreateFcn = @movegui;
hgsave(S.fh,'onscreenfig');
%close(S.fh);
%f2 = hgload('onscreenfig');    
movegui(S.fh,'southwest');
          
          
COL = get(S.fh,'color');    
              
        %'BackgroundColor', 'r',...      
              
% bgcolor = [1 0 0];
% set(figure, 'Color', bgcolor);
% set(findobj(figure,'-property', 'BackgroundColor'), 'BackgroundColor', bgcolor);         
%              
              
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
 S.Title(1) = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[10 810 790 40],...
                    'string','Optimal discharge and optimal penstock diameter for impulse and reaction turbines',...
                    'fontsize',14,'FontName','Arial',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');       
                
S.Author = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[10 780 790 40],...
                    'string','Author:',...
                    'fontsize',11,'FontName','Arial',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left'); 
                
S.Author2 = uicontrol('style','edit',...
                    'Foregroundcolor', 'b', ...
                    'unit','pix',...
                    'position',[70 800 260 25],...
                    'string','Arturo Leon, Oregon State University',...
                    'fontsize',11,'FontName','Arial',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');  
hCMenu = uicontextmenu;                       %# Create a context menu
uimenu(hCMenu,'Label','Copy',...              %# Create a menu item
       'Callback',@(hObject,eventData) clipboard('copy',get(S.Author2,'String')));
set(S.Author2,'UIContextMenu',hCMenu);            %# Add context menu to control
                
S.Author3 = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[340 780 790 40],...
                    'string','e-mail:',...
                    'fontsize',11,'FontName','Arial',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left'); 
S.Author4 = uicontrol('style','edit',...
                    'Foregroundcolor', 'b',...
                    'unit','pix',...
                    'position',[400 800 220 25],...
                    'string','arturo.leon@oregonstate.edu',...
                    'fontsize',11,'FontName','Arial',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left', ...
                    'call','set(gcbo,''string'',''arturo.leon@oregonstate.edu'')');
                
                
hCMenu = uicontextmenu;                       %# Create a context menu
uimenu(hCMenu,'Label','Copy',...              %# Create a menu item
       'Callback',@(hObject,eventData) clipboard('copy',get(S.Author4,'String')));
set(S.Author4,'UIContextMenu',hCMenu);            %# Add context menu to control
                
                
S.Disclaimer1 = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[10 750 790 40],...
                    'string','Disclaimer:',...
                    'fontsize',11,'FontName','Arial',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left'); 
S.Disclaimer2 = uicontrol('style','text',...
                    'Foregroundcolor', 'k', ...
                    'unit','pix',...
                    'position',[100 730 790 60],...
                    'string','This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.', ...
                    'fontsize',9,'FontName','Arial',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 bgcolor = [1 0.5 0];
 bgcolor_light_blue = [0.5  0.5  1];
 S.Logo = uicontrol('style','text',...
                    'Foregroundcolor', 'w', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[10 45 110 52],...
                    'string','OSU',...
                    'fontsize',35,'FontName','LeituraSans-Grot',...
                    'backgroundcolor',bgcolor, 'HorizontalAlignment','center');  
                
S.Logo2 = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[10 25 110 20],...
                    'string','Oregon State',...
                    'fontsize',11,'FontName','SohoStd-Bold',...
                    'backgroundcolor',bgcolor, 'HorizontalAlignment','center');               
                
                
S.Logo3 = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[10 10 110 15],...
                    'string','University ',...                    
                    'fontsize',9,'FontName','SohoStd-Bold',...
                    'backgroundcolor',bgcolor, 'HorizontalAlignment','right');
                    
S.contact_info = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...                    
                    'position',[130 60 450 20],...
                    'string','This program is based on the paper: ',...
                    'fontsize',11,'FontName','LeituraSans-Grot',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');  
                    
S.contact_info1 = uicontrol('style','text',...
                    'Foregroundcolor', 'k', ...
                    'unit','pix',...
                    'position',[130 10 810 50],...
                    'string','Leon, A. S. and Zhu, L. (2014), "A dimensional analysis for determining optimal discharge and penstock diameter in impulse and reaction water turbines", Journal of Renewable Energy.',...
                    'fontsize',9,'FontName','LeituraSans-Grot',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left'); 
% S.contact_info2 = uicontrol('style','text',...
%                     'Foregroundcolor', 'k', ...
%                     'unit','pix',...                                                                                
%                     'position',[130 10 481 20],...
%                     'string','This paper can be downloaded from:',...
%                     'fontsize',9,'FontName','LeituraSans-Grot',...                    
%                     'backgroundcolor',COL, 'HorizontalAlignment','left');    
                
% S.contact_info3 = uicontrol('style','edit',...
%                     'Foregroundcolor', 'b', ...
%                     'unit','pix',...                                        
%                     'SelectionHighlight','on',...   
%                     'Enable','on',...
%                     'HitTest','on',...                    
%                     'position',[350 10 240 20],...
%                     'string','http://web.engr.oregonstate.edu/~leon/',...
%                     'fontsize',9,'FontName','LeituraSans-Grot',...                    
%                     'backgroundcolor',COL, 'HorizontalAlignment','left', ... 
%                     'call','set(gcbo,''string'',''http://web.engr.oregonstate.edu/~leon/'')');  
                
                
S.contact_info3 = uicontrol('Style','pushbutton',...
            'Foregroundcolor', 'k', 'fontweight', 'bold', ...
            'Position',[210 10 240 20],...
            'String','Click here to download paper',...
            'fontsize',11,'FontName','LeituraSans-Grot',... 
            'Callback',@(hObject,eventData) web('http://web.engr.oregonstate.edu/~leon/'));

                
                
                
                
 
hCMenu = uicontextmenu;                       %# Create a context menu
uimenu(hCMenu,'Label','Copy',...              %# Create a menu item
       'Callback',@(hObject,eventData) clipboard('copy',get(S.contact_info3,'String')));
set(S.contact_info3,'UIContextMenu',hCMenu);            %# Add context menu to control 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
 S.Input_Data_text = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[10 715 200 30],...
                    'string','Data Input',...
                    'fontsize',14,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
 S.Results_text = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[365 715 200 30],...
                    'string','Results',...
                    'fontsize',14,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
 S.Comments_text = uicontrol('style','text',...
                    'Foregroundcolor', 'k', 'fontweight', 'bold', ...
                    'unit','pix',...
                    'position',[365 600 200 30],...
                    'string','Comments:',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');                
                
 
 S.Comments_text_result1 = uicontrol('style','text',...
                    'Foregroundcolor', 'r', ...
                    'unit','pix',...
                    'position',[455 590 300 40],...
                    'string','',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');      
S.Comments_text_result2 = uicontrol('style','text',...
                    'Foregroundcolor', 'b', ...
                    'unit','pix',...
                    'position',[455 590 300 40],...
                    'string','',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left'); 
                
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 
 
 S.Type_of_turbine(1) = uicontrol('style','text',...                    
                    'unit','pix',...
                    'position',[10 680 200 30],...
                    'string','Specify the type of turbine',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                
S.Turbine_type = uicontrol('style','pop',...
                  'unit','pix',...
                  'position',[200 680 130 30],...
                  'string',{'Impulse Turbine';'Reaction Turbine'});
             
               
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
 S.Data_specified_text = uicontrol('style','text',...                    
                    'unit','pix',...
                    'position',[10 650 200 30],...
                    'string','Enter variable that is known ',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');              
    
S.Data_specified = uicontrol('style','pop',...
                  'unit','pix',...
                  'position',[200 650 130 30],...
                  'string',{'Only flow discharge (Optimal)';'Only electrical power (Optimal)';'Penstock Diameter and Flow (No optimal)'});
                
                
                
S.Value_flow_or_Power_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 620 240 30],...
                    'string','Flow discharge in m^3/s',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                    
              
S.Value_flow_or_Power = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 620 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',' ');   
                
                
                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
S.Penst_diameter_No_optimal_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 575 240 30],...
                    'string','Penstock diameter (not optimal) [m]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL,'HorizontalAlignment','left');
                
S.Penst_diameter_No_optimal = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 575 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',''); 

                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
                                
S.optimal_diameter_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[605 640 200 30],...
                    'string','Optimal diameter [mm]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                
S.optimal_diameter = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[760 645 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',' ');  
                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                   
S.optimal_power_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[365 680 125 40],...
                    'string','Optimal electrical power [kW]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                
S.optimal_power = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[495 685 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',' ');               
                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                   
S.optimal_flow_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[605 680 140 30],...
                    'string','Optimal flow [Liters/s]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                
S.optimal_flow = uicontrol('style','edit',...
                    'unit','pix',...
                   'position',[760 685 80 30],...
                    'string',' ');    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                   
S.head_loss_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[365 640 125 30],...
                    'string','Head loss (%)',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
                
S.head_loss = uicontrol('style','edit',...
                    'unit','pix',...
                   'position',[495 645 80 30],...
                   'fontsize',11,'FontName','Times New Roman',...
                    'string',' ');  
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                        
 
 S.Penstock_length_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 530 240 30],...
                    'string','Penstock length [m]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
              
S.Penstock_length = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 530 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',' ');    
                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
 S.Gross_head_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 485 240 30],...
                    'string','Gross head [m]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
              
S.Gross_head = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 485 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',' ');  

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 
 S.Ratio_areas_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 430 240 45],...
                    'string','Ratio of penstock area to nozzle area [no units]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');  
S.Ratio_areas = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 440 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',' ');               
              
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  
 
 S.Sum_local_losses_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 320 240 60],...
                    'string','Sum of local losses in penstock due to entrance, bends, penstock fittings and gates [no units]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
              
S.Sum_local_losses = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 335 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string',' '); 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 S.Roughness_height_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 270 240 40],...
                    'string','Roughness height of penstock material [mm]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
              
S.Roughness_height = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 280 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string','0.045'); 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 
 S.Kinematic_Viscosity_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 240 240 30],...
                    'string','Kinematic Viscosity [m^2/s] ',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
              
S.Kinematic_Viscosity = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 240 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string','1.000e-06');    
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 
 S.Turbine_efficiency_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 200 240 30],...
                    'string','Turbine efficiency (%)',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
              
S.Turbine_efficiency = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 200 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string','80 ');  
              
              
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
S.Generator_efficiency_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 160 240 30],...
                    'string','Generator efficiency (%)',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL, 'HorizontalAlignment','left');
              
S.Generator_efficiency = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 160 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string','80');               
              
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
S.Nozzle_vel_coef_text = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[10 385 240 30],...
                    'string','Nozzle velocity coefficient [no units]',...
                    'fontsize',12,'FontName','Times New Roman',...
                    'backgroundcolor',COL,'HorizontalAlignment','left');
                
S.Nozzle_vel_coef = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[250 390 80 30],...
                    'fontsize',11,'FontName','Times New Roman',...
                    'string','0.999'); 
 
%Plotting                
h = subplot('position',[0.44 0.17 0.52 0.48]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                        
%To start with the computations
set(S.Penst_diameter_No_optimal,'visible','off');
set(S.Penst_diameter_No_optimal_text,'visible','off');
set(S.optimal_flow,'visible','off');
set(S.optimal_flow_text,'visible','off');
set(S.Comments_text_result2,'visible','off');
set(S.Comments_text_result1,'visible','off');
set(S.Ratio_areas,'string','');
set(S.Penst_diameter_No_optimal,'string','');
Decide_flow_discharge = 'Flow discharge';                
calculation = 1; 
turbine_type = 1;    
   
S.Calculate = uicontrol('style','push',...
                  'unit','pix',...
                  'position',[100 110 200 30],...                  
                  'fontweight', 'bold', ...
                  'fontsize',14,'FontName','Arial',...
                  'string','Calculate and Plot');  
              
%set([S.Turbine_type,S.Calculate],'callback',{@pb_call,S}); 
set([S.Data_specified],'callback',{@pb_call2,S});      
set([S.Turbine_type],'callback',{@pb_call3,S}); 
set([S.Calculate],'callback',{@pb_call,S}); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  

function [] = pb_call3(varargin);
global Decide_flow_discharge;
global calculation;
global c_bisection;
global turbine_type;
global CL;
global AP_AN;
global eff_turb;
global eff_gener;
global Qplus_actual;
global Pplus_actual;

% Callback for pushbutton
S = varargin{3};  % Get the structure.    
VL3 = get(S.Turbine_type,{'str','value'});  % User's choice from popup.

% Now get the string updates and perform operations.
switch VL3{1}{VL3{2}};  % User's string choice.
    case 'Impulse Turbine';     
        turbine_type = 1
        str = 'Nozzle velocity coefficient [no units]';
        str2 = 'Ratio of penstock area to nozzle area [no units]';
        set(S.Nozzle_vel_coef,'visible','on');
    case 'Reaction Turbine';
        str = '';
        turbine_type = 2
        str2 = 'Ratio of penstock area to draft tube area (at its outlet) [no units]';        
        set(S.Nozzle_vel_coef,'visible','off');        
    otherwise
        turbine_type = 100
        calculation = 100;
end
set(S.Nozzle_vel_coef_text,'string',str);  % Set the operation display.
set(S.Ratio_areas_text,'string',str2);  % Set the operation display.
set(S.Ratio_areas,'string','');  % Set the operation display.
set(S.optimal_flow,'string','');
set(S.optimal_power,'string',''); 
set(S.optimal_diameter,'string','');
set(S.head_loss,'string',''); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [] = pb_call2(varargin)
global Decide_flow_discharge;
global calculation;
global c_bisection;
global turbine_type;
global CL;
global AP_AN;
global eff_turb;
global eff_gener;
global Qplus_actual;
global Pplus_actual;
% Callback for pushbutton
S = varargin{3};  % Get the structure.
VL2 = get(S.Data_specified,{'str','value'});  % User's choice from popup.
switch VL2{1}{VL2{2}};       %{VL1{2}}{VL1{3}}  % User's string choice.
    case 'Only flow discharge (Optimal)'        
        str = 'Flow discharge [m^3/s]';                
        set(S.Penst_diameter_No_optimal,'visible','off');
        set(S.Penst_diameter_No_optimal_text,'visible','off');
        set(S.optimal_power,'visible','on');
        set(S.optimal_power_text,'visible','on');
        set(S.optimal_flow,'visible','off');
        set(S.optimal_flow_text,'visible','off');
        set(S.optimal_diameter,'visible','on');
        set(S.optimal_diameter_text,'visible','on');        
        Decide_flow_discharge = 'Flow discharge';
        calculation = 1; %Flow discharge is given
        string2 = '';
        str3 = 'Optimal electrical power [kW]';
    case 'Only electrical power (Optimal)';        
        str = 'Electrical Power [kW]';
        set(S.Penst_diameter_No_optimal,'visible','off');
        set(S.Penst_diameter_No_optimal_text,'visible','off');
        set(S.optimal_power,'visible','off');
        set(S.optimal_power_text,'visible','off');
        set(S.optimal_flow,'visible','on');
        set(S.optimal_flow_text,'visible','on'); 
        set(S.optimal_diameter,'visible','on');
        set(S.optimal_diameter_text,'visible','on');
        Decide_flow_discharge = 'Electrical Power';
        calculation = 2; %Power is given
        string2 = '';
        str3 = 'Optimal electrical power [kW]';
    case 'Penstock Diameter and Flow (No optimal)';         
        str = 'Flow discharge [m^3/s]';          
        set(S.Penst_diameter_No_optimal,'visible','on');
        set(S.Penst_diameter_No_optimal_text,'visible','on');
        set(S.Penst_diameter_No_optimal,'string','');
        set(S.optimal_flow,'visible','off');
        set(S.optimal_flow_text,'visible','off');
        set(S.optimal_diameter,'visible','off');
        set(S.optimal_diameter_text,'visible','off');
        set(S.optimal_power,'visible','on');
        set(S.optimal_power_text,'visible','on');
        %set(S.Value_flow_or_Power,'string','')
        Decide_flow_discharge = 'Pipe_and_Discharge';
        string2 = '';
        calculation = 3; 
        str3 = 'Electrical power [kW]';
    otherwise        
        set(S.Value_flow_or_Power_text,'string','Error');  % Set the operation display.
        set(S.Value_flow_or_Power,'string',''); 
        error('Error in users choice of pop. Contact Arturo Leon');
        calculation = 100;
        return;
end
set(S.Comments_text_result2,'visible','off');
set(S.Comments_text_result1,'visible','off');
set(S.optimal_power_text,'string',str3);
set(S.Value_flow_or_Power_text,'visible','on');
set(S.Value_flow_or_Power_text,'string',str);  % Set the operation display.
set(S.Value_flow_or_Power,'string',''); 
set (S.optimal_flow, 'String', '');
set(S.optimal_power,'string',''); 
set(S.optimal_diameter,'string',''); 
set(S.head_loss,'string',''); 
 

function [] = pb_call(varargin);
global Decide_flow_discharge;
global calculation;
global c_bisection;
global turbine_type;
global CL;
global AP_AN;
global eff_turb;
global eff_gener;
global Qplus_actual;
global Pplus_actual;
% Callback for pushbutton
S = varargin{3};  % Get the structure.
H_g = str2double(get(S.Gross_head,'string'))  % Numbers from edits to op. on. 
L = str2double(get(S.Penstock_length,'string')) %Penstock length in m 
K_P = str2double(get(S.Sum_local_losses,'string'))  %Sum of coefficients of losses in penstock
Epsilon  = str2double(get(S.Roughness_height,'string')) %Roughness height in mm
Vis = str2double(get(S.Kinematic_Viscosity,'string')) %Kinematic viscosity in m2/s
eff_turb = str2double(get(S.Turbine_efficiency,'string')) %Turbine efficiency
eff_gener = str2double(get(S.Generator_efficiency,'string')) %Generator efficiency
pho = 1000  %density of water 
g = 9.8 %gravity
C_V = str2double(get(S.Nozzle_vel_coef,'string')) %Nozzle velocity coeff.
AP_AN = str2double(get(S.Ratio_areas,'string'))
value_flow_or_power = str2double(get(S.Value_flow_or_Power,'string'))
D1 = str2double(get(S.Penst_diameter_No_optimal,'string')) %commercial diameter 

  if strcmp(Decide_flow_discharge, 'Flow discharge');  
      Q = value_flow_or_power;
      calculation = 1; %Flow discharge is given
      str3 = 'Optimal electrical power [kW]';
  elseif strcmp(Decide_flow_discharge, 'Electrical Power');
      P = value_flow_or_power;
      calculation = 2; %Power is given
       str3 = 'Optimal electrical power [kW]';
  elseif strcmp(Decide_flow_discharge, 'Pipe_and_Discharge');
      Q = value_flow_or_power;
      calculation = 3; %Flow discharge and diameter are given
       str3 = 'Electrical power [kW]';
  else
        fprintf('There is an error in value_flow_or_power \n');        
        %error('There is an error in value_flow_or_power. Contact Arturo Leon')
        calculation = 100;
        return;
  end

if turbine_type == 1;
        K_N = 1/C_V^2 - 1; % Nozzle coefficient   
elseif turbine_type == 2;  
        K_N = 1;  % Nozzle coefficient 
else
        calculation = 100;
end

set(S.optimal_flow,'string','');
set(S.optimal_power,'string',''); 
set(S.optimal_diameter,'string','');
set(S.optimal_power_text,'string',str3);
%set(S.Ratio_areas,'string','');  % Set the operation display.
set(S.head_loss,'string',''); 

        
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
if calculation == 1;
        Const = 14.0/45.0*g*H_g/Q^2;
        %fprintf ('the nozzle coefficient is %6.4f   \n',K_N);
        %fprintf ('the constant is %6.4f   \n', Const);
        Epsilon  = Epsilon/1000.0;

        f1=@(D) (((0.25/(abs(log10(Epsilon/(3.7*D) + 5.74/((Q/((pi/4.0)*D^2))*D/Vis)^0.9)))^2)*L/D) + ...
                 K_P + K_N*AP_AN^2)...
               /(pi/4.0*D^2)^2 - Const; 
        
        %Bisection Method  
            e=10^-8; %Tolerance of calculation   
            a=10^-10; %lower limit
            c=100; %upper limit             
           while abs(c-a)>e;       
                b=(c+a)/2;
                if f1(a)*f1(b)<0;
                    c=b;       
                else a=b;
                end;       
               %disp(b);
           end
        %fprintf ('the optimal diameter of the pipe is %6.4f m  \n', b);
        D = b;
        c_bisection = b;
        D_mm = D*1000;
        Q_Liters = Q*1000;
        A = (pi/4.0)*D^2;
        CL = ((0.25/(abs(log10(Epsilon/(3.7*D) + 5.74/((Q/((pi/4.0)*D^2))*D/Vis)^0.9)))^2)*L/D) + K_P + K_N*AP_AN^2;
        head_loss_dim = CL * Q^2/(2*g*H_g*A^2);
        head_loss_percent = head_loss_dim*100;
        %fprintf ('CL is %8.3f  \n', CL);
        %fprintf ('The dimensionless head loss is %8.3f  \n', head_loss_dim);  
        Power = eff_turb/100*eff_gener/100*pho*g*Q*(H_g-CL*Q^2/(2*g*A^2));
        Power = Power/1000;
elseif calculation == 2;
        P = P*1000; %to convert power to watts     
        Epsilon  = Epsilon/1000.0;
        Q = (45/38)*P/(eff_turb/100*eff_gener/100*pho*g*H_g); 
       %fprintf ('The optimal flow discharge to use is %6.4f m3/s  \n',Q);    
        %fprintf ('The optimal flow discharge to use is %6.4f L/s  \n',1000*Q);    
        Const = 14.0/45.0*g*H_g/Q^2;
        fprintf ('the constant is %6.4f   \n', Const);    

         f1=@(D) (((0.25/(abs(log10(Epsilon/(3.7*D) + 5.74/((Q/((pi/4.0)*D^2))*D/Vis)^0.9)))^2)*L/D) + ...
                 K_P + K_N*AP_AN^2)...
               /(pi/4.0*D^2)^2 - Const; 
       
        %Bisection Method  
            e=10^-8; %Tolerance of calculation   
            a=10^-10; %lower limit
            c=100; %upper limit    
           while abs(c-a)>e;       
                b=(c+a)/2;
                if f1(a)*f1(b)<0;
                    c=b;       
                else a=b;
                end       
               %disp(b);
           end
        %fprintf ('the optimal diameter of the pipe is %6.4f m  \n', b);
        D = b;
        c_bisection = b;
        D_mm = D*1000;
        Q_Liters = Q*1000;
        A = (pi/4.0)*D^2;
        CL = ((0.25/(abs(log10(Epsilon/(3.7*D) + 5.74/((Q/((pi/4.0)*D^2))*D/Vis)^0.9)))^2)*L/D) + K_P + K_N*AP_AN^2;
        head_loss_dim = CL * Q^2/(2*g*H_g*A^2);
        head_loss_percent = head_loss_dim*100;
        %fprintf ('CL is %8.3f  \n', CL);
        %fprintf ('The dimensionless head loss is %8.3f  \n', head_loss_dim);   
        Power = eff_turb/100*eff_gener/100*pho*g*Q*(H_g-CL*Q^2/(2*g*A^2));
        Power = Power/1000;
elseif calculation == 3;
        %Compute CL and Electrical Power________________________________________________________________________________________________________________________________________________________
        c_bisection = 1; % this costant is only for the bisection method. Here it is not used
        D_mm = D1*1000;
        Epsilon  = Epsilon/1000.0;
        Q_Liters = Q*1000;
        A =  (pi/4.0)*D1^2;
        %fprintf ('The area of the commercial diameter is %8.4f  \n', A);        
        CL = ((0.25/(abs(log10(Epsilon/(3.7*D1) + 5.74/((Q/((pi/4.0)*D1^2))*D1/Vis)^0.9)))^2)*L/D1) + K_P + K_N*AP_AN^2;
		%fprintf ('The new CL for the commercial diameter is %8.4f  \n', CL);        
		head_loss_dim1 = CL * Q^2/(2*g*H_g*A^2);
        head_loss_percent = head_loss_dim1*100;
		%if head_loss_percent > 15.6;
            %fprintf ('Warning: Head loss is higher than 15.6% of gross head   \n');
			%fprintf ('A head loss higher than 15.6% may not correspond to the optimal conditions.\n');			            
        %end  
		%fprintf ('The new dimensionless head loss for the commercial diameter is %8.4f  \n', head_loss_dim1);        
        Power = eff_turb/100*eff_gener/100*pho*g*Q*(H_g-CL*Q^2/(2*g*A^2));
        Power = Power/1000;
		%fprintf ('The optimal electrical power for the specified discharge is %8.4f KW  \n', Power);        
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$             
else    
        error('Enter 1, 2 or 3 for the variable "calculation"');
        calculation = 100;
        return;
end

A3 = A/AP_AN;
P_ref = (4/3)*pho*g*H_g*A3*sqrt((1/3)*g*H_g);
P_ref = P_ref/1000;
Q_ref = 2*A3*sqrt((1/3)*g*H_g);
Qplus_actual = Q/Q_ref;
Pplus_actual = Power/P_ref;         

comment = 1;
if varargin{1}==S.Calculate;  % This stuff we only need to do if button is pushed.
    S.CNT = S.CNT + 1;    
    if (isnan(D_mm) == 0 & isnan(Power) == 0 & isnan(Q_Liters) == 0) & calculation < 4 & c_bisection > 0 & c_bisection < 99 & D_mm > 0 & Power > 0 & Q_Liters>0;
        comment = 1;
        
        set(S.optimal_diameter,'str',num2str(D_mm,'%6.2f'))
        set(S.optimal_power,'str',num2str(Power,'%8.2f'))
        set(S.optimal_flow,'str',num2str(Q_Liters,'%10.2f'))  
        set(S.head_loss,'str',num2str(head_loss_percent,'%4.2f'))   
        set(S.Comments_text_result2,'str','The calculation did not produce errors ');         
        set(S.Comments_text_result2,'visible','on');
        set(S.Comments_text_result1,'visible','off');
        
    elseif (isnan(D_mm) == 1 | isnan(Power) == 1 | isnan(Q_Liters) == 1);
        comment = 2;
        set(S.Comments_text_result1,'str','The calculation produced NaN (Not a number). Please revise the input data'); 
    elseif  calculation > 3;
        comment = 2;
        set(S.Comments_text_result1,'str','The calculation produced an unknown error. Please revise the input data');         
    elseif  c_bisection <= 0  | c_bisection > 99; 
        comment = 2;
        set(S.Comments_text_result1,'str','The calculation produced an extremely small or extremely large value. Please revise the input data');    
     elseif  D_mm <= 0; 
        comment = 2;
        set(S.Comments_text_result1,'str','The calculation produced a penstock diameter smaller than or equal to zero. Please revise the input data');    
     elseif  Power <= 0; 
        comment = 2;
        set(S.Comments_text_result1,'str','The calculation produced an Electrical power smaller than or equal to zero. Please revise the input data');          
      elseif  Q_Litersr <= 0; 
        comment = 2;
        set(S.Comments_text_result1,'str','The calculation produced a flow discharge smaller than or equal to zero. Please revise the input data');    
    else  
        comment = 2;
        set(S.Comments_text_result1,'str','The calculation produced an unknown error. Please revise the input data'); 
    end
    
    
    if comment == 2;
        set(S.optimal_diameter,'str','Data Error');    
        set(S.optimal_power,'str','Data Error');    
        set(S.optimal_flow,'str','Data Error');  
        set(S.head_loss,'str','Data Error');  
        set(S.Comments_text_result2,'visible','off');
        set(S.Comments_text_result1,'visible','on');
    end
        
    if calculation == 100;
        error('Errors in the calculation. No plot can be produced');
        return;
    else   
        
        eta = eff_turb/100*eff_gener/100; %Efficiency of turbine (turbine efficiency x generator efficiency)
        Beta(2) = CL/AP_AN^2;
        Beta(1) = Beta(2)/2;
        Beta(3) = 2*Beta(2);
        for jj = 1 : length(Beta)
                dp_dq_for_plotting = 0.8*eta;
                k_temp = dp_dq_for_plotting/eta;
                Qplus_temp =  ((3/2-k_temp)/(3*Beta(jj)))^0.5; 
                plus_temp =  eta*((3/2) * Qplus_temp - Beta(jj) * Qplus_temp .^3);
                Q_eff_0_8(jj) = Qplus_temp;
                P_eff_0_8(jj) = plus_temp;

                dp_dq_for_plotting = 0.0*eta;
                k_temp = dp_dq_for_plotting/eta;
                Qplus_temp =  ((3/2-k_temp)/(3*Beta(jj)))^0.5; 
                plus_temp =  eta*((3/2) * Qplus_temp - Beta(jj) * Qplus_temp .^3);
                Q_eff_0(jj) = Qplus_temp;
                P_eff_0(jj) = plus_temp;  
                Pplus_max(jj) = plus_temp;
                Qplus_max(jj) = Qplus_temp;
        end
        Qplus_max_lim = 1.1*Qplus_max(1);
        Qplus   = linspace(0, Qplus_max_lim, 500);
        colorMap    = ['kbm'] ;
        icolor      = 1; 


        for jj = 1 : length(Beta)
            s{jj} = sprintf('%s%g','',Beta(jj));
            Pplus =  eta*((3/2) * Qplus - Beta(jj) * Qplus .^3); 
            Pplus (Pplus  < 0) = NaN;  %check this

            %writing the curves
            h     = plot(Pplus, Qplus, ['-', colorMap(icolor)], 'linewidth', 2.5);
            hold on
            box on
            icolor = icolor + 1;    
        end
        hold on
        plot(P_eff_0,Q_eff_0,'s--','LineWidth',2.5,'Color','g',...
                               'MarkerEdgeColor','g',...
                               'MarkerFaceColor','g',...
                               'MarkerSize',5);
        plot(P_eff_0_8,Q_eff_0_8,'s--','LineWidth',2.5,'Color','c',...
                               'MarkerEdgeColor','c',...
                               'MarkerFaceColor','c',...
                               'MarkerSize',5);
        hold on
        xx =  '$\eta = \eta_t\times\eta_g = $';
        temp1 = eta*100;
        yy1 = num2str(temp1);
        zz1 =  '$\%$';
        eta_string = strcat(xx,yy1,zz1);
        Pplus_plot= 0.5*max(Pplus_max(1));
        Qplus_plot= 0.15*max(Qplus_max(1));
        AAA = text(Pplus_plot,Qplus_plot, eta_string, 'FontSize',22, 'BackgroundColor', [1 1 1]);   
        set(AAA,'Interpreter','latex');
        plot(Pplus_actual,Qplus_actual,'Marker','p','Color','r', ...
            'MarkerEdgeColor','r',...
            'MarkerFaceColor','r',...
            'MarkerSize',25);
        p_plot_actual(1) = 0.95*Pplus_actual;
        p_plot_actual(2) = 0.85*Qplus_actual;
         text(p_plot_actual(1),p_plot_actual(2), 'Your data places here','FontWeight', 'bold','FontSize',16);    
        hold on
        xx =  '$\beta = $';
        zz =' (Data)';
        yy1 = num2str(Beta(1));
        yy2 = num2str(Beta(2));
        yy3 = num2str(Beta(3));
        xx4 = '$dP_{+}/dQ_{+} = 0$';
        yy4 = '';
        xx5 = '$dP_{+}/dQ_{+} = 0.8 \eta$';
        yy5 = '';
        s_legend{1} = strcat(xx,yy1);
        s_legend{2} = strcat(xx,yy2,zz);
        s_legend{3} = strcat(xx,yy3);
        s_legend{4} = strcat(xx4,yy4);
        s_legend{5} = strcat(xx5,yy5);
        set(gca, 'FontSize', 14);  
        grid minor;
        xlabel('$P_+ = \frac{P}{P_{o}}$','Interpreter','LaTex','FontSize',20);
        ylabel('$Q_+ = \frac{Q}{Q_{o}}$','Interpreter','LaTex','FontSize',20);
        %title('Dimensionless flow discharge versus dimensionless power for various dimensionless head losses','FontSize',20) 
        %annotation('textarrow', [.49 0.53],[.62,0.68],'linewidth',2.5,'Color','k', 'String','$\beta$','Interpreter','LaTex', 'FontSize',20)
        %text(0.37,1.12, '$\beta$','Interpreter','LaTex', 'FontSize',50, 'BackgroundColor', [1 1 1]); 
       
        set(gca,'TickDir','out');        
        Leg = legend(s_legend{:}, 'Location','NorthWest');
        set(Leg,'Interpreter','latex','FontSize',20, 'FontName','Times New Roman','fontweight', 'bold');
        
        
        zoom on;
        datacursormode on;
        hold off
    end    

    %set(S.Calculate,'callback',{@pb_call,S});
    set(S.fh,'deletefcn',{@fig_del,S});   
    %calculation = 1 %to start new process
end

function [] = fig_del(varargin);
global Decide_flow_discharge;
global calculation;
global c_bisection;
global turbine_type;
S = varargin{3};
if ~isempty(S.STR);
    assignin('base',S.STR,S.CHC); % Assign the data to the base workspace.
end


