-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunclassical_graph.py
342 lines (301 loc) · 11.8 KB
/
unclassical_graph.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
"""
ONLY LINEAR ELEMENTS
classical_rod.py works using quaternions for rotation interpolations,
go to etc. for other methods to interpolate rotations
"""
import numpy as np
from gradientsolver import solver1d as sol
from include import slerp as slerpsol, quaternion_smith as quat_sol
import matplotlib.pyplot as plt
from include.AnimationController import ControlledAnimation
import pandas as pd
try:
import scienceplots
plt.style.use(['science'])
except ImportError as e:
pass
np.set_printoptions(linewidth=250)
"""
Main loop
"""
def fea(load_iter_, is_halt=False):
"""
:param load_iter_: Load index
:param is_halt: signals animator if user requested a pause
:return: use input , True if user want to stop animation
"""
global u
global du
global residue_norm
global increments_norm
global is_log_residue
for iter_ in range(MAX_ITER):
KG, FG = sol.init_stiffness_force(numberOfNodes, DOF)
# Follower load
s = sol.get_rotation_from_theta_tensor(u[-6: -3]) @ np.array([0, fapp__[load_iter_], 0])[:, None] * 0
# FG[-12: -9] = s
# Pure Bending
FG[-6, 0] = fapp__[load_iter_] * 0
for elm in range(numberOfElements):
n = icon[elm][1:]
xloc = node_data[n][:, None]
rloc = np.zeros((3, 4))
tloc = np.zeros((3, 4))
rloc[:, [0, 2]] = np.array([u[DOF * n, 0], u[DOF * n + 1, 0], u[DOF * n + 2, 0]])
rloc[:, [1, 3]] = np.array([u[DOF * n + 3, 0], u[DOF * n + 4, 0], u[DOF * n + 5, 0]])
tloc[:, [0, 2]] = np.array([u[DOF * n + 6, 0], u[DOF * n + 7, 0], u[DOF * n + 8, 0]])
tloc[:, [1, 3]] = np.array([u[DOF * n + 9, 0], u[DOF * n + 10, 0], u[DOF * n + 11, 0]])
kloc, floc = sol.init_stiffness_force(nodesPerElement, DOF)
gloc = np.zeros((DOF, 1))
for xgp in range(len(wgp)):
# N_, Bmat = sol.get_lagrange_fn(gp[xgp], element_type)
le = xloc[-1][0] - xloc[0][0]
Jac = le / 2
N_, Nx_, Nxx_ = sol.get_hermite_fn(gp[xgp], Jac, element_type)
N_, Nx_, Nxx_ = N_[:, None], Nx_[:, None], Nxx_[:, None]
rds = rloc @ Nx_
rdsds = rloc @ Nxx_
tds = tloc @ Nx_
tdsds = tloc @ Nxx_
k = tds
kp = tdsds
Rot = sol.get_rotation_from_theta_tensor(tloc @ N_)
v = Rot.T @ rds
Rotds = Rot @ sol.skew(k)
vp = Rotds.T @ rds + Rot.T @ rdsds
# print(v[:, 0])
gloc[0: 3] = Rot @ ElasticityExtension @ (v - np.array([0, 0, 1])[:, None])
gloc[3: 6] = Rot @ ElasticityExtensionH @ vp
gloc[6: 9] = Rot @ ElasticityBending @ k
gloc[9: 12] = Rot @ ElasticityBendingH @ kp
tangent, res = sol.get_higher_order_tangent_residue(N_, Nx_, Nxx_, rds, rdsds, Rot, Rotds, ElasticityExtension,
ElasticityBending, ElasticityExtensionH, ElasticityBendingH, k, DOF, gloc, element_type)
floc += res * wgp[xgp] * Jac
kloc += tangent * wgp[xgp] * Jac
iv = np.array(sol.get_assembly_vector(DOF, n))
FG[iv[:, None], 0] += floc
KG[iv[:, None], iv] += kloc
# TODO: Make a generalized function for application of point as well as body loads
f = np.zeros((12, 12))
f[0: 3, 6: 9] = -sol.skew(s)
KG[-12:, -12:] += f
# dsf = tg - KG
for ibc in range(6):
sol.impose_boundary_condition(KG, FG, ibc, 0 + (-1.05 + u[5, 0]) * (ibc == 5))
for ibc in [-7]:
sol.impose_boundary_condition(KG, FG, ibc, 0 + (-1.05 + u[-7, 0]) * (ibc == -7))
for ibc in [-8, -9, -10, -11, -12]:
sol.impose_boundary_condition(KG, FG, ibc, 0 + (-L + u[-10, 0]) * (ibc == -10))
du = -sol.get_displacement_vector(KG, FG)
residue_norm = np.linalg.norm(FG)
increments_norm = np.linalg.norm(du)
if increments_norm > 1:
du = du / increments_norm
if increments_norm < 1e-6 and residue_norm < 1e-3:
break
"""
Configuration update (not working as of now) for angles greater than 360 deg, Make this work for multi-axis rotations
"""
# for i in range(numberOfNodes):
# q = slerpsol.rotation_vector_to_quaterion(u[6 * i + 3: 6 * i + 6, 0])
# dq = slerpsol.rotation_vector_to_quaterion(du[6 * i + 3: 6 * i + 6, 0])
# q = slerpsol.quatmul(q, dq)
# u[6 * i + 3: 6 * i + 6, 0] = slerpsol.quaterion_to_rotation_vec(q)
# u[6 * i + 0: 6 * i + 3] += du[6 * i + 0: 6 * i + 3]
"""
Approx. configuration update
"""
# TODO: Change this, it works perfectly if two rotations are about one axis (R_(i+1) = exp(dtheta_i) * exp(theta_i))
u += du
if is_log_residue:
print(
"--------------------------------------------------------------------------------------------------------------------------------------------------",
fapp__[load_iter_], load_iter_)
print(residue_norm, increments_norm)
# vv = np.array([i for i in range(numberOfNodes) if i % 2 == 0])
# print(u[DOF * vv + 6, 0])
return is_halt
"""
Set Finite Element Parameters
"""
DIMENSIONS = 1
DOF = 12
MAX_ITER = 60 # Max newton raphson iteration
element_type = 2
L = 1
numberOfElements = 100
icon, node_data = sol.get_connectivity_matrix(numberOfElements, L, element_type)
numberOfNodes = len(node_data)
ngpt = 3
wgp, gp = sol.init_gauss_points(ngpt)
# Setting up displacement vectors
u = np.zeros((numberOfNodes * DOF, 1))
du = np.zeros((numberOfNodes * DOF, 1))
nodesPerElement = element_type ** DIMENSIONS
"""
SET MATERIAL PROPERTIES
-----------------------------------------------------------------------------------------------------------------------
"""
l0 = 1
E0 = 10 ** 8
G0 = E0 / 2.0
d = 1 / 1000 * 25.0
A = np.pi * d ** 2 * 0.25
i0 = np.pi * d ** 4 / 64
J = i0 * 2
EI = 3.5 * 10 ** 7
GA = 1.6 * 10 ** 8
ElasticityExtension = np.array([[G0 * A, 0, 0],
[0, G0 * A, 0],
[0, 0, E0 * A]])
ElasticityBending = np.array([[E0 * i0 + l0 ** 2 * E0 * A, 0, 0],
[0, E0 * i0 + l0 ** 2 * E0 * A, 0],
[0, 0, G0 * J + 2 * l0 ** 2 * G0 * A]])
ElasticityExtensionH = l0 ** 2 * np.array([[G0 * A, 0, 0],
[0, G0 * A, 0],
[0, 0, E0 * A]])
ElasticityBendingH = np.array([[E0 * i0 * l0 ** 2, 0, 0],
[0, E0 * i0 * l0 ** 2, 0],
[0, 0, G0 * J * l0 ** 2]])
# ElasticityExtension = np.array([[GA, 0, 0],
# [0, GA, 0],
# [0, 0, 2 * GA]])
# ElasticityBending = np.array([[EI, 0, 0],
# [0, EI, 0],
# [0, 0, 0.5 * EI]])
"""
Markers
"""
vi = np.array([i for i in range(numberOfNodes)])
vii = np.array([i for i in range(numberOfNodes) if i & 1 == 0])
"""
Starting point
"""
# u = np.zeros((numberOfNodes * DOF, 1))
u[DOF * vi + 2, 0] = node_data
u[DOF * vi + 5, 0] = 0
residue_norm = 0
increments_norm = 0
# since rod is lying straight in E3 direction it's centerline will have these coordinates
# Thetas are zero
r1 = np.zeros(numberOfNodes)
r2 = np.zeros(numberOfNodes)
r3 = np.zeros(numberOfNodes)
for i in range(numberOfNodes):
r1[i] = u[DOF * i][0]
r2[i] = u[DOF * i + 1][0]
r3[i] = u[DOF * i + 2][0]
"""
Initialize Graph
"""
fig, (ax, ay) = plt.subplots(1, 2, figsize=(16, 5), width_ratios=[1, 2])
ax.set_xlim(0, L)
ax.plot(r3, r2, label="un-deformed", marker="o")
"""
Set load and load steps
"""
max_load = 4.35
# max_load = 30 * E0 * i0
LOAD_INCREMENTS = 2 # Follower load usually needs more steps compared to dead or pure bending
fapp__ = -np.linspace(0, max_load, LOAD_INCREMENTS)
marker_ = np.linspace(0, max_load, LOAD_INCREMENTS)
# marker_ = np.insert(marker_, 0, [2000, 6000, 12000], axis=0)
"""
------------------------------------------------------------------------------------------------------------------------------------
Post Processing
------------------------------------------------------------------------------------------------------------------------------------
"""
"""
Graph limits defaults
"""
xmax = 1e-7
ymax = 1e-7
xmin = 0
ymin = 0
video_request = False
is_log_residue = True # Prints residue to console after every load iteration if set true
displacements = []
def act(i):
global u
global xmax
global ymax
global xmin
global ymin
global video_request
global displacements
halt = fea(i)
if halt:
controlled_animation.stop()
return
y0 = u[DOF * vi + 1, 0]
x0 = u[DOF * vi + 2, 0]
if np.isclose(abs(fapp__[i]), marker_).any():
print(u[-12:, 0])
displacements.append(u[-11, 0])
xmax, ymax = max(xmax, np.max(x0)), max(np.max(y0), ymax)
xmin, ymin = min(xmin, np.min(x0)), min(np.min(y0), ymin)
ax.axis('equal')
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
line1.set_ydata(y0)
line1.set_xdata(x0)
ax.set_title("Centerline displacement, Applied Load : " + str(round(fapp__[i], 5)))
if not video_request:
ax.plot(x0, y0)
ay.scatter(abs(fapp__[i]), -L + u[-10, 0], marker=".")
ay.scatter(abs(fapp__[i]), u[-11, 0], marker="+")
if i == LOAD_INCREMENTS - 1:
controlled_animation.disconnect()
ay.scatter(0, 0, marker=".", label="horizontal tip displacement")
ay.scatter(0, 0, marker="+", label="vertical tip displacement")
ay.legend()
ay.axhline(y=0)
ay.set_xlabel(r"LOAD", fontsize=16)
ay.set_ylabel(r"Tip Displacement", fontsize=16)
ax.set_xlabel(r"$r_3$", fontsize=25)
ax.set_ylabel(r"$r_2$", fontsize=25)
ax.set_ylim(-85, 41)
y = u[DOF * vi + 1, 0]
x = u[DOF * vi + 2, 0]
line1, = ax.plot(x, y)
ax.set_title("Centerline displacement")
ay.set_title("Tip Displacement vs Load")
controlled_animation = ControlledAnimation(fig, act, frames=len(fapp__), video_request=video_request, repeat=False)
controlled_animation.start()
l0 = l0 / L
fig2, (a0, a1) = plt.subplots(1, 2, figsize=(12, 6))
node_data = node_data / L
M0 = max_load / (E0 * (A * l0 ** 2 + i0) * L)
ETA = np.sqrt(i0 * l0 ** 2 / (i0 + A * l0 ** 2))
a0.plot(node_data, u[DOF * vi + 6, 0], label="FEM")
a0.plot(node_data, M0 * (node_data - ETA * np.tanh(1 / 2 / ETA) + ETA * np.sinh((1 - 2 * node_data) / 2 / ETA) / np.cosh(1 / 2 / ETA)), label="ANALYTICAL")
a1.plot(node_data, u[DOF * vi + 9, 0], label="FEM")
a1.plot(node_data, M0 * (1 - np.cosh((1 - 2 * node_data) / 2 / ETA) / np.cosh(1 / 2 / ETA)), label="ANALYTICAL")
a1.legend()
a0.legend()
a0.set_title("DISPLACEMENT")
a1.set_title("STRAIN")
# df1 = pd.DataFrame([node_data])
# df1.loc[len(df1)] = u[DOF * vi + 5, 0] - 1
# df2 = pd.DataFrame([node_data])
# df2.loc[len(df2)] = u[DOF * vi + 2, 0] - node_data
# df1.to_csv('GFG1.csv', index=False, header=False)
# df2.to_csv('GFG2.csv', index=False, header=False)
fig3, a00 = plt.subplots(1, 1, figsize=(9, 9))
ETA = 0.05
print(u[:, 0])
print(u[DOF * vi + 5, 0] - 1)
print(u[DOF * vi + 2, 0])
print(node_data)
a00.plot(node_data, u[DOF * vi + 5, 0] - 1, label="FEM")
if l0 == 0:
l0 = l0 + 1
a00.plot(node_data, ETA * (np.cosh((1 - 2 * node_data) / 2 / l0) - 2 * l0 * np.sinh(1 / 2 / l0)) / (np.cosh(1 / 2 / l0) - 2 * l0 * np.sinh(1 / 2 / l0)), label="ANALYTICAL")
a00.legend()
a00.set_title("STRAIN")
plt.show()
print(max_load * L / GA / 2, u[-12:])
print(displacements[1:])
node_data = np.round(node_data, 2)
df = pd.DataFrame(u[DOF * vi + 5, 0][None, :] - 1)
df.to_csv('assets/two.csv', mode='a', index=False, header=False)