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