Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

rst7plugin.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2016 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.21 $       $Date: 2017/08/30 17:27:29 $
00015  *
00016  ***************************************************************************/
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include "molfile_plugin.h"
00022 #include "vmdconio.h"
00023 
00024 typedef struct {
00025   FILE *file;
00026   int has_box;
00027   int has_vels;
00028   int numatoms;
00029   int count;
00030   int rstfile;
00031 #if vmdplugin_ABIVERSION > 10
00032   molfile_timestep_metadata_t ts_meta;
00033 #endif
00034 } rstdata;
00035 
00036 static void *open_rst_read(const char *filename, const char *filetype,int *natoms) {
00037   FILE *fd;
00038   rstdata *data;
00039   int numats=0,i,j,point2,kkk=1; 
00040   char title[82], *field;
00041   char line[82];
00042   float x, y, z,a=0.0,b=0.0,c=0.0;
00043   double  timesteprst;
00044 
00045   /* Amber 7'coord' restart files have a second introduction line with 
00046    * possibly 2 entries only check for one now...
00047    * they include three 90.00 ter cards at the end
00048    * need to fix this, real crd files have atom record but no timestep and no
00049    * velocity info...arggggg
00050    */
00051   fd = fopen(filename, "rb");
00052   if (!fd) 
00053     return NULL; /* failure */
00054 
00055   data = (rstdata *)malloc(sizeof(rstdata));
00056   memset(data, 0, sizeof(rstdata));
00057 #if vmdplugin_ABIVERSION > 10
00058   data->ts_meta.count = -1;
00059   data->ts_meta.has_velocities = 0;
00060 #endif
00061 
00062   fgets(title, 82, fd);
00063   vmdcon_printf(VMDCON_INFO, "rst7plugin) Title: %s\n",title);
00064 
00065   fgets(line, 82, fd);
00066   while (kkk==1) {
00067     /* try to read first field */
00068     field = strtok(line, " \t");
00069     if (field==NULL) {                
00070       continue; /* no fields at all on this line */
00071     }
00072     numats = atoi(field);
00073 
00074     /* try to read second field will be null if not there */
00075     field = strtok(NULL, " \t");
00076     if (field==NULL) {
00077       kkk=0;
00078       vmdcon_printf(VMDCON_INFO, "rst7plugin) This file has no velocity info.\n");
00079       data->has_vels=0;
00080     } else {
00081       timesteprst = strtod(field, NULL);
00082       vmdcon_printf(VMDCON_INFO, "rst7plugin) This file contains velocity info.\n");
00083       data->has_vels=1;
00084 #if vmdplugin_ABIVERSION > 10
00085       data->ts_meta.has_velocities = 1;
00086 #endif
00087       kkk=0;
00088     }
00089   }
00090 
00091   point2=ftell(fd);
00092   data->file = fd;
00093   vmdcon_printf(VMDCON_INFO, "rst7plugin) The Restartcrd has %d atoms.\n",numats);
00094 
00095   /* skip over coordinate data */
00096   for (i=0; i<numats; i++) {
00097     j = fscanf(fd, "%f%f%f", &x, &y, &z);
00098   }
00099 
00100   /* skip over velocity data, if present */
00101   if (data->has_vels) {
00102     for (i=0; i<numats; i++) {
00103       j = fscanf(fd, "%f%f%f", &x, &y, &z);
00104     }
00105   }
00106   
00107   j = fscanf(fd, "%f%f%f%f%f%f", &x, &y, &z,&a,&b,&c);
00108   if (j != EOF) {
00109     vmdcon_printf(VMDCON_INFO, "rst7plugin) This restartcrd file has box info.\n");
00110     data->has_box=1;
00111     vmdcon_printf(VMDCON_INFO, "rst7plugin) Box Dimensions are %f %f %f %f %f %f\n",x,y,z,a,b,c);
00112   }
00113 
00114   *natoms=numats;
00115   data->numatoms=numats;
00116   data->rstfile=1;
00117   fseek(fd,point2,SEEK_SET);
00118 
00119   return data;
00120 }
00121 
00122 #if vmdplugin_ABIVERSION > 10
00123 static int read_timestep_metadata(void *mydata,
00124                                   molfile_timestep_metadata_t *meta) {
00125   rstdata *data = (rstdata *)mydata;
00126   
00127   meta->count = -1;
00128   meta->has_velocities = data->ts_meta.has_velocities;
00129   if (meta->has_velocities) {
00130     vmdcon_printf(VMDCON_INFO,
00131                   "rst7plugin) Importing velocities from restart file.\n");
00132   }
00133   return MOLFILE_SUCCESS;
00134 }
00135 #endif
00136 
00137 static int read_rst_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00138   rstdata *rst= (rstdata *)mydata;
00139   int i, j;
00140   float x, y, z, a, b, c;
00141   
00142   /* check for rst and first read through already taken place */
00143   if(rst->count==1 && rst->rstfile==1) 
00144     return MOLFILE_ERROR; 
00145 
00146   ts->A = ts->B = ts->C = 0.0f;
00147   ts->alpha = ts->beta = ts->gamma = 90.0f;
00148   
00149   for (i=0; i<rst->numatoms; i++)  {
00150     /* changed to i=1 BB */
00151     j = fscanf(rst->file, "%f%f%f", &x, &y, &z);
00152     if (j == EOF) {
00153       return MOLFILE_ERROR;
00154     } else if (j <= 0) {
00155       vmdcon_printf(VMDCON_ERROR, "rst7plugin) Problem reading CRD file\n");
00156       return MOLFILE_ERROR;
00157     }
00158     ts->coords[3*i] = x;
00159     ts->coords[3*i+1] = y;
00160     ts->coords[3*i+2] = z;
00161   }
00162 
00163   if (rst->has_vels) {
00164     /* Read in optional velocity data.  Units are Angstroms per 1/20.455ps. */
00165     for (i=0; i<rst->numatoms; i++)  {
00166       j = fscanf(rst->file, "%f%f%f", &x, &y, &z);
00167       if (j == EOF) {
00168         return MOLFILE_ERROR;
00169       } else if (j <= 0) {
00170         vmdcon_printf(VMDCON_ERROR, "rst7plugin) Problem reading velocities\n");
00171         return MOLFILE_ERROR;
00172       }
00173 #if vmdplugin_ABIVERSION > 10
00174       if (ts->velocities != NULL) {
00175         ts->velocities[3*i] = x;
00176         ts->velocities[3*i+1] = y;
00177         ts->velocities[3*i+2] = z;
00178       }
00179 #endif
00180     }
00181   }
00182  
00183   if (rst->has_box) {
00184     j = fscanf(rst->file, "%f%f%f%f%f%f", &x, &y, &z, &a, &b, &c);
00185     if (j == EOF) {
00186       vmdcon_printf(VMDCON_ERROR, "rst7plugin) Problem reading box data\n");
00187       return MOLFILE_ERROR;
00188     }
00189     ts->A = x;
00190     ts->B = y;
00191     ts->C = z;
00192     ts->alpha = a;
00193     ts->beta = b;
00194     ts->gamma = c;
00195   }
00196   rst->count++;
00197   /* printf("rst->count: %d\n",rst->count); */
00198 
00199   return MOLFILE_SUCCESS;
00200 }
00201     
00202 static void close_rst_read(void *mydata) {
00203   rstdata *rst= (rstdata *)mydata;
00204   fclose(rst->file);
00205   free(rst);
00206 }
00207 
00208 static void *open_rst_write(const char *path, const char *filetype, int natoms) {
00209   char title[82];
00210   rstdata *rst;
00211   FILE *fd;
00212   int len;
00213 
00214   fd = fopen(path, "wb");
00215   if (!fd) {
00216     vmdcon_printf(VMDCON_ERROR, "rst7plugin) Could not open file %s for writing\n", path);
00217     return NULL;
00218   }
00219   /* write out fixed length fortran style string */
00220   sprintf(title, "TITLE : Created by VMD with %d atoms",natoms);
00221   len = strlen(title);
00222   memset(title+len,(int)' ',82-len);
00223   title[80] = '\n';
00224   title[81] = '\0';
00225   fputs(title,fd);
00226 
00227   rst = (rstdata *)malloc(sizeof(rstdata));
00228   rst->file = fd;
00229   rst->numatoms = natoms;
00230   rst->has_box = 1;
00231   return rst;
00232 }
00233   
00234 static int write_rst_timestep(void *v, const molfile_timestep_t *ts) {
00235   rstdata *rst = (rstdata *)v;
00236   int i;
00237   const int ndata = rst->numatoms * 3;
00238 
00239 #if vmdplugin_ABIVERSION > 10
00240   if (ts->velocities != NULL) {
00241     fprintf(rst->file, "%6d %13.7g\n", rst->numatoms, ts->physical_time);
00242   } else
00243 #endif
00244     fprintf(rst->file, "%6d\n", rst->numatoms);
00245 
00246   for (i=0; i<ndata; i++) {
00247     fprintf(rst->file, "%12.7f", ts->coords[i]);
00248     if ((i+1) % 6 == 0) fprintf(rst->file, "\n"); 
00249   }
00250   if (ndata % 6 != 0) fprintf(rst->file,"\n");
00251 
00252 #if vmdplugin_ABIVERSION > 10
00253   if (ts->velocities != NULL) {
00254     for (i=0; i<ndata; i++) {
00255       fprintf(rst->file, "%12.7f", ts->velocities[i]);
00256       if ((i+1) % 6 == 0) fprintf(rst->file, "\n"); 
00257     }
00258     if (ndata % 6 != 0) fprintf(rst->file,"\n");
00259   }
00260 #endif
00261 
00262   fprintf (rst->file, "%12.7f%12.7f%12.7f%12.7f%12.7f%12.7f\n",
00263            ts->A, ts->B, ts->C, ts->alpha, ts->beta, ts->gamma);
00264 
00265   return MOLFILE_SUCCESS;
00266 }
00267 
00268 static void close_rst_write(void *v) {
00269   rstdata *rst = (rstdata *)v;
00270   fclose(rst->file);
00271   free(rst);
00272 }
00273 
00274 /* registration stuff */
00275 static molfile_plugin_t plugin;
00276 
00277 VMDPLUGIN_API int VMDPLUGIN_init(){
00278   memset(&plugin, 0, sizeof(molfile_plugin_t));
00279   plugin.abiversion = vmdplugin_ABIVERSION;
00280   plugin.type = MOLFILE_PLUGIN_TYPE;
00281   plugin.name = "rst7";
00282   plugin.prettyname = "AMBER7 Restart";
00283   plugin.author = "Brian Bennion, Axel Kohlmeyer";
00284   plugin.majorv = 0;
00285   plugin.minorv = 5;
00286   plugin.is_reentrant = VMDPLUGIN_THREADUNSAFE;
00287   plugin.filename_extension = "rst7";
00288   plugin.open_file_read = open_rst_read;
00289   plugin.read_next_timestep = read_rst_timestep;
00290 #if vmdplugin_ABIVERSION > 10
00291   plugin.read_timestep_metadata = read_timestep_metadata;
00292 #endif
00293   plugin.close_file_read = close_rst_read;
00294   plugin.open_file_write = open_rst_write;
00295   plugin.write_timestep = write_rst_timestep;
00296   plugin.close_file_write = close_rst_write;
00297   return VMDPLUGIN_SUCCESS;
00298 }
00299 
00300 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00301   (*cb)(v, (vmdplugin_t *)&plugin);
00302   return VMDPLUGIN_SUCCESS;
00303 }
00304 
00305 VMDPLUGIN_API int VMDPLUGIN_fini(){ return VMDPLUGIN_SUCCESS; }
00306 

Generated on Tue Jan 21 02:56:22 2020 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002