function varargout = Umiu(varargin)
% UMIU M-file for Umiu.fig
%      UMIU, by itself, creates a new UMIU or raises the existing
%      singleton*.
% Last Modified by GUIDE v2.5 23-Jan-2011 23:22:58

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Umiu_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;

% Callbacks-----------------------------------------------------------
function mnuFile_Callback(hObject, eventdata, handles)
% hObject    handle to mnuFile (see GCBO)

% --------------------------------------------------------------------
function mnuExit_Callback(hObject, eventdata, handles)
close % This Application

% --------------------------------------------------------------------
function mnuNewUmiu_Callback(hObject, eventdata, handles)
% Create new Umiu Types for choosen Bstruct
global InfTable 
global  UqType iUqType Ucells Qtypes AtomMasses

[fname, pname] = uigetfile('*.mat','Open','Bstruct.mat');
if pname == 0, return, end

update_ini(pname);

fullname = strcat(pname,fname);
load(fullname);
handles.pname = pname;
guidata(gcbo, handles);

show_InfTable(handles);

init_uq
matrb(handles)
form_utypes(handles)

set(handles.mnuSaveUmiu,'Enable','on');
set(handles.mnuEditUMiu,'Enable','on');
set(handles.mnuUsparse,'Enable','on');
set(handles.mnuSaveMiu,'Enable','on');

pname = handles.pname;
fullname = strcat(pname,'Umiu.mat');
file = fullname;
save(file, 'UqType', 'iUqType', 'Ucells', 'Qtypes', 'AtomMasses'); 

fname = 'u_list.txt';
fullname = strcat(pname,fname);
fid = fopen(fullname,'w');
n = length(Ucells);
for i = 1:n
    c = Ucells{1,i};
    fprintf(fid,'%s \n',c);
end % i
fclose(fid);

% --------------------------------------------------------------------
function mnuOpenUmiu_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds 
global Alphas Gammas Ros His iUqType UqType  Qtypes Ucells AtomMasses

[fname, pname] = uigetfile('*.mat','Open','Bstruct.mat');
if pname == 0, return, end

update_ini(pname);

fullname = strcat(pname,fname);
load(fullname);
handles.pname = pname;
guidata(gcbo, handles);
matrb(handles)

fullname = strcat(pname,'Umiu.mat');
load(fullname);
set(handles.mnuEditUMiu,'Enable','on');
set(handles.mnuSaveUmiu,'Enable','on');
set(handles.mnuUsparse,'Enable','on');
set(handles.mnuSaveMiu,'Enable','on');

set(handles.listUtypes, 'String', Ucells);
set(handles.listUtypes, 'Value', 1);

show_InfTable(handles);

% --------------------------------------------------------------------
function mnuSaveUmiu_Callback(hObject, eventdata, handles)
global UqType iUqType Ucells Qtypes AtomMasses
pname = handles.pname;
fullname = strcat(pname,'Umiu.mat');
file = fullname;
save(file, 'UqType', 'iUqType', 'Ucells', 'Qtypes', 'AtomMasses');  

fname = 'u_list.txt';
fullname = strcat(pname,fname);
fid = fopen(fullname,'w');
n = length(Ucells);
for i = 1:n
    c = Ucells{1,i};
    fprintf(fid,'%s \n',c);
end % i
fclose(fid);

% --------------------------------------------------------------------
function mnuUsparse_Callback(hObject, eventdata, handles)
% Transfer UqType to UqSparse.txt
global InfTable Bonds 
global Alphas Gammas Ros His UqType  Qtypes Bx By Bz

pname = handles.pname;
fullname = strcat(pname,'Uqsparse.txt');
fid = fopen(fullname,'w');

nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
n = InfTable(7);
for i = 1:n
    iu = i;
    Qtyp1 = Qtypes(iu);
    switch Qtyp1
        case 'q'
            Atoms1 = [Bonds(iu,1),Bonds(iu,2)];
        case 'a'
            ind1 = iu - nBonds;  
            Atoms1 = [Alphas(ind1,1),Alphas(ind1,2),Alphas(ind1,3)];
        case 'g'
            ind1 = iu - (nBonds+nAlphas);  
            Atoms1 = [Gammas(ind1,1),Gammas(ind1,2),Gammas(ind1,3)];
        case 'r'
            ind1 = iu - (nBonds+nAlphas+nGammas);  
            Atoms1 = [Ros(ind1,1),Ros(ind1,2),Ros(ind1,3),Ros(ind1,4)];
        case 'h'
            ind1 = iu - (nBonds+nAlphas+nGammas+nRos);  
            Atoms1 = [His(ind1,1),His(ind1,2),His(ind1,3), ...
                    His(ind1,7),His(ind1,8),His(ind1,9)];
    end %  switch Qtyp1

    for j = i:n
        ju = j;
        Qtyp2 = Qtypes(ju);
        switch Qtyp2
            case 'q'
                Atoms2 = [Bonds(ju,1),Bonds(ju,2)];
            case 'a'
                ind2 = ju - nBonds;  
                Atoms2 = [Alphas(ind2,1),Alphas(ind2,2),Alphas(ind2,3)];
            case 'g'
                ind2 = ju - (nBonds+nAlphas);  
                Atoms2 = [Gammas(ind2,1),Gammas(ind2,2),Gammas(ind2,3)];
            case 'r'
                ind2 = ju - (nBonds+nAlphas+nGammas);  
                Atoms2 = [Ros(ind2,1),Ros(ind2,2),Ros(ind2,3),Ros(ind2,4)];
            case 'h'
                ind2 = ju - (nBonds+nAlphas+nGammas+nRos);  
                Atoms2 = [His(ind2,1),His(ind2,2),His(ind2,3), ...
                        His(ind2,7),His(ind2,8),His(ind2,9)];
        end %  switch Qtyp2
        [M1,rc1] = mass_centrum(Atoms1);
        [M2,rc2] = mass_centrum(Atoms2);
        sample.QType1 = Qtyp1;
        sample.QType2 = Qtyp2;
        sample.M = M1 + M2;
        sample.R12 = norm(rc2 - rc1);
        Atoms = [Atoms1,Atoms2];
        tenz = tenzor(Atoms);
        sample.t1 = tenz(1);
        sample.t2 = tenz(2);
        sample.t3 = tenz(3);
        sample.diag = 0;
        if iu == ju
            sample.diag = 1;
        end % if
        i1 = Atoms1(2);
        i2 = Atoms2(2);
        sample.csn = [Bx(i,i1), By(i,i1), Bz(i,i1)]* ...
                     [Bx(j,i2), By(j,i2), Bz(j,i2)]';
        sample.csn = abs(sample.csn);         
        % check iType coincide
        [coincide, uTypeID] = check_type(sample);
        if coincide
            u = UqType(uTypeID).u;
            if abs(u) < 0.0001, continue,end % if 
            fprintf(fid,'%4i %4i %9.5f \n',iu, ju, u);
        end % if coincide
    end % j
end % i
fclose(fid);

% --------------------------------------------------------------------
function mnuSaveMiu_Callback(hObject, eventdata, handles)
% Transfer UqType to Miu.txt and dmdqSparse.txt
global InfTable Bonds Alphas Gammas Ros His 
global UqType  Qtypes Bx By Bz AtomMasses

pname = handles.pname;
fullname = strcat(pname,'dmdqSparse.txt');
fiddmdq = fopen(fullname,'w');
nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
n = InfTable(7);

Miu = zeros(1,nBonds);
ratio_1_2 = zeros(1,length(UqType));
Qtyp1 = 'q';
for i = 1:nBonds
    iu = i;
    Atoms1 = [Bonds(iu,1),Bonds(iu,2)];
    % is the bond symmetrical?
    a1 = Atoms1(1);
    a2 = Atoms1(2);
    symmetrical = 0;
    if AtomMasses(a1) == AtomMasses(a2),symmetrical = 1;end
    if symmetrical
        % try envirements of the two atoms
        M1 = 0.0;
        M2 = 0.0;
        for k = 1:nBonds
            if Bonds(k,1) == a1,M1 = M1 + AtomMasses(Bonds(k,2));end
            if Bonds(k,2) == a1,M1 = M1 + AtomMasses(Bonds(k,1));end
            if Bonds(k,1) == a2,M2 = M2 + AtomMasses(Bonds(k,2));end
            if Bonds(k,2) == a2,M2 = M2 + AtomMasses(Bonds(k,1));end
        end % k
        if M1 ~= M2,symmetrical = 0;end 
    end % if symmetrical
%     if symmetrical, continue, end
    for j = i:n
        ju = j;
        Qtyp2 = Qtypes(ju);
        switch Qtyp2
            case 'q'
                Atoms2 = [Bonds(ju,1),Bonds(ju,2)];
            case 'a'
                ind2 = ju - nBonds;  
                Atoms2 = [Alphas(ind2,1),Alphas(ind2,2),Alphas(ind2,3)];
            case 'g'
                ind2 = ju - (nBonds+nAlphas);  
                Atoms2 = [Gammas(ind2,1),Gammas(ind2,2),Gammas(ind2,3)];
            case 'r'
                ind2 = ju - (nBonds+nAlphas+nGammas);  
                Atoms2 = [Ros(ind2,1),Ros(ind2,2),Ros(ind2,3),Ros(ind2,4)];
            case 'h'
                ind2 = ju - (nBonds+nAlphas+nGammas+nRos);  
                Atoms2 = [His(ind2,1),His(ind2,2),His(ind2,3), ...
                        His(ind2,7),His(ind2,8),His(ind2,9)];
        end %  switch Qtyp2
        [M1,rc1] = mass_centrum(Atoms1);
        [M2,rc2] = mass_centrum(Atoms2);
        sample.QType1 = Qtyp1;
        sample.QType2 = Qtyp2;
        sample.M = M1 + M2;
        sample.R12 = norm(rc2 - rc1);
        Atoms = [Atoms1,Atoms2];
        tenz = tenzor(Atoms);
        sample.t1 = tenz(1);
        sample.t2 = tenz(2);
        sample.t3 = tenz(3);
        sample.diag = 0;
        if iu == ju
            sample.diag = 1;
        end % if
        i1 = Atoms1(2);
        i2 = Atoms2(2);
        sample.csn = [Bx(i,i1), By(i,i1), Bz(i,i1)]* ...
                     [Bx(j,i2), By(j,i2), Bz(j,i2)]';
        sample.csn = abs(sample.csn);         
        % check iType coincide
        [coincide, uTypeID] = check_type(sample);
        if coincide
            swapEOP = 0;
            % prepare ratio for Qtyp2 = 'q'
            if Qtyp2 == 'q'
                [r,s1] = atom_pair_vector(Atoms1(1), Atoms1(2));
                [r,s2] = atom_pair_vector(Atoms2(1), Atoms2(2));
                ratio = s1/s2;
                if ratio_1_2(uTypeID) == 0
                    ratio_1_2(uTypeID) = ratio;
                else
                   if abs(ratio_1_2(uTypeID) - ratio) > 0.001, ...
                           swapEOP = 1;end
                end % if ratio_1_2 == 0
            end % if Qtyp2 == 'q'
            miu = UqType(uTypeID).miu;
            if ~swapEOP
                dm1dq2 = UqType(uTypeID).dm1dq2;
                dm2dq1 = UqType(uTypeID).dm2dq1;
            else
                dm1dq2 = UqType(uTypeID).dm2dq1;
                dm2dq1 = UqType(uTypeID).dm1dq2;
            end % if ~swapEOP
            if symmetrical
                miu = 0.0;
                dm1dq2 = 0.0;
            end % if symmetrical
            if sample.diag
                Miu(i) = miu;
                if abs(dm1dq2) > 0.0001, ...
                 fprintf(fiddmdq,'%4i %4i %9.5f \n',iu,ju,dm1dq2);end
            end % diag
            if (~sample.diag)&&(Qtyp2=='q')
                if (abs(dm1dq2) > 0.0001), ...
                  fprintf(fiddmdq,'%4i %4i %9.5f \n',iu, ju, dm1dq2);end
                if ~isnan(dm2dq1)&&(abs(dm2dq1) > 0.0001), ...
                  fprintf(fiddmdq,'%4i %4i %9.5f \n',ju, iu, dm2dq1);end
                if isnan(dm2dq1), ...
                  fprintf(fiddmdq,'%4i %4i %9.5f \n',ju, iu, dm1dq2);end 
            end % Qtyp2=='q'
            if (~sample.diag)&&(Qtyp2~='q')&&(abs(dm1dq2) > 0.0001)
                fprintf(fiddmdq,'%4i %4i %9.5f \n',iu, ju, dm1dq2);
            end % Qtyp2~='q'
        end % if coincide
    end % j = i:n    
end % i = 1:nBonds
fclose(fiddmdq);

fullname = strcat(pname,'Miu.txt');
fidMiu = fopen(fullname,'w');
fprintf(fidMiu,'%9.5f \n',Miu);
fclose(fidMiu);

% --------------------------------------------------------------------
function ednAtoms_Callback(hObject, eventdata, handles)

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

function ednBonds_Callback(hObject, eventdata, handles)

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

function ednAlphas_Callback(hObject, eventdata, handles)

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

function ednGammas_Callback(hObject, eventdata, handles)

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

function ednRos_Callback(hObject, eventdata, handles)

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

function ednHis_Callback(hObject, eventdata, handles)

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

function ednVibrCoords_Callback(hObject, eventdata, handles)

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

function ednBlocks_Callback(hObject, eventdata, handles)

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

% --- Executes on button press in btnViewBonds.
function btnViewBonds_Callback(hObject, eventdata, handles)
% View Model's bonds only within MatLab
global Type InfTable AtomTypes AtomCoords Bonds 

handles.Hf = figure;
guidata(gcbo, handles);
set(handles.btnViewBonds, 'Enable', 'off');
set(handles.btnCloseView, 'Enable', 'on');

Coords = zeros(2,3);
nBonds = InfTable(2);
for iBond = 1:nBonds
    a1 = Bonds(iBond,1);
    a2 = Bonds(iBond,2);
    Coords(1,:) = AtomCoords(a1,:);
    Coords(2,:) = AtomCoords(a2,:);
    plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'b-o','MarkerSize',12)
    hold on
end % iBond
xlabel('x')
ylabel('y')
zlabel('z')
grid on

nAtoms = InfTable(1);
hold on
for i = 1:nAtoms
    % is isolated?
    isolated = 1;
    for iBond = 1:nBonds
        if (iBond == Bonds(iBond,1))||(iBond == Bonds(iBond,2))
            isolated = 0;
            break
        end % if 
    end % iBond
    if isolated
        Coords(1,:) = AtomCoords(i,:);
     plot3(Coords(1,1),Coords(1,2),Coords(1,3),'bo','MarkerSize',12)
    end % if
    num = [Type(AtomTypes(i)).Symbol,int2str(i)];
    r = AtomCoords(i,:);
    text('String',num, 'Position',r);
end % i Atoms

% --- Executes on button press in btnCloseView.
function btnCloseView_Callback(hObject, eventdata, handles)
delete(handles.Hf);
set(handles.btnViewBonds, 'Enable', 'on');
set(handles.btnCloseView, 'Enable', 'off');

% --------------------------------------------------------------------
function mnuEditUMiu_Callback(hObject, eventdata, handles)

% --------------------------------------------------------------------
function mnuCorrectUMiu_Callback(hObject, eventdata, handles)
set(handles.listUtypes, 'Enable', 'on');
set(handles.btnEditU, 'Enable', 'on');
set(handles.edU, 'Enable', 'on');
set(handles.btnEditMiu, 'Enable', 'on');
set(handles.edMiu, 'Enable', 'on');
set(handles.btnEditdmdq, 'Enable', 'on');
set(handles.eddmdq, 'Enable', 'on');
set(handles.btnEditdm2dq1, 'Enable', 'on');
set(handles.eddm2dq1, 'Enable', 'on');
set(handles.btnDeleteU, 'Enable', 'on');

% --- Executes on selection change in listUtypes.
function listUtypes_Callback(hObject, eventdata, handles)
    set(handles.edU,'String','');
    set(handles.edMiu,'String','');
    set(handles.eddmdq,'String','');

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

% --- Executes on button press in btnEditU.
function btnEditU_Callback(hObject, eventdata, handles)
global UqType Ucells
t = get(handles.listUtypes,'Value');
set(handles.listUtypes,'Enable', 'off');
content = Ucells{1, t};
[tok,rem] = strtok(content);
iu = tok;
[tok,rem] = strtok(rem);
ju = tok;
[tok,rem] = strtok(rem);
if strcmp(get(hObject,'String'),'Edit u')
    set(handles.edU,'String',tok);
    set(hObject,'String','OK');
else
    toknew = get(handles.edU,'String');
    UqType(t).u = str2double(toknew);
    content = sprintf(' %s %s %s %s',iu,ju,toknew,rem);
    Ucells(1, t) = {content};
    set(handles.listUtypes, 'String', Ucells);
    set(handles.listUtypes, 'Value', t);
    set(handles.listUtypes,'Enable', 'on');
    set(hObject,'String','Edit u');
end % if

% --- Executes on button press in btnEditMiu.
function btnEditMiu_Callback(hObject, eventdata, handles)
global UqType Ucells
t = get(handles.listUtypes,'Value');
content = Ucells{1, t};
[tok,rem] = strtok(content);
iu = tok;
[tok,rem] = strtok(rem);
ju = tok;
[tok,rem] = strtok(rem);
u = tok;
[tok,rem] = strtok(rem);
if strcmp(tok,'NaN'), return, end
set(handles.listUtypes,'Enable', 'off');
if strcmp(get(hObject,'String'),'Edit miu')
    set(handles.edMiu,'String',tok);
    set(hObject,'String','OK');
else
    toknew = get(handles.edMiu,'String');
    UqType(t).miu = str2double(toknew);
    content = sprintf(' %s %s %s %s %s',iu,ju,u,toknew,rem);
    Ucells(1, t) = {content};
    set(handles.listUtypes, 'String', Ucells);
    set(handles.listUtypes, 'Value', t);
    set(handles.listUtypes,'Enable', 'on');
    set(hObject,'String','Edit miu');
end % if

% --- Executes on button press in btnEditdmdq.
function btnEditdmdq_Callback(hObject, eventdata, handles)
global UqType Ucells
t = get(handles.listUtypes,'Value');
content = Ucells{1, t};
[tok,rem] = strtok(content);
iu = tok;
[tok,rem] = strtok(rem);
ju = tok;
[tok,rem] = strtok(rem);
u = tok;
[tok,rem] = strtok(rem);
miu = tok;
[tok,rem] = strtok(rem);
if strcmp(tok,'NaN'), return, end
set(handles.listUtypes,'Enable', 'off');
if strcmp(get(hObject,'String'),'Edit dm1dq2')
    set(handles.eddmdq,'String',tok);
    set(hObject,'String','OK');
else
    toknew = get(handles.eddmdq,'String');
    UqType(t).dm1dq2 = str2double(toknew);
    content = sprintf(' %s %s %s %s %s %s',iu,ju,u,miu,toknew,rem);
    Ucells(1, t) = {content};
    set(handles.listUtypes, 'String', Ucells);
    set(handles.listUtypes, 'Value', t);
    set(handles.listUtypes,'Enable', 'on');
    set(hObject,'String','Edit dmdq');
end % if

% --- Executes on button press in btnEditdm2dq1.
function btnEditdm2dq1_Callback(hObject, eventdata, handles)
global UqType Ucells
t = get(handles.listUtypes,'Value');
content = Ucells{1, t};
[tok,rem] = strtok(content);
iu = tok;
[tok,rem] = strtok(rem);
ju = tok;
[tok,rem] = strtok(rem);
u = tok;
[tok,rem] = strtok(rem);
miu = tok;
[tok,rem] = strtok(rem);
dm1dq2 = tok;
[tok,rem] = strtok(rem);
if strcmp(tok,'NaN'), return, end
set(handles.listUtypes,'Enable', 'off');
if strcmp(get(hObject,'String'),'Edit dm2dq1')
    set(handles.eddm2dq1,'String',tok);
    set(hObject,'String','OK');
else
    toknew = get(handles.eddm2dq1,'String');
    UqType(t).dm2dq1 = str2double(toknew);
    content = sprintf(' %s %s %s %s %s %s %s',  ...
        iu,ju,u,miu,dm1dq2,toknew,rem);
    Ucells(1, t) = {content};
    set(handles.listUtypes, 'String', Ucells);
    set(handles.listUtypes, 'Value', t);
    set(handles.listUtypes,'Enable', 'on');
    set(hObject,'String','Edit dm2dq1');
end % if

function eddm2dq1_Callback(hObject, eventdata, handles)

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

function edMiu_Callback(hObject, eventdata, handles)

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

function edU_Callback(hObject, eventdata, handles)

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

function eddmdq_Callback(hObject, eventdata, handles)

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

% --- Executes on button press in btnDeleteU.
function btnDeleteU_Callback(hObject, eventdata, handles)
global UqType iUqType Ucells 
t = get(handles.listUtypes,'Value');
set(handles.listUtypes,'Enable', 'off');
Ucells(:, t) = [];
UqType(:, t) = [];
iUqType = iUqType - 1;
set(handles.listUtypes, 'String', Ucells);
set(handles.listUtypes, 'Value', t);
set(handles.listUtypes,'Enable', 'on');

% --------------------------------------------------------------------
function mnuInputUlist_Callback(hObject, eventdata, handles)
% Input new ulist.txt
global  InfTable  Bonds 
global Alphas Gammas Ros His UqType Qtypes Ucells coincide uTypeID

pname = handles.pname;
fullname = strcat(pname,'ulist.txt');
fid = fopen(fullname, 'rt');

nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
nHis = InfTable(6);

while feof(fid) == 0
    line = fgetl(fid);
    [tok,line] = strtok(line);
    iu = str2double(tok);
    Qtyp1 = Qtypes(iu);
    switch Qtyp1
        case 'q'
            Atoms1 = [Bonds(iu,1),Bonds(iu,2)];
        case 'a'
            ind1 = iu - nBonds;  
            Atoms1 = [Alphas(ind1,1),Alphas(ind1,2),Alphas(ind1,3)];
        case 'g'
            ind1 = iu - (nBonds+nAlphas);  
            Atoms1 = [Gammas(ind1,1),Gammas(ind1,2),Gammas(ind1,3)];
        case 'r'
            ind1 = iu - (nBonds+nAlphas+nGammas);  
            Atoms1 = [Ros(ind1,1),Ros(ind1,2),Ros(ind1,3),Ros(ind1,4)];
        case 'h'
            ind1 = iu - (nBonds+nAlphas+nGammas+nRos);  
            Atoms1 = [His(ind1,1),His(ind1,2),His(ind1,3), ...
                    His(ind1,7),His(ind1,8),His(ind1,9)];
    end %  switch Qtyp1
    [tok,line] = strtok(line);
    ju = str2double(tok);
    Qtyp2 = Qtypes(ju);
    switch Qtyp2
        case 'q'
            Atoms2 = [Bonds(ju,1),Bonds(ju,2)];
        case 'a'
            ind2 = ju - nBonds;  
            Atoms2 = [Alphas(ind2,1),Alphas(ind2,2),Alphas(ind2,3)];
        case 'g'
            ind2 = ju - (nBonds+nAlphas);  
            Atoms2 = [Gammas(ind2,1),Gammas(ind2,2),Gammas(ind2,3)];
        case 'r'
            ind2 = ju - (nBonds+nAlphas+nGammas);  
            Atoms2 = [Ros(ind2,1),Ros(ind2,2),Ros(ind2,3),Ros(ind2,4)];
        case 'h'
            ind2 = ju - (nBonds+nAlphas+nGammas+nRos);  
            Atoms2 = [His(ind2,1),His(ind2,2),His(ind2,3), ...
                    His(ind2,7),His(ind2,8),His(ind2,9)];
    end %  switch Qtyp2
    [tok,line] = strtok(line);
    u = str2double(tok);
    [tok,line] = strtok(line);
    miu = str2double(tok);
    [tok,line] = strtok(line);
    dm1dq2 = str2double(tok);
    [tok,line] = strtok(line);
    dm2dq1 = str2double(tok);
    
    AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
        iu,ju,Atoms1,Atoms2);
    if coincide
        content = Ucells{1, uTypeID};
        [tok,rem] = strtok(content);
        [tok,rem] = strtok(rem);
        [tok,rem] = strtok(rem);
        [tok,rem] = strtok(rem);
        [tok,rem] = strtok(rem);
        [tok,rem] = strtok(rem);
        UqType(uTypeID).u = u;
        UqType(uTypeID).miu = miu;
        UqType(uTypeID).dm1dq2 = dm1dq2;
        UqType(uTypeID).dm2dq1 = dm2dq1;
        s = sprintf(' %i %i %f %f %f %f',iu,ju,u,miu,dm1dq2,dm2dq1);
        content = strcat(s,rem);
        Ucells(1, uTypeID) = {content};
    end % if coincide
end % while feof(fid) == 0
fclose(fid);
set(handles.listUtypes, 'String', Ucells);
set(handles.listUtypes, 'Value', 1);

% --------------------------------------------------------------------
function mnuImportUMiu_Callback(hObject, eventdata, handles)
global UqType Qtypes iUqType Ucells AtomMasses

% Shelter U-globals
UqType_Own = UqType;
iUqType_Own = iUqType; 
Qtypes_Own = Qtypes;
Ucells_Own = protect_Ucells(Ucells);
AtomMasses_Own = AtomMasses;

% Read a foreign Umiu.mat
[fname, pname] = uigetfile('*.mat','Open','Umiu.mat');
if pname == 0, return, end
fullname = strcat(pname,fname);
load(fullname);

nUqType_Own = size(UqType_Own,2);
nUqType = size(UqType,2);
indU = ones(nUqType, 1);

for iown = 1:nUqType_Own
    UqType_Own_row = [UqType_Own(iown).M,UqType_Own(iown).R12, ...
        UqType_Own(iown).t1,UqType_Own(iown).t2, ...
        UqType_Own(iown).t3,UqType_Own(iown).diag, ...
        UqType_Own(iown).csn];
    coincide = 0;
    for i = 1:nUqType
        if indU(i) == 0, continue, end
          if (UqType(i).u == 0.0)
           if (UqType(i).miu == 0.0||isnan(UqType(i).miu))&& ...
           (UqType(i).dm1dq2 == 0.0||isnan(UqType(i).dm1dq2))&& ...
           (UqType(i).dm2dq1 == 0.0||isnan(UqType(i).dm2dq1))
           continue
           end
          end % if (UqType(i).u == 0.0)
        if (UqType_Own(iown).QType1 ~= UqType(i).QType1)|| ...
           (UqType_Own(iown).QType2 ~= UqType(i).QType2),continue,end
        UqType_row = [UqType(i).M,UqType(i).R12, ...
            UqType(i).t1,UqType(i).t2, ...
            UqType(i).t3,UqType(i).diag, ...
            UqType(i).csn];
        if norm(UqType_Own_row - UqType_row) > 0.01, continue, end
        indU(i) = 0;
        UqType_Own(iown).u = UqType(i).u;
        UqType_Own(iown).miu = UqType(i).miu;
        UqType_Own(iown).dm1dq2 = UqType(i).dm1dq2;
        UqType_Own(iown).dm2dq1 = UqType(i).dm2dq1;
        coincide = 1;
        break;
    end % i
    if coincide
        content = Ucells_Own{1, iown};
        [tok,rem] = strtok(content);
        iu = tok;
        [tok,rem] = strtok(rem);
        ju = tok;
        [tok,rem] = strtok(rem);
        u = num2str(UqType_Own(iown).u);
        [tok,rem] = strtok(rem);
        miu = num2str(UqType_Own(iown).miu);
        [tok,rem] = strtok(rem);
        dm1dq2 = num2str(UqType_Own(iown).dm1dq2);
        [tok,rem] = strtok(rem);
        dm2dq1 = num2str(UqType_Own(iown).dm2dq1);
        content = sprintf(' %s %s %s %s %s %s %s', ...
            iu,ju,u,miu,dm1dq2,dm2dq1,rem);
        Ucells_Own(1, iown) = {content};
    end % if coincide
end % iown

% Restore U-globals
UqType = UqType_Own;
iUqType = iUqType_Own;
Qtypes = Qtypes_Own;
AtomMasses = AtomMasses_Own;
clear global Ucells
global Ucells
Ucells = Ucells_Own;

% set(handles.listUtypes, 'Enable', 'on');
set(handles.listUtypes, 'String', Ucells);
set(handles.listUtypes, 'Value', 1);

% My functions-------------------------------------
function result = protect_Ucells(Ucells)
result = Ucells;

function init_uq
% Create empty UqType 

global UqType iUqType Ucells

UqType = struct('QType1','q','QType2','q','u',0.0, ...
                'miu',NaN,'dm1dq2',NaN,'dm2dq1',NaN,'M',1.0, ...
                'R12',1.0,'t1',1.0,'t2',1.0,'t3',1.0,'diag',0, ...
                'csn',0.0);
iUqType = 0;
Ucells = cell(1, 1); % iUcell == iUqType

function matrb(handles)
%MatrB is for checking near interactions between vibr coords with common
%atoms
% Before run create txt files within THIS folder.

global  InfTable Bx By Bz AtomMasses
nAtoms = InfTable(1);
nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
nHis = InfTable(6);
n = InfTable(7);
Bx = zeros(n,nAtoms);
By = zeros(n,nAtoms);
Bz = zeros(n,nAtoms);

pname = handles.pname;
fullname = strcat(pname,'BqStructures.txt');
load(fullname);

fullname = strcat(pname,'AtomMasses.txt');
load(fullname);

ib = 0;
for i = 1:nBonds
    ib = ib + 1;
    ia = BqStructures(i,1);
    Bx(ib,ia) = BqStructures(i,3);
    By(ib,ia) = BqStructures(i,4);
    Bz(ib,ia) = BqStructures(i,5);
    ia = BqStructures(i,2);
    Bx(ib,ia) = BqStructures(i,6);
    By(ib,ia) = BqStructures(i,7);
    Bz(ib,ia) = BqStructures(i,8);
end % i
clear BqStructures
if nAlphas > 0
    fname = 'BaStructures.txt';
    fullname = strcat(pname,fname);
    load(fullname);
	for i = 1:nAlphas
        ib = ib + 1;
        ia = BaStructures(i,1);
        Bx(ib,ia) = BaStructures(i,4);
        By(ib,ia) = BaStructures(i,5);
        Bz(ib,ia) = BaStructures(i,6);
        ia = BaStructures(i,2);
        Bx(ib,ia) = BaStructures(i,7);
        By(ib,ia) = BaStructures(i,8);
        Bz(ib,ia) = BaStructures(i,9);
        ia = BaStructures(i,3);
        Bx(ib,ia) = BaStructures(i,10);
        By(ib,ia) = BaStructures(i,11);
        Bz(ib,ia) = BaStructures(i,12);
	end % i = 1:nAlphas
    clear BaStructures
end % if nAlphas > 0
if nGammas > 0
    fname = 'BgStructures.txt';
    fullname = strcat(pname,fname);
    load(fullname);

    for i = 1:nGammas
        ib = ib + 1;
        ia = BgStructures(i,1);
        Bx(ib,ia) = BgStructures(i,4);
        By(ib,ia) = BgStructures(i,5);
        Bz(ib,ia) = BgStructures(i,6);
        ia = BgStructures(i,2);
        Bx(ib,ia) = BgStructures(i,7);
        By(ib,ia) = BgStructures(i,8);
        Bz(ib,ia) = BgStructures(i,9);
        ia = BgStructures(i,3);
        Bx(ib,ia) = BgStructures(i,10);
        By(ib,ia) = BgStructures(i,11);
        Bz(ib,ia) = BgStructures(i,12);
	end % i = 1:nGammas
    clear BgStructures
end % if nGammas > 0
if nRos > 0
    fname = 'BrStructures.txt';
    fullname = strcat(pname,fname);
    load(fullname);
 
    for i = 1:nRos
        ib = ib + 1;
        ia = BrStructures(i,1);
        Bx(ib,ia) = BrStructures(i,5);
        By(ib,ia) = BrStructures(i,6);
        Bz(ib,ia) = BrStructures(i,7);
        ia = BrStructures(i,2);
        Bx(ib,ia) = BrStructures(i,8);
        By(ib,ia) = BrStructures(i,9);
        Bz(ib,ia) = BrStructures(i,10);
        ia = BrStructures(i,3);
        Bx(ib,ia) = BrStructures(i,11);
        By(ib,ia) = BrStructures(i,12);
        Bz(ib,ia) = BrStructures(i,13);
        ia = BrStructures(i,4);
        Bx(ib,ia) = BrStructures(i,14);
        By(ib,ia) = BrStructures(i,15);
        Bz(ib,ia) = BrStructures(i,16);
    end % i = 1:nRos
    clear BrStructures
end % if nRos > 0
if nHis > 0
    fname = 'BhStructures.txt';
    fullname = strcat(pname,fname);
    load(fullname);
	for i = 1:nHis
        ib = ib + 1;
        ia = BhStructures(i,1);
        Bx(ib,ia) = Bx(ib,ia)+BhStructures(i,7);
        By(ib,ia) = By(ib,ia)+BhStructures(i,8);
        Bz(ib,ia) = Bz(ib,ia)+BhStructures(i,9);
        ia = BhStructures(i,2);
        Bx(ib,ia) = Bx(ib,ia)+BhStructures(i,10);
        By(ib,ia) = By(ib,ia)+BhStructures(i,11);
        Bz(ib,ia) = Bz(ib,ia)+BhStructures(i,12);
        ia = BhStructures(i,3);
        Bx(ib,ia) = Bx(ib,ia)+BhStructures(i,13);
        By(ib,ia) = By(ib,ia)+BhStructures(i,14);
        Bz(ib,ia) = Bz(ib,ia)+BhStructures(i,15);
        ia = BhStructures(i,4);
        Bx(ib,ia) = Bx(ib,ia)+BhStructures(i,16);
        By(ib,ia) = By(ib,ia)+BhStructures(i,17);
        Bz(ib,ia) = Bz(ib,ia)+BhStructures(i,18);
        ia = BhStructures(i,5);
        Bx(ib,ia) = Bx(ib,ia)+BhStructures(i,19);
        By(ib,ia) = By(ib,ia)+BhStructures(i,20);
        Bz(ib,ia) = Bz(ib,ia)+BhStructures(i,21);
        ia = BhStructures(i,6);
        Bx(ib,ia) = Bx(ib,ia)+BhStructures(i,22);
        By(ib,ia) = By(ib,ia)+BhStructures(i,23);
        Bz(ib,ia) = Bz(ib,ia)+BhStructures(i,24);
    end % i = 1:nHis
    clear BhStructures
end % if nHis > 0

function form_utypes(handles)
% Form UqType template; to be filled with u, miu values later 
global InfTable Type AtomTypes Bonds 
global Alphas Gammas Ros His Bx By Bz Qtypes Ucells AtomMasses

nAtoms = InfTable(1);
nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
nHis = InfTable(6);
n = InfTable(7);

AtomMasses = zeros(nAtoms,1);
for i = 1:nAtoms
    AtomMasses(i) = Type(AtomTypes(i)).Mass;
end % i

Qtypes = char([1:n]);
iu = 0;
for i = 1:nBonds
    iu = iu + 1;
    Qtypes(iu) = 'q';
end % i
if nAlphas > 0
for i = 1:nAlphas
    iu = iu + 1;
    Qtypes(iu) = 'a';
end % i
end % if nAlphas > 0
if nGammas > 0
for i = 1:nGammas
    iu = iu + 1;
    Qtypes(iu) = 'g';
end % i
end % if nGammas > 0
if nRos > 0
for i = 1:nRos
    iu = iu + 1;
    Qtypes(iu) = 'r';
end % i
end % if nRos > 0
if nHis > 0
for i = 1:nHis
    iu = iu + 1;
    Qtypes(iu) = 'h';
end % i
end % if nHis > 0

u = 0.0; % for a new UqTypes
dm1dq2 = 0.0; % for a new UqTypes
dm2dq1 = 0.0; % for a new UqTypes

for iq = 1:nBonds
    ind1 = iq;  % for Uq(ind1,ind2) 
    Qtyp1 = 'q';
    Atoms1 = [Bonds(iq,1),Bonds(iq,2)];
    
    Qtyp2 = 'q';
    for jq = iq:nBonds
        ind2 = jq;
        biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
        if abs(biqjq) < 0.001, continue, end % no common atom or perpendicular
        miu = nan; % for a new UqTypes
        if jq == iq, miu = 0.0; end
        Atoms2 = [Bonds(jq,1),Bonds(jq,2)];
        AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
            ind1,ind2,Atoms1,Atoms2);
    end % jq
    miu = nan; % for a new UqTypes
    
    if nAlphas > 0
        Qtyp2 = 'a';
        for ja = 1:nAlphas
            ind2 = ja + nBonds;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Alphas(ja,1),Alphas(ja,2),Alphas(ja,3)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % ja
    end % if nAlphas > 0
    
    if nGammas > 0
        Qtyp2 = 'g';
        for jg = 1:nGammas
            ind2 = jg + nBonds + nAlphas;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Gammas(jg,1),Gammas(jg,2),Gammas(jg,3)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jg
    end % if nGammas > 0
    
    if nRos > 0
        Qtyp2 = 'r';
        for jr = 1:nRos
            ind2 = jr + nBonds + nAlphas + nGammas;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Ros(jr,1),Ros(jr,2),Ros(jr,3),Ros(jr,4)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jr
    end % if nRos > 0
    
    if nHis > 0
        Qtyp2 = 'h';
        for jh = 1:nHis
            ind2 = jh + nBonds + nAlphas + nGammas + nRos;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [His(jh,1),His(jh,2),His(jh,3),His(jh,7), ...
                      His(jh,8),His(jh,9)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jh
    end % if nHis > 0
end % iq

miu = NaN; % for a new UqTypes
dm1dq2 = NaN; % for a new UqTypes
dm2dq1 = NaN; % for a new UqTypes

if nAlphas > 0
for ia = 1:nAlphas
    ind1 = ia + nBonds;  % for Uq(ind1,ind2) 
    Qtyp1 = 'a';
    Atoms1 = [Alphas(ia,1),Alphas(ia,2),Alphas(ia,3)];
    
    Qtyp2 = 'a';
    for ja = ia:nAlphas
        ind2 = ja + nBonds;
        biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
        if abs(biqjq) < 0.001, continue, end 
        Atoms2 = [Alphas(ja,1),Alphas(ja,2),Alphas(ja,3)];
        AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
            ind1,ind2,Atoms1,Atoms2);
    end % ja
    
    if nGammas > 0
        Qtyp2 = 'g';
        for jg = 1:nGammas
            ind2 = jg + nBonds + nAlphas;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Gammas(jg,1),Gammas(jg,2),Gammas(jg,3)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jg
    end % if nGammas > 0

    if nRos > 0
        Qtyp2 = 'r';
        for jr = 1:nRos
            ind2 = jr + nBonds + nAlphas + nGammas;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Ros(jr,1),Ros(jr,2),Ros(jr,3),Ros(jr,4)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jr
    end % if nRos > 0
    
    if nHis > 0
        Qtyp2 = 'h';
        for jh = 1:nHis
            ind2 = jh + nBonds + nAlphas + nGammas + nRos;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [His(jh,1),His(jh,2),His(jh,3),His(jh,7), ...
                      His(jh,8),His(jh,9)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jh
    end % if nHis > 0

end % ia     
end % if nAlphas > 0

if nGammas > 0
for ig = 1:nGammas
    ind1 = ig + nBonds + nAlphas;  % for Uq(ind1,ind2) 
    Qtyp1 = 'g';
    Atoms1 = [Gammas(ig,1),Gammas(ig,2),Gammas(ig,3)];

    Qtyp2 = 'g';
    for jg = ig:nGammas
        ind2 = jg + nBonds + nAlphas;
             biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Gammas(jg,1),Gammas(jg,2),Gammas(jg,3)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
    end % jg
    
    if nRos > 0
        Qtyp2 = 'r';
        for jr = 1:nRos
            ind2 = jr + nBonds + nAlphas + nGammas;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Ros(jr,1),Ros(jr,2),Ros(jr,3),Ros(jr,4)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jr
    end % if nRos > 0
    
    if nHis > 0
        Qtyp2 = 'h';
        for jh = 1:nHis
            ind2 = jh + nBonds + nAlphas + nGammas + nRos;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [His(jh,1),His(jh,2),His(jh,3),His(jh,7), ...
                      His(jh,8),His(jh,9)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jh
    end % if nHis > 0
    
end % ig     
end % if nGammas > 0

if nRos > 0
for ir = 1:nRos
    ind1 = ir + nBonds + nAlphas + nGammas;  % for Uq(ind1,ind2) 
    Qtyp1 = 'r';
    Atoms1 = [Ros(ir,1),Ros(ir,2),Ros(ir,3),Ros(ir,4)];

    Qtyp2 = 'r';
    for jr = ir:nRos
        ind2 = jr + nBonds + nAlphas + nGammas;
             biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [Ros(jr,1),Ros(jr,2),Ros(jr,3),Ros(jr,4)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
    end % jr

    if nHis > 0
        Qtyp2 = 'h';
        for jh = 1:nHis
            ind2 = jh + nBonds + nAlphas + nGammas + nRos;
            biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [His(jh,1),His(jh,2),His(jh,3),His(jh,7), ...
                      His(jh,8),His(jh,9)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
        end % jh
    end % if nHis > 0

end % ir     
end % if nRos > 0

if nHis > 0
for ih = 1:nHis
    ind1 = ih + nBonds + nAlphas + nGammas + nRos;  % for Uq(ind1,ind2) 
    Qtyp1 = 'h';
    Atoms1 = [His(ih,1),His(ih,2),His(ih,3),His(ih,7), ...
              His(ih,8),His(ih,9)];
                  
    Qtyp2 = 'h';
    for jh = ih:nHis
        ind2 = jh + nBonds + nAlphas + nGammas + nRos;
             biqjq = Bx(ind1,:)*Bx(ind2,:)' + ...
                    By(ind1,:)*By(ind2,:)' + Bz(ind1,:)*Bz(ind2,:)';
            if abs(biqjq) < 0.001, continue, end 
            Atoms2 = [His(jh,1),His(jh,2),His(jh,3),His(jh,7), ...
                      His(jh,8),His(jh,9)];
            AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
                ind1,ind2,Atoms1,Atoms2);
    end % jr
end % ih     
end % if nHis > 0
% fclose(fid);
set(handles.listUtypes, 'String', Ucells);
set(handles.listUtypes, 'Value', 1);

function AppendUqTable(u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2, ...
    ind1,ind2,Atoms1,Atoms2)
% if it needs, appends UqType 

global UqType iUqType  Ucells coincide uTypeID Bx By Bz 

[M1,rc1] = mass_centrum(Atoms1);
[M2,rc2] = mass_centrum(Atoms2);
sample.QType1 = Qtyp1;
sample.QType2 = Qtyp2;
sample.u = u;
sample.miu = miu;
sample.dm1dq2 = dm1dq2;
sample.dm2dq1 = dm2dq1;
sample.M = M1 + M2;
sample.R12 = norm(rc2 - rc1);
Atoms = [Atoms1,Atoms2];
tenz = tenzor(Atoms);
sample.t1 = tenz(1);
sample.t2 = tenz(2);
sample.t3 = tenz(3);
sample.diag = 0;
if ind1 == ind2
    sample.diag = 1;
end % if
i1 = Atoms1(2);
i2 = Atoms2(2);
sample.csn = [Bx(ind1,i1), By(ind1,i1), Bz(ind1,i1)]* ...
             [Bx(ind2,i2), By(ind2,i2), Bz(ind2,i2)]';
sample.csn = abs(sample.csn);         
    % check iType coincide
[coincide, uTypeID] = check_type(sample);
if coincide == 0
    iUqType = iUqType + 1;
    iUcell = iUqType;
    UqType(iUqType) = sample;
    str4i = ' %4i';
    str_format = ' ';
    str1 = sprintf('%4i %4i %8.4f %8.4f %8.4f %8.4f %1c%1c  ', ...
        ind1,ind2,u,miu,dm1dq2,dm2dq1,Qtyp1,Qtyp2);
    if ind1 == ind2
        n1 = length(Atoms1);
        for i = 1:n1, str_format = [str_format, str4i]; end
        str2 = sprintf(str_format,Atoms1);
    else
        n1 = length(Atoms1);
        n2 = length(Atoms2);
        for i = 1:n1, str_format = [str_format, str4i]; end
        str_format = [str_format, ' / '];
        for i = 1:n2, str_format = [str_format, str4i]; end
        str_format = [str_format, ' \n'];
        str2 = sprintf(str_format,Atoms1,Atoms2);
    end % if ind1 == ind2
    Ucells(1, iUcell) = {strcat(str1, str2)};
end % if coincide == 0
    
function [M,rc] = mass_centrum(Atoms)
% Find total M and rc for a vibrational coordinate

global AtomCoords AtomMasses
M = sum(AtomMasses(Atoms));
rc = [];
for i = Atoms
%    Masses = [Masses; AtomMasses(i)];
    rc = [rc; AtomMasses(i)*AtomCoords(i,:)];
end % i
rc = sum(rc)/M;

function tenz = tenzor(Atoms)
% Calculate inertia tenzor for Atoms in interacting vibr coordinates
% Then turn the tenzor to its main axes

global AtomCoords AtomMasses
n = length(Atoms);
Coords = AtomCoords(Atoms,:);
Masses = AtomMasses(Atoms);
M = sum(Masses);
rc = [0 0 0];
for i = 1:n
    for j = 1:3
        rc(j) = rc(j) + Masses(i)*Coords(i,j);
    end  % j
end % i
rc = rc/M;
for i = 1:n
    Coords(i,:) = Coords(i,:) - rc;
end % i
tenz0 = zeros(3);
for i = 1:n
    tenz0(1,1) = tenz0(1,1) + Masses(i)*(Coords(i,2)^2 + Coords(i,3)^2);
    tenz0(1,2) = tenz0(1,2) - Masses(i)*Coords(i,1)*Coords(i,2);
    tenz0(1,3) = tenz0(1,3) - Masses(i)*Coords(i,1)*Coords(i,3);
    tenz0(2,2) = tenz0(2,2) + Masses(i)*(Coords(i,1)^2 + Coords(i,3)^2);
    tenz0(2,3) = tenz0(2,3) - Masses(i)*Coords(i,2)*Coords(i,3);
    tenz0(3,3) = tenz0(3,3) + Masses(i)*(Coords(i,1)^2 + Coords(i,2)^2);
end % i
tenz0(2,1) = tenz0(1,2);
tenz0(3,1) = tenz0(1,3); 
tenz0(3,2) = tenz0(2,3); 
tenz = eig(tenz0);

function [coincide, uTypeID] = check_type(sample)
% Compare sample structure with existing UqType
global UqType 
nType = length(UqType);
coincide = 0;
uTypeID = 0;
sample_row = [sample.M,sample.R12,sample.t1,sample.t2,sample.t3, ...
              sample.diag, sample.csn];
for i = 1:nType
    if sample.QType1 ~= UqType(i).QType1 || ...
       sample.QType2 ~= UqType(i).QType2
       continue
    end % if
    UqType_row = [UqType(i).M,UqType(i).R12,UqType(i).t1,UqType(i).t2, ...
        UqType(i).t3,UqType(i).diag, UqType(i).csn]; 
    if norm(sample_row - UqType_row) > 0.01          
       continue
    else
        coincide = 1;
        uTypeID = i;
        return;
    end % if
end % i

function show_InfTable(handles)
global InfTable
nBlocks = InfTable(8);
set(handles.ednBlocks, 'String', int2str(nBlocks));
nAtoms = InfTable(1); 
set(handles.ednAtoms, 'String', int2str(nAtoms));
nBonds = InfTable(2);
set(handles.ednBonds, 'String', int2str(nBonds));
nAlphas = InfTable(3);
set(handles.ednAlphas, 'String', int2str(nAlphas));
nGammas = InfTable(4);
set(handles.ednGammas, 'String', int2str(nGammas));
nRos = InfTable(5);
set(handles.ednRos, 'String', int2str(nRos));
nHis = InfTable(6);
set(handles.ednHis, 'String', int2str(nHis));
nVibrCoords = InfTable(7);
set(handles.ednVibrCoords, 'String', int2str(nVibrCoords));

function update_ini(pname)
diskname = pname(1:2);
filename = strcat(diskname,'\LEVINFINITE\Config\ToCalculate.ini');
s = strcat(diskname,'\LEVINFINITE\MOLECULES\');
ns = length(s);
npn = length(pname);
molname = pname((ns+1):(npn-1));
fid = fopen(filename,'rt+');
fprintf(fid,'%s\n ',molname);
fclose(fid);

% atom_pair_vector
function [r,varargout] = atom_pair_vector(Atom1, Atom2)
% construct unit vector from Atom1 to Atom2 using AtomCoords
global AtomCoords

r = AtomCoords(Atom2,:) - AtomCoords(Atom1,:);
s = norm(r);
r = r/s;
if nargout > 0, varargout{1} = s; end % if

% End My functions-------------------------------------





