Skip to main content

Rocky Solver SDK 2025 R2

api_device_sph

Last update: 16.07.2025
1// (C) 2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
2#pragma once
3
4#include <rocky20/api/rocky_api_sph_functions.h>
5#include <rocky20/api/rocky_model_api.hpp>
6#include <rocky20/api/sph/rocky_sph_element_api.hpp>
7#include <rocky20/api/sph/rocky_sph_interaction_api.hpp>
8#include <rocky20/api/sph/rocky_sph_model_api.hpp>
9
10namespace device {
11
12template <class Backend>
13struct DefaultElementsOperator
14{
15 template <typename Callable>
16 __host__ __device__ void operator()(
17 Callable function_ptr,
18 IRockyDeviceModel& device_model_api,
19 IRockySPHDeviceModel& sph_model,
20 IRockySPHElementInteraction& sph_interaction_api,
21 void* udf_data)
22 {
23 function_ptr(device_model_api, sph_model, sph_interaction_api, udf_data);
24 }
25};
26
27template <class Backend>
28struct DefaultTrianglesOperator
29{
30 template <typename Callable>
31 __host__ __device__ void operator()(
32 Callable function_ptr,
33 IRockyDeviceModel& device_model_api,
34 IRockySPHDeviceModel& sph_model,
35 IRockySPHTriangleInteraction& sph_interaction_api,
36 void* udf_data)
37 {
38 function_ptr(device_model_api, sph_model, sph_interaction_api, udf_data);
39 }
40};
41
42template <class Backend>
43struct DefaultOperator
44{
45 template <typename Callable>
46 __host__ __device__ void operator()(
47 Callable function_ptr,
48 IRockyDeviceModel& device_model_api,
49 IRockySPHDeviceModel& sph_model,
50 IRockySPHElement& sph_element_api,
51 void* udf_data)
52 {
53 function_ptr(device_model_api, sph_model, sph_element_api, udf_data);
54 }
55};
56
57template <class Backend>
58struct HeatTransferElementsOperator
59{
60 template <typename Callable>
61 __host__ __device__ void operator()(
62 Callable function_ptr,
63 IRockyDeviceModel& device_model_api,
64 IRockySPHDeviceModel& sph_model,
65 IRockySPHElementInteraction& sph_interaction_api,
66 void* udf_data)
67 {
68 // Compute and add heat transfer
69 real_sph heat_transfer =
70 function_ptr(device_model_api, sph_model, sph_interaction_api, udf_data);
71 auto home_sph_element = sph_interaction_api.get_home_element();
72 auto near_sph_element = sph_interaction_api.get_near_element();
73 home_sph_element.get_scalars().add_heat_transfer(heat_transfer);
74 near_sph_element.get_scalars().add_heat_transfer(-heat_transfer);
75
76 // Recording coupled heat transfer
77 if (home_sph_element.is_dem_coupled())
78 {
79 auto dem_particle = home_sph_element.get_linked_dem_particle(device_model_api);
80 auto dem_scalars = dem_particle.get_transfer_scalars();
81 dem_scalars.add_heat_transfer(heat_transfer);
82 }
83
84 if (near_sph_element.is_dem_coupled())
85 {
86 auto dem_particle = near_sph_element.get_linked_dem_particle(device_model_api);
87 auto dem_scalars = dem_particle.get_transfer_scalars();
88 dem_scalars.add_heat_transfer(-heat_transfer);
89 }
90 }
91};
92
93template <class Backend>
94struct HeatTransferTrianglesOperator
95{
96 template <typename Callable>
97 __host__ __device__ void operator()(
98 Callable function_ptr,
99 IRockyDeviceModel& device_model_api,
100 IRockySPHDeviceModel& sph_model,
101 IRockySPHTriangleInteraction& sph_interaction_api,
102 void* udf_data)
103 {
104 // Compute and add heat transfer
105 real_sph heat_transfer =
106 function_ptr(device_model_api, sph_model, sph_interaction_api, udf_data);
107
108 // reduction factor for SPH elements with multiple triangle interactions
109 // (1 / number of simultaneous triangle interactions)
110 heat_transfer /= max(1.0f, sph_model._impl.device_model->bnd_force_coefficient[sph_interaction_api._impl->home_index]);
111
112 sph_interaction_api.get_home_element().get_scalars().add_heat_transfer(heat_transfer);
113
114 // Recording data for SPH Boundary Statistics
115 const auto& data_recorder = sph_model._impl.device_model->boundary_data_recorder;
116 if (data_recorder
117 && data_recorder->is_enabled(SPHBoundaryDataRecorder::tSPHTriangleHeatTransfer))
118 {
119 const int index =
120 data_recorder->get_scalar_index(SPHBoundaryDataRecorder::tSPHTriangleHeatTransfer);
121 auto triangle_scalars = sph_interaction_api.get_near_triangle().get_scalars();
122 triangle_scalars.add_scalar(index, heat_transfer * device_model_api.get_timestep());
123 }
124
125 // Accumulation in a scalar used for heat coupling (positive when triangles receive heat)
126 auto* triangle_scalars = device_model_api._impl.model->triangle_scalars;
127 if (triangle_scalars && triangle_scalars->is_heat_exchanged_enabled())
128 {
129 Backend::AtomicAdd(
130 &triangle_scalars->get_heat_exchanged(sph_interaction_api._impl->near_index),
131 static_cast<double>(-heat_transfer * device_model_api.get_timestep()));
132 }
133 }
134};
135
136template <class Backend>
137struct ForceElementInteractionOperator
138{
139 template <typename Callable>
140 __host__ __device__ void operator()(
141 Callable function_ptr,
142 IRockyDeviceModel& device_model_api,
143 IRockySPHDeviceModel& sph_model,
144 IRockySPHElementInteraction& sph_interaction_api,
145 void* udf_data)
146 {
147 // custom calculation of the element-to-element acceleration
148 const real_sph3 acceleration =
149 function_ptr(device_model_api, sph_model, sph_interaction_api, udf_data);
150
151 sph_interaction_api.add_acceleration(acceleration);
152 }
153};
154
155template <class Backend>
156struct ForceTriangleInteractionOperator
157{
158 template <typename Callable>
159 __host__ __device__ void operator()(
160 Callable function_ptr,
161 IRockyDeviceModel& device_model_api,
162 IRockySPHDeviceModel& sph_model,
163 IRockySPHTriangleInteraction& sph_interaction_api,
164 void* udf_data)
165 {
166 // custom calculation of the element-to-triangle acceleration
167 const real_sph3 acceleration =
168 function_ptr(device_model_api, sph_model, sph_interaction_api, udf_data);
169
170 sph_interaction_api.add_acceleration(acceleration);
171 }
172};
173
174template <class Backend>
175struct ForceElementOperator
176{
177 template <typename Callable>
178 __host__ __device__ void operator()(
179 Callable function_ptr,
180 IRockyDeviceModel& device_model_api,
181 IRockySPHDeviceModel& sph_model,
182 IRockySPHElement& sph_element_api,
183 void* udf_data)
184 {
185 // custom calculation of the element acceleration (due to a body force)
186 const real_sph3 acceleration =
187 function_ptr(device_model_api, sph_model, sph_element_api, udf_data);
188
189 sph_element_api.add_acceleration(acceleration);
190 }
191};
192
193template <class Backend>
194struct MolecularViscosityElementOperator
195{
196 template <typename Callable>
197 __host__ __device__ void operator()(
198 Callable function_ptr,
199 IRockyDeviceModel& device_model_api,
200 IRockySPHDeviceModel& sph_model,
201 IRockySPHElement& sph_element_api,
202 void* udf_data)
203 {
204 // custom calculation of the element viscosity
205 const real_sph molecular_viscosity =
206 function_ptr(device_model_api, sph_model, sph_element_api, udf_data);
207
208 sph_element_api.set_molecular_viscosity(molecular_viscosity);
209 }
210};
211
212template <class backend, typename Operator, typename Functor>
213__host__ __device__ void launch_sph_particles_function(
214 SDeviceModel* device_data,
215 SPHDeviceModel* sph_device,
216 double current_time,
217 Functor function_ptr,
218 void* udf_data)
219{
220 IRockyDeviceModel device_model_api;
221 device_model_api._impl.model = device_data;
222 device_model_api._impl.current_time = current_time;
223 IRockySPHDeviceModel sph_model(device_data, sph_device->gen_sph, sph_device);
224
225 int thread_start, thread_step, thread_stop;
226 backend::ThreadIteration(
227 thread_start, thread_step, thread_stop, sph_device->gen_sph->nprt_active);
228 for (int i = thread_start; i < thread_stop; i += thread_step)
229 {
230 // Particle Interface
231 IRockySPHElement sph_particle_api { RockySPHElement(sph_device, i), current_time };
232 if (sph_particle_api.is_enabled() && !sph_particle_api._impl.is_ghost())
233 {
234 // Trigger user kernel
235 Operator caller{};
236 caller(function_ptr, device_model_api, sph_model, sph_particle_api, udf_data);
237 }
238 }
239}
240
241template <class Backend, typename Operator, typename Functor, class SPHNeighborsHandler>
242__host__ __device__ void launch_sph_interactions_particles(
243 SDeviceModel* device_data,
244 SPHDeviceModel* sph_device,
245 double current_time,
246 Functor function_ptr,
247 void* udf_data,
248 SPHNeighborsHandler& handler)
249{
250 IRockyDeviceModel device_model_api;
251 device_model_api._impl.model = device_data;
252 device_model_api._impl.current_time = current_time;
253 IRockySPHDeviceModel sph_model(device_data, sph_device->gen_sph, sph_device);
254
255 RockySPHElementInteraction sph_interaction(device_data, sph_device, current_time);
256 IRockySPHElementInteraction sph_interaction_api { &sph_interaction };
257
258 sph::iterator::IterateElements(handler, [&](const auto& search_element)
259 {
260 const int home_index = search_element.get_home_index();
261 // Home Interaction Interface
262 sph_interaction.reset_home_element(home_index);
263
264 // Loop over number of active interactions
265 sph::iterator::IterateNeighbors(handler, [&](const auto& neighbor_element)
266 {
267 // Near Interaction Interface
268 sph_interaction.reset_near_element(
269 neighbor_element.get_near_index(), neighbor_element.get_periodic());
270
271 // Trigger user kernel
272 Operator caller {};
273 caller(function_ptr, device_model_api, sph_model, sph_interaction_api, udf_data);
274 });
275 });
276}
277
278template <class Backend, typename Operator, typename Functor>
279__host__ __device__ void launch_sph_interactions_triangles(
280 SDeviceModel* device_data,
281 SPHDeviceModel* sph_device,
282 double current_time,
283 Functor function_ptr,
284 void* udf_data)
285{
286 IRockyDeviceModel device_model_api;
287 device_model_api._impl.model = device_data;
288 device_model_api._impl.current_time = current_time;
289 IRockySPHDeviceModel sph_model(device_data, sph_device->gen_sph, sph_device);
290
291 RockySPHTriangleInteraction sph_interaction(device_data, sph_device, current_time);
292 IRockySPHTriangleInteraction sph_interaction_api { &sph_interaction };
293
294 const auto& periodic_domain = device_data->gen->periodic_domain;
295
296 // loop only over element/triangle pairs with projection inside the triangle
297 int thread_start, thread_step, thread_stop;
298 Backend::ThreadIteration(thread_start, thread_step, thread_stop, sph_device->gen_sph->n_triangle_force_pairs);
299 for (int i = thread_start; i < thread_stop; i += thread_step)
300 {
301 const int index = sph_device->triangle_force_pairs[i];
302 cuda_sph_contact* _sph_interaction = sph_device->cnt_sph_t + index;
303 PeriodicPosition cnt_per {};
304 if (periodic_domain.enabled())
305 cnt_per = sph_device->per_sph_cnt_t[index];
306
307 // Interaction Interface
308 sph_interaction.reset_triangle_contact(_sph_interaction, cnt_per);
309
310 // ROCKY-17943: For periodic cases, skipping interaction w/ triangles, such that
311 // the projected point is positioned outside periodic domain
312 if (periodic_domain.enabled()
313 && periodic_domain.is_outside_cartesian_periodic_bounds(sph_interaction.inside_point))
314 {
315 continue;
316 }
317
318 // Trigger user kernel
319 Operator caller {};
320 caller(function_ptr, device_model_api, sph_model, sph_interaction_api, udf_data);
321 }
322}
323
324} // namespace device
Definition rocky_model_api.hpp:20
ROCKY_FUNCTIONS double get_timestep() const
Definition rocky_model_api.hpp:104
ROCKY_FUNCTIONS IRockyTriangleScalars get_scalars()
Definition rocky_sph_model_api.hpp:155
Definition rocky_sph_interaction_api.hpp:19
ROCKY_FUNCTIONS IRockySPHElement get_near_element() const
Definition rocky_sph_interaction_api.hpp:113
ROCKY_FUNCTIONS void add_acceleration(float3 acceleration)
Definition rocky_sph_interaction_api.hpp:123
ROCKY_FUNCTIONS IRockySPHElement get_home_element() const
Definition rocky_sph_interaction_api.hpp:108
ROCKY_FUNCTIONS void add_heat_transfer(const float value)
Definition rocky_sph_element_scalars.hpp:291
Definition rocky_sph_element_api.hpp:21
ROCKY_FUNCTIONS void add_acceleration(const float3 &acceleration)
Definition rocky_sph_element_api.hpp:69
ROCKY_FUNCTIONS IRockySPHElementScalars get_scalars()
Definition rocky_sph_element_api.hpp:260
ROCKY_FUNCTIONS void set_molecular_viscosity(const float molecular_viscosity)
Definition rocky_sph_element_api.hpp:284
Definition rocky_sph_interaction_api.hpp:138
ROCKY_FUNCTIONS void add_acceleration(float3 acceleration)
Definition rocky_sph_interaction_api.hpp:266
ROCKY_FUNCTIONS IRockyGeometryTriangle get_near_triangle() const
Definition rocky_sph_interaction_api.hpp:279
ROCKY_FUNCTIONS IRockySPHElement get_home_element() const
Definition rocky_sph_interaction_api.hpp:256
ROCKY_FUNCTIONS void add_scalar(int scalar_index, double value) const

Connect with Ansys