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
/*
* // Copyright (c) 2021 Feng Yang
* //
* // I am making my contributions/submissions to this project solely in my
* // personal capacity and am not conveying any rights to any intellectual
* // property of any third parties.
*/
use crate::vector2::Vector2D;
use num::traits::Pow;
///
/// # Standard 2-D SPH kernel function object.
///
/// \see Müller, Matthias, David Charypar, and Markus Gross.
/// "Particle-based fluid simulation for interactive applications."
/// Proceedings of the 2003 ACM SIGGRAPH/Eurographics symposium on Computer
/// animation. Eurographics Association, 2003.
///
pub struct SphStdKernel2 {
/// Kernel radius.
pub h: f64,
/// Square of the kernel radius.
pub h2: f64,
/// Cubic of the kernel radius.
pub h3: f64,
/// Fourth-power of the kernel radius.
pub h4: f64,
}
impl SphStdKernel2 {
/// Constructs a kernel object with zero radius.
pub fn new_default() -> SphStdKernel2 {
return SphStdKernel2 {
h: 0.0,
h2: 0.0,
h3: 0.0,
h4: 0.0,
};
}
/// Constructs a kernel object with given radius.
pub fn new(kernel_radius: f64) -> SphStdKernel2 {
return SphStdKernel2 {
h: kernel_radius,
h2: kernel_radius.pow(2),
h3: kernel_radius.pow(3),
h4: kernel_radius.pow(4),
};
}
/// Returns kernel function value at given distance.
pub fn apply(&self, distance: f64) -> f64 {
let distance_squared = distance * distance;
return if distance_squared >= self.h2 {
0.0
} else {
let x = 1.0 - distance_squared / self.h2;
4.0 / (crate::constants::K_PI_D * self.h2) * x * x * x
};
}
/// Returns the first derivative at given distance.
pub fn first_derivative(&self, distance: f64) -> f64 {
return if distance >= self.h {
0.0
} else {
let x = 1.0 - distance * distance / self.h2;
-24.0 * distance / (crate::constants::K_PI_D * self.h4) * x * x
};
}
/// Returns the gradient at a point.
pub fn gradient_pnt(&self, point: &Vector2D) -> Vector2D {
let dist = point.length();
return if dist > 0.0 {
self.gradient_dir(dist, &(*point / dist))
} else {
Vector2D::new(0.0, 0.0)
};
}
/// Returns the gradient at a point defined by distance and direction.
pub fn gradient_dir(&self, distance: f64, direction: &Vector2D) -> Vector2D {
return *direction * -self.first_derivative(distance);
}
/// Returns the second derivative at given distance.
pub fn second_derivative(&self, distance: f64) -> f64 {
let distance_squared = distance * distance;
return if distance_squared >= self.h2 {
0.0
} else {
let x = distance_squared / self.h2;
24.0 / (crate::constants::K_PI_D * self.h4) * (1.0 - x) * (5.0 * x - 1.0)
};
}
}
///
/// # Spiky 2-D SPH kernel function object.
///
/// \see Müller, Matthias, David Charypar, and Markus Gross.
/// "Particle-based fluid simulation for interactive applications."
/// Proceedings of the 2003 ACM SIGGRAPH/Eurographics symposium on Computer
/// animation. Eurographics Association, 2003.
///
pub struct SphSpikyKernel2 {
/// Kernel radius.
pub h: f64,
/// Square of the kernel radius.
pub h2: f64,
/// Cubic of the kernel radius.
pub h3: f64,
/// Fourth-power of the kernel radius.
pub h4: f64,
/// Fifth-power of the kernel radius.
pub h5: f64,
}
impl SphSpikyKernel2 {
/// Constructs a kernel object with zero radius.
pub fn new_default() -> SphSpikyKernel2 {
return SphSpikyKernel2 {
h: 0.0,
h2: 0.0,
h3: 0.0,
h4: 0.0,
h5: 0.0,
};
}
/// Constructs a kernel object with given radius.
pub fn new(kernel_radius: f64) -> SphSpikyKernel2 {
return SphSpikyKernel2 {
h: kernel_radius,
h2: kernel_radius.pow(2),
h3: kernel_radius.pow(3),
h4: kernel_radius.pow(4),
h5: kernel_radius.pow(5),
};
}
/// Returns kernel function value at given distance.
pub fn apply(&self, distance: f64) -> f64 {
return if distance >= self.h {
0.0
} else {
let x = 1.0 - distance / self.h;
10.0 / (crate::constants::K_PI_D * self.h2) * x * x * x
};
}
/// Returns the first derivative at given distance.
pub fn first_derivative(&self, distance: f64) -> f64 {
return if distance >= self.h {
0.0
} else {
let x = 1.0 - distance / self.h;
-30.0 / (crate::constants::K_PI_D * self.h3) * x * x
};
}
/// Returns the gradient at a point.
pub fn gradient_pnt(&self, point: &Vector2D) -> Vector2D {
let dist = point.length();
return if dist > 0.0 {
self.gradient_dir(dist, &(*point / dist))
} else {
Vector2D::new(0.0, 0.0)
};
}
/// Returns the gradient at a point defined by distance and direction.
pub fn gradient_dir(&self, distance: f64, direction: &Vector2D) -> Vector2D {
return *direction * -self.first_derivative(distance);
}
/// Returns the second derivative at given distance.
pub fn second_derivative(&self, distance: f64) -> f64 {
return if distance >= self.h {
0.0
} else {
let x = 1.0 - distance / self.h;
60.0 / (crate::constants::K_PI_D * self.h4) * x
};
}
}