initial changes towards using the potential

This commit is contained in:
Daniel Knüttel 2019-07-15 21:45:32 +02:00
parent d380860e78
commit d2336cded2
4 changed files with 75 additions and 7 deletions

View File

@ -10,6 +10,28 @@ static float
interaction_force_function interaction_force_function
( float r ( float r
, float * coefficients) , float * coefficients)
{
float result = 0;
int i;
result = coefficients[0];
for(i = 1; i < 7; i++)
{
result += coefficients[i] * (powf(r, i - 1) + coefficients[8]*powf(r, i));
}
result *= coefficients[7] * expf(coefficients[8] * (r - coefficients[9]));
result += coefficients[10] * coefficients[11] * expf(coefficients[11] * (r - coefficients[12]));
result += coefficients[13]
* 2 * (r - coefficients[15])
* coefficients[14] * expf(coefficients[14] * raise2(r - coefficients[15]));
result += coefficients[16]
* 2 * (r - coefficients[18])
* coefficients[17] * expf(coefficients[17] * raise2(r - coefficients[18]));
return result;
}
static float
interaction_potential_function
( float r
, float * coefficients)
{ {
float result = 0; float result = 0;
int i; int i;
@ -49,6 +71,30 @@ interaction_ufunc_force
} }
} }
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;
}
}
static void static void
interaction_ufunc_float2D interaction_ufunc_float2D
@ -159,6 +205,10 @@ static char interaction_types[] =
{ NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT}; { NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT};
static char force_types[] = static char force_types[] =
{ NPY_FLOAT, NPY_FLOAT}; { NPY_FLOAT, NPY_FLOAT};
static char potential_types[] =
{ NPY_FLOAT, NPY_FLOAT};
static PyUFuncGenericFunction potential_funcs[1] =
{ interaction_ufunc_potential};
static PyUFuncGenericFunction force_funcs[1] = static PyUFuncGenericFunction force_funcs[1] =
{ interaction_ufunc_force}; { interaction_ufunc_force};
static PyUFuncGenericFunction interaction_funcs[1] = static PyUFuncGenericFunction interaction_funcs[1] =
@ -254,6 +304,21 @@ interaction_UFuncWrapper_init
, 0); , 0);
break; break;
} }
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;
}
default: default:
{ {
PyErr_SetString(PyExc_ValueError, "unknown ufunc type, must be 0 or 1"); PyErr_SetString(PyExc_ValueError, "unknown ufunc type, must be 0 or 1");

View File

@ -8,15 +8,15 @@ c = np.array(
, 0 # x**5 , 0 # x**5
, 0 # x**6) , 0 # x**6)
, 1 # *c*exp( , 1 # *c*exp(
, -1 # c , -0.7 # c
, 0 # (r - c)) , 0 # (r - c))
, -1 # + c*exp( , 0 # + c*exp(
, -.1 # c , -.1 # c
, 2 # (r - c)) , 2 # (r - c))
, -4 # + c*exp( , -0 # + c*exp(
, -0.05 # c , -0.05 # c
, 40 # (r - c)**2) , 40 # (r - c)**2)
, 10 # + c*exp( , 0 # + c*exp(
, -0.05 # c , -0.05 # c
, 20 # (r - c)**2) , 20 # (r - c)**2)
, 1] # dt , 1] # dt

View File

@ -5,7 +5,10 @@ import matplotlib.pyplot as plt
from coefficients import c from coefficients import c
force_function = UFuncWrapper(0, c) force_function = UFuncWrapper(0, c)
potential_function = UFuncWrapper(2, c)
r = np.arange(0, 100, 0.02, dtype=np.float16) r = np.arange(0, 100, 0.02, dtype=np.float16)
plt.plot(r, force_function(r)) f, = plt.plot(r, force_function(r), label="force")
p, = plt.plot(r, potential_function(r), label="potential")
plt.legend(handles=[f, p])
plt.show() plt.show()

View File

@ -15,8 +15,8 @@ borders_x = [-100, 100]
borders_y = [-100, 100] borders_y = [-100, 100]
n_particles = 600 n_particles = 600
frames = 100 frames = 100
spawn_restriction = 1.1 spawn_restriction = 3
dt = 0.01 dt = 0.1
c[-1] = dt c[-1] = dt
x_coords = np.random.uniform(borders_x[0] / spawn_restriction, borders_x[1] / spawn_restriction, n_particles).astype(np.float16) x_coords = np.random.uniform(borders_x[0] / spawn_restriction, borders_x[1] / spawn_restriction, n_particles).astype(np.float16)