/// takes a Python list of spheres with position and velocity vectors and returns the classical kinetic energy of the system static PyObject * total_ke(PyObject *self, PyObject *args) { /* def total_ke(molecules): ke = 0.0 for mol in molecules: ke += abs(mol.vel) ** 2 return ke / 2 */ double ke = 0.0; PyObject *mol_list; if (!PyArg_ParseTuple(args, "O", &mol_list)) return NULL; if (!PyList_Check(mol_list)) return NULL; long size = PyList_Size(mol_list); for (long i = 0; i < size; i++) { PyObject *mol, *vel, *abs; if (!(mol = PyList_GetItem(mol_list, i))) return NULL; if (!(vel = PyObject_GetAttrString(mol, "vel"))) return NULL; if (!(abs = PyNumber_Absolute(vel))) return NULL; double tke = PyFloat_AsDouble(abs); ke += pow(tke, 2); Py_DECREF(vel); Py_DECREF(abs); } return Py_BuildValue("d", ke / 2); }
inline CObject abs(const CObject& a) { PyObject* tmp_obj = PyNumber_Absolute(a.Get()); if ( !tmp_obj ) { throw CArithmeticError("PyNumber_Absolute"); } return CObject(tmp_obj, eTakeOwnership); }
static PyObject *bip_abs(term_t t) { PyObject *pVal, *nVal; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, true); nVal = PyNumber_Absolute(pVal); Py_DecRef(pVal); return nVal; }
/// takes a Python list of spheres with position and velocity vectors and returns the Lennard-Jones potential of the system static PyObject * total_pe(PyObject *self, PyObject *args) { /* def total_pe(): pe = 0.0 for m1 in range(len(molecules)): mol1 = molecules[m1] for m2 in range(m1 + 1, len(molecules)): mol2 = molecules[m2] r12 = abs(mol1.pos - mol2.pos) assert abs(mol2.pos - mol1.pos) == r12 pe += r12 ** -6 * (r12 ** -6 - 2) return pe */ double pe = 0.0; PyObject *mol_list; if (!PyArg_ParseTuple(args, "O", &mol_list)) return NULL; if (!PyList_Check(mol_list)) return NULL; long size = PyList_Size(mol_list); for (long m1 = 0; m1 < size; m1++) { PyObject *mol1, *mol1pos; if (!(mol1 = PyList_GetItem(mol_list, m1))) return NULL; if (!(mol1pos = PyObject_GetAttrString(mol1, "pos"))) return NULL; for (long m2 = m1 + 1; m2 < size; m2++) { PyObject *mol2, *mol2pos, *r12vec, *pysep; if (!(mol2 = PyList_GetItem(mol_list, m2))) return NULL; if (!(mol2pos = PyObject_GetAttrString(mol2, "pos"))) return NULL; if (!(r12vec = PyNumber_Subtract(mol1pos, mol2pos))) return NULL; if (!(pysep = PyNumber_Absolute(r12vec))) return NULL; double r12 = PyFloat_AsDouble(pysep); pe += pow(r12, -6) * (pow(r12, -6) - 2); Py_DECREF(mol2pos); Py_DECREF(r12vec); Py_DECREF(pysep); } Py_DECREF(mol1pos); } return Py_BuildValue("d", pe); }
static PyObject *Proxy_absolute(ProxyObject *self) { Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self); return PyNumber_Absolute(self->wrapped); }