CUDASPHKernels2-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 #ifndef CUBBYFLOW_CUDA_SPH_KERNELS2_IMPL_HPP
12 #define CUBBYFLOW_CUDA_SPH_KERNELS2_IMPL_HPP
13 
14 #ifdef CUBBYFLOW_USE_CUDA
15 
16 #include <Core/CUDA/CUDAUtils.hpp>
17 #include <Core/Utils/Constants.hpp>
18 
19 namespace CubbyFlow
20 {
21 inline CUDASPHStdKernel2::CUDASPHStdKernel2() : h(0), h2(0), h3(0), h4(0)
22 {
23  // Do nothing
24 }
25 
26 inline CUDASPHStdKernel2::CUDASPHStdKernel2(float kernelRadius)
27  : h(kernelRadius), h2(h * h), h3(h2 * h), h4(h2 * h2)
28 {
29  // Do nothing
30 }
31 
32 inline float CUDASPHStdKernel2::operator()(float distance) const
33 {
34  const float distanceSquared = distance * distance;
35 
36  if (distanceSquared >= h2)
37  {
38  return 0.0f;
39  }
40 
41  const float x = 1.0f - distanceSquared / h2;
42  return 4.0f / (PI_FLOAT * h2) * x * x * x;
43 }
44 
45 inline float CUDASPHStdKernel2::FirstDerivative(float distance) const
46 {
47  if (distance >= h)
48  {
49  return 0.0f;
50  }
51 
52  const float x = 1.0f - distance * distance / h2;
53  return -24.0f * distance / (PI_FLOAT * h4) * x * x;
54 }
55 
56 inline float CUDASPHStdKernel2::SecondDerivative(float distance) const
57 {
58  float distanceSquared = distance * distance;
59 
60  if (distanceSquared >= h2)
61  {
62  return 0.0f;
63  }
64 
65  const float x = distanceSquared / h2;
66  return 24.0f / (PI_FLOAT * h4) * (1 - x) * (5 * x - 1);
67 }
68 
69 inline float2 CUDASPHStdKernel2::Gradient(const float2& point) const
70 {
71  float dist = Length(point);
72 
73  if (dist > 0.0f)
74  {
75  return Gradient(dist, point / dist);
76  }
77 
78  return make_float2(0, 0);
79 }
80 
81 inline float2 CUDASPHStdKernel2::Gradient(float distance,
82  const float2& directionToCenter) const
83 {
84  return -FirstDerivative(distance) * directionToCenter;
85 }
86 
87 inline CUDASPHSpikyKernel2::CUDASPHSpikyKernel2()
88  : h(0), h2(0), h3(0), h4(0), h5(0)
89 {
90  // Do nothing
91 }
92 
93 inline CUDASPHSpikyKernel2::CUDASPHSpikyKernel2(float kernelRadius)
94  : h(kernelRadius), h2(h * h), h3(h2 * h), h4(h2 * h2), h5(h3 * h2)
95 {
96  // Do nothing
97 }
98 
99 inline float CUDASPHSpikyKernel2::operator()(float distance) const
100 {
101  if (distance >= h)
102  {
103  return 0.0f;
104  }
105 
106  const float x = 1.0f - distance / h;
107  return 10.0f / (PI_FLOAT * h2) * x * x * x;
108 }
109 
110 inline float CUDASPHSpikyKernel2::FirstDerivative(float distance) const
111 {
112  if (distance >= h)
113  {
114  return 0.0f;
115  }
116 
117  const float x = 1.0f - distance / h;
118  return -30.0f / (PI_FLOAT * h3) * x * x;
119 }
120 
121 inline float CUDASPHSpikyKernel2::SecondDerivative(float distance) const
122 {
123  if (distance >= h)
124  {
125  return 0.0f;
126  }
127 
128  const float x = 1.0f - distance / h;
129  return 60.0f / (PI_FLOAT * h4) * x;
130 }
131 
132 inline float2 CUDASPHSpikyKernel2::Gradient(const float2& point) const
133 {
134  float dist = Length(point);
135 
136  if (dist > 0.0f)
137  {
138  return Gradient(dist, point / dist);
139  }
140 
141  return make_float2(0, 0);
142 }
143 
144 inline float2 CUDASPHSpikyKernel2::Gradient(
145  float distance, const float2& directionToCenter) const
146 {
147  return -FirstDerivative(distance) * directionToCenter;
148 }
149 } // namespace CubbyFlow
150 
151 #endif
152 
153 #endif
Definition: pybind11Utils.hpp:20
constexpr float PI_FLOAT
Float-type PI.
Definition: Constants.hpp:75
size_t Length() const
Definition: ArrayBase-Impl.hpp:84