下面显示了用户自定义法的编码示例。第一个文件
uclib.c 定义两个函数来计算从
Simcenter STAR-CCM+ 调用的力和力矩:
#include "uclib.h"
void calculateForce(Real*, int, CoordReal*, unsigned int*, CoordReal*);
void calculateMoment(Real*, int, CoordReal*, unsigned int*, CoordReal*);
void
USERFUNCTION_EXPORT uclib()
{
/* Register user functions here */
ucfunc(calculateForce, "PointSetProfile", "Calculate Force");
ucarg(calculateForce, "Point", "Coord", sizeof(CoordReal[3]));
ucarg(calculateForce, "Point", "BodyId", sizeof(unsigned int));
ucarg(calculateForce, "Point", "BodyPosition", sizeof(CoordReal[3]));
ucfunc(calculateMoment, "PointSetProfile", "Calculate Moment");
ucarg(calculateMoment, "Point", "Coord", sizeof(CoordReal[3]));
ucarg(calculateMoment, "Point", "BodyId", sizeof(unsigned int));
ucarg(calculateMoment, "Point", "BodyPosition", sizeof(CoordReal[3]));
}
以下用户程序对耦合端点之间作用的弹簧进行建模。一个端点附加到体,另一个端点附加到环境或另一个体。
#include "uclib.h"
void USERFUNCTION_EXPORT
calculateForce(Real (*result)[3], int size, CoordReal (*position)[3],
unsigned int *bodyId, CoordReal (*bodyPosition)[3])
{
CoordReal delta[3];
Real stiffness = 20.; /* Spring stiffness */
int i, j;
if (size != 2) /* If the number of end points is unequal 2, do not execute the following coding. */
/* This if-block is essential for parallel simulations. The DFBI point */
/* set is filled with the two end points on the master node only. */
return;
for (j = 0; j < 3; ++j) /* Compute force vectors at the end points */
{
delta[j] = position[1][j] - position[0][j];
result[1][j] = -stiffness*delta[j]; /* Force at second end point */
result[0][j] = -result[1][j]; /* Force at first end point */
}
}
void USERFUNCTION_EXPORT
calculateMoment(Real (*result)[3], int size, CoordReal (*position)[3],
unsigned int *bodyId, CoordReal (*bodyPosition)[3])
{
int i, j;
for (i = 0; i < size; ++i)
for (j = 0; j < 3; ++j)
result[i][j] = 0.; /* Calculate no additional moment. */
}