⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psn_2d_demo.m

📁 Finite Volume Poisson PDE Solver
💻 M
字号:
function varargout = psn_2d_demo(varargin)
% PSN_2D_DEMO M-file for psn_2d_demo.fig
%      PSN_2D_DEMO, by itself, creates a new PSN_2D_DEMO or raises the existing
%      singleton*.
%
%      H = PSN_2D_DEMO returns the handle to a new PSN_2D_DEMO or the handle to
%      the existing singleton*.
%
%      PSN_2D_DEMO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PSN_2D_DEMO.M with the given input arguments.
%
%      PSN_2D_DEMO('Property','Value',...) creates a new PSN_2D_DEMO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before psn_2d_demo_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to psn_2d_demo_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help psn_2d_demo

% Last Modified by GUIDE v2.5 14-Dec-2003 15:20:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @psn_2d_demo_OpeningFcn, ...
                   'gui_OutputFcn',  @psn_2d_demo_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 psn_2d_demo is made visible.
function psn_2d_demo_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 psn_2d_demo (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% This sets up the initial plot - only do when we are invisible
% so window can get raised using psn_2d_demo.
set(handles.listbox1,'Value',2);
set(handles.popupmenu2,'Value',3);
set(handles.listbox2,'Value',2);
if strcmp(get(hObject,'Visible'),'off')
   listbox1_Callback(hObject, eventdata, handles)
end

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


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

% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


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

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'comet(cos(1:.01:10))', 'bar(1:10)', 'plot(membrane)', 'surf(peaks)'});

% --- Executes on selection change in popupmenu3.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3

% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% --- Executes during object creation, after setting all properties.function listbox1_CreateFcn(hObject, eventdata, handles)% hObject    handle to listbox1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: listbox controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes on selection change in listbox1.function listbox1_Callback(hObject, eventdata, handles)% hObject    handle to listbox1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array%        contents{get(hObject,'Value')} returns selected item from listbox1axes(handles.axes1);
cla;

cmCharge=1;
cmCircle=2;
cmCapacitor=3;
cmDam=4;
cmNiagara=5;
cmIonic=6;
cmBlackhole=7;
cmError=8;
cmConvergence=9;
cmFreehand=10;

met = get(handles.listbox2, 'Value');
crd=0;
grid=1;
Nx=49;
Ny=49;
% enum INPPARAM {
par(1)=Nx;%    INP_NX,        //Number of INTERIOR points in X-dirextion
par(2)=Ny;%    INP_NY,        //Number of INTERIOR points in Y-dirextion
par(3)=1;%    INP_LX,        //X-size
par(4)=1;%    INP_LY,        //Y-size
par(5)=0;%    INP_GRID,      //Grid: 0-standard, 1 - centered
par(6)=met;%    INP_METHOD,    //Solving method from PSN_METHODS
par(7)=1e-11;%    INP_TOL,       //Tolerance
par(8)=4000;%    INP_MAXSTEP,   //Max steps
par(9)=2-4/Nx; %INP_OMEGA 
par(10)=crd;   %COORD   //0 - cartesian, 1-cylindrical
%    //Boundary conditions
par(11)=1;%    INP_ALPHAX0,
par(12)=1;%    INP_ALPHAX1,
par(13)=0;%    INP_BETAX0,
par(14)=0;%    INP_BETAX1,
par(15)=1; %    INP_ALPHAY0,
par(16)=1;%    INP_ALPHAY1,
par(17)=0;%    INP_BETAY0,
par(18)=0;%   INP_BETAY1
h=1/Nx;
v=zeros(Ny+2, Nx+2);
e=ones(Ny+2,Nx+2);
v1=ones(Ny+2,Nx+2);
u1=[];
ind = get(handles.listbox1, 'Value');
tol_ind = get(handles.popupmenu2, 'Value');
switch (tol_ind)
    case 1
       tol=1e-6; 
    case 2
       tol=1e-8; 
    case 3
       tol=1e-10; 
    case 4
       tol=1e-12;
   otherwise 
       tol=1e-10;
end    
par(7)=tol;

switch ind
    case {cmCharge, cmCircle}
      v((Nx+1)/2,(Ny+1)/2)=-50/h/h;
      if ind==cmCircle
         e(:,:)=1;
         for i=1:Ny
            for j=1:Nx
              Cx=(Nx+1)/2;
              Cy=(Ny+1)/2;
              Rc=Nx*0.3;
              r=sqrt((i-Cy).^2+(j-Cx).^2);
              if r<Rc 
                 e(i,j)=5;
             end;    
           end;
         end;    
       end 
   case cmCapacitor
      v(:,Nx+2)=1;
      par(15)=0; %    INP_ALPHAY0,
      par(16)=0;%    INP_ALPHAY1,
      par(17)=1;%    INP_BETAY0,
      par(18)=1;%   INP_BETAY1
    case cmDam
      v(:,Nx+2)=1;
      par(15)=0; %    INP_ALPHAY0,
      par(16)=0;%    INP_ALPHAY1,
      par(17)=1;%    INP_BETAY0,
      par(18)=-1;%   INP_BETAY1
      e(:,:)=80;
      for i=1:Ny+1
         for j=(Nx+1)/2-5:(Nx+1)/2+5
           e(i,j)=2;
         end; 
     end;   
    case {cmNiagara, cmIonic}
      v(:,Nx+2)=1;
      par(15)=0; %    INP_ALPHAY0,
      par(16)=0;%    INP_ALPHAY1,
      par(17)=1;%    INP_BETAY0,
      par(18)=-1;%   INP_BETAY1
      e(:,:)=80;
      for i=1:Ny+1
         for j=(Nx+1)/2-5:(Nx+1)/2+5
           if (abs(i-(Ny+1)/2)>5)
               e(i,j)=2;
           end;    
         end; 
     end;   
     if ind==cmIonic
        v((Ny+1)/2,(Nx+1)/2+7)=-20/h/h; 
     end    
 case cmBlackhole
      e(:,:)=100;
      v(:,:)=100;
       for i=1:Ny
            for j=1:Nx
              Cx=(Nx+1)/2;
              Cy=(Ny+1)/2;
              Rc=Nx*0.3;
              r=sqrt((i-Cy).^2+(j-Cx).^2);
              if r<Rc 
                 e(i,j)=1;
             end;    
           end;
       end;    
       v(:,1)=0;
       v(:,Nx+2)=0;
       v(1,:)=0;
       v(Ny+2,:)=0;
    case {cmError,cmConvergence}
       v(:,1)=1;
       v(:,Nx+2)=1;
       v(1,:)=1;
       v(Ny+2,:)=1;
       u1=ones(Ny+2,Nx+2);
       
   case cmFreehand
      xls=linspace(0,1,Nx+2);
      yls=linspace(0,1,Ny+2);
      v(1,:)=1+xls.^2;
      v(:,1)=1-yls'.^2;
      v(Ny+2,:)=xls.^2;
      v(:,Nx+2)=2-yls'.^2;
      for i=1:Ny+2
          for j=1:Nx+2
              u1(i,j)=1+xls(j).^2-yls(i).^2;
          end;
      end;
      
end

tic;
[u, out_par, step_err]=psn_2d_mex(par, v,e);
tm=toc;
s=sprintf('Iteration Num=%u',out_par(2));
set(handles.text3,'String',s);
s=sprintf('Elapsed time=%f s',tm);
set(handles.text4,'String',s);
switch ind
    case cmError
      surf((u-u1));  
  case cmConvergence
      semilogy(step_err);
  otherwise
      surf(u);
end
colormap('jet');
% --- Executes during object creation, after setting all properties.function listbox2_CreateFcn(hObject, eventdata, handles)% hObject    handle to listbox2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: listbox controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes on selection change in listbox2.function listbox2_Callback(hObject, eventdata, handles)% hObject    handle to listbox2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns listbox2 contents as cell array%        contents{get(hObject,'Value')} returns selected item from listbox2listbox1_Callback(hObject,eventdata,handles);% --- Executes during object creation, after setting all properties.function popupmenu2_CreateFcn(hObject, eventdata, handles)% hObject    handle to popupmenu2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes on selection change in popupmenu2.function popupmenu2_Callback(hObject, eventdata, handles)% hObject    handle to popupmenu2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns popupmenu2 contents as cell array%        contents{get(hObject,'Value')} returns selected item from popupmenu2listbox1_Callback(hObject,eventdata,handles);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -