Skip to main content

Rocky Solver SDK 2025 R2

api_device_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/rocky_model_api.hpp>
5#include <rocky20/device/device_forces.hpp>
6#include <rocky20/device/device_contact.hpp>
7
8namespace
9{
10
11template <typename ForcePipelineFunctions>
12__host__ __device__ void api_process_frictional_contact_data(
13 IRockyContact& contact_api,
14 IRockyContactOutputData& contact_output,
15 const ForcePipelineFunctions& functions,
16 void* udf_data)
17{
18 auto rocky_contact = contact_api._impl;
19
20 // Intermediate Data
21 IRockyContactIntermediateData contact_idata {};
22 cuda_contact_intermediate_data& idata = contact_idata._idata;
23
24 // Output Data
25 contact_output_data& output = contact_output._output;
26
27 functions.get_frictional_parameters(
28 *rocky_contact, output, idata, contact_api.get_current_time());
29
30 // Custom (or standard) function calculating contact normal force
31 functions.template run<EForcePipelineFunctions::fpfComputeNormalForce>(
32 contact_api, contact_idata, contact_output, udf_data);
33
34 // Custom (or standard) function calculating contact tangential force
35 functions.template run<EForcePipelineFunctions::fpfComputeTangentialForce>(
36 contact_api, contact_idata, contact_output, udf_data);
37
38 if (rocky_contact->get_tangential_force_model() == tfmCustom)
39 device::calculate_mu_and_slip(*rocky_contact, contact_output, contact_idata);
40
41 // Custom (or standard) function calculating contact rolling resistance
42 functions.template run<EForcePipelineFunctions::fpfComputeRollingResistance>(
43 contact_api, contact_idata, contact_output, udf_data);
44 if (!contact_output._is_rr_power_set)
45 device::calculate_standard_rr_power(*rocky_contact, contact_idata, contact_output);
46
47 // Custom (or standard) function calculating contact impact energies
48
49 if (rocky_contact->model->gen->flags.needs_impact_energy())
50 {
51 functions.template run<EForcePipelineFunctions::fpfComputeImpactEnergy>(
52 contact_api, contact_idata, contact_output, udf_data);
53 }
54
55 device::calculate_standard_shear_energy(contact_output, contact_api);
56
57 device::get_total_frictional_forces_and_moments(*rocky_contact, output, idata);
58
59 /* updating stored values */
60 const int contact_index = rocky_contact->contact_index;
61 rocky20::ContactScalars& scalars = rocky_contact->get_scalars();
62
63 if (rocky_contact->get_normal_force_model() != nfmLinearHysteresis)
64 scalars.get_normal_force(contact_index) = output.normal_force;
65
66 if (scalars.has_tangential_force_enabled()
67 && (rocky_contact->get_tangential_force_model() == tfmCustom
68 || rocky_contact->get_tangential_force_model() == tfmCoulombLimitTangential))
69 scalars.get_tangential_forces()[contact_index] = output.tangential_force;
70
71 if (scalars.has_tangential_sliding_enabled()
72 && rocky_contact->get_tangential_force_model() == tfmCustom)
73 scalars.get_is_sliding_scalars()[contact_index] = contact_output._sliding;
74
75 if (scalars.has_sliding_distance())
76 scalars.get_sliding_distance(contact_index) = output.sliding_distance;
77 if (scalars.has_normal_relative_velocity())
78 scalars.get_normal_relative_velocity(contact_index) = output.normal_velocity;
79 if (scalars.has_tangential_relative_velocity())
80 scalars.get_tangential_relative_velocity(contact_index) = output.tangential_velocity;
81
82 if (rocky_contact->model->gen->flags.needs_energy_dissipation())
83 device::compute_frictional_dissipation_energy(
84 *rocky_contact, output, idata, output.rr_power);
85}
86
87
88template <typename Callable>
89__host__ __device__ void api_process_adhesive_contact_data(
90 IRockyContact& contact_api, IRockyAdhesionOutputData& output_data,
91 Callable process_contact_ptr, void *udf_data,
92 cu_real* normal_adhesion_forces, cu_real3* tangential_adhesion_forces
93)
94{
95 cuda_contact_intermediate_data idata{};
96 auto rocky_contact = contact_api._impl;
97
98 device::get_adhesive_contact_general_parameters(
99 *rocky_contact, output_data._output, idata, contact_api.get_current_time());
100
101 // Custom function calculating adhesive force
102 process_contact_ptr(contact_api, output_data, udf_data);
103 auto& output = output_data._output;
104
105 const auto scale = rocky_contact->get_scale();
106 output.adhesive_normal_force *= scale;
107 output.adhesive_tangential_force *= scale;
108
109 output.force = output.adhesive_normal_force * rocky_contact->get_normal_unit_vector()
110 + output.adhesive_tangential_force;
111
112 if (rocky_contact->model->gen->flags.needs_energy_dissipation())
113 device::compute_adhesive_dissipation_energy(*rocky_contact, output, idata);
114
115 if (normal_adhesion_forces)
116 normal_adhesion_forces[rocky_contact->contact_index] = output.adhesive_normal_force;
117 if (tangential_adhesion_forces)
118 tangential_adhesion_forces[rocky_contact->contact_index] = output.adhesive_tangential_force;
119
120 output.home_moment = cross(idata.home_arm, output.force);
121 if (rocky_contact->is_particle_contact)
122 output.near_moment = -cross(idata.near_arm, output.force);
123}
124
125} // namespace
126
127
128namespace device
129{
130
131template <class backend, typename Callable>
132__host__ __device__ void execute_contacts_function(
133 SDeviceModel *device_data, cu_real current_time,
134 unsigned int *contact_indexes, int total_number_contacts,
135 bool is_particle_contacts,
136 Callable process_contact_ptr, void *udf_data
137)
138{
139 IRockyDeviceModel device_model_api;
140 RockyContact rocky_contact(device_data, is_particle_contacts);
141 IRockyContact contact_api { &rocky_contact, current_time };
142 device_model_api._impl.model = device_data;
143 device_model_api._impl.current_time = current_time;
144
145 // Thread
146 int thread_start, thread_step, thread_stop;
147 backend::ThreadIteration(thread_start, thread_step, thread_stop, total_number_contacts);
148 for (int index = thread_start; index < thread_stop; index += thread_step)
149 {
150 unsigned int contact_index = contact_indexes[index];
151 rocky_contact.reset(contact_index);
152 const SContactState state = rocky_contact.get_state();
153 if (!state.disabled())
154 {
155 process_contact_ptr(device_model_api, contact_api, udf_data);
156 }
157 }
158}
159
160template <class backend, typename Callable>
161__host__ __device__ void execute_finished_contacts_function(
162 SDeviceModel* device_data, cu_real current_time,
163 int total_number_contacts,
164 bool is_particle_contacts,
165 Callable process_contact_ptr, void* udf_data
166)
167{
168 IRockyDeviceModel device_model_api;
169 RockyContact rocky_contact(device_data, is_particle_contacts);
170 IRockyContact contact_api { &rocky_contact, current_time };
171 device_model_api._impl.model = device_data;
172 device_model_api._impl.current_time = current_time;
173
174 // Thread
175 int thread_start, thread_step, thread_stop;
176 backend::ThreadIteration(thread_start, thread_step, thread_stop, total_number_contacts);
177 for (int index = thread_start; index < thread_stop; index += thread_step)
178 {
179 rocky_contact.reset(index);
180 if (rocky_contact.just_finished_adhesive() || rocky_contact.just_finished_frictional())
181 {
182 process_contact_ptr(device_model_api, contact_api, udf_data);
183 }
184 }
185}
186
187template <class backend, typename Callable>
188__host__ __device__ void execute_active_contacts_function(
189 SDeviceModel* device_data, cu_real current_time,
190 int total_number_contacts,
191 bool is_particle_contacts,
192 Callable process_contact_ptr, void* udf_data
193)
194{
195 IRockyDeviceModel device_model_api;
196 RockyContact rocky_contact(device_data, is_particle_contacts);
197 IRockyContact contact_api { &rocky_contact, current_time };
198 device_model_api._impl.model = device_data;
199 device_model_api._impl.current_time = current_time;
200
201 // Thread
202 int thread_start, thread_step, thread_stop;
203 backend::ThreadIteration(thread_start, thread_step, thread_stop, total_number_contacts);
204 for (int index = thread_start; index < thread_stop; index += thread_step)
205 {
206 rocky_contact.reset(index);
207 const SContactState state = rocky_contact.get_state();
208 if (state.is_frictional() || state.is_adhesive())
209 {
210 process_contact_ptr(device_model_api, contact_api, udf_data);
211 }
212 }
213}
214
215template <class backend, typename Callable>
216__host__ __device__ void execute_all_contacts_function(
217 SDeviceModel *device_data, cu_real current_time,
218 int total_number_contacts,
219 bool is_particle_contacts,
220 Callable process_contact_ptr, void *udf_data
221)
222{
223 IRockyDeviceModel device_model_api;
224 RockyContact rocky_contact(device_data, is_particle_contacts);
225 IRockyContact contact_api { &rocky_contact, current_time };
226 device_model_api._impl.model = device_data;
227 device_model_api._impl.current_time = current_time;
228 contact_api._impl = &rocky_contact;
229
230 // Thread
231 int thread_start, thread_step, thread_stop;
232 backend::ThreadIteration(thread_start, thread_step, thread_stop, total_number_contacts);
233 for (int contact_index = thread_start; contact_index < thread_stop; contact_index += thread_step)
234 {
235 //Process frictional contacts
236 rocky_contact.reset(contact_index);
237 const SContactState state = rocky_contact.get_state();
238 if (!state.disabled())
239 {
240 process_contact_ptr(device_model_api, contact_api, udf_data);
241 }
242 }
243}
244
245template <class backend, typename ForcePipelineFunctions>
246__host__ __device__ void api_compute_particle_contact_frictional_forces(
247 SDeviceModel* device_data,
248 cu_real current_time,
249 const ForcePipelineFunctions& functions,
250 void* udf_data)
251{
252 RockyContact rocky_contact(device_data, true);
253 IRockyContact contact_api { &rocky_contact, current_time };
254
255 // Thread
256 int thread_start, thread_step, thread_stop;
257 backend::ThreadIteration(thread_start, thread_step, thread_stop, device_data->n_contacts->particle_fr);
258 for (int index = thread_start; index < thread_stop; index += thread_step)
259 {
260 unsigned int contact_index = device_data->ind_cnt_p_fr[index];
261 rocky_contact.reset(contact_index);
262
263 if (!rocky_contact.get_state().disabled())
264 {
265 IRockyContactOutputData contact_output{};
266
267 api_process_frictional_contact_data(contact_api, contact_output, functions, udf_data);
268
269 update_frictional_particle_contact_forces_and_statistics<
270 backend,
271 std::is_same_v<ForcePipelineFunctions, DeformablePipelineFunctions>>(
272 rocky_contact, contact_output._output, current_time);
273 }
274 }
275}
276
277template <class backend, typename ForcePipelineFunctions>
278__host__ __device__ void api_compute_triangle_contact_frictional_forces(
279 SDeviceModel *device_data, cu_real current_time,
280 const ForcePipelineFunctions& functions, void *udf_data
281)
282{
283 RockyContact rocky_contact(device_data, false);
284 IRockyContact contact_api { &rocky_contact, current_time };
285
286 // Thread
287 int thread_start, thread_step, thread_stop;
288 backend::ThreadIteration(thread_start, thread_step, thread_stop, device_data->n_contacts->triangle_fr);
289 for (int index = thread_start; index < thread_stop; index += thread_step)
290 {
291 unsigned int contact_index = device_data->ind_cnt_t_fr[index];
292 rocky_contact.reset(contact_index);
293
294 if (!rocky_contact.get_state().disabled())
295 {
296 IRockyContactOutputData contact_output{};
297
298 api_process_frictional_contact_data(contact_api, contact_output, functions, udf_data);
299
300 update_frictional_triangle_contact_forces_and_statistics<
301 backend,
302 std::is_same_v<ForcePipelineFunctions, DeformablePipelineFunctions>>(
303 rocky_contact, contact_output._output, current_time);
304 }
305 }
306}
307
308template <class backend, typename Callable>
309__host__ __device__ void api_compute_particle_contact_adhesive_forces(
310 SDeviceModel* device_data, cu_real current_time, Callable process_contact_ptr, void* udf_data)
311{
312 RockyContact rocky_contact(device_data, true);
313 IRockyContact contact_api { &rocky_contact, current_time };
314
315 rocky20::ContactScalars* contact_scalars = device_data->current_particle_contact_scalars;
316 cu_real* normal_adhesion_forces = contact_scalars->has_normal_adhesion_force() ?
317 contact_scalars->get_normal_adhesion_forces() : nullptr;
318 cu_real3* tangential_adhesion_forces = contact_scalars->has_tangential_adhesion_force() ?
319 contact_scalars->get_tangential_adhesion_forces() : nullptr;
320
321 // Thread
322 int thread_start, thread_step, thread_stop;
323 backend::ThreadIteration(thread_start, thread_step, thread_stop, device_data->n_contacts->particle_ad);
324 for (int index = thread_start; index < thread_stop; index += thread_step)
325 {
326 unsigned int contact_index = device_data->ind_cnt_p_ad[index];
327 const SContactState state = contact_scalars->get_state(contact_index);
328
329 if (!state.disabled())
330 {
331 rocky_contact.reset(contact_index);
332 IRockyAdhesionOutputData adhesion_output{};
333
334 api_process_adhesive_contact_data(
335 contact_api, adhesion_output,
336 process_contact_ptr, udf_data,
337 normal_adhesion_forces, tangential_adhesion_forces
338 );
339
340 update_adhesive_particle_contact_forces_and_statistics<backend>(
341 rocky_contact, adhesion_output._output);
342 }
343 }
344}
345
346template <class backend, typename Callable>
347__host__ __device__ void api_compute_triangle_contact_adhesive_forces(
348 SDeviceModel* device_data, cu_real current_time, Callable process_contact_ptr, void* udf_data)
349{
350 RockyContact rocky_contact(device_data, false);
351 IRockyContact contact_api { &rocky_contact, current_time };
352
353 rocky20::ContactScalars* contact_scalars = device_data->current_triangle_contact_scalars;
354 cu_real* normal_adhesion_forces = contact_scalars->has_normal_adhesion_force() ?
355 contact_scalars->get_normal_adhesion_forces() : nullptr;
356 cu_real3* tangential_adhesion_forces = contact_scalars->has_tangential_adhesion_force() ?
357 contact_scalars->get_tangential_adhesion_forces() : nullptr;
358
359 // Thread
360 int thread_start, thread_step, thread_stop;
361 backend::ThreadIteration(thread_start, thread_step, thread_stop, device_data->n_contacts->triangle_ad);
362 for (int index = thread_start; index < thread_stop; index += thread_step)
363 {
364 unsigned int contact_index = device_data->ind_cnt_t_ad[index];
365 const SContactState state = contact_scalars->get_state(contact_index);
366
367 if (!state.disabled())
368 {
369 rocky_contact.reset(contact_index);
370 IRockyAdhesionOutputData adhesion_output{};
371
372 api_process_adhesive_contact_data(
373 contact_api, adhesion_output,
374 process_contact_ptr, udf_data,
375 normal_adhesion_forces, tangential_adhesion_forces
376 );
377
378 update_adhesive_triangle_contact_forces_and_statistics<backend>(
379 rocky_contact, adhesion_output._output);
380 }
381 }
382}
383
384template <class backend, typename Callable>
385__host__ __device__ void api_compute_boundaries_wear(
386 SDeviceModel* device_data,
387 const cu_real current_time,
388 Callable process_triangle_wear_f_ptr,
389 void* udf_data)
390{
391 RockyContact rocky_contact(device_data, false);
392
393 IRockyContact contact_api;
394 contact_api._impl = &rocky_contact;
395
396 int thread_start, thread_step, thread_stop;
397 backend::ThreadIteration(thread_start, thread_step, thread_stop, device_data->n_contacts->triangle_fr);
398 for (int index = thread_start; index < thread_stop; index += thread_step)
399 {
400 unsigned int contact_index = device_data->ind_cnt_t_fr[index];
401 rocky_contact.reset(contact_index);
402 const SContactState state = rocky_contact.get_state();
403
404 if (!state.disabled())
405 {
406 auto& boundary = rocky_contact.get_near_boundary();
407 if (boundary.wear_model == wmCustom)
408 {
409 double wear_volume;
410 process_triangle_wear_f_ptr(contact_api, wear_volume, udf_data);
411 accumulate_wear_volume<backend>(rocky_contact, wear_volume, current_time);
412 }
413 }
414 }
415}
416
417} // namespace device
Definition rocky_contact_output_data_api.hpp:304
Definition rocky_contact_intermediate_data_api.hpp:18
Definition rocky_contact_output_data_api.hpp:38
ROCKY_FUNCTIONS double get_normal_relative_velocity()
Definition rocky_contact_output_data_api.hpp:280
Definition rocky_contact_api.hpp:47
ROCKY_FUNCTIONS double get_current_time() const
Definition rocky_contact_api.hpp:610
Definition rocky_model_api.hpp:20

Connect with Ansys