-
Notifications
You must be signed in to change notification settings - Fork 2
/
solver.py
283 lines (210 loc) · 9.82 KB
/
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
import numpy as np
import copy
import timeit
import explicit_functions as explicit
import non_linear_solver_functions as nl_solving
import truss_element_linear_functions as truss
import truss_element_non_linear_functions as truss_nl
################################################################################
##################### STATIC SOLVERS ###########################################
################################################################################
def solve_linear(LHS,RHS):
u = np.linalg.solve(LHS,RHS)
return u
#ListOfElement [[element1,E,A,nodes]]
def solve_nonlinear_nr_lc(K_T,ListOfElement,ListOfBc,F_master):
## load control: given load -> solve for displacement
print('\n' + '################################################# \n')
print('Starting Newton-Raphson Iteration: ')
start_time = timeit.default_timer()
e_tollerance = 10**(-6)
system_size = K_T.shape[0]
K_n = copy.deepcopy(K_T)
disp_n = explicit.CreateInitialDisplacementVector(ListOfBc,system_size)
f_int_n = nl_solving.AssembleInternalForceVector(ListOfElement,disp_n,system_size)
r_n = nl_solving.CalculateResidualStatic(f_int_n,F_master)
nl_solving.ModifyResidual(r_n,ListOfBc)
r_n_norm = nl_solving.ResidualNorm(r_n)
n = 0
while (r_n_norm > e_tollerance):
n += 1
disp_n_1 = solve_linear(K_n,r_n)
disp_n_1 = disp_n - disp_n_1
f_int_n_1 = nl_solving.AssembleInternalForceVector(ListOfElement,disp_n_1,system_size)
r_n_1 = nl_solving.CalculateResidualStatic(f_int_n_1,F_master)
#prepare next step
nl_solving.ModifyResidual(r_n_1,ListOfBc)
r_n_norm = nl_solving.ResidualNorm(r_n_1)
r_n = r_n_1
disp_n = disp_n_1
K_n = nl_solving.UpdateStiffnessMatrix(ListOfElement,disp_n,ListOfBc)
nl_solving.PrintSolverUpdate(r_n,r_n_norm,n)
elapsed = timeit.default_timer() - start_time
print('\n'+'Finished in: ', elapsed, ' seconds\n')
print('################################################# \n\n')
return disp_n
def solve_nonlinear_nr_dc(ListOfElement,ListOfBc,F_master):
## displacement control: given displacement -> solve for load
print('\n' + '################################################# \n')
print('Starting Newton-Raphson Iteration: ')
start_time = timeit.default_timer()
e_tollerance = 10**(-6)
system_size = F_master.shape[0]
disp_const = explicit.CreateInitialDisplacementVector(ListOfBc,system_size)
f_ext_0 = nl_solving.CreateInitialForceVector(F_master,ListOfBc)
lambda_n = np.zeros((system_size,1))
f_ext_n = nl_solving.MultiplyVectorEntries(f_ext_0,lambda_n)
f_int_0 = nl_solving.AssembleInternalForceVector(ListOfElement,disp_const,system_size)
r_n = nl_solving.CalculateResidualStatic(f_int_0,f_ext_n)
nl_solving.ModifyResidual(r_n,ListOfBc)
r_n_norm = nl_solving.ResidualNorm(r_n)
n = 0
while (r_n_norm > e_tollerance):
n += 1
lambda_n_1 = nl_solving.DivideVectorEntries(r_n,-f_ext_0)
lambda_n_1 = lambda_n - lambda_n_1
f_ext_n_1 = nl_solving.MultiplyVectorEntries(lambda_n_1,f_ext_0)
r_n_1 = nl_solving.CalculateResidualStatic(f_int_0,f_ext_n_1)
#prepare next step
nl_solving.ModifyResidual(r_n_1,ListOfBc)
r_n_norm = nl_solving.ResidualNorm(r_n_1)
r_n = r_n_1
lambda_n = lambda_n_1
nl_solving.PrintSolverUpdate(r_n,r_n_norm,n)
elapsed = timeit.default_timer() - start_time
print('\n'+'Finished in: ', elapsed, ' seconds\n')
print('################################################# \n\n')
return lambda_n
def solve_nonlinear_updated(ListOfElement,ListOfBc,F_n,disp_n):
K_n = nl_solving.UpdateStiffnessMatrix(ListOfElement,disp_n,ListOfBc)
u_n = solve_linear(K_n,F_n)
return u_n
################################################################################
##################### DYNAMIC SOLVERS ###########################################
################################################################################
def solve_explicit_linear(M_master,K_master,C_master,F_master,Bc_List,d_t,t_end):
print('\n' + '################################################# \n')
print('Starting Explicit Time Integration: ')
start_time = timeit.default_timer()
# initialize
M_master_inv = explicit.InverseLumpedMatrix(M_master)
disp_n = explicit.CreateInitialDisplacementVector(Bc_List,K_master.shape[0])
vel_n = np.zeros((K_master.shape[0],1))
n, t_n = 0, 0.00
f_int_n = truss.CalculateInternalForces(K_master,disp_n)
res_n = explicit.CalculateResidualExplicit(F_master,f_int_n)
acc_n = explicit.ComputeAcceleration(M_master_inv,C_master,vel_n,res_n)
# prepare data
disp_expl = []
time_expl = []
# loop over time
while t_n < t_end:
# half time steps
t_n_05, t_n_1 = explicit.UpdateTime(t_n,d_t)
v_n_05 = explicit.UpdateVelocity(vel_n,acc_n,d_t)
explicit.EnforceBoundaryConditionsVelocity(Bc_List,v_n_05)
# update displacements + residual
disp_n_1 = explicit.UpdateDisplacement(v_n_05,disp_n,d_t)
f_int_n_1 = truss.CalculateInternalForces(K_master,disp_n_1) #----------> linear part!!!
res_n_1 = explicit.CalculateResidualExplicit(F_master,f_int_n_1)
# update acc + vel
acc_n_1 = explicit.ComputeAcceleration(M_master_inv,C_master,v_n_05,res_n_1)
vel_n_1 = explicit.UpdateVelocity(v_n_05,acc_n_1,d_t)
# save data
time_expl.append(t_n)
disp_expl.append(disp_n)
# prepare next time step
disp_n = disp_n_1
vel_n = vel_n_1
acc_n = acc_n_1
t_n += d_t
elapsed = timeit.default_timer() - start_time
print('\n'+'Finished in: ', elapsed, ' seconds\n')
print('################################################# \n\n')
return disp_expl, time_expl
def solve_explicit_non_linear(M_master,K_master,C_master,F_master,F_mod,ListOfElements,Bc_List,d_t,t_end):
# initialize
M_master_inv = explicit.InverseLumpedMatrix(M_master)
disp_n = explicit.CreateInitialDisplacementVector(Bc_List,K_master.shape[0])
vel_n = np.zeros((K_master.shape[0],1))
n, t_n = 0, 0.00
print('\n' + '################################################# \n')
print('Starting Explicit Time Integration: ')
start_time = timeit.default_timer()
f_int_n = solve_nonlinear_nr_dc(ListOfElements,Bc_List,F_mod)
res_n = explicit.CalculateResidualExplicit(F_master,f_int_n)
acc_n = explicit.ComputeAcceleration(M_master_inv,C_master,vel_n,res_n)
# prepare data
disp_expl = []
time_expl = []
# loop over time
while t_n < t_end:
# half time steps
t_n_05, t_n_1 = explicit.UpdateTime(t_n,d_t)
v_n_05 = explicit.UpdateVelocity(vel_n,acc_n,d_t)
explicit.EnforceBoundaryConditionsVelocity(Bc_List,v_n_05)
# update displacements + residual
disp_n_1 = explicit.UpdateDisplacement(v_n_05,disp_n,d_t)
bc_list_n_1 = explicit.UpdateNonLinearDisplacementVector(disp_n_1)
f_int_n_1 = solve_nonlinear_nr_dc(ListOfElements,bc_list_n_1,F_mod)
res_n_1 = explicit.CalculateResidualExplicit(F_master,f_int_n_1)
# update acc + vel
acc_n_1 = explicit.ComputeAcceleration(M_master_inv,C_master,v_n_05,res_n_1)
vel_n_1 = explicit.UpdateVelocity(v_n_05,acc_n_1,d_t)
# save data
time_expl.append(t_n)
disp_expl.append(disp_n)
# prepare next time step
disp_n = disp_n_1
vel_n = vel_n_1
acc_n = acc_n_1
t_n += d_t
elapsed = timeit.default_timer() - start_time
print('\n'+'Finished in: ', elapsed, ' seconds\n')
print('################################################# \n\n')
return disp_expl, time_expl
def solve_explicit_linear_updated(M_master,K_master,C_master,F_master,ListOfElements,Bc_List,d_t,t_end):
print('\n' + '################################################# \n')
print('Starting Explicit Time Integration: ')
start_time = timeit.default_timer()
# initialize
M_master_inv = explicit.InverseLumpedMatrix(M_master)
disp_n = explicit.CreateInitialDisplacementVector(Bc_List,K_master.shape[0])
vel_n = np.zeros((K_master.shape[0],1))
n, t_n = 0, 0.00
f_int_n = truss.CalculateInternalForces(K_master,disp_n)
res_n = explicit.CalculateResidualExplicit(F_master,f_int_n)
acc_n = explicit.ComputeAcceleration(M_master_inv,C_master,vel_n,res_n)
# prepare data
disp_expl = []
time_expl = []
# loop over time
while t_n < t_end:
# half time steps
t_n_05, t_n_1 = explicit.UpdateTime(t_n,d_t)
v_n_05 = explicit.UpdateVelocity(vel_n,acc_n,d_t)
explicit.EnforceBoundaryConditionsVelocity(Bc_List,v_n_05)
# update displacements + residual
disp_n_1 = explicit.UpdateDisplacement(v_n_05,disp_n,d_t)
# update K
K_n_1 = nl_solving.UpdateStiffnessMatrix(ListOfElements,disp_n,Bc_List)
d_disp_n = disp_n_1-disp_n
f_int_n_1 = f_int_n + truss.CalculateInternalForces(K_n_1,d_disp_n) #----------> updated!!!
res_n_1 = explicit.CalculateResidualExplicit(F_master,f_int_n_1)
# update acc + vel
acc_n_1 = explicit.ComputeAcceleration(M_master_inv,C_master,v_n_05,res_n_1)
vel_n_1 = explicit.UpdateVelocity(v_n_05,acc_n_1,d_t)
# save data
time_expl.append(t_n)
disp_expl.append(disp_n)
# prepare next time step
f_int_n = f_int_n_1
disp_n = disp_n_1
vel_n = vel_n_1
acc_n = acc_n_1
t_n += d_t
print(t_n/t_end*100.00 , '% completed\n')
elapsed = timeit.default_timer() - start_time
print('\n'+'Finished in: ', elapsed, ' seconds\n')
print('################################################# \n\n')
return disp_expl, time_expl