function varargout = jarvany_gui(varargin)
% JARVANY_GUI MATLAB code for jarvany_gui.fig
%      JARVANY_GUI, by itself, creates a new JARVANY_GUI or raises the existing
%      singleton*.
%
%      H = JARVANY_GUI returns the handle to a new JARVANY_GUI or the handle to
%      the existing singleton*.
%
%      JARVANY_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in JARVANY_GUI.M with the given input arguments.
%
%      JARVANY_GUI('Property','Value',...) creates a new JARVANY_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before jarvany_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to jarvany_gui_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 jarvany_gui

% Last Modified by GUIDE v2.5 04-Apr-2011 13:30:41

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @jarvany_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @jarvany_gui_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 jarvany_gui is made visible.
function jarvany_gui_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 jarvany_gui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes jarvany_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%handles.tmr = timer('TimerFcn', {@TmrFcn,handles.guifig},'BusyMode','Queue',...
%    'ExecutionMode','FixedRate','Period',0.1);%timer updating after every 0.1 secs

handles.guifig = gcf;
handles.tmr = timer('TimerFcn', {@TmrFcn,handles.guifig},'BusyMode','Queue',...
    'ExecutionMode','FixedRate','Period',0.3);
guidata(handles.guifig,handles);

guidata(hObject, handles);


global map N latentUntil infectionRate healRate initialProbInfection dieAfter pop newpop;

N = 100;

initialProbInfection = 0.001;
infectionRate = 0.1;
healRate = 0.05;
dieAfter = 10;
latentUntil = 4;


pop = zeros(N+2);
pop(2:N+1,2:N+1) = ones(N) + (rand(N)<initialProbInfection);
pop(1,1) = dieAfter+2;
newpop = pop;

%imagesc(pop);
imagesc(pop,'Parent',handles.axes1);
map = zeros(dieAfter+2,3);
map(1,:)= [0 1 0];
map(2,:)= [1 1 1];
map(3:end,1) = flipud(linspace(1,0,dieAfter));
colormap(map);

% --- Outputs from this function are returned to the command line.
function varargout = jarvany_gui_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)
if (length(get(handles.tmr,'Running')) == 3)
    start(handles.tmr);
else
    stop(handles.tmr);
end


%Timer Function
function TmrFcn(src,event,handles) %Timer function
handles = guidata(handles);

guidata(handles.guifig, handles);
global map N latentUntil infectionRate healRate initialProbInfection dieAfter pop newpop;
   %imagesc(pop);
   imagesc(pop,'Parent',handles.axes1);
   colormap(map);
   for i = 2:N+1
       for j = 2:N+1
           guy = pop(i,j);
           if guy == 0
               continue;
           elseif guy >= dieAfter+2
               continue;
           elseif guy > 1 && guy < latentUntil
               newpop(i,j) = guy + 1;
           elseif guy >= latentUntil
               if rand < healRate
                   newpop(i,j) = 0;
               else
                   newpop(i,j) = guy + 1;
               end
           elseif guy == 1
               newpop(i,j) = 1 + (sum(sum((rand(3)<infectionRate)...
                   & (pop(i-1:i+1,j-1:j+1) > 1)...
                   & (pop(i-1:i+1,j-1:j+1) < dieAfter + 2))) >= 1);
           end
       end
   end
   pop = newpop;

% --- Executes on button press in resetButton.
function resetButton_Callback(hObject, eventdata, handles)
% hObject    handle to resetButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global infectionRate healRate initialProbInfection dieAfter pop newpop;

stop(handles.tmr);

N = 100;

% initialProbInfection = 0.001;
% infectionRate = 0.1;
% healRate = 0.05;
% dieAfter = 10;
% latentUntil = 4;


pop = zeros(N+2);
pop(2:N+1,2:N+1) = ones(N) + (rand(N)<initialProbInfection);
pop(1,1) = dieAfter+2;
newpop = pop;

%imagesc(pop);
imagesc(pop,'Parent',handles.axes1);
map = zeros(dieAfter+2,3);
map(1,:)= [0 1 0];
map(2,:)= [1 1 1];
map(3:end,1) = flipud(linspace(1,0,dieAfter));
colormap(map);

% --- Executes on slider movement.
function infectionSlider_Callback(hObject, eventdata, handles)
% hObject    handle to infectionSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global infectionRate;
infectionRate = get(hObject,'Value');

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function healSlider_Callback(hObject, eventdata, handles)
% hObject    handle to healSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global healRate;
healRate = get(hObject,'Value');

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function initialRateSlider_Callback(hObject, eventdata, handles)
% hObject    handle to initialRateSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global initialProbInfection;
initialProbInfection = get(hObject,'Value');

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function dieAfterSlider_Callback(hObject, eventdata, handles)
% hObject    handle to dieAfterSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global dieAfter;
dieAfter = round(get(hObject,'Value'));

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
