affanraz commited on
Commit
480c725
·
verified ·
1 Parent(s): da46b70

Create calculator.py

Browse files
Files changed (1) hide show
  1. calculator.py +144 -0
calculator.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # calculator.py
2
+ import numpy as np
3
+
4
+
5
+ def _safe_float(v, default=0.0):
6
+ try:
7
+ return float(v)
8
+ except Exception:
9
+ return default
10
+
11
+
12
+ # -----------------------------
13
+ # Simply supported: Point load
14
+ # -----------------------------
15
+ # L: beam length
16
+ # P: point load magnitude
17
+ # a: distance from left support to load
18
+ # x: array of positions where deflection is requested
19
+ #
20
+ # Formula (standard):
21
+ # b = L - a
22
+ # for 0 <= x <= a:
23
+ # y = P*b*x*(L**2 - b**2 - x**2) / (6*E*I*L)
24
+ # for a <= x <= L:
25
+ # y = P*a*(L-x)*(L**2 - a**2 - (L-x)**2) / (6*E*I*L)
26
+ #
27
+ def deflection_simply_supported_point_load(L, P, a, E, I, x):
28
+ L = _safe_float(L)
29
+ P = _safe_float(P)
30
+ a = _safe_float(a)
31
+ E = _safe_float(E)
32
+ I = _safe_float(I)
33
+ x = np.array(x, dtype=float)
34
+ b = L - a
35
+ y = np.zeros_like(x, dtype=float)
36
+
37
+ mask1 = x <= a
38
+ if np.any(mask1):
39
+ xx = x[mask1]
40
+ y[mask1] = P * b * xx * (L**2 - b**2 - xx**2) / (6.0 * E * I * L)
41
+
42
+ mask2 = x > a
43
+ if np.any(mask2):
44
+ xx = x[mask2]
45
+ y[mask2] = P * a * (L - xx) * (L**2 - a**2 - (L - xx) ** 2) / (6.0 * E * I * L)
46
+
47
+ # convention: downward deflection negative
48
+ return -y
49
+
50
+
51
+ # -----------------------------
52
+ # Simply supported: UDL (w = N/m)
53
+ # y(x) = (w * x * (L^3 - 2 L x^2 + x^3)) / (24 E I)
54
+ # -----------------------------
55
+ def deflection_simply_supported_udl(L, w, E, I, x):
56
+ L = _safe_float(L)
57
+ w = _safe_float(w)
58
+ E = _safe_float(E)
59
+ I = _safe_float(I)
60
+ x = np.array(x, dtype=float)
61
+
62
+ y = (w * x * (L**3 - 2.0 * L * x**2 + x**3)) / (24.0 * E * I)
63
+ return -y
64
+
65
+
66
+ # -----------------------------
67
+ # Cantilever: Point load at free end P
68
+ # y(x) = P * x^2 * (3L - x) / (6 E I)
69
+ # (x measured from fixed support at 0)
70
+ # -----------------------------
71
+ def deflection_cantilever_point_load(L, P, E, I, x):
72
+ L = _safe_float(L)
73
+ P = _safe_float(P)
74
+ E = _safe_float(E)
75
+ I = _safe_float(I)
76
+ x = np.array(x, dtype=float)
77
+
78
+ y = P * x**2 * (3.0 * L - x) / (6.0 * E * I)
79
+ return -y
80
+
81
+
82
+ # -----------------------------
83
+ # Cantilever: UDL over full length w
84
+ # y(x) = w * x^2 * (6 L^2 - 4 L x + x^2) / (24 E I)
85
+ # -----------------------------
86
+ def deflection_cantilever_udl(L, w, E, I, x):
87
+ L = _safe_float(L)
88
+ w = _safe_float(w)
89
+ E = _safe_float(E)
90
+ I = _safe_float(I)
91
+ x = np.array(x, dtype=float)
92
+
93
+ y = w * x**2 * (6.0 * L**2 - 4.0 * L * x + x**2) / (24.0 * E * I)
94
+ return -y
95
+
96
+
97
+ # -----------------------------
98
+ # Generate deflection curve helper
99
+ # -----------------------------
100
+ def generate_deflection_curve(beam_type, load_type, L, load_value, a, E, I, num=400):
101
+ """
102
+ beam_type: 'Simply Supported' or 'Cantilever'
103
+ load_type: 'Point Load' or 'Uniformly Distributed Load'
104
+ L: length (m)
105
+ load_value: P (N) for point load, w (N/m) for UDL
106
+ a: point load position (m) for simply supported; ignored for cantilever here
107
+ E, I: material & section properties
108
+ num: number of points across the beam
109
+ """
110
+ L = float(L)
111
+ E = float(E)
112
+ I = float(I)
113
+ x = np.linspace(0.0, L, int(num))
114
+
115
+ if beam_type == "Simply Supported":
116
+ if load_type == "Point Load":
117
+ if a is None:
118
+ a = L / 2.0
119
+ y = deflection_simply_supported_point_load(L, load_value, a, E, I, x)
120
+ else: # UDL
121
+ y = deflection_simply_supported_udl(L, load_value, E, I, x)
122
+
123
+ elif beam_type == "Cantilever":
124
+ if load_type == "Point Load":
125
+ # interpretation: point load at free end (common case)
126
+ y = deflection_cantilever_point_load(L, load_value, E, I, x)
127
+ else:
128
+ y = deflection_cantilever_udl(L, load_value, E, I, x)
129
+
130
+ else:
131
+ raise ValueError("Unsupported beam type")
132
+
133
+ return x, y
134
+
135
+
136
+ # quick demo if run directly
137
+ if __name__ == "__main__":
138
+ L = 2.0
139
+ P = 100.0
140
+ E = 2.1e11
141
+ I = 1e-6
142
+ x, y = generate_deflection_curve("Simply Supported", "Point Load", L, P, a=1.0, E=E, I=I)
143
+ print("sample x:", x[:5])
144
+ print("sample y:", y[:5])