Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

cpmdplugin.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2006 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: cpmdplugin.c,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.11 $       $Date: 2006/02/23 19:36:44 $
00015  *
00016  ***************************************************************************/
00017 
00018 #include "largefiles.h"   /* platform dependent 64-bit file I/O defines */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include "molfile_plugin.h"
00024 
00025 typedef struct {
00026   FILE *file;
00027   int numatoms;
00028   const char *file_name;
00029 } cpmddata;
00030  
00031 static void *open_cpmd_read(const char *filename, const char *filetype, 
00032                            int *natoms) {
00033   FILE *fd;
00034   cpmddata *data;
00035   char linebuf[255];
00036   int i, nfi_first, nfi_current, atomcount;
00037  
00038   printf("cpmd) trying to open file '%s'\n",filename);
00039           
00040   fd = fopen(filename, "rb");
00041   if (!fd) return NULL;
00042   
00043   data = (cpmddata *)malloc(sizeof(cpmddata));
00044   data->file = fd;
00045   data->file_name = filename;
00046 
00047   nfi_first = 0;
00048   nfi_current = 0;
00049 
00050   /* first column is the current timestep number  */
00051   fgets(linebuf, 255, fd);
00052   i = sscanf(linebuf, "%d", &nfi_first);
00053   if (i < 1) {
00054     fprintf(stderr, "read) cpmd trajectory file '%s' should have the timestep number "
00055             "in the first column\n", filename);
00056     return NULL;
00057   }
00058   atomcount   = 0;
00059   nfi_current = nfi_first;
00060 
00061   /* loop through file until the contents of the first column
00062      changes, indicating the end of a configuration */
00063   while ((nfi_first == nfi_current) && !ferror(fd) && !feof(fd)) {
00064     ++atomcount;
00065     fgets(linebuf, 255, fd);
00066     i = sscanf(linebuf, "%d", &nfi_current);
00067     if (i < 1) {
00068       fprintf(stderr, "read) cpmd trajectory file '%s' should have the "
00069               "timestep number in the first column\n", filename);
00070       return NULL;
00071     }
00072   }
00073   printf("cpmd) found %d atoms in first timestep\n",atomcount);
00074   *natoms = atomcount;
00075   data->numatoms=*natoms;
00076 
00077   /* rewind to the beginning for reading the coordinates elsewhere.*/
00078   rewind(fd);
00079 
00080   return data;
00081 }
00082 
00083 static int read_cpmd_structure(void *mydata, int *optflags, 
00084                               molfile_atom_t *atoms) {
00085   int i, j;
00086   char *k;
00087   float coord;
00088   molfile_atom_t *atom;
00089   cpmddata *data = (cpmddata *)mydata;
00090   
00091   printf("cpmd) trying to read structure\n");
00092   *optflags = MOLFILE_NOOPTIONS; /* no optional data */
00093 
00094   for(i=0;i<data->numatoms;i++) {
00095     char buffer[1024];
00096     char fbuffer[1024];
00097     k = fgets(fbuffer, 1024, data->file);
00098     atom = atoms + i;
00099     j=sscanf(fbuffer, "%s %f %f %f", buffer, &coord, &coord, &coord);
00100     if (k == NULL) {
00101       fprintf(stderr, "cpmd structure) missing atom(s) in file '%s'\n",data->file_name);
00102       fprintf(stderr, "cpmd structure) expecting '%d' atoms, found only '%d'\n",data->numatoms,i+1);
00103       return MOLFILE_ERROR;
00104     } else if (j < 4) {
00105       fprintf(stderr, "cpmd structure) missing type or coordinate(s) in file '%s' for atom '%d'\n",data->file_name,i+1);
00106       return MOLFILE_ERROR;
00107     }
00108 
00109     /* I don't know what to do with these */
00110     strncpy(atom->name, buffer, sizeof(atom->name));
00111     strncpy(atom->type, buffer, sizeof(atom->type));
00112     atom->resname[0] = '\0';
00113     atom->resid = 1;
00114     atom->chain[0] = '\0';
00115     atom->segid[0] = '\0';
00116     /* skip to the end of line */
00117   }
00118 
00119   rewind(data->file);
00120   return MOLFILE_SUCCESS;
00121 }
00122 
00123 static int read_cpmd_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00124   int i, j, nfi_first, nfi_current;
00125   char fbuffer[1024];
00126   float x, y, z;
00127   const float bohr=0.529177249;
00128   char *k;
00129   
00130   cpmddata *data = (cpmddata *)mydata;
00131   nfi_first = nfi_current = -1;
00132   
00133   /* read the coordinates */
00134   for (i=0; i<natoms; i++) {
00135 
00136     k = fgets(fbuffer, 1024, data->file);
00137 
00138     /* read next line if this is a continuation indicator */
00139     if (strstr(fbuffer, "NEW DATA")) {
00140       k = fgets(fbuffer, 1024, data->file);
00141     }
00142     j = sscanf(fbuffer, "%d %f %f %f", &nfi_current, &x, &y, &z);
00143     if (nfi_first < 0) nfi_first = nfi_current;
00144     
00145     if (k == NULL) {
00146       return MOLFILE_ERROR;
00147     } else if (j < 4) {
00148       fprintf(stderr, "cpmd timestep) missing or illegal data in file"
00149               " '%s' for atom '%d'\n",data->file_name,i+1);
00150       return MOLFILE_ERROR;
00151     } else if (nfi_first != nfi_current) {
00152       fprintf(stderr, "cpmd timestep) short record in timestep %d of file"
00153               " '%s' for atom '%d'\n",nfi_first, data->file_name,i+1);
00154     }
00155     
00156     ts->coords[3*i  ] = x*bohr;
00157     ts->coords[3*i+1] = y*bohr;
00158     ts->coords[3*i+2] = z*bohr;
00159   }
00160 
00161   return MOLFILE_SUCCESS;
00162 }
00163     
00164 static void close_cpmd_read(void *mydata) {
00165   cpmddata *data = (cpmddata *)mydata;
00166   
00167   fclose(data->file);
00168   free(data);
00169 }
00170 
00171 
00172 /* registration stuff */
00173 static molfile_plugin_t cpmdplugin = {
00174   vmdplugin_ABIVERSION,
00175   MOLFILE_PLUGIN_TYPE,                         /* type */
00176   "cpmd",                                      /* short name */
00177   "CPMD",                                      /* pretty name */
00178   "Axel Kohlmeyer, John E. Stone",             /* author */
00179   0,                                           /* major version */
00180   3,                                           /* minor version */
00181   VMDPLUGIN_THREADSAFE,                        /* is reentrant */
00182   "cpmd",
00183   open_cpmd_read,
00184 /*  read_cpmd_structure */
00185   0,
00186   0,
00187   read_cpmd_timestep,
00188   close_cpmd_read,
00189 /*  open_cpmd_write, */
00190   0,
00191   0,
00192 /*  write_cpmd_timestep, */
00193   0,
00194 /*  close_cpmd_write */
00195   0
00196 };
00197 
00198 VMDPLUGIN_API int VMDPLUGIN_init() {
00199   return VMDPLUGIN_SUCCESS;
00200 }
00201 
00202 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00203   (*cb)(v, (vmdplugin_t *)&cpmdplugin);
00204   return VMDPLUGIN_SUCCESS;
00205 }
00206 
00207 VMDPLUGIN_API int VMDPLUGIN_fini() {
00208   return VMDPLUGIN_SUCCESS;
00209 }
00210 

Generated on Wed Mar 22 13:15:28 2006 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002