1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/* MACHINE GENERATED FILE, DO NOT EDIT! */
#define VMDPLUGIN molfile_namdbinplugin
#define STATIC_PLUGIN 1
/***************************************************************************
*cr
*cr (C) Copyright 1995-2016 The Board of Trustees of the
*cr University of Illinois
*cr All Rights Reserved
*cr
***************************************************************************/
/***************************************************************************
* RCS INFORMATION:
*
* $RCSfile: namdbinplugin.c,v $
* $Author: johns $ $Locker: $ $State: Exp $
* $Revision: 1.23 $ $Date: 2016/11/28 05:01:54 $
*
***************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "molfile_plugin.h"
#if INT_MAX == 2147483647
typedef int namdbin_int32;
#elif SHRT_MAX == 2147483647
typedef short namdbin_int32;
#elif LONG_MAX == 2147483647
typedef long namdbin_int32;
#endif
#define BLOCK 500
typedef struct {
double xyz[3*BLOCK];
FILE *fd;
int numatoms;
int wrongendian;
} namdbinhandle;
static void *open_namdbin_read(const char *path, const char *filetype,
int *natoms) {
namdbinhandle *namdbin;
FILE *fd;
int numatoms;
namdbin_int32 filen;
char lenbuf[4];
char tmpc;
namdbin = (namdbinhandle *)malloc(sizeof(namdbinhandle));
if (!namdbin) {
fprintf(stderr, "Unable to allocate space for read buffer.\n");
return NULL;
}
memset(namdbin, 0, sizeof(namdbinhandle));
fd = fopen(path, "rb");
if (!fd) {
fprintf(stderr, "Could not open file '%s' for reading.\n", path);
free(namdbin);
return NULL;
}
fseek(fd,0,SEEK_END);
numatoms = (ftell(fd)-4)/24;
if (numatoms < 1) {
fprintf(stderr, "File '%s' is too short.\n", path);
fclose(fd);
free(namdbin);
return NULL;
}
fseek(fd,0,SEEK_SET);
fread(&filen, sizeof(namdbin_int32), 1, fd);
if (filen != numatoms) {
namdbin->wrongendian = 1;
memcpy(lenbuf, (const char *)&filen, 4);
tmpc = lenbuf[0]; lenbuf[0] = lenbuf[3]; lenbuf[3] = tmpc;
tmpc = lenbuf[1]; lenbuf[1] = lenbuf[2]; lenbuf[2] = tmpc;
memcpy((char *)&filen, lenbuf, 4);
}
if (filen != numatoms) {
fprintf(stderr, "Inconsistent atom count in file '%s'.\n", path);
fclose(fd);
free(namdbin);
return NULL;
}
if ( namdbin->wrongendian ) {
fprintf(stderr, "File '%s' appears to be other-endian.\n", path);
}
namdbin->fd = fd;
namdbin->numatoms = numatoms;
*natoms = namdbin->numatoms;
return namdbin;
}
static int read_next_timestep(void *v, int natoms, molfile_timestep_t *ts) {
namdbinhandle *namdbin;
int i, numatoms;
char *cdata;
namdbin = (namdbinhandle *)v;
if (!namdbin->fd)
return MOLFILE_ERROR; /* Done reading frames */
numatoms = namdbin->numatoms;
for (i=0; i<namdbin->numatoms; i+=BLOCK) {
int j, n;
n = namdbin->numatoms - i;
if ( n > BLOCK ) n = BLOCK;
if (fread(namdbin->xyz, sizeof(double), 3*n, namdbin->fd) != (size_t)(3*n)) {
fprintf(stderr, "Failure reading data from NAMD binary file.\n");
return MOLFILE_ERROR;
}
if (namdbin->wrongendian) {
if ( ! i ) fprintf(stderr, "Converting other-endian data from NAMD binary file.\n");
cdata = (char *) namdbin->xyz;
for ( j=0; j<3*n; ++j, cdata+=8 ) {
char tmp0, tmp1, tmp2, tmp3;
tmp0 = cdata[0]; tmp1 = cdata[1];
tmp2 = cdata[2]; tmp3 = cdata[3];
cdata[0] = cdata[7]; cdata[1] = cdata[6];
cdata[2] = cdata[5]; cdata[3] = cdata[4];
cdata[7] = tmp0; cdata[6] = tmp1;
cdata[5] = tmp2; cdata[4] = tmp3;
}
}
if (ts) {
for (j=0; j<n; ++j) {
ts->coords[3L*(i+j) ] = namdbin->xyz[3*j ];
ts->coords[3L*(i+j)+1] = namdbin->xyz[3*j+1];
ts->coords[3L*(i+j)+2] = namdbin->xyz[3*j+2];
}
}
}
/*
* Close the file handle and set to NULL so we know we're done reading
*/
fclose(namdbin->fd);
namdbin->fd = NULL;
return MOLFILE_SUCCESS;
}
static void close_file_read(void *v) {
namdbinhandle *namdbin = (namdbinhandle *)v;
if (namdbin->fd)
fclose(namdbin->fd);
free(namdbin);
}
static void *open_namdbin_write(const char *path, const char *filetype,
int natoms) {
namdbinhandle *namdbin;
FILE *fd;
namdbin = (namdbinhandle *)malloc(sizeof(namdbinhandle));
if (!namdbin) {
fprintf(stderr, "Unable to allocate space for write buffer.\n");
return NULL;
}
fd = fopen(path, "wb");
if (!fd) {
fprintf(stderr, "Could not open file %s for writing\n", path);
free(namdbin);
return NULL;
}
namdbin->fd = fd;
namdbin->numatoms = natoms;
return namdbin;
}
static int write_timestep(void *v, const molfile_timestep_t *ts) {
int i;
namdbin_int32 myint;
namdbinhandle *namdbin = (namdbinhandle *)v;
if (!namdbin->fd)
return MOLFILE_ERROR;
myint = (namdbin_int32)namdbin->numatoms;
fwrite(&myint, 4, 1, namdbin->fd);
for (i=0; i<namdbin->numatoms; i+=BLOCK) {
double *tmp = namdbin->xyz;
int j, n;
n = namdbin->numatoms - i;
if ( n > BLOCK ) n = BLOCK;
for (j=0; j<n; ++j) {
tmp[3*j ] = ts->coords[3L*(i+j) ];
tmp[3*j+1] = ts->coords[3L*(i+j)+1];
tmp[3*j+2] = ts->coords[3L*(i+j)+2];
}
if (fwrite(tmp, sizeof(double), 3*n, namdbin->fd) != 3*n) {
fprintf(stderr, "Error writing namd binary file\n");
return MOLFILE_ERROR;
}
}
/*
* Close and NULLify the file handle so we don't write any more frames.
*/
fclose(namdbin->fd);
namdbin->fd = NULL;
return MOLFILE_SUCCESS;
}
static void close_file_write(void *v) {
namdbinhandle *namdbin = (namdbinhandle *)v;
if (namdbin->fd)
fclose(namdbin->fd);
free(namdbin);
}
/*
* Initialization stuff here
*/
static molfile_plugin_t plugin;
VMDPLUGIN_API int VMDPLUGIN_init() {
memset(&plugin, 0, sizeof(molfile_plugin_t));
plugin.abiversion = vmdplugin_ABIVERSION;
plugin.type = MOLFILE_PLUGIN_TYPE;
plugin.name = "namdbin";
plugin.prettyname = "NAMD Binary Coordinates";
plugin.author = "James Phillips, Justin Gullingsrud";
plugin.majorv = 0;
plugin.minorv = 2;
plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
plugin.filename_extension = "coor";
plugin.open_file_read = open_namdbin_read;
plugin.read_next_timestep = read_next_timestep;
plugin.close_file_read = close_file_read;
plugin.open_file_write = open_namdbin_write;
plugin.write_timestep = write_timestep;
plugin.close_file_write = close_file_write;
return VMDPLUGIN_SUCCESS;
}
VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
(*cb)(v, (vmdplugin_t *)&plugin);
return VMDPLUGIN_SUCCESS;
}
VMDPLUGIN_API int VMDPLUGIN_fini() {
return VMDPLUGIN_SUCCESS;
}
#ifdef TEST_NAMDBINPLUGIN
int main(int argc, char *argv[]) {
molfile_header_t header;
molfile_timestep_t timestep;
void *v;
int i;
while (--argc) {
++argv;
v = open_namdbin_read(*argv, &header);
if (!v) {
fprintf(stderr, "open_namdbin_read failed for file %s\n", *argv);
return 1;
}
timestep.coords = (float *)malloc(3*sizeof(float)*header.numatoms);
for (i=0; i<header.numsteps; i++) {
int rc = read_next_timestep(v, ×tep);
if (rc) {
fprintf(stderr, "error in read_next_timestep\n");
return 1;
}
}
close_file_read(v);
}
return 0;
}
#endif
|