Kyo-Kai commited on
Commit
4da0772
·
verified ·
1 Parent(s): 4497214

Upload 2 files

Browse files
Files changed (2) hide show
  1. constraints.py +32 -0
  2. frame_helpers.py +345 -0
constraints.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Frame constants
2
+ N_columns = 2
3
+ N_floors = 5
4
+ N_nod_tot = (N_floors + 1) * N_columns
5
+ N_par_nod = 3
6
+ N_par_tot = N_nod_tot * N_par_nod
7
+ N_ele_tot = N_floors * (2 * N_columns - 1)
8
+ N_nod_ele = 2
9
+ N_par_ele = N_par_nod * N_nod_ele
10
+ N_tot_bound = 6
11
+ N_plots = 3
12
+
13
+ # Number of points for plotting
14
+ N_discritizations = 10
15
+
16
+ # Distance between nodes in meters
17
+ X_dist = 4
18
+ Y_dist = 3
19
+
20
+ # Columns 40x40 and beams 30x35 in cm
21
+ width_beam = 0.3
22
+ height_beam = 0.35
23
+ width_column = 0.4
24
+ height_column = 0.4
25
+
26
+ # Horizontal load on columns and angle in kN and degrees
27
+ po = 100
28
+ theta = 0
29
+
30
+ # Unit weight and elastic modulus in kN/m^3 and kN/m^2
31
+ unit_weight = 78.5
32
+ elastic_mod = 21*10**7
frame_helpers.py ADDED
@@ -0,0 +1,345 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import sympy as sp
3
+ from sympy import Matrix, lambdify
4
+
5
+
6
+ def calculate_X_positions(indices, N_columns, X_dist):
7
+ """
8
+ Calculate the X positions of the nodes.
9
+
10
+ Args:
11
+ indices (list): List or numpy array of indices from 1 to N_nod_tot
12
+ N_columns (int): Number of columns
13
+ X_dist (int): Distance between columns
14
+
15
+ Returns:
16
+ List of X positions of the nodes
17
+ """
18
+ X = np.zeros(len(indices))
19
+ X = ((indices % N_columns) - 1) * X_dist
20
+ np.putmask(X, X < 0, (N_columns - 1) * X_dist)
21
+ return X
22
+
23
+
24
+ def calculate_Y_positions(indices, N_columns, Y_dist):
25
+ """
26
+ Calculate the Y positions of the nodes.
27
+
28
+ Args:
29
+ indices (list): List or numpy array of indices from 1 to N_nod_tot
30
+ N_columns (int): Number of columns
31
+ Y_dist (int): Distance between columns
32
+
33
+ Returns:
34
+ List of Y positions of the nodes
35
+ """
36
+ Y = np.zeros(len(indices))
37
+ h_assigner = np.ceil(indices / N_columns - 1)
38
+ h_assigner[h_assigner < 1] = 0
39
+ Y = Y_dist * h_assigner
40
+ return Y
41
+
42
+
43
+ def calculate_element_node_indices(N_floors, N_columns):
44
+ """
45
+ Calculate the element node indices.
46
+
47
+ Args:
48
+ N_floors (int): Number of floors in the frame
49
+ N_columns (int): Number of columns
50
+
51
+ Returns:
52
+ Numpy array of element node indices
53
+ """
54
+ # Initialize the ele_nod array
55
+
56
+ ele_nod = np.zeros((N_floors * (2 * N_columns - 1), 2), dtype=int)
57
+
58
+ # Calculate the indices for the vertical and horizontal elements
59
+ for i in range(1, N_floors + 1):
60
+ for j in range(1, N_columns + 1):
61
+ index = (i - 1) * (2 * N_columns - 1) + j - 1
62
+ ele_nod[index, 0] = j + (i - 1) * N_columns
63
+ ele_nod[index, 1] = ele_nod[index, 0] + N_columns
64
+ for j in range(1, N_columns):
65
+ index = (i - 1) * (2 * N_columns - 1) + N_columns + j - 1
66
+ ele_nod[index, 0] = i * N_columns + j
67
+ ele_nod[index, 1] = ele_nod[index, 0] + 1
68
+
69
+ return ele_nod
70
+
71
+
72
+ def calculate_element_length(N_ele_tot, N_columns, X_dist, Y_dist):
73
+ """
74
+ Calculate the length of the elements.
75
+
76
+ Args:
77
+ N_ele_tot (int): Number of elements in the frame
78
+ N_columns (int): Number of columns
79
+ X_dist (int): Distance between columns
80
+ Y_dist (int): Height of the columns
81
+
82
+ Returns:
83
+ Numpy array of element lengths
84
+ """
85
+
86
+ h = np.zeros(N_ele_tot)
87
+
88
+ # Calculate h, length of the elements
89
+ for i in range(1, N_ele_tot+1):
90
+ if i % (N_columns+1) != 0:
91
+ h[i-1] = Y_dist
92
+ else:
93
+ h[i-1] = X_dist
94
+
95
+ return h
96
+
97
+
98
+ #### Heavy functions
99
+ def initialize_symbols(N_par_ele):
100
+ """
101
+ Create and return the symbolic variables used in the calculations.
102
+
103
+ Args:
104
+ N_par_ele (int): Number of parameter per element
105
+
106
+ Returns:
107
+ Returns a tuple of the symbolic variables
108
+ """
109
+ # Define symbolic variables
110
+ x, h_e, beta_e, beta_curr = sp.symbols('x h_e beta_e beta_curr')
111
+ qe = sp.Array([sp.Symbol(f'q{i}') for i in range(1, N_par_ele+1)])
112
+ a0, a1, c0, c1, c2, c3 = sp.symbols('a0 a1 c0 c1 c2 c3')
113
+ A_e, E_e, J_e, ro_e, T, fo_E = sp.symbols('A_e E_e J_e ro_e T fo_E')
114
+ Qglo_pel_curr1_mode, Qglo_pel_curr2_mode, Qglo_pel_curr3_mode, Qglo_pel_curr4_mode, Qglo_pel_curr5_mode, Qglo_pel_curr6_mode= sp.symbols('Qglo_pel_curr1_mode Qglo_pel_curr2_mode Qglo_pel_curr3_mode Qglo_pel_curr4_mode Qglo_pel_curr5_mode Qglo_pel_curr6_mode')
115
+ X_old, Y_old = sp.symbols('X_old Y_old')
116
+
117
+ return (x, h_e, beta_e, beta_curr, qe, a0, a1, c0, c1, c2, c3, A_e, E_e, J_e, ro_e,
118
+ T, fo_E, Qglo_pel_curr1_mode, Qglo_pel_curr2_mode, Qglo_pel_curr3_mode,
119
+ Qglo_pel_curr4_mode, Qglo_pel_curr5_mode, Qglo_pel_curr6_mode, X_old, Y_old)
120
+
121
+
122
+ def calculate_energies(x, qe, h_e, beta_e, E_e, J_e, A_e, ro_e, ve_beam, ue_beam):
123
+ """
124
+ Creates the symbolic expressions for the potential and kinetic energies of the beam.
125
+ """
126
+
127
+ # Calculate chi_beam and eps_beam
128
+ chi_beam = sp.diff(sp.diff(ve_beam, x), x)
129
+ eps_beam = sp.diff(ue_beam, x)
130
+
131
+ # Calculate potential energy (Pot_beam) and kinetic energy (Kin_beam)
132
+ Pot_beam = 1 / 2 * sp.integrate(E_e * J_e * chi_beam**2 + E_e * A_e * eps_beam**2, (x, 0, h_e))
133
+ Kin_beam = 1 / 2 * ro_e * A_e * sp.integrate(ve_beam**2 + ue_beam**2, (x, 0, h_e))
134
+
135
+ return Pot_beam, Kin_beam, chi_beam, eps_beam
136
+
137
+
138
+ def calculate_beam_displacement_equations(x, h_e, beta_e, qe, a0, a1, c0, c1, c2, c3):
139
+ """
140
+ Calculate the beam displacement equations.
141
+
142
+ Returns:
143
+ Displacement functions for the beam in the u and v directions
144
+ """
145
+ # Compute v1, u1, v2, u2
146
+ v1 = -qe[0] * sp.sin(beta_e) + qe[1] * sp.cos(beta_e)
147
+ u1 = qe[0] * sp.cos(beta_e) + qe[1] * sp.sin(beta_e)
148
+ v2 = -qe[3] * sp.sin(beta_e) + qe[4] * sp.cos(beta_e)
149
+ u2 = qe[3] * sp.cos(beta_e) + qe[4] * sp.sin(beta_e)
150
+
151
+ # Define beam displacement equations
152
+ u_beam = a0 + a1 * x
153
+ v_beam = c0 + c1 * x + c2 * x**2 + c3 * x**3
154
+
155
+ # Define equilibrium equations
156
+ equations = [
157
+ v_beam.subs(x, 0) - v1,
158
+ sp.diff(v_beam, x).subs(x, 0) - qe[2],
159
+ v_beam.subs(x, h_e) - v2,
160
+ sp.diff(v_beam, x).subs(x, h_e) - qe[5],
161
+ u_beam.subs(x, 0) - u1,
162
+ u_beam.subs(x, h_e) - u2
163
+ ]
164
+
165
+ # Solve and assign the solution
166
+ sol = sp.solve(equations, (c0, c1, c2, c3, a0, a1))
167
+ ve_beam = v_beam.subs(sol)
168
+ ue_beam = u_beam.subs(sol)
169
+
170
+ # Lambdify ve_beam and ue_beam
171
+ ve_beam_func = lambdify((x, qe, h_e, beta_e), ve_beam, "numpy")
172
+ ue_beam_func = lambdify((x, qe, h_e, beta_e), ue_beam, "numpy")
173
+
174
+ return ve_beam_func, ue_beam_func, ve_beam, ue_beam
175
+
176
+
177
+ def assemble_global_matrices(N_par_ele, N_par_tot, N_ele_tot, Pot_beam, Kin_beam, qe, h, A, E, J, beta, ro, pel, h_e, A_e, E_e, J_e, beta_e, ro_e, x):
178
+ """
179
+ Assemble the global stiffness and mass matrices by assembling the
180
+ symbolic element stiffness and mass matrices and converting them to
181
+ numeric arrays.
182
+
183
+ Returns:
184
+ Numeric arrays of the global stiffness and mass matrices
185
+ """
186
+ K_beam = np.zeros((N_par_ele, N_par_ele), dtype=object)
187
+ M_beam = np.zeros((N_par_ele, N_par_ele), dtype=object)
188
+
189
+ # Compute K_beam and M_beam
190
+ for i in range(N_par_ele):
191
+ for j in range(N_par_ele):
192
+ K_beam[i][j] = sp.lambdify((x, h_e, A_e, E_e, J_e, beta_e, ro_e),
193
+ sp.diff(sp.diff(Pot_beam, qe[i]), qe[j]), 'numpy')
194
+ M_beam[i][j] = sp.lambdify((x, h_e, A_e, E_e, J_e, beta_e, ro_e),
195
+ sp.diff(sp.diff(Kin_beam, qe[i]), qe[j]), 'numpy')
196
+
197
+ # Initialize element stiffness matrix (Ke) and global stiffness matrix (K)
198
+ K = np.zeros((N_par_tot, N_par_tot))
199
+ M = np.zeros((N_par_tot, N_par_tot))
200
+
201
+ # Compute Ke, Me and assemble K, M using NumPy operations
202
+ for e in range(N_ele_tot):
203
+ for i in range(N_par_ele):
204
+ for j in range(N_par_ele):
205
+ K[pel[e, i]-1, pel[e, j]-1] += K_beam[i, j](0, h[e], A[e], E[e], J[e], beta[e], ro[e])
206
+ M[pel[e, i]-1, pel[e, j]-1] += M_beam[i, j](0, h[e], A[e], E[e], J[e], beta[e], ro[e])
207
+
208
+ return K, M
209
+
210
+
211
+ def apply_boundary_conditions(N_par_tot, N_nod_tot, N_par_nod, w, K, M):
212
+ """
213
+ Applies the boundary conditions to the stiffness and mass matrices.
214
+
215
+ Returns:
216
+ Numpy arrays of the stiffness and mass matrices with the boundary conditions applied
217
+ """
218
+ mask = w == 1
219
+
220
+ K[mask, :] = 0
221
+ K[:, mask] = 0
222
+ M[mask, :] = 0
223
+ M[:, mask] = 0
224
+
225
+ # Set the diagonal elements where W is 1 to 1 or 1e-30
226
+ np.fill_diagonal(K, np.where(mask, 1, K.diagonal()))
227
+ np.fill_diagonal(M, np.where(mask, 1e-30, M.diagonal()))
228
+
229
+ return K, M
230
+
231
+
232
+ def compute_eigenvalues_and_eigenvectors(K, M):
233
+ """
234
+ Compute the eigenvalues and eigenvectors of the stiffness and mass matrices.
235
+
236
+
237
+ Returns:
238
+ The real part of the eigenvalues (frequency) and the normalized eigenvectors (modes of vibration)
239
+ """
240
+ # Compute eigenvalues (lamb) and eigenvectors (phis)
241
+ lamb, phis = np.linalg.eig(np.linalg.inv(M) @ K)
242
+
243
+ # Get the indices that would sort lamb in descending order
244
+ idx = np.argsort(lamb)[::-1]
245
+
246
+ # Sort lamb and phis
247
+ lamb_r = lamb[idx]
248
+ phis_r = phis[:, idx]
249
+
250
+ # Normalize eigenvectors
251
+ N_par_tot = len(lamb)
252
+ phis_norm = np.zeros((N_par_tot, N_par_tot))
253
+ for i in range(N_par_tot):
254
+ c = np.sqrt(np.dot(phis_r[:, i].T, M @ phis_r[:, i]))
255
+ phis_norm[:, i] = phis_r[:, i] / c
256
+
257
+ return lamb_r, phis_norm
258
+
259
+
260
+ def get_mode_indices(lamb_r, phis_norm, N_plots):
261
+ """
262
+ Calculate the periods to get the top contributing index_modes.
263
+
264
+ Returns:
265
+ Numpy array of the indices of the largest N_plots periods
266
+ """
267
+ # Calculate periods
268
+ period = 2 * np.pi / np.sqrt(lamb_r)
269
+
270
+ # Find the indices of the largest N_plots periods
271
+ index_modes = np.argpartition(period, -N_plots)[-N_plots:]
272
+
273
+ # Sort index_modes so that the modes are in descending order of period
274
+ index_modes = index_modes[np.argsort(period[index_modes])][::-1]
275
+
276
+ # Extract lambdas and corresponding eigenvectors
277
+ lamb_plots = lamb_r[index_modes]
278
+ phis_plots = phis_norm[:, index_modes]
279
+
280
+ return index_modes, period
281
+
282
+
283
+ def calculate_global_displacements(Qglo_pel_curr1_mode, Qglo_pel_curr2_mode, Qglo_pel_curr3_mode, Qglo_pel_curr4_mode,
284
+ Qglo_pel_curr5_mode, Qglo_pel_curr6_mode, beta_curr, h_e):
285
+ """
286
+ Calculate the global displacements by solving the local symbolic
287
+ equilibrium equations.
288
+
289
+ Returns:
290
+ Lambda functions for the global displacements
291
+ """
292
+ # Define symbols
293
+ x, f0, f1, g0, g1, g2, g3, X_old, Y_old = sp.symbols('x f0 f1 g0 g1 g2 g3 X_old Y_old')
294
+
295
+ # Define local displacements
296
+ u_loc_i = Qglo_pel_curr1_mode * sp.cos(beta_curr) + Qglo_pel_curr2_mode * sp.sin(beta_curr)
297
+ v_loc_i = -Qglo_pel_curr1_mode * sp.sin(beta_curr) + Qglo_pel_curr2_mode * sp.cos(beta_curr)
298
+ u_loc_j = Qglo_pel_curr4_mode * sp.cos(beta_curr) + Qglo_pel_curr5_mode * sp.sin(beta_curr)
299
+ v_loc_j = -Qglo_pel_curr4_mode * sp.sin(beta_curr) + Qglo_pel_curr5_mode * sp.cos(beta_curr)
300
+
301
+ # Define beam displacements
302
+ u_beam = f1 * x + f0
303
+ v_beam = g3 * x**3 + g2 * x**2 + g1 * x + g0
304
+
305
+ # Define equilibrium equations
306
+ equations = [
307
+ v_beam.subs(x, 0) - v_loc_i,
308
+ sp.diff(v_beam, x).subs(x, 0) - Qglo_pel_curr3_mode,
309
+ v_beam.subs(x, h_e) - v_loc_j,
310
+ sp.diff(v_beam, x).subs(x, h_e) - Qglo_pel_curr6_mode,
311
+ u_beam.subs(x, 0) - u_loc_i,
312
+ u_beam.subs(x, h_e) - u_loc_j
313
+ ]
314
+
315
+ # Solve the equations
316
+ sol = sp.solve(equations, (f0, f1, g0, g1, g2, g3))
317
+
318
+ # Assign the solution
319
+ f0, f1, g0, g1, g2, g3 = sol.values()
320
+ u_beam = u_beam.subs(sol)
321
+ v_beam = v_beam.subs(sol)
322
+
323
+
324
+ # Define new coordinates using the same expressions as in the original code
325
+ X_new_expr = X_old + x * sp.cos(beta_curr) + u_beam * sp.cos(beta_curr) - v_beam * sp.sin(beta_curr)
326
+ Y_new_expr = Y_old + x * sp.sin(beta_curr) + u_beam * sp.sin(beta_curr) + v_beam * sp.cos(beta_curr)
327
+
328
+ # Substitute the solution into the expressions
329
+ X_new_expr_sub = X_new_expr.subs(sol)
330
+ Y_new_expr_sub = Y_new_expr.subs(sol)
331
+
332
+ # Convert X_new and Y_new to lambda functions
333
+ X_new_sub_func = lambdify(
334
+ (x, X_old, Y_old, beta_curr, Qglo_pel_curr1_mode, Qglo_pel_curr2_mode, Qglo_pel_curr3_mode, Qglo_pel_curr4_mode, Qglo_pel_curr5_mode, Qglo_pel_curr6_mode, h_e),
335
+ X_new_expr_sub,
336
+ "numpy",
337
+ )
338
+
339
+ Y_new_sub_func = lambdify(
340
+ (x, X_old, Y_old, beta_curr, Qglo_pel_curr1_mode, Qglo_pel_curr2_mode, Qglo_pel_curr3_mode, Qglo_pel_curr4_mode, Qglo_pel_curr5_mode, Qglo_pel_curr6_mode, h_e),
341
+ Y_new_expr_sub,
342
+ "numpy",
343
+ )
344
+
345
+ return X_new_sub_func, Y_new_sub_func