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

crdplugin.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: crdplugin.c,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.40 $       $Date: 2016/11/28 05:01:53 $
00015  *
00016  ***************************************************************************/
00017 
00018 /*
00019  * TODO: This plugin should probably be merged with the 'rst7' plugin, since
00020  *       the differences between them are minor, and there's no logical reason
00021  *       for them to be implemented completely independently as they are now.
00022  *       The major differences in formatting are in regard to the 6F12.7 (rst7)
00023  *       versus 10F8.3 (crd) ascii floating point conversion modes. 
00024  */
00025 
00026 #include "largefiles.h"   /* platform dependent 64-bit file I/O defines */
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include "molfile_plugin.h"
00032 
00033 typedef struct {
00034   FILE *file;
00035   int has_box;
00036   int numatoms;
00037 } crddata;
00038  
00039 static void *open_crd_read(const char *filename, const char *filetype, 
00040     int *natoms) {
00041  
00042   FILE *fd;
00043   crddata *data;
00044  
00045   fd = fopen(filename, "rb");
00046   if (!fd) return NULL;
00047   
00048   /* first line is title, so skip past it */
00049   while (getc(fd) != '\n');
00050 
00051   /* 
00052    * CRD's don't store the number of atoms in the timestep, so we assume that
00053    * the application will determine this for us.  
00054    */
00055   data = (crddata *)malloc(sizeof(crddata));
00056   data->file = fd;
00057   *natoms = MOLFILE_NUMATOMS_UNKNOWN;
00058   /* filetype "crd" has no box; filetype "crdbox" does. */
00059   data->has_box = strcmp(filetype, "crd"); 
00060   return data;
00061 }
00062 
00063 /*
00064  * CRD files with box info are indistinguishable from regular CRD's.  
00065  * We regard CRD's with box info as a different file format.
00066  * CRD's don't tell how many atoms there are in each frame.  We therefore
00067  * rely on the numatoms field in the molfile_timestep_t parameter.
00068  */
00069 static int read_crd_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00070   crddata *crd = (crddata *)mydata;
00071   int i, j;
00072   float x, y, z;
00073   float a, b, c;
00074 
00075   /* Read in the atom coordinates */
00076   for (i=0; i<natoms; i++) {
00077     j = fscanf(crd->file, "%f %f %f", &x, &y, &z);
00078     if (j == EOF) {
00079       return MOLFILE_ERROR;
00080     } else if (j <= 0) {
00081       fprintf(stderr, "Problem reading CRD file\n");
00082       return MOLFILE_ERROR;
00083     }
00084 
00085     /* only save coords if we're given a valid ts pointer */ 
00086     /* otherwise assume that VMD wants us to skip it.     */
00087     if (ts != NULL) {
00088       ts->coords[3*i  ] = x;
00089       ts->coords[3*i+1] = y;
00090       ts->coords[3*i+2] = z;
00091     }
00092   }
00093 
00094 
00095   /* Read the PBC box info. */
00096   if (crd->has_box) {
00097     j = fscanf(crd->file, "%f %f %f", &a, &b, &c);
00098     if (j == EOF) {
00099       printf("EOF in box\n");
00100       return MOLFILE_ERROR;
00101     } else if (j <= 0) {
00102       printf("Problem reading box part of CRD file, scanf returned %d\n",j);
00103       return MOLFILE_ERROR;
00104     }
00105 
00106     /* only save coords if we're given a valid ts pointer */ 
00107     /* otherwise assume that VMD wants us to skip it.     */
00108     if (ts != NULL) {
00109       ts->A = a;
00110       ts->B = b;
00111       ts->C = c;
00112 
00113       /* XXX periodic cell angles are only stored in the PARM file */
00114       /* we should probably retrieve these from the already-loaded */
00115       /* molecule when possible.                                   */
00116       ts->alpha = 90.0;
00117       ts->beta  = 90.0;
00118       ts->gamma = 90.0;
00119     }
00120   }
00121 
00122   return MOLFILE_SUCCESS;
00123 }
00124     
00125 static void close_crd_read(void *mydata) {
00126   crddata *crd = (crddata *)mydata;
00127   fclose(crd->file);
00128   free(crd);
00129 }
00130 
00131 static void *open_crd_write(const char *path, const char *filetype,
00132     int natoms) {
00133   crddata *crd;
00134   FILE *fd;
00135 
00136   fd = fopen(path, "wb");
00137   if (!fd) {
00138     fprintf(stderr, "Could not open file %s for writing\n", path);
00139     return NULL;
00140   }
00141   fprintf(fd, "TITLE : Created by VMD with %d atoms\n", natoms);
00142   
00143   crd = (crddata *)malloc(sizeof(crddata));
00144   crd->file = fd;
00145   crd->numatoms = natoms;
00146   crd->has_box = strcmp(filetype, "crd"); 
00147   return crd;
00148 }    
00149   
00150 static int write_crd_timestep(void *v, const molfile_timestep_t *ts) {
00151   crddata *crd = (crddata *)v;
00152   int i;
00153   int lfdone=0;
00154   const int ndata = crd->numatoms * 3;
00155 
00156   for (i=0; i<ndata; i++) {
00157     lfdone = 0;
00158     fprintf(crd->file, "%8.3f", ts->coords[i]);
00159     if ((i+1) % 10 == 0) {
00160       fprintf(crd->file, "\n"); 
00161       lfdone = 1;
00162     }
00163   }
00164   if (!lfdone)
00165     fprintf(crd->file, "\n"); 
00166     
00167   if (crd->has_box) {
00168     fprintf (crd->file, "%8.3f%8.3f%8.3f\n", ts->A, ts->B, ts->C);
00169   }
00170 
00171   return MOLFILE_SUCCESS;
00172 }
00173 
00174 static void close_crd_write(void *v) {
00175   crddata *crd = (crddata *)v;
00176   fclose(crd->file);
00177   free(crd);
00178 }
00179 
00180 /* registration stuff */
00181     
00182 static molfile_plugin_t plugin;
00183 static molfile_plugin_t crdboxplugin;
00184 
00185 VMDPLUGIN_API int VMDPLUGIN_init(void) { 
00186   memset(&plugin, 0, sizeof(molfile_plugin_t));
00187   plugin.abiversion = vmdplugin_ABIVERSION;
00188   plugin.type = MOLFILE_PLUGIN_TYPE;
00189   plugin.name = "crd";
00190   plugin.prettyname = "AMBER Coordinates";
00191   plugin.author = "Justin Gullingsrud, John Stone";
00192   plugin.majorv = 0;
00193   plugin.minorv = 9;
00194   plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00195   plugin.filename_extension = "mdcrd,crd";
00196   plugin.open_file_read = open_crd_read;
00197   plugin.read_next_timestep = read_crd_timestep;
00198   plugin.close_file_read = close_crd_read;
00199   plugin.open_file_write = open_crd_write;
00200   plugin.write_timestep = write_crd_timestep;
00201   plugin.close_file_write = close_crd_write;
00202 
00203   memcpy(&crdboxplugin, &plugin, sizeof(molfile_plugin_t));
00204   crdboxplugin.name = "crdbox";
00205   crdboxplugin.prettyname = "AMBER Coordinates with Periodic Box";
00206 
00207   return VMDPLUGIN_SUCCESS; 
00208 }
00209 
00210 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00211   (*cb)(v, (vmdplugin_t *)&plugin);
00212   (*cb)(v, (vmdplugin_t *)&crdboxplugin);
00213   return VMDPLUGIN_SUCCESS;
00214 }
00215 
00216 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00217 

Generated on Thu Apr 18 03:09:35 2024 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002