-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem1.m
459 lines (399 loc) · 15.2 KB
/
Problem1.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
function varargout = Problem1(varargin)
% PROBLEM1 MATLAB code for Problem1.fig
% PROBLEM1, by itself, creates a new PROBLEM1 or raises the existing
% singleton*.
%
% H = PROBLEM1 returns the handle to a new PROBLEM1 or the handle to
% the existing singleton*.
%
% PROBLEM1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PROBLEM1.M with the given input arguments.
%
% PROBLEM1('Property','Value',...) creates a new PROBLEM1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Problem1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Problem1_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 Problem1
% Last Modified by GUIDE v2.5 07-Dec-2017 00:34:43
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Problem1_OpeningFcn, ...
'gui_OutputFcn', @Problem1_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 Problem1 is made visible.
function Problem1_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 Problem1 (see VARARGIN)
% Choose default command line output for Problem1
handles.output = hObject;
% UIWAIT makes Problem1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(hObject, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Problem 1 - Felippa book','numbertitle','off') ;
%status solver: 'INITIAL_MODE', 'IMPORTED_MODE', 'SOLVED_MODE',
%'CHANGED_MODE'
handles.mode = 'INITIAL_MODE';
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = Problem1_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;
% --- Executes on button press in importDataButton.
%start plotting the initial structure, importing all data
function importDataButton_Callback(hObject, eventdata, handles)
% hObject handle to importDataButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla;
finiteElement = importfile('finiteElement.txt');
data = finiteElement.data;
nodetag_temp = finiteElement.nodetag;
forcevalues_temp = finiteElement.forcevalues;
disp(data);
handles.data = data;
%% Preparing the cells
% It separates the data in cells and returns in case of some error
num_rows = size(data);
num_rows = num_rows(1);
% elenod is a cell with the nodes of all elements
elenod{1,num_rows } = [];
elemat{1,num_rows } = {};
%% Checking validity of data
for i = 1:num_rows
type = data{i,1};
if strcmp (type, 'bar')==0 && strcmp (type, 'tri')==0 && strcmp (type, 'quad')==0
sprintf('Erro:');
sprintf('Este tipo de elemento nao existe: %f', type );
return
end
end
%% Calculating the elenod
% elenod: the element end nodes arranged as a two-dimensional list:
% { { i1,j1 }, { i2,j2 }, . . . { ie,je } }, where e is the total number of elements.
for i = 1:num_rows
% nodes is a cell with the nodes of the element i
nodes = data{i,2};
nodes = strsplit(nodes,';');
for j = 1:length(nodes)
elenod{1,i}(j) = str2double( nodes(j) );
end
end
%% Calculating the nodcoor
% nodcoor: Nodal coordinates arranged as a two-dimensional list:
% { { x1,y1 },{ x2,y2 }, . . . { xn,yn } }, where n is the total number of nodes.
% the total number of nodes is equal to the maximum node number
total_node = 0;
for i = 1:num_rows
nodes = data{i,2};
nodes = strsplit(nodes,';');
for j = 1:length(nodes)
n = str2double( nodes(j) );
if n > total_node
total_node = n;
end
end
end
% list is the list with the number of the nodes not repeated
%list_node = [];
% nodcoor is a cell with all coordinates of the nodes
nodcoor{1,total_node } = [];
%i=1;
%while any(list_node)~= total_node && i<= num_rows
for i=1:num_rows
nodes = data{i,2};
nodes = strsplit(nodes,';');
coord_nodes = data{i,3};
coord_nodes = strsplit(coord_nodes,';');
for j=1:length(nodes)
n = str2double ( nodes(j) );
%if ismember(n, list_node) == 0
nodcoor{1,n}(1) = str2double( coord_nodes( 2*j-1 ) );
nodcoor{1,n}(2) = str2double( coord_nodes( 2*j ) );
%list_node(1,n) = n;
%end
end
end
%% Calculating the elemat (type, E, v, h and A)
% elemat = Element material properties arranged as a two-dimensional list:
% { { type1, A1, Em1,v1,h1 }, . . . { typee, Ae, Eme,ve,he } },
% where e is the total number of elements, A is the area.
for i = 1:num_rows
type = data{i,1};
E = data{i,4} ;
v = data{i,5} ;
h = data{i,6} ;
A = data{i,7} ;
elemat{1,i}(1) = {type};
elemat{1,i}(2) = {A};
elemat{1,i}(3) = {E};
elemat{1,i}(4) = {v};
elemat{1,i}(5) = {h};
end
%% nodetag and forcevalues
% Here is calculated the vectors nodetag and forcevalues
% total_node is the total number of nodes
% nodetag = [ nodetag_x1, nodetag_y1; nodetag_x2, nodetag_y2;...., nodetag_xn, nodetag_yn;]
% It is 1 when the displacement is specified and 0 otherwise.
nodetag = zeros(total_node, 2);
for i = 1:size(nodetag_temp,1)
node = str2double ( nodetag_temp{i,1} );
nodeBC = nodetag_temp{i,2};
nodeBC = str2double( strsplit(nodeBC,';') );
nodetag(node, 1) = nodeBC(1);
nodetag(node, 2) = nodeBC(2);
end
% forcevalues = [ force_x1, force_y1; force_x2, force_y2;...., force_xn, force_yn;]
% They are the force applied in each degree of freedom.
forcevalues = zeros(total_node, 2);
for i = 1:size(forcevalues_temp,1)
node = str2double ( forcevalues_temp{i,1} );
nodeForce = forcevalues_temp{i,2};
nodeForce = str2double( strsplit(nodeForce,';') );
forcevalues(node, 1) = nodeForce(1);
forcevalues(node, 2) = nodeForce(2);
end
%storing objects
handles.elemat = elemat;
handles.nodcoor = nodcoor;
handles.elenod = elenod;
handles.nodetag = nodetag;
handles.forcevalues = forcevalues;
handles.mode = 'IMPORTED_MODE';
title('Imported Structure') ;
alpha(1);
% Update handles structure
guidata(hObject, handles);
%plots the mesh
axes(handles.axesProblem1);
hold on;
axis( [-2 7 -1 7] );
axis equal
Plot2DMesh(handles.nodcoor, handles.elenod, handles.elemat, handles.nodetag,handles.forcevalues);
% --- Executes on button press in saveResultsButton.
function saveResultsButton_Callback(hObject, eventdata, handles)
% hObject handle to saveResultsButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(handles.mode,'SOLVED_MODE') == 0 && strcmp(handles.mode,'CHANGED_MODE') == 0
warndlg('Import and solve first!','!! Aviso !!')
return
else
%user chooses the file to save
FileName = uiputfile('*.xlsx',...
'Choose a folder to save the file');
K = handles.K;
count = 1;
data = {'Matrix Stiffness ='};
sheet = 1;
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+1;
data = K;
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+length(K)+2;
data = {'Node displacements'};
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+1;
data = handles.solution.noddisplacement';
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+4;
data = {'Nodal forces ='};
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+1;
data = handles.solution.nodfor';
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+4;
data = {'Stresses XX, YY and XY:'};
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+1;
data = cell2mat ( handles.solution.nodeStresses );
start_line = strcat('A',int2str(count));
xlRange = start_line;
xlswrite(FileName,data,sheet,start_line)
count = count+1;
end
% --- Executes on button press in solveButton.
% calculates the stiffness matrix
function solveButton_Callback(hObject, eventdata, handles)
% hObject handle to solveButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp( handles.mode, 'INITIAL_MODE') == 1
warndlg('Import first!','!! Aviso !!')
return
elseif strcmp( handles.mode, 'SOLVED_MODE') == 1
warndlg('FEM already solved','!! Aviso !!')
else
%% Global Stiffness matrix
% Choose here the value of pG (Gauss points)
pG = 2;
K = PlaneMasterStiffness(handles.nodcoor, handles.elenod, handles.elemat, pG,'Problem1');
handles.K = K;
%% Plane Stress Solution
% It calculates the stress in all points for all elements.
solution = PlaneStressSolution(handles.nodcoor, handles.elenod, handles.elemat, handles.nodetag, handles.forcevalues, K, 'Problem1');
handles.solution = solution;
handles.mode = 'SOLVED_MODE';
% Update handles structure
guidata(hObject, handles);
end
% --- Executes on button press in DeformationButton.
% shows the deformed structure
function DeformationButton_Callback(hObject, eventdata, handles)
% hObject handle to DeformationButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(handles.mode,'SOLVED_MODE') == 0 && strcmp(handles.mode,'CHANGED_MODE') == 0
warndlg('Import and solve first!','!! Aviso !!')
return
else
axes(handles.axesProblem1);
hold on;
axis( [-2 7 -1 7] );
axis equal
if strcmp(handles.mode,'CHANGED_MODE') == 1
alpha(1)
cla;
else %the initial structure becomes transparent
%alpha(.3)
end
PlotDeformedMesh(handles.nodcoor, handles.elenod,handles.elemat, 1e3, handles.solution.noddisplacement);
end
% --- Executes on button press in plotUXButton.
function plotUXButton_Callback(hObject, eventdata, handles)
% hObject handle to plotUXButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(handles.mode,'SOLVED_MODE') == 0
warndlg('Import and solve first!','!! Aviso !!')
return
else
axes(handles.axesProblem1);
hold on;
axis( [-2 7 -1 7] );
axis equal
cla;
PlotResult(handles.nodcoor, handles.elenod,handles.elemat, 1, handles.solution.noddisplacement(:,1), 'UX');
handles.mode='CHANGED_MODE';
end
% --- Executes on button press in plotUYButton.
function plotUYButton_Callback(hObject, eventdata, handles)
% hObject handle to plotUYButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(handles.mode,'SOLVED_MODE') == 0
warndlg('Import and solve first!','!! Aviso !!')
return
else
axes(handles.axesProblem1);
hold on;
axis( [-2 7 -1 7] );
axis equal
cla;
PlotResult(handles.nodcoor, handles.elenod,handles.elemat, 1, handles.solution.noddisplacement(:,2), 'UY');
handles.mode='CHANGED_MODE';
end
% --- Executes on button press in sigmaXXButton.
function sigmaXXButton_Callback(hObject, eventdata, handles)
% hObject handle to sigmaXXButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(handles.mode,'SOLVED_MODE') == 0
warndlg('Import and solve first!','!! Aviso !!')
return
else
axes(handles.axesProblem1);
hold on;
axis( [-2 7 -1 7] );
axis equal
cla;
nodeStresses = cell2mat( handles.solution.nodeStresses );
nodeStresses = nodeStresses(1,:);
PlotResult(handles.nodcoor, handles.elenod,handles.elemat, 1, nodeStresses', 'sigmaXX');
handles.mode='CHANGED_MODE';
end
% --- Executes on button press in sigmaYYButton.
function sigmaYYButton_Callback(hObject, eventdata, handles)
% hObject handle to sigmaYYButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(handles.mode,'SOLVED_MODE') == 0
warndlg('Import and solve first!','!! Aviso !!')
return
else
axes(handles.axesProblem1);
hold on;
axis( [-2 7 -1 7] );
axis equal
cla;
nodeStresses = cell2mat( handles.solution.nodeStresses );
nodeStresses = nodeStresses(2,:);
PlotResult(handles.nodcoor, handles.elenod,handles.elemat, 1, nodeStresses', 'sigmaYY');
handles.mode='CHANGED_MODE';
end
% --- Executes on button press in sigmaXYButton.
function sigmaXYButton_Callback(hObject, eventdata, handles)
% hObject handle to sigmaXYButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(handles.mode,'SOLVED_MODE') == 0
warndlg('Import and solve first!','!! Aviso !!')
return
else
axes(handles.axesProblem1);
hold on;
axis( [-2 7 -1 7] );
axis equal
cla;
nodeStresses = cell2mat( handles.solution.nodeStresses );
nodeStresses = nodeStresses(3,:);
PlotResult(handles.nodcoor, handles.elenod,handles.elemat, 1, nodeStresses', 'sigmaXY');
handles.mode='CHANGED_MODE';
end
% --- Executes on button press in MenuButton.
function MenuButton_Callback(hObject, eventdata, handles)
% hObject handle to MenuButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;
clear all, clc;
finiteElementProgram();