Example #1
0
void py_coulomb_potential_and_field(PyObject *self, void *p, void *nl,
				    double *q, double *phi, double *epot,
				    double *E, double *wpot, int *error)
{
  particles_t *py_p;
  neighbors_t *py_nl;
  PyObject *py_q, *py_phi, *py_epot, *py_E, *py_wpot, *r;

  int nat;
  npy_intp dims[2];

#ifdef DEBUG
  printf("[py_coulomb_potential_and_field]\n");
#endif

  f_particles_get_tag(p, (void**) &py_p);
  assert(py_p->f90obj == p);
  nat = data_get_len(py_p->f90data);

  f_neighbors_get_tag(nl, (void**) &py_nl);
  assert(py_nl->f90obj == nl);

  dims[0] = nat;
  dims[1] = 3;

  py_q = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, q);
  py_phi = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, phi);
  py_E = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, E);

  dims[0] = 1;
  py_epot = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, epot);

  dims[0] = 3;
  dims[1] = 3;
  py_wpot = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, wpot);

  r = PyObject_CallMethod(self, "potential_and_field", "(OOOOOOO)", py_p,
                          py_nl, py_q, py_phi, py_epot, py_E, py_wpot);

  Py_DECREF(py_q);
  Py_DECREF(py_phi);
  Py_DECREF(py_E);
  Py_DECREF(py_epot);
  Py_DECREF(py_wpot);
  PASS_PYTHON_ERROR(error, r);
  Py_DECREF(r);
}
Example #2
0
void py_coulomb_potential(PyObject *self, void *p, void *nl, double *q,
			  double *phi, int *error)
{
  particles_t *py_p;
  neighbors_t *py_nl;
  PyObject *py_q, *py_phi, *r;

  int nat;
  npy_intp dims[2];
  
#ifdef DEBUG
  printf("[py_coulomb_potential] self = %s\n",
	 PyString_AsString(PyObject_Str(self)));
#endif

  f_particles_get_tag(p, (void**) &py_p);
  assert(py_p->f90obj == p);
  nat = data_get_len(py_p->f90data);

  f_neighbors_get_tag(nl, (void**) &py_nl);
  assert(py_nl->f90obj == nl);

  dims[0] = nat;
  dims[1] = 3;

  py_q = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, q);
  py_phi = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, phi);

  r = PyObject_CallMethod(self, "potential", "(OOOO)", py_p, py_nl, py_q,
			  py_phi);

  Py_DECREF(py_q);
  Py_DECREF(py_phi);
  PASS_PYTHON_ERROR(error, r);
  Py_DECREF(r);
}
Example #3
0
static Py_ssize_t
particles_len(particles_t *self)
{
  return data_get_len(self->f90data);
}
Example #4
0
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;
}
Example #5
0
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;
}