static PyObject* particles_set_lees_edwards(particles_t *self, PyObject *value) { PyObject *dx_arr, *dv_arr = NULL; double *dx, dv[3]; int ierror = ERROR_NONE; if (!PyArg_ParseTuple(value, "O|O", &dx_arr, &dv_arr)) return NULL; if (dv_arr == Py_None) dv_arr = NULL; dx_arr = PyArray_FROMANY(dx_arr, NPY_DOUBLE, 1, 1, 0); if (dv_arr) { dv_arr = PyArray_FROMANY(dv_arr, NPY_DOUBLE, 1, 1, 0); if (!dv_arr) { Py_DECREF(dx_arr); return NULL; } } if (PyArray_DIM(dx_arr, 0) != 3) { PyErr_SetString(PyExc_TypeError, "dx needs to be 3 vector."); Py_DECREF(dx_arr); if (dv_arr) { Py_DECREF(dv_arr); } return NULL; } dx = (double *) PyArray_DATA(dx_arr); dv[0] = 0.0; dv[1] = 0.0; dv[2] = 0.0; if (dv_arr) { if (PyArray_DIM(dv_arr, 0) != 3) { PyErr_SetString(PyExc_TypeError, "dv needs to be 3 vector."); Py_DECREF(dx_arr); Py_DECREF(dv_arr); return NULL; } dv[0] = ((double *) PyArray_DATA(dv_arr))[0]; dv[1] = ((double *) PyArray_DATA(dv_arr))[1]; dv[2] = ((double *) PyArray_DATA(dv_arr))[2]; } f_particles_set_lees_edwards(self->f90obj, dx, dv, &ierror); if (error_to_py(ierror)) return NULL; Py_DECREF(dx_arr); if (dv_arr) { Py_DECREF(dv_arr); } Py_RETURN_NONE; }
static PyObject * potential_register_data(potential_t *self, PyObject *args) { particles_t *a; int ierror = ERROR_NONE; if (!PyArg_ParseTuple(args, "O!", &particles_type, &a)) return NULL; self->f90class->register_data(self->f90obj, a->f90obj, &ierror); if (error_to_py(ierror)) return NULL; Py_RETURN_NONE; }
static PyObject* particles_set_cell(particles_t *self, PyObject *args) { PyObject *Abox_obj, *pbc_obj = NULL; PyArrayObject *Abox_arr; PyArrayObject *pbc_arr = NULL; double *Abox; npy_bool *pbc; BOOL pbc_for[3]; int ierror = ERROR_NONE; if (!PyArg_ParseTuple(args, "O|O", &Abox_obj, &pbc_obj)) return NULL; Abox_arr = (PyArrayObject *) PyArray_FROMANY(Abox_obj, NPY_DOUBLE, 2, 2, NPY_C_CONTIGUOUS); if (!Abox_arr) return NULL; Abox = DOUBLEP(Abox_arr); pbc_for[0] = 1; pbc_for[1] = 1; pbc_for[2] = 1; if (pbc_obj) { pbc_arr = (PyArrayObject *) PyArray_FROMANY(pbc_obj, NPY_BOOL, 1, 1, NPY_C_CONTIGUOUS); if (!pbc_arr) return NULL; pbc = (npy_bool *) BOOLP(pbc_arr); pbc_for[0] = pbc[0]; pbc_for[1] = pbc[1]; pbc_for[2] = pbc[2]; } #ifdef DEBUG printf("[particles_set_cell] pbc_for %i, %i, %i\n", pbc_for[0], pbc_for[1], pbc_for[2]); #endif f_particles_set_cell(self->f90obj, Abox, pbc_for, &ierror); if (error_to_py(ierror)) return NULL; Py_RETURN_NONE; }
static PyObject* particles_allocate(particles_t *self, PyObject *args) { int nat; int ierror = ERROR_NONE; if (!PyArg_ParseTuple(args, "i", &nat)) return NULL; f_particles_allocate(self->f90obj, nat, &ierror); if (error_to_py(ierror)) return NULL; self->initialized = TRUE; particles_get_ptr(self); Py_RETURN_NONE; }
static PyObject * potential_bind_to(potential_t *self, PyObject *args) { particles_t *a; neighbors_t *n; int ierror = ERROR_NONE; #ifdef DEBUG printf("[potential_bind_to] self = %p\n", self); #endif if (!PyArg_ParseTuple(args, "O!O!", &particles_type, &a, &neighbors_type, &n)) return NULL; self->f90class->bind_to(self->f90obj, a->f90obj, n->f90obj, &ierror); if (error_to_py(ierror)) return NULL; Py_RETURN_NONE; }
static PyObject * data_array_by_name(particles_t *self, char *key) { int data_type; BOOL ex; int ierror = ERROR_NONE; void *array; PyObject *r; npy_intp dims[3]; #ifndef SEP_XYZ npy_intp strides[3]; #endif char errstr[100]; #ifdef DEBUG printf("[data_array_by_name] self = %p, key = %p\n", self, key); printf("[data_array_by_name] self->f90obj = %p, self->f90data = %p\n", self->f90obj, self->f90data); printf("[data_array_by_name] key = %s\n", key); #endif ex = f_data_exists(self->f90data, key, &data_type); #ifdef DEBUG printf("[data_array_by_name] ex = %i\n", ex); #endif r = NULL; if (ex) { switch (data_type) { case TYPE_REAL: real_ptr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; dims[0] = data_get_len(self->f90data); #ifdef DEBUG printf("[data_array_by_name] TYPE_REAL, dim = %i\n", dims[0]); #endif r = PyArray_New(&PyArray_Type, 1, dims, NPY_DOUBLE, NULL, array, 0, NPY_FARRAY, NULL); break; case TYPE_INTEGER: integer_ptr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; dims[0] = data_get_len(self->f90data); #ifdef DEBUG printf("[data_array_by_name] TYPE_INTEGER, dim = %i\n", dims[0]); #endif r = PyArray_New(&PyArray_Type, 1, dims, NPY_INT, NULL, array, 0, NPY_FARRAY, NULL); break; case TYPE_REAL3: realx_ptr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; dims[0] = data_get_len(self->f90data); dims[1] = 3; strides[0] = 3*NPY_SIZEOF_DOUBLE; strides[1] = NPY_SIZEOF_DOUBLE; #ifdef DEBUG printf("[data_array_by_name] TYPE_REAL3, dim = %i %i, strides = %i %i\n", dims[0], dims[1], strides[0], strides[1]); #endif r = PyArray_New(&PyArray_Type, 2, dims, NPY_DOUBLE, strides, array, 0, NPY_BEHAVED, NULL); break; case TYPE_REAL3x3: #ifdef DEBUG printf("[data_array_by_name] Type is REAL3x3\n"); #endif realxxx_ptr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; dims[0] = data_get_len(self->f90data); dims[1] = 3; dims[2] = 3; #ifdef DEBUG printf("[data_array_by_name] TYPE_REAL3x3, dim = %i %i %i\n", dims[0], dims[1], dims[2]); #endif r = PyArray_New(&PyArray_Type, 3, dims, NPY_DOUBLE, NULL, array, 0, NPY_FARRAY, NULL); break; case TYPE_REAL_ATTR: real_attr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; #ifdef DEBUG printf("[data_array_by_name] TYPE_REAL_ATTR\n"); #endif r = PyFloat_FromDouble(*((double*) array)); break; case TYPE_REAL3_ATTR: real3_attr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; dims[0] = 3; #ifdef DEBUG printf("[data_array_by_name] TYPE_REAL3_ATTR, dim = %i\n", dims[0]); #endif r = PyArray_New(&PyArray_Type, 1, dims, NPY_DOUBLE, NULL, array, 0, NPY_FARRAY, NULL); break; case TYPE_REAL3x3_ATTR: real3x3_attr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; dims[0] = 3; dims[1] = 3; #ifdef DEBUG printf("[data_array_by_name] TYPE_REAL3_ATTR, dim = %i %i\n", dims[0], dims[1]); #endif r = PyArray_New(&PyArray_Type, 2, dims, NPY_DOUBLE, NULL, array, 0, NPY_FARRAY, NULL); break; case TYPE_INTEGER_ATTR: integer_attr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; #ifdef DEBUG printf("[data_array_by_name] TYPE_INTEGER_ATTR\n"); #endif r = PyInt_FromLong(*((int*) array)); break; case TYPE_INTEGER3_ATTR: integer3_attr_by_name(self->f90data, key, &array, &ierror); if (error_to_py(ierror)) return NULL; dims[0] = 3; #ifdef DEBUG printf("[data_array_by_name] TYPE_INTEGER3_ATTR, dim = %i\n", dims[0]); #endif r = PyArray_New(&PyArray_Type, 1, dims, NPY_INT, NULL, array, 0, NPY_FARRAY, NULL); break; default: sprintf(errstr, "InternalError: Unknown type returned for field or " "attribute '%s'.", PyString_AS_STRING(key)); PyErr_SetString(PyExc_KeyError, errstr); r = NULL; } } return r; }
static PyObject * potential_energy_and_forces(potential_t *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "particles", "neighbors", "epot_per_at", "epot_per_bond", "f_per_bond", "wpot_per_at", "wpot_per_bond", NULL }; npy_intp dims[3]; npy_intp strides[3]; particles_t *a; neighbors_t *n; PyObject *return_epot_per_at = NULL; PyObject *return_epot_per_bond = NULL; PyObject *return_f_per_bond = NULL; PyObject *return_wpot_per_at = NULL; PyObject *return_wpot_per_bond = NULL; int ierror = ERROR_NONE; double epot; PyObject *f; PyObject *wpot; PyObject *epot_per_at = NULL; PyObject *epot_per_bond = NULL; PyObject *f_per_bond = NULL; PyObject *wpot_per_at = NULL; PyObject *wpot_per_bond = NULL; double *epot_per_at_ptr = NULL; double *epot_per_bond_ptr = NULL; double *f_per_bond_ptr = NULL; double *wpot_per_at_ptr = NULL; double *wpot_per_bond_ptr = NULL; PyObject *r; int i; /* --- */ #ifdef DEBUG printf("[potential_energy_and_forces] self = %p\n", self); #endif if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O!|O!O!O!O!O!", kwlist, &particles_type, &a, &neighbors_type, &n, &PyBool_Type, &return_epot_per_at, &PyBool_Type, &return_epot_per_bond, &PyBool_Type, &return_f_per_bond, &PyBool_Type, &return_wpot_per_at, &PyBool_Type, &return_wpot_per_bond)) return NULL; epot = 0.0; dims[0] = data_get_len(a->f90data); dims[1] = 3; strides[0] = dims[1]*NPY_SIZEOF_DOUBLE; strides[1] = NPY_SIZEOF_DOUBLE; f = (PyObject*) PyArray_New(&PyArray_Type, 2, dims, NPY_DOUBLE, strides, NULL, 0, NPY_FARRAY, NULL); memset(PyArray_DATA(f), 0, dims[0]*dims[1]*NPY_SIZEOF_DOUBLE); dims[0] = 3; dims[1] = 3; wpot = PyArray_ZEROS(2, dims, NPY_DOUBLE, 1); if (return_epot_per_at) { if (return_epot_per_at == Py_True) { dims[0] = data_get_len(a->f90data); epot_per_at = PyArray_ZEROS(1, dims, NPY_DOUBLE, 1); epot_per_at_ptr = PyArray_DATA(epot_per_at); } else { epot_per_at = Py_None; Py_INCREF(Py_None); } } if (return_epot_per_bond) { if (return_epot_per_bond == Py_True) { dims[0] = get_neighbors_size(n, a); epot_per_bond = PyArray_ZEROS(1, dims, NPY_DOUBLE, 1); epot_per_bond_ptr = PyArray_DATA(epot_per_bond); } else { epot_per_bond = Py_None; Py_INCREF(Py_None); } } if (return_f_per_bond) { if (return_f_per_bond == Py_True) { dims[0] = get_neighbors_size(n, a); dims[1] = 3; strides[0] = dims[1]*NPY_SIZEOF_DOUBLE; strides[1] = NPY_SIZEOF_DOUBLE; f_per_bond = (PyObject*) PyArray_New(&PyArray_Type, 2, dims, NPY_DOUBLE, strides, NULL, 0, NPY_FARRAY, NULL); f_per_bond_ptr = PyArray_DATA(f_per_bond); memset(f_per_bond_ptr, 0, dims[0]*dims[1]*NPY_SIZEOF_DOUBLE); } else { f_per_bond = Py_None; Py_INCREF(Py_None); } } if (return_wpot_per_at) { if (return_wpot_per_at == Py_True) { dims[0] = data_get_len(a->f90data); dims[1] = 3; dims[2] = 3; strides[0] = dims[1]*dims[2]*NPY_SIZEOF_DOUBLE; strides[1] = dims[2]*NPY_SIZEOF_DOUBLE; strides[2] = NPY_SIZEOF_DOUBLE; wpot_per_at = (PyObject*) PyArray_New(&PyArray_Type, 3, dims, NPY_DOUBLE, strides, NULL, 0, NPY_FARRAY, NULL); wpot_per_at_ptr = PyArray_DATA(wpot_per_at); memset(wpot_per_at_ptr, 0, dims[0]*dims[1]*dims[2]*NPY_SIZEOF_DOUBLE); } else { wpot_per_at = Py_None; Py_INCREF(Py_None); } } if (return_wpot_per_bond) { if (return_wpot_per_bond == Py_True) { dims[0] = get_neighbors_size(n, a); dims[1] = 3; dims[2] = 3; strides[0] = dims[1]*dims[2]*NPY_SIZEOF_DOUBLE; strides[1] = dims[2]*NPY_SIZEOF_DOUBLE; strides[2] = NPY_SIZEOF_DOUBLE; wpot_per_bond = (PyObject*) PyArray_New(&PyArray_Type, 3, dims, NPY_DOUBLE, strides, NULL, 0, NPY_FARRAY, NULL); wpot_per_bond_ptr = PyArray_DATA(wpot_per_bond); memset(wpot_per_bond_ptr, 0, dims[0]*dims[1]*dims[2]*NPY_SIZEOF_DOUBLE); } else { wpot_per_bond = Py_None; Py_INCREF(Py_None); } } #ifdef DEBUG printf("[potential_energy_and_forces] self->f90class->name = %s\n", self->f90class->name); printf("[potential_energy_and_forces] self->f90obj = %p\n", self->f90obj); printf("[potential_energy_and_forces] a->f90obj = %p\n", a->f90obj); printf("[potential_energy_and_forces] n->f90obj = %p\n", n->f90obj); printf("[potential_energy_and_forces] self->f90class->energy_and_forces = %p\n", self->f90class->energy_and_forces); #endif self->f90class->energy_and_forces(self->f90obj, a->f90obj, n->f90obj, &epot, PyArray_DATA(f), PyArray_DATA(wpot), epot_per_at_ptr, epot_per_bond_ptr, f_per_bond_ptr, wpot_per_at_ptr, wpot_per_bond_ptr, &ierror); /* * Now we need to reorder the per-bond properties such that some Python * script can actually make sense out of the data. */ if (epot_per_bond_ptr) { dims[0] = get_number_of_all_neighbors(n, a); PyObject *tmp = PyArray_ZEROS(1, dims, NPY_DOUBLE, 1); f_pack_per_bond_scalar(n->f90obj, epot_per_bond_ptr, PyArray_DATA(tmp)); Py_DECREF(epot_per_bond); epot_per_bond = tmp; } if (wpot_per_bond_ptr) { dims[0] = get_number_of_all_neighbors(n, a); dims[1] = 3; dims[2] = 3; PyObject *tmp = PyArray_ZEROS(3, dims, NPY_DOUBLE, 0); f_pack_per_bond_3x3(n->f90obj, wpot_per_bond_ptr, PyArray_DATA(tmp)); Py_DECREF(wpot_per_bond); wpot_per_bond = tmp; } #ifdef DEBUG printf("[potential_energy_and_forces] epot = %f\n", epot); #endif if (error_to_py(ierror)) return NULL; /* --- Compose return tuple --- */ i = 3; if (epot_per_at) i++; if (epot_per_bond) i++; if (f_per_bond) i++; if (wpot_per_at) i++; if (wpot_per_bond) i++; r = PyTuple_New(i); if (!r) return NULL; PyTuple_SET_ITEM(r, 0, PyFloat_FromDouble(epot)); PyTuple_SET_ITEM(r, 1, f); PyTuple_SET_ITEM(r, 2, wpot); i = 2; if (epot_per_at) { i++; PyTuple_SET_ITEM(r, i, epot_per_at); } if (epot_per_bond) { i++; PyTuple_SET_ITEM(r, i, epot_per_bond); } if (f_per_bond) { i++; PyTuple_SET_ITEM(r, i, f_per_bond); } if (wpot_per_at) { i++; PyTuple_SET_ITEM(r, i, wpot_per_at); } if (wpot_per_bond) { i++; PyTuple_SET_ITEM(r, i, wpot_per_bond); } #ifdef DEBUG printf("{potential_energy_and_forces}\n"); #endif return r; }