2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
41 /* Derived from PluginMgr.C and catdcd.c */
43 /* PluginMgr.C: Copyright: */
44 /***************************************************************************
46 * cr (C) Copyright 1995-2009 The Board of Trustees of the
47 * cr University of Illinois
48 * cr All Rights Reserved
50 Developed by: Theoretical and Computational Biophysics Group
51 University of Illinois at Urbana-Champaign
52 http://www.ks.uiuc.edu/
54 Permission is hereby granted, free of charge, to any person obtaining a copy of
55 this software and associated documentation files (the Software), to deal with
56 the Software without restriction, including without limitation the rights to
57 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
58 of the Software, and to permit persons to whom the Software is furnished to
59 do so, subject to the following conditions:
61 Redistributions of source code must retain the above copyright notice,
62 this list of conditions and the following disclaimers.
64 Redistributions in binary form must reproduce the above copyright notice,
65 this list of conditions and the following disclaimers in the documentation
66 and/or other materials provided with the distribution.
68 Neither the names of Theoretical and Computational Biophysics Group,
69 University of Illinois at Urbana-Champaign, nor the names of its contributors
70 may be used to endorse or promote products derived from this Software without
71 specific prior written permission.
73 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
74 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
75 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
76 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
77 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
78 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
79 OTHER DEALINGS WITH THE SOFTWARE.
80 ***************************************************************************/
82 /* catdcd.c: Copyright: */
83 /*****************************************************************************/
85 /* (C) Copyright 2001-2005 Justin Gullingsrud and the University of Illinois.*/
87 /*****************************************************************************/
95 * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
97 #include "external/vmd_molfile/molfile_plugin.h"
98 #include "external/vmd_molfile/vmddlopen.h"
99 #if !GMX_NATIVE_WINDOWS
103 #define _WIN32_IE 0x0500 /* SHGetFolderPath is available since WinXP/IE5 */
109 #include "gromacs/fileio/gmxfio.h"
110 #include "gromacs/math/vec.h"
111 #include "gromacs/pbcutil/pbc.h"
112 #include "gromacs/trajectory/trajectoryframe.h"
113 #include "gromacs/utility/basedefinitions.h"
114 #include "gromacs/utility/fatalerror.h"
115 #include "gromacs/utility/futil.h"
116 #include "gromacs/utility/smalloc.h"
119 typedef int (*initfunc)(void);
120 typedef int (*regfunc)(void *, vmdplugin_register_cb);
121 typedef int (*finifunc)(void);
125 static int register_cb(void *v, vmdplugin_t *p)
127 const char *key = p->name;
128 gmx_vmdplugin_t *vmdplugin = static_cast<gmx_vmdplugin_t *>(v);
130 if (strcmp(key, vmdplugin->filetype) == 0)
132 vmdplugin->api = reinterpret_cast<molfile_plugin_t *>(p);
134 return VMDPLUGIN_SUCCESS;
137 static int load_sharedlibrary_plugins(const char *fullpath, gmx_vmdplugin_t *vmdplugin)
139 /* Open the dll; try to execute the init function. */
140 void *handle, *ifunc, *registerfunc;
141 handle = vmddlopen(fullpath);
146 fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n", fullpath, vmddlerror()); /*only to debug because of stdc++ erros */
151 ifunc = vmddlsym(handle, "vmdplugin_init");
152 if (!ifunc || (reinterpret_cast<initfunc>(ifunc))())
154 printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
159 registerfunc = vmddlsym(handle, "vmdplugin_register");
162 printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
168 /* Load plugins from the library.*/
169 (reinterpret_cast<regfunc>(registerfunc))(vmdplugin, register_cb);
172 /* in case this library does not support the filetype, close it */
173 if (vmdplugin->api == nullptr)
181 /*return: 1: success, 0: last frame, -1: error*/
182 gmx_bool read_next_vmd_frame(gmx_vmdplugin_t *vmdplugin, t_trxframe *fr)
186 molfile_timestep_t ts;
189 fr->bV = vmdplugin->bV;
192 snew(ts.coords, fr->natoms*3);
195 snew(ts.velocities, fr->natoms*3);
198 ts.coords = reinterpret_cast<float*>(fr->x);
201 ts.velocities = reinterpret_cast<float*>(fr->v);
205 rc = vmdplugin->api->read_next_timestep(vmdplugin->handle, fr->natoms, &ts);
209 fprintf(stderr, "\nError reading input file (error code %d)\n", rc);
213 vmdplugin->api->close_file_read(vmdplugin->handle);
218 for (i = 0; i < fr->natoms; i++)
220 fr->x[i][0] = .1*ts.coords[i*3];
221 fr->x[i][1] = .1*ts.coords[i*3+1];
222 fr->x[i][2] = .1*ts.coords[i*3+2];
225 fr->v[i][0] = .1*ts.velocities[i*3];
226 fr->v[i][1] = .1*ts.velocities[i*3+1];
227 fr->v[i][2] = .1*ts.velocities[i*3+2];
233 sfree(ts.velocities);
236 for (i = 0; i < fr->natoms; i++)
238 svmul(.1, fr->x[i], fr->x[i]);
241 svmul(.1, fr->v[i], fr->v[i]);
248 vec[0] = .1*ts.A; vec[1] = .1*ts.B; vec[2] = .1*ts.C;
249 angle[0] = ts.alpha; angle[1] = ts.beta; angle[2] = ts.gamma;
250 matrix_convert(fr->box, vec, angle);
251 if (vmdplugin->api->abiversion > 10)
254 fr->time = ts.physical_time;
265 static int load_vmd_library(const char *fn, gmx_vmdplugin_t *vmdplugin)
267 char pathname[GMX_PATH_MAX];
271 char pathenv_buffer[GMX_PATH_MAX];
272 #if !GMX_NATIVE_WINDOWS
274 const char *defpath_suffix = "/plugins/*/molfile";
275 const char *defpathenv = GMX_VMD_PLUGIN_PATH;
278 HANDLE hFind = INVALID_HANDLE_VALUE;
279 char progfolder[GMX_PATH_MAX];
280 char defpathenv[GMX_PATH_MAX];
281 const char *defpath_suffix = "\\plugins\\WIN32\\molfile";
282 SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, progfolder);
283 sprintf(defpathenv, "%s\\University of Illinois\\VMD\\plugins\\WIN32\\molfile", progfolder);
286 vmdplugin->api = nullptr;
287 vmdplugin->filetype = strrchr(fn, '.');
288 if (!vmdplugin->filetype)
292 vmdplugin->filetype++;
294 /* First look for an explicit path given at run time for the
295 * plugins, then an implicit run-time path, and finally for one
296 * given at configure time. This last might be hard-coded to the
297 * default for VMD installs. */
298 pathenv = getenv("VMD_PLUGIN_PATH");
299 if (pathenv == nullptr)
301 pathenv = getenv("VMDDIR");
302 if (nullptr == pathenv)
304 printf("\nNeither VMD_PLUGIN_PATH or VMDDIR set. ");
305 printf("Using default location:\n%s\n", defpathenv);
306 pathenv = defpathenv;
310 printf("\nVMD_PLUGIN_PATH no set, but VMDDIR is set. ");
312 _snprintf_s(pathenv_buffer, sizeof(pathenv_buffer), _TRUNCATE, "%s%s", pathenv, defpath_suffix);
314 snprintf(pathenv_buffer, sizeof(pathenv_buffer), "%s%s", pathenv, defpath_suffix);
316 printf("Using semi-default location:\n%s\n", pathenv_buffer);
317 pathenv = pathenv_buffer;
320 strncpy(pathname, pathenv, sizeof(pathname));
321 #if !GMX_NATIVE_WINDOWS
322 strcat(pathname, "/*.so");
323 glob(pathname, 0, nullptr, &globbuf);
324 if (globbuf.gl_pathc == 0)
326 printf("\nNo VMD Plugins found\n"
327 "Set the environment variable VMD_PLUGIN_PATH to the molfile folder within the\n"
328 "VMD installation.\n"
329 "The architecture (e.g. 32bit versus 64bit) of GROMACS and VMD has to match.\n");
332 for (size_t i = 0; i < globbuf.gl_pathc && vmdplugin->api == nullptr; i++)
334 /* FIXME: Undefined which plugin is chosen if more than one plugin
335 can read a certain file ending. Requires some additional command
336 line option or enviroment variable to specify which plugin should
339 ret |= load_sharedlibrary_plugins(globbuf.gl_pathv[i], vmdplugin);
343 strcat(pathname, "\\*.so");
344 hFind = FindFirstFile(pathname, &ffd);
345 if (INVALID_HANDLE_VALUE == hFind)
347 printf("\nNo VMD Plugins found\n");
352 char filename[GMX_PATH_MAX];
353 sprintf(filename, "%s\\%s", pathenv, ffd.cFileName);
354 ret |= load_sharedlibrary_plugins(filename, vmdplugin);
356 while (FindNextFile(hFind, &ffd ) != 0 && vmdplugin->api == NULL);
362 printf("\nCould not open any VMD library.\n");
366 printf("Compiled with dlopen?\n");
370 printf("Last error:\n%s\n", err);
375 if (vmdplugin->api == nullptr)
377 printf("\nNo plugin for %s found\n", vmdplugin->filetype);
381 if (vmdplugin->api->abiversion < 10)
383 printf("\nPlugin and/or VMD is too old. At least VMD 1.8.6 is required.\n");
387 printf("\nUsing VMD plugin: %s (%s)\n", vmdplugin->api->name, vmdplugin->api->prettyname);
393 int read_first_vmd_frame(const char *fn, gmx_vmdplugin_t **vmdpluginp, t_trxframe *fr)
395 molfile_timestep_metadata_t *metadata = nullptr;
396 gmx_vmdplugin_t *vmdplugin;
399 *vmdpluginp = vmdplugin;
400 if (!load_vmd_library(fn, vmdplugin))
405 vmdplugin->handle = vmdplugin->api->open_file_read(fn, vmdplugin->filetype, &fr->natoms);
407 if (!vmdplugin->handle)
409 fprintf(stderr, "\nError: could not open file '%s' for reading.\n",
414 if (fr->natoms == MOLFILE_NUMATOMS_UNKNOWN)
416 fprintf(stderr, "\nFormat of file %s does not record number of atoms.\n", fn);
419 else if (fr->natoms == MOLFILE_NUMATOMS_NONE)
421 fprintf(stderr, "\nNo atoms found by VMD plugin in file %s.\n", fn );
424 else if (fr->natoms < 1) /*should not be reached*/
426 fprintf(stderr, "\nUnknown number of atoms %d for VMD plugin opening file %s.\n",
431 snew(fr->x, fr->natoms);
434 if (vmdplugin->api->abiversion > 10 && vmdplugin->api->read_timestep_metadata)
436 vmdplugin->api->read_timestep_metadata(vmdplugin->handle, metadata);
438 vmdplugin->bV = metadata->has_velocities;
441 snew(fr->v, fr->natoms);
447 "\nThis trajectory is being read with a VMD plug-in from before VMD"
448 "\nversion 1.8, or from a trajectory that lacks time step metadata."
449 "\nEither way, GROMACS cannot tell whether the trajectory has velocities.\n");