% Sets up GUI for handling time domain simulations.
%

if exist('dlgfig')
    set(dlgfig,'Visible','off');
end

% Make sure data is loaded.
if ~exist('DataFlag'),DataFlag=0;end
if DataFlag==0,getdata;end
if DataFlag==0,return;end

if (strcmp(computer,'SOL2' ))
  mexwild = '*.mexsol';
  lensub=7;
elseif (strcmp(computer, 'PCWIN'))
   mexwild = '*.dll';
    lensub=4;
end

[SMfilename,SMpathname]=uigetfile(...
    mexwild,'Choose system for analysis');

if SMfilename~=0
    CurrentSystem=SMfilename(1:length(SMfilename)-lensub);
    if isempty(findstr(CurrentFileName,CurrentSystem))
        titleStr=[' Inconsistent Names '];
        textStr=['                                              '
                 ' Names of the data file and executable must   '
                 ' be consistent. Do you want to choose another '
                 ' executable now ?                             '
                ];
        dlgfig=mydlg(titleStr,textStr,'sim_0 | rtnnow');
        return;
    end

    % Define a figure window and assign a handle to it.
    position=get(0,'DefaultFigurePosition');
    position=position-[70 70 0 0];
    simulation_fig=figure(...
        'NumberTitle','off',...
        'Name','Voltage Stability Toolbox - Dynamic Simulation',...
        'Resize','off',...
        'Position',position,...
        'Color',[0.7 0.8 0.9]);

% ==============================
% Help menu
helpmenu=uimenu(simulation_fig,...
    'Label','Help');

    hlp=uimenu(helpmenu,...
        'Label','Help',...
        'CallBack','helpfun(sim_hlpTitle,hlpStr3)');

sim_hlpTitle=['Voltage Stability Toolbox - Time Domain Simulation Help Window'];

    % set simulation data flag
    Sim_Data_Flag=0;

    path(path,SMpathname);
    eval(['load ',CurrentSystem,'.mat']);

    % set initial conditions to the point near bifurcation
    if exist('simul_st_x')
        x=simul_st_x;
    else
        simul_st_x=x;
    end

    if exist('simul_st_p')
        param=simul_st_p;
    else
        simul_st_p=param;
    end

    if ~exist('v')
        v=zeros(size(x));
    end

    titlepanel;
        
    % Present editable data: states & params
    k_states=length(x);
    k_params=length(param);
    lfmklabl;

    % Frame
    uicontrol(simulation_fig,...
        'Style','frame',...
        'Background','B',...
        'Position',[25,17,min(max(k_states,k_params),5)*90+10,172]);

    %*********************************
    % State Data Display
    %*********************************
    % Define state slider
    display_length=min(k_states,5);

    sli_states=uicontrol(simulation_fig,...
        'Style','slider',...
        'Position',[30,40,display_length*90,15],...
        'Min',1,'Max',k_states,'Value',1,...
        'Callback','lfsetsta');

    CurrentState=max([1,round(get(sli_states,'Value'))]);

    % State slider text
    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[30,23,25,12],...
        'String','1');
    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[5+display_length*90,23,25,12],...
        'String',num2str(k_states));
    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[display_length*45-15,22,100,15],...
        'BackgroundColor','Y',...
        'String','State Values');

    for i=1:display_length,
        StateLabel(i)=uicontrol(simulation_fig,...
            'Style','text',...
            'Position',[30+(i-1)*90,80,90,18],...
            'HorizontalAlignment','Center',...
            'String',statename(max(CurrentState-5,0)+i,:));
         
        StateValue(i)=uicontrol(simulation_fig,...
            'Style','edit',...
            'Position',[30+(i-1)*90,60,90,18],...
            'HorizontalAlignment','Center',...
            'String',num2str(x(max(CurrentState-5,0)+i)),...
            'Callback','smstatup');
    end

    %*********************************
    % Parameter Data Display
    %*********************************
    % Define parameter slider
    clear ParamSlider;
    display_length=min(k_params,5);
    sli_params=uicontrol(simulation_fig,...
        'Style','slider',...
        'Position',[30,125,display_length*90,15],...
        'Min',1,'Max',k_params,'Value',1,...
        'Callback','lfsetpar');

    CurrentParam=max([1,round(get(sli_params,'Value'))]);

    % Parameter slider text
    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[30,108,25,12],...
        'String','1');
    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[5+display_length*90,108,25,12],...
        'String',num2str(k_params));

    for i=1:display_length,
        ParamLabel(i)=uicontrol(simulation_fig,...
            'Style','text',...
            'Position',[30+(i-1)*90,165,90,18],...
            'HorizontalAlignment','Center',...
            'String',paramname(max(CurrentParam-5,0)+i,:));

        ParamValue(i)=uicontrol(simulation_fig,...
            'Style','edit',...
            'Position',[30+(i-1)*90,145,90,18],...
            'HorizontalAlignment','Right',...
            'String',num2str(param(max(CurrentParam-5,0)+i)),...
            'Callback','smparupd');
    end

    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[display_length*45-30,106,150,16],...
        'String','Parameter Values',...
        'BackgroundColor','y');

    %*********************************
    % upper panel
    %*********************************
    uicontrol(simulation_fig,...
        'Style','frame',...
        'BackgroundColor','b',...
        'Position',[25,197,min(max(k_states,k_params),5)*90+10,67]);

    t_final=100;

    simu_time_title=uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[30,242,120,16],...
        'BackgroundColor','Y',...
        'String','Simulation Time:');

    simul_time_value=uicontrol(simulation_fig,...
        'Style','edit',...
        'Position',[151,242,109,16],...
        'HorizontalAlignment','Center',...
        'String',num2str(t_final),...
        'Callback','smcntrup');

    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[270,242,100,16],...
        'BackgroundColor','R',...
        'ForegroundColor','W',...
        'HorizontalAlignment','Center',...
        'String','Time');

    simul_time=uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[371,242,109,16],...
        'HorizontalAlignment','Center',...
        'String','0'); 

    k_var=no_gen+no_pv-1
    smlabel;
    if Sim_Data_Flag==0
        % Create variable slider
        sli_var=uicontrol(simulation_fig,...
            'Style','slider',...
            'Position',[30,222,400,15],...
            'Min',1,'Max',2*k_var,'Value',1,...
            'Callback','smsetvar');

        CurrentVar=max([1,round(get(sli_var,'Value'))]);

        % Variable slider text
        uicontrol(simulation_fig,...
            'Style','text',...
            'Position',[30,204,25,12],...
            'String','1');
        uicontrol(simulation_fig,...
            'Style','text',...
            'Position',[120,202,120,15],...
            'BackgroundColor','Y',...
            'String','Selected Variable:');
        varHandle=uicontrol(simulation_fig,...
            'Style','text',...
            'Position',[241,202,100,15],...
            'BackgroundColor','Y',...
            'String',var_lst(CurrentVar,:));
        smsetvar;
        uicontrol(simulation_fig,...
            'Style','text',...
            'Position',[400,204,30,12],...
            'String',num2str(2*k_var));

        plotHANDLE=uicontrol(simulation_fig,...
            'Style','pushbutton',...
            'BackgroundColor','G',...
            'Position',[433,202,50,35],...
            'HorizontalAlignment','Center',...
            'String','Plot',...
            'Callback','sim_plot',...
            'Enable','off');

        Sim_Data_Flag=1;
    end

  if exist('PP')
    % Setup frame
    uicontrol(simulation_fig,...
        'Style','frame',...
        'BackgroundColor','b',...
        'Position',[25,272,min(max(k_states,k_params),5)*90+10,44]);

    % Define point slider
    [m,n_s]=size(PP);
    sli_point=uicontrol(simulation_fig,...
        'Style','slider',...
        'Position',[30,297,400,15],...
        'Min',1,'Max',n_s,'Value',simul_pt_idx,...
        'SliderStep',[0.0005 0.01],...
        'Callback','smsetpar');
    % Point slider text
    uicontrol(simulation_fig,...
       'Style','text',...
       'BackgroundColor','Y',...
        'Position',[30,279,25,12],...
        'String','1');
    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[140,277,150,15],...
        'BackgroundColor','Y',...
        'String','Current Point Number:');
    pointHandle=uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[291,277,30,15],...
        'BackgroundColor','Y',...
        'String',num2str(simul_pt_idx));
    uicontrol(simulation_fig,...
        'Style','text',...
        'Position',[400,279,30,12],...
        'String',num2str(n_s));

    uicontrol(simulation_fig,...
        'Style','pushbutton',...
        'BackgroundColor',[0 1 0],...
        'Position',[433,277,50,35],...
        'HorizontalAlignment','Center',...
        'String','Start',...
        'Callback','sim_1');

    smsetpar;
  else
    uicontrol(simulation_fig,...
        'Style','pushbutton',...
        'BackgroundColor','R',...
        'Position',[60,280,60,29],...
        'HorizontalAlignment','Center',...
        'String','Reset',...
        'Callback',['x=simul_st_x;'...
            'param=simul_st_p;'...
            'timereset;'...
            'lfsetpar;'...
            'lfsetsta;']);
    uicontrol(simulation_fig,...
        'Style','pushbutton',...
        'BackgroundColor',[.1 1 .1],...
        'Position',[170,280,50,29],...
        'HorizontalAlignment','Center',...
        'String','Start',...
        'Callback','sim_1');
    %uicontrol(simulation_fig,...
     %   'Style','pushbutton',...
      %  'BackgroundColor','R',...
       % 'Position',[270,280,50,29],...
        %'HorizontalAlignment','Center',...
        %'String','Save',...
        %'Enable','off');
    %uicontrol(simulation_fig,...
     %   'Style','pushbutton',...
      %  'BackgroundColor','R',...
       % 'Position',[370,280,60,29],...
        %'HorizontalAlignment','Center',...
        %'String','Save As',...
        %'Enable','off');
  end
end
