-
Notifications
You must be signed in to change notification settings - Fork 299
/
ParticlesRepulsionForceField.inl
217 lines (184 loc) · 7.54 KB
/
ParticlesRepulsionForceField.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
/******************************************************************************
* 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_PARTICLESREPULSIONFORCEFIELD_INL
#define SOFA_COMPONENT_FORCEFIELD_PARTICLESREPULSIONFORCEFIELD_INL
#include <SofaSphFluid/ParticlesRepulsionForceField.h>
#include <sofa/core/visual/VisualParams.h>
#include <SofaSphFluid/SpatialGridContainer.inl>
#include <cmath>
#include <iostream>
namespace sofa::component::forcefield
{
template<class DataTypes>
ParticlesRepulsionForceField<DataTypes>::ParticlesRepulsionForceField()
: distance (initData(&distance, Real(1) , "distance", "Distance to maintain between particles"))
, stiffness(initData(&stiffness, Real(100), "stiffness", "Stiffness"))
, damping (initData(&damping , Real(0.1), "damping", "Damping"))
, grid(nullptr)
{
}
template<class DataTypes>
void ParticlesRepulsionForceField<DataTypes>::init()
{
this->Inherit::init();
this->getContext()->get(grid); //new Grid(distance.getValue());
if (grid==nullptr)
msg_error()<<"SpatialGridContainer not found by ParticlesRepulsionForceField, slow O(n2) method will be used !!!";
}
template<class DataTypes>
void ParticlesRepulsionForceField<DataTypes>::addForce(const core::MechanicalParams* /* mparams */, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v)
{
VecDeriv& f = *d_f.beginEdit();
const VecCoord& x = d_x.getValue();
const VecDeriv& v = d_v.getValue();
const Real h = distance.getValue();
const Real h2 = h*h;
const Real ks = stiffness.getValue();
const Real kd = damping.getValue();
const size_t n = x.size();
// Initialization
f.resize(n);
particles.resize(n);
for (size_t i=0; i<n; i++)
{
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 (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)
particles[i].neighbors.push_back(j);
}
}
}
else
{
grid->updateGrid(x);
grid->findNeighbors(this, h);
}
// Compute the forces
for (size_t i=0; i<n; i++)
{
Particle& Pi = particles[i];
for (typename std::vector< int >::const_iterator it = Pi.neighbors.begin(); it != Pi.neighbors.end(); ++it)
{
const int j = *it;
Deriv force;
Deriv n = (x[j]-x[i]);
Real d2 = n.norm2();
if (d2!=0.0 && d2 < h2)
{
Deriv vi = v[j]-v[i];
Real d = sqrt(d2);
n /= d;
d -= h;
Real forceIntensity = ks*d;
Real dampingIntensity = kd*d;
force = n*forceIntensity - vi*dampingIntensity;
}
f[i] += force;
f[j] -= force;
}
}
d_f.endEdit();
}
template<class DataTypes>
void ParticlesRepulsionForceField<DataTypes>::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df, const DataVecDeriv& d_dx)
{
VecDeriv& df = *d_df.beginEdit();
const VecDeriv& dx = d_dx.getValue();
const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
const Real h = distance.getValue();
const Real h2 = h*h;
const Real ks = (Real)(stiffness.getValue() * sofa::core::mechanicalparams::kFactorIncludingRayleighDamping(mparams, this->rayleighStiffness.getValue()));
//const Real kd = damping.getValue()*bFactor;
const size_t n = x.size();
df.resize(dx.size());
// Compute the forces
for (size_t i=0; i<n; i++)
{
Particle& Pi = particles[i];
for (typename std::vector< int >::const_iterator it = Pi.neighbors.begin(); it != Pi.neighbors.end(); ++it)
{
const int j = *it;
Deriv dforce;
Deriv n = (x[j]-x[i]);
Real d2 = n.norm2();
if (d2!=0.0 && d2 < h2)
{
Deriv dxi = dx[j]-dx[i];
dforce = n*((ks * dot(dxi,n))/d2);
}
df[i] += dforce;
df[j] -= dforce;
}
}
d_df.endEdit();
}
template<class DataTypes>
void ParticlesRepulsionForceField<DataTypes>::addKToMatrix(const sofa::core::MechanicalParams* /*mparams*/, const sofa::core::behavior::MultiMatrixAccessor* /*matrix*/)
{
return;
}
template<class DataTypes>
void ParticlesRepulsionForceField<DataTypes>::draw(const core::visual::VisualParams* vparams)
{
if (!vparams->displayFlags().getShowForceFields() && !vparams->displayFlags().getShowInteractionForceFields())
return;
const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle();
vparams->drawTool()->disableLighting();
const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
const Real h = distance.getValue();
std::vector<sofa::type::RGBAColor> colorVector;
std::vector<sofa::type::Vec3> vertices;
for (unsigned int i=0; i<particles.size(); i++)
{
Particle& Pi = particles[i];
for (typename std::vector< int >::const_iterator it = Pi.neighbors.begin(); it != Pi.neighbors.end(); ++it)
{
const int j = *it;
const float r_h = (float)((x[j]-x[i]).norm() / h);
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);
}
} // namespace sofa::component::forcefield
#endif // SOFA_COMPONENT_FORCEFIELD_PARTICLESREPULSIONFORCEFIELD_INL