You are on page 1of 8

IAM 760, Tansu SEVINE - HW 1

Due December 22, 2017

Instructor: Prof. Dr. Bulent KARASOZEN

Tansu Sevine
1679950, Dept. of Aerospace Eng.
April 22, 2018

1 Objective
Objective of this project is to write a MATLAB code to calculate heat diffusion of a body of 2D using
finite difference methods. Then, calculate POD basis and reconstruct temperature fields based on POD
modes.

2 Method

3 Results and Discussion


In order to illustrate the Myers’ figures, 2 MATLAB codes are written. The first one makes calculations
about ice thickness and draws the figures 2 and 3 of Myers. The second code calculates freezing fractions
and plots the figures 5 and 6. The parameters of the calculations are used exactly as in the Myers’ paper
(Figure ??).

4 Conclusion
In this study, a MATLAB code is written to calculate ice thickness at which the glaze ice starts to
build up, Bg ; ice thickness, B; freezing fraction, F F and water layer thickness, h at given ambient and
substrate temperatures, Ta and Ts is successfully. The results are compared with [Myers, 2001] paper.
Re-illustration of the Figure 2 (Ice and water growth for Ts = Ta = 263.15K and 270K), Figure 3 (Bg vsTa
with Ts = 263.15K Ta = 270K and Ts = Ta = 270K), Figure 5 (Variation of freezing fraction with time
for Ta = Ts = 263.15K), Figure 6 (Variation of freezing fraction with time for Ta = Ts = 270K) are done.
It is seen that results are near perfectly matching.

1
Main MATLAB code for calculations and plots
1 clc;clear all; close all; format compact; format shortg
2 nx = 101;
3 ny = 101;
4 dt = 0.1; % time step
5 casenumber = 2; % use 1 or 2 only for case1 (c =0.01), case2(c=0.05)
6 prompt = ’Enter method name "IE", "CN", "RS" >> ’;
7 method = input(prompt,’s’)
8
9 if(casenumber == 1)
10 disp(’1st case selected’)
11 ka = 0.01; % thermal diffisuvity
12 else
13 disp(’2nd case selected’)
14 ka = 0.1;
15 end
16
17 %% Defaults for figures
18 width = 8; % Width in inches
19 height = 10; % Height in inches
20 alw = 0.2; % AxesLineWidth
21 fsz = 14; % Fontsize
22 lw = 1.5; % LineWidth
23 msz = 8; % MarkerSize
24 % The properties we’ve been using in the figures
25 set(0,’defaultLineLineWidth’,lw); % set the default line width to lw
26 set(0,’defaultLineMarkerSize’,msz); % set the default line marker size to msz
27 set(0,’defaultLineLineWidth’,lw); % set the default line width to lw
28 set(0,’defaultLineMarkerSize’,msz); % set the default line marker size to msz
29
30 % Set the default Size for display
31 defpos = get(0,’defaultFigurePosition’);
32 set(0,’defaultFigurePosition’, [100 -50 width*100, height*100]);
33
34 % Set the defaults for saving/printing to a file
35 set(0,’defaultFigureInvertHardcopy’,’on’); % This is the default anyway
36 set(0,’defaultFigurePaperUnits’,’inches’); % This is the default anyway
37 defsize = get(gcf, ’PaperSize’);
38 left = (defsize(1)- width)/2;
39 bottom = (defsize(2)- height)/2;
40 defsize = [left, bottom, width, height];
41 set(0, ’defaultFigurePaperPosition’, defsize);
42
43 %% Preperation Section
44 [A,dx,dy,x,y,Number] = preparation(nx,ny);
45 [X,Y] = meshgrid(x,y);
46 % T0 = ones(nx*ny ,1)*10;
47 for j=1:ny
48 for i=1:nx
49 if(casenumber == 1)
50 T01(i,j) = sin(2*pi*x(i))*sin(pi*y(i)); % case 1
51 else
52 if(x(i) > 0.249 && x(i)< 0.751 && y(j) > 0.249 && y(j)< 0.751) % case 2
53 T01(i,j) = 1;
54 else
55 T01(i,j) = 0;
56 end
57 end
58 T0(Number(i,j),1) = T01(i,j);
59 end
60 end
61 figure
62 pcolor(X,Y,T01), shading interp, colorbar, colormap(’jet’);
63 title(’Initial temperature distribution’)
64 [T0] = add_boundary(T0,nx,ny,Number);
65
66 %% Solution Section
67 % [sln_E,time_E] = solve_heat_fdm(ka,A,nx,ny,Number,dx,dy,dt,T0,’IE’);
68 % [sln_CN,time_CN] = solve_heat_fdm(ka,A,nx,ny,Number,dx,dy,dt,T0,’CN’);
69 % [sln_RS,time_RS] = solve_heat_fdm(ka,A,nx,ny,Number,dx,dy,dt,T0,’RS’);
70 [sln,time] = solve_heat_fdm(ka,A,nx,ny,Number,dx,dy,dt,T0,method);
71
72 num =1;
73 for j=1:ny
74 for i=1:nx
75 slnij(i,j) = sln(num,end);
76 % euler_sln(i,j) = sln_E(num,end);
77 % CN_sln(i,j) = sln_CN(num,end);
78 % RS_sln(i,j) = sln_RS(num,end);
79 num = num+1;
80 end
81 end
82 % %% Plot Section
83 % figure
84 % subplot(3,1,1)
85 % pcolor(Y,X,euler_sln); shading interp, colorbar, colormap(’jet’);
86 % title([’EULER, final time= ’ num2str(time_E), ’, # of tstep= 100 ’])
87 % subplot(3,1,2)
88 % pcolor(Y,X,CN_sln); shading interp, colorbar, colormap(’jet’);
89 % title([’CN, final time= ’ num2str(time_CN), ’, # of tstep= 100 ’])
90 % subplot(3,1,3)
91 % pcolor(Y,X,RS_sln); shading interp, colorbar, colormap(’jet’);
92 % title([’RS, final time= ’ num2str(time_RS), ’, # of tstep= 100 ’])
93 %% POD calculation
94 % sent snapshot matrix to this function, returns POD basis and Singular values
95 [U,SS,coef] = podhesap(sln);
96 %% weight matrix
97 W_e = weight_matrix(SS,’E’); % for Euclidian norm
98 W_l2 = weight_matrix(SS,’L2’); % for L2 norm
99 %% reconstruction section
100 rank = 10 ; % rank number for reduced order model
101 red_coef = coef(1:rank,:);
102 rA = U(:,1:rank)*red_coef ;
103 error = rms(rms(rA-sln))
104 tcontrol = 40;
105 num = 1;
106 for j=1:ny
107 for i=1:nx
108 sln_control(i,j) = sln(num,tcontrol);
109 r_control(i,j) = rA(num,tcontrol);
110 num = num+1;
111 end
112 end
113
114
115
116 close all
117
118 figure
119 subplot(3,1,1)
120 pcolor(Y,X,sln_control); shading interp, colorbar, colormap(’jet’);
121 title([’Real solution at time= ’ num2str(tcontrol) ])
122 subplot(3,1,2)
123 pcolor(Y,X,r_control); shading interp, colorbar, colormap(’jet’);
124 title([’Approximated solution with reduced order model’ ])
125 subplot(3,1,3)
126 pcolor(Y,X,abs(sln_control - r_control)); shading interp, colorbar, colormap(’jet’);
127 title([’Abs. error distribution, | Real - Reduced order model |’])
128 suptitle([’Method : ’ method, ’, Rank: ’ num2str(rank), ’, Kappa= ’ num2str(ka)])
129
130 filename = [ ’figs/’ method ’_t’ num2str(tcontrol) ’_r’ num2str(rank) ’_c’ num2str(ka*100) ’.png’ ];
131 print(figure(1),filename,’-dpng’)

MATLAB function for preparation


1 function [A,dx,dy,x,y,Number] = preparation(nx,ny)
2 nxgrid = nx; nygrid = ny;
3 x1 = 0 ; x2 = 1;
4 y1 = 0 ; y2 = 1;
5 x = linspace(x1,x2,nxgrid);
6 y = linspace(y1,y2,nygrid);
7 dx = (x2 - x1)/(nxgrid-1);
8 dy = (y2 - y1)/(nygrid-1);
9 num = 1;
10 for j=1:nygrid
11 for i=1:nxgrid
12
13 Number(i,j) = num;
14 num = num+1;
15 end
16 end
17 % Construct the T matrix
18 A = sparse(nxgrid*nygrid,nxgrid*nygrid);
19 end

MATLAB function for solving finite difference methods


1 function [sol,time] = solve_heat_fdm(ka,A,nx,ny,Number,dx,dy,dt,T0,method)
2 TOL = 1e-8;
3 itmax = 100;
4 io=1;
5 if(method == ’IE’)
6 ax = ka*dt/dxˆ2;
7 ay = ka*dt/dyˆ2;
8
9 for j = 2:ny-1
10 for i = 2:nx-1
11 ii = Number(i,j);
12 A( ii, Number(i+1,j )) = -ax;
13 A( ii, Number(i ,j+1)) = -ay;
14 A( ii,ii) = 1+2*ax+2*ay;
15 A( ii, Number(i-1,j )) = -ax;
16 A( ii, Number(i ,j-1)) = -ay;
17 end
18 end
19 % Set left and right BC
20 for i = 1:nx
21 ii = Number(i,1);
22 ik = Number(i,end);
23 A(ii,ii) = 1;
24 A(ik,ik) = 1;
25 end
26 % Set lower and upper BC
27 for j = 1:ny
28 jj = Number(1,j);
29 jk = Number(end,j);
30 A(jj,jj) = 1;
31 A(jk,jk) = 1;
32 end
33 Tn = T0;
34 time = 0;
35 for itr=1:itmax % solution loop for marching time
36 Tn = add_boundary(Tn,nx,ny,Number);
37 Tnp1 = A \ Tn;
38 if( mod(itr,io) == 0)
39 k = itr/io;
40 sol(:,k) = Tn;
41 end
42 error = max(abs(Tn-Tnp1));
43 if error < TOL
44 break
45 end
46 Tn = Tnp1;
47 time = time +dt;
48 end
49 disp( ’iteration#, time, error, TOL for implicit Euler’)
50 disp( [itr time error TOL] )
51
52 sln = zeros(nx,ny,length(sol(1,:)));
53 for itr = 1:length(sol(1,:))
54 num = 1;
55 for j=1:ny
56 for i=1:nx
57 sln(i,j,itr) = sol(num,itr);
58 T_in(i,j) = T0(num);
59 num = num+1;
60 end
61 end
62 end
63
64
65 elseif(method == ’CN’);
66 ax = ka*dt/dxˆ2;
67 ay = ka*dt/dyˆ2;
68 time = 0;
69 for j = 2:ny-1
70 for i = 2:nx-1
71 ii = Number(i,j);
72 A( ii, Number(i+1,j )) = -ax;
73 A( ii, Number(i ,j+1)) = -ay;
74 A( ii,ii) = 2+2*ax+2*ay;
75 A( ii, Number(i-1,j )) = -ax;
76 A( ii, Number(i ,j-1)) = -ay;
77 end
78 end
79 % Set left and right BC
80 for i = 1:nx
81 ii = Number(i,1);
82 ik = Number(i,end);
83 A(ii,ii) = 1;
84 A(ik,ik) = 1;
85 end
86 % Set lower and upper BC
87 for j = 1:ny
88 jj = Number(1,j);
89 jk = Number(end,j);
90 A(jj,jj) = 1;
91 A(jk,jk) = 1;
92 end
93
94 Tn = T0;
95 rhs = T0;
96 for itr=1:itmax % solution loop for marching time
97 for j = 2:ny-1
98 for i = 2:nx-1
99 rhs(Number(i,j) ) = Tn(Number(i,j) ) * (2-2*ax-2*ay) + Tn(Number(i+1,j) ) * ax + Tn(Number(i,j+1) ) * ay+ Tn(Number(i-1,j) ) * ax+Tn(
Number(i,j-1) ) * ay;
100 end
101 end
102
103 rhs = add_boundary(rhs,nx,ny,Number);
104
105
106 Tnp1 = A \ rhs;
107
108 num = 1;
109 for j=1:ny
110 for i=1:nx
111 T_in(i,j) = T0(num);
112 Tn1(i,j) = Tn(num);
113 Tnp(i,j) = Tnp1(num);
114 rhs1(i,j) = rhs(num);
115 num = num+1;
116 end
117 end
118 if( mod(itr,io) == 0)
119 k = itr/io;
120 sol(:,k) = Tn;
121 end
122 error = max(abs(Tn-Tnp1));
123 if error < TOL
124 break
125 end
126 Tn = Tnp1;
127 time = time +dt;
128 end
129 disp( ’iteration#, time, error, TOL for Crank Nicolson’)
130 disp( [itr time error TOL] )
131
132 sln = zeros(nx,ny,length(sol(1,:)));
133 for itr = 1:length(sol(1,:))
134 num = 1;
135 for j=1:ny
136 for i=1:nx
137 sln(i,j,itr) = sol(num,itr);
138 T_in(i,j) = T0(num);
139 num = num+1;
140 end
141 end
142 end
143
144
145 elseif(method == ’RS’);
146 dt = dt/2;
147 ax = ka*dt/dxˆ2;
148 ay = ka*dt/dyˆ2;
149 time = 0;
150 for j = 2:ny-1
151 for i = 2:nx-1
152 ii = Number(i,j);
153 A( ii, Number(i+1,j )) = -ax;
154 A( ii, Number(i ,j+1)) = -ay;
155 A( ii,ii) = 1+2*ax+2*ay;
156 A( ii, Number(i-1,j )) = -ax;
157 A( ii, Number(i ,j-1)) = -ay;
158 end
159 end
160 % Set left and right BC
161 for i = 1:nx
162 ii = Number(i,1);
163 ik = Number(i,end);
164 A(ii,ii) = 1;
165 A(ik,ik) = 1;
166 end
167 % Set lower and upper BC
168 for j = 1:ny
169 jj = Number(1,j);
170 jk = Number(end,j);
171 A(jj,jj) = 1;
172 A(jk,jk) = 1;
173 end
174 Tn = T0;
175 time = 0;
176 for itr=1:4 % solution loop for marching time
177 Tn = add_boundary(Tn,nx,ny,Number);
178
179 Tnp1 = A \ Tn;
180 if( mod(itr,io) == 0)
181 k = itr/io;
182 sol(:,k) = Tn;
183 end
184
185 Tn = Tnp1;
186 time = time +dt;
187 end
188
189
190 dt = dt*2;
191 ax = ka*dt/dxˆ2;
192 ay = ka*dt/dyˆ2;
193
194 for j = 2:ny-1
195 for i = 2:nx-1
196 ii = Number(i,j);
197 A( ii, Number(i+1,j )) = -ax;
198 A( ii, Number(i ,j+1)) = -ay;
199 A( ii,ii) = 2+2*ax+2*ay;
200 A( ii, Number(i-1,j )) = -ax;
201 A( ii, Number(i ,j-1)) = -ay;
202 end
203 end
204 % Set left and right BC
205 for i = 1:nx
206 ii = Number(i,1);
207 ik = Number(i,end);
208 A(ii,ii) = 1;
209 A(ik,ik) = 1;
210 end
211 % Set lower and upper BC
212 for j = 1:ny
213 jj = Number(1,j);
214 jk = Number(end,j);
215 A(jj,jj) = 1;
216 A(jk,jk) = 1;
217 end
218 rhs = Tn;
219 for itr=5:itmax % solution loop for marching time
220 for j = 2:ny-1
221 for i = 2:nx-1
222 rhs(Number(i,j) ) = Tn(Number(i,j) ) * (2-2*ax-2*ay) + Tn(Number(i+1,j) ) * ax + Tn(Number(i,j+1) ) * ay+ Tn(Number(i-1,j) ) * ax+Tn(
Number(i,j-1) ) * ay;
223 end
224 end
225
226 rhs = add_boundary(rhs,nx,ny,Number);
227 Tnp1 = A \ rhs;
228 num = 1;
229 for j=1:ny
230 for i=1:nx
231 T_in(i,j) = T0(num);
232 Tn1(i,j) = Tn(num);
233 Tnp(i,j) = Tnp1(num);
234 rhs1(i,j) = rhs(num);
235 num = num+1;
236 end
237 end
238 if( mod(itr,io) == 0)
239 k = itr/io;
240 sol(:,k) = Tn;
241 end
242 error = max(abs(Tn-Tnp1));
243 if error < TOL
244 break
245 end
246 Tn = Tnp1;
247 time = time +dt;
248 end
249
250
251 disp( ’iteration#, time, error, TOL for RS’)
252 disp( [itr time error TOL] )
253
254 sln = zeros(nx,ny,length(sol(1,:)));
255 for itr = 1:length(sol(1,:))
256 num = 1;
257 for j=1:ny
258 for i=1:nx
259 sln(i,j,itr) = sol(num,itr);
260 T_in(i,j) = T0(num);
261 num = num+1;
262 end
263 end
264 end
265
266 end
267
268
269 end

MATLAB function for weight matrix


1 function nr = weight_matrix(SS,wtype)
2 wtype
3 ntime = length(SS);
4
5 if (wtype == ’L2’)
6 nSS = norm(SS,2);
7 for i=1:ntime
8 nr(i,1) = norm(SS(1:i),2)/nSS;
9 end
10 format longg
11 nr(1:10)
12
13 else (wtype == ’E’);
14 nSS = norm(SS);
15
16 for i=1:ntime
17 nr(i,1) = norm(SS(1:i),2)/nSS;
18 end
19 format longg
20 nr(1:10)
21 end
22 end

MATLAB function for POD calculations


1 function [U,SS,w] = podhesap(slnvec)
2
3 [U,S,V] = svd(slnvec,0); % svd calculation
4 SS = diag(S);
5 ntime = length(SS);
6 w = S*V’;
7
8 width = 8; % Width in inches
9 height = 6; % Height in inches
10 alw = 0.2; % AxesLineWidth
11 fsz = 14; % Fontsize
12 lw = 1.5; % LineWidth
13 msz = 8; % MarkerSize
14 % The properties we’ve been using in the figures
15 set(0,’defaultLineLineWidth’,lw); % set the default line width to lw
16 set(0,’defaultLineMarkerSize’,msz); % set the default line marker size to msz
17 set(0,’defaultLineLineWidth’,lw); % set the default line width to lw
18 set(0,’defaultLineMarkerSize’,msz); % set the default line marker size to msz
19
20 % Set the default Size for display
21 defpos = get(0,’defaultFigurePosition’);
22 set(0,’defaultFigurePosition’, [100 -50 width*100, height*100]);
23
24 % Set the defaults for saving/printing to a file
25 set(0,’defaultFigureInvertHardcopy’,’on’); % This is the default anyway
26 set(0,’defaultFigurePaperUnits’,’inches’); % This is the default anyway
27 defsize = get(gcf, ’PaperSize’);
28 left = (defsize(1)- width)/2;
29 bottom = (defsize(2)- height)/2;
30 defsize = [left, bottom, width, height];
31 set(0, ’defaultFigurePaperPosition’, defsize);
32
33
34 figure(5)
35 t = (’Semilog plots of singular values’);
36 subplot(1,2,1)
37 plot(log(1:ntime),SS,’*’,’MarkerSize’ ,10)
38 xlabel(’log of Number of Singular Values’)
39 ylabel(’Singular Values’)
40 subplot(1,2,2)
41 semilogx(1:ntime,log(SS),’*’,’MarkerSize’ ,10)
42 xlabel(’Number of Singular Values’)
43 ylabel(’log of Singular Values’)
44 suptitle(t)
45 filename = [ ’figs/svd.png’ ];
46 print(figure(5),filename,’-dpng’)
47 end
References
[Myers, 2001] Myers, T. G. (2001). Extension to the Messinger model for aircraft icing. AIAA journal, 39(2), 211-218.

[2017 Fall] Lecture Notes

You might also like