1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This file is part of Gromacs Copyright (c) 1991-2008
5 * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * To help us fund GROMACS development, we humbly ask that you cite
13 * the research papers on the package. Check out http://www.gromacs.org
16 * Gnomes, ROck Monsters And Chili Sauce
24 /* Derived from PluginMgr.C and catdcd.c */
26 /* PluginMgr.C: Copyright: */
27 /***************************************************************************
29 *cr (C) Copyright 1995-2009 The Board of Trustees of the
30 *cr University of Illinois
31 *cr All Rights Reserved
33 Developed by: Theoretical and Computational Biophysics Group
34 University of Illinois at Urbana-Champaign
35 http://www.ks.uiuc.edu/
37 Permission is hereby granted, free of charge, to any person obtaining a copy of
38 this software and associated documentation files (the Software), to deal with
39 the Software without restriction, including without limitation the rights to
40 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
41 of the Software, and to permit persons to whom the Software is furnished to
42 do so, subject to the following conditions:
44 Redistributions of source code must retain the above copyright notice,
45 this list of conditions and the following disclaimers.
47 Redistributions in binary form must reproduce the above copyright notice,
48 this list of conditions and the following disclaimers in the documentation
49 and/or other materials provided with the distribution.
51 Neither the names of Theoretical and Computational Biophysics Group,
52 University of Illinois at Urbana-Champaign, nor the names of its contributors
53 may be used to endorse or promote products derived from this Software without
54 specific prior written permission.
56 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
59 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
60 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
61 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
62 OTHER DEALINGS WITH THE SOFTWARE.
63 ***************************************************************************/
65 /* catdcd.c: Copyright: */
66 /*****************************************************************************/
68 /* (C) Copyright 2001-2005 Justin Gullingsrud and the University of Illinois.*/
70 /*****************************************************************************/
77 * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
79 #include "molfile_plugin.h"
80 #include "vmddlopen.h"
81 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
92 #include "types/simple.h"
97 typedef int (*initfunc)(void);
98 typedef int (*regfunc)(void *, vmdplugin_register_cb);
99 typedef int (*finifunc)(void);
103 static int register_cb(void *v, vmdplugin_t *p) {
104 const char *key = p->name;
105 t_gmxvmdplugin *vmdplugin = (t_gmxvmdplugin*)v;
107 if (strcmp(key,vmdplugin->filetype)==0)
109 vmdplugin->api = (molfile_plugin_t *)p;
111 return VMDPLUGIN_SUCCESS;
114 static int load_sharedlibrary_plugins(const char *fullpath,t_gmxvmdplugin* vmdplugin) {
115 /* Open the dll; try to execute the init function. */
116 void *handle, *ifunc, *registerfunc;
117 handle = vmddlopen(fullpath);
119 if (debug) fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n", fullpath, vmddlerror()); /*only to debug because of stdc++ erros */
123 ifunc = vmddlsym(handle, "vmdplugin_init");
124 if (ifunc && ((initfunc)(ifunc))()) {
125 printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
130 registerfunc = vmddlsym(handle, "vmdplugin_register");
132 printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
136 /* Load plugins from the library.*/
137 ((regfunc)registerfunc)(vmdplugin, register_cb);
140 /* in case this library does not support the filetype, close it */
141 if (vmdplugin->api == NULL)
149 /*return: 1: success, 0: last frame, -1: error*/
150 bool read_next_vmd_frame(int status,t_trxframe *fr)
154 molfile_timestep_t ts;
157 fr->bV = fr->vmdplugin.bV;
160 snew(ts.coords, fr->natoms*3);
163 snew(ts.velocities, fr->natoms*3);
166 ts.coords = (float*)fr->x;
169 ts.velocities = (float*)fr->v;
173 rc = fr->vmdplugin.api->read_next_timestep(fr->vmdplugin.handle, fr->natoms, &ts);
176 fprintf(stderr, "\nError reading input file (error code %d)\n", rc);
180 fr->vmdplugin.api->close_file_read(fr->vmdplugin.handle);
185 for (i=0;i<fr->natoms;i++)
187 fr->x[i][0] = .1*ts.coords[i*3];
188 fr->x[i][1] = .1*ts.coords[i*3+1];
189 fr->x[i][2] = .1*ts.coords[i*3+2];
192 fr->v[i][0] = .1*ts.velocities[i*3];
193 fr->v[i][1] = .1*ts.velocities[i*3+1];
194 fr->v[i][2] = .1*ts.velocities[i*3+2];
200 sfree(ts.velocities);
203 for (i=0;i<fr->natoms;i++)
205 svmul(.1,fr->x[i],fr->x[i]);
208 svmul(.1,fr->v[i],fr->v[i]);
214 vec[0] = .1*ts.A; vec[1] = .1*ts.B; vec[2] = .1*ts.B;
215 angle[0] = ts.alpha; angle[1] = ts.beta; angle[2] = ts.gamma;
216 matrix_convert(fr->box,vec,angle);
218 fr->time = ts.physical_time;
225 int load_vmd_library(const char *fn, t_gmxvmdplugin *vmdplugin)
227 char pathname[GMX_PATH_MAX],filename[GMX_PATH_MAX];
232 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
234 const char *defpathenv = "/usr/local/lib/vmd/plugins/*/molfile";
237 HANDLE hFind = INVALID_HANDLE_VALUE;
238 char progfolder[GMX_PATH_MAX];
239 char defpathenv[GMX_PATH_MAX];
240 SHGetFolderPath(NULL,CSIDL_PROGRAM_FILES,NULL,SHGFP_TYPE_CURRENT,progfolder);
241 sprintf(defpathenv,"%s\\University of Illinois\\VMD\\plugins\\WIN32\\molfile",progfolder);
244 vmdplugin->api = NULL;
245 vmdplugin->filetype = strrchr(fn,'.');
246 if (!vmdplugin->filetype)
250 vmdplugin->filetype++;
252 pathenv = getenv("VMD_PLUGIN_PATH");
255 printf("\nVMD_PLUGIN_PATH not set. ");
256 printf("Using default location:\n%s\n",defpathenv);
259 strncpy(pathname,pathenv,sizeof(pathname));
260 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
261 strcat(pathname,"/*.so");
262 glob(pathname, 0, NULL, &globbuf);
263 if (globbuf.gl_pathc == 0)
265 printf("\nNo VMD Plugins found\n"
266 "Set the environment variable VMD_PLUGIN_PATH to the molfile folder within the\n"
267 "VMD installation.\n"
268 "The architecture (e.g. 32bit versus 64bit) of Gromacs and VMD has to match.\n");
271 for (i=0; i<globbuf.gl_pathc && vmdplugin->api == NULL; i++)
273 ret|=load_sharedlibrary_plugins(globbuf.gl_pathv[i],vmdplugin);
277 strcat(pathname,"\\*.so");
278 hFind = FindFirstFile(pathname, &ffd);
279 if (INVALID_HANDLE_VALUE == hFind)
281 printf("\nNo VMD Plugins found\n");
286 sprintf(filename,"%s\\%s",pathenv,ffd.cFileName);
287 ret|=load_sharedlibrary_plugins(filename,vmdplugin);
289 while (FindNextFile(hFind, &ffd ) != 0 && vmdplugin->api == NULL );
295 printf("\nCould not open any VMD library.\n");
299 printf("Compiled with dlopen?\n");
303 printf("Last error:\n%s\n",err);
308 if (vmdplugin->api == NULL)
310 printf("\nNo plugin for %s found\n",vmdplugin->filetype);
314 printf("\nUsing VMD plugin: %s (%s)\n",vmdplugin->api->name,vmdplugin->api->prettyname);
320 int read_first_vmd_frame(int *status,const char *fn,t_trxframe *fr,int flags)
322 molfile_timestep_metadata_t *metadata=NULL;
324 if (!load_vmd_library(fn,&(fr->vmdplugin)))
329 fr->vmdplugin.handle = fr->vmdplugin.api->open_file_read(fn, fr->vmdplugin.filetype, &fr->natoms);
331 if (!fr->vmdplugin.handle) {
332 fprintf(stderr, "\nError: could not open file '%s' for reading.\n",
337 if (fr->natoms < 1) {
338 fprintf(stderr, "\nNo atoms found by VMD plugin in %s.\n"
339 "Or format does not record number of atoms.\n", fn );
343 snew(fr->x,fr->natoms);
345 fr->vmdplugin.bV = 0;
346 if (fr->vmdplugin.api->read_timestep_metadata)
348 fr->vmdplugin.api->read_timestep_metadata(fr->vmdplugin.handle, metadata);
349 fr->vmdplugin.bV = metadata->has_velocities;
350 if (fr->vmdplugin.bV)
352 snew(fr->v,fr->natoms);