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.22 $       $Date: 2020/11/01 06:30:44 $
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,velpbcoffset=0; 
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 = 1;
00060 #endif
00061   data->has_vels=1; /* Assume we have velocities and correct if we don't */
00062 
00063   fgets(title, 82, fd);
00064   vmdcon_printf(VMDCON_INFO, "rst7plugin) Title: %s\n",title);
00065 
00066   fgets(line, 82, fd);
00067   kkk=1; /* parse until something is found */
00068   while (kkk==1) {
00069     /* try to read first field */
00070     field = strtok(line, " \t");
00071     if (field==NULL) {                
00072       continue; /* no fields at all on this line, continue and try again. */
00073     }
00074     numats = atoi(field);
00075 
00076     /* try to read second field will be null if not there */
00077     field = strtok(NULL, " \t");
00078     if (field==NULL) {
00079       vmdcon_printf(VMDCON_INFO, "rst7plugin) This file has no timestep information.\n");
00080     } else {
00081       timesteprst = strtod(field, NULL);
00082       vmdcon_printf(VMDCON_INFO, "rst7plugin) This file is from timestep %f.\n", timesteprst);
00083     }
00084     kkk=0;
00085   }
00086 
00087   point2=ftell(fd);
00088   data->file = fd;
00089   vmdcon_printf(VMDCON_INFO, "rst7plugin) This restartcrd has %d atoms.\n",numats);
00090 
00091   /* skip over coordinate data */
00092   for (i=0; i<numats; i++) {
00093     j = fscanf(fd, "%f%f%f", &x, &y, &z);
00094   }
00095   
00096   /* Store the current file offset in case we must rewind to parse 
00097    * PBC unit cell information if it turns out that velocities aren't present.
00098    */
00099   velpbcoffset=ftell(fd);
00100   
00101   /* skip over velocity data, if present */
00102   if (data->has_vels) {
00103     for (i=0; i<numats; i++) {
00104       j = fscanf(fd, "%f%f%f", &x, &y, &z);
00105 
00106       /* If we reach the end of file prematurely, there weren't really 
00107        * velocities in the file.  Reset the file pointer, and check for boxes.
00108        */
00109       if (j == EOF) {
00110         /* Rewind and go back! The file doesn't have velocities after all. */
00111         fseek(fd, velpbcoffset, SEEK_SET);
00112         data->has_vels = 0;
00113 #if vmdplugin_ABIVERSION > 10
00114         data->ts_meta.has_velocities = 0;
00115 #endif
00116         if (i == 2) {
00117           vmdcon_printf(VMDCON_INFO, "rst7plugin) This file does not contain velocity info.\n");
00118         } else {
00119           vmdcon_printf(VMDCON_INFO, "rst7plugin) This file does not have the right number of entries for velocity or boxsize info.\n");
00120         }
00121         break;
00122       }
00123     }
00124   }
00125 
00126   /* Recheck if we have velocity info -- it may have changed in the loop. */
00127   if (data->has_vels) {
00128     vmdcon_printf(VMDCON_INFO, "rst7plugin) This file contains velocity info.\n");
00129   }
00130  
00131   /* Parse PBC unit cell side lengths and angles. */ 
00132   j = fscanf(fd, "%f%f%f%f%f%f", &x, &y, &z,&a,&b,&c);
00133   if (j != EOF) {
00134     vmdcon_printf(VMDCON_INFO, "rst7plugin) This restartcrd file has box info.\n");
00135     data->has_box=1;
00136     vmdcon_printf(VMDCON_INFO, "rst7plugin) Box Dimensions are %f %f %f %f %f %f\n",x,y,z,a,b,c);
00137   }
00138 
00139   *natoms=numats;
00140   data->numatoms=numats;
00141   data->rstfile=1;
00142   fseek(fd,point2,SEEK_SET);
00143 
00144   return data;
00145 }
00146 
00147 #if vmdplugin_ABIVERSION > 10
00148 static int read_timestep_metadata(void *mydata,
00149                                   molfile_timestep_metadata_t *meta) {
00150   rstdata *data = (rstdata *)mydata;
00151   
00152   meta->count = -1;
00153   meta->has_velocities = data->ts_meta.has_velocities;
00154   if (meta->has_velocities) {
00155     vmdcon_printf(VMDCON_INFO,
00156                   "rst7plugin) Importing velocities from restart file.\n");
00157   }
00158   return MOLFILE_SUCCESS;
00159 }
00160 #endif
00161 
00162 static int read_rst_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00163   rstdata *rst= (rstdata *)mydata;
00164   int i, j;
00165   float x, y, z, a, b, c;
00166   
00167   /* check for rst and first read through already taken place */
00168   if(rst->count==1 && rst->rstfile==1) 
00169     return MOLFILE_ERROR; 
00170 
00171   ts->A = ts->B = ts->C = 0.0f;
00172   ts->alpha = ts->beta = ts->gamma = 90.0f;
00173   
00174   for (i=0; i<rst->numatoms; i++)  {
00175     /* changed to i=1 BB */
00176     j = fscanf(rst->file, "%f%f%f", &x, &y, &z);
00177     if (j == EOF) {
00178       vmdcon_printf(VMDCON_ERROR, "rst7plugin) End of file reached before read\n");
00179       return MOLFILE_ERROR;
00180     } else if (j <= 0) {
00181       vmdcon_printf(VMDCON_ERROR, "rst7plugin) Problem reading CRD file\n");
00182       return MOLFILE_ERROR;
00183     }
00184     ts->coords[3*i] = x;
00185     ts->coords[3*i+1] = y;
00186     ts->coords[3*i+2] = z;
00187   }
00188 
00189   if (rst->has_vels) {
00190     /* Read in optional velocity data.  Units are Angstroms per 1/20.455ps. */
00191     for (i=0; i<rst->numatoms; i++)  {
00192       j = fscanf(rst->file, "%f%f%f", &x, &y, &z);
00193       if (j == EOF) {
00194         vmdcon_printf(VMDCON_ERROR, "rst7plugin) End of file reached before read\n");
00195         return MOLFILE_ERROR;
00196       } else if (j <= 0) {
00197         vmdcon_printf(VMDCON_ERROR, "rst7plugin) Problem reading velocities\n");
00198         return MOLFILE_ERROR;
00199       }
00200 #if vmdplugin_ABIVERSION > 10
00201       if (ts->velocities != NULL) {
00202         ts->velocities[3*i  ] = x;
00203         ts->velocities[3*i+1] = y;
00204         ts->velocities[3*i+2] = z;
00205       }
00206 #endif
00207     }
00208   }
00209  
00210   if (rst->has_box) {
00211     j = fscanf(rst->file, "%f%f%f%f%f%f", &x, &y, &z, &a, &b, &c);
00212     if (j == EOF) {
00213       vmdcon_printf(VMDCON_ERROR, "rst7plugin) Problem reading box data\n");
00214       return MOLFILE_ERROR;
00215     }
00216     ts->A = x;
00217     ts->B = y;
00218     ts->C = z;
00219     ts->alpha = a;
00220     ts->beta = b;
00221     ts->gamma = c;
00222   }
00223   rst->count++;
00224   /* printf("rst->count: %d\n",rst->count); */
00225 
00226   return MOLFILE_SUCCESS;
00227 }
00228     
00229 static void close_rst_read(void *mydata) {
00230   rstdata *rst= (rstdata *)mydata;
00231   fclose(rst->file);
00232   free(rst);
00233 }
00234 
00235 static void *open_rst_write(const char *path, const char *filetype, int natoms) {
00236   char title[82];
00237   rstdata *rst;
00238   FILE *fd;
00239   int len;
00240 
00241   fd = fopen(path, "wb");
00242   if (!fd) {
00243     vmdcon_printf(VMDCON_ERROR, "rst7plugin) Could not open file %s for writing\n", path);
00244     return NULL;
00245   }
00246   /* write out fixed length fortran style string */
00247   sprintf(title, "TITLE : Created by VMD with %d atoms",natoms);
00248   len = strlen(title);
00249   memset(title+len,(int)' ',82-len);
00250   title[80] = '\n';
00251   title[81] = '\0';
00252   fputs(title,fd);
00253 
00254   rst = (rstdata *)malloc(sizeof(rstdata));
00255   rst->file = fd;
00256   rst->numatoms = natoms;
00257   rst->has_box = 1;
00258   return rst;
00259 }
00260   
00261 static int write_rst_timestep(void *v, const molfile_timestep_t *ts) {
00262   rstdata *rst = (rstdata *)v;
00263   int i;
00264   const int ndata = rst->numatoms * 3;
00265 
00266 #if vmdplugin_ABIVERSION > 10
00267   if (ts->velocities != NULL) {
00268     fprintf(rst->file, "%6d %13.7g\n", rst->numatoms, ts->physical_time);
00269   } else
00270 #endif
00271     fprintf(rst->file, "%6d\n", rst->numatoms);
00272 
00273   for (i=0; i<ndata; i++) {
00274     fprintf(rst->file, "%12.7f", ts->coords[i]);
00275     if ((i+1) % 6 == 0) fprintf(rst->file, "\n"); 
00276   }
00277   if (ndata % 6 != 0) fprintf(rst->file,"\n");
00278 
00279 #if vmdplugin_ABIVERSION > 10
00280   if (ts->velocities != NULL) {
00281     for (i=0; i<ndata; i++) {
00282       fprintf(rst->file, "%12.7f", ts->velocities[i]);
00283       if ((i+1) % 6 == 0) fprintf(rst->file, "\n"); 
00284     }
00285     if (ndata % 6 != 0) fprintf(rst->file,"\n");
00286   }
00287 #endif
00288 
00289   fprintf (rst->file, "%12.7f%12.7f%12.7f%12.7f%12.7f%12.7f\n",
00290            ts->A, ts->B, ts->C, ts->alpha, ts->beta, ts->gamma);
00291 
00292   return MOLFILE_SUCCESS;
00293 }
00294 
00295 static void close_rst_write(void *v) {
00296   rstdata *rst = (rstdata *)v;
00297   fclose(rst->file);
00298   free(rst);
00299 }
00300 
00301 /* registration stuff */
00302 static molfile_plugin_t plugin;
00303 
00304 VMDPLUGIN_API int VMDPLUGIN_init(){
00305   memset(&plugin, 0, sizeof(molfile_plugin_t));
00306   plugin.abiversion = vmdplugin_ABIVERSION;
00307   plugin.type = MOLFILE_PLUGIN_TYPE;
00308   plugin.name = "rst7";
00309   plugin.prettyname = "AMBER7 Restart";
00310   plugin.author = "Brian Bennion, Axel Kohlmeyer, Josh Vermaas, John Stone";
00311   plugin.majorv = 0;
00312   plugin.minorv = 6;
00313   plugin.is_reentrant = VMDPLUGIN_THREADUNSAFE;
00314   plugin.filename_extension = "rst7";
00315   plugin.open_file_read = open_rst_read;
00316   plugin.read_next_timestep = read_rst_timestep;
00317 #if vmdplugin_ABIVERSION > 10
00318   plugin.read_timestep_metadata = read_timestep_metadata;
00319 #endif
00320   plugin.close_file_read = close_rst_read;
00321   plugin.open_file_write = open_rst_write;
00322   plugin.write_timestep = write_rst_timestep;
00323   plugin.close_file_write = close_rst_write;
00324   return VMDPLUGIN_SUCCESS;
00325 }
00326 
00327 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00328   (*cb)(v, (vmdplugin_t *)&plugin);
00329   return VMDPLUGIN_SUCCESS;
00330 }
00331 
00332 VMDPLUGIN_API int VMDPLUGIN_fini(){ return VMDPLUGIN_SUCCESS; }
00333 

Generated on Thu Mar 28 03:08:19 2024 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002