%单位：长沙理工大学能源与动力工程学院
%作者：姜昌伟
%时间：2005年12月7日
% 二维导热问题
% 控制容积法
% SOR

function varargout = heat_conduction(varargin)
% HEAT_CONDUCTION M-file for heat_conduction.fig
%      HEAT_CONDUCTION, by itself, creates a new HEAT_CONDUCTION or raises the existing
%      singleton*.
%
%      H = HEAT_CONDUCTION returns the handle to a new HEAT_CONDUCTION or the handle to
%      the existing singleton*.
%
%      HEAT_CONDUCTION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HEAT_CONDUCTION.M with the given input arguments.
%
%      HEAT_CONDUCTION('Property','Value',...) creates a new HEAT_CONDUCTION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before heat_conduction_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to heat_conduction_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 heat_conduction

% Last Modified by GUIDE v2.5 27-Mar-2006 23:29:54

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = heat_conduction_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 lambda_Callback(hObject, eventdata, handles)
% hObject    handle to lambda (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of lambda as text
%        str2double(get(hObject,'String')) returns contents of lambda as a double
lambda = str2double(get(hObject, 'String'));
if isnan(lambda)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end


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

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


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


lambda = str2double(get(handles.lambda, 'String'));   % 导热系数
alfa   = str2double(get(handles.edit2, 'String'));  % 对流换热系数
density = str2double(get(handles.edit3, 'String'));  % 密度
cp = str2double(get(handles.edit4, 'String'));      % 比热
Tfluid = str2double(get(handles.edit5, 'String'));  % 流体温度

x=[];y=[];T=[];Told=[];Su=[];Sp=[];ap=[];ae=[];aw=[];as=[];an=[];
great = 1.e20;

dt = great; % 时间步. 如果dt很大，可作稳态处理
Lx = 0.12; % x方向长度
Ly = 0.12; % Y方向长度
Tinit = 50; % 初始温度场设定、顶部和底部温度设定
%cv_x = input('x方向网格数 = ')
%cv_y = input('y方向网格数 = ')
cv_x=10;cv_y=10;
ni = cv_x+2; % x方向网格点
nj = cv_y+2; % Y方向网格点
dx = Lx/cv_x;
dy = Ly/cv_y;
x(1) = 0;
x(2)=dx/2;
for i = 3:ni-1
   x(i)=x(i-1)+dx;
end;
x(ni)=Lx;
y(1) = 0;
y(2)=dy/2;
for j = 3:nj-1
   y(j)=y(j-1)+dy;
end
y(nj)=Ly;
% 初始化值与系数
for i = 1:ni
  for j = 1:nj
    T(i,j) = Tinit;  %温度初始化
    Told(i,j) = Tinit;
    T(i,1) = 50;
    T(i,nj) = 50;
    Su(i,j)=0;       %初始化独立源项
    Sp(i,j)=0;       %初始化依靠源项
    ae(i,j) = lambda*dy/dx;
    aw(i,j) = lambda*dy/dx;
    an(i,j) = lambda*dx/dy;
    as(i,j) =lambda*dx/dy;
    dV = dx*dy;
    ap0 = density*cp*dV/dt;
    if i==2  % 对流换热系数
       Su(i,j) = Tfluid/(1/alfa+dx/(2*lambda))*dy/dV;
       Sp(i,j) = -1/(1/alfa+dx/(2*lambda))*dy/dV;
       aw(i,j) = 0;
    end;
    if i==ni-1 % 绝热
       ae(i,j) = 0;
    end
    if j==2 %下部，温度边界
       as(i,j)=2*lambda*dx/dy;
    end   
    if j==nj-1 % 上部，温度边界
       an(i,j)=2*lambda*dx/dy;
    end   
    ap(i,j) = ae(i,j)+aw(i,j)+an(i,j)+as(i,j)-Sp(i,j)*dV+ap0;
  end;
end;
%%%%%%%%%%%
maxres = 1.0e-6;
maxit = 100;
time=0;
maxtime=100;
s=(cos(pi/cv_x)+(dx/dy)^2*cos(pi/cv_y))/(1+(dx/dy)^2);
omega =2/(1+sqrt(1-s^2));omega=1;
while (time < (maxtime+dt/2))
 Told=T;  
 sumres = 1;
 counter = 0;
 while (sumres>maxres&counter<maxit)
  sumres = 0;
  for i = 2:ni-1
   for j = 2:nj-1
    T(i,j)=omega*(ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+an(i,j)*T(i,j+1)...
       +as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j))/ap(i,j)+(1-omega)*T(i,j);
    res =  abs(ap(i,j)*T(i,j)-(ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+...
      an(i,j)*T(i,j+1)+as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j)));
    sumres=sumres+res;
   end;
  end;
  for i = 2:ni-1
   for j = 2:nj-1
    res =  abs(ap(i,j)*T(i,j)-(ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+...
      an(i,j)*T(i,j+1)+as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j)));
    sumres=sumres+res;
   end;
  end

  sumerr=sumres
  counter = counter + 1
 end;
 time = time +dt;
end; 
%计算边界值
 for j = 2:nj-1
   T(1,j)=(alfa*Tfluid+lambda/(dx/2)*T(2,j))/(alfa+lambda/(dx/2));
   T(ni,j) = T(ni-1,j);
end;   
%
pcolor(x,y,T');shading interp;xlabel('x方向');ylabel('y方向');title('温度场');
colorbar;



% --- 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 listbox1


% --- 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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end







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

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


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

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



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

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


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

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



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

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


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

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



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

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


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

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


