/
turbine_analysis.py
985 lines (825 loc) · 39.2 KB
/
turbine_analysis.py
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
"""This project builds off a project given by Cornell's Spring 2013 MAE
2120 course. Given external settings, material properties, and the
design for a wind turbine, students were asked to solve for and
summarize mechanical factors faced by sections of the turbine's shafts.
Originally coded as a single, standalone file in MATLAB, this program
utilizes object-oriented programming via Python and integrates multiple
libraries in order to solve for, plot, display, and iterate via a GUI
turbine shaft geometries and safety factors.
"""
from __future__ import division
import sys
import numpy as np
from math import pi, sqrt, cos, sin
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt4 import QtGui, QtCore
from formulas import *
from constants import Constants
class Solutions(object):
"""This class houses values of safety factors and shaft specifications."""
def __init__(self, constants):
self.c = constants
count = len(self.c.L)
self.mass = [None]*count
self.sf_yield = [None]*count
self.sf_fatigue = [None]*count
self.max_twists = [None]*count
self.life = [None]*count
self.values_set = [None]*len(self.c.v_wind)
def solve(self):
"""Solve for safety factors against fatigue and yielding, the
maximum angle of twist, and days until failure for the shafts.
"""
c = self.c
# Check for invalid geometry. Run if all dimension are valid.
if 0 in c.L[1:]:
print "You cannot have lengths of 0!"
elif 0 in c.D[1:]:
print "You cannot have diameters of 0!"
else:
# Obtain and store relevant values for sections of the main shaft
# and crankshaft---these values are dependent on wind forces and
# their durations.
#
# These values are:
# values_set[i][0:7] - ratios of cycles to cycles until failure.
# values_set[i][7:14] - safety factors against yielding.
# values_set[i][14:21] - maximum angles of twist.
# *where 'i' corresponds to a 'wind force & duration'.
for i in range(1, len(c.v_wind)):
self.values_set[i] = self.get_values(c.v_wind[i], c.duration[i])
# Find the set of values corresponding to the strongest wind force
# and store corresponding safety factors against yielding and the
# maximum angles of twist.
max_wind_index = np.where(c.v_wind==max(c.v_wind))[0][0]
self.sf_yield[1:] = self.values_set[max_wind_index][7:14]
self.max_twists[1:] = self.values_set[max_wind_index][14:21]
#######################
# Days Until Failures #
#######################
# Calculate the days until sections of the shaft fail under given
# wind conditions.
sum_n = [0]*len(c.L)
for i in range(1, len(c.L)): # for each section
for k in range(1, len(c.v_wind)): # for each wind
sum_n[i] += self.values_set[k][i-1]
if sum_n[i] == 0:
self.life[i] = 'i' # inf
else:
self.life[i] = calc_life(sum_n[i])
#################################
# Safety Factor Against Fatigue #
#################################
# Calculate the safety factor against fatigue for sections of the
# shafts.
for i in range(1, len(c.L)):
if self.life[i] == 'i':
self.sf_fatigue[i] = 'i' # inf
else:
self.sf_fatigue[i] = calc_sf_fatigue(self.life[i],
c.life, c.exp_b)
########
# Mass #
########
# Create an array containing the masses of sections L1-L7 of the
# shafts.
for i in range(1, len(c.L)):
self.mass[i] = (calc_mass_cyl(c.L[i], c.D[i], c.mass_density))
def get_values(self, velocity, time):
"""Return a list containing safety factors against yielding,
maximum angles of twist, and values required to solve for
safety factors against fatigue and days until failure.
This method analyzes the shafts for a given wind velocity and
duration and obtains the maximum angles of twist undergone by
sections of the shaft, as well as safety factors against
yielding and the ratios of shaft revolutions to revolutions
until failure.
Parameters:
velocity: The wind velocity.
time: The corresponding wind duration.
Returns:
A list containing ratios of cycles to cycles until failure,
safety factors against yielding, and the maximum angle of
twist faced by the 7 shaft sections.
"""
# constants
c = self.c
# power generated by the wind
p_wind = c.cp*0.5*c.rho_air*(pi*c.r_prop**2)*velocity**3
# tangential velocity of the propeller tip
u = c.lamb*velocity
if velocity != 0:
# force_+x of the wind on the main shaft
f_wind = p_wind/velocity
# angular velocity of the propeller
omega_prop = u/c.r_prop
# torque_+x acting on the main shaft
t_in = p_wind/omega_prop
# 0 velocity results in 0 force, 0 rotation, and 0 torque.
else:
f_wind = 0
omega_prop = 0
t_in = 0
# torque_+x acting on the main shaft
t_gear_main = -t_in
# force_+y acting on the right end of the main shaft
f_gear_main = t_gear_main/c.R[1]
# reaction forces at point 2
rxn_2_x = -f_wind
rxn_2_y = -((c.L[2]+c.L[3]+c.L[4])*f_gear_main)/(c.L[2]+c.L[3])
# reaction forces at point 1
rxn_1_y = -rxn_2_y-f_gear_main
# torque acting on the gear of crankshaft
t_gear_crank = -t_gear_main*(c.R[2]/c.R[1])
# force_+y acting on the gear of the crankshaft
f_gear_crank = -f_gear_main
# torque acting on the crankshaft by the pump rod
t_crank = -t_gear_crank
# Calculate the force generated by the pump rod on the crankshaft
# and decompose it into orthogonal forces.
f_pump = abs(t_crank/(c.R[3]*cos(c.theta*pi/180)))
f_pump_z = -f_pump*cos(c.theta*pi/180)
f_pump_y = f_pump*sin(c.theta*pi/180)
# reaction forces at point 3
rxn_3_y = ((c.L[7]*f_gear_crank)-(c.L[6]*f_pump_y))/(c.L[6]+c.L[5])
rxn_3_z = (-f_pump_z*c.L[6])/(c.L[5]+c.L[6])
# reaction forces at point 4
rxn_4_y = -f_gear_crank-rxn_3_y-f_pump_y
rxn_4_z = -f_pump_z-rxn_3_z
###################
# Bending Moments #
###################
bending_z = {}
# Calculate z+ moments across the main shaft (lengths 1, 2&3, 4).
bending_z[1] = 0*c.length[1]
bending_z[2] = bending_z[1][-1]+(rxn_1_y*(c.length[2]-c.distance[1]))
bending_z[3] = bending_z[2][-1]+(rxn_1_y*(c.length[3]-c.distance[2]))
bending_z[23] = bending_z[1][-1]+(rxn_1_y*(c.length[23]-c.distance[1]))
bending_z[4] = bending_z[23][-1]+((rxn_2_y+rxn_1_y)
*(c.length[4]-c.distance[3]))
# Calculate z+ bending moments across the crankshaft.
bending_z[5] = rxn_3_y*c.length[5]
bending_z[6] = bending_z[5][-1]+((rxn_3_y+f_pump_y)
*(c.length[6]-c.distance[5]))
bending_z[7] = bending_z[6][-1]+((rxn_3_y+f_pump_y+rxn_4_y)
*(c.length[7]-c.distance[6]))
bending_y = {}
# Calculate y+ bending moments across the crankshaft.
bending_y[5] = rxn_3_z*c.length[5]
bending_y[6] = bending_y[5][-1]+((rxn_3_z+f_pump_z)
*(c.length[6]-c.distance[5]))
bending_y[7] = bending_y[6][-1]+((rxn_3_z+f_pump_z+rxn_4_z)
*(c.length[7]-c.distance[6]))
bending_mag = {}
# Calculate the magnitudes of bending moments across the crankshaft.
for i in range(5, 8):
bending_mag[i] = calc_magnitude(bending_z[i], bending_y[i])
#########################
# Normal Stress (Axial) #
#########################
sigma_axial = {}
# Calculate the normal stresses from axial loads across the main shaft.
for i in range (1, 5):
sigma_axial[i] = calc_axial_stress(-f_wind, c.D[i])
###########################
# Normal Stress (Bending) #
###########################
sigma_bending_x = {}
# Calculate the normal stresses from bending across the main shaft.
for i in range(1, 5):
sigma_bending_x[i] = abs(calc_bend_stress(bending_z[i], c.D[i]))
# Calculate the normal stresses from bending across the crankshaft.
for i in range(5, 8):
sigma_bending_x[i] = abs(calc_bend_stress(bending_mag[i], c.D[i]))
####################
# Effective Stress #
####################
sigma_eff = {}
# Calculate the effective stresses across the main shaft.
for i in range(1, 5):
sigma_eff[i] = calc_octa_stress(
sigma_bending_x[i]+abs(sigma_axial[i]),
t_in, c.D[i])
# Calculate the effective stresses across the crankshaft.
sigma_eff[5] = calc_octa_stress(sigma_bending_x[5], 0, c.D[5])
for i in range(6, 8):
sigma_eff[i] = calc_octa_stress(
sigma_bending_x[i],
t_gear_crank, c.D[i])
##################################
# Safety Factor against Yielding #
##################################
sf_yield = {}
# Calculate safety factors across both shafts.
for i in range(1, 8):
sf_yield[i] = calc_sf_yield(c.sigma_o, max(sigma_eff[i]))
######################
# Mean Cyclic Stress #
######################
sigma_avg = {}
# Store the mean cyclic stress across the main shaft.
for i in range(1, 5):
sigma_avg[i] = sigma_axial[i]
# Store the mean cyclic stress across the crankshaft
for i in range(5, 8):
sigma_avg[i] = 0 # due to no axial loads--cycles around 0 stress
####################
# Stress Amplitude #
####################
sigma_amp = {}
# Obtain the amplitudes of cyclic stresses.
for i in range(1, 8):
sigma_amp[i] = max(sigma_bending_x[i])
###################################################
# Equivalent Completely Reversed Stress Amplitude #
###################################################
sigma_rev = {}
# Calculate the equivalent completely reversed stress amplitudes across
# the shafts.
for i in range(1, 8):
sigma_rev[i] = calc_ECRSA(sigma_amp[i], sigma_avg[i], c.sigma_f)
########################
# Cycles until Failure #
########################
n_failure = {}
# Calculate the cycles to failure for the shaft sections.
for i in range(1, 8):
n_failure[i] = calc_cycles_fail(sigma_rev[i], c.sigma_f, c.exp_b)
###########################################
# Ratio of Cycles to Cycles until Failure #
###########################################
n_ratio = {}
# Calculate the ratio for the main shaft.
n_given_main = (omega_prop/(2*pi))*time # number of cycles
for i in range(1, 5):
n_ratio[i] = calc_cycle_ratio(n_given_main, n_failure[i])
# Calculate the ratio for the crankshaft.
n_given_crank = n_given_main/(c.R[2]/c.R[1]) # number of cycles
for i in range(5, 8):
n_ratio[i] = calc_cycle_ratio(n_given_crank, n_failure[i])
##########################
# Maximum Angle of Twist #
##########################
twist_max = {}
# Calculate the maximum angle of twist for the main shaft.
for i in range(1, 5):
twist_max[i] = max(abs(calc_max_twist(
t_in, c.length[i]-c.distance[i-1],
c.D[i], c.young_E, c.poisson_v)))
# Calculate the maximum angle of twist for the crankshaft.
twist_max[5] = max(abs(calc_max_twist(
0, c.length[5],
c.D[5], c.young_E, c.poisson_v)))
for i in range(6, 8):
twist_max[i] = max(abs(calc_max_twist(
t_crank, c.length[i]-c.distance[i-1],
c.D[i], c.young_E, c.poisson_v)))
##########
# Values #
##########
# Store the necessary information required to calculate the safety
# factor against stress-based fatigue.
answer = [n_ratio[1], n_ratio[2], n_ratio[3], n_ratio[4],
n_ratio[5], n_ratio[6], n_ratio[7],
sf_yield[1], sf_yield[2], sf_yield[3], sf_yield[4],
sf_yield[5], sf_yield[6], sf_yield[7],
twist_max[1], twist_max[2], twist_max[3], twist_max[4],
twist_max[5], twist_max[6], twist_max[7]
]
return answer
class InfoPlot(FigureCanvas):
"""This class takes inputted statics and mechanics solutions and
plots them. Shown are the main shaft and crankshaft geometries and
safety factors as well as design specifications.
"""
def __init__(self, solution, constants, normalized, parent=None):
"""Initialize the plot canvas and store data to be used.
Parameters:
solution: The set of data to be displayed (mass, safety factor
against yielding, safety factor against fatigue, maximum angle
of twist, and service life).
constants: Constants such as shaft lengths and diameters.
normalized: True if info is to be uniformly displayed.
"""
self.solution = solution
self.constants = constants
self.normalized = normalized
fig = Figure(tight_layout=True)
FigureCanvas.__init__(self, fig)
self.plot_info(fig)
def plot_info(self, fig):
"""Plot the shafts and corresponding information.
This method uses the solutions, constants, and normalized values
provided when InfoPlot was initialized.
The constants are used to calculate where shafts and corresponding
infomation are plotted. Solution values are used to determine the color
of the plots (red for failure, black otherwise). The normalized value
determines if the outputted info is uniformly displayed.
"""
c = self.constants
sol = self.solution
# initialize shaft coordinates
x = [0]*len(c.L)
y = [0]*len(c.L)
main_distance = 0
crank_distance = 0
# reference values used to scale plotted shafts and text
main_D_max = max(c.D[1:5])
crank_D_max = max(c.D[5:8])
main_length = sum(c.L[1:5])
crank_length = sum(c.L[5:8])
# reference values for plotting info depending on viewing property
if self.normalized == False:
main_info_location = 0
crank_info_location = 0
else:
main_info_location = -main_length/8
crank_info_location = -crank_length/6
# Draw each shaft section and display relevant info.
for i in range(1,8):
failure = False # whether or not safety factors are exceeded
# Plot-scaling values depend on what shaft is being plotted.
if i <= 4:
a = main_D_max
else:
a = crank_D_max
# Create a subplot for the main shaft.
if i == 1:
self.axes = fig.add_subplot(211)
self.axes.hold(True)
shaft_distance = main_distance
info_location = main_info_location
# Create a subplot for the crankshaft.
if i == 5:
self.axes = fig.add_subplot(212)
self.axes.hold(True)
shaft_distance = crank_distance
info_location = crank_info_location
# Location of shaft info depends on viewing property.
if self.normalized == False:
info_location = shaft_distance+c.L[i]/2
else:
if i <= 4:
info_location += main_length/4
else:
info_location += crank_length/3
# Print shaft geometry onto the plot.
self.axes.text(info_location, -(a/1.5)-(a/3.2),
'Diameter: %.2g m' % (c.D[i]),
ha='center', fontsize=11)
self.axes.text(info_location, -(a/1.5)-2*(a/3.2),
'Length: %.2g m' % (c.L[i]),
ha='center', fontsize=11)
# Print shaft safety factor against yielding.
if sol.sf_yield[i] == 'i':
sf_yield = 'inf'
color = 'k'
else:
sf_yield = str('%.2f' % (sol.sf_yield[i]))
if sol.sf_yield[i] < c.sf_o:
color = 'r'
failure = True
else:
color = 'k'
self.axes.text(info_location, -(a/1.5)-3*(a/3.2),
'Xo: %s' % (sf_yield), color=color,
ha='center', fontsize=11)
# Print shaft safety factor against fatigue.
if sol.sf_fatigue[i] == 'i':
sf_fatigue = 'inf'
color = 'k'
else:
sf_fatigue = str('%.2f' % (sol.sf_fatigue[i]))
if sol.sf_fatigue[i] < c.sf_s:
color = 'r'
failure = True
else:
color = 'k'
self.axes.text(info_location, -(a/1.5)-4*(a/3.2),
'Xs: %s' % (sf_fatigue), color=color,
ha='center', fontsize=11)
# Print shaft maximum angle of twist.
if sol.max_twists[i] == 'i':
twist = 'inf'
color = 'r'
else:
twist = str('%.2g' % (sol.max_twists[i]))
if sol.max_twists[i] > c.twist_max:
color = 'r'
failure = True
else:
color = 'k'
self.axes.text(info_location, -(a/1.5)-5*(a/3.2),
'Twist: %s rad' % (twist), color=color,
ha='center', fontsize=11)
# Print shaft service life.
if sol.life[i] == 'i':
service = 'inf'
color = 'k'
else:
service = str('%.3g' % (sol.life[i]))
if sol.life[i] < c.life:
color = 'r'
failure = True
else:
color = 'k'
self.axes.text(info_location, -(a/1.5)-6*(a/3.2),
'Life: %s days' % (service), color=color,
ha='center', fontsize=11)
# Draw the shafts.
if failure == True:
color = 'r'
else:
color = 'k'
x[i] = np.linspace(shaft_distance, shaft_distance+c.L[i], 2)
y[i] = np.linspace(c.D[i]/2, c.D[i]/2, 2)
# Plot horizontal lines
self.axes.plot(x[i], y[i], '-', linewidth=2.0, color=color)
self.axes.plot(x[i], -y[i], '-', linewidth=2.0, color=color)
# Plot vertical lines
self.axes.plot((shaft_distance, shaft_distance),
(-c.D[i]/2, c.D[i]/2),
'-', linewidth=2.0, color=color)
shaft_distance += c.L[i]
self.axes.plot((shaft_distance, shaft_distance),
(-c.D[i]/2, c.D[i]/2),
'-', linewidth=2.0, color=color)
if i == 4:
# Configure the plot axes and titles of the main shaft.
self.configure_plot(self.axes, shaft_distance, a, i)
if i == 7:
# Configure the plot axes and titles of the crankshaft.
self.configure_plot(self.axes, shaft_distance, a, i)
def configure_plot(self, axes, shaft_length, a, i):
"""Configure the plot axes and titles and display specifications.
Parameters:
axes: The subplot being made.
shaft_length: The length of the entire shaft.
a: A value to scale text to the plot.
i: The iteration number to determine what shaft is being plotted.
"""
c = self.constants
sol = self.solution
xmin = 0
xmax = shaft_length
ymin = 3*-a
ymax = 3*a
axes.axis([xmin, xmax, ymin, ymax])
if i == 4:
axes.set_title('Main Shaft')
mass = sum(sol.mass[1:5])
else:
axes.set_title('Crankshaft')
mass = sum(sol.mass[5:8])
axes.set_xlabel('Length from Left Edge [m]')
axes.set_ylabel('Radius [m]')
# Plot design specifications.
shaft_info = ("Total Mass: %.2f kg\nMinimum Required Safety Factor"
" Against Yielding, Xo: %.2g\nMinimum Required Safety"
" Factor Against Fatigue, Xs: %.2g\nMaximum Allowable"
" Angle of Twist: %.2g rad\nDesired Service Life: %.2g"
" days" % (mass, c.sf_o, c.sf_s, c.twist_max, c.life))
axes.text(xmax/2, a, shaft_info, ha='center', fontsize=11)
class EditWindow(QtGui.QDialog):
"""This is a window which allows the user to change the current
lengths and diameters of sections of the shafts. When dimensions
are updated, the main window refreshes and shows info corresponding
to the updated design.
"""
def __init__(self, constants, parent):
QtGui.QDialog.__init__(self)
self.setModal(True)
self.setFixedWidth(200)
self.parent = parent
self.c = constants
self.L = [None]*len(self.c.L) # lengths in Edit Window.
self.D = [None]*len(self.c.D) # diameters in Edit Window.
vlayout = QtGui.QVBoxLayout()
line = [None]*8
# Add fields to edit main shaft sections.
main_header = QtGui.QLabel('Main Shaft')
vlayout.addWidget(main_header)
for i in range(1,5):
line[i] = QtGui.QHBoxLayout()
line[i].addWidget(QtGui.QLabel("L%i" % i))
self.L[i] = QtGui.QLineEdit(str(self.c.L[i]))
line[i].addWidget(self.L[i])
line[i].addWidget(QtGui.QLabel("D%i" % i))
self.D[i] = QtGui.QLineEdit(str(self.c.D[i]))
line[i].addWidget(self.D[i])
vlayout.addLayout(line[i])
vlayout.addWidget(QtGui.QLabel(""))
# Add fields to edit crankshaft sections.
crank_header = QtGui.QLabel('Crankshaft')
vlayout.addWidget(crank_header)
for i in range(5,8):
line[i] = QtGui.QHBoxLayout()
line[i].addWidget(QtGui.QLabel('L%i' % i))
self.L[i] = QtGui.QLineEdit(str(self.c.L[i]))
line[i].addWidget(self.L[i])
line[i].addWidget(QtGui.QLabel('D%i' % i))
self.D[i] = QtGui.QLineEdit(str(self.c.D[i]))
line[i].addWidget(self.D[i])
vlayout.addLayout(line[i])
vlayout.addWidget(QtGui.QLabel(""))
# Add update and cancel buttons.
update_button = QtGui.QPushButton('Update')
update_button.clicked.connect(self.update)
cancel_button = QtGui.QPushButton('Cancel')
cancel_button.clicked.connect(self.cancel)
buttonBox = QtGui.QDialogButtonBox()
buttonBox.addButton(update_button, QtGui.QDialogButtonBox.ActionRole)
buttonBox.addButton(cancel_button, QtGui.QDialogButtonBox.ActionRole)
button_line = QtGui.QHBoxLayout()
button_line.addStretch(0)
button_line.addWidget(buttonBox)
button_line.addStretch(0)
vlayout.addLayout(button_line)
self.setLayout(vlayout)
def update(self):
"""Update the plot in the main window."""
window_title = 'Edit Design [m]'
# Store Edit Window dimensions based on input.
temp_L = [None]*len(self.c.L)
temp_D = [None]*len(self.c.L)
all_are_nums = True # Inputted values are all numbers.
for i in range(1, len(self.c.L)):
try:
temp_L[i] = float(self.L[i].text())
temp_D[i] = float(self.D[i].text())
except ValueError:
window_title = 'Numbers only.'
all_are_nums = False
# Make sure no dimensions of 0 or non-numbers are inputted. If
# dimensions are valid, update dimensions to those inputted to the Edit
# Window.
if 0 in temp_L[1:] or 0 in temp_D[1:]:
window_title = 'No 0s allowed.'
elif all_are_nums:
for i in range(1, len(self.c.L)):
self.c.L[i] = float(self.L[i].text())
self.c.D[i] = float(self.D[i].text())
self.parent.reload = True
self.parent.refresh()
self.close()
self.setWindowTitle(window_title)
# Close the window.
def cancel(self):
self.close()
class AppWindow(QtGui.QMainWindow):
"""This is the main application window displaying the shaft
geometries and relevant safety factors. A toolbar allows the
user to load & save files, edit the configuration, and alter views.
"""
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("Wind Turbine Shaft Analysis")
self.setFixedSize(1280,720)
self.create_actions()
self.create_menu()
# Setup the main application.
self.main_widget = QtGui.QWidget(self)
self.layout = QtGui.QVBoxLayout(self.main_widget)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
# Initialize constants and statuses of the application.
self.init_values()
# Update the main window with solutions.
self.update_solution()
# Tell the user what files are still required.
self.update_status()
def init_values(self):
"""Initialize statuses of whether or not files have been
loaded, whether shaft information displays are normalized,
and whether or not the plot needs to be refreshed. Also
initialize a class holding constants.
"""
# Holds the states of whether the necessary files have been loaded.
# The 4 required files are:
# - designs specifications
# - material properties
# - load factors
# - wind history.
self.loaded = [False, False, False, False]
# This value determines how details of the shaft sections are displayed.
# A value of 'True' scales text uniformly; A value of 'False' places
# text underneath plotted shaft sections.
self.normalized = False
# This value determines whether solutions have to be (re)solved.
# Prevents recalculating solutions if user only wants to change views.
self.reload = True
# Initialize constants.
self.c = Constants()
def create_actions(self):
"""Create menu actions."""
# File Menu Actions
self.act_obt_design = QtGui.QAction('Obtain Design', self,
shortcut='Ctrl+1', triggered=self.open_design)
self.act_obt_prop = QtGui.QAction('Obtain Material Properties', self,
shortcut='Ctrl+2', triggered=self.open_prop)
self.act_obt_load = QtGui.QAction('Obtain Load Factors', self,
shortcut='Ctrl+3', triggered=self.open_load)
self.act_obt_wind = QtGui.QAction('Obtain Wind History', self,
shortcut='Ctrl+4', triggered=self.open_wind)
self.act_save_design = QtGui.QAction('Save Design', self,
shortcut='Ctrl+S', triggered=self.save_design)
self.act_quit = QtGui.QAction('Quit', self,
shortcut='Ctrl+Q', triggered=self.quit_file)
# Edit Menu Actions
self.act_edit_design = QtGui.QAction('Edit Design', self,
shortcut='Ctrl+E', triggered=self.edit_design)
# View Menu Actions
self.act_normalize_view = QtGui.QAction('Normalize View', self,
shortcut='Ctrl+V', checkable=True,
triggered=self.normalize_view)
self.act_normalize_view.setStatusTip('Uniformly reposition shaft '
'information. The order of information still '
'corresponds to the order of shaft sections '
'i.e. the first set of information corresponds'
' to section 1, the second to section 2, etc.')
# Help Menu Actions
self.act_about = QtGui.QAction('About', self,
shortcut='Ctrl+A', triggered=self.about)
def create_menu(self):
"""Create the application menu with File, Edit, View, Help
and About tabs.
"""
file_menu = QtGui.QMenu('File', self)
file_menu.addAction(self.act_obt_design)
file_menu.addAction(self.act_obt_prop)
file_menu.addAction(self.act_obt_load)
file_menu.addAction(self.act_obt_wind)
file_menu.addSeparator()
file_menu.addAction(self.act_save_design)
file_menu.addSeparator()
file_menu.addAction(self.act_quit)
edit_menu = QtGui.QMenu('Edit', self)
edit_menu.addAction(self.act_edit_design)
view_menu = QtGui.QMenu('View', self)
view_menu.addAction(self.act_normalize_view)
help_menu = QtGui.QMenu('Help', self)
help_menu.addAction(self.act_about)
menubar = self.menuBar()
menubar.addMenu(file_menu)
menubar.addMenu(edit_menu)
menubar.addMenu(view_menu)
menubar.addMenu(help_menu)
def update_solution(self):
"""Plot shaft sections and related info to the application
window.
"""
if self.loaded == [True, True, True, True]:
if self.reload == True:
self.sol = Solutions(self.c)
self.sol.solve()
infoplot = InfoPlot(self.sol, self.c,
self.normalized, self.main_widget)
self.layout.addWidget(infoplot)
self.reload = False
else:
infoplot = InfoPlot(self.sol, self.c,
self.normalized, self.main_widget)
self.layout.addWidget(infoplot)
def delete_solution(self):
"""Delete the current plot of the shaft and infomation."""
for i in range(self.layout.count()):
self.layout.itemAt(i).widget().setParent(None)
def normalize_view(self):
"""Scale the info dislayed beneath each shaft section so that
text does not interfer with each other or the plot.
"""
self.normalized = not self.normalized
self.refresh()
def refresh(self):
"""Refresh the plot by deleting current solutions and updating
with another set of solutions.
"""
self.delete_solution()
self.update_solution()
self.update_status()
def update_status(self):
"""Update the status bar and display what files are still
required.
"""
self.statusBar().showMessage(self.check_loaded())
def status_format_error(self):
"""Display an error message on the status bar if the format of
a file is not usable."""
self.statusBar().showMessage("ERROR. Please check formatting of file.")
def open_design(self):
"""Load shaft geometries from a file."""
try:
file = QtGui.QFileDialog.getOpenFileName(self, 'Open design')
self.c.open_design(str(file))
self.loaded[0] = True
self.reload = True
self.refresh()
except IndexError or ValueError:
self.status_format_error()
def open_prop(self):
"""Load material properties from a file."""
try:
file = QtGui.QFileDialog.getOpenFileName(self, 'Open material properties')
self.c.open_prop(str(file))
self.loaded[1] = True
self.reload = True
self.refresh()
except IndexError or ValueError:
self.status_format_error()
def open_load(self):
"""Load external/environmental factors from a file."""
try:
file = QtGui.QFileDialog.getOpenFileName(self, 'Open load factors')
self.c.open_load(str(file))
self.loaded[2] = True
self.reload = True
self.refresh()
except IndexError or ValueError:
self.status_format_error()
def open_wind(self):
"""Load wind history from a file."""
try:
file = QtGui.QFileDialog.getOpenFileName(self, 'Open wind history')
self.c.open_wind(str(file))
self.loaded[3] = True
self.reload = True
self.refresh()
except IndexError or ValueError:
self.status_format_error()
def check_loaded(self):
"""Check to see what files have been loaded and return a
message stating which files are still needed.
"""
msg0 = msg1 = msg2 = msg3 = msg4 = ""
if False in self.loaded:
msg0 = "Required: "
if self.loaded[0] == False:
msg1 = "Design specifications. "
if self.loaded[1] == False:
msg2 = "Material properties. "
if self.loaded[2] == False:
msg3 = "Load factors. "
if self.loaded[3] == False:
msg4 = "Wind history."
message = "%s%s%s%s%s" % (msg0, msg1, msg2, msg3, msg4)
return message
def save_design(self):
"""Save the current design by storing shaft lengths and
diameters into a file. Original design specifications are
stored in order to preserve the design file format.
"""
if self.loaded[0] == False:
self.statusBar().showMessage("Please load design specifications first.")
else:
c = self.c
design = (('%g %g %g %g %g %g %g\n'
'%g %g %g %g %g %g %g\n'
'%g %g %g %g %g %g %g') %
(c.L[1], c.L[2], c.L[3], c.L[4], c.L[5], c.L[6], c.L[7],
c.D[1], c.D[2], c.D[3], c.D[4], c.D[5], c.D[6], c.D[7],
c.sf_o, c.sf_s, c.life, c.twist_max, c.R[1], c.R[2], c.R[3]))
file = QtGui.QFileDialog.getSaveFileName(self, 'Save design',
selectedFilter='*.txt')
fname = open(file, 'w')
fname.write(design)
fname.close()
def edit_design(self):
"""Open a new window to allow users to edit the lengths and
diameters of the shaft sections.
"""
if self.loaded[0] == False:
self.statusBar().showMessage("Please load design specifications first.")
else:
self.edit_window = EditWindow(self.c, parent=self)
self.edit_window.show()
self.edit_window.setWindowTitle('Edit Design [m]')
def about(self):
QtGui.QMessageBox.about(self, 'About', "This project builds off a"
" project given by Cornell's Spring 2013 MAE"
" 2120 course. Given external settings,"
" material properties, and the design for a"
" wind turbine, students were asked to solve"
" for and summarize mechanical factors faced"
" by sections of the turbine's shafts."
" Originally coded as a single, standalone"
" file in MATLAB, this program utilizes"
" object-oriented programming via Python and"
" integrates multiple libraries in order to"
" solve for, plot, display, and iterate via a"
" GUI turbine shaft geometries and safety"
" factors.")
# Close the application when Ctrl+Q keyboard command is inputted or when
# 'Quit' is selected in the 'File' menu tab.
def quit_file(self):
self.close()
# Close the application when the red 'X' of the window is pressed.
def closeEvent(self, event):
self.close()
if __name__ == "__main__":
App = QtGui.QApplication(sys.argv)
aw = AppWindow()
aw.setWindowTitle('Wind Turbine Shaft Analysis')
aw.show()
sys.exit(App.exec_())