Create fileio module
[alexxy/gromacs.git] / src / gromacs / fileio / vmdio.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2008, by the GROMACS development team.
5  * Copyright (c) 2013, 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
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
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 #include <windows.h>
103 #include <shlobj.h>
104 #endif
105 #include "smalloc.h"
106 #include "futil.h"
107 #include "vmdio.h"
108
109
110 #include "types/simple.h"
111 #include "vec.h"
112 #include "gmxfio.h"
113
114
115 typedef int (*initfunc)(void);
116 typedef int (*regfunc)(void *, vmdplugin_register_cb);
117 typedef int (*finifunc)(void);
118
119
120
121 static int register_cb(void *v, vmdplugin_t *p)
122 {
123     const char     *key       = p->name;
124     t_gmxvmdplugin *vmdplugin = (t_gmxvmdplugin*)v;
125
126     if (strcmp(key, vmdplugin->filetype) == 0)
127     {
128         vmdplugin->api = (molfile_plugin_t *)p;
129     }
130     return VMDPLUGIN_SUCCESS;
131 }
132
133 static int load_sharedlibrary_plugins(const char *fullpath, t_gmxvmdplugin* vmdplugin)
134 {
135     /* Open the dll; try to execute the init function. */
136     void *handle, *ifunc, *registerfunc;
137     handle = vmddlopen(fullpath);
138     if (!handle)
139     {
140         if (debug)
141         {
142             fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n",  fullpath, vmddlerror());         /*only to debug because of stdc++ erros */
143         }
144         return 0;
145     }
146
147     ifunc = vmddlsym(handle, "vmdplugin_init");
148     if (!ifunc || ((initfunc)(ifunc))())
149     {
150         printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
151         vmddlclose(handle);
152         return 0;
153     }
154
155     registerfunc = vmddlsym(handle, "vmdplugin_register");
156     if (!registerfunc)
157     {
158         printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
159         vmddlclose(handle);
160         return 0;
161     }
162     else
163     {
164         /* Load plugins from the library.*/
165         ((regfunc)registerfunc)(vmdplugin, register_cb);
166     }
167
168     /* in case this library does not support the filetype, close it */
169     if (vmdplugin->api == NULL)
170     {
171         vmddlclose(handle);
172     }
173
174     return 1;
175 }
176
177 /*return: 1: success, 0: last frame, -1: error*/
178 gmx_bool read_next_vmd_frame(t_trxframe *fr)
179 {
180     int                rc, i;
181     rvec               vec, angle;
182     molfile_timestep_t ts;
183
184
185     fr->bV = fr->vmdplugin->bV;
186
187 #ifdef GMX_DOUBLE
188     snew(ts.coords, fr->natoms*3);
189     if (fr->bV)
190     {
191         snew(ts.velocities, fr->natoms*3);
192     }
193 #else
194     ts.coords = (float*)fr->x;
195     if (fr->bV)
196     {
197         ts.velocities = (float*)fr->v;
198     }
199 #endif
200
201     rc = fr->vmdplugin->api->read_next_timestep(fr->vmdplugin->handle, fr->natoms, &ts);
202
203     if (rc < -1)
204     {
205         fprintf(stderr, "\nError reading input file (error code %d)\n", rc);
206     }
207     if (rc < 0)
208     {
209         fr->vmdplugin->api->close_file_read(fr->vmdplugin->handle);
210         return 0;
211     }
212
213 #ifdef GMX_DOUBLE
214     for (i = 0; i < fr->natoms; i++)
215     {
216         fr->x[i][0] = .1*ts.coords[i*3];
217         fr->x[i][1] = .1*ts.coords[i*3+1];
218         fr->x[i][2] = .1*ts.coords[i*3+2];
219         if (fr->bV)
220         {
221             fr->v[i][0] = .1*ts.velocities[i*3];
222             fr->v[i][1] = .1*ts.velocities[i*3+1];
223             fr->v[i][2] = .1*ts.velocities[i*3+2];
224         }
225     }
226     sfree(ts.coords);
227     if (fr->bV)
228     {
229         sfree(ts.velocities);
230     }
231 #else
232     for (i = 0; i < fr->natoms; i++)
233     {
234         svmul(.1, fr->x[i], fr->x[i]);
235         if (fr->bV)
236         {
237             svmul(.1, fr->v[i], fr->v[i]);
238         }
239     }
240 #endif
241
242     fr->bX   = 1;
243     fr->bBox = 1;
244     vec[0]   = .1*ts.A; vec[1] = .1*ts.B; vec[2] = .1*ts.C;
245     angle[0] = ts.alpha; angle[1] = ts.beta; angle[2] = ts.gamma;
246     matrix_convert(fr->box, vec, angle);
247     if (fr->vmdplugin->api->abiversion > 10)
248     {
249         fr->bTime = TRUE;
250         fr->time  = ts.physical_time;
251     }
252     else
253     {
254         fr->bTime = FALSE;
255     }
256
257
258     return 1;
259 }
260
261 static int load_vmd_library(const char *fn, t_gmxvmdplugin *vmdplugin)
262 {
263     char            pathname[GMX_PATH_MAX], filename[GMX_PATH_MAX];
264     const char     *pathenv;
265     const char     *err;
266     int             i;
267     int             ret = 0;
268     char            pathenv_buffer[GMX_PATH_MAX];
269 #ifndef GMX_NATIVE_WINDOWS
270     glob_t          globbuf;
271     const char     *defpath_suffix = "/plugins/*/molfile";
272     const char     *defpathenv     = GMX_VMD_PLUGIN_PATH;
273 #else
274     WIN32_FIND_DATA ffd;
275     HANDLE          hFind = INVALID_HANDLE_VALUE;
276     char            progfolder[GMX_PATH_MAX];
277     char            defpathenv[GMX_PATH_MAX];
278     const char     *defpath_suffix = "\\plugins\\WIN32\\molfile";
279     SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, progfolder);
280     sprintf(defpathenv, "%s\\University of Illinois\\VMD\\plugins\\WIN32\\molfile", progfolder);
281 #endif
282
283     vmdplugin->api      = NULL;
284     vmdplugin->filetype = strrchr(fn, '.');
285     if (!vmdplugin->filetype)
286     {
287         return 0;
288     }
289     vmdplugin->filetype++;
290
291     /* First look for an explicit path given at run time for the
292      * plugins, then an implicit run-time path, and finally for one
293      * given at configure time. This last might be hard-coded to the
294      * default for VMD installs. */
295     pathenv = getenv("VMD_PLUGIN_PATH");
296     if (pathenv == NULL)
297     {
298         pathenv = getenv("VMDDIR");
299         if (NULL == pathenv)
300         {
301             printf("\nNeither VMD_PLUGIN_PATH or VMDDIR set. ");
302             printf("Using default location:\n%s\n", defpathenv);
303             pathenv = defpathenv;
304         }
305         else
306         {
307             printf("\nVMD_PLUGIN_PATH no set, but VMDDIR is set. ");
308 #ifdef _MSC_VER
309             _snprintf_s(pathenv_buffer, sizeof(pathenv_buffer), _TRUNCATE, "%s%s", pathenv, defpath_suffix);
310 #else
311             snprintf(pathenv_buffer, sizeof(pathenv_buffer), "%s%s", pathenv, defpath_suffix);
312 #endif
313             printf("Using semi-default location:\n%s\n", pathenv_buffer);
314             pathenv = pathenv_buffer;
315         }
316     }
317     strncpy(pathname, pathenv, sizeof(pathname));
318 #ifndef GMX_NATIVE_WINDOWS
319     strcat(pathname, "/*.so");
320     glob(pathname, 0, NULL, &globbuf);
321     if (globbuf.gl_pathc == 0)
322     {
323         printf("\nNo VMD Plugins found\n"
324                "Set the environment variable VMD_PLUGIN_PATH to the molfile folder within the\n"
325                "VMD installation.\n"
326                "The architecture (e.g. 32bit versus 64bit) of Gromacs and VMD has to match.\n");
327         return 0;
328     }
329     for (i = 0; i < globbuf.gl_pathc && vmdplugin->api == NULL; i++)
330     {
331         /* FIXME: Undefined which plugin is chosen if more than one plugin
332            can read a certain file ending. Requires some additional command
333            line option or enviroment variable to specify which plugin should
334            be picked.
335          */
336         ret |= load_sharedlibrary_plugins(globbuf.gl_pathv[i], vmdplugin);
337     }
338     globfree(&globbuf);
339 #else
340     strcat(pathname, "\\*.so");
341     hFind = FindFirstFile(pathname, &ffd);
342     if (INVALID_HANDLE_VALUE == hFind)
343     {
344         printf("\nNo VMD Plugins found\n");
345         return 0;
346     }
347     do
348     {
349         sprintf(filename, "%s\\%s", pathenv, ffd.cFileName);
350         ret |= load_sharedlibrary_plugins(filename, vmdplugin);
351     }
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 == NULL)
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
389 int read_first_vmd_frame(const char *fn, t_trxframe *fr)
390 {
391     molfile_timestep_metadata_t *metadata = NULL;
392
393     snew(fr->vmdplugin, 1);
394     if (!load_vmd_library(fn, fr->vmdplugin))
395     {
396         return 0;
397     }
398
399     fr->vmdplugin->handle = fr->vmdplugin->api->open_file_read(fn, fr->vmdplugin->filetype, &fr->natoms);
400
401     if (!fr->vmdplugin->handle)
402     {
403         fprintf(stderr, "\nError: could not open file '%s' for reading.\n",
404                 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",
421                 fr->natoms, fn );
422         return 0;
423     }
424
425     snew(fr->x, fr->natoms);
426
427     fr->vmdplugin->bV = 0;
428     if (fr->vmdplugin->api->abiversion > 10 && fr->vmdplugin->api->read_timestep_metadata)
429     {
430         fr->vmdplugin->api->read_timestep_metadata(fr->vmdplugin->handle, metadata);
431         assert(metadata);
432         fr->vmdplugin->bV = metadata->has_velocities;
433         if (fr->vmdplugin->bV)
434         {
435             snew(fr->v, fr->natoms);
436         }
437     }
438     else
439     {
440         fprintf(stderr,
441                 "\nThis trajectory is being read with a VMD plug-in from before VMD"
442                 "\nversion 1.8, or from a trajectory that lacks time step metadata."
443                 "\nEither way, GROMACS cannot tell whether the trajectory has velocities.\n");
444     }
445     return 1;
446
447 }