-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beam_element_FEM_solver.py
582 lines (458 loc) · 20.9 KB
/
Beam_element_FEM_solver.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
from dearpygui.core import *
from dearpygui.simple import *
import numpy as np
from Table_API import *
from assembler_and_solver import *
import webbrowser
def run_checks_and_solve(sender, data):
close_popup("Confirmation Window")
clear_log(logger="log")
clear_table("Results table")
log_info("---- Solver initiated ----", logger="log")
log_info("Running data checks...", logger="log")
flag = 0
elements = get_value("Number of elements")
# -------Check material properties---------#
E_val = np.zeros(elements)
I_val = np.zeros(elements)
L_val = np.zeros(elements)
if get_value("same_mat"): # solve for uniform
try:
E_val = np.full(elements, float(get_value("##material_table_0_1")))
log_info("E values: OK", logger="log")
except:
log_error("Please enter a valid value for E in the Element No. 1 row.", logger="log")
flag = 1
try:
I_val = np.full(elements, float(get_value("##material_table_0_2")))
log_info("I values: OK", logger="log")
except:
log_error("Please enter a valid value for I in the Element No. 1 row.", logger="log")
flag = 1
try:
L_val = np.zeros(elements)
for i in range(elements):
L_val[i] = float(get_value(f"##material_table_{i}_3"))
log_info("L values: OK", logger="log")
except:
log_error("Please enter valid values for L.", logger="log")
flag = 1
else: # solve for non-uniform
try:
for i in range(elements):
E_val[i] = float(get_value(f"##material_table_{i}_1"))
log_info("E values: OK", logger="log")
except:
log_error("Please enter valid values for E.", logger="log")
flag = 1
try:
for i in range(elements):
I_val[i] = float(get_value(f"##material_table_{i}_2"))
log_info("I values: OK", logger="log")
except:
log_error("Please enter valid values for I.", logger="log")
flag = 1
try:
for i in range(elements):
L_val[i] = float(get_value(f"##material_table_{i}_3"))
log_info("L values: OK", logger="log")
except:
log_error("Please enter valid values for L.", logger="log")
flag = 1
# -------------------------------------------------#
# ----------Check displacement constraints---------#
Q_val = np.zeros((elements + 1, 2))
for i in range(elements + 1):
d = get_value(f"##displacement_table_{i}_{1}")
if not d:
Q_val[i][0] = 1
r = get_value(f"##displacement_table_{i}_{2}")
if not r:
Q_val[i][1] = 1
if np.all(Q_val == 1):
log_error("No displacement constraints found. Please select at least one.", logger="log")
flag = 1
else:
log_info("Displacement constraints: OK", logger="log")
# -------------------------------------------#
# --------Check UDL Force----------------------#
UDL_val = np.zeros(elements)
try:
for i in range(elements):
if get_value(f"##UDL_table_{i}_1") == "":
continue
else:
UDL_val[i] = float(get_value(f"##UDL_table_{i}_1"))
log_info("UDL values: OK", logger="log")
except:
log_error("Please enter valid values for UDL.", logger="log")
flag = 1
# --------------------------------------------#
# --------Check UVL Force----------------------#
UVL_val = np.zeros(elements * 2)
try:
for i in range(elements * 2):
if get_value(f"##UVL_table_{i}_2") == "":
continue
else:
UVL_val[i] = float(get_value(f"##UVL_table_{i}_2"))
log_info("UVL values: OK", logger="log")
except:
log_error("Please enter valid values for UVL.", logger="log")
flag = 1
# --------------------------------------------#
# --------Check Point Force----------------------#
point_force_val = np.zeros(elements + 1)
try:
for i in range(elements + 1):
if get_value(f"##point_load_table_{i}_1") == "":
continue
else:
point_force_val[i] = float(get_value(f"##point_load_table_{i}_1"))
log_info("Point Force values: OK", logger="log")
except:
log_error("Please enter valid values for Point Forces.", logger="log")
flag = 1
# --------------------------------------------#
# --------Check Moments----------------------#
moment_val = np.zeros(elements + 1)
try:
for i in range(elements + 1):
if get_value(f"##moment_table_{i}_1") == "":
continue
else:
moment_val[i] = float(get_value(f"##moment_table_{i}_1"))
log_info("Moment values: OK", logger="log")
except:
log_error("Please enter valid values for Moments.", logger="log")
flag = 1
# --------------------------------------------#
if flag == 1:
log_info("--- Solver terminated without a solution. Please resolve all errors. ---", logger="log")
return 0
else:
log_info("All values have been checked and are valid.", logger="log")
log_info("Solving...", logger="log")
F_val, M_val = udl_vdl_point_force_solver(UDL_val, UVL_val, point_force_val, moment_val, L_val)
element_data_val = np.zeros((elements, 4))
temp_val = 1
col_temp = 0
for i in range(elements):
for j in range(temp_val, temp_val + 4, 1):
element_data_val[i][col_temp] = j
col_temp += 1
temp_val += 2
col_temp = 0
del temp_val, col_temp
element_data_val = element_data_val.astype(int)
try:
F_M_result, Q_result = solve(element_data_val, E_val, I_val, L_val, Q_val, F_val, M_val)
node = 1
for i in range(0, len(F_M_result), 2):
add_row("Results table",
[str(node), f"{F_M_result[i][0]:.5}", f"{F_M_result[i + 1][0]:.5}", f"{Q_result[i][0]:.5}",
f"{Q_result[i + 1][0]:.5}"])
node += 1
del node
log_info("Solution has been calculated!", logger="log")
log_info("---- Solver terminated ----", logger="log")
except:
log_error("An exception has occurred while solving. Please check the values entered and try again.",
logger="log")
log_info("--- Solver terminated without a solution. ---", logger="log")
def generate_tables(sender, data):
if get_value("Number of elements") < 1:
log_warning("Number of elements cannot be less than 1!", logger="log")
configure_item("Solve!", enabled=False, tip="Number of elements cannot be less than 1.")
configure_item("Confirmation Window", show=False)
else:
configure_item("Solve!", enabled=True, tip="")
configure_item("Confirmation Window", show=True)
add_material_table()
add_displacement_table()
add_UDL_table()
add_UVL_table()
add_point_load_table()
add_moment_table()
def add_material_table():
if does_item_exist("2. Material properties"):
delete_item("2. Material properties")
with window("2. Material properties", x_pos=10, y_pos=90, no_resize=True, no_move=True, no_collapse=True,
no_close=True, width=450, height=237):
add_spacing(count=2)
add_checkbox("same_mat", label=" Material is uniform throughout",
tip="If the material is uniform throughout, entering the\nE and I values only in the Element "
"No. 1 row is enough.")
add_spacing(count=2)
mat_table = SmartTable("material_table")
mat_table.add_header(
["Element No.", "Young's modulus (E)", "Area moment of inertia (I)", "Length of element (L)"])
for i in range(get_value("Number of elements")):
mat_table.add_row([str(i + 1), "", "", ""])
def add_displacement_table():
if does_item_exist("Displacement constraints"):
delete_item("Displacement constraints")
add_tab("Displacement constraints", parent="BC Data")
add_spacing(count=2)
disp_table = SmartTable("displacement_table")
disp_table.add_header(["Node No.", "Displacement Fixed", "Rotation Fixed"])
for i in range(get_value("Number of elements") + 1):
disp_table.add_row([str(i + 1), "C", ""])
def add_UDL_table():
if does_item_exist("UDL"):
delete_item("UDL")
add_tab("UDL", parent="BC Data", tip="Add uniformly distributed loads on elements")
add_spacing(count=2)
udl_table = SmartTable("UDL_table")
udl_table.add_header(["Element No.", "UDL Pressure value"])
for i in range(get_value("Number of elements")):
udl_table.add_row([str(i + 1), ""])
def add_UVL_table():
if does_item_exist("UVL"):
delete_item("UVL")
add_tab("UVL", parent="BC Data", tip="Add uniformly varying loads.")
add_spacing(count=2)
uvl_table = SmartTable("UVL_table")
uvl_table.add_header(["Element No.", "Node No.", "UVL Pressure value"])
for i in range(get_value("Number of elements")):
uvl_table.add_row([str(i + 1), "flag_1", str(i + 1)])
uvl_table.add_row([str(i + 1), "flag_1", str(i + 2)])
def add_point_load_table():
if does_item_exist("Point loads"):
delete_item("Point loads")
add_tab("Point loads", parent="BC Data")
add_spacing(count=2)
point_table = SmartTable("point_load_table")
point_table.add_header(["Node No.", "Point force value"])
for i in range(get_value("Number of elements") + 1):
point_table.add_row([str(i + 1), ""])
def add_moment_table():
if does_item_exist("Moments"):
delete_item("Moments")
add_tab("Moments", parent="BC Data")
add_spacing(count=2)
moment_table = SmartTable("moment_table")
moment_table.add_header(["Node No.", "Moment value"])
for i in range(get_value("Number of elements") + 1):
moment_table.add_row([str(i + 1), ""])
def close_confirmation(sender, data):
close_popup("Confirmation Window")
def close_info_window(sender, data):
if sender == "OK":
close_popup("Information window")
else:
close_popup("Information window")
webbrowser.open("https://github.com/RahulShagri/1D-Beam-Element-2-DOF-FEM-Solver")
def switch_theme(sender, data):
if sender == "dark_mode":
delete_item("dark_mode")
add_image_button("light_mode", "icons/light_mode.png", width=23, height=23, tip="Light mode", parent="Extras",
callback=switch_theme)
set_theme("Grey")
set_main_window_title("1D Beam Element 2 DOF FEM Solver")
set_main_window_pos(x=0, y=0)
set_main_window_size(width=1300, height=740)
set_main_window_resizable(False)
set_style_window_padding(4.00, 4.00)
set_style_frame_padding(6.00, 4.00)
set_style_item_spacing(6.00, 2.00)
set_style_item_inner_spacing(4.00, 4.00)
set_style_touch_extra_padding(0.00, 0.00)
set_style_indent_spacing(21.00)
set_style_scrollbar_size(12.00)
set_style_grab_min_size(10.00)
set_style_window_border_size(1.00)
set_style_child_border_size(1.00)
set_style_popup_border_size(1.00)
set_style_frame_border_size(0.00)
set_style_tab_border_size(0.00)
set_style_window_rounding(4.00)
set_style_child_rounding(4.00)
set_style_frame_rounding(4.00)
set_style_popup_rounding(4.00)
set_style_scrollbar_rounding(4.00)
set_style_grab_rounding(4.00)
set_style_tab_rounding(4.00)
set_style_window_title_align(0.50, 0.50)
set_style_window_menu_button_position(mvDir_Left)
set_style_color_button_position(mvDir_Right)
set_style_button_text_align(0.50, 0.50)
set_style_selectable_text_align(0.00, 0.00)
set_style_display_safe_area_padding(3.00, 3.00)
set_style_global_alpha(1.00)
set_style_antialiased_lines(True)
set_style_antialiased_fill(True)
set_style_curve_tessellation_tolerance(1.25)
set_style_circle_segment_max_error(1.60)
else:
delete_item("light_mode")
add_image_button("dark_mode", "icons/dark_mode.png", width=23, height=23, tip="Dark mode", parent="Extras",
callback=switch_theme)
set_theme("Light")
set_main_window_title("1D Beam Element 2 DOF FEM Solver")
set_main_window_pos(x=0, y=0)
set_main_window_size(width=1300, height=740)
set_main_window_resizable(False)
set_style_window_padding(4.00, 4.00)
set_style_frame_padding(6.00, 4.00)
set_style_item_spacing(6.00, 2.00)
set_style_item_inner_spacing(4.00, 4.00)
set_style_touch_extra_padding(0.00, 0.00)
set_style_indent_spacing(21.00)
set_style_scrollbar_size(12.00)
set_style_grab_min_size(10.00)
set_style_window_border_size(1.00)
set_style_child_border_size(1.00)
set_style_popup_border_size(1.00)
set_style_frame_border_size(0.00)
set_style_tab_border_size(0.00)
set_style_window_rounding(4.00)
set_style_child_rounding(4.00)
set_style_frame_rounding(4.00)
set_style_popup_rounding(4.00)
set_style_scrollbar_rounding(4.00)
set_style_grab_rounding(4.00)
set_style_tab_rounding(4.00)
set_style_window_title_align(0.50, 0.50)
set_style_window_menu_button_position(mvDir_Left)
set_style_color_button_position(mvDir_Right)
set_style_button_text_align(0.50, 0.50)
set_style_selectable_text_align(0.00, 0.00)
set_style_display_safe_area_padding(3.00, 3.00)
set_style_global_alpha(1.00)
set_style_antialiased_lines(True)
set_style_antialiased_fill(True)
set_style_curve_tessellation_tolerance(1.25)
set_style_circle_segment_max_error(1.60)
with window("Main"):
set_theme("Light")
set_main_window_title("1D Beam Element 2 DOF FEM Solver")
set_main_window_pos(x=0, y=0)
set_main_window_size(width=1300, height=740)
set_main_window_resizable(False)
set_style_window_padding(4.00, 4.00)
set_style_frame_padding(6.00, 4.00)
set_style_item_spacing(6.00, 2.00)
set_style_item_inner_spacing(4.00, 4.00)
set_style_touch_extra_padding(0.00, 0.00)
set_style_indent_spacing(21.00)
set_style_scrollbar_size(12.00)
set_style_grab_min_size(10.00)
set_style_window_border_size(1.00)
set_style_child_border_size(1.00)
set_style_popup_border_size(1.00)
set_style_frame_border_size(0.00)
set_style_tab_border_size(0.00)
set_style_window_rounding(4.00)
set_style_child_rounding(4.00)
set_style_frame_rounding(4.00)
set_style_popup_rounding(4.00)
set_style_scrollbar_rounding(4.00)
set_style_grab_rounding(4.00)
set_style_tab_rounding(4.00)
set_style_window_title_align(0.50, 0.50)
set_style_window_menu_button_position(mvDir_Left)
set_style_color_button_position(mvDir_Right)
set_style_button_text_align(0.50, 0.50)
set_style_selectable_text_align(0.00, 0.00)
set_style_display_safe_area_padding(3.00, 3.00)
set_style_global_alpha(1.00)
set_style_antialiased_lines(True)
set_style_antialiased_fill(True)
set_style_curve_tessellation_tolerance(1.25)
set_style_circle_segment_max_error(1.60)
with window("1. Discretization", x_pos=10, y_pos=10, no_resize=True, no_move=True, no_collapse=True, no_close=True,
width=450, height=70):
add_spacing(count=2)
add_input_int("Number of elements", default_value=1, callback=generate_tables,
tip="Number of elements should always be greater than or equal to 1.")
with window("2. Material properties", x_pos=10, y_pos=90, no_resize=True, no_move=True, no_collapse=True, no_close=True,
width=450, height=237):
add_spacing(count=2)
add_checkbox("same_mat", label=" Material is uniform throughout",
tip="If the material is uniform throughout, entering the\nE and I values only in the Element No. 1 "
"row is enough.")
add_spacing(count=2)
mat_table = SmartTable("material_table")
mat_table.add_header(["Element No.", "Young's modulus (E)", "Area moment of inertia (I)", "Length of element (L)"])
for i in range(get_value("Number of elements")):
mat_table.add_row([str(i + 1), "", "", ""])
with window("3. Boundary conditions", x_pos=10, y_pos=337, no_resize=True, no_move=True, no_collapse=True,
no_close=True, width=450, height=237):
add_spacing(count=2)
add_tab_bar("BC Data")
add_tab("Displacement constraints", parent="BC Data")
add_spacing(count=2)
disp_table = SmartTable("displacement_table")
disp_table.add_header(["Node No.", "Displacement Fixed", "Rotation Fixed"])
for i in range(get_value("Number of elements") + 1):
disp_table.add_row([str(i + 1), "C", ""])
add_tab("UDL", parent="BC Data", tip="Add uniformly distributed loads.")
add_spacing(count=2)
udl_table = SmartTable("UDL_table")
udl_table.add_header(["Element No.", "UDL Pressure value"])
for i in range(get_value("Number of elements")):
udl_table.add_row([str(i + 1), ""])
add_tab("UVL", parent="BC Data", tip="Add uniformly varying loads.")
add_spacing(count=2)
uvl_table = SmartTable("UVL_table")
uvl_table.add_header(["Element No.", "Node No.", "UVL Pressure value"])
for i in range(get_value("Number of elements")):
uvl_table.add_row([str(i + 1), "flag_1", str(i + 1)])
uvl_table.add_row([str(i + 1), "flag_1", str(i + 2)])
add_tab("Point loads", parent="BC Data")
add_spacing(count=2)
point_table = SmartTable("point_load_table")
point_table.add_header(["Node No.", "Point force value"])
for i in range(get_value("Number of elements") + 1):
point_table.add_row([str(i + 1), ""])
add_tab("Moments", parent="BC Data")
add_spacing(count=2)
moment_table = SmartTable("moment_table")
moment_table.add_header(["Node No.", "Moment value"])
for i in range(get_value("Number of elements") + 1):
moment_table.add_row([str(i + 1), ""])
with window("Diagram", x_pos=10, y_pos=584, no_resize=True, no_move=True, no_collapse=True, no_close=True, width=450,
height=46, no_title_bar=True):
add_button("Generate diagram", width=442, height=36, tip="Feature still in development.", enabled=False)
with window("Solve", x_pos=10, y_pos=640, no_resize=True, no_move=True, no_collapse=True, no_close=True, width=450,
height=46, no_title_bar=True):
add_button("Solve!", width=442, height=36)
add_popup("Solve!", 'Confirmation Window', modal=True, mousebutton=mvMouseButton_Left)
add_text("Are you sure you want to solve?")
add_button("Yes", width=150, callback=run_checks_and_solve)
add_same_line(spacing=10)
add_button("No", width=150, callback=close_confirmation)
with window("Results", x_pos=470, y_pos=10, no_resize=True, no_move=True, no_collapse=True, no_close=True, width=755,
height=361):
add_table("Results table",
["Node No.", "Equivalent\nForce", "Equivalent\nMoment", "Displacement", "Rotation\n(radians)"], width=740,
height=212)
with window("Logger", x_pos=470, y_pos=381, no_resize=True, no_move=True, no_collapse=True, no_close=True, width=805,
height=305):
add_logger("log", log_level=0, auto_scroll=True)
log("Welcome to the 1D beam element 2 DOF FEM Solver!", logger="log")
with window("Extras", x_pos=1230, y_pos=10, no_resize=True, no_move=True, no_collapse=True, no_close=True, width=43,
height=80, no_title_bar=True):
add_image_button("Help", "icons/help.png", width=23, height=23)
add_spacing()
add_separator()
add_spacing()
add_image_button("dark_mode", "icons/dark_mode.png", width=23, height=23, tip="Dark mode", callback=switch_theme)
add_popup("Help", "Information window", modal=True, mousebutton=mvMouseButton_Left)
add_spacing(count=5)
add_text(" -> All forces acting downwards are -ve")
add_spacing(count=2)
add_text(" -> All forces acting upwards are +ve.")
add_spacing(count=2)
add_separator()
add_spacing(count=2)
add_text(" -> All moments acting clockwise are -ve")
add_spacing(count=2)
add_text(" -> All moments acting anti-clockwise are +ve.")
add_spacing(count=5)
add_button("OK", width=100, callback=close_info_window)
add_same_line(spacing=5)
add_button("Get more info on GitHub", width=250, callback=close_info_window)
add_spacing(count=2)
start_dearpygui(primary_window="Main")