2019-07-08 09:21:51 +00:00
|
|
|
#include "interaction.h"
|
|
|
|
#include "math.h"
|
|
|
|
|
2019-07-13 13:20:50 +00:00
|
|
|
//#define WARN_NAN_OCCUR
|
|
|
|
//#define WARN_CORRECTED_R_0
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
#define raise2(x) (x)*(x)
|
|
|
|
|
|
|
|
static float
|
|
|
|
interaction_force_function
|
|
|
|
( float r
|
|
|
|
, float * coefficients)
|
2019-07-15 19:45:32 +00:00
|
|
|
{
|
|
|
|
float result = 0;
|
|
|
|
int i;
|
2019-07-15 20:27:12 +00:00
|
|
|
result = coefficients[0] * coefficients[8];
|
2019-07-15 19:45:32 +00:00
|
|
|
for(i = 1; i < 7; i++)
|
|
|
|
{
|
2019-07-15 20:27:12 +00:00
|
|
|
result += coefficients[i] * (powf(r, i - 1) / i + coefficients[8]*powf(r, i));
|
2019-07-15 19:45:32 +00:00
|
|
|
}
|
|
|
|
result *= coefficients[7] * expf(coefficients[8] * (r - coefficients[9]));
|
2019-07-15 20:27:12 +00:00
|
|
|
result += coefficients[10] * coefficients[11]
|
|
|
|
* expf(coefficients[11] * (r - coefficients[12]));
|
2019-07-15 19:45:32 +00:00
|
|
|
result += coefficients[13]
|
|
|
|
* 2 * (r - coefficients[15])
|
2019-07-15 20:27:12 +00:00
|
|
|
* coefficients[14]
|
|
|
|
* expf(coefficients[14] * raise2(r - coefficients[15]));
|
2019-07-15 19:45:32 +00:00
|
|
|
result += coefficients[16]
|
|
|
|
* 2 * (r - coefficients[18])
|
2019-07-15 20:27:12 +00:00
|
|
|
* coefficients[17]
|
|
|
|
* expf(coefficients[17] * raise2(r - coefficients[18]));
|
2019-07-15 19:45:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static float
|
|
|
|
interaction_potential_function
|
|
|
|
( float r
|
|
|
|
, float * coefficients)
|
2019-07-08 09:21:51 +00:00
|
|
|
{
|
|
|
|
float result = 0;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < 7; i++)
|
|
|
|
{
|
|
|
|
result += coefficients[i] * powf(r, i);
|
|
|
|
}
|
|
|
|
result *= coefficients[7] * expf(coefficients[8] * (r - coefficients[9]));
|
|
|
|
result += coefficients[10] * expf(coefficients[11] * (r - coefficients[12]));
|
|
|
|
result += coefficients[13] * expf(coefficients[14] * raise2(r - coefficients[15]));
|
|
|
|
result += coefficients[16] * expf(coefficients[17] * raise2(r - coefficients[18]));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
interaction_ufunc_force
|
|
|
|
( char ** args
|
|
|
|
, npy_intp * dimensions
|
|
|
|
, npy_intp * steps
|
2019-07-12 18:24:02 +00:00
|
|
|
, void * data)
|
2019-07-08 09:21:51 +00:00
|
|
|
{
|
|
|
|
char * in = args[0]
|
2019-07-12 08:48:49 +00:00
|
|
|
, * out = args[1];
|
2019-07-08 09:21:51 +00:00
|
|
|
npy_intp n = dimensions[0];
|
|
|
|
npy_intp in_step = steps[0]
|
2019-07-12 08:48:49 +00:00
|
|
|
, out_step = steps[1];
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
npy_intp i;
|
|
|
|
|
2019-07-12 18:24:02 +00:00
|
|
|
float * coefficients = (float *) data;
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
*(float *)out = interaction_force_function(*(float *)in, coefficients);
|
|
|
|
out += out_step;
|
|
|
|
in += in_step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 19:45:32 +00:00
|
|
|
static void
|
|
|
|
interaction_ufunc_potential
|
|
|
|
( char ** args
|
|
|
|
, npy_intp * dimensions
|
|
|
|
, npy_intp * steps
|
|
|
|
, void * data)
|
|
|
|
{
|
|
|
|
char * in = args[0]
|
|
|
|
, * out = args[1];
|
|
|
|
npy_intp n = dimensions[0];
|
|
|
|
npy_intp in_step = steps[0]
|
|
|
|
, out_step = steps[1];
|
|
|
|
|
|
|
|
npy_intp i;
|
|
|
|
|
|
|
|
float * coefficients = (float *) data;
|
|
|
|
|
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
*(float *)out = interaction_potential_function(*(float *)in, coefficients);
|
|
|
|
out += out_step;
|
|
|
|
in += in_step;
|
|
|
|
}
|
|
|
|
}
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
interaction_ufunc_float2D
|
|
|
|
( char ** args
|
|
|
|
, npy_intp * dimensions
|
|
|
|
, npy_intp * steps
|
|
|
|
, void * data)
|
|
|
|
{
|
2019-07-15 14:05:27 +00:00
|
|
|
//NPY_BEGIN_THREADS_DEF;
|
2019-07-08 09:21:51 +00:00
|
|
|
npy_intp i;
|
|
|
|
npy_intp j;
|
|
|
|
npy_intp n = dimensions[0];
|
|
|
|
|
|
|
|
char * x_old = args[0]
|
|
|
|
, * y_old = args[1]
|
2019-07-15 14:56:16 +00:00
|
|
|
, * p_x_new = args[2]
|
|
|
|
, * p_y_new = args[3];
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
npy_intp x_old_steps = steps[0]
|
|
|
|
, y_old_steps = steps[1]
|
2019-07-15 14:56:16 +00:00
|
|
|
, p_x_new_steps = steps[2]
|
|
|
|
, p_y_new_steps = steps[3];
|
2019-07-12 08:48:49 +00:00
|
|
|
|
2019-07-13 12:31:37 +00:00
|
|
|
float * coefficients = (float *) data;
|
|
|
|
float dt = coefficients[19];
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
// Compute the new momenta:
|
|
|
|
|
|
|
|
// Stuff we will need:
|
|
|
|
float r; // Distance between interacting particles.
|
|
|
|
float delta_x_e; // x-component of unit direction vector.
|
|
|
|
float delty_y_e; // y-component of unit direction vector.
|
|
|
|
float delta_p_x;
|
|
|
|
float delta_p_y;
|
|
|
|
|
|
|
|
// The current x_old[i], y_old[i] coordinates.
|
|
|
|
float this_x_i;
|
|
|
|
float this_y_i;
|
|
|
|
// The current x_old[j], y_old[j] coordinates.
|
|
|
|
float this_x_j;
|
|
|
|
float this_y_j;
|
|
|
|
|
2019-07-15 14:05:27 +00:00
|
|
|
//NPY_BEGIN_THREADS;
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
this_x_i = *(float *)(x_old + i*x_old_steps);
|
|
|
|
this_y_i = *(float *)(y_old + i*y_old_steps);
|
|
|
|
|
2019-07-15 14:56:16 +00:00
|
|
|
*(float *)(p_x_new + i*p_x_new_steps) = 0;
|
|
|
|
*(float *)(p_y_new + i*p_y_new_steps) = 0;
|
2019-07-08 09:21:51 +00:00
|
|
|
// compute and add the momentum offset
|
|
|
|
for(j = 0; j < i; j++)
|
|
|
|
{
|
|
|
|
this_x_j = *(float *)(x_old + j*x_old_steps);
|
|
|
|
this_y_j = *(float *)(y_old + j*y_old_steps);
|
|
|
|
|
|
|
|
// Compute distance and direction between particles i,j.
|
|
|
|
r = sqrtf(raise2(this_x_i - this_x_j) + raise2(this_y_i - this_y_j));
|
2019-07-13 12:31:37 +00:00
|
|
|
// r = 0, we cannot compute the direction.
|
|
|
|
// In this case choose the direction randomly.
|
|
|
|
if(r > 0)
|
|
|
|
{
|
|
|
|
delta_x_e = (this_x_i - this_x_j) / r;
|
|
|
|
delty_y_e = (this_y_i - this_y_j) / r;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long int rand = random();
|
2019-07-13 13:20:50 +00:00
|
|
|
float random_angle = (((float)rand) / ((float)RAND_MAX)) * 2 * M_PI;
|
2019-07-13 12:31:37 +00:00
|
|
|
#ifdef WARN_CORRECTED_R_0
|
2019-07-13 13:20:50 +00:00
|
|
|
printf("Warning: corrected r = 0 with random angle pi*%f\n", random_angle / M_PI);
|
2019-07-13 12:31:37 +00:00
|
|
|
#endif
|
|
|
|
delta_x_e = cosf(random_angle);
|
|
|
|
delty_y_e = sinf(random_angle);
|
|
|
|
}
|
|
|
|
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
// Update the momenta.
|
|
|
|
delta_p_x = delta_x_e * interaction_force_function(r, coefficients);
|
|
|
|
delta_p_y = delty_y_e * interaction_force_function(r, coefficients);
|
2019-07-13 12:31:37 +00:00
|
|
|
#ifdef WARN_NAN_OCCUR
|
|
|
|
if(isnan(delta_p_x))
|
|
|
|
{
|
|
|
|
printf("Warning: delta_p_x is NaN, after r = %f\n", r);
|
|
|
|
}
|
|
|
|
if(isnan(delta_p_y))
|
|
|
|
{
|
|
|
|
printf("Warning: delta_p_y is NaN, after r = %f\n", r);
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-12 19:51:24 +00:00
|
|
|
//printf("%d, %d: %f, %f\n", i, j, delta_p_x, delta_p_y);
|
2019-07-15 20:27:12 +00:00
|
|
|
*(float *)(p_x_new + i*p_x_new_steps) -= dt*delta_p_x;
|
|
|
|
*(float *)(p_y_new + i*p_y_new_steps) -= dt*delta_p_y;
|
|
|
|
*(float *)(p_x_new + j*p_x_new_steps) += dt*delta_p_x;
|
|
|
|
*(float *)(p_y_new + j*p_y_new_steps) += dt*delta_p_y;
|
2019-07-08 09:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-15 14:05:27 +00:00
|
|
|
//NPY_END_THREADS;
|
2019-07-08 09:21:51 +00:00
|
|
|
}
|
2019-07-12 17:53:13 +00:00
|
|
|
static char interaction_types[] =
|
2019-07-15 14:56:16 +00:00
|
|
|
{ NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT};
|
2019-07-12 17:53:13 +00:00
|
|
|
static char force_types[] =
|
|
|
|
{ NPY_FLOAT, NPY_FLOAT};
|
2019-07-15 19:45:32 +00:00
|
|
|
static char potential_types[] =
|
|
|
|
{ NPY_FLOAT, NPY_FLOAT};
|
|
|
|
static PyUFuncGenericFunction potential_funcs[1] =
|
|
|
|
{ interaction_ufunc_potential};
|
2019-07-12 17:53:13 +00:00
|
|
|
static PyUFuncGenericFunction force_funcs[1] =
|
|
|
|
{ interaction_ufunc_force};
|
|
|
|
static PyUFuncGenericFunction interaction_funcs[1] =
|
|
|
|
{ interaction_ufunc_float2D};
|
2019-07-08 09:21:51 +00:00
|
|
|
|
2019-07-12 08:48:49 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
PyObject_HEAD
|
2019-07-13 12:31:37 +00:00
|
|
|
float coefficients[20];
|
2019-07-12 08:48:49 +00:00
|
|
|
PyObject * ufunc;
|
2019-07-12 18:24:02 +00:00
|
|
|
void *data[1];
|
2019-07-12 08:48:49 +00:00
|
|
|
} interaction_UFuncWrapper;
|
|
|
|
|
|
|
|
static int
|
|
|
|
interaction_UFuncWrapper_init
|
|
|
|
( interaction_UFuncWrapper * self
|
|
|
|
, PyObject * args
|
|
|
|
, PyObject * kwds)
|
|
|
|
{
|
2019-07-12 09:07:24 +00:00
|
|
|
// 0: ufunc_force
|
|
|
|
// 1: interaction2D
|
2019-07-12 08:48:49 +00:00
|
|
|
char type;
|
|
|
|
PyObject * coefficients;
|
2019-07-12 11:51:43 +00:00
|
|
|
int i;
|
|
|
|
PyObject * this_coefficient;
|
2019-07-12 08:48:49 +00:00
|
|
|
|
2019-07-12 17:53:13 +00:00
|
|
|
char *kwords[] = { "type_", "coefficients", NULL};
|
|
|
|
|
|
|
|
if(!PyArg_ParseTupleAndKeywords(args, kwds, "bO", kwords, &type, &coefficients))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!PySequence_Check(coefficients))
|
2019-07-12 09:07:24 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-07-13 12:31:37 +00:00
|
|
|
if(PySequence_Size(coefficients) != 20)
|
2019-07-12 09:07:24 +00:00
|
|
|
{
|
2019-07-13 12:31:37 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "coefficients must have length 20");
|
2019-07-12 09:07:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:51:43 +00:00
|
|
|
// copy the coefficients.
|
2019-07-13 12:31:37 +00:00
|
|
|
for(i = 0; i < 20; i++)
|
2019-07-12 11:51:43 +00:00
|
|
|
{
|
2019-07-12 17:53:13 +00:00
|
|
|
this_coefficient = PySequence_GetItem(coefficients, i);
|
|
|
|
if(!this_coefficient)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2019-07-12 11:51:43 +00:00
|
|
|
// XXX: PyFloat_AsDouble might call python code,
|
|
|
|
// so make sure that nothing bad can happen.
|
|
|
|
Py_INCREF(this_coefficient);
|
|
|
|
self->coefficients[i] = PyFloat_AsDouble(this_coefficient);
|
|
|
|
Py_DECREF(this_coefficient);
|
|
|
|
if(PyErr_Occurred())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 18:24:02 +00:00
|
|
|
self->data[0] = (void *)self->coefficients;
|
2019-07-12 11:51:43 +00:00
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
self->ufunc = PyUFunc_FromFuncAndData(
|
2019-07-12 18:24:02 +00:00
|
|
|
force_funcs // func
|
|
|
|
, self->data // data
|
|
|
|
, force_types //types
|
|
|
|
, 1 // ntypes
|
|
|
|
, 1 // nin
|
|
|
|
, 1 // nout
|
|
|
|
, PyUFunc_None // identity
|
|
|
|
, "force_function" // name
|
|
|
|
, "computes the scalar force between two particles with given coefficients" // doc
|
|
|
|
, 0); // unused
|
2019-07-12 11:51:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
self->ufunc = PyUFunc_FromFuncAndData(
|
|
|
|
interaction_funcs
|
2019-07-12 18:24:02 +00:00
|
|
|
, self->data
|
2019-07-12 11:51:43 +00:00
|
|
|
, interaction_types
|
2019-07-15 14:56:16 +00:00
|
|
|
, 1 // ntypes
|
|
|
|
, 2 // nin
|
|
|
|
, 2 // nout
|
2019-07-12 11:51:43 +00:00
|
|
|
, PyUFunc_None
|
|
|
|
, "interaction2D"
|
|
|
|
, "Update the momenta according to the given coefficients and positions"
|
|
|
|
, 0);
|
|
|
|
break;
|
|
|
|
}
|
2019-07-15 19:45:32 +00:00
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
self->ufunc = PyUFunc_FromFuncAndData(
|
|
|
|
potential_funcs // func
|
|
|
|
, self->data // data
|
|
|
|
, potential_types //types
|
|
|
|
, 1 // ntypes
|
|
|
|
, 1 // nin
|
|
|
|
, 1 // nout
|
|
|
|
, PyUFunc_None // identity
|
|
|
|
, "potential_function" // name
|
|
|
|
, "computes the scalar potential between two particles with given coefficients" // doc
|
|
|
|
, 0); // unused
|
|
|
|
break;
|
|
|
|
}
|
2019-07-12 11:51:43 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_ValueError, "unknown ufunc type, must be 0 or 1");
|
2019-07-12 17:53:13 +00:00
|
|
|
return -1;
|
2019-07-12 11:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 17:53:13 +00:00
|
|
|
Py_INCREF(self->ufunc);
|
|
|
|
return 0;
|
2019-07-12 08:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
interaction_UFuncWrapper_call
|
2019-07-12 11:51:43 +00:00
|
|
|
(interaction_UFuncWrapper * self
|
|
|
|
, PyObject * args
|
|
|
|
, PyObject * kwargs)
|
2019-07-12 08:48:49 +00:00
|
|
|
{
|
2019-07-12 11:51:43 +00:00
|
|
|
return PyObject_Call(self->ufunc, args, kwargs);
|
2019-07-12 08:48:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 17:53:13 +00:00
|
|
|
static PyMemberDef interaction_UFuncWrapper_members[] =
|
|
|
|
{
|
|
|
|
{"ufunc", T_OBJECT_EX, offsetof(interaction_UFuncWrapper, ufunc), 0, "ufunc"},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyTypeObject interaction_UFuncWrapperType =
|
|
|
|
{
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
.tp_name = "brown.interaction.UFuncWrapper",
|
|
|
|
.tp_doc = "A wrapper that wraps the ufuncs for interaction and force, storing the coefficients",
|
|
|
|
.tp_basicsize = sizeof(interaction_UFuncWrapper),
|
|
|
|
.tp_itemsize = 0,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
|
|
.tp_new = PyType_GenericNew,
|
|
|
|
.tp_init = (initproc) interaction_UFuncWrapper_init,
|
|
|
|
.tp_call = interaction_UFuncWrapper_call,
|
|
|
|
.tp_members = interaction_UFuncWrapper_members
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-08 09:21:51 +00:00
|
|
|
static PyMethodDef InteractionMethods[] = {
|
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static struct PyModuleDef moduledef = {
|
|
|
|
PyModuleDef_HEAD_INIT
|
|
|
|
, "brown.interaction"
|
|
|
|
, NULL
|
|
|
|
, -1
|
|
|
|
, InteractionMethods
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit_interaction(void)
|
|
|
|
{
|
2019-07-12 17:53:13 +00:00
|
|
|
PyObject * module;
|
|
|
|
|
|
|
|
if(PyType_Ready(&interaction_UFuncWrapperType) < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
module = PyModule_Create(&moduledef);
|
|
|
|
if(!module)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
import_array();
|
2019-07-12 09:07:24 +00:00
|
|
|
import_ufunc();
|
2019-07-08 09:21:51 +00:00
|
|
|
|
2019-07-12 17:53:13 +00:00
|
|
|
Py_INCREF(&interaction_UFuncWrapperType);
|
|
|
|
PyModule_AddObject(module, "UFuncWrapper", (PyObject *) &interaction_UFuncWrapperType);
|
2019-07-08 09:21:51 +00:00
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|