static PyObject* Py_convert_shape( PyObject* self, PyObject* args) { PyObject* landmarks_obj; int format; if (!PyArg_ParseTuple(args, "Oi:convert_shape", &landmarks_obj, &format)) return NULL; PyArrayObject* landmarks_array = (PyArrayObject*) PyArray_FROM_OTF(landmarks_obj, NPY_FLOAT, NPY_ARRAY_IN_ARRAY); if (landmarks_array == NULL) { PyErr_SetString(PyExc_TypeError, landmark_error); return NULL; } if (PyArray_NDIM(landmarks_array) != 2) { PyErr_SetString(PyExc_TypeError, landmark_dim_error); return NULL; } PyObject* retArray = PyArray_Copy(landmarks_array); Py_DECREF(landmarks_array); float* landmarks = (float*)PyArray_DATA((PyArrayObject*)retArray); stasm_convert_shape(landmarks, format); return retArray; }
/* optimize float array or complex array to a scalar power */ static PyObject * fast_scalar_power(PyArrayObject *a1, PyObject *o2, int inplace) { double exp; if (PyArray_Check(a1) && array_power_is_scalar(o2, &exp)) { PyObject *fastop = NULL; if (PyArray_ISFLOAT(a1) || PyArray_ISCOMPLEX(a1)) { if (exp == 1.0) { /* we have to do this one special, as the "copy" method of array objects isn't set up early enough to be added by PyArray_SetNumericOps. */ if (inplace) { Py_INCREF(a1); return (PyObject *)a1; } else { return PyArray_Copy(a1); } } else if (exp == -1.0) { fastop = n_ops.reciprocal; } else if (exp == 0.0) { fastop = n_ops.ones_like; } else if (exp == 0.5) { fastop = n_ops.sqrt; } else if (exp == 2.0) { fastop = n_ops.square; } else { return NULL; } if (inplace) { return PyArray_GenericInplaceUnaryFunction(a1, fastop); } else { return PyArray_GenericUnaryFunction(a1, fastop); } } else if (exp==2.0) { fastop = n_ops.multiply; if (inplace) { return PyArray_GenericInplaceBinaryFunction (a1, (PyObject *)a1, fastop); } else { return PyArray_GenericBinaryFunction (a1, (PyObject *)a1, fastop); } } } return NULL; }
PyObject* sndarray_array (PyObject* self, PyObject* arg) { PyObject *array, *arraycopy=NULL; /*we'll let numeric do the copying for us*/ array = sndarray_samples (self, arg); if(array) { arraycopy = PyArray_Copy ((PyArrayObject*) array); Py_DECREF (array); } return arraycopy; }
static PyObject* Py_force_points_into_image( PyObject* self, PyObject* args) { PyObject* landmarks_obj; PyObject* img_obj; if (!PyArg_ParseTuple(args, "OO:force_points_into_image", &landmarks_obj, &img_obj)) return NULL; PyArrayObject* landmarks_array = (PyArrayObject*) PyArray_FROM_OTF(landmarks_obj, NPY_FLOAT, NPY_ARRAY_IN_ARRAY); if (landmarks_array == NULL) { PyErr_SetString(PyExc_TypeError, landmark_error); return NULL; } if (PyArray_NDIM(landmarks_array) != 2) { PyErr_SetString(PyExc_TypeError, landmark_dim_error); return NULL; } int width, height; if (PyArray_to_image(img_obj, &width, &height) == NULL) return NULL; PyObject* retArray = PyArray_Copy(landmarks_array); Py_DECREF(landmarks_array); float* landmarks = (float*)PyArray_DATA((PyArrayObject*)retArray); stasm_force_points_into_image(landmarks, width, height); return retArray; }
// * GCHMCIntegrator Constructor * // GCHMCIntegrator::GCHMCIntegrator(PyObject *universe, std::string ligdir, std::string gaff_fn) { std::cout<<"GCHMCIntegrator instance created 20160826"<<std::endl; import_array(); temperature = 300.0; delta_t = 0.0015; trouble = 0; this->universe = universe; PyObject *pyo_universe_spec; pyo_universe_spec = PyObject_GetAttrString(universe, "_spec"); universe_spec = (PyUniverseSpecObject *)pyo_universe_spec; PyObject *objectList; objectList = PyObject_CallMethod(universe, "objectList", NULL); PyObject *object0 = PyList_GetItem(objectList, 0); // * Get confarr * // PyObject *confobj; PyObject *confarr; confobj = PyObject_CallMethod(universe, "configuration", NULL); confarr = PyObject_GetAttrString(confobj, "array"); configuration = (PyArrayObject *)confarr; vector3 *x; x = (vector3 *)configuration->data; p_energy_po = new energy_data; #if defined(NUMPY) gradarr = (PyArrayObject *)PyArray_Copy(configuration); #else gradarr = (PyArrayObject *)PyArray_FromDims(configuration->nd, configuration->dimensions, PyArray_DOUBLE); #endif vector3 *f; f = (vector3 *)gradarr->data; for(int i=0; i<configuration->dimensions[0]; i++){ f[i][0] = f[i][1] = f[i][2] = .0; } p_energy_po->gradients = (PyObject *)gradarr; p_energy_po->gradient_fn = NULL; p_energy_po->force_constants = NULL; p_energy_po->fc_fn = NULL; int natoms = configuration->dimensions[0]; int order[natoms+2]; int acceptance; PyObject *prmtop_order_obj = PyObject_GetAttrString(object0, "prmtop_order"); for(int i=0; i<PyList_Size(prmtop_order_obj); i++){ order[i] = (int)PyInt_AS_LONG( PyObject_GetAttrString(PyList_GetItem((prmtop_order_obj), i), "number") ); } PyArrayObject *prmtop_order = (PyArrayObject *)prmtop_order_obj; order[natoms] = 1; order[natoms+1] = 1945; acceptance = order[natoms]; int argc = 6; ligdir += '/'; //gaffdir += '/'; //string gaff_fn = gaffdir + "gaff.dat"; /* Options are: IC: Internal Coordinates: fully flexible TD: Torsional Dynamics: only torsions are flexible RR: Rigid Rings: torsional dynamics with rigid rings */ const char *argv[6] = { "-ligdir", ligdir.c_str(), "-gaff", gaff_fn.c_str(), "-ictd", "TD" }; //+++++++ Simbody PART ++++++++++++ bArgParser parser(argc, argv); parser.Print(); TARGET_TYPE **indexMap = NULL; TARGET_TYPE *PrmToAx_po = NULL; TARGET_TYPE *MMTkToPrm_po = NULL; std::cout<<"MMTK configuration->dimensions[0]"<<configuration->dimensions[0]<<std::endl<<std::flush; indexMap = new TARGET_TYPE*[(configuration->dimensions[0])]; int _indexMap[natoms][3]; PrmToAx_po = new TARGET_TYPE[configuration->dimensions[0]]; MMTkToPrm_po = new TARGET_TYPE[configuration->dimensions[0]]; int natoms3 = 3*(configuration->dimensions[0]); int arrays_cut = 2 + 4*natoms3; SHMSZ = ( 2*sizeof(TARGET_TYPE) + // Counter and flag natoms3*sizeof(TARGET_TYPE) + // Positions natoms3*sizeof(TARGET_TYPE) + // Velocities natoms3*sizeof(TARGET_TYPE) + // indexMap natoms3*sizeof(TARGET_TYPE) + // Gradients 5*sizeof(TARGET_TYPE) + // ac + 0 -> 4 // step, nosteps, temperature, timestep, trouble 1*sizeof(TARGET_TYPE) + // ac + 5 // potential energy 2*sizeof(TARGET_TYPE) + // ac + 6->7 // DAE step done; Move accepted 1*sizeof(TARGET_TYPE) + // ac + 8 // KE 1*sizeof(TARGET_TYPE) + // ac + 9 // steps_per_trial 1*sizeof(TARGET_TYPE) //+ // ac + 10 // trial ); shm = new TARGET_TYPE[SHMSZ]; std::cout<<"parser.mol2F "<<parser.mol2F<<std::endl<<std::flush; std::cout<<"parser.rbF "<<parser.rbF<<std::endl<<std::flush; std::cout<<"parser.gaffF "<<parser.gaffF<<std::endl<<std::flush; std::cout<<"parser.frcmodF "<<parser.frcmodF<<std::endl<<std::flush; std::cout<<"parser.ictdF "<<parser.ictdF<<std::endl<<std::flush; // LS EU //(((PyFFEnergyTermObject **)(evaluator)->terms->data)[5])->param[0] = 0.5; // LS EU sys = new SymSystem( parser.mol2F, parser.rbF, parser.gaffF, parser.frcmodF, parser.ictdF, PrmToAx_po, MMTkToPrm_po, evaluator, p_energy_po, configuration, universe_spec, shm ); for(int i=0; i<natoms; i++){ _indexMap[i][2] = order[i]; } // * Memory alloc for convinient arrays * // coords = new TARGET_TYPE*[sys->mr->natms]; TARGET_TYPE **vels = new TARGET_TYPE*[sys->mr->natms]; TARGET_TYPE **inivels = new TARGET_TYPE*[sys->mr->natms]; TARGET_TYPE **grads = new TARGET_TYPE*[sys->mr->natms]; // * Seed the random number generator * // srand (time(NULL)); //+++++++ SERVER PART +++++++++ float mysweep = 0; shm[0] = CNT_START; int cli; sweep = 0; bool system_initialized = false; int big_loop_i; shm[1] = CLIENT_FLAG; int a; int start, start_1, stop; int i = natoms*2*3 + 2; // * Assign convenient pointers for order * / start = 2 + natoms3 + natoms3; for(a=0; a<natoms; a++){ indexMap[a] = &(shm[a*3 + start]); } // * Assign order in shm[][2] * // start = 2 + natoms3 + natoms3; cli = start; for (a=0; a<natoms; a++){ // Order cli += 2; shm[cli++] = order[a]; } cli = 2; for (a=0; a<natoms; a++){ // Positions shm[cli++] = x[a][0]; shm[cli++] = x[a][1]; shm[cli++] = x[a][2]; } for (a=0; a<natoms; a++){ // Velocities shm[cli++] = .0; //vels[a][0];//shm[cli++] = v[a][0]; shm[cli++] = .0; //vels[a][1];//shm[cli++] = v[a][1]; shm[cli++] = .0; //vels[a][2];//shm[cli++] = v[a][2]; } for (a=0; a<natoms; a++){ // Order cli += 2; shm[cli++] = order[a]; } for (a=0; a<natoms; a++){ // Forces shm[cli++] = .0; shm[cli++] = .0; shm[cli++] = .0; } shm[cli++] = big_loop_i; // Step shm[cli++] = 50.0; //(TARGET_TYPE)nosteps; // Number of steps shm[cli++] = temperature; // Temeperature shm[cli++] = delta_t; shm[cli++] = trouble; shm[cli++] = p_energy_po->energy; cli++; // DAE set only by the server shm[cli] = acceptance + 0.0; //Set the client flag shm[1] = CLIENT_FLAG; shm[arrays_cut + 6] = 13.0; // set DAE to done // * Assign convenient pointers for coordinates * // int shm_coords_sup = (natoms3)+2; int j=-1, k=0; start = 2; for(j=0; j<natoms; j++){ coords[j] = &(shm[j*3 + start]); } mysweep = shm[0] - CNT_START; // * Assign convenient pointers for velocities * // start = 2 + natoms3; start_1 = start - 1; stop = 2 + natoms3 + natoms3; for(j=0; j<natoms; j++){ vels[j] = &(shm[j*3 + start]); inivels[j] = &(shm[j*3 + start]); } // * Assign convenient pointers for gradients * // start = 2 + natoms3 + natoms3 + natoms3; start_1 = start - 1; stop = 2 + natoms3 + natoms3 + natoms3 + natoms3; for(j=0; j<natoms; j++){ grads[j] = &(shm[j*3 + start]); } // * Rewrite indexMap to memory * / start = 2 + natoms3 + natoms3; start_1 = start - 1; stop = 2 + natoms3 + natoms3 + natoms3; // ************************* / // * Initialize Simulation * / // ************************* / TARGET_TYPE timestep, mytimestep; mytimestep = shm[arrays_cut + 3]; // * Build bMainResidue and fill indexMap * / sys->InitSimulation(coords, vels, inivels, indexMap, grads, mytimestep, true); system_initialized = true; }
static PyObject * _array_copy_nice(PyArrayObject *self) { return PyArray_Return((PyArrayObject *) PyArray_Copy(self)); }
/* optimize float array or complex array to a scalar power */ static PyObject * fast_scalar_power(PyArrayObject *a1, PyObject *o2, int inplace) { double exponent; NPY_SCALARKIND kind; /* NPY_NOSCALAR is not scalar */ if (PyArray_Check(a1) && ((kind=is_scalar_with_conversion(o2, &exponent))>0)) { PyObject *fastop = NULL; if (PyArray_ISFLOAT(a1) || PyArray_ISCOMPLEX(a1)) { if (exponent == 1.0) { /* we have to do this one special, as the "copy" method of array objects isn't set up early enough to be added by PyArray_SetNumericOps. */ if (inplace) { Py_INCREF(a1); return (PyObject *)a1; } else { return PyArray_Copy(a1); } } else if (exponent == -1.0) { fastop = n_ops.reciprocal; } else if (exponent == 0.0) { fastop = n_ops._ones_like; } else if (exponent == 0.5) { fastop = n_ops.sqrt; } else if (exponent == 2.0) { fastop = n_ops.square; } else { return NULL; } if (inplace) { return PyArray_GenericInplaceUnaryFunction(a1, fastop); } else { return PyArray_GenericUnaryFunction(a1, fastop); } } /* Because this is called with all arrays, we need to * change the output if the kind of the scalar is different * than that of the input and inplace is not on --- * (thus, the input should be up-cast) */ else if (exponent == 2.0) { fastop = n_ops.multiply; if (inplace) { return PyArray_GenericInplaceBinaryFunction (a1, (PyObject *)a1, fastop); } else { PyArray_Descr *dtype = NULL; PyObject *res; /* We only special-case the FLOAT_SCALAR and integer types */ if (kind == NPY_FLOAT_SCALAR && PyArray_ISINTEGER(a1)) { dtype = PyArray_DescrFromType(NPY_DOUBLE); a1 = (PyArrayObject *)PyArray_CastToType(a1, dtype, PyArray_ISFORTRAN(a1)); if (a1 == NULL) { return NULL; } } else { Py_INCREF(a1); } res = PyArray_GenericBinaryFunction(a1, (PyObject *)a1, fastop); Py_DECREF(a1); return res; } } } return NULL; }
/*NUMPY_API * Round */ NPY_NO_EXPORT PyObject * PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out) { PyObject *f, *ret = NULL, *tmp, *op1, *op2; int ret_int=0; PyArray_Descr *my_descr; if (out && (PyArray_SIZE(out) != PyArray_SIZE(a))) { PyErr_SetString(PyExc_ValueError, "invalid output shape"); return NULL; } if (PyArray_ISCOMPLEX(a)) { PyObject *part; PyObject *round_part; PyObject *arr; int res; if (out) { arr = (PyObject *)out; Py_INCREF(arr); } else { arr = PyArray_Copy(a); if (arr == NULL) { return NULL; } } /* arr.real = a.real.round(decimals) */ part = PyObject_GetAttrString((PyObject *)a, "real"); if (part == NULL) { Py_DECREF(arr); return NULL; } part = PyArray_EnsureAnyArray(part); round_part = PyArray_Round((PyArrayObject *)part, decimals, NULL); Py_DECREF(part); if (round_part == NULL) { Py_DECREF(arr); return NULL; } res = PyObject_SetAttrString(arr, "real", round_part); Py_DECREF(round_part); if (res < 0) { Py_DECREF(arr); return NULL; } /* arr.imag = a.imag.round(decimals) */ part = PyObject_GetAttrString((PyObject *)a, "imag"); if (part == NULL) { Py_DECREF(arr); return NULL; } part = PyArray_EnsureAnyArray(part); round_part = PyArray_Round((PyArrayObject *)part, decimals, NULL); Py_DECREF(part); if (round_part == NULL) { Py_DECREF(arr); return NULL; } res = PyObject_SetAttrString(arr, "imag", round_part); Py_DECREF(round_part); if (res < 0) { Py_DECREF(arr); return NULL; } return arr; } /* do the most common case first */ if (decimals >= 0) { if (PyArray_ISINTEGER(a)) { if (out) { if (PyArray_AssignArray(out, a, NULL, NPY_DEFAULT_ASSIGN_CASTING) < 0) { return NULL; } Py_INCREF(out); return (PyObject *)out; } else { Py_INCREF(a); return (PyObject *)a; } } if (decimals == 0) { if (out) { return PyObject_CallFunction(n_ops.rint, "OO", a, out); } return PyObject_CallFunction(n_ops.rint, "O", a); } op1 = n_ops.multiply; op2 = n_ops.true_divide; } else { op1 = n_ops.true_divide; op2 = n_ops.multiply; decimals = -decimals; } if (!out) { if (PyArray_ISINTEGER(a)) { ret_int = 1; my_descr = PyArray_DescrFromType(NPY_DOUBLE); } else { Py_INCREF(PyArray_DESCR(a)); my_descr = PyArray_DESCR(a); } out = (PyArrayObject *)PyArray_Empty(PyArray_NDIM(a), PyArray_DIMS(a), my_descr, PyArray_ISFORTRAN(a)); if (out == NULL) { return NULL; } } else { Py_INCREF(out); } f = PyFloat_FromDouble(power_of_ten(decimals)); if (f == NULL) { return NULL; } ret = PyObject_CallFunction(op1, "OOO", a, f, out); if (ret == NULL) { goto finish; } tmp = PyObject_CallFunction(n_ops.rint, "OO", ret, ret); if (tmp == NULL) { Py_DECREF(ret); ret = NULL; goto finish; } Py_DECREF(tmp); tmp = PyObject_CallFunction(op2, "OOO", ret, f, ret); if (tmp == NULL) { Py_DECREF(ret); ret = NULL; goto finish; } Py_DECREF(tmp); finish: Py_DECREF(f); Py_DECREF(out); if (ret_int) { Py_INCREF(PyArray_DESCR(a)); tmp = PyArray_CastToType((PyArrayObject *)ret, PyArray_DESCR(a), PyArray_ISFORTRAN(a)); Py_DECREF(ret); return tmp; } return ret; }