File: MMTK_DCD.c

package info (click to toggle)
mmtk 2.7.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,788 kB
  • ctags: 6,600
  • sloc: python: 18,050; ansic: 12,400; makefile: 129; csh: 3
file content (330 lines) | stat: -rw-r--r-- 8,441 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* I/O in DCD format
 *
 * Written by Lutz Ehrlich
 * Adapted to MMTK conventions by Konrad Hinsen
 */

#include "MMTK/universe.h"
#include "MMTK/trajectory.h"
#include "MMTK/readdcd.h"

/* Global variables */

double angstrom_factor;
double akma_time_factor;

/* Allocate and initialize Output variable descriptors */

static  PyTrajectoryVariable *
get_data_descriptors(PyArrayObject *configuration, double *time,
		     double *box_size, int box_size_length)
{
  static PyTrajectoryVariable vars[4];
  if (vars != NULL) {
    vars[0].name = "time";
    vars[0].text = "Time: %lf\n";
    vars[0].unit = time_unit_name;
    vars[0].type = PyTrajectory_Scalar;
    vars[0].class = PyTrajectory_Time;
    vars[0].value.dp = time;
    vars[1].name = "configuration";
    vars[1].text = "Configuration:\n";
    vars[1].unit = length_unit_name;
    vars[1].type = PyTrajectory_ParticleVector;
    vars[1].class = PyTrajectory_Configuration;
    vars[1].value.array = configuration;
    if (box_size_length > 0) {
      vars[2].name = "box_size";
      vars[2].text = "Box size:";
      vars[2].unit = length_unit_name;
      vars[2].type = PyTrajectory_BoxSize;
      vars[2].class = PyTrajectory_Configuration;
      vars[2].value.dp = box_size;
      vars[2].length = box_size_length;
      vars[3].name = NULL;
    }
    else
      vars[2].name = NULL;
  }
  return vars;
}


static  PyObject *
readDCD(PyObject *dummy, PyObject *args)
{
  PyObject *universe;
  PyUniverseSpecObject *universe_spec;
  PyArrayObject *configuration;
  PyListObject *spec_list;
  PyTrajectoryOutputSpec *output;
  vector3 *x;
  int atoms;

  char buffer[100];
  PyTrajectoryVariable *data_descriptors;
  int j;

  /* DCD transformation variables */
  char * dcdFileName;
  FILE * dcdFile;
  int *  dcdFreeatoms;
  int    dcdAtoms;
  int    dcdNFrames=0;
  int    dcdFrameStart=0;
  int    dcdFrameSkip=0;
  float  dcdTimeStep;
  int    dcdNamnf;
  float *dcdX = NULL;
  float *dcdY = NULL;
  float *dcdZ = NULL;
  int    dcdErrcode;
  int    currFrames=0;
  double time;

  dcdFreeatoms = NULL;
    

  /* Parse and check arguments */
  if (!PyArg_ParseTuple(args, "OO!O!s", &universe,
			&PyArray_Type, &configuration,
			&PyList_Type, &spec_list, &dcdFileName))
    return NULL;

  universe_spec = (PyUniverseSpecObject *)
                   PyObject_GetAttrString(universe, "_spec");
  if (universe_spec == NULL)
    return NULL;
  atoms = configuration->dimensions[0];
  x = (vector3 *)configuration->data;

  /* Prepare output data descriptors */
  data_descriptors =
    get_data_descriptors(configuration, &time,
			 universe_spec->geometry_data,
			 universe_spec->geometry_data_length);

  /* Initialize output */
  output = PyTrajectory_OutputSpecification(universe, spec_list,
					    dcdFileName,
					    data_descriptors);
  if (output == NULL)
    return NULL;

  /* open DCD file  */
  dcdFile = open_dcd_read( dcdFileName );
  if ( dcdFile == NULL ){
    PyErr_SetString(PyExc_IOError, "Cannot open file");
    goto error;
  }

  /* read header */
  dcdErrcode = read_dcdheader(dcdFile, &dcdAtoms, &dcdNFrames, 
			      &dcdFrameStart,  &dcdFrameSkip, &dcdTimeStep,
			      &dcdNamnf, &dcdFreeatoms);
  if ( dcdErrcode == DCD_BADFORMAT ) {
    PyErr_SetString(PyExc_IOError, "Not a DCD file");
    goto error;
  }
  else if ( dcdErrcode != 0 ) {
    PyErr_SetString(PyExc_IOError, "DCD reading error");
    goto error;
  }
  if( atoms != dcdAtoms ){
    snprintf(buffer, sizeof(buffer),
             "number of atoms in DCD file (%d) doesn't "
	    "match universe (%d)", dcdAtoms, atoms);
    PyErr_SetString(PyExc_ValueError, buffer);
    goto error;
  }
  if ( dcdNamnf != 0 ){
    PyErr_SetString(PyExc_ValueError, "Can't read DCD files with free atoms");
    goto error;
  }

  /* allocate the dcd{X,Y,Z} arrays */
  dcdX = ( float*) malloc(dcdAtoms * sizeof(float) );
  dcdY = ( float*) malloc(dcdAtoms * sizeof(float) );
  dcdZ = ( float*) malloc(dcdAtoms * sizeof(float) );
  if( (dcdX==NULL) || (dcdY==NULL) || (dcdZ==NULL) ){
    PyErr_NoMemory();
    goto error;
  }
  
  /* read in the frames one after the other */
  currFrames = 0;
  time = 0.;
  while ( 1 ){
    int err_code = read_dcdstep(dcdFile, dcdAtoms, dcdX, dcdY, dcdZ,
				dcdNamnf, (currFrames == 0), dcdFreeatoms);
    if (err_code == -1)
      break;
    if (err_code < 0) {
      PyErr_SetString(PyExc_IOError, "DCD read error");
      goto error;
    }
    for (j = 0; j < dcdAtoms; j++) {
       x[j][0] = angstrom_factor * dcdX[j];
       x[j][1] = angstrom_factor * dcdY[j];
       x[j][2] = angstrom_factor * dcdZ[j];
    }
    if (PyTrajectory_Output(output, currFrames, data_descriptors, NULL) == -1)
      goto error;
    currFrames++;
    time += dcdFrameSkip*dcdTimeStep*akma_time_factor;
  }
  close_dcd_read(dcdFile,0,dcdFreeatoms);

  /* Clean up and return None */
  if( dcdX != NULL )
    free(dcdX);
  if( dcdY != NULL )
    free(dcdY);
  if( dcdZ != NULL )
    free(dcdZ);
  PyTrajectory_OutputFinish(output, currFrames-1, 0, 1, data_descriptors);
  Py_INCREF(Py_None);
  return Py_None;

  /* Clean up and return error */
error:
  if( dcdX != NULL )
    free(dcdX);
  if( dcdY != NULL )
    free(dcdY);
  if( dcdZ != NULL )
    free(dcdZ);
  close_dcd_read(dcdFile,0,dcdFreeatoms);
  PyTrajectory_OutputFinish(output, currFrames, 1, 1, data_descriptors);
  return NULL;
}


static PyObject *
writeOpenDCD(PyObject *dummy, PyObject *args)
{
  /* DCD transformation variables */
  char * dcdFileName;
  int    dcdAtoms;
  int    dcdNFrames=0;
  int    dcdFrameStart=0;
  int    dcdNSavc;
  double time;
  FILE * fd;

  /* Parse and check arguments */
  if (!PyArg_ParseTuple(args, "siiiid", &dcdFileName,&dcdAtoms, &dcdNFrames, 
			&dcdFrameStart, &dcdNSavc, &time))
    return NULL;

  /* open DCD file  */
  fd = open_dcd_write( dcdFileName );
  if ( fd == NULL ){
    PyErr_SetString(PyExc_IOError, "Cannot open file");
    return NULL;
  }

  /* write header */
  write_dcdheader(fd , dcdFileName, dcdAtoms, dcdNFrames, 
                  dcdFrameStart,  dcdNSavc,
                  time/akma_time_factor);
  return (PyObject*)PyCObject_FromVoidPtr((void *) fd, NULL);
}

static  PyObject *
writeDCDStep(PyObject *dummy, PyObject *args)
{
  int    dcdAtoms;
  float *dcdX = NULL;
  float *dcdY = NULL;
  float *dcdZ = NULL;
  int err;
  FILE * fd;
  PyObject *fd_cobj;
  PyArrayObject *xconfig, *yconfig, *zconfig;

  /* Parse and check arguments */
  if (!PyArg_ParseTuple(args, "O!O!O!O!",
                        &PyCObject_Type, &fd_cobj,
			&PyArray_Type, &xconfig,
			&PyArray_Type, &yconfig,
			&PyArray_Type, &zconfig))
    return NULL;

  fd = PyCObject_AsVoidPtr(fd_cobj);
  dcdAtoms = xconfig->dimensions[0];
  dcdX = (float *)xconfig->data;
  dcdY = (float *)yconfig->data;
  dcdZ = (float *)zconfig->data;

  err = write_dcdstep( fd, dcdAtoms, dcdX, dcdY, dcdZ);
  if (err != 1) {
    PyErr_SetString(PyExc_IOError, "Couldn't write DCD step");
    return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

static  PyObject *
writeCloseDCD(PyObject *dummy, PyObject *args)
{
  PyObject *fd_cobj;
  FILE * fd;

  /* Parse and check arguments */
  if (!PyArg_ParseTuple(args, "O!",
                        &PyCObject_Type, &fd_cobj))
    return NULL;

  fd = PyCObject_AsVoidPtr(fd_cobj);
  close_dcd_read(fd, 0, NULL);

  Py_INCREF(Py_None);
  return Py_None;

}


static PyMethodDef DCD_methods[] = {
  {"readDCD", readDCD, 1},
  {"writeOpenDCD", writeOpenDCD, 1},
  {"writeDCDStep", writeDCDStep, 1},
  {"writeCloseDCD", writeCloseDCD, 1},
  {NULL, NULL}		/* sentinel */
};


/* Initialization function for the module */

DL_EXPORT(void)
initMMTK_DCD(void)
{
  PyObject *units;

  /* Create the module and add the functions */
  Py_InitModule("MMTK_DCD", DCD_methods);

  /* Import the array module */
#ifdef import_array
  import_array();
#endif

  /* Import MMTK modules */
  import_MMTK_trajectory();

  /* Get the length and time unit conversion factor from Units */
  units = PyImport_ImportModule("MMTK.Units");
  if (units != NULL) {
    PyObject *module_dict = PyModule_GetDict(units);
    PyObject *factor = PyDict_GetItemString(module_dict, "Ang");
    angstrom_factor = PyFloat_AsDouble(factor);
    factor = PyDict_GetItemString(module_dict, "akma_time");
    akma_time_factor = PyFloat_AsDouble(factor);
  }

  /* Check for errors */
  if (PyErr_Occurred())
    Py_FatalError("can't initialize module MMTK_DCD");
}