Menu

[760515]: / vmdfio.c  Maximize  Restore  History

Download this file

397 lines (330 with data), 10.2 kB

  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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* ERmod - Eneregy Representation Module
Copyright (C) 2000-2016 Nobuyuki Matubayasi
Copyright (C) 2010-2016 Shun Sakuraba
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
As a special exception, you may use this file as part of a free software
without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*
Generic trajectory access via VMD plugin.
*/
/* FIXME: add F77_FUNC(small,CAPITAL) issues */
#include "config.h"
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <math.h>
#include <dlfcn.h>
#include "vmdplugins/molfile_plugin.h"
/*
Declaration of plugin access points.
*/
#ifndef INSTALL_PLUGIN_PATH
#warning INSTALL_PLUGIN_PATH is not defined
#define INSTALL_PLUGIN_PATH ""
#endif
#define ADHOC_RESOLVER "adhocresolve.list"
#define ERMOD_FORCE_PLUGIN "ERMOD_FORCE_PLUGIN_TYPE"
/* lazy */
#define MAXTYPES 1000
static int typecounts = 0;
static molfile_plugin_t* vmdpluginentries[MAXTYPES];
/* lazy too */
#define MAXPLUGINS 100
static int plugincounts = 0;
static void* ldhandles[MAXPLUGINS];
/*
Callback function. Must be compatible with vmdplugin_register_cb.
*/
static int register_callback(void* __vp, vmdplugin_t * entity)
{
if(entity == NULL || entity -> type == NULL) return 0;
vmdpluginentries[typecounts++] = (molfile_plugin_t*)entity;
return 0;
}
static int check_directory_available(char *path)
{
struct stat st;
int r = stat(path, &st);
if(r == 0 && S_ISDIR(st.st_mode))
return 1;
return 0;
}
static int is_executable(char* path)
{
struct stat st;
int r;
r = stat(path, &st);
if(r != 0) return 0;
if(!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
return 0;
}
if(access(path, X_OK) == 0) return 1;
return 0;
}
static char* strdupcat(char* str1, char* str2)
{
char* buf = malloc(strlen(str1) + strlen(str2) + 1);
strcpy(buf, str1);
strcat(buf, str2);
return buf;
}
static char* find_plugin_paths()
{
char *pluginpath;
/* contract: return malloc'ed string, or return NULL if error */
/* is ERMOD_PLUGINS present and directory? */
if((pluginpath = getenv("ERMOD_PLUGINS")) &&
check_directory_available(pluginpath)) {
return strdup(pluginpath);
}
/* is INSTALL_PLUGIN_PATH available? */
if(strlen(INSTALL_PLUGIN_PATH) > 0 &&
check_directory_available(INSTALL_PLUGIN_PATH)) {
return strdup(INSTALL_PLUGIN_PATH);
}
return NULL;
}
void vmdfio_init_traj_(void)
{
char* plugindir;
char* buf;
if((plugindir = find_plugin_paths()) == NULL){
fprintf(stderr, "Error: cannot find plugin paths, please set ERMOD_PLUGINS environment variable\n");
exit(EXIT_FAILURE);
}
/* traverse directory */
do{
DIR* dirp;
struct dirent *ent;
dirp = opendir(plugindir);
while((ent = readdir(dirp)) != NULL){
/* concat paths */
char* d1 = strdupcat(plugindir, "/");
char* pluginpath = strdupcat(d1, ent -> d_name);
free(d1);
if(is_executable(pluginpath) && pluginpath[strlen(pluginpath)-1] != '.'){
void* handle;
handle = dlopen(pluginpath, RTLD_NOW | RTLD_GLOBAL);
if(!handle){
fprintf(stderr, "Warning: failed to load plugin \"%s\", reason: \"%s\"\n", pluginpath, dlerror());
continue;
}
void* initptr;
void* entptr;
initptr = dlsym(handle, "vmdplugin_init");
entptr = dlsym(handle, "vmdplugin_register");
if(initptr != NULL && entptr != NULL){
int r;
r = ((int (*)(void))initptr)();
if(r != 0)
fprintf(stderr, "Warning: error while initializing %s\n", pluginpath);
ldhandles[plugincounts++] = handle;
r = ((int (*)(void*, vmdplugin_register_cb))entptr)(NULL, register_callback);
if(r != 0)
fprintf(stderr, "Warning: error while registering %s\n", pluginpath);
}else{
fprintf(stderr, "Warning: failed to load entry point (plugin: %s)\n", pluginpath);
}
}
}
}while(0);
}
void vmdfio_fini_traj_(void)
{
int i;
for(i = 0; i < plugincounts; ++i){
void* entptr = dlsym(ldhandles[i], "vmdplugin_fini");
if(entptr){
((int (*)(void))entptr)();
}
dlclose(ldhandles[i]);
}
}
typedef struct vmdpluginio_t {
molfile_plugin_t * plugin;
void* filehandle;
int natoms;
} vmdpluginio;
void vmdfio_open_traj_(void **handle, char *fname, int *fnamelen, int *status)
{
char* buf;
size_t buflen = 8192;
ssize_t r;
int fp;
char* ext;
int i;
buf = malloc(sizeof(char) * buflen + 1);
strncpy(buf, fname, *fnamelen);
buf[*fnamelen] = '\0';
while(1){
r = readlink(buf, buf, buflen);
if(r == -1) break;
buf[r] = '\0';
}
if(*fnamelen == strlen(buf) && strncmp(fname, buf, *fnamelen) == 0){
/* not a symbolic link? */
fprintf(stderr, "Error: vmdfio.c: failed to open with vmdfio_open_traj_. (filename = \"%s\".) Perhaps it's not a symbolic link?\n", buf);
*status = -1;
goto cleanup;
}
/*
select plugin to use.
use filename_extension to select
*/
*status = -1;
*handle = NULL;
{
/* get filename extension */
char* lastdot = strrchr(buf, '.');
if(lastdot == NULL) goto cleanup;
ext = lastdot + 1;
}
fprintf(stderr, "Opening: \"%s\"...\n", buf);
if(getenv(ERMOD_FORCE_PLUGIN) != NULL &&
strlen(getenv(ERMOD_FORCE_PLUGIN)) > 0) {
ext = getenv(ERMOD_FORCE_PLUGIN);
}
for(i = 0; i < typecounts; ++i){
int extlen = strlen(ext);
molfile_plugin_t *p = vmdpluginentries[i];
char* plugin_supportext;
char* tokptr;
char* ptr;
if(strcmp(p -> type, MOLFILE_PLUGIN_TYPE) != 0) continue;
plugin_supportext = strdup(p -> filename_extension);
while(1){
ptr = strtok_r(plugin_supportext, ",", &tokptr);
if(ptr == NULL) break;
plugin_supportext = NULL; /* in the next subsequent call NULL is passed */
if(strncmp(ptr, ext, extlen) == 0 &&
(ptr[extlen] == '\0' || ptr[extlen] == ',')){
void* fh;
vmdpluginio* pp;
fprintf(stderr, " Trying plugin \"%s\"...", p -> prettyname);
if(p -> abiversion <= 10) {
fprintf(stderr, "Error: \"%s\" supports trajectory format, but it is too old (requires ABI version > 10)\n", p -> prettyname);
fprintf(stderr, "Please update plugin files to those of the latest VMD's\n");
continue;
}
pp = malloc(sizeof(vmdpluginio));
pp -> plugin = p;
/* Found, open with this plugin */
pp -> natoms = MOLFILE_NUMATOMS_UNKNOWN;
fh = (p -> open_file_read)(buf, ext, &(pp->natoms));
if(fh == NULL){
fprintf(stderr,
"Error!\n"
"Plugin supports file format \"%s\", estimated from the extension \"%s\".\n"
"But the file format of \"%s\" does not match to the estimated format.\n"
"This is possibly that the file has an incorrect extension, the file is a nested symbolic link (which is unsupported), or the trajectory file header is corrupt.\n",
p -> prettyname, ext, buf);
/* Special error message for AMBER */
if(strcmp(ext, "nc") == 0) {
fprintf(stderr,
"Perhaps you forgot to specify \"ioutfm = 1\" in AMBER?\n");
}
exit(1);
}
pp -> filehandle = fh;
fprintf(stderr, "OK\n");
*handle = pp;
break;
}
}
free(plugin_supportext);
if(*handle){
*status = 0;
break;
}
}
if(*status == -1){
fprintf(stderr, "Error: could not find appropriate plugins\n");
}
cleanup:
free(buf);
return;
}
void vmdfio_read_traj_step_(void **handle, double* xout, double* box, int *natoms_aux, int *status)
{
vmdpluginio *p = *handle;
molfile_plugin_t *plugin = p -> plugin;
molfile_timestep_t snapshot;
int natoms = p -> natoms;
float* buf;
int r;
if(natoms == MOLFILE_NUMATOMS_UNKNOWN){
/* not determined from the trajectory */
natoms = *natoms_aux;
}else{
/* check integrity */
if(natoms != *natoms_aux){
fprintf(stderr, "Error: # of atoms in trajectory does not match with # of atoms in configurations. Perhaps you mistook the trajectory?\n");
*status = -1;
return;
}
}
buf = malloc(sizeof(float) * 3 * natoms);
snapshot.coords = buf;
r = (plugin -> read_next_timestep)(p -> filehandle, natoms, &snapshot);
do{
int i;
double x, y, u, v, w;
if(r == MOLFILE_EOF){
*status = -1;
break;
}
/* both are the same format */
for(i = 0; i < natoms * 3; ++i)
xout[i] = buf[i];
/*
~a = (1, 0, 0)
~b = (x, y, 0)
~c = (u, v, w)
~a.~b = x = cos gamma
|~a*~b| = y = sin gamma
~a.~c = u = cos beta
~b.~c = xu + yv = cos alpha
*/
x = cos(snapshot.gamma * M_PI / 180.0);
y = sin(snapshot.gamma * M_PI / 180.0);
u = cos(snapshot.beta * M_PI / 180.0);
v = (cos(snapshot.alpha * M_PI / 180.0) - x * u) / y; /* FIXME: potential underflow risk */
w = sqrt(1 - u * u - v * v); /* FIXME: same above */
box[0] = snapshot.A;
box[3] = snapshot.B * x;
box[4] = snapshot.B * y;
box[6] = snapshot.C * u;
box[7] = snapshot.C * v;
box[8] = snapshot.C * w;
box[1] = 0.; box[2] = 0.; box[5] = 0;
*status = 0;
}while(0);
free(buf);
return;
}
void vmdfio_close_traj_(void **handle)
{
vmdpluginio *p = *handle;
molfile_plugin_t *plugin = p -> plugin;
(plugin -> close_file_read)(p -> filehandle);
free(*handle);
}