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

rst7plugin.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: rst7plugin.c,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.16 $       $Date: 2006/02/23 19:36:45 $
00015  *
00016  ***************************************************************************/
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include "molfile_plugin.h"
00022 
00023 typedef struct {
00024   FILE *file;
00025   int has_box;
00026   int numatoms;
00027   int count;
00028   int rstfile;
00029 } rstdata;
00030 
00031 static void *open_rst_read(const char *filename, const char *filetype,int *natoms) {
00032   FILE *fd;
00033   rstdata *data;
00034   int numats=0,i,j,point2,kkk=1; 
00035   char title[82], *field;
00036   char line[82];
00037   float x, y, z,a=0.0,b=0.0,c=0.0;
00038   double  timesteprst;
00039 
00040   /* Amber 7'coord' restart files have a second introduction line with 
00041    * possibly 2 entries only check for one now...
00042    * they include three 90.00 ter cards at the end
00043    * need to fix this, real crd files have atom record but no timestep and no
00044    * velocity info...arggggg
00045    */
00046   fd = fopen(filename, "rb");
00047   if (!fd) 
00048     return NULL; /* failure */
00049 
00050   data = (rstdata *)malloc(sizeof(rstdata));
00051   memset(data, 0, sizeof(rstdata));
00052   fgets(title, 82, fd);
00053   printf("Title: %s\n",title);
00054 
00055   fgets(line, 82, fd);
00056   while (kkk==1) {
00057     /* try to read first field */
00058     field = strtok(line, "  ");
00059     if (field==NULL) {                
00060       continue; /* no fields at all on this line */
00061     }
00062     numats = atoi(field);
00063 
00064     /* try to read second field will be null if not there */
00065     field = strtok(line, "  ");
00066     if (field==NULL) {
00067       kkk=0;
00068       printf("This file has no velocity info.\n");
00069     } else {
00070       timesteprst = strtod(field, NULL);
00071       printf("This file contains velocity info.\n");
00072       kkk=0;
00073     }
00074   }
00075 
00076   point2=ftell(fd);
00077   data->file = fd;
00078   printf("The Restartcrd has %d atoms.\n",numats);
00079   for (i=0; i<numats; i++) {
00080     j = fscanf(fd, "%f %f %f", &x, &y, &z);
00081   }
00082 
00083   j = fscanf(fd, "%f %f %f %f %f %f", &x, &y, &z,&a,&b,&c);
00084   if (j != EOF) {
00085     printf("This restartcrd file has box info.\n");
00086     data->has_box=1;
00087     if((int)a==90) {
00088       printf("Box Dimensions are %f  %f  %f  %f  %f  %f\n",x,y,z,a,b,c);
00089     } else {
00090       for (i=0; i<numats-2; i++) {
00091         j = fscanf(fd, "%f %f %f", &x, &y, &z);
00092       }
00093       j = fscanf(fd, "%f %f %f %f %f %f", &x, &y, &z,&a,&b,&c);
00094       if (j != EOF) {
00095         if((int)a==90) {
00096           printf("Box Dimensions are %f  %f  %f  %f  %f  %f\n",x,y,z,a,b,c);
00097         }
00098       }
00099     }
00100   } 
00101 
00102   *natoms=numats;
00103   data->numatoms=numats;
00104   data->rstfile=1;
00105   fseek(fd,point2,SEEK_SET);
00106 
00107   return data;
00108 }
00109 
00110 static int read_rst_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00111   rstdata *rst= (rstdata *)mydata;
00112   int i, j;
00113   float x, y, z;
00114 
00115   /* check for rst and first read through already taken place */
00116   if(rst->count==1 && rst->rstfile==1) 
00117     return MOLFILE_ERROR; 
00118 
00119   for (i=0; i<rst->numatoms; i++)  {
00120     /* changed to i=1 BB */
00121     j = fscanf(rst->file, "%f %f %f", &x, &y, &z);
00122     if (j == EOF) {
00123       return MOLFILE_ERROR;
00124     } else if (j <= 0) {
00125       fprintf(stderr, "Problem reading CRD file\n");
00126       return MOLFILE_ERROR;
00127     }
00128     ts->coords[3*i] = x;
00129     ts->coords[3*i+1] = y;
00130     ts->coords[3*i+2] = z;
00131   }
00132 
00133   /* Read in optional velocity data.  Units are Angstroms per 1/20.455ps. */
00134   /* XXX Not currently implemented.  This should be added sometime soon.  */
00135  
00136   /* Don't Read box info.  No use for it yet.. */
00137   rst->count++;
00138   /* printf("rst->count: %d\n",rst->count); */
00139 
00140   return MOLFILE_SUCCESS;
00141 }
00142     
00143 static void close_rst_read(void *mydata) {
00144   rstdata *rst= (rstdata *)mydata;
00145   fclose(rst->file);
00146   free(rst);
00147 }
00148 
00149 static void *open_rst_write(const char *path, const char *filetype, int natoms) {
00150   /* Not fixed for writing proper rsts yet....BB */
00151   rstdata *rst;
00152   FILE *fd;
00153 
00154   fd = fopen(path, "wb");
00155   if (!fd) {
00156     fprintf(stderr, "Could not open file %s for writing\n", path);
00157     return NULL;
00158   }
00159   fprintf(fd, "TITLE : Created by VMD with %d atoms\n",natoms);
00160   
00161   rst = (rstdata *)malloc(sizeof(rstdata));
00162   rst->file = fd;
00163   rst->numatoms = natoms;
00164   rst->has_box = strcmp(filetype, "rst"); 
00165   return rst;
00166 }    
00167   
00168 static int write_rst_timestep(void *v, const molfile_timestep_t *ts) {
00169   rstdata *rst = (rstdata *)v;
00170   int i;
00171   const int ndata = rst->numatoms * 3;
00172   for (i=0; i<ndata; i++) {
00173     fprintf(rst->file, "%8.3f", ts->coords[i]);
00174     if (i % 10 == 0) fprintf(rst->file, "\n"); 
00175   }
00176   if (rst->has_box) {
00177     fprintf (rst->file, "\n0.000 0.000 0.000\n");
00178   }
00179 
00180   return MOLFILE_SUCCESS;
00181 }
00182 
00183 static void close_rst_write(void *v) {
00184   rstdata *rst = (rstdata *)v;
00185   fclose(rst->file);
00186   free(rst);
00187 }
00188 
00189 /* registration stuff */
00190 static molfile_plugin_t rst7plugin = {
00191     vmdplugin_ABIVERSION,   /* ABI version */
00192     MOLFILE_PLUGIN_TYPE,    /* type of plugin */
00193     "rst7",                 /* short name of plugin */
00194     "AMBER7 Restart",       /* pretty name of plugin */
00195     "Brian Bennion",        /* authors */
00196     0,                      /* major version */
00197     2,                      /* minor version */
00198     VMDPLUGIN_THREADUNSAFE, /* is not reentrant */
00199     "rst7",                 /* filename extension */
00200     open_rst_read,
00201     0,
00202     0,
00203     read_rst_timestep,
00204     close_rst_read,
00205     open_rst_write,
00206     0,
00207     write_rst_timestep,
00208     close_rst_write
00209 };
00210 
00211 VMDPLUGIN_API int VMDPLUGIN_init(){
00212   return VMDPLUGIN_SUCCESS;
00213 }
00214 VMDPLUGIN_API int VMDPLUGIN_fini(){
00215   return VMDPLUGIN_SUCCESS;
00216 }
00217 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00218   (*cb)(v, (vmdplugin_t *)&rst7plugin);
00219   return VMDPLUGIN_SUCCESS;
00220 }

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