function varargout = ADDIF(varargin)
% ADDIF M-file for ADDIF.fig
%      ADDIF, by itself, creates a new ADDIF or raises the existing
%      singleton*.
%
%      H = ADDIF returns the handle to a new ADDIF or the handle to
%      the existing singleton*.
%
%      ADDIF('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ADDIF.M with the given input arguments.
%
%      ADDIF('Property','Value',...) creates a new ADDIF or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ADDIF_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ADDIF_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ADDIF

% Last Modified by GUIDE v2.5 19-Nov-2010 09:47:35

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ADDIF_OpeningFcn, ...
                   'gui_OutputFcn',  @ADDIF_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before ADDIF is made visible.
function ADDIF_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ADDIF (see VARARGIN)

% Choose default command line output for ADDIF
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% This sets up the initial plot - only do when we are invisible
% so window can get raised using ADDIF.
if strcmp(get(hObject,'Visible'),'off')
    D = str2num(get(handles.editD,'String'));                % Diffusivity (mē/s)
    v = str2num(get(handles.editv,'String'));                  % Standard Deviation (Sqrt(Variance))
    u = str2num(get(handles.editU,'String'));                  % Velocity (m/s)
    x0 = str2num(get(handles.editx0,'String'));               % Station of Intial Peak Concentration (m)
    xs = 0;                 % Start Station (m)
    ts = 0;
    xf = str2num(get(handles.editxf,'String'));               % Final Station (m)
    dx = str2num(get(handles.editdx,'String'));               % Station Step, Delta X (m)    
    x = [xs:dx:xf]'; % Store X values for each node of the approximate solution    
    C = v/(v^2+2*D*ts)^0.5.*exp(-(x-x0-u*ts).^2./(2*v^2+4*D*ts));
    plot(x,C,'b')
    axis ([xs xf -1 1 ]);
end

% UIWAIT makes ADDIF wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ADDIF_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Cmd = get(handles.pushbutton1,'String');
if isequal(Cmd,'Stop !')
    set(handles.pushbutton1,'String','Wait !');
elseif isequal(Cmd,'GO !')
    set(handles.pushbutton1,'String','Stop !');
    tic
    D = str2num(get(handles.editD,'String'));                % Diffusivity (mē/s)
    v = str2num(get(handles.editv,'String'));                  % Standard Deviation (Sqrt(Variance))
    u = str2num(get(handles.editU,'String'));                  % Velocity (m/s)
    theta = str2num(get(handles.edittheta,'String'));
    ts = 0;                 % Start Time (s)
    tf = str2num(get(handles.edittf,'String'));             % Final Time (s)
    dt = str2num(get(handles.editdt,'String'));              % Time Step, Delta T (s)
    Pcnt = str2num(get(handles.editPcnt,'String'));
    x0 = str2num(get(handles.editx0,'String'));               % Station of Intial Peak Concentration (m)
    xs = 0;                 % Start Station (m)
    xf = str2num(get(handles.editxf,'String'));               % Final Station (m)
    dx = str2num(get(handles.editdx,'String'));               % Station Step, Delta X (m)
    n = (xf-xs)/dx+1;       % Number of Nodes
    x = [xs:dx:xf]'; % Store X values for each node of the approximate solution
    xx = [xs:1:xf]'; % Store X values for 1m spacing for the exact solution
    Ct = u*dt/dx;
    Peclet=u*dx/D;
    Ps = Pcnt/dt;
    Ps = round(Ps);
    set(handles.editCnt,'String',Ct);
    set(handles.editPeclet,'String',Peclet);
    C = v/(v^2+2*D*ts)^0.5.*exp(-(x-x0-u*ts).^2./(2*v^2+4*D*ts));
    L1 = theta*(-u/2/dx-D/dx^2);
    L2 = 1/dt+theta*2*D/dx^2;
    L3 = theta*(u/2/dx-D/dx^2);
    R1 = (1-theta)*(D/dx^2+u/2/dx);
    R2 = (1/dt-(1-theta)*2*D/dx^2);
    R3 = (1-theta)*(D/dx^2-u/2/dx);
    A = sparse(n);           % Size Matrix "A"
    A(1,1) = 1;             % First Equation Sets BC1
    A(n,n) = 1;
    for i = 2:n-1           % Loop through Inner Matrix & Set L1,L2,L3
        A(i,i-1) = L1;
        A(i,i) = L2;
        A(i,i+1) = L3; 
    end
    A = sparse(A);          %Compress "A" Matrix into a sparse matrix
    Pstep = 0;
    axes(handles.axes1);
    cla;
    t = 0;
    CE = v/(v^2+2*D*t)^0.5.*exp(-(xx-x0-u*t).^2./(2*v^2+4*D*t)); %Exact Sol
    plot(x,C,'r',xx,CE,'b');  % Plot Approximate & Exact Solutions             
    TITLE = {strrep(strcat('at t =:',num2str(t,'%10.1f'),':s'),':',' ')};
    title(TITLE);
    axis ([xs xf -1 1 ]);        
    xlabel('x')
    ylabel('C')
    legend('Approximate Solution','Exact Solution')
    legend('Location','SouthEast')    
    pause(0.1); %Allows plot to print to screen during runtime
    for t = ts+dt:dt:tf     % Loop through time (dt increment)
        B(1,1) = v/(v^2+2*D*t)^0.5.*exp(-(xs-x0-u*t).^2./(2*v^2+4*D*t)); %BC1        
        B(n,1) = v/(v^2+2*D*t)^0.5.*exp(-(xf-x0-u*t).^2./(2*v^2+4*D*t)); %BC2        
        for i = 2:n-1       % Loop through inner B Matrix
            B(i) = R1*C(i-1) + R2*C(i) + R3*C(i+1);
        end
        CNEW = A\B;         % Solve for Cj+1        
        C = CNEW;           % Store Cj+1 as Cj for next time step
        Pstep = Pstep + 1;
        if round(1000*(round(t/Pcnt)-t/Pcnt))/1000==0 %Plot Solution at Print Step
            Pstep = 0;
            CE = v/(v^2+2*D*t)^0.5.*exp(-(xx-x0-u*t).^2./(2*v^2+4*D*t)); %Exact Sol
            CL = v/(v^2+2*D*t)^0.5.*exp(-(x-x0-u*t).^2./(2*v^2+4*D*t)); %Exact Sol
            Linf = max(CL-C);
            L2 = (1/n*sum((CL-C).^2))^0.5;
            set(handles.editL2,'String',L2);
            set(handles.editLinf,'String',Linf);
            set(handles.editRunTime,'String',toc);
            plot(x,C,'r',xx,CE,'b');  % Plot Approximate & Exact Solutions             
            TITLE = {strrep(strcat('at t =:',num2str(t,'%10.1f'),':s'),':',' ')};
            title(TITLE);
            axis ([xs xf -1 1 ]);        
            xlabel('x')
            ylabel('C')
            legend('Approximate Solution','Exact Solution')
            legend('Location','SouthEast')
            Cmd = get(handles.pushbutton1,'String');
            if isequal(Cmd,'Wait !'), break, end                
            pause(0.05); %Allows plot to print to screen during runtime
        end
    end
    CE = v/(v^2+2*D*t)^0.5.*exp(-(xx-x0-u*t).^2./(2*v^2+4*D*t)); %Exact Sol
    CL = v/(v^2+2*D*t)^0.5.*exp(-(x-x0-u*t).^2./(2*v^2+4*D*t)); %Exact Sol
    Linf = max(CL-C);
    L2 = (1/n*sum((CL-C).^2))^0.5;
    set(handles.editL2,'String',L2);
    set(handles.editLinf,'String',Linf);
    set(handles.editRunTime,'String',toc);
    plot(x,C,'r',xx,CE,'b');  % Plot Approximate & Exact Solutions             
    TITLE = {strrep(strcat('at t =:',num2str(t,'%10.1f'),':s'),':',' ')};
    title(TITLE);
    axis ([xs xf -1 1 ]);        
    xlabel('x')
    ylabel('C')
    set(handles.pushbutton1,'String','GO !');
end      


% --- Executes during object creation, after setting all properties.
function editdx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editdx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editdt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editdt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)



function editdx_Callback(hObject, eventdata, handles)
% hObject    handle to editdx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editdx as text
%        str2double(get(hObject,'String')) returns contents of editdx as a double



function editdt_Callback(hObject, eventdata, handles)
% hObject    handle to editdt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editdt as text
%        str2double(get(hObject,'String')) returns contents of editdt as a double



function edittheta_Callback(hObject, eventdata, handles)
% hObject    handle to edittheta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edittheta as text
%        str2double(get(hObject,'String')) returns contents of edittheta as a double



function editU_Callback(hObject, eventdata, handles)
% hObject    handle to editU (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editU as text
%        str2double(get(hObject,'String')) returns contents of editU as a double



function editD_Callback(hObject, eventdata, handles)
% hObject    handle to editD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editD as text
%        str2double(get(hObject,'String')) returns contents of editD as a double



function edittf_Callback(hObject, eventdata, handles)
% hObject    handle to edittf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edittf as text
%        str2double(get(hObject,'String')) returns contents of edittf as a double



function editxf_Callback(hObject, eventdata, handles)
% hObject    handle to editxf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editxf as text
%        str2double(get(hObject,'String')) returns contents of editxf as a double



function editx0_Callback(hObject, eventdata, handles)
% hObject    handle to editx0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editx0 as text
%        str2double(get(hObject,'String')) returns contents of editx0 as a double



function edit18_Callback(hObject, eventdata, handles)
% hObject    handle to edit18 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit18 as text
%        str2double(get(hObject,'String')) returns contents of edit18 as a double


% --- Executes during object creation, after setting all properties.
function edit18_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit18 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function edittheta_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edittheta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editU_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editU (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editD_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function edittf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edittf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editxf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editxf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editx0_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editx0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editv_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editPcnt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editPcnt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editCnt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editCnt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editPeclet_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editPeclet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editRunTime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editRunTime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editL2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editL2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function editLinf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editLinf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
