Skip to main content

Rocky Solver SDK 2025 R2

api_cuda_contacts

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/device/api_device_contacts.hpp>
5#include <rocky20/api/device/api_cuda.hpp>
6#include <rocky20/api/rocky_api_functions.h>
7#include <rocky20/api/rocky_api.h>
8
9namespace rocky::cuda
10{
11
12// =================================================================================================
13// Contacts Loop
14// =================================================================================================
15
16template <typename Callable>
17__global__ void execute_frictional_contacts_function(
18 SDeviceModel *device_data,
19 cu_real time_curr,
20 Callable particle_contact_f_ptr,
21 Callable triangle_contact_f_ptr,
22 void *udf_device_data
23)
24{
25 // Particles
26 device::execute_contacts_function<CudaBackend>(
27 device_data,
28 time_curr,
29 device_data->ind_cnt_p_fr,
30 device_data->n_contacts->particle_fr,
31 true,
32 particle_contact_f_ptr,
33 udf_device_data);
34
35 // Triangles
36 device::execute_contacts_function<CudaBackend>(
37 device_data,
38 time_curr,
39 device_data->ind_cnt_t_fr,
40 device_data->n_contacts->triangle_fr,
41 false,
42 triangle_contact_f_ptr,
43 udf_device_data);
44}
45
46template <typename Callable>
47__global__ void execute_finished_contacts_function(
48 SDeviceModel* device_data,
49 cu_real time_curr,
50 Callable particle_contact_f_ptr,
51 Callable triangle_contact_f_ptr,
52 void* udf_device_data
53)
54{
55 // particles -> particles
56 device::execute_finished_contacts_function<CudaBackend>(
57 device_data,
58 time_curr,
59 device_data->gen->ncont_p,
60 true,
61 particle_contact_f_ptr, udf_device_data
62 );
63
64 // particles -> triangles
65 device::execute_finished_contacts_function<CudaBackend>(
66 device_data,
67 time_curr,
68 device_data->gen->ncont_t,
69 false,
70 triangle_contact_f_ptr, udf_device_data
71 );
72}
73
74template <typename Callable>
75__global__ void execute_active_contacts_function(
76 SDeviceModel* device_data,
77 cu_real time_curr,
78 Callable particle_contact_f_ptr,
79 Callable triangle_contact_f_ptr,
80 void* udf_device_data
81)
82{
83 // particles -> particles
84 device::execute_active_contacts_function<CudaBackend>(
85 device_data,
86 time_curr,
87 device_data->gen->ncont_p,
88 true,
89 particle_contact_f_ptr,
90 udf_device_data);
91
92 // particles -> triangles
93 device::execute_active_contacts_function<CudaBackend>(
94 device_data,
95 time_curr,
96 device_data->gen->ncont_t,
97 false,
98 triangle_contact_f_ptr,
99 udf_device_data);
100}
101
102template <typename Callable>
103void dispatch_frictional_contacts_kernel(
104 IRockyModel &model,
105 Callable h_particle_contact_f_ptr,
106 Callable h_triangle_contact_f_ptr,
107 void **udf_devices_data
108)
109{
110 if (model.get_number_of_active_particles() <= 0)
111 return;
112
113 for (int i = 0; i < model.get_number_of_devices(); i++)
114 {
115 void *udf_device_data = udf_devices_data[i];
116 SDeviceModel *device_data = model.get_device_data(i);
117
118 CUDA_SET_DEVICE(model.get_device_id(i));
119 rocky::cuda::execute_frictional_contacts_function<<<CD_NBLOCKS, CD_NTHREADS>>>(
120 device_data,
121 model.get_current_time(),
122 h_particle_contact_f_ptr,
123 h_triangle_contact_f_ptr,
124 udf_device_data);
125 }
126
127 //Sync all GPUs
128 sync_devices(model);
129}
130
131// frictional and adhesive contacts just finished
132template <typename Callable>
133void dispatch_finished_contacts_kernel(
134 IRockyModel& model,
135 Callable h_particle_contact_f_ptr,
136 Callable h_triangle_contact_f_ptr,
137 void** udf_devices_data
138)
139{
140 if (model.get_number_of_active_particles() <= 0)
141 return;
142
143 for (int i = 0; i < model.get_number_of_devices(); i++)
144 {
145 void* udf_device_data = udf_devices_data[i];
146 SDeviceModel* device_data = model.get_device_data(i);
147
148 CUDA_SET_DEVICE(model.get_device_id(i));
149 rocky::cuda::execute_finished_contacts_function <<<CD_NBLOCKS, CD_NTHREADS>>> (
150 device_data, model.get_current_time(),
151 h_particle_contact_f_ptr, h_triangle_contact_f_ptr,
152 udf_device_data
153 );
154 }
155
156 sync_devices(model);
157}
158
159// frictional and adhesive contacts still active
160template <typename Callable>
161void dispatch_active_contacts_kernel(
162 IRockyModel& model,
163 Callable h_particle_contact_f_ptr,
164 Callable h_triangle_contact_f_ptr,
165 void** udf_devices_data)
166{
167 if (model.get_number_of_active_particles() <= 0)
168 return;
169
170 for (int i = 0; i < model.get_number_of_devices(); i++)
171 {
172 void* udf_device_data = udf_devices_data[i];
173 SDeviceModel* device_data = model.get_device_data(i);
174
175 CUDA_SET_DEVICE(model.get_device_id(i));
176 rocky::cuda::execute_active_contacts_function<<<CD_NBLOCKS, CD_NTHREADS>>>(
177 device_data,
178 model.get_current_time(),
179 h_particle_contact_f_ptr,
180 h_triangle_contact_f_ptr,
181 udf_device_data);
182 }
183
184 sync_devices(model);
185}
186
187// Adhesive ----------------------------------------------------------------------------------------
188
189template <typename Callable>
190__global__ void execute_adhesive_contacts_function(
191 SDeviceModel *device_data,
192 cu_real time_curr,
193 Callable particle_contact_f_ptr,
194 Callable triangle_contact_f_ptr,
195 void *udf_device_data
196)
197{
198 // Particles
199 if (device_data->n_contacts->particle_ad > 0)
200 {
201 device::execute_contacts_function<CudaBackend>(
202 device_data,
203 time_curr,
204 device_data->ind_cnt_p_ad,
205 device_data->n_contacts->particle_ad,
206 true,
207 particle_contact_f_ptr,
208 udf_device_data);
209 }
210
211 // Triangles
212 if (device_data->n_contacts->triangle_ad > 0)
213 {
214 device::execute_contacts_function<CudaBackend>(
215 device_data,
216 time_curr,
217 device_data->ind_cnt_t_ad,
218 device_data->n_contacts->triangle_ad,
219 false,
220 triangle_contact_f_ptr,
221 udf_device_data);
222 }
223}
224
225template <typename Callable>
226void dispatch_adhesive_contacts_kernel(
227 IRockyModel &model,
228 Callable h_particle_contact_f_ptr,
229 Callable h_triangle_contact_f_ptr,
230 void **udf_devices_data
231)
232{
233 if (model.get_number_of_active_particles() <= 0)
234 return;
235
236 for (int i = 0; i<model.get_number_of_devices(); i++)
237 {
238 void *udf_device_data = udf_devices_data[i];
239 SDeviceModel *device_data = model.get_device_data(i);
240
241 CUDA_SET_DEVICE(model.get_device_id(i));
242 rocky::cuda::execute_adhesive_contacts_function<<<CD_NBLOCKS, CD_NTHREADS>>>(
243 device_data,
244 model.get_current_time(),
245 h_particle_contact_f_ptr,
246 h_triangle_contact_f_ptr,
247 udf_device_data);
248 }
249
250 //Sync all GPUs
251 sync_devices(model);
252
253}
254
255// =================================================================================================
256// All Contacts Loop
257// =================================================================================================
258template <typename Callable>
259__global__ void execute_all_contacts_function(
260 SDeviceModel *device_data,
261 cu_real time_curr,
262 Callable particle_contact_f_ptr,
263 Callable triangle_contact_f_ptr,
264 void *udf_device_data
265)
266{
267 // Particles
268 device::execute_all_contacts_function<CudaBackend>(
269 device_data,
270 time_curr,
271 device_data->gen->ncont_p,
272 true,
273 particle_contact_f_ptr,
274 udf_device_data);
275
276 // Triangles
277 device::execute_all_contacts_function<CudaBackend>(
278 device_data,
279 time_curr,
280 device_data->gen->ncont_t,
281 false,
282 triangle_contact_f_ptr,
283 udf_device_data);
284}
285
286template <typename Callable>
287void dispatch_all_contacts_kernel(
288 IRockyModel &model,
289 Callable h_particle_contact_f_ptr,
290 Callable h_triangle_contact_f_ptr,
291 void **udf_device_data
292)
293{
294 if (model.get_number_of_active_particles() <= 0)
295 return;
296
297 for (int i = 0; i<model.get_number_of_devices(); i++)
298 {
299 CUDA_SET_DEVICE(model.get_device_id(i));
300 rocky::cuda::execute_all_contacts_function<<<CD_NBLOCKS, CD_NTHREADS>>>(
301 model.get_device_data(i),
302 model.get_current_time(),
303 h_particle_contact_f_ptr,
304 h_triangle_contact_f_ptr,
305 udf_device_data[i]);
306 }
307
308 //Sync all GPUs
309 sync_devices(model);
310}
311
312// =================================================================================================
313// Contact forces
314// =================================================================================================
315template <typename CallableCollection>
316__global__ void execute_frictional_particle_contact_forces_function(
317 SDeviceModel* device_data,
318 cu_real time_curr,
319 const CallableCollection functions,
320 void* udf_device_data)
321{
322 device::api_compute_particle_contact_frictional_forces<CudaBackend>(
323 device_data, time_curr, functions, udf_device_data);
324}
325
326template <typename CallableCollection>
327__global__ void execute_frictional_triangle_contact_forces_function(
328 SDeviceModel* device_data,
329 cu_real time_curr,
330 const CallableCollection functions,
331 void* udf_device_data)
332{
333 device::api_compute_triangle_contact_frictional_forces<CudaBackend>(
334 device_data, time_curr, functions, udf_device_data);
335}
336
337
338template <typename CallableCollection>
339void dispatch_frictional_contact_forces_kernel(
340 IRockyModel& model, const CallableCollection& function_ptrs, void** udf_devices_data)
341{
342 if (model.get_number_of_active_particles() <= 0)
343 return;
344
345 auto& solver_model = *model._impl;
346
347 solver_model.n_contacts.reset();
348 std::vector<SNumberContacts> n_contacts(model.get_number_of_devices());
349 for (int i = 0; i < model.get_number_of_devices(); i++)
350 {
351 CUDA_SET_DEVICE(model.get_device_id(i));
352 CUDA_COPY_D2H(n_contacts.data() + i, solver_model.GetDeviceModel(i).n_contacts, 1);
353 solver_model.n_contacts.join(n_contacts[i]);
354 }
355
356 void* udf_device_data = nullptr;
357 for (int i = 0; i < model.get_number_of_devices(); i++)
358 {
359 if (udf_devices_data) udf_device_data = udf_devices_data[i];
360 SDeviceModel* device_data = model.get_device_data(i);
361
362 CUDA_SET_DEVICE(model.get_device_id(i))
363 auto [blocks, threads] = CudaBackend::get_launch_params(n_contacts[i].particle_fr);
364 rocky::cuda::execute_frictional_particle_contact_forces_function<<<blocks, threads>>>(
365 device_data, model.get_current_time(), function_ptrs, udf_device_data);
366 }
367
368 for (int i = 0; i < model.get_number_of_devices(); i++)
369 {
370 if (udf_devices_data) udf_device_data = udf_devices_data[i];
371 SDeviceModel* device_data = model.get_device_data(i);
372
373 CUDA_SET_DEVICE(model.get_device_id(i))
374 auto [blocks, threads] = CudaBackend::get_launch_params(n_contacts[i].triangle_fr);
375 rocky::cuda::execute_frictional_triangle_contact_forces_function <<<blocks, threads>>> (
376 device_data, model.get_current_time(), function_ptrs, udf_device_data);
377 }
378
379 sync_devices(model);
380}
381
382template <typename Callable>
383__global__ void execute_adhesive_contact_forces_function(
384 SDeviceModel *device_data,
385 cu_real time_curr,
386 Callable particle_contact_f_ptr,
387 Callable triangle_contact_f_ptr,
388 void *udf_device_data
389)
390{
391 // Particles
392 device::api_compute_particle_contact_adhesive_forces<CudaBackend>(
393 device_data, time_curr,
394 particle_contact_f_ptr, udf_device_data
395 );
396
397 // Triangles
398 device::api_compute_triangle_contact_adhesive_forces<CudaBackend>(
399 device_data, time_curr,
400 triangle_contact_f_ptr, udf_device_data
401 );
402}
403
404template <typename Callable>
405void dispatch_adhesive_contact_forces_kernel(
406 IRockyModel &model,
407 Callable h_particle_contact_f_ptr,
408 Callable h_triangle_contact_f_ptr,
409 void **udf_devices_data
410)
411{
412 if (model.get_number_of_active_particles() <= 0)
413 return;
414
415 std::vector<SNumberContacts> n_contacts(model.get_number_of_devices());
416 for (int i = 0; i < model.get_number_of_devices(); i++)
417 {
418 CUDA_SET_DEVICE(model.get_device_id(i))
419 CUDA_COPY_D2H(n_contacts.data() + i, model._impl->GetDeviceModel(i).n_contacts, 1)
420 }
421
422 for (int i = 0; i < model.get_number_of_devices(); i++)
423 {
424 void *udf_device_data = udf_devices_data[i];
425 SDeviceModel *device_data = model.get_device_data(i);
426
427 CUDA_SET_DEVICE(model.get_device_id(i))
428 auto [blocks, threads] = CudaBackend::get_launch_params(
429 std::max(n_contacts[i].particle_ad, n_contacts[i].triangle_ad));
430 rocky::cuda::execute_adhesive_contact_forces_function<<<blocks, threads>>>(
431 device_data,
432 model.get_current_time(),
433 h_particle_contact_f_ptr,
434 h_triangle_contact_f_ptr,
435 udf_device_data);
436 }
437
438 //Sync all GPUs
439 sync_devices(model);
440}
441
442// Boundary wear on particle-triangle contacts ----------------------------------------------------
443
444template <typename Callable>
445__global__ void execute_boundaries_wear_function_kernel(
446 SDeviceModel* device_data,
447 Callable triangle_wear_f_ptr,
448 void* udf_device_data,
449 cu_real time_curr)
450{
451 device::api_compute_boundaries_wear<CudaBackend>(
452 device_data, time_curr, triangle_wear_f_ptr, udf_device_data);
453}
454
455template <typename Callable>
456void dispatch_boundaries_wear_kernel(
457 IRockyModel& model,
458 Callable triangle_wear_f_ptr,
459 void** udf_devices_data
460)
461{
462 for (int i = 0; i < model.get_number_of_devices(); i++)
463 {
464 void* udf_device_data = udf_devices_data[i];
465 SDeviceModel* device_data = model.get_device_data(i);
466
467 CUDA_SET_DEVICE(model.get_device_id(i))
468 rocky::cuda::execute_boundaries_wear_function_kernel<<<CD_NBLOCKS, CD_NTHREADS>>>(
469 device_data,
470 triangle_wear_f_ptr,
471 udf_device_data,
472 model._impl->iteration.current_time);
473 }
474
475 sync_devices(model);
476}
477
478} // namespace rocky::cuda
Definition rocky_api.h:58
int get_number_of_devices() const
Definition rocky_api.cpp:173
int get_number_of_active_particles() const
Definition rocky_api.cpp:153
double get_current_time() const
Definition rocky_api.cpp:143
int get_device_id(int device_index) const
Definition rocky_api.cpp:178

Connect with Ansys