-
Notifications
You must be signed in to change notification settings - Fork 11
/
treedy.py
346 lines (282 loc) · 11 KB
/
treedy.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
#!/usr/bin/env pyython3
import pyservoce
import zencad.mbody.kinematic
import zencad.libs.inertia
from zencad.libs.screw import screw
from zencad.mbody.kinematic import kinematic_frame
from zencad.libs.dynsolver import dynamic_solver
import zencad.libs.rigid_body as rigid_body
import zencad.libs.constraits as constraits
import zencad.libs.matrix_solver as matrix_solver
import zencad.libs.assemble_addons as assemble_addons
class tree_dynamic_solver(dynamic_solver):
def __init__(self, baseunit):
super().__init__(baseunit)
#self.find_post_force_sources()
#self.find_post_inertial_objects()
assemble_addons.attach_speed_model(baseunit)
self.attach_finite_elements_model()
self.reaction_solver = matrix_solver.reaction_solver(
rigid_bodies = self.rigid_bodies,
constraits = self.constraits)
#print(self.reaction_solver.rigid_bodies)
def collect_and_atach_rigid_bodies(self):
arr = []
for kinframe in self.kinematic_frames:
kinframe.rigid_body = rigid_body.rigid_body_from_kinframe(kinframe)
#print(kinframe.name, kinframe.rigid_body)
arr.append(kinframe.rigid_body)
return arr
def collect_and_atach_constraits(self):
arr = []
for kinframe in self.kinematic_frames:
kinframe.constrait = constraits.make_constraits_from_kinframe(kinframe)
arr.append(kinframe.constrait)
return arr
def attach_finite_elements_model(self):
self.rigid_bodies = self.collect_and_atach_rigid_bodies()
self.constraits = self.collect_and_atach_constraits()
def print_state(self):
print("kinematic_frames", self.kinematic_frames)
print("inertial_objects", self.inertial_objects)
print("force_sources", self.force_sources)
print("constrait_conditions", self.constrait_conditions)
print("units", self.units)
def print_pre_kinematic_frames(self):
print("pre_kinematic_frames for iners:")
for iner in self.inertial_objects:
print("{}: {}".format(iner.unit.name, iner.pre_kinematic_frames))
def print_post_inertial_objects(self):
print("post_inertial_objects for kinframes:")
for kinframe in self.kinematic_frames:
print("{}: {}".format(kinframe, kinframe.post_inertial_objects))
def print_post_force_sources(self):
print("post_force_sources for kinframes:")
for kinframe in self.kinematic_frames:
print("{}: {}".format(kinframe, kinframe.post_force_sources))
#def find_pre_kinematic_frames(self):
# for iner in self.inertial_objects:
# u = iner.unit
# iner.pre_kinematic_frames = []
# while u is not None:
# if isinstance(u, kinematic_frame):
# iner.pre_kinematic_frames.append(u)
# u = u.parent
#
# for kinframe in self.kinematic_frames:
# u = kinframe.parent
# kinframe.pre_kinematic_frames = []
# while u is not None:
# if isinstance(u, kinematic_frame):
# kinframe.pre_kinematic_frames.append(u)
# u = u.parent
#
#def find_post_force_sources(self):
# for kinframe in self.kinematic_frames:
# kinframe.post_force_sources = []
#
# def add_forces_recurse(u):
# if hasattr(u, "force_sources"):
# #kinframe.post_force_sources.extend(u.force_sources)
# for fs in u.force_sources:
# if fs.tp == "compenser": continue
# if fs.tp == "node" and fs.unit is not kinframe: continue
# kinframe.post_force_sources.append(fs)
#
# for c in u.childs:
# add_forces_recurse(c)
#
# add_forces_recurse(kinframe)
#
#def find_post_inertial_objects(self):
# for kinframe in self.kinematic_frames:
# kinframe.post_inertial_objects = []
#
# def add_iobj_recurse(u):
# if hasattr(u, "inertial_object"):
# kinframe.post_inertial_objects.append(u.inertial_object)
#
# for c in u.childs:
# add_iobj_recurse(c)
#
# add_iobj_recurse(kinframe)
#
#def find_post_inertia_objects(self):
# for f in self.kinematic_frames:
# f.post_inertia_objects = []
#
# def iteration(unit):
# if hasattr(unit, "inertial_object"):
# f.post_inertia_objects.append(unit.inertial_object)
# for u in unit.childs:
# iteration(u)
#
# iteration(f)
def calculate_impulses(self):
pass
# for iner in self.inertial_objects:
# accum = screw()
# for p in iner.pre_kinematic_frames:
# arm = (iner.global_pose.translation() - p.global_pose.translation())
# accum += p.global_spdscr.angular_carry(arm)
# iner.global_speed = accum
#
# accum = screw()
# for p in iner.pre_kinematic_frames:
# arm = (iner.global_pose.translation() - p.global_pose.translation())
# accum += zencad.libs.screw.second_kinematic_carry(iacc=p.global_accscr, ispd=p.global_spdscr, arm=arm)
# iner.global_acceleration = accum
#
# iner.update_global_impulse_with_global_speed(accum)
def print_impulses(self):
for i in self.inertial_objects:
print("{}: {}".format(i.unit, i.global_impulse))
def onestep(self, delta):
self.baseunit.location_update(deep=True, view=False)
for k in self.kinematic_frames:
k.update_global_speed()
k.update_global_acceleration()
assemble_addons.update_speed_model(self.baseunit)
for iner in self.inertial_objects:
iner.update_globals()
self.reaction_solver.update_globals()
reactions = self.reaction_solver.solve()
self.reaction_solver.apply_reactions_for_constraits(reactions)
self.reduce_kinframe_forces()
self.set_kynframe_accelerations_by_reduced_forces()
for kinframe in self.kinematic_frames:
kinframe.dynstep(delta)
#self.naive_integrate()
self.baseunit.location_update(deep=True, view=False)
#def naive_integrate(self, delta):
#for kinframe in self.kinematic_frames:
# inertia = kinframe.rigid_body.global_inertia
# kinframe.global_accscr = inertia.force_to_acceleration()
# kinframe.global_spdscr = kinframe.global_spdscr + kinframe.global_accscr * delta
def set_kynframe_accelerations_by_reduced_forces(self):
for kinframe in self.kinematic_frames:
#if kinframe.parent:
reference = kinframe.global_frame_acceleration_reference
spdref = kinframe.global_frame_speed_reference
radius = (kinframe.global_pose.inverse() * kinframe.output.global_pose).translation()
arm = kinframe.global_pose(radius)
#else:
# reference = screw()
# spdref = screw()
# arm = pyservoce.vector3()
addacc = zencad.libs.screw.second_kinematic_carry(reference, spdref, arm)
masskoeff = kinframe.rigid_body.global_inertia.koefficient_for_with_guigens(kinframe.global_sensivity())
rforce = kinframe.force_reduction.dot(kinframe.global_sensivity())
accel = rforce / masskoeff - addacc.dot(kinframe.global_sensivity())
#kinframe.set_acceleration(accel)
#print(kinframe.name, kinframe.force_reduction)
#print(masskoeff)
#print(accel)
#def func(unit, lastkinframe=None):
# if isinstance(unit, kinematic_frame):
# if lastkinframe:
# reference = lastkinframe.global_frame_acceleration_reference
# spdref = lastkinframe.global_frame_speed_reference
# radius = (lastkinframe.global_pose.inverse() * unit.global_pose).translation()
# arm = lastkinframe.global_pose(radius)
# else:
# reference = screw()
# spdref = screw()
# arm = pyservoce.vector3()
#
#
# addacc = zencad.libs.screw.second_kinematic_carry(reference, spdref, arm)
# masskoeff = unit.rigid_body.global_inertia.koefficient_for_with_guigens(unit.global_sensivity())
# rforce = unit.force_reduction.dot(unit.global_sensivity())
# accel = rforce / masskoeff #+ addacc.dot(unit.global_sensivity())
#
# print(addacc)
#
# unit.set_acceleration(accel)
#
# lastkinframe = unit
#
# for u in unit.childs:
# func(u, lastkinframe)
#
#func(self.baseunit)
def reduce_kinframe_forces(self):
#print("reduce_kinframe_forces")
for kinframe in self.kinematic_frames:
accum = screw()
accum += kinframe.rigid_body.inertia_force_in_body_frame()
for c in kinframe.rigid_body.constrait_connections:
reactions = c.get_reaction_force_global()
#print(reactions)
for r in reactions:
accum += r
kinframe.force_reduction = accum
#print(accum)
def calculate_kinframe_frame_speeds_accelerations(self):
for kinframe in self.kinematic_frames:
kinframe.global_frame_speed = screw()
kinframe.global_frame_acceleration = screw()
for u in kinframe.pre_kinematic_frames:
arm = kinframe.global_pose.translation() - u.global_pose.translation()
kinframe.global_frame_speed += u.global_spdscr.kinematic_carry(arm)
kinframe.global_frame_acceleration += \
zencad.libs.screw.second_kinematic_carry(iacc=u.global_accscr, ispd=u.global_spdscr, arm=arm)
def calculate_kinframe_accelerations_no_constrait(self):
for kinframe in self.kinematic_frames:
kinframe.evaluate_accelerations_without_constraits()
def calculate_kinframe_accelerations_primitive(self):
for kinframe in self.kinematic_frames:
if len(kinframe.post_inertial_objects) > 0:
kinframe.global_accscr = \
kinframe.global_force_reduction * kinframe.post_inertial_objects[0].mass
def calculate_kinframe_complex_inertia(self):
for kinframe in self.kinematic_frames:
kinframe.inertia_koefficient = 0
for iner in kinframe.post_inertial_objects:
arm = (kinframe.global_pose.translation() - iner.global_pose.translation())
inertia = iner.global_inertia().guigens_transform(arm)
kinframe.inertia_koefficient += inertia.koefficient_for(kinframe.global_sensivity())
def calculate_kinframe_forces(self):
for kinframe in self.kinematic_frames:
accum = screw()
for fs in kinframe.post_force_sources:
arm = (kinframe.global_pose.translation() - fs.point())
f = fs.global_force().force_carry(arm)
accum += f
a = accum.copy()
for iner in kinframe.post_inertial_objects:
ispd = iner.global_speed - kinframe.global_frame_speed
iacc = iner.global_acceleration - kinframe.global_frame_acceleration
espd = kinframe.global_frame_speed
eacc = kinframe.global_frame_acceleration
arm = iner.global_pose.translation() - kinframe.global_pose.translation()
#I_trans = - iner.mass * ( eacc.lin + eacc.ang.cross(arm) + espd.ang.cross(espd.ang.cross(arm)) )
#I_trans = - iner.mass * ( eacc.ang.cross(arm) + espd.ang.cross(espd.ang.cross(arm)) )
#I_kor = - iner.mass * (2 * espd.ang.cross(ispd.lin))
#print(iacc)
I_trans = pyservoce.vector3()
I_kor = pyservoce.vector3()
#Dalamber = - iner.mass * iacc.lin
#print(kinframe.name,iner.unit.name, screw(lin=Dalamber).force_carry(-arm))
Dalamber = pyservoce.vector3()
accum += screw(lin=I_trans + I_kor + Dalamber).force_carry(-arm)
kinframe.global_force_reduction = accum
#print("summary", kinframe.global_force_reduction)
#print("HEERE", kinframe.global_force)
def calculate_kinframe_free_forces(self):
for kinframe in self.kinematic_frames:
accum = screw()
for fs in kinframe.local_force_sources:
pass
#arm = (kinframe.global_pose.translation() - fs.point())
#f = fs.global_force().force_carry(arm)
#print(kinframe.name, fs.unit.name, fs.__class__, f)
#accum += f
def calculate_kinframe_inertia_forces(self):
for kinframe in self.kinematic_frames:
inertia = kinframe.local_inertia
angspd = kinframe.frame_spdscr
kinframe.inertia_forces = screw(
lin = - m * (angspd.cross(angspd.cross(inertia.cm))),
ang = - angspd.cross(inertia.matrix * angspd)
)