2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009-2018, The GROMACS development team.
5 * Copyright (c) 2019,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
42 #include "gromacs/utility/path.h"
43 #include "gromacs/utility/stringutil.h"
45 /* Derived from PluginMgr.C and catdcd.c */
47 /* PluginMgr.C: Copyright: */
48 /***************************************************************************
50 * cr (C) Copyright 1995-2009 The Board of Trustees of the
51 * cr University of Illinois
52 * cr All Rights Reserved
54 Developed by: Theoretical and Computational Biophysics Group
55 University of Illinois at Urbana-Champaign
56 http://www.ks.uiuc.edu/
58 Permission is hereby granted, free of charge, to any person obtaining a copy of
59 this software and associated documentation files (the Software), to deal with
60 the Software without restriction, including without limitation the rights to
61 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
62 of the Software, and to permit persons to whom the Software is furnished to
63 do so, subject to the following conditions:
65 Redistributions of source code must retain the above copyright notice,
66 this list of conditions and the following disclaimers.
68 Redistributions in binary form must reproduce the above copyright notice,
69 this list of conditions and the following disclaimers in the documentation
70 and/or other materials provided with the distribution.
72 Neither the names of Theoretical and Computational Biophysics Group,
73 University of Illinois at Urbana-Champaign, nor the names of its contributors
74 may be used to endorse or promote products derived from this Software without
75 specific prior written permission.
77 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
80 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
81 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
82 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
83 OTHER DEALINGS WITH THE SOFTWARE.
84 ***************************************************************************/
86 /* catdcd.c: Copyright: */
87 /*****************************************************************************/
89 /* (C) Copyright 2001-2005 Justin Gullingsrud and the University of Illinois.*/
91 /*****************************************************************************/
99 * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
101 #include "external/vmd_molfile/molfile_plugin.h"
102 #include "external/vmd_molfile/vmddlopen.h"
103 #if !GMX_NATIVE_WINDOWS
107 # define _WIN32_IE 0x0500 /* SHGetFolderPath is available since WinXP/IE5 */
110 # include <windows.h>
113 #include "gromacs/fileio/gmxfio.h"
114 #include "gromacs/math/vec.h"
115 #include "gromacs/pbcutil/pbc.h"
116 #include "gromacs/trajectory/trajectoryframe.h"
117 #include "gromacs/utility/basedefinitions.h"
118 #include "gromacs/utility/fatalerror.h"
119 #include "gromacs/utility/futil.h"
120 #include "gromacs/utility/smalloc.h"
123 typedef int (*initfunc)();
124 typedef int (*regfunc)(void*, vmdplugin_register_cb);
125 typedef int (*finifunc)();
128 static int register_cb(void* v, vmdplugin_t* p)
130 const char* key = p->name;
131 gmx_vmdplugin_t* vmdplugin = static_cast<gmx_vmdplugin_t*>(v);
133 if (strcmp(key, vmdplugin->filetype) == 0)
135 vmdplugin->api = reinterpret_cast<molfile_plugin_t*>(p);
137 return VMDPLUGIN_SUCCESS;
140 static int load_sharedlibrary_plugins(const char* fullpath, gmx_vmdplugin_t* vmdplugin)
142 /* Open the dll; try to execute the init function. */
143 void *handle, *ifunc, *registerfunc;
144 handle = vmddlopen(fullpath);
149 fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n", fullpath, vmddlerror()); /*only to debug because of stdc++ erros */
154 ifunc = vmddlsym(handle, "vmdplugin_init");
155 if (!ifunc || (reinterpret_cast<initfunc>(ifunc))())
157 printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
162 registerfunc = vmddlsym(handle, "vmdplugin_register");
165 printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
171 /* Load plugins from the library.*/
172 (reinterpret_cast<regfunc>(registerfunc))(vmdplugin, register_cb);
175 /* in case this library does not support the filetype, close it */
176 if (vmdplugin->api == nullptr)
184 /*return: 1: success, 0: last frame, -1: error*/
185 gmx_bool read_next_vmd_frame(gmx_vmdplugin_t* vmdplugin, t_trxframe* fr)
189 molfile_timestep_t ts;
192 fr->bV = vmdplugin->bV;
195 snew(ts.coords, fr->natoms * 3);
198 snew(ts.velocities, fr->natoms * 3);
201 ts.coords = reinterpret_cast<float*>(fr->x);
204 ts.velocities = reinterpret_cast<float*>(fr->v);
208 rc = vmdplugin->api->read_next_timestep(vmdplugin->handle, fr->natoms, &ts);
212 fprintf(stderr, "\nError reading input file (error code %d)\n", rc);
216 vmdplugin->api->close_file_read(vmdplugin->handle);
221 for (i = 0; i < fr->natoms; i++)
223 fr->x[i][0] = .1 * ts.coords[i * 3];
224 fr->x[i][1] = .1 * ts.coords[i * 3 + 1];
225 fr->x[i][2] = .1 * ts.coords[i * 3 + 2];
228 fr->v[i][0] = .1 * ts.velocities[i * 3];
229 fr->v[i][1] = .1 * ts.velocities[i * 3 + 1];
230 fr->v[i][2] = .1 * ts.velocities[i * 3 + 2];
236 sfree(ts.velocities);
239 for (i = 0; i < fr->natoms; i++)
241 svmul(.1, fr->x[i], fr->x[i]);
244 svmul(.1, fr->v[i], fr->v[i]);
257 matrix_convert(fr->box, vec, angle);
258 if (vmdplugin->api->abiversion > 10)
261 fr->time = ts.physical_time;
272 static int load_vmd_library(const char* fn, gmx_vmdplugin_t* vmdplugin)
276 #if !GMX_NATIVE_WINDOWS
278 const std::string defpath_suffix = "/plugins/*/molfile";
279 const std::string defpathenv = GMX_VMD_PLUGIN_PATH;
282 HANDLE hFind = INVALID_HANDLE_VALUE;
283 char progfolder[GMX_PATH_MAX];
284 std::string defpath_suffix = "\\plugins\\WIN32\\molfile";
285 SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, progfolder);
286 std::string defpathenv =
287 gmx::formatString("%s\\University of Illinois\\VMD\\plugins\\WIN32\\molfile", progfolder);
290 vmdplugin->api = nullptr;
291 vmdplugin->filetype = strrchr(fn, '.');
292 if (!vmdplugin->filetype)
296 vmdplugin->filetype++;
298 /* First look for an explicit path given at run time for the
299 * plugins, then an implicit run-time path, and finally for one
300 * given at configure time. This last might be hard-coded to the
301 * default for VMD installs. */
302 const char* pathenv = getenv("VMD_PLUGIN_PATH");
303 std::string fallBackPathEnv;
306 pathenv = getenv("VMDDIR");
309 printf("\nNeither VMD_PLUGIN_PATH or VMDDIR set. ");
310 printf("Using default location:\n%s\n", defpathenv.c_str());
311 pathenv = defpathenv.c_str();
315 printf("\nVMD_PLUGIN_PATH no set, but VMDDIR is set. ");
316 fallBackPathEnv = gmx::Path::join(pathenv, defpath_suffix);
317 pathenv = fallBackPathEnv.c_str();
318 printf("Using semi-default location:\n%s\n", pathenv);
321 #if !GMX_NATIVE_WINDOWS
322 std::string pathname = gmx::Path::join(pathenv, "/*.so");
323 glob(pathname.c_str(), 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 std::string pathname = gmx::Path::join(pathenv, "\\*.so");
344 hFind = FindFirstFile(pathname.c_str(), &ffd);
345 if (INVALID_HANDLE_VALUE == hFind)
347 printf("\nNo VMD Plugins found\n");
352 std::string filename = gmx::Path::join(pathenv, ffd.cFileName);
353 ret |= load_sharedlibrary_plugins(filename.c_str(), vmdplugin);
354 } while (FindNextFile(hFind, &ffd) != 0 && vmdplugin->api == NULL);
360 printf("\nCould not open any VMD library.\n");
364 printf("Compiled with dlopen?\n");
368 printf("Last error:\n%s\n", err);
373 if (vmdplugin->api == nullptr)
375 printf("\nNo plugin for %s found\n", vmdplugin->filetype);
379 if (vmdplugin->api->abiversion < 10)
381 printf("\nPlugin and/or VMD is too old. At least VMD 1.8.6 is required.\n");
385 printf("\nUsing VMD plugin: %s (%s)\n", vmdplugin->api->name, vmdplugin->api->prettyname);
390 int read_first_vmd_frame(const char* fn, gmx_vmdplugin_t** vmdpluginp, t_trxframe* fr)
392 molfile_timestep_metadata_t* metadata = nullptr;
393 gmx_vmdplugin_t* vmdplugin;
396 *vmdpluginp = vmdplugin;
397 if (!load_vmd_library(fn, vmdplugin))
402 vmdplugin->handle = vmdplugin->api->open_file_read(fn, vmdplugin->filetype, &fr->natoms);
404 if (!vmdplugin->handle)
406 fprintf(stderr, "\nError: could not open file '%s' for reading.\n", fn);
410 if (fr->natoms == MOLFILE_NUMATOMS_UNKNOWN)
412 fprintf(stderr, "\nFormat of file %s does not record number of atoms.\n", fn);
415 else if (fr->natoms == MOLFILE_NUMATOMS_NONE)
417 fprintf(stderr, "\nNo atoms found by VMD plugin in file %s.\n", fn);
420 else if (fr->natoms < 1) /*should not be reached*/
422 fprintf(stderr, "\nUnknown number of atoms %d for VMD plugin opening file %s.\n", fr->natoms, fn);
426 snew(fr->x, fr->natoms);
428 vmdplugin->bV = false;
429 if (vmdplugin->api->abiversion > 10 && vmdplugin->api->read_timestep_metadata)
431 vmdplugin->api->read_timestep_metadata(vmdplugin->handle, metadata);
433 vmdplugin->bV = (metadata->has_velocities != 0);
436 snew(fr->v, fr->natoms);
442 "\nThis trajectory is being read with a VMD plug-in from before VMD"
443 "\nversion 1.8, or from a trajectory that lacks time step metadata."
444 "\nEither way, GROMACS cannot tell whether the trajectory has velocities.\n");