SPHKernels-Impl.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 // Adopted from the sample code of:
12 // Bart Adams and Martin Wicke,
13 // "Meshless Approximation Methods and Applications in Physics Based Modeling
14 // and Animation", Eurographics 2009 Tutorial
15 
16 #ifndef CUBBYFLOW_SPH_KERNELS_IMPL_HPP
17 #define CUBBYFLOW_SPH_KERNELS_IMPL_HPP
18 
19 namespace CubbyFlow
20 {
21 inline SPHStdKernel2::SPHStdKernel() : h(0), h2(0), h3(0), h4(0)
22 {
23  // Do nothing
24 }
25 
26 inline SPHStdKernel2::SPHStdKernel(double kernelRadius)
27  : h(kernelRadius), h2(h * h), h3(h2 * h), h4(h2 * h2)
28 {
29  // Do nothing
30 }
31 
32 inline double SPHStdKernel2::operator()(double distance) const
33 {
34  const double distanceSquared = distance * distance;
35 
36  if (distanceSquared >= h2)
37  {
38  return 0.0;
39  }
40 
41  const double x = 1.0 - distanceSquared / h2;
42  return 4.0 / (PI_DOUBLE * h2) * x * x * x;
43 }
44 
45 inline double SPHStdKernel2::FirstDerivative(double distance) const
46 {
47  if (distance >= h)
48  {
49  return 0.0;
50  }
51 
52  const double x = 1.0 - distance * distance / h2;
53  return -24.0 * distance / (PI_DOUBLE * h4) * x * x;
54 }
55 
56 inline double SPHStdKernel2::SecondDerivative(double distance) const
57 {
58  const double distanceSquared = distance * distance;
59 
60  if (distanceSquared >= h2)
61  {
62  return 0.0;
63  }
64 
65  const double x = distanceSquared / h2;
66  return 24.0 / (PI_DOUBLE * h4) * (1 - x) * (5 * x - 1);
67 }
68 
69 inline Vector2D SPHStdKernel2::Gradient(const Vector2D& point) const
70 {
71  if (const double dist = point.Length(); dist > 0.0)
72  {
73  return Gradient(dist, point / dist);
74  }
75 
76  return Vector2D{ 0, 0 };
77 }
78 
79 inline Vector2D SPHStdKernel2::Gradient(double distance,
80  const Vector2D& directionToCenter) const
81 {
82  return -FirstDerivative(distance) * directionToCenter;
83 }
84 
85 inline SPHStdKernel3::SPHStdKernel() : h(0), h2(0), h3(0), h5(0)
86 {
87  // Do nothing
88 }
89 
90 inline SPHStdKernel3::SPHStdKernel(double kernelRadius)
91  : h(kernelRadius), h2(h * h), h3(h2 * h), h5(h2 * h3)
92 {
93  // Do nothing
94 }
95 
96 inline double SPHStdKernel3::operator()(double distance) const
97 {
98  if (distance * distance >= h2)
99  {
100  return 0.0;
101  }
102 
103  const double x = 1.0 - distance * distance / h2;
104  return 315.0 / (64.0 * PI_DOUBLE * h3) * x * x * x;
105 }
106 
107 inline double SPHStdKernel3::FirstDerivative(double distance) const
108 {
109  if (distance >= h)
110  {
111  return 0.0;
112  }
113 
114  const double x = 1.0 - distance * distance / h2;
115  return -945.0 / (32.0 * PI_DOUBLE * h5) * distance * x * x;
116 }
117 
118 inline double SPHStdKernel3::SecondDerivative(double distance) const
119 {
120  if (distance * distance >= h2)
121  {
122  return 0.0;
123  }
124 
125  const double x = distance * distance / h2;
126  return 945.0 / (32.0 * PI_DOUBLE * h5) * (1 - x) * (5 * x - 1);
127 }
128 
129 inline Vector3D SPHStdKernel3::Gradient(const Vector3D& point) const
130 {
131  if (const double dist = point.Length(); dist > 0.0)
132  {
133  return Gradient(dist, point / dist);
134  }
135 
136  return Vector3D{ 0, 0, 0 };
137 }
138 
139 inline Vector3D SPHStdKernel3::Gradient(double distance,
140  const Vector3D& directionToCenter) const
141 {
142  return -FirstDerivative(distance) * directionToCenter;
143 }
144 
145 inline SPHSpikyKernel2::SPHSpikyKernel() : h(0), h2(0), h3(0), h4(0), h5(0)
146 {
147  // Do nothing
148 }
149 
150 inline SPHSpikyKernel2::SPHSpikyKernel(double kernelRadius)
151  : h(kernelRadius), h2(h * h), h3(h2 * h), h4(h2 * h2), h5(h3 * h2)
152 {
153  // Do nothing
154 }
155 
156 inline double SPHSpikyKernel2::operator()(double distance) const
157 {
158  if (distance >= h)
159  {
160  return 0.0;
161  }
162 
163  const double x = 1.0 - distance / h;
164  return 10.0 / (PI_DOUBLE * h2) * x * x * x;
165 }
166 
167 inline double SPHSpikyKernel2::FirstDerivative(double distance) const
168 {
169  if (distance >= h)
170  {
171  return 0.0;
172  }
173 
174  const double x = 1.0 - distance / h;
175  return -30.0 / (PI_DOUBLE * h3) * x * x;
176 }
177 
178 inline double SPHSpikyKernel2::SecondDerivative(double distance) const
179 {
180  if (distance >= h)
181  {
182  return 0.0;
183  }
184 
185  const double x = 1.0 - distance / h;
186  return 60.0 / (PI_DOUBLE * h4) * x;
187 }
188 
189 inline Vector2D SPHSpikyKernel2::Gradient(const Vector2D& point) const
190 {
191  if (const double dist = point.Length(); dist > 0.0)
192  {
193  return Gradient(dist, point / dist);
194  }
195 
196  return Vector2D{ 0, 0 };
197 }
198 
200  double distance, const Vector2D& directionToCenter) const
201 {
202  return -FirstDerivative(distance) * directionToCenter;
203 }
204 
205 inline SPHSpikyKernel3::SPHSpikyKernel() : h(0), h2(0), h3(0), h4(0), h5(0)
206 {
207  // Do nothing
208 }
209 
210 inline SPHSpikyKernel3::SPHSpikyKernel(double kernelRadius)
211  : h(kernelRadius), h2(h * h), h3(h2 * h), h4(h2 * h2), h5(h3 * h2)
212 {
213  // Do nothing
214 }
215 
216 inline double SPHSpikyKernel3::operator()(double distance) const
217 {
218  if (distance >= h)
219  {
220  return 0.0;
221  }
222 
223  const double x = 1.0 - distance / h;
224  return 15.0 / (PI_DOUBLE * h3) * x * x * x;
225 }
226 
227 inline double SPHSpikyKernel3::FirstDerivative(double distance) const
228 {
229  if (distance >= h)
230  {
231  return 0.0;
232  }
233 
234  const double x = 1.0 - distance / h;
235  return -45.0 / (PI_DOUBLE * h4) * x * x;
236 }
237 
238 inline double SPHSpikyKernel3::SecondDerivative(double distance) const
239 {
240  if (distance >= h)
241  {
242  return 0.0;
243  }
244 
245  const double x = 1.0 - distance / h;
246  return 90.0 / (PI_DOUBLE * h5) * x;
247 }
248 
249 inline Vector3D SPHSpikyKernel3::Gradient(const Vector3D& point) const
250 {
251  if (const double dist = point.Length(); dist > 0.0)
252  {
253  return Gradient(dist, point / dist);
254  }
255 
256  return Vector3D{ 0, 0, 0 };
257 }
258 
260  double distance, const Vector3D& directionToCenter) const
261 {
262  return -FirstDerivative(distance) * directionToCenter;
263 }
264 } // namespace CubbyFlow
265 
266 #endif
SPHSpikyKernel()
Constructs a kernel object with zero radius.
Definition: SPHKernels-Impl.hpp:145
double operator()(double distance) const
Returns kernel function value at given distance.
Definition: SPHKernels-Impl.hpp:32
double SecondDerivative(double distance) const
Returns the second derivative at given distance.
Definition: SPHKernels-Impl.hpp:238
constexpr double PI_DOUBLE
Double-type PI.
Definition: Constants.hpp:77
Vector3D Gradient(const Vector3D &point) const
Returns the gradient at a point.
Definition: SPHKernels-Impl.hpp:249
double SecondDerivative(double distance) const
Returns the second derivative at given distance.
Definition: SPHKernels-Impl.hpp:178
Vector3D Gradient(const Vector3D &point) const
Returns the gradient at a point.
Definition: SPHKernels-Impl.hpp:129
double FirstDerivative(double distance) const
Returns the first derivative at given distance.
Definition: SPHKernels-Impl.hpp:227
ValueType Length() const
Definition: MatrixExpression-Impl.hpp:278
Vector2D Gradient(const Vector2D &point) const
Returns the gradient at a point.
Definition: SPHKernels-Impl.hpp:189
double operator()(double distance) const
Returns kernel function value at given distance.
Definition: SPHKernels-Impl.hpp:216
Definition: Matrix.hpp:27
SPHStdKernel()
Constructs a kernel object with zero radius.
Definition: SPHKernels-Impl.hpp:85
SPHStdKernel()
Constructs a kernel object with zero radius.
Definition: SPHKernels-Impl.hpp:21
Definition: pybind11Utils.hpp:20
Vector2D Gradient(const Vector2D &point) const
Returns the gradient at a point.
Definition: SPHKernels-Impl.hpp:69
double SecondDerivative(double distance) const
Returns the second derivative at given distance.
Definition: SPHKernels-Impl.hpp:56
double FirstDerivative(double distance) const
Returns the first derivative at given distance.
Definition: SPHKernels-Impl.hpp:107
double FirstDerivative(double distance) const
Returns the first derivative at given distance.
Definition: SPHKernels-Impl.hpp:167
double operator()(double distance) const
Returns kernel function value at given distance.
Definition: SPHKernels-Impl.hpp:96
double operator()(double distance) const
Returns kernel function value at given distance.
Definition: SPHKernels-Impl.hpp:156
double FirstDerivative(double distance) const
Returns the first derivative at given distance.
Definition: SPHKernels-Impl.hpp:45
double SecondDerivative(double distance) const
Returns the second derivative at given distance.
Definition: SPHKernels-Impl.hpp:118
SPHSpikyKernel()
Constructs a kernel object with zero radius.
Definition: SPHKernels-Impl.hpp:205