PeriDyno 1.0.0
Loading...
Searching...
No Matches
TJSoftConstraintSolver.cu
Go to the documentation of this file.
1#include "TJSoftConstraintSolver.h"
2#include "SharedFuncsForRigidBody.h"
3//#define USE_RELAXATION
4namespace dyno
5{
6 IMPLEMENT_TCLASS(TJSoftConstraintSolver, TDataType)
7
8 template<typename TDataType>
9 TJSoftConstraintSolver<TDataType>::TJSoftConstraintSolver()
10 :ConstraintModule()
11 {
12 this->inContacts()->tagOptional(true);
13 }
14
15 template<typename TDataType>
16 TJSoftConstraintSolver<TDataType>::~TJSoftConstraintSolver()
17 {
18 }
19
20 template<typename TDataType>
21 void TJSoftConstraintSolver<TDataType>::initializeJacobian(Real dt)
22 {
23 int constraint_size = 0;
24 int contact_size = this->inContacts()->size();
25
26 auto topo = this->inDiscreteElements()->constDataPtr();
27
28 int ballAndSocketJoint_size = topo->ballAndSocketJoints().size();
29 int sliderJoint_size = topo->sliderJoints().size();
30 int hingeJoint_size = topo->hingeJoints().size();
31 int fixedJoint_size = topo->fixedJoints().size();
32 int pointJoint_size = topo->pointJoints().size();
33
34 if (this->varFrictionEnabled()->getData())
35 {
36 constraint_size += 3 * contact_size;
37 }
38 else
39 {
40 constraint_size = contact_size;
41 }
42
43 if (ballAndSocketJoint_size != 0)
44 {
45 constraint_size += 3 * ballAndSocketJoint_size;
46 }
47
48 if (sliderJoint_size != 0)
49 {
50 constraint_size += 8 * sliderJoint_size;
51 }
52
53 if (hingeJoint_size != 0)
54 {
55 constraint_size += 8 * hingeJoint_size;
56 }
57
58 if (fixedJoint_size != 0)
59 {
60 constraint_size += 6 * fixedJoint_size;
61 }
62
63 if (pointJoint_size != 0)
64 {
65 constraint_size += 3 * pointJoint_size;
66 }
67
68 if (constraint_size == 0)
69 {
70 return;
71 }
72 if(mVelocityConstraints.size() != constraint_size)
73 mVelocityConstraints.resize(constraint_size);
74
75 if (contact_size != 0)
76 {
77 auto& contacts = this->inContacts()->getData();
78 setUpContactAndFrictionConstraints(
79 mVelocityConstraints,
80 mContactsInLocalFrame,
81 this->inCenter()->getData(),
82 this->inRotationMatrix()->getData(),
83 this->varFrictionEnabled()->getData()
84 );
85 }
86
87 if (ballAndSocketJoint_size != 0)
88 {
89 auto& joints = topo->ballAndSocketJoints();
90 int begin_index = contact_size;
91
92 if (this->varFrictionEnabled()->getData())
93 {
94 begin_index += 2 * contact_size;
95 }
96
97 setUpBallAndSocketJointConstraints(
98 mVelocityConstraints,
99 joints,
100 this->inCenter()->getData(),
101 this->inRotationMatrix()->getData(),
102 begin_index
103 );
104 }
105
106 if (sliderJoint_size != 0)
107 {
108 auto& joints = topo->sliderJoints();
109 int begin_index = contact_size;
110
111 if (this->varFrictionEnabled()->getData())
112 {
113 begin_index += 2 * contact_size;
114 }
115 begin_index += 3 * ballAndSocketJoint_size;
116 setUpSliderJointConstraints(
117 mVelocityConstraints,
118 joints,
119 this->inCenter()->getData(),
120 this->inRotationMatrix()->getData(),
121 this->inQuaternion()->getData(),
122 begin_index
123 );
124 }
125
126 if (hingeJoint_size != 0)
127 {
128 auto& joints = topo->hingeJoints();
129 int begin_index = contact_size + 3 * ballAndSocketJoint_size + 8 * sliderJoint_size;
130 if (this->varFrictionEnabled()->getData())
131 {
132 begin_index += 2 * contact_size;
133 }
134 setUpHingeJointConstraints(
135 mVelocityConstraints,
136 joints,
137 this->inCenter()->getData(),
138 this->inRotationMatrix()->getData(),
139 this->inQuaternion()->getData(),
140 begin_index
141 );
142 }
143
144 if (fixedJoint_size != 0)
145 {
146 auto& joints = topo->fixedJoints();
147 int begin_index = contact_size + 3 * ballAndSocketJoint_size + 8 * sliderJoint_size + 8 * hingeJoint_size;
148 if (this->varFrictionEnabled()->getData())
149 {
150 begin_index += 2 * contact_size;
151 }
152 setUpFixedJointConstraints(
153 mVelocityConstraints,
154 joints,
155 this->inRotationMatrix()->getData(),
156 this->inQuaternion()->getData(),
157 begin_index
158 );;
159 }
160
161 if (pointJoint_size != 0)
162 {
163 auto& joints = topo->pointJoints();
164 int begin_index = contact_size + 3 * ballAndSocketJoint_size + 8 * sliderJoint_size + 8 * hingeJoint_size + 6 * fixedJoint_size;
165 if (this->varFrictionEnabled()->getData())
166 {
167 begin_index += 2 * contact_size;
168 }
169 setUpPointJointConstraints(
170 mVelocityConstraints,
171 joints,
172 this->inCenter()->getData(),
173 begin_index
174 );
175 }
176
177 auto sizeOfRigids = this->inCenter()->size();
178 mContactNumber.resize(sizeOfRigids);
179
180 mJ.resize(4 * constraint_size);
181 mB.resize(4 * constraint_size);
182 mK_1.resize(constraint_size);
183 mK_2.resize(constraint_size);
184 mK_3.resize(constraint_size);
185 mEta.resize(constraint_size);
186 mLambda.resize(constraint_size);
187
188 mJ.reset();
189 mB.reset();
190 mK_1.reset();
191 mK_2.reset();
192 mK_3.reset();
193 mEta.reset();
194 mLambda.reset();
195
196 mContactNumber.reset();
197
198 calculateJacobianMatrix(
199 mJ,
200 mB,
201 this->inCenter()->getData(),
202 this->inInertia()->getData(),
203 this->inMass()->getData(),
204 this->inRotationMatrix()->getData(),
205 mVelocityConstraints
206 );
207
208 calculateK(
209 mVelocityConstraints,
210 mJ,
211 mB,
212 this->inCenter()->getData(),
213 this->inInertia()->getData(),
214 this->inMass()->getData(),
215 mK_1,
216 mK_2,
217 mK_3
218 );
219
220 calculateEtaVectorForPJSoft(
221 mEta,
222 mJ,
223 this->inVelocity()->getData(),
224 this->inAngularVelocity()->getData(),
225 this->inCenter()->getData(),
226 this->inQuaternion()->getData(),
227 mVelocityConstraints,
228 this->varSlop()->getValue(),
229 this->varDampingRatio()->getValue(),
230 this->varHertz()->getValue(),
231 this->varSubStepping()->getValue(),
232 dt
233 );
234
235 if (contact_size != 0)
236 {
237 calculateContactPoints(
238 this->inContacts()->getData(),
239 mContactNumber);
240 }
241 }
242
243 template<typename TDataType>
244 void TJSoftConstraintSolver<TDataType>::constrain()
245 {
246 uint bodyNum = this->inCenter()->size();
247
248 auto topo = this->inDiscreteElements()->constDataPtr();
249
250 mImpulseC.resize(bodyNum * 2);
251 mImpulseExt.resize(bodyNum * 2);
252 mImpulseC.reset();
253 mImpulseExt.reset();
254
255 Real dt = this->inTimeStep()->getData();
256
257
258 if (!this->inContacts()->isEmpty() || topo->totalJointSize() > 0)
259 {
260 if (mContactsInLocalFrame.size() != this->inContacts()->size()) {
261 mContactsInLocalFrame.resize(this->inContacts()->size());
262 }
263
264 setUpContactsInLocalFrame(
265 mContactsInLocalFrame,
266 this->inContacts()->getData(),
267 this->inCenter()->getData(),
268 this->inRotationMatrix()->getData()
269 );
270
271 Real dh = dt / this->varSubStepping()->getValue();
272
273 for (int i = 0; i < this->varSubStepping()->getValue(); i++)
274 {
275 if (this->varGravityEnabled()->getValue())
276 {
277 setUpGravity(
278 mImpulseExt,
279 this->varGravityValue()->getValue(),
280 dh
281 );
282 }
283
284
285 updateVelocity(
286 this->inAttribute()->getData(),
287 this->inVelocity()->getData(),
288 this->inAngularVelocity()->getData(),
289 mImpulseExt,
290 this->varLinearDamping()->getValue(),
291 this->varAngularDamping()->getValue(),
292 dh
293 );
294
295 mImpulseC.reset();
296 initializeJacobian(dh);
297 for (int j = 0; j < this->varIterationNumberForVelocitySolver()->getValue(); j++)
298 {
299 JacobiIterationForSoft(
300 mLambda,
301 mImpulseC,
302 mJ,
303 mB,
304 mEta,
305 mVelocityConstraints,
306 mContactNumber,
307 mK_1,
308 mK_2,
309 mK_3,
310 this->inMass()->getData(),
311 this->inFrictionCoefficients()->getData(),
312 this->varGravityValue()->getValue(),
313 dh,
314 this->varDampingRatio()->getValue(),
315 this->varHertz()->getValue()
316 );
317 }
318
319 updateVelocity(
320 this->inAttribute()->getData(),
321 this->inVelocity()->getData(),
322 this->inAngularVelocity()->getData(),
323 mImpulseC,
324 this->varLinearDamping()->getValue(),
325 this->varAngularDamping()->getValue(),
326 dh
327 );
328
329 updateGesture(
330 this->inAttribute()->getData(),
331 this->inCenter()->getData(),
332 this->inQuaternion()->getData(),
333 this->inRotationMatrix()->getData(),
334 this->inInertia()->getData(),
335 this->inVelocity()->getData(),
336 this->inAngularVelocity()->getData(),
337 this->inInitialInertia()->getData(),
338 dh
339 );
340 }
341 }
342
343 else
344 {
345 if (this->varGravityEnabled()->getValue())
346 {
347 setUpGravity(
348 mImpulseExt,
349 this->varGravityValue()->getValue(),
350 dt
351 );
352 }
353
354
355 updateVelocity(
356 this->inAttribute()->getData(),
357 this->inVelocity()->getData(),
358 this->inAngularVelocity()->getData(),
359 mImpulseExt,
360 this->varLinearDamping()->getValue(),
361 this->varAngularDamping()->getValue(),
362 dt
363 );
364
365 updateGesture(
366 this->inAttribute()->getData(),
367 this->inCenter()->getData(),
368 this->inQuaternion()->getData(),
369 this->inRotationMatrix()->getData(),
370 this->inInertia()->getData(),
371 this->inVelocity()->getData(),
372 this->inAngularVelocity()->getData(),
373 this->inInitialInertia()->getData(),
374 dt
375 );
376 }
377
378 }
379
380 DEFINE_CLASS(TJSoftConstraintSolver);
381}