function varargout = GUI_1_MW_of_UCNPs(varargin)

% A GRAPHICAL USER INTERFACE TO CALCULATE THE MOLECULAR WEIGHT OF NaYF4:RE
% LUMINESCENCE UP-CONVERSION NANOPARTICLES

% RUN SCRIPT TO OPEN GUI

% CREATED BY DR LEWIS MACKENZIE. © Lewis MacKenzie 2017.
% www.LewisMacKenzie.science
% e: L.MacKenzie1@Leeds.ac.uk

% PLEASE CITE 'MACKENZIE ET AL., (2017) "The molecular weight of NaYF4:RE
% photonic up-conversion nanoparticles".

% Last Modified by GUIDE v2.5 07-Mar-2017 12:01:02

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @GUI_1_MW_of_UCNPs_OpeningFcn, ...
    'gui_OutputFcn',  @GUI_1_MW_of_UCNPs_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 GUI_1_MW_of_UCNPs is made visible.
function GUI_1_MW_of_UCNPs_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for GUI_1_MW_of_UCNPs
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

%grey-out the hexagonal lattice parameters at start-up
set(handles.Lattice_c, 'Visible', 'off');
set(handles.text18, 'Visible', 'off');
grid on
grid minor
xlabel('Nanoparticle charactaristic size (nm)');
ylabel('Molecular weight (MDa)')
 %set(gcf, 'units', 'normalized', 'position', [0.05 0.15 0.45 0.55]) %set window size
set(gcf,'position',get(0,'screensize'))
% UIWAIT makes GUI_1_MW_of_UCNPs wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = GUI_1_MW_of_UCNPs_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Text input to specify lattice a parameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Lattice_a_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function Lattice_a_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Text input to specify hexagonal lattice c parameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Lattice_c_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function Lattice_c_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Text input to specify MIN DIAMETER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function min_UCNP_diameter_INPUT_Callback(hObject, eventdata, handles)

function min_UCNP_diameter_INPUT_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Text input to specify MAX DIAMETER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function max_UCNP_diameter_INPUT_Callback(hObject, eventdata, handles)

function max_UCNP_diameter_INPUT_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Text input to specify % of Er
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function PcEr_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function PcEr_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Text input to specify % of Yb
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function PcYb_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function PcYb_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Text input to specify % of Gd
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in GdPc.
function GdPc_Callback(hObject, eventdata, handles)

function GdPc_CreateFcn(hObject, eventdata, handles)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     Pop up menu to specify cubic or hexagonal lattice parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on selection change in CubicORHex.
function CubicORHex_Callback(hObject, eventdata, handles)

%if a cubic, then grey out some options for heaxgonal lattice parameters
switch get(hObject,'Value')
    case 1
        set(handles.Lattice_c, 'Visible', 'off');
       set(handles.text18, 'Visible', 'off');
    case 2
        set(handles.Lattice_c, 'Visible', 'on');
         set(handles.text18, 'Visible', 'on');
end

% --- Executes during object creation, after setting all properties.
function CubicORHex_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Radio button to specify if graphs are held
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in HoldPlotButton.
function HoldPlotButton_Callback(hObject, eventdata, handles)



%%%%%%%%%%%%%%%%%%%%%%%%%%
%     CLICK BUTTON TO PLOT GRAPH
%%%%%%%%%%%%%%%%%%%%%%%%%%

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

%get lattice constant inputs
lattice_a=str2num(get(handles.Lattice_a,'String'));
lattice_c=str2num(get(handles.Lattice_c,'String'));

%get UCNP diameter inputs
maxd=str2num(get(handles.max_UCNP_diameter_INPUT,'String'));
mind=str2num(get(handles.min_UCNP_diameter_INPUT,'String'));
d_range=[mind:0.01:maxd]; %nanoparticle size range

%get graph hold option info
holdplotoption=get(handles.HoldPlotButton,'Value')

%get parameter specifying cubic (t=1) or hex (t=2) lattices
t=get(handles.CubicORHex,'value')
grid minor
%if cubic lattice
if t==1
    cubic=1
    hex=0;
    
    disp('CUBIC LATTICE')
    
    unit_cell_volume=(lattice_a/10)^3; %calculate unit cell volume in nm^3
    
    %number of atoms in unit cell - CUBIC
    number_Na=2;
    number_F=8;
    number_lanthanides=2;
        grid minor
    %if hexagonal lattice
    
    elseif t ==2
    cubic=0
    hex=1
    disp('HEXAGONAL LATTICE')
    unit_cell_volume=2*(sqrt(3))/4*(lattice_a/10)^2*lattice_c/10; %calculate unit cell volume in nm^3
    number_Na=1.5;
    number_F=6;
    number_lanthanides=1.5;
    grid minor
end

%get the % of dopants
dopedErPc=str2num(get(handles.PcEr,'String'));
dopedYbPc=str2num(get(handles.PcYb,'String'));
dopedGdPc=str2num(get(handles.GdPc,'String'));

%convert % to fraction
dopedErPcf=dopedErPc/100;
dopedYbPcf=dopedYbPc/100;
dopedGdPcf=dopedGdPc/100;

%compute doping factor
doping_factor=dopedErPcf+dopedYbPcf+dopedGdPcf;

%doping factor warning box
if doping_factor>1
    msgbox('Warning: unphysical doping factors. Make sure dopants total < 100%')
    return
end

%calculate UCNP volume in nm^3
np_volumes=(4/3)*pi*(d_range/2).^3;

%calculate number of units cells in volume
n_unit_cells=np_volumes./unit_cell_volume;


%atomic weight of each element in UCNP
F_atomic_weight=18.998403163;
Na_atomic_weight=22.98976928;
Y_atomic_weight=88.90584;
Er_atomic_weight=167.259;
Yb_atomic_weight=173.054;
Gd_atomic_weight=157.25;

%calculate molecular weight of a unit cell
unit_cell_weight=(number_Na*Na_atomic_weight)+(number_F*F_atomic_weight)+((1-doping_factor)*number_lanthanides*Y_atomic_weight)+(number_lanthanides*Er_atomic_weight*dopedErPcf)+(number_lanthanides*Yb_atomic_weight*dopedYbPcf)+(number_lanthanides*Gd_atomic_weight*dopedGdPcf);

%calculate total weight of UCNP
np_weight=n_unit_cells*unit_cell_weight;

% 
%clear or hold graph, as desired
if holdplotoption==1
    hold  on
    grid on
    grid minor %keep grid minor
elseif holdplotoption==0
    cla reset; % clear plots
    grid on
    grid minor %keep grid minor
end

%plot graph
plot(d_range,np_weight/1000000,'LineWidth',2)

%throw up a title
if t==1
    title('Cubic lattice')
elseif  t==2
    title('Hexagonal lattice')
end

%misc graph stuff
grid on
grid minor
xlabel('UCNP diameter (nm)')
ylabel('Molecular weight (MDa)')


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


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text10.
% function text10_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to text10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when figure1 is resized.
function figure1_SizeChangedFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
