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
00027
00028
00029 #include <stdlib.h>
00030 #include <stdio.h>
00031 #include <ctype.h>
00032 #include <math.h>
00033 #include <string.h>
00034
00035 #if defined(_AIX)
00036 #include <strings.h>
00037 #endif
00038
00039 #if defined(WIN32) || defined(WIN64)
00040 #define strcasecmp stricmp
00041 #define strncasecmp strnicmp
00042 #endif
00043
00044 #include "molfile_plugin.h"
00045
00046 #define LINESIZE 85
00047
00048 typedef struct {
00049 FILE *fd;
00050 int nsets;
00051 int ndata;
00052 molfile_volumetric_t *vol;
00053 } phi_t;
00054
00055
00056
00057 static char *phigets(char *s, int n, FILE *stream) {
00058 char *returnVal;
00059
00060 if (feof(stream)) {
00061 fprintf(stderr, "phiplugin) Unexpected end-of-file.\n");
00062 returnVal = NULL;
00063 }
00064 else if (ferror(stream)) {
00065 fprintf(stderr, "phiplugin) Error reading file.\n");
00066 return NULL;
00067 }
00068 else {
00069 returnVal = fgets(s, n, stream);
00070 if (returnVal == NULL) {
00071 fprintf(stderr, "phiplugin) Error reading line.\n");
00072 }
00073 }
00074
00075 return returnVal;
00076 }
00077
00078
00079 static void *open_phi_read(const char *filepath, const char *filetype,
00080 int *natoms) {
00081 FILE *fd;
00082 phi_t *phi;
00083 char inbuf[LINESIZE];
00084
00085 float scale, midX, midY, midZ;
00086 float cellSize, iGrid = 0.0;
00087
00088 fd = fopen(filepath, "rb");
00089 if (!fd) {
00090 fprintf(stderr, "phiplugin) Error opening file.\n");
00091 return NULL;
00092 }
00093
00094
00095 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00096 return NULL;
00097 }
00098 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00099 return NULL;
00100 }
00101 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00102 return NULL;
00103 }
00104
00105
00106 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00107 return NULL;
00108 }
00109 while (strncasecmp(inbuf, " end of phimap", 14) != 0) {
00110
00111
00112 iGrid += strlen(inbuf) / 4;
00113
00114 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00115 return NULL;
00116 }
00117 }
00118
00119
00120
00121
00122 cellSize = pow((double) iGrid, (double) 1.0/3.0);
00123 if (fabs((double)(cellSize - floor(cellSize))) > 1e-8) {
00124 return NULL;
00125 }
00126
00127
00128 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00129 return NULL;
00130 }
00131 sscanf(inbuf, " %f %f %f %f", &scale, &midX, &midY, &midZ);
00132
00133
00134 phi = new phi_t;
00135 phi->fd = fd;
00136 phi->vol = NULL;
00137 phi->ndata = (int) iGrid;
00138 *natoms = MOLFILE_NUMATOMS_NONE;
00139 phi->nsets = 1;
00140
00141 phi->vol = new molfile_volumetric_t[1];
00142 strcpy(phi->vol[0].dataname, "PHIMAP Electron Density Map");
00143
00144
00145 phi->vol[0].origin[0] = -0.5*(cellSize+1.0) / scale + midX;
00146 phi->vol[0].origin[1] = -0.5*(cellSize+1.0) / scale + midY;
00147 phi->vol[0].origin[2] = -0.5*(cellSize+1.0) / scale + midZ;
00148
00149 phi->vol[0].xaxis[0] = cellSize / scale;
00150 phi->vol[0].xaxis[1] = 0;
00151 phi->vol[0].xaxis[2] = 0;
00152
00153 phi->vol[0].yaxis[0] = 0;
00154 phi->vol[0].yaxis[1] = cellSize / scale;
00155 phi->vol[0].yaxis[2] = 0;
00156
00157 phi->vol[0].zaxis[0] = 0;
00158 phi->vol[0].zaxis[1] = 0;
00159 phi->vol[0].zaxis[2] = cellSize / scale;
00160
00161 phi->vol[0].xsize = (int) cellSize;
00162 phi->vol[0].ysize = (int) cellSize;
00163 phi->vol[0].zsize = (int) cellSize;
00164
00165 phi->vol[0].has_color = 0;
00166
00167 return phi;
00168 }
00169
00170 static int read_phi_metadata(void *v, int *nsets,
00171 molfile_volumetric_t **metadata) {
00172 phi_t *phi = (phi_t *)v;
00173 *nsets = phi->nsets;
00174 *metadata = phi->vol;
00175
00176 return MOLFILE_SUCCESS;
00177 }
00178
00179 static int read_phi_data(void *v, int set, float *datablock,
00180 float *colorblock) {
00181 phi_t *phi = (phi_t *)v;
00182 float *cellIndex;
00183 int value, ndata, count = 0;
00184 FILE *fd = phi->fd;
00185 char inbuf[LINESIZE], currNum[5], *currChar;
00186
00187 cellIndex = datablock;
00188 ndata = phi->ndata;
00189 memset(currNum, 0, 5);
00190
00191
00192 rewind(fd);
00193 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00194 return MOLFILE_ERROR;
00195 }
00196 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00197 return MOLFILE_ERROR;
00198 }
00199 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00200 return MOLFILE_ERROR;
00201 }
00202
00203
00204 while (count < ndata) {
00205 if (phigets(inbuf, LINESIZE, fd) == NULL) {
00206 return MOLFILE_ERROR;
00207 }
00208
00209 for (currChar = inbuf; (*currChar != '\n') && (*currChar != '\0');
00210 currChar += 4) {
00211 strncpy(currNum, currChar, 4);
00212 value = atoi(currNum);
00213
00214 *cellIndex = 0.01 * (value - 5000);
00215 cellIndex++;
00216 count++;
00217 }
00218 }
00219
00220 return MOLFILE_SUCCESS;
00221 }
00222
00223 static void close_phi_read(void *v) {
00224 phi_t *phi = (phi_t *)v;
00225
00226 fclose(phi->fd);
00227 if (phi->vol != NULL)
00228 delete [] phi->vol;
00229 delete phi;
00230 }
00231
00232
00233
00234
00235 static molfile_plugin_t plugin;
00236
00237 VMDPLUGIN_API int VMDPLUGIN_init(void) {
00238 memset(&plugin, 0, sizeof(molfile_plugin_t));
00239 plugin.abiversion = vmdplugin_ABIVERSION;
00240 plugin.type = MOLFILE_PLUGIN_TYPE;
00241 plugin.name = "delphibig";
00242 plugin.prettyname = "Delphi 'Big' Formatted Potential Map";
00243 plugin.author = "Eamon Caddigan";
00244 plugin.majorv = 0;
00245 plugin.minorv = 7;
00246 plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00247 plugin.filename_extension = "big";
00248 plugin.open_file_read = open_phi_read;
00249 plugin.read_volumetric_metadata = read_phi_metadata;
00250 plugin.read_volumetric_data = read_phi_data;
00251 plugin.close_file_read = close_phi_read;
00252 return VMDPLUGIN_SUCCESS;
00253 }
00254
00255 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00256 (*cb)(v, (vmdplugin_t *)&plugin);
00257 return VMDPLUGIN_SUCCESS;
00258 }
00259
00260 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00261