-
Notifications
You must be signed in to change notification settings - Fork 5
/
GUI.py
executable file
·1284 lines (1143 loc) · 69.2 KB
/
GUI.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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import tkinter as tk
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import matplotlib.animation as animation
import copy
import importlib
import inspect
import math
import multiprocessing
import networkx as nx
import numpy as np
import os
from pathlib import Path
import pickle
import sys
import time
import traceback
import CDTtools # a package that contains, among other things, dyn_core_tools.py
import custom_functions # a package that handles the various custom functions used for torque specification
toplevel_path = os.path.dirname(os.path.realpath('__file__'))
class CDT_GUI:
def __init__(self, master):
open('logfile', 'w').close() # wipe log file
self.cpr, self.cps = multiprocessing.Pipe(duplex=False) # one-way pipe from solver process to parent
# cpr is the receiving end
self.rp = 4 # number of digits after decimal in rounded values
self.cspcalls = 0
self.solver_runningB = tk.BooleanVar() # is a solver currently running?
self.solver_runningB.set(False)
self.solver_runningB.trace_add("write", self._solver_state_change)
# needs to be set before adding the trace, else the state change callback tries to disable
# a button that doesn't yet exist
self.prev_solve_type = None # needed by postproc and solver diagnostics to tell what kind
# of run the self.sol object came from, as the user may have changed simTypeVar since the
# previous run
self.master = master
master.title("Catapult Design Tool")
tk.Tk.report_callback_exception = self.show_error # turns off the default "silent failure mode"...
self.back = tk.Frame(master=self.master)
# self.back.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
self.back.pack(fill=tk.BOTH, expand=1) #Expand the frame to fill the root window
# PLOT WINDOW 1
self.fig1 = Figure(figsize=(5, 4), dpi=100, tight_layout=True)
self.ax1 = self.fig1.add_subplot(111)
self.plotFrame1 = tk.Frame(master=self.back)
self.plotFrame1.grid(row=2, column=0, rowspan=2, columnspan=2)
self.plot1SubFrame = tk.Frame(master=self.plotFrame1)
self.plot1SubFrame.grid(row=0, column=0, columnspan=2)
self.canvas1 = FigureCanvasTkAgg(self.fig1, master=self.plot1SubFrame) # A tk.DrawingArea.
self.canvas1.draw()
self.canvas1.get_tk_widget().pack()
self.toolbar1 = NavigationToolbar2Tk(self.canvas1, self.plot1SubFrame)
self.toolbar1.update()
self.p1_conditional_controls = tk.Frame(master=self.plotFrame1)
self.p1_conditional_controls.grid(row=2, column=0, columnspan=2) # stuff like the play button for the animation
# navigation toolbars use pack() internally, so you can't place them with grid
# so you create a frame that's a child of the main frame, position the *child frame* using grid,
# and the toolbar packs itself within that frame...
tk.Label(master=self.plotFrame1, text="Plot Type").grid(row=1, column=0)
self.p1TypeVar = tk.StringVar()
self.p1TypeVar.trace_add("write", self._change_p1)
self.p1types = ['animation', 'arm load', 'projectile path',
'efficiency', 'puller potential energy', 'axle reaction load']
self.prevP1Type = None # necessary because destroy doesn't properly destroy all widgets in p1 cond frame
self.p1TypeVar.set(self.p1types[0])
self.p1TypeMenu = tk.OptionMenu(self.plotFrame1, self.p1TypeVar, *self.p1types)
self.p1TypeMenu.grid(row=1, column=1)
# PLOT WINDOW 1
# PLOT WINDOW 2
self.fig2 = Figure(figsize=(5, 4), dpi=100, tight_layout=True)
self.ax2 = self.fig2.add_subplot(111)
self.plotFrame2 = tk.Frame(master=self.back)
self.plotFrame2.grid(row=2, column=3, rowspan=2)
self.plot2SubFrame = tk.Frame(master=self.plotFrame2)
self.plot2SubFrame.grid(row=0, column=0)
self.canvas2 = FigureCanvasTkAgg(self.fig2, master=self.plot2SubFrame) # A tk.DrawingArea.
self.canvas2.draw()
self.canvas2.get_tk_widget().pack()
self.toolbar2 = NavigationToolbar2Tk(self.canvas2, self.plot2SubFrame)
self.toolbar2.update()
self.p2ControlF = tk.Frame(master=self.plotFrame2) # child frame for plot 2 controls
self.p2ControlF.grid(row=1, column=0)
tk.Label(master=self.p2ControlF, text="x axis").grid(row=0, column=0)
self.p2xVar = tk.StringVar()
self.p2xOpts = ['time', 'theta', 'psi', 'projectile velocity']
self.p2xVar.set(self.p2xOpts[0])
self.p2xVar.trace_add("write", self._change_p2)
self.p2xMenu = tk.OptionMenu(self.p2ControlF, self.p2xVar, *self.p2xOpts)
self.p2xMenu.grid(row=0, column=1)
tk.Label(master=self.p2ControlF, text="y axis").grid(row=0, column=2)
self.p2yVar = tk.StringVar()
self.p2yOpts = ['sling tension', 'projectile velocity', 'theta', 'psi',
'arm rotational speed', 'puller speed']
self.p2yVar.set(self.p2yOpts[0])
self.p2yVar.trace_add("write", self._change_p2)
self.p2yMenu = tk.OptionMenu(self.p2ControlF, self.p2yVar, *self.p2yOpts)
self.p2yMenu.grid(row=0, column=3)
# PLOT WINDOW 2
# MAIN CONTROLS
self.mcFrame = tk.Frame(master=self.back)
self.mcFrame.grid(row=3, column=2)
self.simMsgM = tk.Message(master=self.mcFrame, text="", width=256, bg='white')
self.simMsgM.grid(row=0, column=0, rowspan=2)
self.msg_text = tk.StringVar()
self.msg_text.trace_add("write", self._update_simMsg)
self.msg_text.set("") # contents of simMsgM
self.run_button = tk.Button(master=self.mcFrame, text="Start Solver", command=self._simulate)
self.run_button.grid(row=0, column=1)
self.stop_button = tk.Button(master=self.mcFrame, text="Stop Solver", command=self._stopsolve)
self.stop_button.grid(row=1, column=1)
self.stop_button.config(state=tk.DISABLED)
# MAIN CONTROLS
# SAVE AND LOAD BUTTONS
self.saveFrame = tk.Frame(master=self.back)
self.saveFrame.grid(row=0, column=0, columnspan=2, sticky=tk.NW)
self.saveBtn = tk.Button(master=self.saveFrame, text="Save Design", command=self._save_design)
self.saveBtn.grid(row=0, column=0)
self.loadBtn = tk.Button(master=self.saveFrame, text="Load Design", command=self._load_design)
self.loadBtn.grid(row=0, column=1)
self.quit_button = tk.Button(master=self.saveFrame, text="Quit", command=self._quit)
self.quit_button.grid(row=0, column=2)
# SAVE AND LOAD BUTTONS
# DYNAMICS PARAMETERS
self.dpFrame = tk.LabelFrame(master=self.back, text="Dynamics Parameters")
self.dpFrame.grid(row=1, column=0)
DPF = self.dpFrame #shorthand name
# correct order: (La, Ls, ds, mb, rc, Ia, mp, g)
self.DPEL = ("Arm Length", "Sling Length", "Sling Linear Density", "Arm Mass",
"Distance from Pivot to Arm COM", "Arm Rotational Inertia", "Projectile + Pouch Mass",
"Gravitational Acceleration") # ORDER IS IMPORTANT!
DPF.CW = self._gen_entries2(DPF, self.DPEL, (0,0), dict(), maxcol=len(self.DPEL))
DPF.CW['Gravitational AccelerationE'].insert(0, "9.8")
# DYNAMICS PARAMETERS
# INITIAL CONDITIONS
self.icFrame = tk.LabelFrame(master=self.back, text="Initial Conditions")
self.icFrame.grid(row=1, column=1) # between the plot windows
tk.Label(master=self.icFrame, text="\u03B8").grid(row=0, column=0)
self.t0SV = tk.StringVar()
self.theta0E = tk.Entry(master=self.icFrame, textvariable=self.t0SV)
self.theta0E.grid(row=0, column=1)
self.theta0E.insert(0, "3.14")
self.ICEL = ("d\u03B8/dt", "\u03C8", "d\u03C8/dt")
self.icFrame.CW = self._gen_entries2(self.icFrame, self.ICEL, (1,0), dict(), maxcol=len(self.ICEL))
self.icFrame.CW['d\u03B8/dtE'].insert(0, "0")
self.icFrame.CW['d\u03C8/dtE'].insert(0, "0")
# INITIAL CONDITIONS
# SIMULATION CONTROLS
self.scFrame = tk.LabelFrame(master=self.back, text="Solver Controls")
self.scFrame.grid(row=1, column=3)
self.scCondFrame = tk.Frame(master=self.scFrame)
# frame containing the conditional entries, which depend on simtype
self.scCondFrame.grid(row=4, column=0, columnspan=2)
# --- widget definition for scCondFrame ---
self.simtypes = ['psi', 'launch_angle', 'sling_len_opt', 'arm_inertia_opt', 'max_speed']
self.sccw = {k: dict() for k in self.simtypes}
self.sccw['psi']['psi_label'] = tk.Label(master=self.scCondFrame, text="psi at release")
self.sccw['psi']['psi_label'].grid(row=0, column=0)
self.sccw['psi']['psif'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['psi']['psif'].grid(row=0, column=1)
self.sccw['launch_angle']['pb_label'] = tk.Label(master=self.scCondFrame,
text="psi final bounds", wraplength=80)
self.sccw['launch_angle']['pb_label'].grid(row=0, column=0)
self.sccw['launch_angle']['psifmin'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['launch_angle']['psifmin'].grid(row=0, column=1)
self.sccw['launch_angle']['psifmax'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['launch_angle']['psifmax'].grid(row=0, column=2)
self.sccw['launch_angle']['la_label'] = tk.Label(master=self.scCondFrame, text="launch angle")
self.sccw['launch_angle']['la_label'].grid(row=2, column=0)
self.sccw['launch_angle']['vpa'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['launch_angle']['vpa'].grid(row=2, column=1)
self.sccw['sling_len_opt']['pb_label'] = self.sccw['launch_angle']['pb_label']
self.sccw['sling_len_opt']['psifmin'] = self.sccw['launch_angle']['psifmin']
self.sccw['sling_len_opt']['psifmax'] = self.sccw['launch_angle']['psifmax']
self.sccw['sling_len_opt']['slb_label'] = tk.Label(master=self.scCondFrame,
text="sling length bounds", wraplength=80)
self.sccw['sling_len_opt']['slb_label'].grid(row=1, column=0)
self.sccw['sling_len_opt']['lsmin'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['sling_len_opt']['lsmin'].grid(row=1, column=1)
self.sccw['sling_len_opt']['lsmax'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['sling_len_opt']['lsmax'].grid(row=1, column=2)
self.sccw['sling_len_opt']['la_label'] = self.sccw['launch_angle']['la_label']
self.sccw['sling_len_opt']['vpa'] = self.sccw['launch_angle']['vpa']
self.sccw['sling_len_opt']['tf_label'] = tk.Label(master=self.scCondFrame, text="\u03B8 final")
self.sccw['sling_len_opt']['tf_label'].grid(row=3, column=0)
self.sccw['sling_len_opt']['thetaf'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['sling_len_opt']['thetaf'].grid(row=3, column=1)
self.sccw['arm_inertia_opt']['pb_label'] = self.sccw['launch_angle']['pb_label']
self.sccw['arm_inertia_opt']['psifmin'] = self.sccw['launch_angle']['psifmin']
self.sccw['arm_inertia_opt']['psifmax'] = self.sccw['launch_angle']['psifmax']
self.sccw['arm_inertia_opt']['aib_label'] = tk.Label(master=self.scCondFrame,
text="arm inertia bounds", wraplength=80)
self.sccw['arm_inertia_opt']['aib_label'].grid(row=1, column=0)
self.sccw['arm_inertia_opt']['aimin'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['arm_inertia_opt']['aimin'].grid(row=1, column=1)
self.sccw['arm_inertia_opt']['aimax'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['arm_inertia_opt']['aimax'].grid(row=1, column=2)
self.sccw['arm_inertia_opt']['la_label'] = self.sccw['launch_angle']['la_label']
self.sccw['arm_inertia_opt']['vpa'] = self.sccw['launch_angle']['vpa']
self.sccw['arm_inertia_opt']['tf_label'] = self.sccw['sling_len_opt']['tf_label']
self.sccw['arm_inertia_opt']['thetaf'] = self.sccw['sling_len_opt']['thetaf']
self.sccw['max_speed']['pb_label'] = self.sccw['launch_angle']['pb_label']
self.sccw['max_speed']['psifmin'] = self.sccw['launch_angle']['psifmin']
self.sccw['max_speed']['psifmax'] = self.sccw['launch_angle']['psifmax']
self.sccw['max_speed']['slb_label'] = self.sccw['sling_len_opt']['slb_label']
self.sccw['max_speed']['lsmin'] = self.sccw['sling_len_opt']['lsmin']
self.sccw['max_speed']['lsmax'] = self.sccw['sling_len_opt']['lsmax']
self.sccw['max_speed']['aib_label'] = tk.Label(master=self.scCondFrame,
text="arm inertia bounds", wraplength=80)
self.sccw['max_speed']['aib_label'].grid(row=2, column=0)
self.sccw['max_speed']['aimin'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['max_speed']['aimin'].grid(row=2, column=1)
self.sccw['max_speed']['aimax'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['max_speed']['aimax'].grid(row=2, column=2)
self.sccw['max_speed']['la_label'] = tk.Label(master=self.scCondFrame, text="launch angle")
self.sccw['max_speed']['la_label'].grid(row=4, column=0)
self.sccw['max_speed']['vpa'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['max_speed']['vpa'].grid(row=4, column=1)
self.sccw['max_speed']['tf_label'] = tk.Label(master=self.scCondFrame, text="\u03B8 final")
self.sccw['max_speed']['tf_label'].grid(row=5, column=0)
self.sccw['max_speed']['thetaf'] = tk.Entry(master=self.scCondFrame, width=8)
self.sccw['max_speed']['thetaf'].grid(row=5, column=1)
# -------------------------------
tk.Label(master=self.scFrame, text="Time Step").grid(row=0, column=0)
self.dtE = tk.Entry(master=self.scFrame)
self.dtE.grid(row=0, column=1)
self.dtE.insert(0, "0.001")
self.platformVar = tk.BooleanVar()
tk.Label(master=self.scFrame, text="Platform").grid(row=1, column=0)
tk.Checkbutton(master=self.scFrame, variable=self.platformVar).grid(row=1, column=1)
tk.Label(master=self.scFrame, text="Simulation Type").grid(row=2, column=0)
self.simTypeVar = tk.StringVar(root)
self.simTypeVar.trace_add("write", self._stchange) # calls _stchange when simTypeVar is written to
self.simTypeVar.set(self.simtypes[1])
self.simTypeMenu = tk.OptionMenu(self.scFrame, self.simTypeVar, *self.simtypes)
self.simTypeMenu.grid(row=2, column=1)
tk.Label(master=self.scFrame, text="Solver Timeout (s)").grid(row=5, column=0)
self.timeoutE = tk.Entry(master=self.scFrame)
self.timeoutE.grid(row=5, column=1)
self.timeoutE.insert(0, "10")
# SIMULATION CONTROLS
# TORQUE CONTROLS
self.tcFrame = tk.LabelFrame(master=self.back, text="Torque Source")
self.tcFrame.grid(row=0, column=2, rowspan=3)
tk.Label(master=self.tcFrame, text="Torque Specification Type").grid(row=0, column=0)
self.tsTypeVar = tk.StringVar() # constructor argument just specifies the Tk instance, and we only have 1
self.tsTypes = ['Configuration A', 'Configuration B', 'r(\u03B8) and F(\u03B8)',
'\u03C4(\u03B8) and r(\u03B8)']
self.tsTypeVar.trace_add("write", self._tcchange)
self.tsTypeMenu = tk.OptionMenu(self.tcFrame, self.tsTypeVar, *self.tsTypes)
self.tsTypeMenu.grid(row=0, column=1)
self.tcCondFrame = tk.Frame(master=self.tcFrame)
self.tcCondFrame.grid(row=1, column=0, columnspan=2)
self.GPFpos = {'row': 1, 'column': 0, 'columnspan': 2, 'rowspan': 2} # shared b/w A, B
tk.Label(master=self.tcFrame, text="Hysteresis Parameter (k_w)").grid(row=2, column=0)
self.tc_kwE = tk.Entry(master=self.tcFrame)
self.tc_kwE.grid(row=2, column=1)
self.torqueMsgM = tk.Message(master=self.tcFrame, text="", width=430)
self.torqueMsgM.grid(row=4, column=0, columnspan=2)
self.tfig = Figure(figsize=(4, 3), dpi=100, tight_layout=True)
self.tax = self.tfig.add_subplot(111)
self.tplotFrame = tk.Frame(master=self.tcFrame)
self.tplotFrame.grid(row=3, column=0, columnspan=2)
self.tCanvas = FigureCanvasTkAgg(self.tfig, master=self.tplotFrame)
self.tCanvas.draw()
self.tCanvas.get_tk_widget().pack()
# DICTIONARIES OF custom_func: callable FOR ALL CUSTOM FUNCTION MODULES
self.RThetaDict = self.funcDict(custom_functions.RofTheta)
self.FLDict = self.funcDict(custom_functions.FofL)
self.TauThetaDict = self.funcDict(custom_functions.TauofTheta)
self.FThetaDict = self.funcDict(custom_functions.FofTheta)
# ******************************************************
self.tcFVar = tk.StringVar()
self.tcTauVar = tk.StringVar()
self.tcRVar = tk.StringVar()
self.tcF2Var = tk.StringVar()
IRlabels = ['\u03B8 min', '\u03B8 max']
IRstartpos = (4, 0)
tpf_maxcol = 4
self.tccw = {k: dict() for k in self.tsTypes}
self.tccw['A'] = dict()
self.tccw['A']['gpl'] = tk.Label(master=self.tcCondFrame, text="Geometry Parameters")
self.tccw['A']['gpl'].grid(row=0, column=0, columnspan=2)
self.tccw['A']['GPFrame'] = tk.Frame(master=self.tcCondFrame)
self.tccw['A']['GPFrame'].grid(**self.GPFpos)
GPFA = self.tccw['A']['GPFrame'] #shorthand name
GPFED = {'A': ('r_s', 'd', '\u03B2'), 'B': ('r_w', 'r_c', 'd', '\u03B2')}
GPFsp = {'A': (1, 0), 'B': (1, 0)}
GPFA.CW = self._gen_entries2(GPFA, GPFED['A'], GPFsp['A'], dict()) # adds all relevant entries & labels
self.tccw['A']['fpl'] = tk.Label(master=self.tcCondFrame, text='Force Parameters')
self.tccw['A']['fpl'].grid(row=0, column=2, columnspan=2)
self.tccw['A']['fll'] = tk.Label(master=self.tcCondFrame, text='F(L)')
self.tccw['A']['fll'].grid(row=1, column=2)
self.tccw['A']['tsTypeMenu'] = tk.OptionMenu(self.tcCondFrame, self.tcFVar, *self.FLDict.keys())
self.tccw['A']['tsTypeMenu'].grid(row=1, column=3)
self.tccw['A']['irl'] = tk.Label(master=self.tcCondFrame, text="Interpolation Range")
self.tccw['A']['irl'].grid(row=3, column=0, columnspan=2)
self.tccw['common'] = dict()
self.tccw['common']['irl'] = tk.Label(master=self.tcCondFrame, text="Interpolation Range")
self.tccw['common']['irl'].grid(row=3, column=0, columnspan=2)
self.tccw['common'] = self._gen_entries2(self.tcCondFrame, IRlabels, IRstartpos, self.tccw['common'])
self.t0SV.trace_add("write", self._t0update) # NOW we can add the trace to the theta0 tracker
# interpolation range stuff
self.tccw['A']['cfBtn'] = tk.Button(master=self.tcCondFrame, text="Calculate Fit", command=self._calcfit)
self.tccw['A']['cfBtn'].grid(row=5, column=2, columnspan=2)
self.tccw['A']['FLFrame'] = tk.Frame(master=self.tcCondFrame)
self.tccw['A']['FLFrame'].grid(row=2, column=2, columnspan=2)
# now we need to create all possible variations of FLFrame contents
FLFA = self.tccw['A']['FLFrame'] # shorthand
FLFA.CW = self._parameter_entry_initializer(dict(), FLFA, self.FLDict, (0,0), maxcol=tpf_maxcol) # child widgets
for w in FLFA.winfo_children():
w.grid_remove() # otherwise all parameters for F(L) are present despite no func being selected
def FLchange(*args):
self._entry_change(self.tccw['A']['FLFrame'], self.tccw['A']['FLFrame'].CW, self.tcFVar.get())
self.tcFVar.trace_add("write", FLchange) # when the F(L) specification type changes
# CONFIGURATION B *****************
self.tccw['B'] = dict()
self.tccw['B']['gpl'] = tk.Label(master=self.tcCondFrame, text="Geometry Parameters")
self.tccw['B']['gpl'].grid(row=0, column=0, columnspan=2)
self.tccw['B']['GPFrame'] = tk.Frame(master=self.tcCondFrame)
self.tccw['B']['GPFrame'].grid(**self.GPFpos)
GPFB = self.tccw['B']['GPFrame'] #shorthand name
GPFB.CW = self._gen_entries2(GPFB, GPFED['B'], GPFsp['B'], dict()) # adds all relevant entries & labels
self.tccw['B']['fpl'] = tk.Label(master=self.tcCondFrame, text='Force Parameters')
self.tccw['B']['fpl'].grid(row=0, column=2, columnspan=2)
self.tccw['B']['fll'] = tk.Label(master=self.tcCondFrame, text='F(L)')
self.tccw['B']['fll'].grid(row=1, column=2)
self.tccw['B']['tsTypeMenu'] = tk.OptionMenu(self.tcCondFrame, self.tcFVar, *self.FLDict.keys())
self.tccw['B']['tsTypeMenu'].grid(row=1, column=3)
self.tccw['B']['cfBtn'] = tk.Button(master=self.tcCondFrame, text="Calculate Fit", command=self._calcfit)
self.tccw['B']['cfBtn'].grid(row=5, column=2, columnspan=2)
self.tccw['B']['FLFrame'] = self.tccw['A']['FLFrame'] # shared b/w A & B
# R(theta) and F(theta) *****************
self.tccw['C'] = dict() # R & F
self.tccw['C']['fpl'] = tk.Label(master=self.tcCondFrame, text="Force Parameters")
self.tccw['C']['fpl'].grid(row=0, column=0, columnspan=2)
self.tccw['C']['ftl'] = tk.Label(master=self.tcCondFrame, text="F(\u03B8)")
self.tccw['C']['ftl'].grid(row=1, column=0)
self.tccw['C']['ftTypeMenu'] = tk.OptionMenu(self.tcCondFrame, self.tcF2Var, *self.FThetaDict.keys())
self.tccw['C']['ftTypeMenu'].grid(row=1, column=1)
self.tccw['C']['FPFrame'] = tk.Frame(master=self.tcCondFrame)
self.tccw['C']['FPFrame'].grid(row=2, column=0, columnspan=2)
FTFC = self.tccw['C']['FPFrame']
FTFC.CW = self._parameter_entry_initializer(dict(), FTFC, self.FThetaDict, (0,0), maxcol=tpf_maxcol)
for w in FTFC.winfo_children():
w.grid_remove()
def FTchange(*args):
self._entry_change(self.tccw['C']['FPFrame'], self.tccw['C']['FPFrame'].CW, self.tcF2Var.get())
self.tcF2Var.trace_add("write", FTchange)
self.tccw['C']['rpl'] = tk.Label(master=self.tcCondFrame, text="Radius Parameters")
self.tccw['C']['rpl'].grid(row=0, column=2, columnspan=2)
self.tccw['C']['rtl'] = tk.Label(master=self.tcCondFrame, text="R(\u03B8)")
self.tccw['C']['rtl'].grid(row=1, column=2)
self.tccw['C']['rtTypeMenu'] = tk.OptionMenu(self.tcCondFrame, self.tcRVar, *self.RThetaDict.keys())
self.tccw['C']['rtTypeMenu'].grid(row=1, column=3)
self.tccw['C']['RPFrame'] = tk.Frame(master=self.tcCondFrame)
self.tccw['C']['RPFrame'].grid(row=2, column=2, columnspan=2)
RTFC = self.tccw['C']['RPFrame']
RTFC.CW = self._parameter_entry_initializer(dict(), RTFC, self.RThetaDict, (0,0), maxcol=tpf_maxcol)
for w in RTFC.winfo_children():
w.grid_remove()
def RTchange(*args):
self._entry_change(self.tccw['C']['RPFrame'], self.tccw['C']['RPFrame'].CW, self.tcRVar.get())
self.tcRVar.trace_add("write", RTchange)
self.tccw['C']['cfBtn'] = tk.Button(master=self.tcCondFrame, text="Calculate Fit", command=self._calcfit)
self.tccw['C']['cfBtn'].grid(row=5, column=2, columnspan=2)
# tau(theta) and r(theta) ********************
self.tccw['D'] = dict() # tau and R
self.tccw['D']['tpl'] = tk.Label(master=self.tcCondFrame, text="Torque Parameters")
self.tccw['D']['tpl'].grid(row=0, column=0, columnspan=2)
self.tccw['D']['ttl'] = tk.Label(master=self.tcCondFrame, text="\u03C4(\u03B8)")
self.tccw['D']['ttl'].grid(row=1, column=0)
self.tccw['D']['ttTypeMenu'] = tk.OptionMenu(self.tcCondFrame, self.tcTauVar, *self.TauThetaDict.keys())
self.tccw['D']['ttTypeMenu'].grid(row=1, column=1)
self.tccw['D']['TPFrame'] = tk.Frame(master=self.tcCondFrame)
self.tccw['D']['TPFrame'].grid(row=2, column=0, columnspan=2)
TTFD = self.tccw['D']['TPFrame']
TTFD.CW = self._parameter_entry_initializer(dict(), TTFD, self.TauThetaDict, (0, 0), maxcol=tpf_maxcol)
for w in TTFD.winfo_children():
w.grid_remove()
def TTchange(*args):
self._entry_change(self.tccw['D']['TPFrame'], self.tccw['D']['TPFrame'].CW, self.tcTauVar.get())
self.tcTauVar.trace_add("write", TTchange)
self.tccw['D']['rpl'] = self.tccw['C']['rpl']
self.tccw['D']['rtl'] = self.tccw['C']['rtl']
self.tccw['D']['rtTypeMenu'] = self.tccw['C']['rtTypeMenu']
self.tccw['D']['RPFrame'] = self.tccw['C']['RPFrame']
self.tccw['D']['cfBtn'] = self.tccw['C']['cfBtn']
self.tsTypeVar.set('Configuration A')
# TORQUE CONTROLS
# miscellaneous Vars
# for loading to go smoothly, these should all be defined at initialization
self.effxVar = tk.StringVar(root)
self.effxvals = ['time', 'theta', 'psi', 'projectile speed']
self.effxVar.set(self.effxvals[0])
self.effxVar.trace_add("write", self._change_p1)
def _save_design(self):
classes_to_save = ['Frame', 'Labelframe', 'Entry', 'Checkbutton', 'Menubutton']
blacklist = [self.plotFrame1, self.plotFrame2]
D = dict()
G = nx.DiGraph()
G.add_node(0)
G.nodes[0]['level'] = 0
G.nodes[0]['wname'] = self.back.winfo_name()
G.nodes[0]['widget'] = self.back # this gets wiped before saving, and regenerated on loading
G.nodes[0]['class'] = self.back.winfo_class()
def map_GUI(G, node):
current_widget = G.nodes[node]['widget']
if current_widget not in blacklist:
child_widgets = current_widget.winfo_children()
for cw in child_widgets:
add_widget(G, cw, node)
# ignore any blacklisted widgets, like the frames that control plots
return G
def add_widget(G, widget, parent_node):
C = widget.winfo_class()
if C in classes_to_save:
nnn = max(list(G.nodes)) + 1
G.add_node(nnn)
G.add_edge(parent_node, nnn)
G.nodes[nnn]['level'] = G.nodes[parent_node]['level'] + 1
G.nodes[nnn]['wname'] = widget.winfo_name()
G.nodes[nnn]['widget'] = widget
G.nodes[nnn]['class'] = C
if (C == 'Frame') or (C == 'Labelframe'):
G = map_GUI(G, nnn) # the map_GUI->add_widget->map_GUI cycle is what does the recursive
# mapping of the tree structure of the GUI
elif C == 'Entry':
G.nodes[nnn]['value'] = widget.get()
elif C == 'Checkbutton':
varname = widget.cget('variable')
G.nodes[nnn]['value'] = widget.getvar(varname)
# to set in Load, widget.setvar(varname, newvalue)
elif C == 'Menubutton':
# for item in widget.keys():
# print("item = ", item)
# print("widget.cget(item) = ", widget.cget(item))
# above code allows inspecting all the valid cget options
varname = widget.cget('textvariable')
G.nodes[nnn]['value'] = widget.getvar(varname)
# does nothing if the widget isn't a class to be saved
return G
# these two together should recursively map the entire family tree of self.back
G = map_GUI(G, 0)
for n in G.nodes:
G.nodes[n]['widget'] = False # can't pickle the widgets themselves, and they
# need to be reconstituted later anyway
default_dir = os.path.join(toplevel_path, "saved_designs")
f = tk.filedialog.asksaveasfile(mode='wb', defaultextension=".pkl", initialdir=default_dir)
if f is None:
return
else:
pickle.dump(G, f)
f.close()
def _load_design(self):
default_dir = os.path.join(toplevel_path, "saved_designs")
f = tk.filedialog.askopenfile(mode='rb', initialdir=default_dir)
if f is None:
return
G = pickle.load(f)
G.nodes[0]['widget'] = self.back
def repair_node(G, node, parent_node):
# reassign widgets to nodes, set entry values, and set variables associated with menus and checkboxes
parent_widget = G.nodes[parent_node]['widget']
try:
W = parent_widget.nametowidget(G.nodes[node]['wname'])
G.nodes[node]['widget'] = W
C = G.nodes[node]['class']
if C == 'Entry':
# varname = W.cget("variable")
W.delete(0, 'end')
W.insert(0, G.nodes[node]['value'])
# W.setvar(varname, G.nodes[node]['value'])
elif C == 'Checkbutton':
varname = W.cget('variable')
W.setvar(varname, G.nodes[node]['value'])
elif C == 'Menubutton':
varname = W.cget('textvariable')
W.setvar(varname, G.nodes[node]['value'])
except:
print('\n\nERROR ON LEVEL: ' + str(G.nodes[node]['level']))
pw_children = parent_widget.winfo_children()
pw_children_names = [pwc.winfo_name() for pwc in pw_children]
target_name = G.nodes[node]['wname']
print("target name: ", target_name)
print("available names: ", pw_children_names)
print("Note that these errors can sometimes be caused by trying to open files created on different machines due to the inherent brittleness of the save code")
print('--- END ERROR ---\n')
def repair_G(G):
# have to build the GUI state level by level
# reassign widgets from top down, completing level n entirely before moving to level n+1
# while doing this, set entries and associated variable values
# sort nodes into list of lists. Inner lists are all nodes on a level, outer list is levels
# outer_list[0] contains level 0 nodes, and so on
max_level = max([G.nodes[n]['level'] for n in G.nodes])
for level in range(1, max_level+1):
# don't want to include the root node, as it has no predecessors
Lnodes = [n for n in G.nodes if (G.nodes[n]['level'] == level)]
for n in Lnodes:
parent_node = list(G.predecessors(n))[0]
repair_node(G, n, parent_node)
repair_G(G)
try:
self._calcfit()
except:
pass
def show_error(self, *args):
err = traceback.format_exception(*args)
tk.messagebox.showerror('Exception',err)
def _update_simMsg(self, *args):
self.simMsgM.configure(text=self.msg_text.get())
def _quit(self):
self.master.quit() # stops mainloop
self.master.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
def _t0update(self, *args):
self.tccw['common']['\u03B8 maxE'].delete(0, "end")
self.tccw['common']['\u03B8 maxE'].insert(0, self.theta0E.get())
try:
self._calcfit()
except:
pass # might not currently have all the other necessary parameters specified
def _stopsolve(self):
# user got sick of waiting for the solver to finish
self.solve_proc.terminate()
self.solver_runningB.set(False)
def _solver_state_change(self, *args):
# callback for what it says on the lid
# *args is because tkinter passes some stuff by default
# no docs on what it is, but safely ignored
if self.solver_runningB.get():
# solver is running
self.run_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
else:
self.run_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
def _p1destroyer(self):
self.p1_conditional_controls.destroy()
self.p1_conditional_controls = tk.Frame(master=self.plotFrame1)
self.p1_conditional_controls.grid(row=2, column=0, columnspan=2)
def _change_p1(self, *args):
# all the "weird" plots that draw multiple lines or mess with the axes / figures
# as there is no need to save plots, the whole frame is destroyed whenever the plot type is changed
p1T = self.p1TypeVar.get()
if p1T == 'animation':
if self.prevP1Type == 'animation':
self._run_animation()
else:
self._conf_animation()
self._run_animation()
elif p1T == 'arm load':
self._p1destroyer()
self.fig1.clear()
axF = self.fig1.add_subplot(311)
axO = self.fig1.add_subplot(312)
axA = self.fig1.add_subplot(313)
if hasattr(self, "sol"):
if not self.sol is None:
group = (self.fig1, axF, axO, axA)
h = self.sol['H']
Y, Yd, t = (np.array(h['Y']), np.array(h['Yd']), np.array(h['t']))
CDTtools.dyn_core_tools.load_info(Y, Yd, t, self.dp, plot=True, group=group)
self.canvas1.draw()
def sas():
# def y_hist(y0, dt, dp, tp, psif, N=0, platform=True):
Npoints = int(self.p1_snpE.get())
D = CDTtools.dyn_core_tools.y_hist(self.y0, self.dt, self.dp, self.tp, self.sol['yf'][2],
N=Npoints, platform=self.p)
data = CDTtools.dyn_core_tools.load_info(D['ys'], D['yds'], D['ts'], self.dp, plot=False)
# {'Fx_tip': Ftx, 'Fy_tip': Fty, 'omega': omega, 'alpha': alpha}
data_arr = np.stack((D['ts'], data['Fx_tip'], data['Fy_tip'], data['omega'], data['alpha']), 1)
default_dir = os.path.join(toplevel_path, "saved_data")
fname = tk.filedialog.asksaveasfilename(initialdir=default_dir)
if fname is None:
return
np.savetxt(fname, data_arr)
# def load_info(Y, Yd, t, dp, plot=False, group=None):
tk.Label(master=self.p1_conditional_controls, text="Number of Points").grid(row=0, column=0)
self.p1_snpE = tk.Entry(master=self.p1_conditional_controls)
self.p1_snpE.grid(row=0, column=1)
tk.Button(master=self.p1_conditional_controls, text="Save Path", command=sas).grid(row=0, column=3)
elif p1T == 'projectile path':
def sp():
if hasattr(self, "sol"):
if not self.sol is None:
Y = np.array(self.sol['H']['Y'])
default_dir = os.path.join(toplevel_path, "saved_data")
fname = tk.filedialog.asksaveasfilename(initialdir=default_dir)
scale = float(pathSaveScaleE.get())
CDTtools.dyn_core_tools.projectile_path(Y, self.dp, plot=False, fname=fname,
scale_factor=scale, axPP=None)
self._p1destroyer()
tk.Button(master=self.p1_conditional_controls, text="Save Path", command=sp).grid(row=1, column=2)
tk.Label(master=self.p1_conditional_controls, text="scale factor").grid(row=1, column=0)
pathSaveScaleE = tk.Entry(master=self.p1_conditional_controls)
pathSaveScaleE.grid(row=1, column=1)
pathSaveScaleE.insert(0, "1")
self.fig1.clear()
# def projectile_path(Y, dp, plot=False, fname=None, scale_factor=1, axPP=None):
self.ax1 = self.fig1.add_subplot(111)
if hasattr(self, "sol"):
if not self.sol is None:
Y = np.array(self.sol['H']['Y'])
CDTtools.dyn_core_tools.projectile_path(Y, self.dp, plot=True, axPP=self.ax1)
self.canvas1.draw()
elif p1T == 'efficiency':
if self.prevP1Type != 'efficiency':
self._p1destroyer()
tk.Label(master=self.p1_conditional_controls, text="x axis").grid(row=0, column=0)
self.effxMenu = tk.OptionMenu(self.p1_conditional_controls, self.effxVar, *self.effxvals)
self.effxMenu.grid(row=0, column=1)
self.fig1.clear()
self.ax1 = self.fig1.add_subplot(111)
if hasattr(self, "sol"):
if not self.sol is None:
Y = np.array(self.sol['H']['Y'])
Yd = np.array(self.sol['H']['Yd'])
t = np.array(self.sol['H']['t'])
# def energy_plot(Y, Yd, t, dp, x='time', axE=None):
CDTtools.dyn_core_tools.energy_plot(Y, Yd, t, self.dp, x=self.effxVar.get(), axE=self.ax1)
self.canvas1.draw()
elif p1T == 'puller potential energy':
if self.prevP1Type != 'puller potential energy':
self._p1destroyer()
tk.Label(master=self.p1_conditional_controls, text="plot domain (theta)").grid(row=0, column=0)
self.ppe_tminE = tk.Entry(master=self.p1_conditional_controls)
self.ppe_tminE.grid(row=0, column=1)
self.ppe_tmaxE = tk.Entry(master=self.p1_conditional_controls)
self.ppe_tmaxE.grid(row=0, column=2)
self.ppe_plotB = tk.Button(master=self.p1_conditional_controls, text="Replot",
command=self._change_p1)
self.ppe_plotB.grid(row=0, column=3)
self.fig1.clear()
self.ax1 = self.fig1.add_subplot(111)
if hasattr(self, "tp"):
# def puller_potential_energy(tp, theta0, plot=False, plotdomain=None, axPPE=None):
tmin = self.ppe_tminE.get()
tmax = self.ppe_tmaxE.get()
theta0 = float(self.theta0E.get())
if (tmin != "") and (tmax != ""):
ppe = CDTtools.dyn_core_tools.puller_potential_energy(self.tp, theta0, plot=True,
plotdomain=(float(tmin), float(tmax)),
axPPE = self.ax1)
else:
ppe = CDTtools.dyn_core_tools.puller_potential_energy(self.tp, theta0, plot=True,
axPPE=self.ax1)
msg1 = "Puller potential energy: " + str(round(ppe, self.rp))
if hasattr(self, "vpf"):
Ekf = 0.5*float(self.dpFrame.CW['Projectile + Pouch MassE'].get())*(self.vpf)**2
msg2 = "\nOverall system efficiency: " + str(round(Ekf/ppe, self.rp))
else:
msg2 = ""
if self.prevP1Type == 'puller potential energy':
self.ppeMsg.configure(text=msg1+msg2)
else:
self.ppeMsg = tk.Message(master=self.p1_conditional_controls, text=msg1+msg2,
bg='white', width=256)
self.ppeMsg.grid(row=1, column=0, columnspan=3)
self.canvas1.draw()
elif p1T == 'axle reaction load':
# more or less the same as the projectile path - needs an option to save the load data
def sarl():
if hasattr(self, "sol"):
if not self.sol is None:
# def y_hist(y0, dt, dp, tp, psif, N=0, platform=True):
# return {'ts': sample_times, 'ys': ys, 'yds': yds}
Npoints = int(arlSaveNE.get())
D = CDTtools.dyn_core_tools.y_hist(self.y0, self.dt, self.dp, self.tp, self.sol['yf'][2],
N=Npoints, platform=self.p)
default_dir = os.path.join(toplevel_path, "saved_data")
fname = tk.filedialog.asksaveasfilename(initialdir=default_dir)
# axle_reaction_force(Y, Yd, dp, t, plot=False, fname=None, ax_arf=None)
CDTtools.dyn_core_tools.axle_reaction_force(D['ys'], D['yds'], self.dp,
D['ts'], fname=fname)
self._p1destroyer()
tk.Button(master=self.p1_conditional_controls,
text="Save Load Data", command=sarl).grid(row=0, column=2)
default_dir = os.path.join(toplevel_path, "saved_designs")
tk.Label(master=self.p1_conditional_controls, text="number of points").grid(row=0, column=0)
arlSaveNE = tk.Entry(master=self.p1_conditional_controls)
arlSaveNE.grid(row=0, column=1)
arlSaveNE.insert(0, "20")
self.fig1.clear()
self.ax1 = self.fig1.add_subplot(111)
if hasattr(self, "sol"):
if not self.sol is None:
Y = np.array(self.sol['H']['Y'])
Yd = np.array(self.sol['H']['Yd'])
t = np.array(self.sol['H']['t'])
CDTtools.dyn_core_tools.axle_reaction_force(Y, Yd, self.dp, t, plot=True, ax_arf=self.ax1)
self.canvas1.draw()
else:
msg = self.msg_text.get() + "Unimplemented plot 1 type: " + self.p1TypeVar.get()
self.msg_text.set(msg)
self.prevP1Type = p1T
def _calc_ax2_data(self, s, Y, Yd, t):
if s == 'time':
return t
elif s == 'theta':
return Y[:,0]
elif s == 'psi':
return Y[:,2]
elif s == 'projectile velocity':
vp = [CDTtools.dyn_core_tools.vp(Y[i,:], self.dp) for i in range(len(t))]
return np.array([np.linalg.norm(v) for v in vp])
elif s == 'sling tension':
return CDTtools.dyn_core_tools.sling_tension(Y, Yd, t, self.dp)
elif s == 'arm rotational speed':
return Y[:,1]
elif s == 'puller speed':
theta_hist = (np.array(self.sol['H']['Y']))[:, 0]
omega_hist = (np.array(self.sol['H']['Y']))[:, 1]
t_hist = np.array(self.sol['H']['t'])
R_hist = np.array([self.puller_geometry.R(x) for x in theta_hist])
Ldot = np.array([omega_hist[i] * R_hist[i] for i in range(len(t_hist))])
return Ldot
else:
self.msg_text.set(self.msg_text.get() + "Unimplemented axis option: " + s)
return None
def _change_p2(self, *args):
if hasattr(self, "sol"):
if not self.sol is None:
t = np.array(self.sol['H']['t'])
Y = np.array(self.sol['H']['Y'])
Yd = np.array(self.sol['H']['Yd'])
p2x = self.p2xVar.get()
p2y = self.p2yVar.get()
xdata = self._calc_ax2_data(p2x, Y, Yd, t)
ydata = self._calc_ax2_data(p2y, Y, Yd, t)
if (not xdata is None) and (not ydata is None):
self.ax2.clear()
self.ax2.plot(xdata, ydata)
self.ax2.set_xlabel(p2x)
self.ax2.set_ylabel(p2y)
self.canvas2.draw()
def _conf_animation(self, *args):
# def launch_animation(Y, dp, t, axLA=None, figLA=None):
self._p1destroyer()
self.play_button = tk.Button(master=self.p1_conditional_controls, text="PLAY",
command=self._run_animation)
self.play_button.grid(row=0, column=2)
tk.Label(master=self.p1_conditional_controls, text="Animation Length (s)").grid(row=0, column=0)
self.aniLengthE = tk.Entry(master=self.p1_conditional_controls, width=8)
self.aniLengthE.grid(row=0, column=1)
self.aniLengthE.insert(0, "2")
def _run_animation(self, *args):
# matplotlib FuncAnimation seems easy, but is more trouble than it's worth
# impossible to disable the play button while it's running, and doesn't work on all systems
self.play_button['state'] = 'disabled' # disable play button while animation is running
self.fig1.clear()
if hasattr(self, "sol"):
if not self.sol is None:
# need to clean up data so it's evenly spaced in time using hist
# this also determines how long the animation runs (framerate is constant 25fps)
fps = 20
# frame delay depends on rendering delay
# and is changed as the animation runs and the real system-specific delay is measured
target_animation_runtime = float(self.aniLengthE.get())
nframes = int(1 + fps*target_animation_runtime)
I = CDTtools.dyn_core_tools.y_hist(self.y0, self.dt, self.dp, self.tp,
self.sol['yf'][2], N=nframes, platform=self.p)
Y = I['ys']
t = I['ts']
Xp, Yp = CDTtools.dyn_core_tools.projectile_path(Y, self.dp, plot=False)
Xt = np.array([-self.dp.La*np.sin(Y[j, 0]) for j in range(len(t))])
Yt = np.array([self.dp.La*np.cos(Y[j, 0]) for j in range(len(t))])
xmin, xmax, ymin, ymax = (min(min(Xp), min(Xt)), max(max(Xp), max(Xt)), min(min(Yp), min(Yt)), max(max(Yp), max(Yt)))
# need all this so the frame size doesn't keep changing
xrange = xmax - xmin
yrange = ymax - ymin
self.ax1 = self.fig1.add_subplot(111)
self.ax1.set_autoscale_on(False)
self.ax1.set_xlim(xmin-(xrange/8), xmax+(xrange/10))
self.ax1.set_ylim(ymin-(yrange/10), ymax+(yrange/6))
self.ax1.set_aspect('equal')
self.ax1.grid()
lobj1 = self.ax1.plot([], [], 'o-', lw=3)[0]
lobj2 = self.ax1.plot([], [], lw=1)[0]
time_template = 'time = %.3fs'
time_text = self.ax1.text(0.05, 0.9, '', transform=self.ax1.transAxes)
artists = [lobj1, lobj2, time_text]
artists[0].set_data([], [])
artists[1].set_data([], [])
artists[2].set_text('')
for i in range(nframes):
t0 = time.time() # adaptively change the render delay as needed
mechx = [0, Xt[i], Xp[i]]
mechy = [0, Yt[i], Yp[i]]
pathx = Xp[:i+1]
pathy = Yp[:i+1]
artists[0].set_data(mechx, mechy)
artists[1].set_data(pathx, pathy)
artists[2].set_text(time_template % t[i])
self.canvas1.draw()
root.update() # absolutely critical, you only get the last frame otherwise
render_delay = time.time() - t0
frame_delay = (1./fps) - render_delay # time to wait before starting
# rendering of next frame
root.after(int(1000 * frame_delay)) # after works in ms
self.play_button['state'] = 'normal'
def _check_solveproc(self):
# as long as the solver is running, checks periodically to see if it's still alive
if not (self.solve_proc).is_alive():
self.solver_runningB.set(False)
with open('logfile', 'a') as f:
f.write('solve_proc is no longer alive, t = ' + str(time.time() - self.t0) + "\n")
self.cspcalls = 0 # reset for next simulation run
self._wrapup() # displays basic results from the simulation in simMsgM
elif (self.solve_proc).is_alive and (time.time() - self.t0) > float(self.timeoutE.get()):
# timeout exceeded, stop the run
with open('logfile', 'a') as f:
f.write('terminating solve_proc due to timeout, t = ' + str(time.time() - self.t0) + '\n')
self.solve_proc.terminate() # ran for too long
self.master.after(100, self._check_solveproc) # next call will find the process dead and return
else:
self.solver_runningB.set(True)
self.cspcalls += 1
with open('logfile', 'a') as f:
f.write(str(self.cspcalls) + ' calls after ' + str(time.time() - self.t0) + 's\n')
self.master.after(500, self._check_solveproc)
return 0
def _stchange(self, *args):
# called when simTypeVar changes to update conditional simulation controls
# needs to take three parameters, but I don't need them, and the docs don't say what they are...
for w in self.scCondFrame.winfo_children():
w.grid_remove() # removes all of scCondFrame's children
stv = self.simTypeVar.get()
for k in self.sccw[stv].keys():
self.sccw[stv][k].grid()
def funcDict(self, module):
M = [t for t in inspect.getmembers(module) if inspect.isfunction(t[1])]
# list of tuples of form: (namestr, callable)
return {t[0]: t[1] for t in M} # dictionary of {name: callable}
def _tcchange(self, *args):
tst = self.tsTypeVar.get()
for w in self.tcCondFrame.winfo_children():
if not w in self.tccw['common'].values():
w.grid_remove()
if tst == 'Configuration A':
k = 'A'
elif tst == 'Configuration B':
k = 'B'
elif tst == 'r(\u03B8) and F(\u03B8)':
k = 'C'
elif tst == '\u03C4(\u03B8) and r(\u03B8)':
k = 'D'
for w in self.tccw[k].keys():
self.tccw[k][w].grid()
def _gen_entries2(self, frame, en, sp, D, entrywidth=None, maxcol=6):
# uses existing dict, includes labels
for i in range(len(en)):
c = math.floor(i/maxcol)
r = i - (maxcol*c)
label_string = en[i] + 'L'
entry_string = en[i] + 'E'
D[label_string] = tk.Label(master=frame, text=en[i])
D[label_string].grid(row=sp[0]+r, column=sp[1]+(2*c))
D[entry_string] = tk.Entry(master=frame)
if not entrywidth is None:
D[entry_string].configure(width=entrywidth)
D[entry_string].grid(row=sp[0]+r, column=sp[1]+1+(2*c))
return D
def _parameter_entry_initializer(self, D, frame, funcDict, startpos, entrywidth=8, maxcol=6):
for funcname in funcDict.keys():
f = funcDict[funcname]
S = inspect.signature(f)
i = 0
SL = [s for s in S.parameters][1:] # L, theta, etc. should not be added
D_inner = dict()
D[funcname] = self._gen_entries2(frame, SL, startpos, D_inner, entrywidth=entrywidth, maxcol=maxcol)
return D
def _entry_change(self, frame, cwd, funcname):
# changes entries / labels in frame to those matching funcname, as defined in cwd
for w in frame.winfo_children():
w.grid_remove()
if funcname != '':