Skip to main content

Rocky Solver SDK 2024 R2

rocky_sph_element

Last update: 17.07.2025
1#pragma once
2
3// Includes =======================================================================================
4#include "rocky_sph_element_scalars.hpp"
5
6#include <rocky20/api/device/api_backend.hpp>
7#include <rocky20/api/sph/rocky_sph_element_scalars.hpp>
8#include <rocky20/device/device_model.hpp>
9#include <rocky20/sph/sph_data.h>
10
11// ================================================================================================
12// RockySPHElement
13// ================================================================================================
14
15template <typename Model, typename ModelSPH>
16struct RockySPHElement
17{
18 inline ROCKY_FUNCTIONS bool is_dem_coupled() const
19 {
20 return this->element->is_dem_coupled();
21 }
22
23 template <typename Particle>
24 ROCKY_FUNCTIONS Particle get_linked_dem_particle() const
25 {
26 if constexpr (std::is_same_v<Particle, IRockyParticle>)
27 {
28 return IRockyParticle(element->prt_id, model);
29 }
30 else
31 {
32#ifndef __CUDA_ARCH__
33 return Particle(element->prt_id, model);
34#endif
35 }
36 }
37
38 inline ROCKY_FUNCTIONS int get_input_index() const
39 {
40 return this->element->inl_id;
41 }
42
43 inline ROCKY_FUNCTIONS bool is_enabled() const
44 {
45 return this->element->enabled();
46 }
47
48 inline ROCKY_FUNCTIONS float get_density() const
49 {
50 return this->element->dens;
51 }
52
53 inline ROCKY_FUNCTIONS float get_pressure() const
54 {
55 return this->sph_model->gen_sph->get_pressure(element->dens);
56 }
57
58 inline ROCKY_FUNCTIONS float get_release_time() const
59 {
60 return this->element->trel;
61 }
62
63 inline ROCKY_FUNCTIONS float3 get_adjusted_coordinates() const
64 {
65 return this->element->get_adjusted_coordinates(this->current_time);
66 }
67
68 inline ROCKY_FUNCTIONS float3 get_velocity() const
69 {
70 return float3 { this->element->ux, this->element->uy, this->element->uz };
71 }
72
73 inline ROCKY_FUNCTIONS float3 get_normal() const
74 {
75 return this->sph_model->nrm_sph[this->element_index];
76 }
77
78 inline ROCKY_FUNCTIONS float3 get_force() const
79 {
80 // For SPH elements, the force vector actually holds acceleration in those elements.
81 // For the sake of uniformity throughout our SPH-DEM APIs, we return here a force.
82 const float mass = this->sph_model->gen_sph->get_mass();
83 return this->sph_model->frc_sph[this->element_index] * mass;
84 }
85
86 inline ROCKY_FUNCTIONS float3 get_acceleration() const
87 {
88 // For SPH elements, the force vector actually holds acceleration in those elements.
89 return this->sph_model->frc_sph[this->element_index];
90 }
91
92 inline ROCKY_FUNCTIONS void add_force(const float3& f) const
93 {
94 // For SPH elements, the force vector actually holds acceleration in those elements.
95 // For the sake of uniformity throughout our SPH-DEM APIs, we receive here a force,
96 // and therefore must extract the acceleration from it.
97 //TODO Refinement
98 const float mass = this->sph_model->gen_sph->get_mass();
99 this->add_acceleration(f / mass);
100 }
101
102 inline ROCKY_FUNCTIONS void add_acceleration(const float3& a) const
103 {
104 // For SPH elements, the force vector actually holds acceleration in those elements.
105 float3& prt_forces = this->sph_model->frc_sph[this->element_index];
106 backend::atomic_add(&prt_forces.x, a.x);
107 backend::atomic_add(&prt_forces.y, a.y);
108 backend::atomic_add(&prt_forces.z, a.z);
109 }
110
111 // Scalars -----------------------------------------------------------------------------------
112 inline ROCKY_FUNCTIONS float get_turbulent_viscosity() const
113 {
114 return this->sph_model->sph_element_scalars->get_turbulent_viscosity(this->element_index);
115 }
116
117 inline ROCKY_FUNCTIONS void set_turbulent_viscosity(float turbulent_viscosity)
118 {
119 auto& scalar =
120 this->sph_model->sph_element_scalars->get_turbulent_viscosity(this->element_index);
121 scalar = turbulent_viscosity;
122 }
123
124 inline ROCKY_FUNCTIONS void add_turbulent_viscosity(float turbulent_viscosity)
125 {
126 auto& scalar =
127 this->sph_model->sph_element_scalars->get_turbulent_viscosity(this->element_index);
128 backend::atomic_add(&scalar, turbulent_viscosity);
129 }
130
131 inline ROCKY_FUNCTIONS const rocky20::SPHParticleScalars* get_sph_element_scalars() const
132 {
133 if constexpr (std::is_same<ModelSPH, SPHDeviceModel>::value)
134 return this->sph_model->sph_element_scalars;
135 else
136 return &this->sph_model->sph_element_scalars_controller.host_data;
137 }
138
139 inline ROCKY_FUNCTIONS rocky20::SPHParticleScalars* get_sph_element_scalars()
140 {
141 if constexpr (std::is_same<ModelSPH, SPHDeviceModel>::value)
142 return this->sph_model->sph_particle_scalars;
143 else
144 return &this->sph_model->sph_particle_scalars_controller.host_data;
145 }
146
147 inline ROCKY_FUNCTIONS IRockySPHElementScalars get_scalars()
148 {
149 return IRockySPHElementScalars(*this->get_sph_element_scalars(), this->element_index);
150 }
151
152 // Flags --------------------------------------------------------------------------------------
153 inline ROCKY_FUNCTIONS bool is_frozen() const
154 {
155 return element->frozen();
156 }
157
158 inline ROCKY_FUNCTIONS void set_frozen(bool _frozen)
159 {
160 if (!element->is_dem_coupled())
161 {
162 return element->set_frozen(_frozen);
163 }
164 }
165
166 // Particle data (resettable)
167 cuda_particle_sph* element;
168 int element_index;
169
170 // Setup data (non-resettable)
171 Model* model;
172 ModelSPH* sph_model;
173 double current_time;
174
175 ROCKY_FUNCTIONS RockySPHElement(Model* _model, ModelSPH* _sph_model)
176 : model(_model)
177 , sph_model(_sph_model)
178 {
179 }
180
181 inline ROCKY_FUNCTIONS void reset(int index, cuda_particle_sph* _element)
182 {
183 this->element_index = index;
184 this->element = _element;
185 }
186};
187
188typedef RockySPHElement<SDeviceModel, SPHDeviceModel> RockySPHElementDevice;
189typedef RockySPHElement<RockyModel, SPHModel> RockySPHElementHost;
Definition rocky_particle_api.hpp:27
Definition rocky_sph_element_scalars.hpp:121

Connect with Ansys