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 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
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