00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00041
00042
00043
00044
00045
00046 fd = fopen(filename, "rb");
00047 if (!fd)
00048 return NULL;
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
00058 field = strtok(line, " ");
00059 if (field==NULL) {
00060 continue;
00061 }
00062 numats = atoi(field);
00063
00064
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
00116 if(rst->count==1 && rst->rstfile==1)
00117 return MOLFILE_ERROR;
00118
00119 for (i=0; i<rst->numatoms; i++) {
00120
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
00134
00135
00136
00137 rst->count++;
00138
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
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
00190 static molfile_plugin_t rst7plugin = {
00191 vmdplugin_ABIVERSION,
00192 MOLFILE_PLUGIN_TYPE,
00193 "rst7",
00194 "AMBER7 Restart",
00195 "Brian Bennion",
00196 0,
00197 2,
00198 VMDPLUGIN_THREADUNSAFE,
00199 "rst7",
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 }