Skip to main content

Rocky Solver SDK 2025 R2

rocky_sph_interaction

Last update: 16.07.2025
1// (C) 2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
2#pragma once
3
4// Includes =======================================================================================
5#include <rocky20/api/device/api_backend.hpp>
6#include <rocky20/api/sph/rocky_sph_element.hpp>
7#include <rocky20/device/device_model.hpp>
8#include <rocky20/device/device_triangles.hpp>
9#include <rocky20/sph/device_sph.hpp>
10#include <rocky20/sph/sph_model.h>
11
12// ================================================================================================
13// RockySPHInteraction
14// ================================================================================================
15
16template <class NearType>
17struct RockySPHInteraction
18{
19 // Interacting relative data ------------------------------------------------------------------
20 inline ROCKY_FUNCTIONS float3 get_home_velocity() const
21 {
22 const auto near_periodic_position = [this] {
23 if constexpr (std::is_same_v<NearType, RockySPHElement>)
24 return RockySPHElement(this->sph_model, this->near_index).get_periodic_position();
25 else
26 return PeriodicPosition {};
27 }();
28
29 const RockySPHElement home_element = this->get_home_element();
30 float3 velocity_copy = home_element.get_velocity();
31 const auto& periodic_domain = rocky_model->gen->periodic_domain;
32 if (periodic_domain.enabled() && periodic_domain.is_cylindrical())
33 periodic_domain.get_cylindric_periodic_position(
34 velocity_copy,
35 this->cnt_per,
36 home_element.get_periodic_position(),
37 near_periodic_position,
38 false);
39 return velocity_copy;
40 }
41
42 inline ROCKY_FUNCTIONS float3 get_distance_vector() const
43 {
44 if constexpr (std::is_same_v<NearType, RockySPHElement>)
45 {
46 // Distance between SPH elements
47 const auto& periodic_domain = rocky_model->gen->periodic_domain;
48 const RockySPHElement home_element = this->get_home_element();
49 const RockySPHElement near_element(this->sph_model, this->near_index);
50 float3 home_coord { home_element.get_adjusted_coordinates(current_time) };
51 if (periodic_domain.enabled())
52 {
53 periodic_domain.get_periodic_position(
54 home_coord,
55 cnt_per,
56 home_element.get_periodic_position(),
57 near_element.get_periodic_position(),
58 false);
59 }
60 return home_coord - near_element.get_adjusted_coordinates(current_time);
61 }
62 else
63 return {};
64 }
65
66 inline ROCKY_FUNCTIONS float get_distance() const
67 {
68 return rocky::vector::get_norm(this->get_distance_vector());
69 }
70
71 // Interacting data ---------------------------------------------------------------------------
72 inline ROCKY_FUNCTIONS RockySPHElement get_home_element() const
73 {
74 return { this->sph_model, this->home_index };
75 }
76
77 // Contact data (resettable)
78 int home_index, near_index;
79 PeriodicPosition cnt_per;
80
81 // Setup data (non-resettable)
82 float current_time;
83 SDeviceModel* rocky_model;
84 SPHDeviceModel* sph_model;
85
86 ROCKY_FUNCTIONS explicit RockySPHInteraction(
87 SDeviceModel* rocky_model, SPHDeviceModel* sph_model, float current_time)
88 : rocky_model(rocky_model)
89 , sph_model(sph_model)
90 , current_time(current_time)
91 {
92 }
93};
94
95// ================================================================================================
96// RockySPHElementInteraction
97// ================================================================================================
98
99struct RockySPHElementInteraction : RockySPHInteraction<RockySPHElement>
100{
101 // Interacting relative data ------------------------------------------------------------------
102 // Velocity difference between the SPH elements in contact
103 inline ROCKY_FUNCTIONS float3 calculate_elements_relative_velocity()
104 {
105 const auto& periodic_domain = rocky_model->gen->periodic_domain;
106 const RockySPHElement home_element = this->get_home_element();
107 const RockySPHElement near_element = this->get_near_element();
108 float3 home_velocity = home_element.get_velocity();
109 if (periodic_domain.is_cylindrical())
110 periodic_domain.get_cylindric_periodic_position(
111 home_velocity,
112 cnt_per,
113 home_element.get_periodic_position(),
114 near_element.get_periodic_position(),
115 false);
116 return home_velocity - near_element.get_velocity();
117 }
118
119 inline ROCKY_FUNCTIONS float3 get_near_velocity() const
120 {
121 const RockySPHElement near_element = this->get_near_element();
122 float3 velocity_copy = near_element.get_velocity();
123 const auto& periodic_domain = rocky_model->gen->periodic_domain;
124 if (periodic_domain.enabled() && periodic_domain.is_cylindrical())
125 periodic_domain.get_cylindric_periodic_position(
126 velocity_copy.x,
127 velocity_copy.y,
128 velocity_copy.z,
129 this->cnt_per,
130 near_element.get_periodic_position(),
131 this->get_home_element().get_periodic_position(),
132 false);
133 return velocity_copy;
134 }
135
136 // Interacting data ---------------------------------------------------------------------------
137 inline ROCKY_FUNCTIONS RockySPHElement get_near_element() const
138 {
139 return RockySPHElement(this->sph_model, this->near_index);
140 }
141
142 // Forces & Accelerations ---------------------------------------------------------------------
143 inline ROCKY_FUNCTIONS void add_force(float3 force)
144 {
145 // For SPH elements, the force vector actually holds acceleration in those elements.
146 // So, for SPH elements, we must extract acceleration from force.
147 force /= this->get_home_element().get_mass();
148 this->add_acceleration(force);
149 }
150
151 inline ROCKY_FUNCTIONS void add_acceleration(float3 acceleration)
152 {
153 RockySPHElement home_element = this->get_home_element();
154 RockySPHElement near_element = this->get_near_element();
155 // For SPH elements, the force vector actually holds acceleration in those elements.
156 if (home_element.is_enabled() && !home_element.is_frozen())
157 {
158 const auto& periodic_domain = rocky_model->gen->periodic_domain;
159 if (periodic_domain.is_cylindrical())
160 periodic_domain.get_cylindric_periodic_position(
161 acceleration,
162 this->cnt_per,
163 home_element.get_periodic_position(),
164 near_element.get_periodic_position(),
165 true);
166 home_element.add_acceleration(acceleration);
167 }
168
169 if (near_element.is_enabled() && !near_element.is_frozen())
170 near_element.add_acceleration(-acceleration);
171 }
172
173 inline ROCKY_FUNCTIONS void reset_home_element(int home_index)
174 {
175 // Home
176 this->home_index = home_index;
177 }
178
179 inline ROCKY_FUNCTIONS void reset_near_element(int near_index, PeriodicPosition cnt_per)
180 {
181 // Near
182 this->near_index = near_index;
183
184 // Periodic stuff
185 this->cnt_per = cnt_per;
186 }
187
188 ROCKY_FUNCTIONS explicit RockySPHElementInteraction(
189 SDeviceModel* rocky_model, SPHDeviceModel* sph_model, double current_time)
190 : RockySPHInteraction<RockySPHElement>(rocky_model, sph_model, current_time)
191 {
192 }
193};
194
195// ================================================================================================
196// RockySPHTriangleInteraction
197// ================================================================================================
198
199struct RockySPHTriangleInteraction : RockySPHInteraction<RockyTriangle>
200
201{
202 // Interacting relative data ------------------------------------------------------------------
203 inline ROCKY_FUNCTIONS float3 get_near_velocity() const
204 {
205 return this->boundary_velocity;
206 }
207
208 inline ROCKY_FUNCTIONS float3 get_unit_vector() const
209 {
210 return this->unit_vector;
211 }
212
213 inline ROCKY_FUNCTIONS float3 get_nearest_point_to_home() const
214 {
215 return this->inside_point;
216 }
217
218 inline ROCKY_FUNCTIONS float get_distance() const
219 {
220 return this->distance;
221 }
222
223 // Forces & accelerations ---------------------------------------------------------------------
224 inline ROCKY_FUNCTIONS void add_force(float3 force)
225 {
226 // For SPH elements, the force vector actually holds acceleration in those elements.
227 // Here we receive a force, which is straight applied to the boundaries triangles.
228 // For the home element, we must extract the acceleration.
229 float3 acceleration = force / this->get_home_element().get_mass();
230 this->add_force_and_acceleration(force, acceleration);
231 }
232
233 inline ROCKY_FUNCTIONS void add_acceleration(float3 acceleration)
234 {
235 // For SPH elements, the force vector actually holds acceleration in those elements.
236 // Here we receive an acceleration, which is straight applied to the home element.
237 // For the boundaries triangles, we must extract the force.
238 float3 force = acceleration * this->get_home_element().get_mass();
239 this->add_force_and_acceleration(force, acceleration);
240 }
241
242 inline ROCKY_FUNCTIONS void add_force_and_acceleration(
243 float3 home_acceleration, float3 near_force)
244 {
245 RockyBoundaryTriangle triangle = this->get_near_triangle();
246
247 // reduction factor for SPH elements with multiple triangle interactions
248 // (1 / number of simultaneous triangle interactions)
249 const float reduction = 1.0f / max(1.0f, this->sph_model->bnd_force_coefficient[this->home_index]);
250 home_acceleration *= reduction;
251 near_force *= reduction;
252
253 // Home element acceleration
254 RockySPHElement home_element = this->get_home_element();
255 if (home_element.is_enabled() && !home_element.is_frozen())
256 {
257 const auto& periodic_domain = rocky_model->gen->periodic_domain;
258 if (periodic_domain.is_cylindrical())
259 periodic_domain.get_cylindric_periodic_position(
260 home_acceleration,
261 this->cnt_per,
262 home_element.get_periodic_position(),
263 {},
264 true);
265 home_element.add_acceleration(home_acceleration);
266 }
267
268 // Near triangle: calculating moment & power
269 const auto& boundary_matrix = triangle.get_boundary_matrix();
270 const double3 aux = rocky::vector::cast<double3>(this->inside_point)
271 - boundary_matrix.get_current_frame_center();
272 const double3 moment = rocky::vector::cross(aux, rocky::vector::cast<double3>(near_force));
273
274 // General boundary data
275 auto& boundary = triangle.get_boundary();
276
277 // Freely moving boundaries
278 if (boundary.free_f)
279 {
280 backend::atomic_add(&boundary.fx_inst, (cu_real) -near_force.x);
281 backend::atomic_add(&boundary.fy_inst, (cu_real) -near_force.y);
282 backend::atomic_add(&boundary.fz_inst, (cu_real) -near_force.z);
283 }
284 if (boundary.free_m)
285 {
286 backend::atomic_add(&boundary.mx_inst, -moment.x);
287 backend::atomic_add(&boundary.my_inst, -moment.y);
288 backend::atomic_add(&boundary.mz_inst, -moment.z);
289 }
290
291 // Record boundary statistics
292 auto* data_recorder = this->sph_model->boundary_data_recorder;
293 if (data_recorder && data_recorder->enabled)
294 {
295#ifdef GPU_KERNEL_COMPILING_STAGE
296 data_recorder->update_forces_data<CudaBackend>(
297 this->rocky_model->boundary_scalars,
298 this->rocky_model->triangle_scalars,
299 { -near_force.x, -near_force.y, -near_force.z },
300 -moment,
301 cast_vector<cu_real3>(this->boundary_velocity),
302 triangle.get_normal(),
303 this->sph_model->gen_sph->dt_sph,
304 triangle.get_boundary_index(),
305 this->near_index);
306#else
307 data_recorder->update_forces_data<OMPBackend>(
308 this->rocky_model->boundary_scalars,
309 this->rocky_model->triangle_scalars,
310 { -near_force.x, -near_force.y, -near_force.z },
311 -moment,
312 cast_vector<cu_real3>(this->boundary_velocity),
313 triangle.get_normal(),
314 this->sph_model->gen_sph->dt_sph,
315 triangle.get_boundary_index(),
316 this->near_index);
317#endif
318 }
319 }
320
321 // Triangle Information (calculated)
322 real_sph distance;
323 real_sph normal_velocity, tangential_velocity;
324 real_sph3 inside_point, unit_vector, boundary_velocity;
325
326 ROCKY_FUNCTIONS explicit RockySPHTriangleInteraction(
327 SDeviceModel* rocky_model, SPHDeviceModel* sph_model, double current_time)
328 : RockySPHInteraction<RockyTriangle>(rocky_model, sph_model, current_time)
329 {
330 }
331
332 inline ROCKY_FUNCTIONS void reset_triangle_contact(
333 cuda_sph_contact* contact, PeriodicPosition cnt_per)
334 {
335 // Home
336 this->home_index = contact->nh;
337
338 // Near
339 this->near_index = contact->nn;
340
341 // Periodic stuff
342 this->cnt_per = cnt_per;
343
344 this->calculate_triangle_distance_information();
345 }
346
347 // Near triangle distance information
348 inline ROCKY_FUNCTIONS void calculate_triangle_distance_information()
349 {
350 const RockySPHElement home_element = this->get_home_element();
351 const RockyBoundaryTriangle triangle = this->get_near_triangle();
352 real_sph3 position_copy = home_element.get_coordinates();
353 const auto& periodic_domain = rocky_model->gen->periodic_domain;
354 if (periodic_domain.enabled())
355 periodic_domain.get_periodic_position(
356 position_copy, this->cnt_per, home_element.get_periodic_position(), {}, false);
357
358 // Distance, inside point and normal unit vector
359 this->distance = device::get_triangle_orthogonal_distance(
360 triangle, position_copy, this->inside_point, this->unit_vector);
361
362 // Triangle velocity
363 cu_real3 triangle_velocity = (rocky_model->gen->flags.has_boundaries_prescribed_velocity())
364 ? rocky_model->triangle_scalars->get_known_scalar<double3>(
365 rocky20::KnownTriangleScalars::Velocity, this->near_index)
366 : double3 { 0.0, 0.0, 0.0 };
367
368 // Boundary velocity
369 this->boundary_velocity = cast_vector<real_sph3>(device::get_boundary_velocity_at_contact(
370 triangle.get_boundary(),
371 triangle.get_boundary_matrix(),
372 triangle_velocity,
373 cast_vector<double3>(this->inside_point),
374 this->current_time));
375
376 // Relative velocity decomposition
377 real_sph3 relative_velocity = this->boundary_velocity - home_element.get_velocity();
378 this->normal_velocity = dot(relative_velocity, this->unit_vector);
379 this->tangential_velocity = get_norm(relative_velocity - this->normal_velocity * this->unit_vector);
380 }
381
382 inline ROCKY_FUNCTIONS RockyTriangle get_near_triangle() const
383 {
384 return { this->rocky_model, this->near_index };
385 }
386
387 inline ROCKY_FUNCTIONS float get_wall_turbulent_thermal_conductance(
388 const float v_tang, const float dst_wall, const float molecular_viscosity) const
389 {
390 return this->sph_model->gen_sph->get_wall_turbulent_thermal_conductance(
391 v_tang,
392 dst_wall,
393 molecular_viscosity,
394 this->get_home_element().get_size(),
395 this->sph_model->sph_particle_scalars->is_turbulent_viscosity_enabled());
396 }
397};

Connect with Ansys