Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / fileio / vmdio.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2012,2013,2014, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 #include "vmdio.h"
36
37 #include "config.h"
38
39 /* Derived from PluginMgr.C and catdcd.c */
40
41 /* PluginMgr.C: Copyright: */
42 /***************************************************************************
43  * cr
44  * cr            (C) Copyright 1995-2009 The Board of Trustees of the
45  * cr                        University of Illinois
46  * cr                         All Rights Reserved
47  * cr
48    Developed by:           Theoretical and Computational Biophysics Group
49                         University of Illinois at Urbana-Champaign
50                         http://www.ks.uiuc.edu/
51
52    Permission is hereby granted, free of charge, to any person obtaining a copy of
53    this software and associated documentation files (the Software), to deal with
54    the Software without restriction, including without limitation the rights to
55    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
56    of the Software, and to permit persons to whom the Software is furnished to
57    do so, subject to the following conditions:
58
59    Redistributions of source code must retain the above copyright notice,
60    this list of conditions and the following disclaimers.
61
62    Redistributions in binary form must reproduce the above copyright notice,
63    this list of conditions and the following disclaimers in the documentation
64    and/or other materials provided with the distribution.
65
66    Neither the names of Theoretical and Computational Biophysics Group,
67    University of Illinois at Urbana-Champaign, nor the names of its contributors
68    may be used to endorse or promote products derived from this Software without
69    specific prior written permission.
70
71    THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
72    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
73    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
74    THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
75    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
76    ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
77    OTHER DEALINGS WITH THE SOFTWARE.
78  ***************************************************************************/
79
80 /* catdcd.c: Copyright: */
81 /*****************************************************************************/
82 /*                                                                           */
83 /* (C) Copyright 2001-2005 Justin Gullingsrud and the University of Illinois.*/
84 /*                                                                           */
85 /*****************************************************************************/
86
87 #include <stdlib.h>
88 #include <stdio.h>
89 #include <string.h>
90 #include <assert.h>
91
92 /*
93  * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
94  */
95 #include "external/vmd_molfile/molfile_plugin.h"
96 #include "external/vmd_molfile/vmddlopen.h"
97 #ifndef GMX_NATIVE_WINDOWS
98 #include <glob.h>
99 #else
100 #include <windows.h>
101 #include <shlobj.h>
102 #endif
103
104 #include "gromacs/fileio/gmxfio.h"
105 #include "gromacs/fileio/trx.h"
106 #include "gromacs/math/vec.h"
107 #include "gromacs/utility/basedefinitions.h"
108 #include "gromacs/utility/futil.h"
109 #include "gromacs/utility/smalloc.h"
110
111
112 typedef int (*initfunc)(void);
113 typedef int (*regfunc)(void *, vmdplugin_register_cb);
114 typedef int (*finifunc)(void);
115
116
117
118 static int register_cb(void *v, vmdplugin_t *p)
119 {
120     const char     *key       = p->name;
121     t_gmxvmdplugin *vmdplugin = (t_gmxvmdplugin*)v;
122
123     if (strcmp(key, vmdplugin->filetype) == 0)
124     {
125         vmdplugin->api = (molfile_plugin_t *)p;
126     }
127     return VMDPLUGIN_SUCCESS;
128 }
129
130 static int load_sharedlibrary_plugins(const char *fullpath, t_gmxvmdplugin* vmdplugin)
131 {
132     /* Open the dll; try to execute the init function. */
133     void *handle, *ifunc, *registerfunc;
134     handle = vmddlopen(fullpath);
135     if (!handle)
136     {
137         if (debug)
138         {
139             fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n",  fullpath, vmddlerror());         /*only to debug because of stdc++ erros */
140         }
141         return 0;
142     }
143
144     ifunc = vmddlsym(handle, "vmdplugin_init");
145     if (!ifunc || ((initfunc)(ifunc))())
146     {
147         printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
148         vmddlclose(handle);
149         return 0;
150     }
151
152     registerfunc = vmddlsym(handle, "vmdplugin_register");
153     if (!registerfunc)
154     {
155         printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
156         vmddlclose(handle);
157         return 0;
158     }
159     else
160     {
161         /* Load plugins from the library.*/
162         ((regfunc)registerfunc)(vmdplugin, register_cb);
163     }
164
165     /* in case this library does not support the filetype, close it */
166     if (vmdplugin->api == NULL)
167     {
168         vmddlclose(handle);
169     }
170
171     return 1;
172 }
173
174 /*return: 1: success, 0: last frame, -1: error*/
175 gmx_bool read_next_vmd_frame(t_trxframe *fr)
176 {
177     int                rc, i;
178     rvec               vec, angle;
179     molfile_timestep_t ts;
180
181
182     fr->bV = fr->vmdplugin->bV;
183
184 #ifdef GMX_DOUBLE
185     snew(ts.coords, fr->natoms*3);
186     if (fr->bV)
187     {
188         snew(ts.velocities, fr->natoms*3);
189     }
190 #else
191     ts.coords = (float*)fr->x;
192     if (fr->bV)
193     {
194         ts.velocities = (float*)fr->v;
195     }
196 #endif
197
198     rc = fr->vmdplugin->api->read_next_timestep(fr->vmdplugin->handle, fr->natoms, &ts);
199
200     if (rc < -1)
201     {
202         fprintf(stderr, "\nError reading input file (error code %d)\n", rc);
203     }
204     if (rc < 0)
205     {
206         fr->vmdplugin->api->close_file_read(fr->vmdplugin->handle);
207         return 0;
208     }
209
210 #ifdef GMX_DOUBLE
211     for (i = 0; i < fr->natoms; i++)
212     {
213         fr->x[i][0] = .1*ts.coords[i*3];
214         fr->x[i][1] = .1*ts.coords[i*3+1];
215         fr->x[i][2] = .1*ts.coords[i*3+2];
216         if (fr->bV)
217         {
218             fr->v[i][0] = .1*ts.velocities[i*3];
219             fr->v[i][1] = .1*ts.velocities[i*3+1];
220             fr->v[i][2] = .1*ts.velocities[i*3+2];
221         }
222     }
223     sfree(ts.coords);
224     if (fr->bV)
225     {
226         sfree(ts.velocities);
227     }
228 #else
229     for (i = 0; i < fr->natoms; i++)
230     {
231         svmul(.1, fr->x[i], fr->x[i]);
232         if (fr->bV)
233         {
234             svmul(.1, fr->v[i], fr->v[i]);
235         }
236     }
237 #endif
238
239     fr->bX   = 1;
240     fr->bBox = 1;
241     vec[0]   = .1*ts.A; vec[1] = .1*ts.B; vec[2] = .1*ts.C;
242     angle[0] = ts.alpha; angle[1] = ts.beta; angle[2] = ts.gamma;
243     matrix_convert(fr->box, vec, angle);
244     if (fr->vmdplugin->api->abiversion > 10)
245     {
246         fr->bTime = TRUE;
247         fr->time  = ts.physical_time;
248     }
249     else
250     {
251         fr->bTime = FALSE;
252     }
253
254
255     return 1;
256 }
257
258 static int load_vmd_library(const char *fn, t_gmxvmdplugin *vmdplugin)
259 {
260     char            pathname[GMX_PATH_MAX], filename[GMX_PATH_MAX];
261     const char     *pathenv;
262     const char     *err;
263     int             i;
264     int             ret = 0;
265     char            pathenv_buffer[GMX_PATH_MAX];
266 #ifndef GMX_NATIVE_WINDOWS
267     glob_t          globbuf;
268     const char     *defpath_suffix = "/plugins/*/molfile";
269     const char     *defpathenv     = GMX_VMD_PLUGIN_PATH;
270 #else
271     WIN32_FIND_DATA ffd;
272     HANDLE          hFind = INVALID_HANDLE_VALUE;
273     char            progfolder[GMX_PATH_MAX];
274     char            defpathenv[GMX_PATH_MAX];
275     const char     *defpath_suffix = "\\plugins\\WIN32\\molfile";
276     SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, progfolder);
277     sprintf(defpathenv, "%s\\University of Illinois\\VMD\\plugins\\WIN32\\molfile", progfolder);
278 #endif
279
280     vmdplugin->api      = NULL;
281     vmdplugin->filetype = strrchr(fn, '.');
282     if (!vmdplugin->filetype)
283     {
284         return 0;
285     }
286     vmdplugin->filetype++;
287
288     /* First look for an explicit path given at run time for the
289      * plugins, then an implicit run-time path, and finally for one
290      * given at configure time. This last might be hard-coded to the
291      * default for VMD installs. */
292     pathenv = getenv("VMD_PLUGIN_PATH");
293     if (pathenv == NULL)
294     {
295         pathenv = getenv("VMDDIR");
296         if (NULL == pathenv)
297         {
298             printf("\nNeither VMD_PLUGIN_PATH or VMDDIR set. ");
299             printf("Using default location:\n%s\n", defpathenv);
300             pathenv = defpathenv;
301         }
302         else
303         {
304             printf("\nVMD_PLUGIN_PATH no set, but VMDDIR is set. ");
305 #ifdef _MSC_VER
306             _snprintf_s(pathenv_buffer, sizeof(pathenv_buffer), _TRUNCATE, "%s%s", pathenv, defpath_suffix);
307 #else
308             snprintf(pathenv_buffer, sizeof(pathenv_buffer), "%s%s", pathenv, defpath_suffix);
309 #endif
310             printf("Using semi-default location:\n%s\n", pathenv_buffer);
311             pathenv = pathenv_buffer;
312         }
313     }
314     strncpy(pathname, pathenv, sizeof(pathname));
315 #ifndef GMX_NATIVE_WINDOWS
316     strcat(pathname, "/*.so");
317     glob(pathname, 0, NULL, &globbuf);
318     if (globbuf.gl_pathc == 0)
319     {
320         printf("\nNo VMD Plugins found\n"
321                "Set the environment variable VMD_PLUGIN_PATH to the molfile folder within the\n"
322                "VMD installation.\n"
323                "The architecture (e.g. 32bit versus 64bit) of Gromacs and VMD has to match.\n");
324         return 0;
325     }
326     for (i = 0; i < globbuf.gl_pathc && vmdplugin->api == NULL; i++)
327     {
328         /* FIXME: Undefined which plugin is chosen if more than one plugin
329            can read a certain file ending. Requires some additional command
330            line option or enviroment variable to specify which plugin should
331            be picked.
332          */
333         ret |= load_sharedlibrary_plugins(globbuf.gl_pathv[i], vmdplugin);
334     }
335     globfree(&globbuf);
336 #else
337     strcat(pathname, "\\*.so");
338     hFind = FindFirstFile(pathname, &ffd);
339     if (INVALID_HANDLE_VALUE == hFind)
340     {
341         printf("\nNo VMD Plugins found\n");
342         return 0;
343     }
344     do
345     {
346         sprintf(filename, "%s\\%s", pathenv, ffd.cFileName);
347         ret |= load_sharedlibrary_plugins(filename, vmdplugin);
348     }
349     while (FindNextFile(hFind, &ffd )  != 0 && vmdplugin->api == NULL);
350     FindClose(hFind);
351 #endif
352
353     if (!ret)
354     {
355         printf("\nCould not open any VMD library.\n");
356         err = vmddlerror();
357         if (!err)
358         {
359             printf("Compiled with dlopen?\n");
360         }
361         else
362         {
363             printf("Last error:\n%s\n", err);
364         }
365         return 0;
366     }
367
368     if (vmdplugin->api == NULL)
369     {
370         printf("\nNo plugin for %s found\n", vmdplugin->filetype);
371         return 0;
372     }
373
374     if (vmdplugin->api->abiversion < 10)
375     {
376         printf("\nPlugin and/or VMD is too old. At least VMD 1.8.6 is required.\n");
377         return 0;
378     }
379
380     printf("\nUsing VMD plugin: %s (%s)\n", vmdplugin->api->name, vmdplugin->api->prettyname);
381
382     return 1;
383
384 }
385
386 int read_first_vmd_frame(const char *fn, t_trxframe *fr)
387 {
388     molfile_timestep_metadata_t *metadata = NULL;
389
390     snew(fr->vmdplugin, 1);
391     if (!load_vmd_library(fn, fr->vmdplugin))
392     {
393         return 0;
394     }
395
396     fr->vmdplugin->handle = fr->vmdplugin->api->open_file_read(fn, fr->vmdplugin->filetype, &fr->natoms);
397
398     if (!fr->vmdplugin->handle)
399     {
400         fprintf(stderr, "\nError: could not open file '%s' for reading.\n",
401                 fn);
402         return 0;
403     }
404
405     if (fr->natoms == MOLFILE_NUMATOMS_UNKNOWN)
406     {
407         fprintf(stderr, "\nFormat of file %s does not record number of atoms.\n", fn);
408         return 0;
409     }
410     else if (fr->natoms == MOLFILE_NUMATOMS_NONE)
411     {
412         fprintf(stderr, "\nNo atoms found by VMD plugin in file %s.\n", fn );
413         return 0;
414     }
415     else if (fr->natoms < 1)     /*should not be reached*/
416     {
417         fprintf(stderr, "\nUnknown number of atoms %d for VMD plugin opening file %s.\n",
418                 fr->natoms, fn );
419         return 0;
420     }
421
422     snew(fr->x, fr->natoms);
423
424     fr->vmdplugin->bV = 0;
425     if (fr->vmdplugin->api->abiversion > 10 && fr->vmdplugin->api->read_timestep_metadata)
426     {
427         fr->vmdplugin->api->read_timestep_metadata(fr->vmdplugin->handle, metadata);
428         assert(metadata);
429         fr->vmdplugin->bV = metadata->has_velocities;
430         if (fr->vmdplugin->bV)
431         {
432             snew(fr->v, fr->natoms);
433         }
434     }
435     else
436     {
437         fprintf(stderr,
438                 "\nThis trajectory is being read with a VMD plug-in from before VMD"
439                 "\nversion 1.8, or from a trajectory that lacks time step metadata."
440                 "\nEither way, GROMACS cannot tell whether the trajectory has velocities.\n");
441     }
442     return 1;
443
444 }