function f = pottsGUI
global T BB

f = figure('Visible','on','Position',[50,50,1100,600]);


%%set figure axis 
ha = axes('Parent',f, ...
	'Units','pixels', ...
	'Color',[1 1 1], ...
	'Position',[50 40 500 500], ...
	'Tag','Axes1', ...
	'XColor',[0 0 0], ...
	'YColor',[0 0 0]);

Ea = axes('Parent',f, ...
	'Units','pixels', ...
	'Color',[1 1 1], ...
	'Position',[600 275 450 250], ...
	'Tag','Axes1', ...
    'xlim',[0 300], ...
	'XColor',[0 0 0], ...
	'YColor',[0 0 0]);
xlabel(Ea,'Time Step','fontsize',12,'fontweight','b');
ylabel(Ea,'Energy per Spin','fontsize',12,'fontweight','b');

%%Start Simulation Pushbutton
startSim = uicontrol('Parent',f, ...
    'Style','pushbutton', ...
    'String','Start Simulation', ...
    'Position',[850 80 150 20], ...
	'BackgroundColor',[0 1 0], ...
    'FontWeight','bold', ...
	'Tag','NotStarted', ...
	'TooltipString','start simulation',...
    'Callback',{@startSim_Callback});

%%Stop Simulation Pushbutton
stopSim = uicontrol('Parent',f, ...
    'Style','pushbutton', ...
    'String','Stop Simulation', ...
    'Position',[850 50 150 20], ...
	'BackgroundColor',[0 1 0], ...
    'FontWeight','bold', ...
	'Tag','IsStopped',...
	'TooltipString','Stop Simulation',...
    'Callback',{@stopSim_Callback});

%%%Various boxes and text
LatticeSizeText = uicontrol('parent',f,'Style','text','String','Lattice Size:',...
    'Position',[600,50,100,30],...
    'FontWeight','bold',...
    'FontSize',12);      

QText = uicontrol('parent',f,'Style','text','String','q value:',...
    'Position',[600,80,80,30],...
    'FontWeight','bold',...
    'FontSize',12);      

TempText = uicontrol('parent',f,'Style','text','String','Temperature (0 to 3):',...
    'Position',[600,140,140,30],...
    'FontWeight','bold',...
    'FontSize',12);   

CriticalTempText = uicontrol('parent',f,'Style','text','String','Tcrit:',...
    'Position',[600,170,140,30],...
    'FontWeight','bold',...
    'FontSize',12);  

CriticalTemp = uicontrol('parent',f,'Style','text','String','0.852',...
    'Position',[750,170,80,30],...
    'FontWeight','bold',...
    'FontSize',12);   


TempValueText = uicontrol('parent',f,'Style','text','String','1',...
    'Position',[750,140,100,30],...
    'FontWeight','bold',...
    'FontSize',12);  

%%Control temp With A Slider      
temperature = uicontrol('parent',f, ...
    'Units','pixels', ...
    'Tag','temperature',...
    'Style','Slider', ...
    'TooltipString','Temperature',...
    'Position', [600 110 400 20], ...
    'FontWeight','bold',...
    'FontSize',10,...
    'Callback',{@temperature_Callback},...
    'Min',0,'Max',3,'Value',1);

qInput = uicontrol('Parent',f,...
    'Style','Edit',...
    'String','5',...
    'Position',[730 80 80 30],...
    'Tag','%%%%%%%%%',...
    'fontsize',12,...
    'Callback',{@qInput_Callback});

LInput = uicontrol('Parent',f,...
    'Style','Edit',...
    'String','150',...
    'Position',[730 50 80 30],...
    'Tag','%%%%%%%%%',...
    'fontsize',12);




%% function that executes when start button is pressed
function startSim_Callback(source,eventdata)
    q = get(qInput,'String');
    T = get(temperature,'Value');
    L = get(LInput,'String');
    
q = eval(q);
L = eval(L);
      
M=[];

%coupling usually always normalized to 1
J = 1;

%initial state
grid = randint(L,L,q)+1;


BB = 0;
for tt = 1:1000;

  %%logic to kill run if stop button pressed  
  if BB==1;
      break
  end


%%Choose random possibility for each spin in lattice to flip to 
%%(excluding flipping to itself), calculate local change in energy 
%%for each spin if it does flip holding spins around it constant, 
%%calulate bolztman weight by change in energy, and perform metropolis
%%decision
rr = randint(L,L,q-1)+1;
grid2 = (rr<grid).*rr + ((rr==grid) + (rr>grid)).*(rr+1);
E1 = (grid==circshift(grid, [ 0 1])) + (grid==circshift(grid, [ 0 -1])) + (grid==circshift(grid, [ 1 0])) + (grid==circshift(grid, [-1 0]));
E2 = (grid2==circshift(grid, [ 0 1])) + (grid2==circshift(grid, [ 0 -1])) + (grid2==circshift(grid, [ 1 0])) + (grid2==circshift(grid, [-1 0]));
DeltaE = -J * (E2 - E1);

%%metropolis decision
p_trans = exp(-DeltaE/(T));

%%execute sping flips according to metropolis decision
R = rand(L);
transitions = (R < p_trans ) .* grid2 + (R > p_trans ) .* grid;

%%hold sublattice 1 constant,ie, set it back to original values
C = zeros(L,1);
C(1:2:length(C))=1;
toe = toeplitz(C,C);
pos = find(toe==1);
transitions(pos)=grid(pos);

%%spin lattice after one sublattice has flipped according to metropolis
%%decision
grid = transitions;



%%repeat process holding sublattice 2 fixed 
rr = randint(L,L,q-1)+1;
grid2 = (rr<grid).*rr + ((rr==grid) + (rr>grid)).*(rr+1);
E1 = (grid==circshift(grid, [ 0 1])) + (grid==circshift(grid, [ 0 -1])) + (grid==circshift(grid, [ 1 0])) + (grid==circshift(grid, [-1 0]));
E2 = (grid2==circshift(grid, [ 0 1])) + (grid2==circshift(grid, [ 0 -1])) + (grid2==circshift(grid, [ 1 0])) + (grid2==circshift(grid, [-1 0]));
DeltaE = -J * (E2 - E1);

p_trans = exp(-DeltaE/(T));

R = rand(L);
transitions = (R < p_trans ) .* grid2 + (R > p_trans ) .* grid;

C = zeros(L,1);
C(2:2:length(C)-1)=1;
toe = toeplitz(C,C);
pos = find(toe==1);
transitions(pos)=grid(pos);

%%spin lattice after both sublattices have flipped according to metropolis
%%decision
grid = transitions;

        %%update picture of lattice
        image(grid,'CDataMapping','scaled','Parent',ha);
        
        %%energy
        Esites = ( (grid==circshift(grid, [ 0 1])) + (grid==circshift(grid, [ 0 -1])) + (grid==circshift(grid, [ 1 0])) + (grid==circshift(grid, [-1 0])) );
        E = sum(sum(J.*Esites))/(2*L^2);
       
        %%magnetization
        for j=1:q
            B(j)=length(find(grid==j));
        end

        M = (max(B)-(L^2-max(B))/(q-1))/L^2;
            
      
        %%plots energy in Ea axes as simulation runs
        if mod(tt,35)==0 && tt>130
        set(Ea,'xlim',[tt-80 tt+50]);
        end
        plot(Ea,tt,E,'b.','markersize',18);  
        xlabel(Ea,'Time Step','fontsize',12,'fontweight','b');
        ylabel(Ea,'Energy per Spin','fontsize',12,'fontweight','b');
        hold on 
        drawnow;
             
                
end
end


%%function that executes ever time temp slider is changed. T is 
%%changed as a global variable and its value displayed
function temperature_Callback(source,eventdata)
  
    T = get(temperature,'Value');
    set(TempValueText,'String',T);
    
end

%%dislplays value of Tc for given q
function qInput_Callback(source,eventdata)
    Tc = get(qInput,'String');
    Tc=1/log(sqrt(eval(Tc))+1);
    set(CriticalTemp,'String',Tc);
end


%%function that executes when stop button pressed
function stopSim_Callback(source,eventdata)
    BB = 1;
    
    cla(Ea);

    set(Ea,...
	'Units','pixels', ...
	'Color',[1 1 1], ...
	'Position',[600 275 450 250], ...
	'Tag','Axes1', ...
    'xlim',[0 300], ...
	'XColor',[0 0 0], ...
	'YColor',[0 0 0]);
    hold off

end


end