-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdiffusion_1d.m
More file actions
124 lines (108 loc) · 5.86 KB
/
diffusion_1d.m
File metadata and controls
124 lines (108 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
% ----------------------------------------------------------------------- %
% __ __ __ _ __ __ %
% |\/| _ |_ | _ |_ |__| / |_ | \ _ (_ |__) |_ %
% | | (_| |_ | (_| |_) | \__ | |__/ (_) | | \ | %
% %
% ----------------------------------------------------------------------- %
% %
% Author: Alberto Cuoci <alberto.cuoci@polimi.it> %
% CRECK Modeling Group <http://creckmodeling.chem.polimi.it> %
% Department of Chemistry, Materials and Chemical Engineering %
% Politecnico di Milano %
% P.zza Leonardo da Vinci 32, 20133 Milano %
% %
% ----------------------------------------------------------------------- %
% %
% This file is part of Matlab4CFDofRF framework. %
% %
% License %
% %
% Copyright(C) 2019 Alberto Cuoci %
% Matlab4CFDofRF is free software: you can redistribute it and/or %
% modify it under the terms of the GNU General Public License as %
% published by the Free Software Foundation, either version 3 of the %
% License, or (at your option) any later version. %
% %
% Matlab4CFDofRF is distributed in the hope that it will be useful, %
% but WITHOUT ANY WARRANTY; without even the implied warranty of %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the %
% GNU General Public License for more details. %
% %
% You should have received a copy of the GNU General Public License %
% along with Matlab4CRE. If not, see <http://www.gnu.org/licenses/>. %
% %
%-------------------------------------------------------------------------%
% %
% The meaning of this code is to give a simple introduction to the %
% finite volume (FV) technique for discretizing transport equations %
% in space. %
% %
% Code: 1D diffusion equation with finite volume method and explicit %
% Euler method. A constant, uniform source term q is included. %
% %
% rho*cp*dT/dt = k*d2T/dx2 + q %
% T(x=0)=T0, T(x=L)=TL, T(t=0)=Ti %
% %
% ----------------------------------------------------------------------- %
close all;
clear variables;
% ----------------------------------------------------------------------- %
% User data
% ----------------------------------------------------------------------- %
rho = 1000.; % density [kg/m3]
cp = 1000.; % specific heat [J/kg/K]
kappa = 0.5; % thermal conductivity [W/m/K]
L = 0.02; % length of computational domain [m]
tau = 1000; % total time of simulation [s]
T0 = 100; % temperature on the left side [C]
TL = 200; % temperature on the right side [C]
Ti = 100; % initial temperature [C]
q = 1e6; % heat generation [W/m3]
% Numerical parameters
nx = 100; % number of points [-]
sigma=0.5; % safety coefficient for time step [-]
% ----------------------------------------------------------------------- %
% Data processing
% ----------------------------------------------------------------------- %
h = L/(nx-1); % step size [m]
alpha = kappa/rho/cp; % thermal diffusivity [m2/s]
dt_diff=h^2/2/alpha; % time step (diffusion stability) [s]
dt = sigma*dt_diff; % time step [s]
nsteps=tau/dt; % number of time steps [-]
tc = L^2/alpha; % characteristic diffusion time [s]
xi=(h/2):h:(L-h/2); % grid internal points
fprintf('Max Time step [s]: %f\n', dt_diff);
fprintf('Time step [s]: %f\n', dt);
fprintf('Diffusivity [m2/s]: %f\n', alpha);
fprintf('Characteristic time [s]: %f\n', tc);
% ----------------------------------------------------------------------- %
% Solution
% ----------------------------------------------------------------------- %
T = zeros(nx+1,1) + Ti;
% Advancing in time
for j=1:nsteps
% Update boundary conditions (ghost points)
T(1) = 2*T0-T(2);
T(nx+1) = 2*TL-T(nx);
% Advance solution along the internal points
To = T;
for i=2:nx
T(i) = To(i) + alpha*dt/h^2*(To(i+1)-2*To(i)+To(i-1)) + ...
q/(rho*cp)*dt;
end
% On-the-fly post processing
if (mod(j,25)==1)
plot(xi,T(2:nx));
drawnow;
end
end
% ----------------------------------------------------------------------- %
% Data postprocessing
% ----------------------------------------------------------------------- %
x = 0:h:L;
TT = (T(1:nx)+T(2:nx+1))/2;
Ta = ((TL-T0)/L + q/2/kappa*(L-x)).*x + T0; % analytical solution
plot(x,TT,'-', x,Ta,'o'); xlabel('x[m]'); ylabel('temperature [C]');
% Error estimation
error = norm(Ta-TT')/nx;
fprintf('Error: %e\n', error);