-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.m
157 lines (132 loc) · 5.99 KB
/
Test.m
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
function heat_transfer_analysis_2D_half_shape()
% Main function to perform 2D heat transfer analysis using FEM for half of the shape
% Reading mesh file data for the full shape
fileName = 'C:\Users\argaex\Desktop\Uni\final_2.inp';
[nodes, elements] = readMeshFile(fileName);
% Identify nodes for half of the shape
half_nodes = nodes(nodes(:, 2) >= 0, :);
half_elements = elements(elements(:, 3) >= 0, :);
% Defining problem parameters
conductivity_x = 2; % Thermal conductivity in x direction (W/cm°C)
conductivity_y = 2; % Thermal conductivity in y direction (W/cm°C)
convectionCoefficient = 1.5; % Convective heat transfer coefficient (W/cm²°C)
ambientTemperature = 20; % Ambient temperature (°C)
internalTemperature = 140; % Internal wall temperature (°C)
heatSource = 0; % Internal heat source (W/m³)
% Number of nodes and elements for half of the shape
numNodes = size(half_nodes, 1);
numElements = size(half_elements, 1);
% Creating global stiffness matrix and thermal force vector
K = sparse(numNodes, numNodes);
F = zeros(numNodes, 1);
% Calculating stiffness matrix and thermal force vector for each element
for e = 1:numElements
nodeNumbers = half_elements(e, 2:end);
x = half_nodes(nodeNumbers, 2);
y = half_nodes(nodeNumbers, 3);
[Ke, Fe] = elementStiffnessAndThermalForce(x, y, conductivity_x, conductivity_y, heatSource);
K(nodeNumbers, nodeNumbers) = K(nodeNumbers, nodeNumbers) + Ke;
F(nodeNumbers) = F(nodeNumbers) + Fe;
end
% Applying boundary conditions for half of the shape
[K, F] = applyBoundaryConditions(K, F, half_nodes, half_elements, convectionCoefficient, ambientTemperature, internalTemperature);
% Solving the system of equations
T = K \ F;
% Displaying results for half of the shape
displayResults(half_nodes, half_elements, T);
end
function [Ke, Fe] = elementStiffnessAndThermalForce(x, y, conductivity_x, conductivity_y, heatSource)
% Calculating the B matrix for a four-node element
xi = [-1 1 1 -1];
eta = [-1 -1 1 1];
Ke = zeros(4,4);
Fe = zeros(4,1);
for i = 1:4
[dN_dxi, dN_deta] = shapeFunction(xi(i), eta(i));
J = [dN_dxi; dN_deta] * [x, y];
detJ = det(J);
invJ = inv(J);
B = invJ * [dN_dxi; dN_deta];
D = [conductivity_x 0; 0 conductivity_y];
Ke = Ke + B' * D * B * detJ;
Fe = Fe + [1; 1; 1; 1] * heatSource * detJ / 4;
end
end
function [dN_dxi, dN_deta] = shapeFunction(xi, eta)
dN_dxi = 0.25 * [-1+eta, 1-eta, 1+eta, -1-eta];
dN_deta = 0.25 * [-1+xi, -1-xi, 1+xi, 1-xi];
end
function [K, F] = applyBoundaryConditions(K, F, nodes, elements, convectionCoefficient, ambientTemperature, internalTemperature)
% Identifying boundary nodes
internalRadius = 0.01; % Internal radius (m)
externalRadius = 0.04; % External radius (m)
internal_nodes = find(sqrt(nodes(:,2).^2 + nodes(:,3).^2) < (internalRadius + 0.001));
external_nodes = find(sqrt(nodes(:,2).^2 + nodes(:,3).^2) > (externalRadius - 0.001));
% Applying fixed temperature boundary condition at internal boundary
K(internal_nodes, :) = 0;
K(internal_nodes, internal_nodes) = eye(length(internal_nodes));
F(internal_nodes) = internalTemperature;
% Applying convective heat transfer boundary condition at external boundary
for e = 1:size(elements, 1)
nodeNumbers = elements(e, 2:end);
if any(ismember(nodeNumbers, external_nodes))
x = nodes(nodeNumbers, 2);
y = nodes(nodeNumbers, 3);
[Ke_convection, Fe_convection] = convectionBoundary(x, y, convectionCoefficient, ambientTemperature);
K(nodeNumbers, nodeNumbers) = K(nodeNumbers, nodeNumbers) + Ke_convection;
F(nodeNumbers) = F(nodeNumbers) + Fe_convection;
end
end
end
function [Ke_convection, Fe_convection] = convectionBoundary(x, y, convectionCoefficient, ambientTemperature)
% Calculating edge length (assuming only one edge of the element is on the boundary)
L = sqrt((x(2)-x(1))^2 + (y(2)-y(1))^2);
% Convection heat transfer stiffness matrix
Ke_convection = (convectionCoefficient * L / 6) * [2 1 0 0; 1 2 0 0; 0 0 0 0; 0 0 0 0];
% Convection heat transfer force vector
Fe_convection = (convectionCoefficient * ambientTemperature * L / 2) * [1; 1; 0; 0];
end
function displayResults(nodes, elements, T)
% Visualize the results of the FEM simulation for half of the shape
figure;
patch('Faces', elements(:,2:end), 'Vertices', nodes(:,2:3), 'FaceVertexCData', T, 'FaceColor', 'interp', 'EdgeColor', 'none');
colorbar;
title('Temperature Distribution for Half of the Shape');
xlabel('X [m]');
ylabel('Y [m]');
axis equal;
colormap(parula); % Change the colormap to 'parula'
end
function [nodes, elements] = readMeshFile(fileName)
% Reads the mesh file and extracts node and element data
fid = fopen(fileName, 'r');
nodes = [];
elements = [];
line = fgetl(fid);
while ischar(line)
if contains(line, '*Node')
line = fgetl(fid);
while ~startsWith(line, '*')
nodeData = sscanf(line, '%f,%f,%f');
nodes = [nodes; nodeData'];
line = fgetl(fid);
if ~ischar(line)
break;
end
end
elseif contains(line, '*Element')
line = fgetl(fid);
while ~startsWith(line, '*')
elementData = sscanf(line, '%f,%f,%f,%f,%f');
elements = [elements; elementData'];
line = fgetl(fid);
if ~ischar(line)
break;
end
end
else
line = fgetl(fid);
end
end
fclose(fid);
end