function pdeModeler(action,flag) %#ok - flag used in eval
%pdeModeler graphical user interface for pde.
%   pdeModeler provides the graphical user interface for the
%   PDE Toolbox.
%
%   Call pdeModeler without arguments to start application.

%   Copyright 1994-2017 The MathWorks, Inc.

    %Prepare globals to be used in other files
    global PDEAXISRATIO;
    PDEAXISRATIO = 1.5;
    
    %Check input
    if(nargin < 1), action='initialize'; end
    
    %Call appropriate function
    if(nargin < 2)
        eval(['pdetool_' action '();']);
    else
        eval(['pdetool_' action '(flag);']);
    end
    
    %Cleanup globals
    clear global PDEAXISRATIO;
end

function pdetool_error(errorMsg) %#ok
% case: error

    errordlg(errorMsg,'PDE Modeler Error','modal');
end

function pdetool_initialize() %#ok
% case: initialize

    pde_fig=findobj(allchild(0),'flat','Tag','PDETool');
    
    if ~isempty(pde_fig)
        clear global PDEAXISRATIO;
        % Pop the existing pdetool to the front.
        figure(pde_fig);
        return;
    end
    
    global PDEAXISRATIO;
    axisRatio = PDEAXISRATIO;
    
    ScreenUnits = get(0,'Units');
    set(0,'Unit','pixels');
    ScreenPos = get(0,'ScreenSize');
    set(0,'Unit',ScreenUnits);
    Figpos=[.15 .15 .65 .75].*ScreenPos+[0.1*ScreenPos(3:4) 0 0];
    pde_fig=figure(...
        'Visible','off',...
        'Color','w',...
        'Colormap',gray(20),...
        'Position',Figpos,...
        'IntegerHandle','off',...
        'DockControls','off',...
        'Interruptible','on','CloseRequestFcn','pdetool(''exit'')');

    % Set the pointer to a watch:
    set(pde_fig,'Pointer','watch')
    drawnow

    % Set up some default values:

    % flags=[save_flag mode_flag flag1 flag2 flag3 flag4 flag5]
    % save_flag: model needs to be saved.
    % mode_flag: 0=draw mode; 1=boundary mode; 2=mesh mode;
    % 3=solve mode; 4=pde mode
    % flag1=Geometry has changed, decomposition needed (flag1=-1 if decsg error)
    % flag2=Decomposed geometry has changed, mesh initialization needed
    % flag3=Mesh or boundary condition has changed, need to solve system
    % flag4=New solution computed
    % flag5=PDE equation has changed; recompute solution if necessary
    %
    % Storage: File menu's UserData
    flags=zeros(7,1);

    % PDE coefficients:
    c='1.0'; a='0.0'; f='10.0'; d='1.0'; tlist='0:10'; u0='0.0';...
        ut0='0.0'; r='[0 100]'; rtol='0.01'; atol='0.001';
    params=char(c,a,f,d);
    setappdata(pde_fig,'currparam',char(c,a,f,d))
    setappdata(pde_fig,'timeeigparam',char(tlist,u0,ut0,r,rtol,atol))
    setappdata(pde_fig,'ncafd',ones(1,4))
    % Default solve parameters:
    % Adaptive mode: 0 (=off)
    % Max number of triangles: 1000
    % Max number of refinements: 10
    % Triangle selection method: 'pdeadworst'
    % Function parameter: 0.5
    % Local tolerance: 1E-3
    % Triangle refinement method - adaption: 'longest'
    % Nonlinear solver: 0 (=off)
    % Nonlinear tolerance: 1.0
    % Initial solution for nonlinear solver: none (optional argument)
    % Jacobian: fixed
    % Nonlinear norm: Inf
    solveparams=char('0','1000','10','pdeadworst','0.5','longest',...
        '0','1E-4','','fixed','Inf');
    setappdata(pde_fig,'solveparam',solveparams)


    %
    % Initialize main PDE figure window and axes:
    set(pde_fig,...
        'Tag','PDETool',...
        'NumberTitle','off',...
        'Name','PDE Modeler - [Untitled]',...
        'Resize','on',...
        'MenuBar','none',...
        'Visible','on',...
        'KeyPressFcn','pdeModeler keycall',...
        'Units','pixels')

    % Note: this same axis position is independently set in pdeplot 
    axstdpos =[0.13 0.1 0.8 0.75];

    ax=axes(...
        'Parent',pde_fig,...
        'Position',axstdpos,...
        'XColor','k',...
        'Box','on',...
        'YColor','k',...
        'Color','w',...
        'Tag','PDEAxes',...
        'XLimMode','manual',...
        'YLimMode','manual',...
        'XLim',[-axisRatio axisRatio],...
        'YLim',[-1 1],...
        'XGrid','off',...
        'YGrid','off',...
        'ZGrid','off',...
        'XTickMode','auto',...
        'YTickMode','auto',...
        'Units','normalized',...
        'DataAspectRatio',[1 1 1]);

    % Save axes position:
    setappdata(ax,'axstdpos',axstdpos)

    % Save draw mode color map (gray) and solve mode default colormap (cool)
    set(get(ax,'XLabel'),'UserData',gray(20))
    set(get(ax,'YLabel'),'UserData',cool(64))

    % Initialize user interface menus and ui controls (buttons etc.)
    % File menu:
    file_menu = uimenu(pde_fig,'Label','&File',...
        'Tag','PDEFileMenu','UserData',flags);
    uimenu(file_menu,'Accelerator','n','Label','&New ',...
        'Interruptible','on','CallBack','pdeModeler(''new'')');
    uimenu(file_menu,'Accelerator','o','Label','&Open... ',...
        'Interruptible','on','CallBack','pdeModeler(''open'')');
    uimenu(file_menu,'Accelerator','s','Label','&Save ',...
        'Tag','PDESave','Enable','off','CallBack','pdeModeler(''save'')');
    uimenu(file_menu,'Label','Save &As...',...
        'CallBack','pdeModeler(''save_as''),');
    uimenu(file_menu,'Label','Export &Image...',...
        'CallBack','pdeModeler(''export_image''),');
    uimenu(file_menu,'Label','&Print...','Separator','on',...
        'CallBack','pdeModeler(''print'')');
    uimenu(file_menu,'Accelerator','w','Label','E&xit ',...
        'Interruptible','on', 'Separator','on','CallBack','pdeModeler(''exit'')');

    % Edit menu:
    edit_menu = uimenu(pde_fig,'Label','&Edit',...
        'Tag','PDEEditMenu','UserData',[]);
    uimenu(edit_menu,'Accelerator','z','Label','&Undo ','Enable','off',...
        'Tag','PDEUndo','CallBack','pdeModeler(''undo'')');
    uimenu(edit_menu,'Accelerator','x','Label','Cu&t ','Separator','on',...
        'Tag','PDECut','CallBack','pdeModeler(''cut'',1)');
    uimenu(edit_menu,'Accelerator','c','Label','&Copy ','Tag','PDECopy',...
        'CallBack','pdeModeler(''cut'',2)');
    uimenu(edit_menu,'Accelerator','v','Label','&Paste... ','Enable','off',...
        'Tag','PDEPaste','CallBack','pdepsdlg');
    uimenu(edit_menu,'Accelerator','r','Label','Clea&r ','Enable','off',...
        'Separator','on','Tag','PDEClear','CallBack','pdeModeler(''clear'')');
    uimenu(edit_menu,'Label','Select &All ','Accelerator','a','Separator','on',...
        'Tag','PDESelall','CallBack','pdeselect(''select'',1)');

    % Options menu:
    opt_menu = uimenu(pde_fig,'Label','&Options',...
        'Tag','PDEOptMenu');
    uimenu(opt_menu,'Label','&Grid','Tag','PDEGrid',...
        'CallBack','pdeModeler gridon');
    uimenu(opt_menu,'Label','Gr&id Spacing...','Tag','PDESpace',...
        'CallBack','pdeModeler spacing');
    uimenu(opt_menu,'Label','&Snap','Tag','PDESnap',...
        'CallBack','pdeModeler(''snapon'')');
    uimenu(opt_menu,'Label','&Axes Limits...','Tag','PDEAxMenu',...
        'Separator','on','CallBack','pdeModeler axlim');
    uimenu(opt_menu,'Label','Axes &Equal','Tag','PDEAxeq',...
        'CallBack','pdeModeler axeq');
    uimenu(opt_menu,'Label','&Turn off Toolbar Help','Tag','PDEHelpoff',...
        'Separator','on','CallBack','pdeModeler helponoff');
    uimenu(opt_menu,'Label','&Zoom','Tag','PDEZoom',...
        'Separator','on','CallBack','pdeModeler zoom');
    appl_menu=uimenu(opt_menu,'Label','A&pplication',...
        'Tag','PDEAppMenu','Separator','on');
    uimenu(appl_menu,'Label','Generic Scalar','Checked','on',...
        'CallBack','pdeModeler(''appl_cb'',1)','UserData',1);
    uimenu(appl_menu,'Label','Generic System',...
        'CallBack','pdeModeler(''appl_cb'',2)','UserData',2);
    uimenu(appl_menu,'Label','Structural Mechanics, Plane Stress',...
        'CallBack','pdeModeler(''appl_cb'',3)','UserData',3);
    uimenu(appl_menu,'Label','Structural Mechanics, Plane Strain',...
        'CallBack','pdeModeler(''appl_cb'',4)','UserData',4);
    uimenu(appl_menu,'Label','Electrostatics',...
        'CallBack','pdeModeler(''appl_cb'',5)','UserData',5);
    uimenu(appl_menu,'Label','Magnetostatics',...
        'CallBack','pdeModeler(''appl_cb'',6)','UserData',6);
    uimenu(appl_menu,'Label','AC Power Electromagnetics',...
        'CallBack','pdeModeler(''appl_cb'',7)','UserData',7);
    uimenu(appl_menu,'Label','Conductive Media DC',...
        'CallBack','pdeModeler(''appl_cb'',8)','UserData',8);
    uimenu(appl_menu,'Label','Heat Transfer',...
        'CallBack','pdeModeler(''appl_cb'',9)','UserData',9);
    uimenu(appl_menu,'Label','Diffusion',...
        'CallBack','pdeModeler(''appl_cb'',10)','UserData',10);
    uimenu(opt_menu,'Label','&Refresh','Tag','PDERefresh',...
        'Separator','on','CallBack','pdeModeler refresh');

    % Draw menu:
    draw_menu = uimenu(pde_fig,'Label','D&raw',...
        'Tag','PDEDrawMenu');
    uimenu(draw_menu,'Label','&Draw Mode',...
        'Tag','PDEDrawMode','Interruptible','on',...
        'CallBack','pdeModeler(''changemode'',1)');
    draw_hndl(1)=uimenu(draw_menu,'Label','&Rectangle/square',...
        'Tag','PDERect','Interruptible','on',...
        'CallBack','pdeModeler(''drawrect'',1)');
    draw_hndl(2)=uimenu(draw_menu,'Label','Rectangle/&square (centered)',...
        'Tag','PDERectc','Interruptible','on',...
        'CallBack','pdeModeler(''drawrect'',2)');
    draw_hndl(3)=uimenu(draw_menu,'Label','&Ellipse/circle',...
        'Tag','PDEEllip','Interruptible','on',...
        'CallBack','pdeModeler(''drawellipse'',1)');
    draw_hndl(4)=uimenu(draw_menu,'Label','Ellipse/&circle (centered)',...
        'Tag','PDEEllipc','Interruptible','on',...
        'CallBack','pdeModeler(''drawellipse'',2)');
    draw_hndl(5)=uimenu(draw_menu,'Label','&Polygon',...
        'Tag','PDEPoly','Interruptible','on',...
        'CallBack','pdeModeler drawline');

    uimenu(draw_menu,'Label','R&otate...','Separator','on',...
        'Tag','PDERotate','CallBack','pdertdlg');

    set(draw_menu,'UserData',draw_hndl);

    uimenu(draw_menu,'Label','E&xport Geometry Description, Set Formula, Labels...','Tag','PDEExpGD',...
        'Separator','on','CallBack','pdeModeler(''export'',1)','Enable','off');

    % Boundary menu:
    bound_menu=uimenu(pde_fig,'Label','&Boundary',...
        'Tag','PDEBoundMenu');
    uimenu(bound_menu,'Label','&Boundary Mode ','Accelerator','b',...
        'Tag','PDEBoundMode','CallBack','pdeModeler(''boundmode'')');
  function PDEBoundMenuCallBack(~,~)
    hBoundMenu = findobj(get(pde_fig, 'Children'), 'flat', 'tag', 'PDEBoundMenu');
    h = get(hBoundMenu, 'UserData');
    if isempty(h)
      pdeModeler('error', ' No boundary selected.');
    else
      pdeModeler('set_bounds');
    end
  end

    uimenu(bound_menu,'Label','&Specify Boundary Conditions...',...
    'Tag','PDEBoundSpec','CallBack', @PDEBoundMenuCallBack);

    uimenu(bound_menu,'Label','Show &Edge Labels',...
        'Tag','PDEShowEdge','CallBack','pdeModeler(''edgelbl'')');
    uimenu(bound_menu,'Label','Show S&ubdomain Labels',...
        'Tag','PDEShowSub','CallBack','pdeModeler(''sublbl'')');
    uimenu(bound_menu,'Label','&Remove Subdomain Border','Tag','PDERemBord',...
        'Enable','off','Separator','on','CallBack','pdeModeler(''removeb'')');
    uimenu(bound_menu,'Label','Re&move All Subdomain Borders','Tag','PDERemAllBord',...
        'Enable','off','CallBack','pdeModeler(''removeb'',-1)');
    uimenu(bound_menu,'Label','E&xport Decomposed Geometry, Boundary Cond''s...','Enable','off',...
        'Separator','on','Tag','PDEExpBound','CallBack','pdeModeler(''export'',2)');

    % PDE menu:
    pde_menu=uimenu(pde_fig,'Label','P&DE',...
        'Tag','PDEPDEMenu','UserData',params);
    uimenu(pde_menu,'Label','&PDE Mode','Tag','PDEMode',...
        'CallBack','pdeModeler(''pdemode'')');
    uimenu(pde_menu,'Label','&Show Subdomain Labels','Tag','PDEShowPDESub',...
        'CallBack','pdeModeler(''pdesublbl'')');
    uimenu(pde_menu,'Label','P&DE Specification...','Tag','PDEParam',...
        'CallBack','pdeModeler(''set_param'')');
    uimenu(pde_menu,'Label','E&xport PDE Coefficients...','Tag','PDEExpParam',...
        'Separator','on','CallBack','pdeModeler(''export'',4)');

    % Mesh menu:
    mesh_menu=uimenu(pde_fig,'Label','&Mesh',...
        'Tag','PDEMeshMenu');
    uimenu(mesh_menu,'Label','&Mesh Mode','Tag','PDEMeshMenu',...
        'CallBack','pdeModeler(''meshmode'')');
    uimenu(mesh_menu,'Label','&Initialize Mesh ','Accelerator','i','Tag','PDEInitMesh',...
        'CallBack','pdeModeler(''initmesh'')');
    uimenu(mesh_menu,'Label','&Refine Mesh ','Accelerator','m','Tag','PDERefine',...
        'CallBack','pdeModeler(''refine'')');
    uimenu(mesh_menu,'Label','&Jiggle Mesh','Tag','PDEJiggle',...
        'CallBack','pdeModeler(''jiggle'')');
    uimenu(mesh_menu,'Label','&Undo Mesh Change','Tag','PDEUnrefine',...
        'Enable','off','CallBack','pdeModeler(''unrefine'')');
    uimenu(mesh_menu,'Label','&Display Triangle Quality','Tag','PDETriQual',...
        'CallBack','pdeModeler(''triqual'')');
    uimenu(mesh_menu,'Label','Show &Node Labels',...
        'Tag','PDEShowNode','CallBack','pdeModeler(''pdenodelbl'')');
    uimenu(mesh_menu,'Label','Show &Triangle Labels',...
        'Tag','PDEShowTriangles','CallBack','pdeModeler(''pdetrilbl'')');
    uimenu(mesh_menu,'Label','&Parameters...','Tag','PDEMeshParam',...
        'Separator','on','CallBack','pdemhdlg');
    uimenu(mesh_menu,'Label','E&xport Mesh...','Enable','off',...
        'Separator','on','Tag','PDEExpMesh','CallBack','pdeModeler(''export'',3)');

    % Solve menu:
    solve_menu=uimenu(pde_fig,'Label','&Solve',...
        'Tag','PDESolveMenu');
    uimenu(solve_menu,'Label','&Solve PDE ','Accelerator','e','Tag','PDESolve',...
        'CallBack','pdeModeler(''solve'')');
    uimenu(solve_menu,'Label','&Parameters...','Separator','on',...
        'CallBack','pdeModeler(''solve_param'')','Tag','PDESolveParam');
    uimenu(solve_menu,'Label','E&xport Solution...','Separator','on',...
        'Enable','off','CallBack','pdeModeler(''export'',5)','Tag','PDEExpSol');

    % Plot menu:
    plot_menu =uimenu(pde_fig,'Label','&Plot',...
        'Tag','PDEPlotMenu');

    uimenu(plot_menu,'Label','&Plot Solution ','Accelerator','p','Tag','PDEPlot',...
        'CallBack',['pdeptdlg(''initialize'',2),pdeptdlg(''plot''),',...
        'pdeinfo(''PDE solution plotted.'')']);
    pdeptdlgCallBack = @(hObject,eventdata) pdeptdlg('initialize', 0, getappdata(pde_fig, 'plotstrings'));
    uimenu(plot_menu,'Label','Parameters...','Tag','PDEPlotParam',...
        'CallBack',pdeptdlgCallBack);

    uimenu(plot_menu,'Label','E&xport Movie...','Separator','on',...
        'Tag','PDEExpMovie','Enable','off','CallBack','pdeModeler(''export'',6)');
    
    % Window menu:
    matlab.ui.internal.createWinMenu(pde_fig);

    % Help menu:
    h1 = uimenu(pde_fig,'Label','&Help','Tag','PDEHelpMenu','UserData',1);
    uimenu(h1,'Label','PDE Modeler &Help',...
        'Callback',' doc pde/pdeModeler');
    uimenu(h1,'Label','&PDE Toolbox Help', ...
        'Callback','doc pde/');
    uimenu(h1,'Label','&Examples', ...
        'Separator','on', ...
        'Callback','demo toolbox partial');
    uimenu(h1,'Label','&About the PDE Modeler',...
        'Separator','on', ...
        'Callback','pdetool about');
    
    drawnow;

    %=========================================================
    %Button info and definitions

    % load button icons

    % 1st group of buttons: drawing tools
    iconFcns1 = char('pdeicon(''rect'');', ...
        'pdeicon(''rectc'');', ...
        'pdeicon(''ellip'');', ...
        'pdeicon(''ellipc'');', ...
        'pdeicon(''polygon'');');

    buttonId=char('rect','rectc','ellip','ellipc','poly');
    callBacks=char('pdeModeler(''drawrect'',1)',...
        'pdeModeler(''drawrect'',2)',...
        'pdeModeler(''drawellipse'',1)',...
        'pdeModeler(''drawellipse'',2)',...
        'pdeModeler(''drawline'')');
    btngroup(pde_fig,'Iconfunctions',iconFcns1,'GroupID','draw',...
        'ButtonID',buttonId,'Callbacks',callBacks,'Groupsize',[1 5],...
        'Position',[0 0.96 .20 .04]);

    % 2nd group of buttons: PDE solution tools
    iconFcns2 = char('pdeicon(''DeltaOmega'');', ...
        'pdeicon(''pde'');', ...
        'pdeicon(''triangle'');', ...
        'pdeicon(''triangle2'');',...
        'pdeicon(''equal'');', ...
        'pdeicon(''plot'');');

    buttonId2=char('bounds','spec','init','refine','solve','plot');
    callBacks2=char('pdeModeler(''boundmode'')','pdeModeler(''set_param'')',...
        'pdeModeler(''initmesh'')','pdeModeler(''refine'')','pdeModeler(''solve'')',...
        ['pdeptdlg(''initialize'',0,getappdata(findobj(get(0,''Children''),',...
        '''flat'',''Tag'',''PDETool''),''plotstrings''))']);
    btngroup(pde_fig,'Iconfunctions',iconFcns2,'GroupID','solve',...
        'ButtonID',buttonId2,'Callbacks',callBacks2,'Groupsize',[1 6],...
        'PressType','flash','Position',[0.20 0.96 .24 .04]);

    zoomicon = char('pdeicon(''zoom'');');
    btngroup(pde_fig,'Iconfunctions',zoomicon,'GroupID','zoom',...
        'ButtonID','zoom','Callbacks','pdeModeler(''zoom'')','Groupsize',[1 1],...
        'Position',[0.44 0.96 .04 .04]);

    % Application indicator:
    uicontrol(pde_fig,'Style','frame','Units','normalized',...
        'Position',[0.48 0.96 .28 .04]);
    uicontrol(pde_fig,'Style','popup','Units','normalized',...
        'HorizontalAlignment','left','String',...
        ['Generic Scalar|Generic System|Structural Mech., Plane Stress|',...
        'Structural Mech., Plane Strain|Electrostatics|Magnetostatics|',...
        'AC Power Electromagnetics|Conductive Media DC|',...
        'Heat Transfer|Diffusion'],...
        'Position',[0.485 0.965 .27 .03],'Tag','PDEAppl','UserData',1,...
        'Callback','pdeModeler(''appl_cb'',0)');

    % X and Y positions:
    uicontrol(pde_fig,'Style','frame','Units','normalized',...
        'Position',[0.76 0.96 .24 .04]);
    uicontrol(pde_fig,'Style','text',...
        'Units','normalized',...
        'Position',[.77 .965 .03 .03],...
        'ForegroundColor','k','String','X:');
    xh=uicontrol(pde_fig,'Style','text','Units','normalized',...
        'HorizontalAlignment','left','Tag','PDEXField',...
        'Position',[.8 .965 .075 .03],'String','0.0');
    uicontrol(pde_fig,'Style','text',...
        'Units','normalized',...
        'Position',[.89 .965 .03 .03],...
        'ForegroundColor','k','String','Y:');
    yh=uicontrol(pde_fig,'Style','text','Units','normalized',...
        'HorizontalAlignment','left','Tag','PDEYField',...
        'Position',[.92 .965 .075 .03],'String','0.0');

    % Set formula:
    uicontrol(pde_fig,'Style','frame','Units','normalized',...
        'Position',[0 0.9 1 .06]);
    uicontrol(pde_fig,'Style','text',...
        'Units','normalized', 'HorizontalAlignment','left',...
        'Position',[.03 .91 .12 .04],...
        'String','Set formula:');
    uicontrol(pde_fig,...
        'Style','edit',...
        'Units','normalized',...
        'Position',[.15 .91 .8 .04],...
        'HorizontalAlignment','left',...
        'BackgroundColor','w',...
        'Enable','on',...
        'Tag','PDEEval',...
        'Callback','pdeModeler formchk',...
        'String','',...
        'UserData','');

    % Info string display:
    uicontrol(pde_fig,'Style','frame','Units','normalized',...
        'Position',[0  0 .86 .06]);
    uicontrol(pde_fig,'Style','text',...
        'Units','normalized','HorizontalAlignment','right',...
        'Position',[.02 .01 .1  .04],...
        'String','Info:');
    uicontrol(pde_fig,'Style','text','Units','normalized',...
        'Position',[.14 .01 .7 .04],'HorizontalAlignment','left',...
        'Value',1,'Tag','PDEInfo','String','Draw 2-D geometry.',...
        'Callback','Draw 2-D geometry.',...
        'UserData','Draw 2-D geometry.');

    % Exit button
    uicontrol(pde_fig,'Style','frame','Units','normalized',...
        'Position',[0.86 0 .14 .06]);
    uicontrol(pde_fig,'Style','pushbutton',...
        'Interruptible','on','Units','normalized',...
        'Position',[.88 .01 .1 .04],...
        'Callback','pdeModeler(''exit'')','String','Exit');

    % Initialize property containers:
    setappdata(pde_fig,'toolhelp',1);
    setappdata(pde_fig,'stick',zeros(6,1));
    setappdata(pde_fig,'application',1);
    setappdata(pde_fig,'equation','-div(c*grad(u))+a*u=f');
    setappdata(pde_fig,'params',char('c','a','f','d'));
    setappdata(pde_fig,'description',char([],[],[],[]));
    setappdata(pde_fig,'bounddescr',char([],[],[],[]));
    setappdata(pde_fig,'boundeq',...
        char('n*c*grad(u)+qu=g','h*u=r',[]))
    setappdata(pde_fig,'objnames',[]);
    setappdata(pde_fig,'showsublbl',0);
    setappdata(pde_fig,'showpdesublbl',0);
    setappdata(pde_fig,'showedgelbl',0);
    setappdata(pde_fig,'shownodelbl',0);
    setappdata(pde_fig,'showtrilbl',0);
    setappdata(pde_fig,'trisize',[])
    setappdata(pde_fig,'Hgrad',1.3)
    setappdata(pde_fig,'refinemethod','regular')
    setappdata(pde_fig,'jiggle',char('on','mean',[]))
    setappdata(pde_fig,'MesherVersion', 'preR2013a');
    setappdata(pde_fig,'meshstat',[])
    setappdata(pde_fig,'bl',[])
    setappdata(pde_fig,'animparam',[6 5 0])
    setappdata(pde_fig,'colstring','')
    setappdata(pde_fig,'arrowstring','')
    setappdata(pde_fig,'deformstring','')
    setappdata(pde_fig,'heightstring','')
    setappdata(pde_fig,'plotflags',[1 1 1 1 1 1 1 1 0 0 0 1 1 0 0 0 0 1])
    % [colvar colstyle heightvar heightstyle vectorvar vectorstyle
    % colval doplot xyplot showmesh animationflag popupvalue
    % colflag contflag heightflag vectorflag deformflag deformvar]

    str1='u|abs(grad(u))|abs(c*grad(u))|user entry';
    str2=' -grad(u)| -c*grad(u)| user entry';
    strmtx=char(str1,str2);
    setappdata(pde_fig,'plotstrings',strmtx)
    setappdata(ax,'snap',0);
    setappdata(ax,'subsel',[]);
    setappdata(ax,'bordsel',[]);
    setappdata(ax,'extraspacex','');
    setappdata(ax,'extraspacey','');
    setappdata(ax,'selinit',0);

    pde_i=0;
    % PDETool figure's UserData contains the following:
    % 1-2: handles to X and Y position text objects
    % 3-4: pde_xc: circle center coordinates
    % 5: pde_i: counter: number of lines drawn when drawing a polygon.
    % 6..6+3*pde_i-2: pde_x: x data for lines (1:pde_i), y data for
    % lines (1:pde_i), handles to line objects (1:pde_i-1)
    user_data = double([xh yh 0.0 0.0 pde_i 0.0 0.0]);

    set(pde_fig,'WindowButtonMotionFcn','pdemtncb(0)',...
        'WindowButtonDownFcn','pdeselect select',...
        'Pointer','arrow',...
        'HandleVisibility','callback',...
        'UserData',user_data)
    set(findobj(allchild(pde_fig),'flat','Type','axes'),...
        'HandleVisibility','callback')
    drawnow;
end