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

% Last Modified by GUIDE v2.5 24-Oct-2013 07:17:16

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

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

set(handles.nSlider,'Min',10,'Max',30,'Sliderstep',[1/20 .1])
set(handles.nSlider,'Value',15)
set(handles.nEdit,'String',sprintf('%2d',get(handles.nSlider,'Value')))

set(handles.pSlider,'Value',.50);
set(handles.pEdit,'String',sprintf('%4.2f',get(handles.pSlider,'Value')))

set(handles.TSlider,'Min',.5,'Max',4.0,'Sliderstep',[1/35 .1],'Value',1.5);
set(handles.TEdit,'String',sprintf('%4.2f',get(handles.TSlider,'Value')))

[handles.A,handles.H] = SetUp(get(handles.nSlider,'Value'),get(handles.pSlider,'Value'));
% Update handles structure
guidata(hObject, handles);

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = IsingGUI_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 slider movement.
function nSlider_Callback(hObject, eventdata, handles)
% hObject handle to nSlider (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

set(handles.nSlider,'Value',round(get(handles.nSlider,'Value')))
set(handles.nEdit,'String',sprintf('%2d',get(handles.nSlider,'Value')))
[handles.A,handles.H] = SetUp(get(handles.nSlider,'Value'),get(handles.pSlider,'Value'));
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function nSlider_CreateFcn(hObject, eventdata, handles)
% hObject handle to nSlider (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 pSlider_Callback(hObject, eventdata, handles)
% hObject handle to pSlider (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

set(handles.pEdit,'String',sprintf('%4.2f',get(handles.pSlider,'Value')))
[handles.A,handles.H] = SetUp(get(handles.nSlider,'Value'),get(handles.pSlider,'Value'));
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function pSlider_CreateFcn(hObject, eventdata, handles)
% hObject handle to pSlider (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

function nEdit_Callback(hObject, eventdata, handles)
% hObject handle to nEdit (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 nEdit as text
% str2double(get(hObject,'String')) returns contents of nEdit as a double

% --- Executes during object creation, after setting all properties.
function nEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to nEdit (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 pEdit_Callback(hObject, eventdata, handles)
% hObject handle to pEdit (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 pEdit as text
% str2double(get(hObject,'String')) returns contents of pEdit as a double

% --- Executes during object creation, after setting all properties.
function pEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to pEdit (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 [A,H] = SetUp(n,p)
R = rand(n,n);
A = (R>p)-(R<=p);
fill([1 n+1 n+1 1],[1 1 n+1 n+1],'k')
axis equal off
hold on
for i=1:n
for j = 1:n
if A(i,j)==1
H(i,j) = fill([j j+1 j+1 j],[i i i+1 i+1],'c');
else
H(i,j) = fill([j j+1 j+1 j],[i i i+1 i+1],'m');
end
end
end
hold off

function A = Metro(A,H,N)
[n,n] = size(A);
step=10;
for k=1:N
i = ceil(n*rand(1));
j = ceil(n*rand(1));
if i>1
North = A(i-1,j);
else
North = A(n,j);
end
if i<n
South = A(i+1,j);
else
South = A(1,j);
end
if j>1
West = A(i,j-1);
else
West = A(i,n);
end
if j<n
East = A(i,j+1);
else
East = A(i,1);
end
EP = A(i,j)*(North+East+South+West);
T = 1;
if EP<0 || rand(1) < exp(-2*EP/T)
A(i,j) = -A(i,j);
end
if rem(k,step)==0
if A(i,j)==1;
set(H(i,j),'facecolor',[0 1 1]);
else
set(H(i,j),'facecolor',[1 0 1]);
end
end
end

% --- Executes on button press in NewButton.
function NewButton_Callback(hObject, eventdata, handles)
% hObject handle to NewButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[handles.A,handles.H] = SetUp(get(handles.nSlider,'Value'),get(handles.pSlider,'Value'));
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in StartButton.
function StartButton_Callback(hObject, eventdata, handles)
% hObject handle to StartButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

n = get(handles.nSlider,'Value');
T = get(handles.TSlider,'Value');
N = 10*n^2;
for k=1:N
i = ceil(n*rand(1));
j = ceil(n*rand(1));
if i>1
North = handles.A(i-1,j);
else
North = handles.A(n,j);
end
if i<n
South = handles.A(i+1,j);
else
South = handles.A(1,j);
end
if j>1
West = handles.A(i,j-1);
else
West = handles.A(i,n);
end
if j<n
East = handles.A(i,j+1);
else
East = handles.A(i,1);
end
EP = handles.A(i,j)*(North+East+South+West);
T = 1;
if EP<0 || rand(1) < exp(-2*EP/T)
handles.A(i,j) = -handles.A(i,j);
end

if handles.A(i,j)==1;
set(handles.H(i,j),'facecolor',[0 1 1]);
else
set(handles.H(i,j),'facecolor',[1 0 1]);
end
pause(.01)

end
guidata(hObject, handles);

% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over StartButton.

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

% --- Executes on slider movement.
function TSlider_Callback(hObject, eventdata, handles)
% hObject handle to TSlider (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

set(handles.TEdit,'String',sprintf('%4.2f',get(handles.TSlider,'Value')))

[handles.A,handles.H] = SetUp(get(handles.nSlider,'Value'),get(handles.pSlider,'Value'));
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function TSlider_CreateFcn(hObject, eventdata, handles)
% hObject handle to TSlider (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

function TEdit_Callback(hObject, eventdata, handles)
% hObject handle to TEdit (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 TEdit as text
% str2double(get(hObject,'String')) returns contents of TEdit as a double

% --- Executes during object creation, after setting all properties.
function TEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to TEdit (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