static void *open_dcd_write(const char *path, const char *filetype, int natoms) { dcdhandle *dcd; fio_fd fd; int rc; int istart, nsavc; double delta; int with_unitcell; int charmm; if (fio_open(path, FIO_WRITE, &fd) < 0) { printf("dcdplugin) Could not open file '%s' for writing\n", path); return NULL; } dcd = (dcdhandle *)malloc(sizeof(dcdhandle)); memset(dcd, 0, sizeof(dcdhandle)); dcd->fd = fd; istart = 0; /* starting timestep of DCD file */ nsavc = 1; /* number of timesteps between written DCD frames */ delta = 1.0; /* length of a timestep */ if (getenv("VMDDCDWRITEXPLORFORMAT") != NULL) { with_unitcell = 0; /* no unit cell info */ charmm = DCD_IS_XPLOR; /* X-PLOR format */ printf("dcdplugin) WARNING: Writing DCD file in X-PLOR format, \n"); printf("dcdplugin) WARNING: unit cell information will be lost!\n"); } else { with_unitcell = 1; /* contains unit cell infor (Charmm format) */ charmm = DCD_IS_CHARMM; /* charmm-formatted DCD file */ if (with_unitcell) charmm |= DCD_HAS_EXTRA_BLOCK; } rc = write_dcdheader(dcd->fd, "Created by DCD plugin", natoms, istart, nsavc, delta, with_unitcell, charmm); if (rc < 0) { print_dcderror("write_dcdheader", rc); fio_fclose(dcd->fd); free(dcd); return NULL; } dcd->natoms = natoms; dcd->nsets = 0; dcd->istart = istart; dcd->nsavc = nsavc; dcd->with_unitcell = with_unitcell; dcd->charmm = charmm; dcd->x = (float *)malloc(natoms * sizeof(float)); dcd->y = (float *)malloc(natoms * sizeof(float)); dcd->z = (float *)malloc(natoms * sizeof(float)); return dcd; }
/// /// @par Detailed description /// ... /// @param [in, out] (param1) ... /// @return ... /// @note ... int //sasio::Files:: sasio:: write_dcd_header(FILE *outfile, const std::string &filename, int &natoms, int &nset) { char *c_filename = new char[filename.size()+1] ; c_filename[filename.size()] = 0 ; memcpy(c_filename,filename.c_str(),filename.size()) ; int istart = 0 , nsavc = 1 ; double delta = 1.0 ; int header_result = 0 ; header_result = write_dcdheader(outfile, c_filename, natoms, nset, \ istart, nsavc, delta); return header_result ; }
static PyObject * __write_dcd_header(PyObject *self, PyObject *args) { /* At this point we assume the file has been opened for writing */ PyObject *temp = NULL; dcdhandle *dcd = NULL; fio_fd fd; int rc = 0; int natoms = 0; const char *remarks = "DCD"; int istart = 0; /* starting timestep of DCD file */ int nsavc = 1; /* number of timesteps between written DCD frames */ double delta = 1.0; /* length of a timestep */ int with_unitcell = 1; /* contains unit cell information (charmm format) */ int charmm = DCD_IS_CHARMM; /* charmm-formatted DCD file */ if (with_unitcell) charmm |= DCD_HAS_EXTRA_BLOCK; if (! self) { /* we were in fact called as a module function, try to retrieve a matching object from args */ if( !PyArg_ParseTuple(args, "Oi|iids", &self, &natoms, &istart, &nsavc, &delta, &remarks) ) return NULL; } else { /* we were obviously called as an object method so args should only have the int value. */ if( !PyArg_ParseTuple(args, "i|iids", &natoms, &istart, &nsavc, &delta, &remarks) ) return NULL; } // Get the file object from the class if (!PyObject_HasAttrString(self, "dcdfile")) { // Raise exception PyErr_SetString(PyExc_AttributeError, "dcdfile is not an attribute"); return NULL; } if ((temp = PyObject_GetAttrString(self, "dcdfile")) == NULL) { // This gives me a New Reference // Raise exception PyErr_SetString(PyExc_AttributeError, "dcdfile is not an attribute"); return NULL; } if (!PyFile_CheckExact(temp)) { // Raise exception PyErr_SetString(PyExc_TypeError, "dcdfile does not refer to a file object"); Py_DECREF(temp); return NULL; } fd = fileno(PyFile_AsFile(temp)); // No longer need the reference to temp Py_DECREF(temp); dcd = (dcdhandle *)malloc(sizeof(dcdhandle)); memset(dcd, 0, sizeof(dcdhandle)); dcd->fd = fd; if ((rc = write_dcdheader(dcd->fd, remarks, natoms, istart, nsavc, delta, with_unitcell, charmm)) < 0) { PyErr_SetString(PyExc_IOError, "Cannot write header of DCD file"); free(dcd); return NULL; } dcd->natoms = natoms; dcd->nsets = 0; dcd->istart = istart; dcd->nsavc = nsavc; dcd->delta = delta; dcd->with_unitcell = with_unitcell; dcd->charmm = charmm; temp = PyCObject_FromVoidPtr(dcd, free); // Creates a New Reference if (PyObject_SetAttrString(self, "_dcd_C_ptr", temp) == -1) { // Raise exception - who knows what exception to raise?? PyErr_SetString(PyExc_AttributeError, "Could not create attribute _dcd_C_ptr"); Py_DECREF(temp); return NULL; } Py_DECREF(temp); // For debugging purposes temp = PyBuffer_FromMemory(dcd, sizeof(dcdhandle)); // Creates a New Reference if (PyObject_SetAttrString(self, "_dcd_C_str", temp) == -1) { // Raise exception - who knows what exception to raise?? PyErr_SetString(PyExc_AttributeError, "Could not create attribute _dcd_C_str"); Py_DECREF(temp); return NULL; } Py_DECREF(temp); Py_INCREF(Py_None); return Py_None; }