A simple custom module
This section describes the implementation of the code of a simple module in order to illustrate the use of the Rocky Solver SDK. The example module is calledSpherical Region. Its only purpose
is to allow users to define a region of spherical shape inside
the simulation domain, and mark all particles located inside this region
at any given time.
The source files detailed below should be saved in the following
directory structure:
File spherical_region.plugin
File spherical_region.py
This is a Python file that specifies which are the module parameters that must be displayed in the Rocky UI*. When a project is set up in Rocky and the module is enabled, users will be able to enter values for all parameters defined in this file, through the Rocky UI. Refer to chapter [Module specification(module_specification.md) for a complete description of how different types of data can be specified by means of this file.SphericalRegionModel is defined, which includes the
specification of the four parameters that the model needs.
They are the three Cartesian coordinates of the sphere center and the sphere radius.
Between lines 18 and 26, the SphericalRegionSpecs class is defined, which in this case
includes only the definition of the name of the module and the
specification of the parameters previously included in the SphericalRegionModel class.
Additionally, the CreateAddin method is defined, which handles the loading of the
compiled library file for the module when the simulation starts.
Finally, at line 29, the definition of the SphericalRegionModule class is included. Only
one method is implemented in this class, get_addin_specs, which makes available all
the specifications made previously in the file.
File spherical_region.pdf
This is an optional help file that may include usage instructions and/or technical details about the implementation of the module. If included, Rocky will automatically link it to a special icon displayed in the UI that will allow users of the module to open the file.File CMakeLists.txt
This file includes compilation, linkage and install instructions in the CMake language, needed to build the module. Several commands defined by the Rocky SDK are used in order to simplify this file. The content of this file must be practically the same reproduced below for any custom module implemented using the Rocky Solver SDK. Users only have to replace the actual name of the compiled library at line 3 and define theROCKY_PLUGINS_PATH
environment variable with the directory in which the compiled modules
must be installed, if this definition has not been made previously.
pdf file, the line 8 must include this file
in the list of module assets, in the following way:
ROCKY_PLUGINS_PATH, the following additional line can be inserted between lines 3 and 5:
custom_path is the path of the folder where the module must be installed,
relative to the source directory being processed.
File spherical_region.cu
This file includes the C++ code that implements the intended functionality of the module during the execution of a simulation. Every function defined in this file must be executed at strategic points when a simulation is running in Rocky. These points are defined via the simulation hooks described in the chapter Solver Hooks, and must therefore be chosen carefully by taking into account what operations the module must perform and what information provided by Rocky will be necessary for their execution. In order to interact with Rocky, the Solver SDK classes and methods described in chapter Class Reference must be used.ROCKY_PLUGIN_INITIALIZE_CUDA function in order to allocate
memory in the GPU and to copy the module data to the GPU, as described below.
In line 5, a custom data structure ModuleData is defined, that will be used to
store the data needed by the module. In this case, the structure
includes the coordinates of the center point*, the radius of the sphere
and an index to identify the particle scalar that will
be used to identify the particles inside the spherical region.
*In order to simplify the code, the
Rocky Solver SDK provides the struct
double3 for defining 3D points and 3D
vectors. It includes as data members
the three coordinates x, y and z. Special operators for this struct are also
defined, for addition, subtraction, and
multiplication by a scalar. Moreover,
the functions dot, cross, and get_norm
are also provided, for obtaining the
dot product, the cross product, and the
norm, respectively.
In line 12, the name and the version of the module implemented by the
code in the file are defined. Afterwards, between lines 14 and 23, the
code associated to the hook ROCKY_PLUGIN_CONFIGURE is defined. Here, an instance of the
custom struct is allocated and the corresponding pointer is named module_data. Next,
the values of the four module parameters entered through the Rocky UI
are retrieved from the function argument input_data, and assigned to the
corresponding variables in the object pointed by module_data. Finally, in line
23, this pointer is converted to a void pointer in order to assign it to
the function argument data, which Rocky will store and make available in
subsequent stages of the simulation.
Between lines 26 and 31, the code associated to the hook ROCKY_PLUGIN_SETUP is defined.
As in other preprocessing hooks, the arguments provided are an IRockyModel
object, named simply model, and the void pointer that was defined
previously in ROCKY_PLUGIN_CONFIGURE. In order to have access to the data
stored previously, the void pointer must be converted back into a ModuleData
pointer, as it is done in line 28. In this specific case, the only operation performed in
this block is to define a particle scalar variable that will be used to
mark particles inside the spherical region. In line 29, the object that
handles operations with particle scalars is defined. Subsequently, in
line 30 the custom particle scalar is specified. The two arguments in
the add function are the label that must associated to the particle
scalar in the Rocky UI, and the unit. After creating the particle
scalar, the add function will return an integer index that, in this case,
will be stored in module_data->scalar_index. Any subsequent operation involving that specific
particle scalar will require the use of that index.
In order to avoid precision loss because of roundoff errors, Rocky works
internally with dimensionless variables. Therefore, the module has to
nondimensionalize all custom parameters previously defined, in order to
use them in operations involving data coming from the Rocky solver. This
operation is made in the block beginning at line 33, within the
ROCKY_PLUGIN_NON_DIMENSIONALIZE hook. In this case all parameters are lengths,
so they are nondimensionalized using the length factor provided by the
corresponding method of the model object.
Between lines 42 and 50, the module data is copied to the GPU
devices. This kind of operation is mandatory when a
module is intended for execution in single or multi-GPU configurations.
The hook ROCKY_PLUGIN_INITIALIZE_CUDA provides two generic pointers as
arguments: host_data and device_data. The
first one points to the object that currently stores the data defined in
the previous pre-processing functions. The objective of this function is
to allocate a memory block of the right size in the GPU devices, copy
the module data pointed by host_data to that memory block, and finally assign
to device_data the pointer that points to the beginning of that block. This is
done in lines 47 (memory allocation), 48 (data copy) and 49 (conversion
to a generic pointer and assigment device_data).
The only operation performed by the module during the time iterative
process is the one implemented in the block beginning at line 52. This
operation is associated to the ROCKY_PLUGIN_POST_MOVE_PARTICLES
hook, which is executed inside a loop over the active particles in the
simulation, after the position and
velocities of the particles were updated in Rocky. In line 58, the
distance between the centroid of the current particle and the center of
the spherical region is calculated. If that distance is less than the
region radius, the particle is considered to be inside the region. The
scalar value associated to the particle is set be equal to 1 in that
case, and equal to 0 otherwise. The block is closed by the
ROCKY_PLUGIN_POST_MOVE_PARTICLES_END function
call, during which Rocky takes care of some additional operations needed
to execute the module code correctly. Note that any of the functions associated to a
hook executed during the time iterative loop must be closed by a similar function.
Finally, the ROCKY_PLUGIN_TEAR_DOWN hook frees the memory allocated for the local data
structure in the CPU memory area. The equivalent operation on the GPU
memory area is made in the ROCKY_PLUGIN_TEAR_DOWN_CUDA hook.
Building procedures
This section shows a way to compile the module spherical_region described in the
previous section.
Building in Centos 7
In theconda command prompt that was activated in section
Build Tools - Linux, navigate to the spherical_region
directory and execute the following commands:
spherical_region module is available in the folder pointed by the ROCKY_PLUGINS_PATH
environment variable:
Building in Windows 10
Open thee x64 Native Tools Command Prompt for VS 2022, navigate to the spherical_region
directory and execute the following commands:
spherical_region module is available in the folder pointed to by the ROCKY_PLUGINS_PATH
environment variable:
Running the module in Rocky
After having completed successfully all the steps described in previous sections, the new module will be ready to be executed in a Rocky simulation. Restart the Rocky application in order for it to acknowledge the recently created module. When selecting the Modules item in the Data panel, the name of the new module must appear in the list of modules recognized by Rocky, displayed in the Data Editors panel. The activation of the module in a simulation requires that the corresponding checkbox is turned on, as shown in Figure 5.1.
spherical_region.py. The default values displayed are those defined
in that file. Users of the module can alter those values at their
convenience. On the other hand, if the
Info tab is selected, the reference data included in the spherical_region.plugin is displayed in the
panel.



A module implementing a custom
As an example of the implementation of a custom model using the Rocky Solver SDK, this section describes the code associated to a custom module that implements a contact model, including the calculation of both components of the contact force, as well as the calculation of the impact energy. A contact model may include in the most general case the calculation of both components of the contact force, and, if necessary, the calculation of the impact energy during a collision. Usually the tangential component of the contact force depends on the normal component, and the impact energy on both of them. Because of that, they are calculated in a fixed sequence, as shown schematically in Figure 5.4.
Module specification file
The following listing reproduces the Python file that includes the specifications for the custom module, which is named simply asContact Model Example.
Implementation of the contact model
This section describes the different parts that make up thecustom_model_example.cu file that
includes the C++ code that implements the custom models.
ROCKY_CUDA_API that enables the compilation of the module in
CUDA and, in turn, enables the possibility of the module being used in a
simulation processed on GPU. Subsequently, between lines 5 and 9, it is
defined a local struct that will hold the values entered through the Rocky
UI for the two material interaction parameters that the module uses.
Then, between lines 11 and 19, another local struct is defined, that will
store the indices attributed in runtime to the contact scalars that the
module will create. Additionally,
that struct includes an array to store the values of the custom properties
associated to all material interactions in the project.
Note: Here ppi stands for particle-particle index, while pti stands for particle-triangle index.
ROCKY_PLUGIN_CONFIGURE hook, the values for the two module parameters per material
interaction, entered through the Rocky UI, are stored in the array
within the module local struct defined previously. Subsequently, in the ROCKY_PLUGIN_SETUP
hook, contact scalars are created for storing the values of the contact
forces calculated in the module. This storage is necessary because both
the normal and tangential force models being implemented depend upon the
history; therefore, in order to calculate the force values at a given
timestep, the values from the previous timestep are required to be
available.
In the ROCKY_PLUGIN_INITIALIZE_CUDA hook, the local module data is copied to GPU devices after
being allocated the required memory block there. On the other hand, that
memory block is freed in the ROCKY_PLUGIN_TEAR_DOWN_CUDA hook. An equivalent operation for the
memory allocated in CPU is made in the ROCKY_PLUGIN_TEAR_DOWN hook.
set_normal_force method of the output_data
object. This operation is mandatory, because only in that way Rocky will
become aware of the new
force value calculated by the module. From that point on, Rocky will use
internally that force for solving the particles’ motion equations and
perform all the other operations that may need it.
As a final operation of the normal force calculation, the new normal
contact force value is set also to the corresponding contact scalar in
line 135, in order to preserve it and make it available during the next
time iteration.
set_sliding method of the output_data object, in line 165. In
case that the Coulomb limit is not exceeded, the sliding condition is
always false, and that value is set in line 169.
The tangential contact force just calculated is passed also to Rocky by
means of the set_tangential_force method of the output_data object, in line 172. The specification
of both the sliding state and the tangential force vector is mandatory
for a module implementing a tangential contact force model. As a final
step, the components of the tangential force are stored in the contact
scalars previously created, in order to make available those values in
the next time iteration.
set_home_impact_energy and set_near_impact_energy
methods of the output_data object, respectively.
After the module is built following the steps given in section 5.1.6,
the module will be available to be used in Rocky. When the Contact Model Example
is enabled in a Rocky project, the models
of normal and tangential forces, besides the impact energy model
implemented on it will override built-in models in those categories.
This will be indicated within the Physics | Momentum tab, which
will appear as shown in Figure 5.5.

A module implementing variable properties
As an example of the implementation of modules that override built-in
constant properties in Rocky, this section describes a module that
converts the particle’s thermal conductivity into a function of the
temperature. Additionally, the module makes possible also to define the
temperature of boundaries as a function of the position. The functional
relation for the thermal conductivity that the module implements is the
following:
where and are two module parameters and is the particle temperature.
On the other hand, the modules allows to define the temperature of boundary triangles as a
quadratic function of the -coordinate:
where and are two module parameters and is the -coordinate of the triangle’s centroid.
Module specification file
The following listing reproduces the Python file with the specifications of the module, which is named asVariable Properties Example.
ThermalConductivity) which the solver will use to store the values calculated by the
module. A similar specification is made between lines 30 and 35 for the
temperature of the boundary triangles. For more details about the
specification of variable properties, please refer to section
2.9.
Since the module will define the thermal conductivity as a variable
property for particles exclusively, only parameter values from materials associated to particles
will be effectively used by the module.
Module implementation
This section describes the C++ code of the main parts of thevariable_properties_example.cu file.
The code from initialization and termination hooks related to CUDA operations is omitted,
since it was described in previous examples. This code implements the calculation of the
property values per particle and per boundary triangle that the module must deliver to the Rocky
solver. In turn, the Rocky solver will use these values internally in
the thermal model calculations.
ROCKY_PLUGIN_CONFIGURE hook receives the parameter values
delivered by the Rocky UI, and then stores them in the data structures
previously defined. On the other hand, the ROCKY_PLUGIN_SETUP hook is only used to
verify if the project has the thermal model enabled. The module will
perform its operations only if that is the case. Then the module
parameters are non-dimensionalized, in order to be used in the
calculations that must be made later on, during the simulation.
Since the module is intended to be used with static boundaries only, it
needs to prescribe the temperature of the boundary triangles only once,
during the initialization. In order to do that, the
ROCKY_PLUGIN_INITIALIZE_TRIANGLE hook is used in
the following way:

ROCKY_PLUGIN_PRE_FORCE_ON_PARTICLES hook is the most adequate for that task,
as shown in the following code block:
set_thermal_conductivity method. The Rocky Solver SDK includes
equivalent methods associated to particles, triangles and contacts, for
every property that can be overridden by a custom module. Figure 5.8
shows an example of a particle bed in which the
particles’ thermal conductivity has been calculated by the module
described in this section.
When a custom module that implements a variable property is active in a
simulation, Rocky will issue a warning message, indicating which
properties are being overridden by the module. Although the constant
property values being replaced will remain editable in the Rocky UI, it
is important to note that they will be ignored in all the calculations
done by the Rocky solver.

A module implementing a custom SPH model
As an example of the implementation of a custom SPH model using the Rocky Solver SDK, this section describes the code associated to a custom module that implements simultaneously the Large Eddy Simulation (LES) turbulence model*, the Morris viscosity model and the Free Slip boundary condition type. Be aware that when implementing forces or parameters directly related to forces, a custom module must implement the calculation of all relevant forces, as it is being done in this example. Note that not all forces or options present in Rocky are replicated in this module example, but only the most important ones. *For more information about the model implemented in this example, refer to “Numerical modelling of complex turbulent free-surface flows with the SPH method: an overview”, Violeau D., Issa, R., International Journal of Numerical Methods in Fluids, vol. 53, pp. 277-304, 2007.Module specification file
The following listing reproduces the Python file that includes the specifications for the custom module, which is named simply asLES Turbulence Model.
Implementation of the SPH LES turbulence model
This section describes the different parts that make up thesph_les_model.cu file that
includes the C++ code implementing the custom models.
ROCKY_CUDA_API that enables the compilation of the module in
CUDA and, in turn, enables the module to be used in a simulation
processed on GPU. Subsequently, between lines 8 and 13, it is defined a
local struct that will be used to store the data needed by the module. In
the current module, a parameter needed is the index turbulent_viscosity_index assigned to a
scalar that stores the turbulent viscosity calculated during the
simulation. The other two parameters are needed to store the data
entered by the user via the Rocky UI.
ROCKY_PLUGIN_CONFIGURE hook, the values for the module parameter are stored in the
array within the module local struct defined previously. Subsequently, in
the ROCKY_PLUGIN_SETUP hook, a scalar is created for storing the values of the turbulent
viscosity calculated in the module. This scalar is needed as the values
from neighbor elements must be accumulated in order to approximate the
tensor of strain rate as a previous step for calculating the turbulent
viscosity. In the lines 68-70, three operations made on the scalar that
stores turbulent viscosity are made: reset values, sum of values
associated to elements in the halo regions between GPUs, and copy of
values of those elements between GPUs. The last two operations are done
only when the module is being employed in simulations with multi GPU.
According to the specification made, the order of operations in the
module is the following:
ROCKY_PLUGIN_INITIALIZE_CUDA hook, the local module data is copied to GPU devices after
being allocated the required memory block there.
d_shear, is calculated according to the equation
available in section 2.4 of the SPH Technical Manual.
SPH LES Turbulence Model is enabled in a Rocky project, the models
of turbulence, viscosity and boundary forces implemented on it will
override built-in models in those categories.