-
Notifications
You must be signed in to change notification settings - Fork 299
/
SPHFluidForceField.inl
555 lines (494 loc) · 20.1 KB
/
SPHFluidForceField.inl
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_COMPONENT_FORCEFIELD_SPHFLUIDFORCEFIELD_INL
#define SOFA_COMPONENT_FORCEFIELD_SPHFLUIDFORCEFIELD_INL
#include <SofaSphFluid/SPHFluidForceField.h>
#include <sofa/core/visual/VisualParams.h>
#include <SofaSphFluid/SpatialGridContainer.inl>
#include <cmath>
#include <iostream>
#include <sofa/helper/AdvancedTimer.h>
namespace sofa::component::forcefield
{
template<class DataTypes>
SPHFluidForceField<DataTypes>::SPHFluidForceField()
: d_particleRadius (initData(&d_particleRadius, Real(1), "radius", "Radius of a Particle"))
, d_particleMass (initData(&d_particleMass, Real(1), "mass", "Mass of a Particle"))
, d_pressureStiffness (initData(&d_pressureStiffness, Real(100), "pressure", "Pressure"))
, d_density0 (initData(&d_density0, Real(1), "density", "Density"))
, d_viscosity (initData(&d_viscosity, Real(0.001f), "viscosity", "Viscosity"))
, d_surfaceTension (initData(&d_surfaceTension, Real(0), "surfaceTension", "Surface Tension"))
, d_kernelType(initData(&d_kernelType, 0, "kernelType", "0 = default kernels, 1 = cubic spline"))
, d_pressureType(initData(&d_pressureType, 1, "pressureType", "0 = none, 1 = default pressure"))
, d_viscosityType(initData(&d_viscosityType, 1, "viscosityType", "0 = none, 1 = default d_viscosity using kernel Laplacian, 2 = artificial d_viscosity"))
, d_surfaceTensionType(initData(&d_surfaceTensionType, 1, "surfaceTensionType", "0 = none, 1 = default surface tension using kernel Laplacian, 2 = cohesion forces surface tension from Becker et al. 2007"))
, d_debugGrid(initData(&d_debugGrid, false, "debugGrid", "If true will store additionnal information on the grid to check neighbors and draw them"))
, m_grid(nullptr)
{
}
template<class DataTypes>
void SPHFluidForceField<DataTypes>::init()
{
this->Inherit::init();
SPHKernel<SPH_KERNEL_CUBIC, Deriv> Kcubic(4);
{
std::ostringstream ossInfo;
std::ostringstream ossError;
if (!Kcubic.CheckAll(2, ossInfo, ossError))
{
msg_info_when(ossInfo.str().empty(), this) << ossInfo.str();
msg_error_when(ossError.str().empty(), this) << ossError.str();
}
}
SPHKernel<SPH_KERNEL_DEFAULT_DENSITY,Deriv> Kd(4);
{
std::ostringstream ossInfo;
std::ostringstream ossError;
if (!Kd.CheckAll(2, ossInfo, ossError))
{
msg_info_when(ossInfo.str().empty(), this) << ossInfo.str();
msg_error_when(ossError.str().empty(), this) << ossError.str();
}
}
SPHKernel<SPH_KERNEL_DEFAULT_PRESSURE,Deriv> Kp(4);
{
std::ostringstream ossInfo;
std::ostringstream ossError;
if (!Kp.CheckAll(1, ossInfo, ossError))
{
msg_info_when(ossInfo.str().empty(), this) << ossInfo.str();
msg_error_when(ossError.str().empty(), this) << ossError.str();
}
}
SPHKernel<SPH_KERNEL_DEFAULT_VISCOSITY,Deriv> Kv(4);
{
std::ostringstream ossInfo;
std::ostringstream ossError;
if (!Kv.CheckAll(2, ossInfo, ossError))
{
msg_info_when(ossInfo.str().empty(), this) << ossInfo.str();
msg_error_when(ossError.str().empty(), this) << ossError.str();
}
}
this->getContext()->get(m_grid); //new Grid(d_particleRadius.getValue());
if (m_grid==nullptr)
msg_error() << "SpatialGridContainer not found by SPHFluidForceField, slow O(n2) method will be used !!!";
size_t n = this->mstate->getSize();
m_particles.resize(n);
for (unsigned i=0u; i<n; i++)
{
m_particles[i].neighbors.clear();
m_particles[i].density = d_density0.getValue();
m_particles[i].pressure = 0;
m_particles[i].normal.clear();
m_particles[i].curvature = 0;
}
if (d_debugGrid.getValue())
{
for (unsigned i = 0u; i < n; i++)
{
m_particles[i].neighbors2.clear();
}
}
m_lastTime = (Real)this->getContext()->getTime();
}
template<class DataTypes>
void SPHFluidForceField<DataTypes>::addForce(const core::MechanicalParams* mparams, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v)
{
computeNeighbors(mparams, d_x, d_v);
switch(d_kernelType.getValue())
{
default:
msg_error() << "Unsupported d_kernelType " << d_kernelType.getValue();
// fallthrough
case 0: // default
{
computeForce <SPHKernel<SPH_KERNEL_DEFAULT_DENSITY,Deriv>,
SPHKernel<SPH_KERNEL_DEFAULT_PRESSURE,Deriv>,
SPHKernel<SPH_KERNEL_DEFAULT_VISCOSITY,Deriv>,
SPHKernel<SPH_KERNEL_DEFAULT_DENSITY,Deriv> > (mparams, d_f, d_x, d_v);
break;
}
case 1: // cubic
{
computeForce <SPHKernel<SPH_KERNEL_CUBIC,Deriv>,
SPHKernel<SPH_KERNEL_CUBIC,Deriv>,
SPHKernel<SPH_KERNEL_CUBIC,Deriv>,
SPHKernel<SPH_KERNEL_CUBIC,Deriv> > (mparams, d_f, d_x, d_v);
break;
}
}
msg_info() << "density[" << 0 << "] = " << m_particles[0].density << "(" << m_particles[0].neighbors.size() << " neighbors)"
<< "density[" << m_particles.size()/2 << "] = " << m_particles[m_particles.size()/2].density ;
}
template<class DataTypes>
void SPHFluidForceField<DataTypes>::computeNeighbors(const core::MechanicalParams* /*mparams*/, const DataVecCoord& d_x, const DataVecDeriv& /*d_v*/)
{
helper::ReadAccessor<DataVecCoord> x = d_x;
const Real h = d_particleRadius.getValue();
const Real h2 = h*h;
size_t n = x.size();
m_particles.resize(n);
for (size_t i=0; i<n; i++) {
m_particles[i].neighbors.clear();
}
// First compute the neighbors
// This is an O(n2) step, except if a hash-grid is used to optimize it
if (m_grid == nullptr)
{
for (size_t i = 0; i<n; i++)
{
const Coord& ri = x[i];
for (size_t j = i + 1; j<n; j++)
{
const Coord& rj = x[j];
Real r2 = (rj - ri).norm2();
if (r2 < h2)
{
Real r_h = (Real)sqrt(r2 / h2);
m_particles[i].neighbors.push_back(std::make_pair(j, r_h));
//m_particles[j].neighbors.push_back(std::make_pair(i,r_h));
}
}
}
}
else
{
m_grid->updateGrid(x.ref());
m_grid->findNeighbors(this, h);
if (!d_debugGrid.getValue())
return;
for (size_t i = 0; i < n; i++) {
m_particles[i].neighbors2.clear();
}
// Check grid info
for (size_t i=0; i<n; i++)
{
const Coord& ri = x[i];
for (size_t j=i+1; j<n; j++)
{
const Coord& rj = x[j];
Real r2 = (rj-ri).norm2();
if (r2 < h2)
{
Real r_h = (Real)sqrt(r2/h2);
m_particles[i].neighbors2.push_back(std::make_pair(j,r_h));
}
}
}
for (size_t i=0; i<n; i++)
{
if (m_particles[i].neighbors.size() != m_particles[i].neighbors2.size())
{
msg_error() << "particle "<<i<<" "<< x[i] <<" : "<<m_particles[i].neighbors.size()<<" neighbors on grid, "<< m_particles[i].neighbors2.size() << " neighbors on bruteforce.";
msg_error() << "grid-only neighbors:";
for (unsigned int j=0; j<m_particles[i].neighbors.size(); j++)
{
int index = m_particles[i].neighbors[j].first;
unsigned int j2 = 0;
while (j2 < m_particles[i].neighbors2.size() && m_particles[i].neighbors2[j2].first != index)
++j2;
if (j2 == m_particles[i].neighbors2.size())
msg_error() << " "<< x[index] << "<"<< m_particles[i].neighbors[j].first<<","<<m_particles[i].neighbors[j].second<<">";
}
msg_error() << "";
msg_error() << "bruteforce-only neighbors:";
for (unsigned int j=0; j<m_particles[i].neighbors2.size(); j++)
{
int index = m_particles[i].neighbors2[j].first;
unsigned int j2 = 0;
while (j2 < m_particles[i].neighbors.size() && m_particles[i].neighbors[j2].first != index)
++j2;
if (j2 == m_particles[i].neighbors.size())
msg_error() << " "<< x[index] << "<"<< m_particles[i].neighbors2[j].first<<","<<m_particles[i].neighbors2[j].second<<">";
}
msg_error() << "";
}
}
}
}
template<class DataTypes> template<class TKd, class TKp, class TKv, class TKc>
void SPHFluidForceField<DataTypes>::computeForce(const core::MechanicalParams* /* mparams */, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v)
{
helper::WriteAccessor<DataVecDeriv> f = d_f;
helper::ReadAccessor<DataVecCoord> x = d_x;
helper::ReadAccessor<DataVecDeriv> v = d_v;
const Real h = d_particleRadius.getValue();
const Real h2 = h*h;
const Real m = d_particleMass.getValue();
const Real m2 = m*m;
const Real d0 = d_density0.getValue();
const Real k = d_pressureStiffness.getValue();
const Real time = (Real)this->getContext()->getTime();
const Real viscosity = d_viscosity.getValue();
const int viscosityT = (viscosity == 0) ? 0 : d_viscosityType.getValue();
const Real surfaceTension = d_surfaceTension.getValue();
const int surfaceTensionT = (surfaceTension <= 0) ? 0 : d_surfaceTensionType.getValue();
//const Real dt = (Real)this->getContext()->getDt();
m_lastTime = time;
size_t n = x.size();
// Initialization
f.resize(n);
dforces.clear();
//int n0 = m_particles.size();
m_particles.resize(n);
for (size_t i=0; i<n; i++)
{
m_particles[i].density = 0;
m_particles[i].pressure = 0;
m_particles[i].normal.clear();
m_particles[i].curvature = 0;
}
TKd Kd(h);
TKp Kp(h);
TKv Kv(h);
TKc Kc(h);
// Compute density and pressure
for (size_t i=0; i<n; i++)
{
Particle& Pi = m_particles[i];
Real density = Pi.density;
density += m*Kd.W(0); // density from current particle
for (typename std::vector< std::pair<int,Real> >::const_iterator it = Pi.neighbors.begin(); it != Pi.neighbors.end(); ++it)
{
const int j = it->first;
const Real r_h = it->second;
Particle& Pj = m_particles[j];
Real d = m*Kd.W(r_h);
density += d;
Pj.density += d;
}
Pi.density = density;
Pi.pressure = k*(density - d0);
}
// Compute surface normal and curvature
if (surfaceTensionT == 1)
{
for (size_t i=0; i<n; i++)
{
Particle& Pi = m_particles[i];
for (typename std::vector< std::pair<int,Real> >::const_iterator it = Pi.neighbors.begin(); it != Pi.neighbors.end(); ++it)
{
const int j = it->first;
const Real r_h = it->second;
Particle& Pj = m_particles[j];
Deriv n = Kc.gradW(x[i]-x[j],r_h) * (m / Pj.density - m / Pi.density);
Pi.normal += n;
Pj.normal -= n;
Real c = Kc.laplacianW(r_h) * (m / Pj.density - m / Pi.density);
Pi.curvature += c;
Pj.curvature -= c;
}
}
}
// Compute the forces
for (size_t i = 0; i < n; i++)
{
const Particle& Pi = m_particles[i];
// Gravity
//f[i] += g*(m*Pi.density);
for (auto it = Pi.neighbors.begin(); it != Pi.neighbors.end(); ++it)
{
const int j = it->first;
const Real r_h = it->second;
const Particle& Pj = m_particles[j];
// Pressure
Real pressureFV = ( - m2 * (Pi.pressure / (Pi.density*Pi.density) + Pj.pressure / (Pj.density*Pj.density)) );
// Viscosity
switch(viscosityT)
{
case 0: break;
case 1:
{
Deriv fd_viscosity = ( v[j] - v[i] ) * ( m2 * viscosity / (Pi.density * Pj.density) * Kv.laplacianW(r_h) );
f[i] += fd_viscosity;
f[j] -= fd_viscosity;
break;
}
case 2:
{
Real vx = dot(v[i]-v[j],x[i]-x[j]);
if (vx < 0)
{
pressureFV += (vx * viscosity * h * m / ((r_h*r_h + 0.01f*h2)*(Pi.density+Pj.density)*0.5f));
}
break;
}
default:
break;
}
Deriv fpressure = Kp.gradW(x[i]-x[j],r_h) * pressureFV;
f[i] += fpressure;
f[j] -= fpressure;
}
switch(surfaceTensionT)
{
case 0: break;
case 1:
{
Real n = Pi.normal.norm();
if (n > 0.000001)
{
Deriv fsurface = Pi.normal * ( - m * surfaceTension * Pi.curvature / n );
f[i] += fsurface;
}
break;
}
case 2:
{
break;
}
default:
break;
}
}
}
template<class DataTypes>
void SPHFluidForceField<DataTypes>::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df, const DataVecDeriv& d_dx)
{
VecDeriv& f1 = *d_df.beginEdit();
const VecDeriv& dx1 = d_dx.getValue();
Real kFactor = (Real)sofa::core::mechanicalparams::kFactorIncludingRayleighDamping(mparams, this->rayleighStiffness.getValue());
const VecCoord& p1 = this->mstate->read(core::ConstVecCoordId::position())->getValue();
f1.resize(dx1.size());
for (unsigned int i=0; i<this->dforces.size(); i++)
{
const DForce& df = this->dforces[i];
const unsigned int ia = df.a;
const unsigned int ib = df.b;
const Deriv u = p1[ib]-p1[ia];
const Deriv du = dx1[ib]-dx1[ia];
const Deriv dforce = u * (df.df * (du*u) * kFactor);
f1[ia] += dforce * kFactor;
f1[ib] -= dforce * kFactor;
}
d_df.endEdit();
}
template <class DataTypes>
SReal SPHFluidForceField<DataTypes>::getPotentialEnergy(const core::MechanicalParams* /* mparams */, const DataVecCoord& /* d_x */) const
{
msg_error() << "getPotentialEnergy-not-implemented !!!";
return 0;
}
template<class DataTypes>
void SPHFluidForceField<DataTypes>::draw(const core::visual::VisualParams* vparams)
{
if (!vparams->displayFlags().getShowForceFields())
return;
const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle();
vparams->drawTool()->disableLighting();
vparams->drawTool()->enableBlending();
vparams->drawTool()->disableDepthTest();
const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
std::vector<sofa::type::RGBAColor> colorVector;
std::vector<sofa::type::Vec3> vertices;
if (d_debugGrid.getValue())
{
for (unsigned int i = 0; i < m_particles.size(); i++)
{
Particle& Pi = m_particles[i];
if (Pi.neighbors.size() != Pi.neighbors2.size())
{
colorVector.push_back(sofa::type::RGBAColor::red());
for (unsigned int j = 0; j < Pi.neighbors.size(); j++)
{
int index = Pi.neighbors[j].first;
unsigned int j2 = 0;
while (j2 < Pi.neighbors2.size() && Pi.neighbors2[j2].first != index)
++j2;
if (j2 == Pi.neighbors2.size())
{
vertices.push_back(sofa::type::Vec3(x[i]));
vertices.push_back(sofa::type::Vec3(x[index]));
}
}
vparams->drawTool()->drawLines(vertices, 1, colorVector[0]);
vertices.clear();
colorVector.clear();
colorVector.push_back(sofa::type::RGBAColor::magenta());
for (unsigned int j = 0; j < Pi.neighbors2.size(); j++)
{
int index = Pi.neighbors2[j].first;
unsigned int j2 = 0;
while (j2 < Pi.neighbors.size() && Pi.neighbors[j2].first != index)
++j2;
if (j2 == Pi.neighbors.size())
{
vertices.push_back(sofa::type::Vec3(x[i]));
vertices.push_back(sofa::type::Vec3(x[index]));
}
}
vparams->drawTool()->drawLines(vertices, 1, colorVector[0]);
vertices.clear();
colorVector.clear();
}
}
}
else
{
for (unsigned int i = 0; i < m_particles.size(); i++)
{
Particle& Pi = m_particles[i];
for (typename std::vector< std::pair<int, Real> >::const_iterator it = Pi.neighbors.begin(); it != Pi.neighbors.end(); ++it)
{
const int j = it->first;
const float r_h = (float)it->second;
float f = r_h * 2;
if (f < 1)
{
colorVector.push_back({0.0f, 1.0f - f, f, 1.0f - r_h});
}
else
{
colorVector.push_back({f - 1.0f, 0.0f, 2.0f - f, 1.0f - r_h});
}
vertices.push_back(sofa::type::Vec3(x[i]));
vertices.push_back(sofa::type::Vec3(x[j]));
}
vparams->drawTool()->drawLines(vertices, 1, colorVector);
vertices.clear();
colorVector.clear();
}
}
vparams->drawTool()->disableBlending();
vparams->drawTool()->enableDepthTest();
for (unsigned int i=0; i<m_particles.size(); i++)
{
Particle& Pi = m_particles[i];
float f = (float)(Pi.density / d_density0.getValue());
f = 1+10*(f-1);
if (f < 1)
{
colorVector.push_back({0.0f, 1.0f - f, f, 1.0f});
}
else
{
colorVector.push_back( { f - 1.0f, 0.0f, 2.0f - f, 1.0f});
}
vertices.push_back(sofa::type::Vec3(x[i]));
}
vparams->drawTool()->drawPoints(vertices,5,colorVector);
vertices.clear();
colorVector.clear();
}
} // namespace sofa::component::forcefield
#endif // SOFA_COMPONENT_FORCEFIELD_SPHFLUIDFORCEFIELD_INL