fixed dispersion correction with parallel EM
[gromacs.git] / src / gmxlib / vmdio.c
blob15a868e9967c7a8b07d2ba4b5fb2630f4f6e83d3
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
3 *
4 * This file is part of Gromacs Copyright (c) 1991-2008
5 * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * To help us fund GROMACS development, we humbly ask that you cite
13 * the research papers on the package. Check out http://www.gromacs.org
15 * And Hey:
16 * Gnomes, ROck Monsters And Chili Sauce
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
24 /* Derived from PluginMgr.C and catdcd.c */
26 /* PluginMgr.C: Copyright: */
27 /***************************************************************************
28 *cr
29 *cr (C) Copyright 1995-2009 The Board of Trustees of the
30 *cr University of Illinois
31 *cr All Rights Reserved
32 *cr
33 Developed by: Theoretical and Computational Biophysics Group
34 University of Illinois at Urbana-Champaign
35 http://www.ks.uiuc.edu/
37 Permission is hereby granted, free of charge, to any person obtaining a copy of
38 this software and associated documentation files (the Software), to deal with
39 the Software without restriction, including without limitation the rights to
40 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
41 of the Software, and to permit persons to whom the Software is furnished to
42 do so, subject to the following conditions:
44 Redistributions of source code must retain the above copyright notice,
45 this list of conditions and the following disclaimers.
47 Redistributions in binary form must reproduce the above copyright notice,
48 this list of conditions and the following disclaimers in the documentation
49 and/or other materials provided with the distribution.
51 Neither the names of Theoretical and Computational Biophysics Group,
52 University of Illinois at Urbana-Champaign, nor the names of its contributors
53 may be used to endorse or promote products derived from this Software without
54 specific prior written permission.
56 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
59 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
60 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
61 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
62 OTHER DEALINGS WITH THE SOFTWARE.
63 ***************************************************************************/
65 /* catdcd.c: Copyright: */
66 /*****************************************************************************/
67 /* */
68 /* (C) Copyright 2001-2005 Justin Gullingsrud and the University of Illinois.*/
69 /* */
70 /*****************************************************************************/
72 #include <stdlib.h>
73 #include <stdio.h>
74 #include <string.h>
76 /*
77 * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
79 #include "molfile_plugin.h"
80 #include "vmddlopen.h"
81 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
82 #include "glob.h"
83 #else
84 #include <windows.h>
85 #include <shlobj.h>
86 #endif
87 #include "smalloc.h"
88 #include "futil.h"
89 #include "vmdio.h"
92 #include "types/simple.h"
93 #include "vec.h"
94 #include "gmxfio.h"
97 typedef int (*initfunc)(void);
98 typedef int (*regfunc)(void *, vmdplugin_register_cb);
99 typedef int (*finifunc)(void);
103 static int register_cb(void *v, vmdplugin_t *p) {
104 const char *key = p->name;
105 t_gmxvmdplugin *vmdplugin = (t_gmxvmdplugin*)v;
107 if (strcmp(key,vmdplugin->filetype)==0)
109 vmdplugin->api = (molfile_plugin_t *)p;
111 return VMDPLUGIN_SUCCESS;
114 static int load_sharedlibrary_plugins(const char *fullpath,t_gmxvmdplugin* vmdplugin) {
115 /* Open the dll; try to execute the init function. */
116 void *handle, *ifunc, *registerfunc;
117 handle = vmddlopen(fullpath);
118 if (!handle) {
119 if (debug) fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n", fullpath, vmddlerror()); /*only to debug because of stdc++ erros */
120 return 0;
123 ifunc = vmddlsym(handle, "vmdplugin_init");
124 if (ifunc && ((initfunc)(ifunc))()) {
125 printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
126 vmddlclose(handle);
127 return 0;
130 registerfunc = vmddlsym(handle, "vmdplugin_register");
131 if (!registerfunc) {
132 printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
133 vmddlclose(handle);
134 return 0;
135 } else {
136 /* Load plugins from the library.*/
137 ((regfunc)registerfunc)(vmdplugin, register_cb);
140 /* in case this library does not support the filetype, close it */
141 if (vmdplugin->api == NULL)
143 vmddlclose(handle);
146 return 1;
149 /*return: 1: success, 0: last frame, -1: error*/
150 gmx_bool read_next_vmd_frame(int status,t_trxframe *fr)
152 int rc,i;
153 rvec vec, angle;
154 molfile_timestep_t ts;
157 fr->bV = fr->vmdplugin.bV;
159 #ifdef GMX_DOUBLE
160 snew(ts.coords, fr->natoms*3);
161 if (fr->bV)
163 snew(ts.velocities, fr->natoms*3);
165 #else
166 ts.coords = (float*)fr->x;
167 if (fr->bV)
169 ts.velocities = (float*)fr->v;
171 #endif
173 rc = fr->vmdplugin.api->read_next_timestep(fr->vmdplugin.handle, fr->natoms, &ts);
175 if (rc < -1) {
176 fprintf(stderr, "\nError reading input file (error code %d)\n", rc);
178 if (rc < 0)
180 fr->vmdplugin.api->close_file_read(fr->vmdplugin.handle);
181 return 0;
184 #ifdef GMX_DOUBLE
185 for (i=0;i<fr->natoms;i++)
187 fr->x[i][0] = .1*ts.coords[i*3];
188 fr->x[i][1] = .1*ts.coords[i*3+1];
189 fr->x[i][2] = .1*ts.coords[i*3+2];
190 if (fr->bV)
192 fr->v[i][0] = .1*ts.velocities[i*3];
193 fr->v[i][1] = .1*ts.velocities[i*3+1];
194 fr->v[i][2] = .1*ts.velocities[i*3+2];
197 sfree(ts.coords);
198 if (fr->bV)
200 sfree(ts.velocities);
202 #else
203 for (i=0;i<fr->natoms;i++)
205 svmul(.1,fr->x[i],fr->x[i]);
206 if (fr->bV)
208 svmul(.1,fr->v[i],fr->v[i]);
211 #endif
213 fr->bX = 1;
214 fr->bBox = 1;
215 vec[0] = .1*ts.A; vec[1] = .1*ts.B; vec[2] = .1*ts.C;
216 angle[0] = ts.alpha; angle[1] = ts.beta; angle[2] = ts.gamma;
217 matrix_convert(fr->box,vec,angle);
218 if (fr->vmdplugin.api->abiversion>10)
220 fr->bTime = TRUE;
221 fr->time = ts.physical_time;
223 else
225 fr->bTime = FALSE;
229 return 1;
232 int load_vmd_library(const char *fn, t_gmxvmdplugin *vmdplugin)
234 char pathname[GMX_PATH_MAX],filename[GMX_PATH_MAX];
235 const char *pathenv;
236 const char *err;
237 int i;
238 int ret=0;
239 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
240 glob_t globbuf;
241 const char *defpathenv = "/usr/local/lib/vmd/plugins/*/molfile";
242 #else
243 WIN32_FIND_DATA ffd;
244 HANDLE hFind = INVALID_HANDLE_VALUE;
245 char progfolder[GMX_PATH_MAX];
246 char defpathenv[GMX_PATH_MAX];
247 SHGetFolderPath(NULL,CSIDL_PROGRAM_FILES,NULL,SHGFP_TYPE_CURRENT,progfolder);
248 sprintf(defpathenv,"%s\\University of Illinois\\VMD\\plugins\\WIN32\\molfile",progfolder);
249 #endif
251 vmdplugin->api = NULL;
252 vmdplugin->filetype = strrchr(fn,'.');
253 if (!vmdplugin->filetype)
255 return 0;
257 vmdplugin->filetype++;
259 pathenv = getenv("VMD_PLUGIN_PATH");
260 if (pathenv==NULL)
262 printf("\nVMD_PLUGIN_PATH not set. ");
263 printf("Using default location:\n%s\n",defpathenv);
264 pathenv=defpathenv;
266 strncpy(pathname,pathenv,sizeof(pathname));
267 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
268 strcat(pathname,"/*.so");
269 glob(pathname, 0, NULL, &globbuf);
270 if (globbuf.gl_pathc == 0)
272 printf("\nNo VMD Plugins found\n"
273 "Set the environment variable VMD_PLUGIN_PATH to the molfile folder within the\n"
274 "VMD installation.\n"
275 "The architecture (e.g. 32bit versus 64bit) of Gromacs and VMD has to match.\n");
276 return 0;
278 for (i=0; i<globbuf.gl_pathc && vmdplugin->api == NULL; i++)
280 ret|=load_sharedlibrary_plugins(globbuf.gl_pathv[i],vmdplugin);
282 globfree(&globbuf);
283 #else
284 strcat(pathname,"\\*.so");
285 hFind = FindFirstFile(pathname, &ffd);
286 if (INVALID_HANDLE_VALUE == hFind)
288 printf("\nNo VMD Plugins found\n");
289 return 0;
293 sprintf(filename,"%s\\%s",pathenv,ffd.cFileName);
294 ret|=load_sharedlibrary_plugins(filename,vmdplugin);
296 while (FindNextFile(hFind, &ffd ) != 0 && vmdplugin->api == NULL );
297 FindClose(hFind);
298 #endif
300 if (!ret)
302 printf("\nCould not open any VMD library.\n");
303 err = vmddlerror();
304 if (!err)
306 printf("Compiled with dlopen?\n");
308 else
310 printf("Last error:\n%s\n",err);
312 return 0;
315 if (vmdplugin->api == NULL)
317 printf("\nNo plugin for %s found\n",vmdplugin->filetype);
318 return 0;
321 printf("\nUsing VMD plugin: %s (%s)\n",vmdplugin->api->name,vmdplugin->api->prettyname);
323 return 1;
327 int read_first_vmd_frame(int *status,const char *fn,t_trxframe *fr,int flags)
329 molfile_timestep_metadata_t *metadata=NULL;
331 if (!load_vmd_library(fn,&(fr->vmdplugin)))
333 return 0;
336 fr->vmdplugin.handle = fr->vmdplugin.api->open_file_read(fn, fr->vmdplugin.filetype, &fr->natoms);
338 if (!fr->vmdplugin.handle) {
339 fprintf(stderr, "\nError: could not open file '%s' for reading.\n",
340 fn);
341 return 0;
344 if (fr->natoms == MOLFILE_NUMATOMS_UNKNOWN) {
345 fprintf(stderr, "\nFormat of file %s does not record number of atoms.\n", fn);
346 return 0;
347 } else if (fr->natoms == MOLFILE_NUMATOMS_NONE) {
348 fprintf(stderr, "\nNo atoms found by VMD plugin in file %s.\n", fn );
349 return 0;
350 } else if (fr->natoms < 1) { /*should not be reached*/
351 fprintf(stderr, "\nUnknown number of atoms %d for VMD plugin opening file %s.\n",
352 fr->natoms, fn );
353 return 0;
356 snew(fr->x,fr->natoms);
358 fr->vmdplugin.bV = 0;
359 if (fr->vmdplugin.api->abiversion > 10 && fr->vmdplugin.api->read_timestep_metadata)
361 fr->vmdplugin.api->read_timestep_metadata(fr->vmdplugin.handle, metadata);
362 fr->vmdplugin.bV = metadata->has_velocities;
363 if (fr->vmdplugin.bV)
365 snew(fr->v,fr->natoms);
368 return 1;