00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "largefiles.h"
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
00049 while (getc(fd) != '\n');
00050
00051
00052
00053
00054
00055 data = (crddata *)malloc(sizeof(crddata));
00056 data->file = fd;
00057 *natoms = MOLFILE_NUMATOMS_UNKNOWN;
00058
00059 data->has_box = strcmp(filetype, "crd");
00060 return data;
00061 }
00062
00063
00064
00065
00066
00067
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
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
00086
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
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
00107
00108 if (ts != NULL) {
00109 ts->A = a;
00110 ts->B = b;
00111 ts->C = c;
00112
00113
00114
00115
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 const int ndata = crd->numatoms * 3;
00154 for (i=0; i<ndata; i++) {
00155 fprintf(crd->file, "%8.3f", ts->coords[i]);
00156 if ((i+1) % 10 == 0) fprintf(crd->file, "\n");
00157 }
00158 if (crd->has_box) {
00159 fprintf (crd->file, "\n%8.3f %8.3f %8.3f\n", ts->A, ts->B, ts->C);
00160 }
00161
00162 return MOLFILE_SUCCESS;
00163 }
00164
00165 static void close_crd_write(void *v) {
00166 crddata *crd = (crddata *)v;
00167 fclose(crd->file);
00168 free(crd);
00169 }
00170
00171
00172
00173 static molfile_plugin_t crdplugin = {
00174 vmdplugin_ABIVERSION,
00175 MOLFILE_PLUGIN_TYPE,
00176 "crd",
00177 "AMBER Coordinates",
00178 "Justin Gullingsrud, John E. Stone",
00179 0,
00180 5,
00181 VMDPLUGIN_THREADSAFE,
00182 "mdcrd,crd",
00183 open_crd_read,
00184 0,
00185 0,
00186 read_crd_timestep,
00187 close_crd_read,
00188 open_crd_write,
00189 0,
00190 write_crd_timestep,
00191 close_crd_write
00192 };
00193
00194 static molfile_plugin_t crdboxplugin = {
00195 vmdplugin_ABIVERSION,
00196 MOLFILE_PLUGIN_TYPE,
00197 "crdbox",
00198 "AMBER Coordinates with Periodic Box",
00199 "Justin Gullingsrud, John E. Stone",
00200 0,
00201 4,
00202 VMDPLUGIN_THREADSAFE,
00203 "mdcrd,crd",
00204 open_crd_read,
00205 0,
00206 0,
00207 read_crd_timestep,
00208 close_crd_read,
00209 open_crd_write,
00210 0,
00211 write_crd_timestep,
00212 close_crd_write
00213 };
00214
00215 VMDPLUGIN_API int VMDPLUGIN_init(void) { return VMDPLUGIN_SUCCESS; }
00216 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00217 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00218 (*cb)(v, (vmdplugin_t *)&crdplugin);
00219 (*cb)(v, (vmdplugin_t *)&crdboxplugin);
00220 return VMDPLUGIN_SUCCESS;
00221 }
00222