-
Notifications
You must be signed in to change notification settings - Fork 0
/
aeronastran.m
1397 lines (1153 loc) · 41 KB
/
aeronastran.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% AERONastran
% Helper code to perform aeroelasticity simulations in NASTRAN.
%% === Wing class ===
classdef aeronastran < handle
properties
version = '2024-05-16';
geom = wing_geom();
format = file_format();
model = build_model();
oper = oper_cond();
flutter = flutter_cond();
end
methods
function W = aeronastran()
% Constructor
end
function set_flightcond(W)
aero_param(W)
end
function solve(W)
if (W.oper.analysis == 1)
% Solve flutter
solve_flutter(W);
elseif (W.oper.analysis == 2)
% Solve divergence
solve_divergence(W);
else
end
runNastran(W.format.savefile);
if (W.oper.analysis == 1)
output_file_f06 = W.format.savefile;
output_file_f06(end-3:end) = '.f06';
flutter_extract(output_file_f06)
end
end
% Build mesh
function gen_fem(W)
build_mesh(W)
end
% Build lattice
function lattice(W)
build_dlm(W)
end
% Ploting
function panel_plot(W)
panelplot(W);
end
function mesh_plot(W)
meshplot(W);
end
end
end
%% ===== STRUCTURE ======
function S = oper_cond()
% Operating conditions
S.analysis = 2;
S.h = 0;
S.dens = [];
S.vel = [];
S.M = [];
S.flightparam = struct('M', 0.0, 'Q', 1500, 'AOA', 0.0174533);
end
function flut = flutter_cond()
flut.freq = size(2,1);
flut.nroots = 0;
end
function F = file_format()
% File and format
F.format_line = '\n$...1...|...2...|...3...|...4...|...5...|...6...|...7...|...8...|...9...|..10... \n';
F.heading_line = '';
F.savefile = '';
end
function M = build_model()
% Model
M.chorddiv = 7;
M.spandiv = 45;
M.panspandiv = 0;
M.panchorddiv = 0;
M.panspan = 21;
M.panchord = 7;
M.xpanelcoords = 0;
M.ypanelcoords = 0;
M.zpanelcoords = 0;
M.xnodecoords = 0;
M.ynodecoords = 0;
M.znodecoords = 0;
M.nodeplotflag = true;
M.shell = [];
M.nodes = [];
M.panels = [];
end
function S = wing_geom()
% Wing geometry
S.angle = 0;
S.chord = 0.5;
S.span = 2.5;
S.angleflag = 0;
S.material = struct('E', 7.E10, 'dens', 2750, 'poisson', 0.3);
end
%% === Main ===
function build_mesh(W)
% Generates mesh for FEM
% Input:
% - W : wing
% Output:
% - W.model.nodes
% - W.model.shell
% FEM model generation
FEM(W);
nodes = get_nodes(W);
shell_elements = gen_shell(nodes);
W.model.nodes = nodes;
W.model.shell = shell_elements;
end
function build_dlm(W)
% Generates doubblet lattice method for the wing
% Input:
% - W : wing class
% Sets:
% - W.model.panels : panels for dlm
% - W.model.panelcoords : panel coordinates
W.model.panspandiv= linspace(0,1,W.model.panspan);
W.model.panchorddiv = 0.5 + 0.5*cos(linspace(pi,0,W.model.panchord));
[XP, YP, ZP] = dlmpanels(W);
panels = get_panels(XP,YP,ZP);
W.model.panels = panels;
W.model.xpanelcoords = XP;
W.model.ypanelcoords = YP;
W.model.zpanelcoords = ZP;
end
function solve_flutter(W)
% Generates and solves bdf file for sol 145 (flutter).
% Input:
% - W : wing
disp('Flutter')
create_bdf_file(W);
write_flutter_analysis_file(W);
nodedump(W);
shelldump(W);
set_material(W);
method(W);
write_paneldivision(W);
wingstructcoupling(W);
set_eigen(W);
set_flightcond(W);
set_flutter(W);
set_mkaero(W);
set_flfacts(W);
end_file(W.format.savefile);
end
function solve_divergence(W)
% Generates and solves bdf file for sol 144 (divergence).
% Input:
% - W : wing
disp('Divergence')
create_bdf_file(W);
write_divergence_analysis_file(W);
nodedump(W);
shelldump(W);
set_material(W);
method(W);
write_paneldivision(W);
wingstructcoupling(W);
set_aeros(W);
set_trim(W);
end_file(W.format.savefile);
end
function fligthcond(W)
h = W.oper.h;
end
%% === Geometry definition ===
function write_paneldivision(W)
% Sets the panel division for the DLM and writes it into the input file for
% Nastran to execute the analyisis.
%
% Input:
% - W : aeronastran wing
% Output:
% -
file = W.format.savefile;
format_line = W.format.format_line;
chord_division = W.model.panchorddiv;
span_division = W.model.panspandiv;
% Open file in append mode
fileopen_entry = fopen(file, 'a');
% Check if open == ERROR
if fileopen_entry == -1
error('Cannot open entry file');
end
% Write format line
fprintf(fileopen_entry, format_line);
% Write span division
fprintf(fileopen_entry, 'AEFACT 101 ');
write_array(fileopen_entry, span_division);
fprintf(fileopen_entry, format_line);
% Write chord division
fprintf(fileopen_entry, 'AEFACT 151 ');
write_array(fileopen_entry, chord_division);
% Close file
fclose(fileopen_entry);
end
%% === Doublet lattice method ===
function [X,Y,Z] = dlmpanels(W)
% Double lattice panel generation
% Input
% - W : wing
% Output
% - [X,Y,Z] : coordinates of the panel nodes
span = W.geom.span;
chord = W.geom.chord;
angle = W.geom.angle;
span_division = W.model.panspandiv;
chord_division = W.model.panchorddiv;
% Calculate the number of nodes for each section
nnodes_section = ceil(length(span_division) / 2);
% Generate nodes for the first section
span_nodes1 = span * span_division(1:nnodes_section);
chord_nodes1 = chord * chord_division - chord/2;
% Calculate the slope for the first section
theta1 = deg2rad(angle);
m1 = tan(theta1);
% Generate nodes for the second section
% Start at the end of the first section
span_nodes2 = span * span_division(nnodes_section:end); % Include one more node for smooth connection
% Initialize arrays to store coordinates of all nodes
X1 = zeros(nnodes_section, length(chord_division));
Y1 = zeros(nnodes_section, length(chord_division));
Z1 = zeros(nnodes_section, length(chord_division));
X2 = zeros(nnodes_section, length(chord_division)); % Correct size initialization
Y2 = zeros(nnodes_section, length(chord_division)); % Correct size initialization
Z2 = zeros(nnodes_section, length(chord_division)); % Correct size initialization
% Generate coordinates for the first section
for i = 1:nnodes_section
for j = 1:length(chord_division)
X1(i, j) = chord_nodes1(j) + m1 * span_nodes1(i);
Y1(i, j) = span_nodes1(i);
Z1(i, j) = 0;
end
end
span_nodes2 = flip(span_nodes2);
% Generate coordinates for the second section
for i = 1:length(span_nodes2)
for j = 1:length(chord_division)
X2(i, j) = chord_nodes1(j) + m1 * span_nodes1(i); % Use chord_nodes1
Y2(i, j) = span_nodes2(i);
Z2(i, j) = 0;
end
end
% flip the matrices to fit the wing
X2 = flip(X2, 1);
Y2 = flip(Y2, 1);
% concatenate the matrices and create the full mesh
X = vertcat(X1, X2);
Y = vertcat(Y1, Y2);
Z = vertcat(Z1, Z2);
end
function panels = get_panels(X, Y, Z)
% Generates the DLM panels from the point mesh that gives the location
% of each node. The panel generation is only for plot purpposes, as in
% the .bdf only the division is needed.
% Input:
% - [X,Y,Z] : mesh coordinates
% Output:
% - panels : panel struct
elim_row = size(X)/2;
X(elim_row(1),:) = [];
Y(elim_row(1),:) = [];
Z(elim_row(1),:) = [];
[rows, cols] = size(X);
panels = struct('id', [], 'x', [], 'y', [], 'z', []);
panel_id = 40001; % Starting ID for panels
for i = 1:rows-1
for j = 1:cols-1
panel = struct();
panel.id = panel_id;
panel.x = [X(i, j), X(i+1, j), X(i+1, j+1), X(i, j+1)];
panel.y = [Y(i, j), Y(i+1, j), Y(i+1, j+1), Y(i, j+1)];
panel.z = [Z(i, j), Z(i+1, j), Z(i+1, j+1), Z(i, j+1)];
panels(end+1) = panel;
panel_id = panel_id + 1;
end
end
panels(1) = [];
end
function method(W)
% filename, format_line,angleflag, nodes, spandiv, panels
% Sets the CAERO method for the .bdf
% Input
% - filename : bdf file
% - format_line : division line
% - angleflag : checks if wing has some angle
% - nodes : node array
% - spandiv : span division
filename = W.format.savefile;
format_line = W.format.format_line;
angle = W.geom.angle;
nodes = W.model.nodes;
spandiv = W.model.spandiv;
panels = W.model.panels;
% Open the file for appending
fid = fopen(filename, 'a');
if fid == -1
error('Could not open the file');
end
index = round(spandiv/2);
if angle == 0
str = 'CAERO1 40001 10000 101 151 1 +';
str_nodes = '-0.25 0.0 0.0 0.5 -0.25 2.5 0.0 0.5';
% Append the string to the file
fprintf(fid, '$');
fprintf(fid, format_line);
fprintf(fid, '%s\n', str);
fprintf(fid, '+ ');
fprintf(fid, '%s', str_nodes);
fprintf(fid, format_line);
fprintf(fid, 'PAERO1 10000');
% Close the file
fclose(fid);
else
str_w1 = 'CAERO1 40001 10000 101 151 1 +';
midxcoord = 2*nodes(1).x - nodes(index).x;
midycoord = nodes(index).y;
chord = 2*nodes(1).x;
fprintf(fid, '$');
fprintf(fid, format_line);
fprintf(fid, '%s\n', str_w1);
fprintf(fid, '+ ');
fprintf(fid, '-%.2f 0.0 0.0 %.1f -%.4f %.2f 0.0 %.1f',nodes(1).x,chord,midxcoord,midycoord,chord);
fprintf(fid, format_line);
fprintf(fid, 'CAERO1 %i 10000 101 151 1 +\n',panels(end).id+1);
fprintf(fid, '+ ');
fprintf(fid, '-%.4f %.2f 0.0 %.1f %.2f %.4f 0.0 %.1f',midxcoord,midycoord, chord, nodes(end).x, nodes(end).y, chord);
fprintf(fid, format_line);
fprintf(fid, 'PAERO1 10000');
end
end
function wingstructcoupling(W)
% Open the file for appending wingstructcoupling.
% Input:
% - W : aeronastran wing
file = W.format.savefile;
nodes = W.model.nodes;
format_line = W.format.format_line;
panels = W.model.panels;
angle = W.geom.angle;
fid = fopen(file, 'a');
if fid == -1
error('Could not open the file');
end
wing1 = [];
wing2 = [];
for j = 1:numel(nodes)
if nodes(j).wing == 1
wing1(end+1) = nodes(j).id;
elseif nodes(j).wing == 2
wing2(end+1) = nodes(j).id;
end
end
fprintf(fid, '$ EID CAERO BOX1 BOX2 SETG');
fprintf(fid, format_line);
if angle == 0
% Append the format line
fprintf(fid, 'SPLINE1 50001 40001 40001 %i 901\n',panels(end).id);
fprintf(fid, 'SET1 901 ');
write_set(fid, [nodes(:).id])
else
fprintf(fid, 'SPLINE1 50001 40001 40001 %i 901\n',panels(end).id);
fprintf(fid, 'SPLINE1 50002 %i %i %i 902',panels(end).id+1,panels(end).id+1, mod(panels(end).id,1000)*2+40000);
fprintf(fid, '\nSET1 901 ');
write_set(fid, wing1)
fprintf(fid, '\nSET1 902 ');
write_set(fid, wing2)
end
fclose(fid);
end
%% === Solving divergence ===
function set_aeros(W)
% Used in sol 144 to give the reference coordinates system and values. The
% reference values are taken from the chord and span, and the reference
% coordinate system is the default.
file = W.format.savefile;
chord = W.geom.chord;
span = W.geom.span;
format_line = W.format.format_line;
fid = fopen(file, 'a');
if fid == -1
error('Could not open the file');
end
% Write the AEROS line
aerosinfo = '\n$AEROS ACSID RCSID REFC REFB REFS SYMXZ SYMXY';
fprintf(fid,aerosinfo);
fprintf(fid,format_line);
fprintf(fid, 'AEROS %.1f %.1f %.2f +1\n', span, chord, span*chord);
% Close the file
fclose(fid);
end
function set_trim(W)
% Sets TRIM entry for static analysis.
% Input:
% - W : aeronastran wing
file = W.format.savefile;
aeroparam = W.oper.flightparam;
format_line = W.format.format_line;
format_str = '$TRIM ID MACH Q LABEL1 UX1 LABEL2 UX2 AEQR +\n';
format2_str = '$+ LABEL3 UX3 ...';
fid = fopen(file, 'a');
if fid == -1
error('Could not open the file');
end
fprintf(fid,format_str);
fprintf(fid, format2_str);
fprintf(fid, format_line);
fprintf(fid, 'TRIM 6 %.1f %.1e ANGLEA %.6f %.1f',aeroparam.M, aeroparam.Q, aeroparam.AOA,1);
fprintf(fid, '\nAESTAT 61 ANGLEA');
end
%% === Solving flutter ===
function set_mkaero(W)
% Sets MKAERO1 entry for flutter analysis.
% Input:
% - outputfile : .bdf file
% - format_line
outputfile = W.format.savefile;
format_line = W.format.format_line;
M = W.oper.M(1);
fid = fopen(outputfile, 'a');
if fid == -1
error('Could not open the file');
end
fprintf(fid, format_line);
fprintf(fid, 'MKAERO1 %.4f\n', M);
fprintf(fid, '+ 1.0000 0.9000 0.8000 0.7000 0.6000 0.5000 0.4000 0.3000\n');
fprintf(fid, 'MKAERO1 %.4f\n', M);
fprintf(fid, '+ 0.2000 0.1000 0.0100');
fclose(fid);
end
function set_eigen(W)
% Sets eigenvalue extraction method and prints it in the .bdf file.
% Inputs:
% - outputfile : .bdf file
% - format_line
% - V1 : lower frequency limit
% - V2 : higher frequency limit
% - nroots : number of modes
outputfile = W.format.savefile;
format_line = W.format.format_line;
V1 = W.flutter.freq(1);
V2 = W.flutter.freq(2);
nroots = W.flutter.nroots;
fid = fopen(outputfile, 'a');
if fid == -1
error('Could not open the file');
end
fprintf(fid, '$\n$ Modal Analysis\n$ ');
fprintf(fid, format_line);
fprintf(fid, '$ SID V1 V2 ND MSGLVL MAXSET SHFSCL NORM\n');
fprintf(fid, 'EIGRL 200 %.2f %.2f %i MAX', V1,V2, nroots);
fclose(fid);
end
function set_flightcond(W)
% Sets flight conditions
% Input:
% - outputfile : .bdf file
% - format_line
% - M : Mach number
% - Q : dynamic pressure
outputfile = W.format.savefile;
format_line = W.format.format_line;
M = W.oper.M(1);
Q = 1500;
fid = fopen(outputfile, 'a');
if fid == -1
error('Could not open the file');
end
fprintf(fid, format_line);
fprintf(fid, 'PARAM MACH %.4f\n', M);
fprintf(fid, 'PARAM Q %.3f', Q);
fprintf(fid, format_line);
fprintf(fid, '$ VELOCITY REFC RHOREF SIMXZ\n');
fprintf(fid, 'AERO 9999.00 0.5 1.225 +1\n');
fclose(fid);
end
function set_flutter(W)
% Sets flutter method and parameters
% Input:
% - outputfile : .bdf file
% - format_line
outputfile = W.format.savefile;
format_line = W.format.format_line;
fid = fopen(outputfile, 'a');
if fid == -1
error('Could not open the file');
end
fprintf(fid, '$\n$ Flutter Analysis\n$\n');
fprintf(fid, 'PARAM VREF 0.514444');
fprintf(fid, format_line);
fprintf(fid, '$ METHOD DENS MACH VEL IMETH\n');
fprintf(fid, 'FLUTTER 300 PKNL 1 2 3 L');
fclose(fid);
end
function set_flfacts(W)
% Sets FLFACTS entry
% Input:
% -
outputfile = W.format.savefile;
format_line = W.format.format_line;
dens = W.oper.dens;
Mach = W.oper.M;
Vel = W.oper.vel;
fid = fopen(outputfile, 'a');
if fid == -1
error('Could not open the file');
end
fprintf(fid,'\n$ SID F1 F2 F3 F4 F5 F6 F7\n');
fprintf(fid, format_line);
fprintf(fid,'FLFACT 1 ');
write_array(fid, dens);
fprintf(fid,'FLFACT 2 ');
write_array(fid, Mach);
fprintf(fid,'FLFACT 3 ');
write_flutter_array(fid, Vel);
fclose(fid);
end
%% === Set FEM properties ===
function set_material(W)
% Sets material entry in the .bdf file
% Input:
% - W : aeronastran wing
format_line = W.format.format_line;
material = W.geom.material;
file = W.format.savefile;
fid = fopen(file, 'a');
if fid == -1
error('Could not open the file');
end
fprintf(fid, format_line);
fprintf(fid, '$ MID E G NU RHO A TREF GE\n');
fprintf(fid, 'MAT1 1 %.1e %.1f %.1f 0.03\n', material.E, material.poisson, material.dens);
fclose(fid);
end
%% === FEM Grid generation ===
function FEM(W)
% Generates FEM grid and the X,Y,Z coordinates of the nodes
% Input:
% - W : aeronastran wing
% Output:
% - W.model.nodecoords : node coordinates (X,Y,Z) [3 x n]
% get wing specs
span = W.geom.span; chord = W.geom.chord; angle = W.geom.angle;
% generate nodes for the first section
span_nodes1 = linspace(0, span/2, round(W.model.spandiv/2));
chord_nodes = linspace(chord/2, -chord/2, W.model.chorddiv);
% calculate the slope for the first section
theta1 = deg2rad(angle);
m1 = tan(theta1);
% generate nodes for the second section
% start at the end of the first section
span_offset = span_nodes1(end); % Offset for the start of the second section
span_nodes2 = linspace(0, span/2, round(W.model.spandiv/2)) + span_offset; % Include one more node for smooth connection
span_nodes2(1) = [];
% Initialize arrays to store coordinates of all nodes
X1 = zeros(round(W.model.spandiv/2), W.model.chorddiv);
Y1 = zeros(round(W.model.spandiv/2), W.model.chorddiv);
Z1 = zeros(round(W.model.spandiv/2), W.model.chorddiv);
X2 = zeros(round(W.model.spandiv/2)-1, W.model.chorddiv);
Y2 = zeros(round(W.model.spandiv/2)-1, W.model.chorddiv);
Z2 = zeros(round(W.model.spandiv/2)-1, W.model.chorddiv);
% Generate coordinates for the first section
for i = 1:round(W.model.spandiv/2)
for j = 1:W.model.chorddiv
X1(i, j) = chord_nodes(j) + m1 * span_nodes1(i);
Y1(i, j) = span_nodes1(i);
Z1(i, j) = 0;
end
end
span_nodes2 = flip(span_nodes2);
% Generate coordinates for the second section
for i = 1:round(W.model.spandiv/2)-1
for j = 1:W.model.chorddiv
X2(i, j) = chord_nodes(j) + m1 * span_nodes1(i);
Y2(i, j) = span_nodes2(i);
Z2(i, j) = 0;
end
end
X2 = flip(X2,1);
Y2 = flip(Y2,1);
% X,Y,Z => [spandiv, chordiv]
W.model.xnodecoords = vertcat(X1, X2);
W.model.ynodecoords = vertcat(Y1, Y2);
W.model.znodecoords = vertcat(Z1, Z2);
end
function shell_elements = gen_shell(nodes)
% Generates the shell elements that conform the wing
% Shell elements are defined in the .bdf file with:
% - index
% - pshell pointer
% - id's of the corner nodes
shell_elements = struct('id', [], 'pshell', [], 'node1', [], ...
'node2',[], 'node3',[], 'node4',[]);
idstart = 20000;
count = 0;
% Iterate over nodes
for i = 1:length(nodes)
% Check if y-coordinate is zero
if nodes(i).y == 0
% Increment count
count = count + 1;
end
end
nchord = count;
nspan = length(nodes)/nchord;
for j = 1:nchord-1
for i = 1:nspan-1
nodeid = 1000;
idstart = idstart +1;
shell_elements(end+1) = struct('id', idstart, 'pshell', 1, ...
'node1', nodeid*j + i, ...
'node2', nodeid*j+i+1, ...
'node3', nodeid*(j+1) + i+1, ...
'node4', nodeid*(j+1) +i);
end
end
shell_elements(1) = [];
end
function nodes = get_nodes(W)
% Get node struct from node coordinates
% Input:
% - W : aeronastran wing
% Output
% - W.model.nodes : FEM nodes
% Initialize array to store node coordinates
node_coordinates = struct('id', [], 'x', [], 'y', [], 'z', [], 'bc', [],'wing',[]);
X = W.model.xnodecoords;
Y = W.model.ynodecoords;
Z = W.model.znodecoords;
% Get the number of nodes and dimensions
[nnodes_row, nnodes_col] = size(X);
% Loop through the coordinates and assign boundary conditions
for j = 1:nnodes_col
start_id = 1000*j;
for i = 1:nnodes_row
x = X(i, j);
y = Y(i, j);
z = Z(i, j);
id = start_id + i; % Assign a unique ID for each node
if mod(id, 1000) == 1
% Update the 'bc' attribute for these nodes
bc = 123456; % Assuming 'bc' is set to 1 for these nodes
else
bc = [];
end
if mod(id, 100) <= round(nnodes_row/2)
wing = 1;
else
wing = 2;
end
node_coordinates(end+1) = struct('id', id, 'x', x, 'y', y, 'z', z, 'bc', bc,'wing',wing);
end
end
% Remove the first empty entry
node_coordinates(1) = [];
nodes = node_coordinates;
end
%% === FEM stiffness and mass matrices ===
% To obtain the FEM stiffness and mass matrices another .bdf file is going
% to be created with the punch (.pch) file output.
function matrices(matrixfile)
fid = fopen(matrixfile, 'w');
if fid == -1
error('Could not open the file');
end
fprintf(fid, '$ Direct Text Input for Nastran System Cell Section\n');
fprintf(fid, '$ Direct Text Input for File Management Section\n');
fprintf(fid, '$ Direct Text Input for Executive Control\n');
fprintf(fid, '$ Normal Modes Analysis, Database\n');
fprintf(fid, 'SOL 103\nCEND\n');
fprintf(fid, '$ Direct Text Input for Global Case Control Data\n');
fprintf(fid, 'TITLE = MSC.Nastran job created on %s\n', datestr(now, 'dd-mmm-yy at HH:MM:SS'));
fprintf(fid, 'ECHO = NONE\n');
fprintf(fid, 'RESVEC = NO\n');
fprintf(fid, '$\n'); % End of section
fprintf(fid, '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n');
fprintf(fid, 'EXTSEOUT(STIF,DAMP,MASS,EXTID=1,DMIGPCH)\n');
fprintf(fid, '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n');
fprintf(fid, 'SUBCASE 1\n');
fprintf(fid, '$ Subcase name : Default\n');
fprintf(fid, ' SUBTITLE=Default\n');
fprintf(fid, ' METHOD = 1\n');
fprintf(fid, ' VECTOR(SORT1,REAL)=ALL\n');
fprintf(fid, ' SPCFORCES(SORT1,REAL)=ALL\n');
fprintf(fid, 'BEGIN BULK\n');
fprintf(fid, '$ Direct Text Input for Bulk Data\n');
fprintf(fid, 'PARAM POST 0\n');
fprintf(fid, 'PARAM PRTMAXIM YES\n');
fprintf(fid, 'EIGRL 1 3 0\n');
fclose(fid);
end
%% === File comprehension functions ===
function flutter_extract(filename)
% Extracts the flutter results from the .f06 file.
fid = fopen(filename, 'r');
if fid == -1
error('Could not open the file');
end
nPointTables = 0; % Initialize nPointTables
nModes = 0; % Initialize nModes
nLines = 0; % Initialize nLines
while true
tline = fgetl(fid);
if ~ischar(tline), break, end;
if length(tline) >= 12 && strcmp(tline(8:12), 'POINT')
nPointTables = nPointTables + 1;
nModes = str2double(tline(18:19));
end
nLines = nLines + 1;
end
frewind(fid);
iModeBreak = 0;
modeCounter = 1; % Initialize mode counter
while true
tline = fgetl(fid);
if ~ischar(tline), break, end;
values = strsplit(tline);
if length(tline) >= 12 && strcmp(tline(8:12), 'POINT')
nPointTables = nPointTables + 1;
iMode = str2double(tline(18:19));
for j = 1:3
fgetl(fid);
end
tline(1:1) = ' ';
if iMode ~= iModeBreak
iLine = 0;
iModeBreak = iMode;
end
while tline(1:1) == ' '
tline = fgetl(fid);
if strcmp(tline(1:1), '1')
break;
end
if strcmp(tline(1:4), ' ***') || strcmp(tline(1:3), '***')
break;
end
iLine = iLine + 1;
INVk(iLine, iMode) = str2double(tline(17:28));
DENS(iLine, iMode) = str2double(tline(31:41));
VELO(iLine, iMode) = str2double(tline(59:69));
EAS(iLine, iMode) = VELO(iLine, iMode) * sqrt(DENS(iLine, iMode) / 1.225);
DAMP(iLine, iMode) = str2double(tline(72:83));
FREQ(iLine, iMode) = str2double(tline(87:97));
REVA(iLine, iMode) = str2double(tline(100:111));
IMVA(iLine, iMode) = str2double(tline(114:125));
end
end
end
fclose(fid);
figure('Position', [50 50 650 600]);
subplot(2, 1, 1);
hold on;
for iMode = 1:nModes
plot(EAS(:, iMode), DAMP(:, iMode), '.-');
end
grid;
ylim([-0.10 0.5]);
ylabel('Damping [g]');
xlabel('Flight Speed [KTAS]');
legend(arrayfun(@(x) ['Mode ' num2str(x)], 1:nModes, 'UniformOutput', false));
subplot(2, 1, 2);
hold on;
for iMode = 1:nModes
plot(EAS(:, iMode), FREQ(:, iMode), '.-');
end
ylim([0 50]);
ylabel('Frequency [Hz]');
legend(arrayfun(@(x) ['Mode ' num2str(x)], 1:nModes, 'UniformOutput', false));
grid;
end
function nodedump(W)
% nodes, format_line, output_file
% Dumps nodes into the .bdf file in order, adding the format line to
% give the columns.
% Order nodes by ID
nodes = W.model.nodes;
format_line = W.format.format_line;
output_file = W.format.savefile;
[~, idx] = sort([nodes.id]);
sorted_nodes = nodes(idx);
% Open the output file
foutput = fopen(output_file, 'a');
if foutput == -1
error('Error opening the output file');
end
% Write the format line to the output file
fprintf(foutput, format_line);
% Write nodes to the output file
for i = 1:numel(sorted_nodes)
node = sorted_nodes(i);
fprintf(foutput, 'GRID %d %.4f %.4f %.4f %i\n', node.id, node.x, node.y, node.z, node.bc);
end
% Close the output file
fclose(foutput);
end
function shelldump(W)
% output_file, shell_elements, format_line
% Open the output file for writing
output_file = W.format.savefile;
shell_elements = W.model.shell;
format_line = W.format.format_line;
fid = fopen(output_file, 'a');
if fid == -1
error('Unable to open the output file.');
end
fprintf(fid,format_line);
fprintf(fid,'$ PID MID1 T MID2 MID3 TS/T NSM\n');
fprintf(fid,'PSHELL 1 1 .008 1 1\n');
fprintf(fid,format_line);
% Write shell elements to the file
for i = 1:length(shell_elements)
fprintf(fid, 'CQUAD4 %d 1 %d %d %d %d\n', ...
shell_elements(i).id, shell_elements(i).node1, shell_elements(i).node2, ...
shell_elements(i).node3, shell_elements(i).node4);
end
% Close the output file
fclose(fid);
end