everything is technically working

This commit is contained in:
Daniel Knüttel 2019-07-12 20:24:02 +02:00
parent d3a227ac19
commit c52fbe796d
3 changed files with 52 additions and 58 deletions

View File

@ -13,7 +13,6 @@ interaction_force_function
int i;
for(i = 0; i < 7; i++)
{
printf("%x\n", coefficients);
result += coefficients[i] * powf(r, i);
}
result *= coefficients[7] * expf(coefficients[8] * (r - coefficients[9]));
@ -28,7 +27,7 @@ interaction_ufunc_force
( char ** args
, npy_intp * dimensions
, npy_intp * steps
, void ** data)
, void * data)
{
char * in = args[0]
, * out = args[1];
@ -38,9 +37,7 @@ interaction_ufunc_force
npy_intp i;
printf("DATA IS: %x\n", data);
printf("DATA[0] IS: %x\n", data[0]);
float * coefficients = (float *) data[0];
float * coefficients = (float *) data;
for(i = 0; i < n; i++)
{
@ -139,6 +136,7 @@ typedef struct
PyObject_HEAD
float coefficients[19];
PyObject * ufunc;
void *data[1];
} interaction_UFuncWrapper;
static int
@ -189,29 +187,30 @@ interaction_UFuncWrapper_init
return -1;
}
}
self->data[0] = (void *)self->coefficients;
switch(type)
{
case 0:
{
self->ufunc = PyUFunc_FromFuncAndData(
force_funcs
, (void **) &(self->coefficients)
, force_types
, 1
, 1
, 1
, PyUFunc_None
, "force_function"
, "computes the scalar force between two particles with given coefficients"
, 0);
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
break;
}
case 1:
{
self->ufunc = PyUFunc_FromFuncAndData(
interaction_funcs
, (void **) &(self->coefficients)
, self->data
, interaction_types
, 1
, 4
@ -229,8 +228,6 @@ interaction_UFuncWrapper_init
}
}
Py_INCREF(self->ufunc);
printf("PASSED = %x\n", &(self->coefficients));
printf("PASSED[0] = %x\n", self->coefficients);
return 0;
}

View File

@ -37,6 +37,6 @@ interaction_ufunc_force
( char ** args
, npy_intp * dimensions
, npy_intp * steps
, void ** data);
, void * data);
#endif

75
test.py
View File

@ -4,48 +4,45 @@ from collections import deque
from copy import copy
import matplotlib.pyplot as plt
c = np.array([5, 10, 20, 0, 0, 0, 0, 1, -2, 0, -2, -0.1, 2, 0, 0, 0, 0, 0, 0], dtype=np.float16)
print(c)
print(len(c))
#x_coords = np.array([0, 1], dtype=np.float16)
#y_coords = np.array([0, 0], dtype=np.float16)
#
#x_momenta = np.array([0, 0], dtype=np.float16)
#y_momenta = np.array([0, 0], dtype=np.float16)
#
#time = np.arange(0, 5, 1, dtype=np.float16)
#
#time_evolution = deque()
#
#for t in time:
# x_momenta, y_momenta = interaction2D(x_coords, y_coords, x_momenta, y_momenta, c)
# x_coords += x_momenta
# y_coords += y_momenta
#
# time_evolution.append(copy(x_coords))
#
#time_evolution = np.array(time_evolution)
#
#particle_one_evolution = time_evolution[:, 0]
#particle_two_evolution = time_evolution[:, 1]
#plt.subplot(2, 1, 1)
c = np.array([5, 10, 20, 30, 0, 0, 0, 1, -20, 0, -2, -0.1, 2, 0, 0, 0, 0, 0, 0], dtype=np.float16)
force_function = UFuncWrapper(0, c)
r = np.arange(0, 0.5, 0.01, dtype=np.float16)
interaction2D = UFuncWrapper(1, c)
x_coords = np.array([0, 1], dtype=np.float16)
y_coords = np.array([0, 0], dtype=np.float16)
x_momenta = np.array([0, 0], dtype=np.float16)
y_momenta = np.array([0, 0], dtype=np.float16)
time = np.arange(0, 5, 1, dtype=np.float16)
time_evolution = deque()
for t in time:
x_momenta, y_momenta = interaction2D(x_coords, y_coords, x_momenta, y_momenta)
x_coords += x_momenta
y_coords += y_momenta
time_evolution.append(copy(x_coords))
time_evolution = np.array(time_evolution)
particle_one_evolution = time_evolution[:, 0]
particle_two_evolution = time_evolution[:, 1]
plt.subplot(2, 1, 1)
r = np.arange(0, 50, 0.1, dtype=np.float16)
plt.title("Force")
plt.xlabel("particle distance")
plt.ylabel("scalar force")
print(r)
print(force_function.ufunc(r))
plt.plot(r, force_function.ufunc(r))
plt.plot(r, force_function(r))
#plt.subplot(2, 1, 2)
#plt.title("Particle x-positions over time")
#plt.xlabel("time")
#plt.ylabel("particle x-position")
#h0, = plt.plot(time, particle_one_evolution, label="particle 1")
#h1, = plt.plot(time, particle_two_evolution, label="particle 2")
#plt.legend(handles=[h0, h1])
plt.subplot(2, 1, 2)
plt.title("Particle x-positions over time")
plt.xlabel("time")
plt.ylabel("particle x-position")
h0, = plt.plot(time, particle_one_evolution, label="particle 1")
h1, = plt.plot(time, particle_two_evolution, label="particle 2")
plt.legend(handles=[h0, h1])
plt.show()