Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / fileio / vmdio.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009-2018, The GROMACS development team.
5  * Copyright (c) 2019,2020, 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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 #include "gmxpre.h"
37
38 #include "vmdio.h"
39
40 #include "config.h"
41
42 #include "gromacs/utility/path.h"
43 #include "gromacs/utility/stringutil.h"
44
45 /* Derived from PluginMgr.C and catdcd.c */
46
47 /* PluginMgr.C: Copyright: */
48 /***************************************************************************
49  * cr
50  * cr            (C) Copyright 1995-2009 The Board of Trustees of the
51  * cr                        University of Illinois
52  * cr                         All Rights Reserved
53  * cr
54    Developed by:           Theoretical and Computational Biophysics Group
55                         University of Illinois at Urbana-Champaign
56                         http://www.ks.uiuc.edu/
57
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:
64
65    Redistributions of source code must retain the above copyright notice,
66    this list of conditions and the following disclaimers.
67
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.
71
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.
76
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  ***************************************************************************/
85
86 /* catdcd.c: Copyright: */
87 /*****************************************************************************/
88 /*                                                                           */
89 /* (C) Copyright 2001-2005 Justin Gullingsrud and the University of Illinois.*/
90 /*                                                                           */
91 /*****************************************************************************/
92
93 #include <cassert>
94 #include <cstdio>
95 #include <cstdlib>
96 #include <cstring>
97
98 /*
99  * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
100  */
101 #include "external/vmd_molfile/molfile_plugin.h"
102 #include "external/vmd_molfile/vmddlopen.h"
103 #if !GMX_NATIVE_WINDOWS
104 #    include <glob.h>
105 #else
106 #    ifndef _WIN32_IE
107 #        define _WIN32_IE 0x0500 /* SHGetFolderPath is available since WinXP/IE5 */
108 #    endif
109 #    include <shlobj.h>
110 #    include <windows.h>
111 #endif
112
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"
121
122
123 typedef int (*initfunc)();
124 typedef int (*regfunc)(void*, vmdplugin_register_cb);
125 typedef int (*finifunc)();
126
127
128 static int register_cb(void* v, vmdplugin_t* p)
129 {
130     const char*      key       = p->name;
131     gmx_vmdplugin_t* vmdplugin = static_cast<gmx_vmdplugin_t*>(v);
132
133     if (strcmp(key, vmdplugin->filetype) == 0)
134     {
135         vmdplugin->api = reinterpret_cast<molfile_plugin_t*>(p);
136     }
137     return VMDPLUGIN_SUCCESS;
138 }
139
140 static int load_sharedlibrary_plugins(const char* fullpath, gmx_vmdplugin_t* vmdplugin)
141 {
142     /* Open the dll; try to execute the init function. */
143     void *handle, *ifunc, *registerfunc;
144     handle = vmddlopen(fullpath);
145     if (!handle)
146     {
147         if (debug)
148         {
149             fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n", fullpath, vmddlerror()); /*only to debug because of stdc++ erros */
150         }
151         return 0;
152     }
153
154     ifunc = vmddlsym(handle, "vmdplugin_init");
155     if (!ifunc || (reinterpret_cast<initfunc>(ifunc))())
156     {
157         printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
158         vmddlclose(handle);
159         return 0;
160     }
161
162     registerfunc = vmddlsym(handle, "vmdplugin_register");
163     if (!registerfunc)
164     {
165         printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
166         vmddlclose(handle);
167         return 0;
168     }
169     else
170     {
171         /* Load plugins from the library.*/
172         (reinterpret_cast<regfunc>(registerfunc))(vmdplugin, register_cb);
173     }
174
175     /* in case this library does not support the filetype, close it */
176     if (vmdplugin->api == nullptr)
177     {
178         vmddlclose(handle);
179     }
180
181     return 1;
182 }
183
184 /*return: 1: success, 0: last frame, -1: error*/
185 gmx_bool read_next_vmd_frame(gmx_vmdplugin_t* vmdplugin, t_trxframe* fr)
186 {
187     int                rc, i;
188     rvec               vec, angle;
189     molfile_timestep_t ts;
190
191
192     fr->bV = vmdplugin->bV;
193
194 #if GMX_DOUBLE
195     snew(ts.coords, fr->natoms * 3);
196     if (fr->bV)
197     {
198         snew(ts.velocities, fr->natoms * 3);
199     }
200 #else
201     ts.coords = reinterpret_cast<float*>(fr->x);
202     if (fr->bV)
203     {
204         ts.velocities = reinterpret_cast<float*>(fr->v);
205     }
206 #endif
207
208     rc = vmdplugin->api->read_next_timestep(vmdplugin->handle, fr->natoms, &ts);
209
210     if (rc < -1)
211     {
212         fprintf(stderr, "\nError reading input file (error code %d)\n", rc);
213     }
214     if (rc < 0)
215     {
216         vmdplugin->api->close_file_read(vmdplugin->handle);
217         return false;
218     }
219
220 #if GMX_DOUBLE
221     for (i = 0; i < fr->natoms; i++)
222     {
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];
226         if (fr->bV)
227         {
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];
231         }
232     }
233     sfree(ts.coords);
234     if (fr->bV)
235     {
236         sfree(ts.velocities);
237     }
238 #else
239     for (i = 0; i < fr->natoms; i++)
240     {
241         svmul(.1, fr->x[i], fr->x[i]);
242         if (fr->bV)
243         {
244             svmul(.1, fr->v[i], fr->v[i]);
245         }
246     }
247 #endif
248
249     fr->bX   = true;
250     fr->bBox = true;
251     vec[0]   = .1 * ts.A;
252     vec[1]   = .1 * ts.B;
253     vec[2]   = .1 * ts.C;
254     angle[0] = ts.alpha;
255     angle[1] = ts.beta;
256     angle[2] = ts.gamma;
257     matrix_convert(fr->box, vec, angle);
258     if (vmdplugin->api->abiversion > 10)
259     {
260         fr->bTime = TRUE;
261         fr->time  = ts.physical_time;
262     }
263     else
264     {
265         fr->bTime = FALSE;
266     }
267
268
269     return true;
270 }
271
272 static int load_vmd_library(const char* fn, gmx_vmdplugin_t* vmdplugin)
273 {
274     const char* err;
275     int         ret = 0;
276 #if !GMX_NATIVE_WINDOWS
277     glob_t            globbuf;
278     const std::string defpath_suffix = "/plugins/*/molfile";
279     const std::string defpathenv     = GMX_VMD_PLUGIN_PATH;
280 #else
281     WIN32_FIND_DATA ffd;
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);
288 #endif
289
290     vmdplugin->api      = nullptr;
291     vmdplugin->filetype = strrchr(fn, '.');
292     if (!vmdplugin->filetype)
293     {
294         return 0;
295     }
296     vmdplugin->filetype++;
297
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     std::string pathenv = getenv("VMD_PLUGIN_PATH");
303     if (pathenv.empty())
304     {
305         pathenv = getenv("VMDDIR");
306         if (pathenv.empty())
307         {
308             printf("\nNeither VMD_PLUGIN_PATH or VMDDIR set. ");
309             printf("Using default location:\n%s\n", defpathenv.c_str());
310             pathenv = defpathenv;
311         }
312         else
313         {
314             printf("\nVMD_PLUGIN_PATH no set, but VMDDIR is set. ");
315             pathenv = gmx::Path::join(pathenv, defpath_suffix);
316             printf("Using semi-default location:\n%s\n", pathenv.c_str());
317         }
318     }
319 #if !GMX_NATIVE_WINDOWS
320     std::string pathname = gmx::Path::join(pathenv, "/*.so");
321     glob(pathname.c_str(), 0, nullptr, &globbuf);
322     if (globbuf.gl_pathc == 0)
323     {
324         printf("\nNo VMD Plugins found\n"
325                "Set the environment variable VMD_PLUGIN_PATH to the molfile folder within the\n"
326                "VMD installation.\n"
327                "The architecture (e.g. 32bit versus 64bit) of GROMACS and VMD has to match.\n");
328         return 0;
329     }
330     for (size_t i = 0; i < globbuf.gl_pathc && vmdplugin->api == nullptr; i++)
331     {
332         /* FIXME: Undefined which plugin is chosen if more than one plugin
333            can read a certain file ending. Requires some additional command
334            line option or enviroment variable to specify which plugin should
335            be picked.
336          */
337         ret |= load_sharedlibrary_plugins(globbuf.gl_pathv[i], vmdplugin);
338     }
339     globfree(&globbuf);
340 #else
341     std::string pathname = gmx::Path::join(pathenv, "\\*.so");
342     hFind                = FindFirstFile(pathname.c_str(), &ffd);
343     if (INVALID_HANDLE_VALUE == hFind)
344     {
345         printf("\nNo VMD Plugins found\n");
346         return 0;
347     }
348     do
349     {
350         std::string filename = gmx::Path::join(pathenv, ffd.cFileName);
351         ret |= load_sharedlibrary_plugins(filename.c_str(), vmdplugin);
352     } while (FindNextFile(hFind, &ffd) != 0 && vmdplugin->api == NULL);
353     FindClose(hFind);
354 #endif
355
356     if (!ret)
357     {
358         printf("\nCould not open any VMD library.\n");
359         err = vmddlerror();
360         if (!err)
361         {
362             printf("Compiled with dlopen?\n");
363         }
364         else
365         {
366             printf("Last error:\n%s\n", err);
367         }
368         return 0;
369     }
370
371     if (vmdplugin->api == nullptr)
372     {
373         printf("\nNo plugin for %s found\n", vmdplugin->filetype);
374         return 0;
375     }
376
377     if (vmdplugin->api->abiversion < 10)
378     {
379         printf("\nPlugin and/or VMD is too old. At least VMD 1.8.6 is required.\n");
380         return 0;
381     }
382
383     printf("\nUsing VMD plugin: %s (%s)\n", vmdplugin->api->name, vmdplugin->api->prettyname);
384
385     return 1;
386 }
387
388 int read_first_vmd_frame(const char* fn, gmx_vmdplugin_t** vmdpluginp, t_trxframe* fr)
389 {
390     molfile_timestep_metadata_t* metadata = nullptr;
391     gmx_vmdplugin_t*             vmdplugin;
392
393     snew(vmdplugin, 1);
394     *vmdpluginp = vmdplugin;
395     if (!load_vmd_library(fn, vmdplugin))
396     {
397         return 0;
398     }
399
400     vmdplugin->handle = vmdplugin->api->open_file_read(fn, vmdplugin->filetype, &fr->natoms);
401
402     if (!vmdplugin->handle)
403     {
404         fprintf(stderr, "\nError: could not open file '%s' for reading.\n", fn);
405         return 0;
406     }
407
408     if (fr->natoms == MOLFILE_NUMATOMS_UNKNOWN)
409     {
410         fprintf(stderr, "\nFormat of file %s does not record number of atoms.\n", fn);
411         return 0;
412     }
413     else if (fr->natoms == MOLFILE_NUMATOMS_NONE)
414     {
415         fprintf(stderr, "\nNo atoms found by VMD plugin in file %s.\n", fn);
416         return 0;
417     }
418     else if (fr->natoms < 1) /*should not be reached*/
419     {
420         fprintf(stderr, "\nUnknown number of atoms %d for VMD plugin opening file %s.\n", fr->natoms, fn);
421         return 0;
422     }
423
424     snew(fr->x, fr->natoms);
425
426     vmdplugin->bV = false;
427     if (vmdplugin->api->abiversion > 10 && vmdplugin->api->read_timestep_metadata)
428     {
429         vmdplugin->api->read_timestep_metadata(vmdplugin->handle, metadata);
430         assert(metadata);
431         vmdplugin->bV = (metadata->has_velocities != 0);
432         if (vmdplugin->bV)
433         {
434             snew(fr->v, fr->natoms);
435         }
436     }
437     else
438     {
439         fprintf(stderr,
440                 "\nThis trajectory is being read with a VMD plug-in from before VMD"
441                 "\nversion 1.8, or from a trajectory that lacks time step metadata."
442                 "\nEither way, GROMACS cannot tell whether the trajectory has velocities.\n");
443     }
444     return 1;
445 }