-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpatialIntegratorODE.cpp
More file actions
380 lines (329 loc) · 11.2 KB
/
SpatialIntegratorODE.cpp
File metadata and controls
380 lines (329 loc) · 11.2 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/***************************************************************************//**
* Project: Colony
*
* \file SpatialIntegratorODE.cpp
* \author Marc Weber\n
* The SiMBioSys group (CosmoLab)\n
* Parc Científic de Barcelona\n
* Barcelona, Spain.\n
* http://www.thesimbiosys.com
* \version 1.0
* \date 05/2011
*
* Copyright 2009 by Marc Weber
******************************************************************************/
#include "SpatialIntegratorODE.h"
#include "GraphicsCellODE.h"
namespace
{
void nearCallback(void *data, dGeomID o0, dGeomID o1)
{
reinterpret_cast<SpatialIntegratorODE*>(data)->handleCollisionBetween(o0,o1);
}
}
//------------------------------------------------------------------------------
SpatialIntegratorODE::SpatialIntegratorODE(Simulator* simulatorPtr)
: simulatorPtr_(simulatorPtr)
{
cellsList_.clear();
init();
}
//------------------------------------------------------------------------------
SpatialIntegratorODE::~SpatialIntegratorODE()
{
dSpaceDestroy(space_);
dWorldDestroy(world_);
dCloseODE();
}
//------------------------------------------------------------------------------
void SpatialIntegratorODE::init()
{
isEquilibrationSteps_ = false;
dInitODE();
world_ = dWorldCreate();
space_ = dHashSpaceCreate(0);
dWorldSetGravity (world_, 0.0f, 0.0f, 0.0f);
contactgroup_ = dJointGroupCreate(0);
//nbSecondsByStep_ = 0.001;
nbSecondsByStep_ = 0.002;
//contactMode_ = dContactSoftCFM | dContactSoftERP;
contactMode_ = dContactSoftERP;
erp_ = 0.2;//0.2
cfm_ = 0.02;//0.0
mu_ = 0;
interactionForce_ = 0.0;
centralForce0_ = 10.0;
//viscousCoefficient_ = 200.0;
viscousCoefficient_ = 20.0;
//rotationalViscousCoefficient_ = 600.0;
rotationalViscousCoefficient_ = 20.0;
aligningAngle_ = 35.0;
aligningTorqueCoeff_ = 5.0;
aligningRotationalViscousCoeff_ = 50.0;
brownianForce_ = 50.0;
maxVelocitySquared_ = 0.5*0.5;
maxRotVelocitySquared_ = 1*1;
// Set the damping coefficients of the ODE library in the world
//dWorldSetDamping ( world_, 0.1, 0.2);
//dWorldSetLinearDampingThreshold ( world_, 0.001);
//dWorldSetAngularDampingThreshold ( world_, 0.001);
dWorldSetMaxAngularSpeed ( world_, 2.0 );
dWorldSetContactMaxCorrectingVel ( world_, 0.2);
}
//------------------------------------------------------------------------------
dWorldID SpatialIntegratorODE::getWorld() const
{
return world_;
}
//------------------------------------------------------------------------------
dSpaceID SpatialIntegratorODE::getSpace() const
{
return space_;
}
//------------------------------------------------------------------------------
QList<GraphicsCellODE*>* SpatialIntegratorODE::getCellsList()
{
return &cellsList_;
}
//------------------------------------------------------------------------------
void SpatialIntegratorODE::setIsEquilibrationSteps(bool isEquilibrationSteps)
{
isEquilibrationSteps_ = isEquilibrationSteps;
}
//------------------------------------------------------------------------------
void SpatialIntegratorODE::integrate(double timeStep)
{
// We tweak the central force intensity in function of the radius of the colony.
{
TinyVector<double,4> centerAndRadius = computeColonyCenterAndRadius();
int n = cellsList_.size();
// This is more or less the radius of a totally packed colony
double cellLength0 = simulatorPtr_->cellCollection_[0].getCellLength0();
double cellHeight0 = simulatorPtr_->cellCollection_[0].getCellHeight0();
double denseColonyRadius = sqrt((1.44*cellLength0*cellHeight0*n)/3.141592654) / 2.0;
// Correct central force in function of colony radius
centralForce_ = (centerAndRadius(3)/denseColonyRadius)*
(centerAndRadius(3)/denseColonyRadius)*centralForce0_;
// Amplify central force during equilibration
if (isEquilibrationSteps_)
{
centralForce_ = centralForce_ * 100.0;
}
}
int nbStepsToPerform = static_cast<int>(timeStep/nbSecondsByStep_);
// Make these steps to advance world time
for (int i=0;i<nbStepsToPerform;++i)
{
// Add forces
computeForces(isEquilibrationSteps_);
// Detect collision
dSpaceCollide(space_, this, &nearCallback);
// Step world
dWorldQuickStep(world_, nbSecondsByStep_);
//dWorldStep(world_, nbSecondsByStep_);
foreach (GraphicsCellODE *cell, cellsList_)
{
// Apply correction from 2D constraint
cell->alignToZAxis();
}
if (!isEquilibrationSteps_)
{
foreach (GraphicsCellODE *cell, cellsList_)
{
// Apply maximum velocity limit
cell->limitVelocity(maxVelocitySquared_,maxRotVelocitySquared_);
}
}
// Remove all temporary collision joints now that the world has been stepped
dJointGroupEmpty(contactgroup_);
}
// Send the new position and angle of the cells back to the simulator
for (int i=0; i < cellsList_.size(); ++i)
{
TinyVector<double,3> pos = cellsList_.at(i)->getPositionODE();
simulatorPtr_->cellCollection_[i].Cell::setPosition(pos);
double angle = cellsList_.at(i)->getAngle();
simulatorPtr_->cellCollection_[i].Cell::setAngle(angle);
}
}
//------------------------------------------------------------------------------
void SpatialIntegratorODE::handleCollisionBetween(dGeomID o0, dGeomID o1)
{
// Create an array of dContact objects to hold the contact joints
static const int MAX_CONTACTS = 10;
dContact contact[MAX_CONTACTS];
/*
By adjusting the values of ERP and CFM, you can achieve various effects.
For example you can simulate springy constraints, where the two bodies
oscillate as though connected by springs. Or you can simulate more spongy
constraints, without the oscillation. In fact, ERP and CFM can be selected
to have the same effect as any desired spring and damper constants. If you
have a spring constant kp and damping constant kd, then the corresponding
ODE constants are:
ERP = h kp / (h kp + kd)
CFM = 1 / (h kp + kd)
where h is the time step
or
(spring) kp = ECM / CFM h
(damping) kd = (ECM - 1) / CFM
*/
for (int i = 0; i < MAX_CONTACTS; i++)
{
contact[i].surface.mode = contactMode_;
contact[i].surface.soft_erp = erp_;
contact[i].surface.soft_cfm = cfm_;
contact[i].surface.mu = mu_;
}
if (int numc = dCollide(o0, o1, MAX_CONTACTS, &contact[0].geom, sizeof(dContact)))
{
// Get the dynamics body for each geom
dBodyID b1 = dGeomGetBody(o0);
dBodyID b2 = dGeomGetBody(o1);
// To add each contact point found to our joint group we call dJointCreateContact which is just one of the many
// different joint types available.
for (int i = 0; i < numc; i++)
{
// dJointCreateContact needs to know which world and joint group to work with as well as the dContact
// object itself. It returns a new dJointID which we then use with dJointAttach to finally create the
// temporary contact joint between the two geom bodies.
dJointID c = dJointCreateContact(world_, contactgroup_, contact + i);
dJointAttach(c, b1, b2);
}
}
}
//------------------------------------------------------------------------------
void SpatialIntegratorODE::computeForces(bool lowFriction)
{
int n = cellsList_.size();
double cellLength0 = simulatorPtr_->cellCollection_[0].getCellLength0();
float cutoff = 5.0*cellLength0;
float forceMax = interactionForce_;
float center[3] = {0.0, 0.0, 0.0};
float viscousCoefficient;
float rotationalViscousCoefficient;
if (!lowFriction)
{
viscousCoefficient = viscousCoefficient_;
rotationalViscousCoefficient = rotationalViscousCoefficient_;
} else {
viscousCoefficient = 0.05*viscousCoefficient_;
rotationalViscousCoefficient = 0.05*rotationalViscousCoefficient_;
}
for (int i=0; i<n; ++i)
{
TinyVector<double,3> pos_i = cellsList_.at(i)->getPositionODE();
// Interaction force.
/*
for (int j=0; j<i; ++j)
{
TinyVector<double,3> pos_j = cellsList_.at(j)->getPosition();
float force[3], x[3], r, r2;
r2 = 0.0;
for (int k=0; k<3; ++k)
{
x[k] = pos_j[k] - pos_i[k];
r2 += x[k]*x[k];
}
r = sqrt(r2);
for (int k=0; k<3; ++k)
{
x[k] /= r;
if (r > cutoff)
{
force[k] = 0.0;
} else {
force[k] = (forceMax/cutoff)*(cutoff - r) * x[k];
}
}
dBodyAddForce(cellsList_.at(i)->getBody(), force[0], force[1], force[2]);
dBodyAddForce(cellsList_.at(j)->getBody(), -force[0], -force[1], -force[2]);
}
*/
// Central force.
{
float force[3], x[3], r, r2;
r2 = 0.0;
for (int k=0; k<3; ++k)
{
x[k] = center[k] - pos_i(k);
r2 += x[k]*x[k];
}
r = sqrt(r2);
for (int k=0; k<3; ++k)
{
if (r != 0)
{
x[k] /= r;
} else {
x[k] = 0.0;
}
//force[k] = max(1.0,r/(colonySize/4.0)) * centralForce_ * x[k];
force[k] = centralForce_ * x[k];
//force[k] = max(0.0, cos((3.141592654*r)/(2.0*20.0*cellLength0))) * centralForce_ * x[k];
}
dBodyAddForce(cellsList_[i]->getBody(), force[0], force[1], 0.0);
}
// Viscous force.
{
float force[3];
const dReal *velocity = dBodyGetLinearVel(cellsList_.at(i)->getBody());
for (int k=0; k<3; ++k)
{
force[k] = - viscousCoefficient * velocity[k];
}
dBodyAddForce(cellsList_[i]->getBody(), force[0], force[1], 0.0);
}
// Rotational viscous force.
{
float torque[3];
const dReal *angularVelocity = dBodyGetAngularVel(cellsList_.at(i)->getBody());
torque[0] = 0.0;
torque[1] = 0.0;
torque[2] = - rotationalViscousCoefficient* angularVelocity[2];
dBodyAddTorque(cellsList_[i]->getBody(), torque[0], torque[1], torque[2]);
}
// Brownian force
// if (lowFriction)
// {
// {
// float force[3];
// for (int k=0; k<3; ++k)
// {
// force[k] = brownianForce_ * RandomNumberGenerator::getNormal();
// }
// dBodyAddForce(cellsList_[i]->getBody(), force[0], force[1], 0.0);
// }
// }
}
}
//------------------------------------------------------------------------------
TinyVector<double,4> SpatialIntegratorODE::computeColonyCenterAndRadius()
{
TinyVector<double,4> centerAndRadius;
int n = cellsList_.size();
double r2Sum = 0.0;
double colonyRadius;
// Compute the center of mass
TinyVector<double,3> center = (0.0, 0.0, 0.0);
QList<GraphicsCellODE*>::const_iterator it;
for (it = cellsList_.constBegin(); it != cellsList_.constEnd(); ++it)
{
TinyVector<double,3> pos = (*it)->getPositionODE();
center += pos;
}
center /= n;
centerAndRadius(0) = center(0);
centerAndRadius(1) = center(1);
centerAndRadius(2) = center(2);
// Compute the radius of gyration
double radius = 0.0;
for (it = cellsList_.constBegin(); it != cellsList_.constEnd(); ++it)
{
TinyVector<double,3> pos = (*it)->getPositionODE();
radius += sum( (pos - center)*(pos - center) );
}
radius = sqrt(radius/n);
centerAndRadius(3) = radius;
return centerAndRadius;
}
//------------------------------------------------------------------------------