Merge release-4-6 into master
[alexxy/gromacs.git] / src / programs / view / manager.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gyas ROwers Mature At Cryogenic Speed
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <sysstuff.h>
40 #include <string.h>
41 #include <smalloc.h>
42 #include <ctype.h>
43 #include <typedefs.h>
44 #include <smalloc.h>
45 #include "gromacs/fileio/tpxio.h"
46 #include <macros.h>
47 #include <maths.h>
48 #include <atomprop.h>
49 #include <names.h>
50 #include "manager.h"
51 #include "gromacs/fileio/futil.h"
52 #include "pbc.h"
53 #include "nmol.h"
54 #include "copyrite.h"
55 #include "gstat.h"
56 #include "vec.h"
57 #include "statutil.h"
58 #include "string2.h"
59
60 static void add_object(t_manager *man, eObject eO, atom_id ai, atom_id aj)
61 {
62     srenew(man->obj, ++man->nobj);
63     man->obj[man->nobj-1].eO    = eO;
64     man->obj[man->nobj-1].eV    = eVNormal;
65     man->obj[man->nobj-1].color = WHITE;
66     man->obj[man->nobj-1].ai    = ai;
67     man->obj[man->nobj-1].aj    = aj;
68     man->obj[man->nobj-1].z     = 0.0;
69 }
70
71 static void add_bonds(t_manager *man, t_functype func[],
72                       t_ilist *b, bool bB[])
73 {
74     bool        *bH = man->bHydro;
75     t_iatom     *ia;
76     t_iatom      type, ai, aj, ak;
77     int          i, delta, ftype;
78
79 #ifdef DEBUG
80     fprintf(stderr, "Going to make bonds from an ilist with %d entries\n", b->nr);
81 #endif
82     ia = b->iatoms;
83     for (i = 0; (i < b->nr); )
84     {
85         type  = ia[0];
86         ai    = ia[1];
87         ftype = func[type];
88         delta = interaction_function[ftype].nratoms;
89
90         if (ftype == F_SETTLE)
91         {
92             aj     = ia[2];
93             ak     = ia[3];
94             bB[ai] = bB[aj] = bB[ak] = true;
95             add_object(man, eOHBond, ai, aj);
96             add_object(man, eOHBond, ai, ak);
97         }
98         else if (IS_CHEMBOND(ftype))
99         {
100             aj = ia[2];
101 #ifdef DEBUG
102             fprintf(stderr, "Adding bond from %d to %d\n", ai, aj);
103 #endif
104             bB[ai] = bB[aj] = true;
105             if (!(bH[ai] == bH[aj]))
106             {
107                 add_object(man, eOHBond, ai, aj);
108             }
109             else if (!bH[ai] && !bH[aj])
110             {
111                 add_object(man, eOBond, ai, aj);
112             }
113         }
114 #ifdef DEBUG
115         fprintf(stderr, "Type: %5d, delta: %5d\n", type, delta);
116 #endif
117         ia += delta+1;
118         i  += delta+1;
119     }
120 }
121
122 static void add_bpl(t_manager *man, t_idef *idef, bool bB[])
123 {
124     int ftype;
125
126     for (ftype = 0; ftype < F_NRE; ftype++)
127     {
128         if (IS_CHEMBOND(ftype) || ftype == F_SETTLE)
129         {
130             add_bonds(man, idef->functype, &idef->il[ftype], bB);
131         }
132     }
133 }
134
135 static atom_id which_atom(t_manager *man, int x, int y)
136 {
137 #define DELTA 5
138     int  i;
139     iv2 *ix = man->ix;
140
141     for (i = 0; (i < man->natom); i++)
142     {
143         if ((abs(ix[i][XX]-x) < DELTA) && (abs(ix[i][YY]-y) < DELTA))
144         {
145             if (man->bVis[i])
146             {
147                 return (atom_id) i;
148             }
149         }
150     }
151     return NO_ATID;
152 }
153
154 static void do_label(t_x11 *x11, t_manager *man, int x, int y, bool bSet)
155 {
156     atom_id         ai;
157     unsigned long   col;
158
159     if ((ai = which_atom(man, x, y)) != NO_ATID)
160     {
161         x = man->ix[ai][XX];
162         y = man->ix[ai][YY];
163         if (bSet && !man->bLabel[ai])
164         {
165             col             = WHITE;
166             man->bLabel[ai] = true;
167         }
168         else if (!bSet && man->bLabel[ai])
169         {
170             col             = BLUE;
171             man->bLabel[ai] = false;
172         }
173         else
174         {
175             return;
176         }
177         XSetForeground(x11->disp, x11->gc, col);
178         XDrawString(x11->disp, man->molw->wd.self, x11->gc, x+2, y-2, man->szLab[ai],
179                     strlen(man->szLab[ai]));
180         XSetForeground(x11->disp, x11->gc, x11->fg);
181     }
182 }
183
184 static void show_label(t_x11 *x11, t_manager *man, int x, int y)
185 {
186     do_label(x11, man, x, y, true);
187 }
188
189 static void hide_label(t_x11 *x11, t_manager *man, int x, int y)
190 {
191     do_label(x11, man, x, y, false);
192 }
193
194 void set_file(t_x11 *x11, t_manager *man, const char *trajectory,
195               const char *status)
196 {
197     gmx_atomprop_t    aps;
198     char              buf[256], quote[256];
199     t_tpxheader       sh;
200     t_atoms          *at;
201     bool             *bB;
202     int               i;
203
204     read_tpxheader(status, &sh, true, NULL, NULL);
205     snew(man->ix, sh.natoms);
206     snew(man->zz, sh.natoms);
207     snew(man->col, sh.natoms);
208     snew(man->size, sh.natoms);
209     snew(man->vdw, sh.natoms);
210     snew(man->bLabel, sh.natoms);
211     snew(man->bVis, sh.natoms);
212     for (i = 0; (i < sh.natoms); i++)
213     {
214         man->bVis[i] = false;
215     }
216
217     man->bPbc = false;
218
219     snew(man->szLab, sh.natoms);
220     snew(man->bHydro, sh.natoms);
221     snew(bB, sh.natoms);
222     read_tpx_top(status, NULL, man->box, &man->natom, NULL, NULL, NULL, &man->top);
223     man->gpbc = gmx_rmpbc_init(&man->top.idef, -1, man->natom);
224
225     man->natom =
226         read_first_x(man->oenv, &man->status, trajectory, &(man->time), &(man->x),
227                      man->box);
228     man->trajfile = strdup(trajectory);
229     if (man->natom > man->top.atoms.nr)
230     {
231         gmx_fatal(FARGS, "Topology %s (%d atoms) and trajectory %s (%d atoms) "
232                   "do not match", status, man->top.atoms.nr,
233                   trajectory, man->natom);
234     }
235
236     cool_quote(quote, 255, NULL);
237     sprintf(buf, "%s: %s", *man->top.name, quote);
238     man->title.text = strdup(buf);
239     man->view       = init_view(man->box);
240     at              = &(man->top.atoms);
241     aps             = gmx_atomprop_init();
242     for (i = 0; (i < man->natom); i++)
243     {
244         char      *aname = *(at->atomname[i]);
245         t_resinfo *ri    = &at->resinfo[at->atom[i].resind];
246
247         man->col[i] = Type2Color(aname);
248         snew(man->szLab[i], 20);
249         if (ri->ic != ' ')
250         {
251             sprintf(man->szLab[i], "%s%d%c, %s", *ri->name, ri->nr, ri->ic, aname);
252         }
253         else
254         {
255             sprintf(man->szLab[i], "%s%d, %s", *ri->name, ri->nr, aname);
256         }
257         man->bHydro[i] = (toupper(aname[0]) == 'H');
258         if (man->bHydro[i])
259         {
260             man->vdw[i] = 0;
261         }
262         else if (!gmx_atomprop_query(aps, epropVDW, *ri->name, aname, &(man->vdw[i])))
263         {
264             man->vdw[i] = 0;
265         }
266     }
267     gmx_atomprop_destroy(aps);
268     add_bpl(man, &(man->top.idef), bB);
269     for (i = 0; (i < man->natom); i++)
270     {
271         if (!bB[i])
272         {
273             add_object(man, eOSingle, (atom_id) i, 0);
274         }
275     }
276     sfree(bB);
277
278     ExposeWin(x11->disp, man->molw->wd.self);
279 }
280
281 void step_message(t_x11 *x11, t_manager *man)
282 {
283     XEvent letter;
284
285     letter.type                 = ClientMessage;
286     letter.xclient.display      = x11->disp;
287     letter.xclient.window       = man->wd.self;
288     letter.xclient.message_type = 0;
289     letter.xclient.format       = 32;
290     letter.xclient.data.l[0]    = IDSTEP;
291     letter.xclient.data.l[1]    = Button1;
292     XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
293 }
294
295 static void reset_mols(t_block *mols, matrix box, rvec x[])
296 {
297     int  i, m0, m1, j, m;
298     rvec xcm, icm;
299     real ix, iy, iz;
300
301     for (i = 0; (i < mols->nr); i++)
302     {
303         m0 = mols->index[i];
304         m1 = mols->index[i+1];
305
306         clear_rvec(xcm);
307         clear_rvec(icm);
308
309         for (j = m0; (j < m1); j++)
310         {
311             rvec_inc(xcm, x[j]);
312         }
313         for (m = 0; (m < DIM); m++)
314         {
315             xcm[m] /= (m1-m0);
316         }
317         for (m = 0; (m < DIM); m++)
318         {
319             if (xcm[m] < 0)
320             {
321                 icm[m] = box[m][m];
322             }
323             else if (xcm[m] >= box[m][m])
324             {
325                 icm[m] = -box[m][m];
326             }
327         }
328         ix = icm[XX], iy = icm[YY], iz = icm[ZZ];
329
330         if ((ix != 0) || (iy != 0) || (iz != 0))
331         {
332             for (j = m0; (j < m1); j++)
333             {
334                 x[j][XX] += ix;
335                 x[j][YY] += iy;
336                 x[j][ZZ] += iz;
337             }
338         }
339     }
340 }
341
342 static bool step_man(t_manager *man, int *nat)
343 {
344     static int      ncount = 0;
345     static bool     bWarn  = false;
346     bool            bEof;
347     const char     *warn;
348
349     if (!man->natom)
350     {
351         fprintf(stderr, "Not initiated yet!");
352         exit(1);
353     }
354     bEof = read_next_x(man->oenv, man->status, &man->time, man->x, man->box);
355     *nat = man->natom;
356     if (ncount == man->nSkip)
357     {
358         switch (man->molw->boxtype)
359         {
360             case esbTri:
361                 put_atoms_in_triclinic_unitcell(ecenterDEF, man->box, man->natom, man->x);
362                 break;
363             case esbTrunc:
364                 warn = put_atoms_in_compact_unitcell(man->molw->ePBC, ecenterDEF, man->box,
365                                                      man->natom, man->x);
366                 if (warn && !bWarn)
367                 {
368                     fprintf(stderr, "\n%s\n", warn);
369                     bWarn = true;
370                 }
371                 break;
372             case esbRect:
373             case esbNone:
374             default:
375                 break;
376         }
377         if (man->bPbc)
378         {
379             gmx_rmpbc(man->gpbc, man->natom, man->box, man->x);
380             reset_mols(&(man->top.mols), man->box, man->x);
381         }
382         ncount = 0;
383     }
384     else
385     {
386         if (man->nSkip > 0)
387         {
388             ncount++;
389             return step_man(man, nat);
390         }
391     }
392
393     return bEof;
394 }
395
396 static void HandleClient(t_x11 *x11, t_manager *man, long data[])
397 {
398     int  ID, button, x, y;
399     bool bPos;
400     real fac;
401
402     ID     = data[0];
403     button = data[1];
404     x      = data[2];
405     y      = data[3];
406     bPos   = (button == Button1);
407     switch (ID)
408     {
409         case IDROTX:
410         case IDROTY:
411         case IDROTZ:
412             rotate_3d(man->view, ID-IDROTX, bPos);
413             draw_mol(x11, man);
414             break;
415         case IDZOOM:
416             if (bPos)
417             {
418                 fac = 0.8; /* Reduce distance between eye and origin */
419             }
420             else
421             {
422                 fac = 1.25;
423             }
424
425             /*  zoom changed to scale by Berk Hess 3-7-96
426                if (zoom_3d(man->view,fac))
427                draw_mol(x11,man); */
428             man->view->sc_x /= fac;
429             man->view->sc_y /= fac;
430             draw_mol(x11, man);
431             break;
432         case IDTRANSX:
433         case IDTRANSY:
434         case IDTRANSZ:
435             translate_view(man->view, ID-IDTRANSX, bPos);
436             draw_mol(x11, man);
437             break;
438         case IDREWIND:
439             if (man->status)
440             {
441                 rewind_trj(man->status);
442                 read_next_x(man->oenv, man->status, &(man->time), man->x,
443                             man->box);
444                 man->bEof = false;
445                 draw_mol(x11, man);
446             }
447             break;
448         case IDSTEP:
449         {
450             int      nat;
451
452             nat = 0;
453             if (!step_man(man, &nat))
454             {
455                 man->bEof  = true;
456                 man->bStop = true;
457             }
458             else
459             {
460                 if (nat > 0)
461                 {
462                     draw_mol(x11, man);
463                     usleep(man->nWait*1000);
464                 }
465             }
466             break;
467         }
468         case IDFF:
469             man->bStop = false;
470             break;
471         case IDSTOP_ANI:
472             man->bStop = true;
473             break;
474         case IDDRAWMOL:
475             draw_mol(x11, man);
476             break;
477         case IDLABEL:
478             switch (button)
479             {
480                 case Button1:
481                 case Button2:
482                     show_label(x11, man, x, y);
483                     break;
484                 case Button3:
485                     hide_label(x11, man, x, y);
486                     break;
487             }
488             break;
489         default:
490             break;
491     }
492     if (man->bAnimate && !man->bEof && !man->bStop)
493     {
494         step_message(x11, man);
495     }
496 }
497
498 static bool TitleCallBack(t_x11 *x11, XEvent *event, Window w, void *data)
499 {
500     t_windata *wd;
501
502     wd = (t_windata *)data;
503     switch (event->type)
504     {
505         case Expose:
506             if (wd->text && (wd->width > 10))
507             {
508                 XSetForeground(x11->disp, x11->gc, WHITE);
509                 TextInWin(x11, wd, wd->text, eXCenter, eYCenter);
510                 XDrawLine(x11->disp, wd->self, x11->gc, 0, wd->height,
511                           wd->width, wd->height);
512             }
513             break;
514         case ConfigureNotify:
515             wd->width  = event->xconfigure.width;
516             wd->height = event->xconfigure.height;
517             break;
518     }
519     return false;
520 }
521
522 static bool ManCallBack(t_x11 *x11, XEvent *event, Window w, void *data)
523 {
524     t_manager *man;
525     int        width, height;
526
527     man = (t_manager *)data;
528     switch (event->type)
529     {
530         case ConfigureNotify:
531             width  = event->xconfigure.width;
532             height = event->xconfigure.height;
533             if ((width != man->wd.width) || (height != man->wd.height))
534             {
535                 move_man(x11, man, width, height);
536             }
537             break;
538         case ClientMessage:
539             HandleClient(x11, man, event->xclient.data.l);
540             break;
541         default:
542             break;
543     }
544     return false;
545 }
546
547 void no_labels(t_x11 *x11, t_manager *man)
548 {
549     int i;
550
551     for (i = 0; (i < man->natom); i++)
552     {
553         man->bLabel[i] = false;
554     }
555     draw_mol(x11, man);
556 }
557
558 void move_man(t_x11 *x11, t_manager *man, int width, int height)
559 {
560     int x0, y0, mw, mh, hb;
561     int th;
562
563 #ifdef DEBUG
564     fprintf(stderr, "Move manager %dx%d\n", width, height);
565 #endif
566     man->wd.width  = width;
567     man->wd.height = height;
568
569     /* Move all subwindows, resize only Mol window */
570     x0 = width-EWIDTH-AIR-4*BORDER;           /* Starting of ewin etc. */
571     y0 = AIR;
572
573     /* Mol Window */
574     mw = x0-2*AIR-4*BORDER;
575     mh = height-y0-AIR-2*BORDER;
576     XMoveResizeWindow(x11->disp, man->molw->wd.self, AIR, y0, mw, mh);
577
578     /* Title Window */
579     th = XTextHeight(x11->font);
580     XMoveResizeWindow(x11->disp, man->title.self, 0, 0, mw, th+AIR);
581
582     /* Legend Window */
583     XMoveResizeWindow(x11->disp, man->legw->wd.self, x0, y0, EWIDTH, LEGHEIGHT);
584     y0 += LEGHEIGHT+AIR+2*BORDER;
585
586     if (y0 > height)
587     {
588         printf("Error: Windows falling out of main window!\n");
589     }
590
591     /* Button Box */
592     hb = height-y0-AIR-2*BORDER;
593     XMoveResizeWindow(x11->disp, man->bbox->wd.self, x0, y0, EWIDTH, hb);
594
595     /* Video Box */
596     x0 = (mw-man->vbox->wd.width)/2;
597     y0 = (mh-2-AIR-man->vbox->wd.height);
598     XMoveWindow(x11->disp, man->vbox->wd.self, x0, y0);
599 }
600
601 void map_man(t_x11 *x11, t_manager *man)
602 {
603     XMapWindow(x11->disp, man->wd.self);
604     map_mw(x11, man->molw);
605     XMapWindow(x11->disp, man->title.self);
606     map_legw(x11, man->legw);
607     show_but(x11, man->bbox);
608 }
609
610 bool toggle_animate (t_x11 *x11, t_manager *man)
611 {
612     if (man->status)
613     {
614         man->bAnimate = !man->bAnimate;
615         man->bStop    = true;
616         man->bEof     = false;
617         if (man->bAnimate)
618         {
619             show_but(x11, man->vbox);
620         }
621         else
622         {
623             hide_but(x11, man->vbox);
624         }
625     }
626     return man->bAnimate;
627 }
628
629 bool toggle_pbc (t_manager *man)
630 {
631     man->bPbc = !man->bPbc;
632
633     return man->bPbc;
634 }
635
636
637 t_manager *init_man(t_x11 *x11, Window Parent,
638                     int x, int y, int width, int height,
639                     unsigned long fg, unsigned long bg,
640                     int ePBC, matrix box,
641                     const output_env_t oenv)
642 {
643     t_manager *man;
644
645     snew(man, 1);
646     man->status = NULL;
647     man->bPlus  = true;
648     man->bSort  = true;
649     man->oenv   = oenv;
650     InitWin(&(man->wd), x, y, width, height, 0, "Manager");
651     man->wd.self = XCreateSimpleWindow(x11->disp, Parent, man->wd.x, man->wd.y,
652                                        man->wd.width, man->wd.height,
653                                        man->wd.bwidth, fg, bg);
654     x11->RegisterCallback(x11, man->wd.self, Parent, ManCallBack, man);
655     x11->SetInputMask(x11, man->wd.self, StructureNotifyMask |
656                       ExposureMask | ButtonPressMask);
657
658     /* The order of creating windows is important for the stacking order */
659     /* Mol Window */
660     man->molw = init_mw(x11, man->wd.self, 0, 0, 1, 1, WHITE, BLUE, ePBC, box);
661
662     /* Title Window */
663     InitWin(&(man->title), 0, 0, 1, 1, 0, NULL);
664     man->title.self = XCreateSimpleWindow(x11->disp, man->molw->wd.self,
665                                           man->title.x, man->title.y,
666                                           man->title.width, man->title.height,
667                                           man->title.bwidth, WHITE, BLUE);
668     x11->RegisterCallback(x11, man->title.self, man->molw->wd.self,
669                           TitleCallBack, &(man->title));
670     x11->SetInputMask(x11, man->title.self, ExposureMask | StructureNotifyMask);
671
672     /* Button box */
673     man->bbox = init_bbox(x11, man->wd.self, man->wd.self, 1, WHITE, BLUE);
674
675     /* Legend Window */
676     man->legw = init_legw(x11, man->wd.self, 0, 0, EWIDTH, LEGHEIGHT, WHITE, BLUE);
677
678     /* Video Box */
679     man->vbox = init_vbox(x11, man->molw->wd.self, man->wd.self, WHITE, BLUE);
680
681     return man;
682 }
683
684 void done_man(t_x11 *x11, t_manager *man)
685 {
686     done_bbox(x11, man->vbox);
687     done_bbox(x11, man->bbox);
688     done_mw(x11, man->molw);
689     done_legw(x11, man->legw);
690     x11->UnRegisterCallback(x11, man->title.self);
691     x11->UnRegisterCallback(x11, man->wd.self);
692     sfree(man->x);
693     sfree(man->obj);
694     sfree(man->bHydro);
695     sfree(man->bLabel);
696     sfree(man->szLab);
697     sfree(man->col);
698     sfree(man);
699 }
700
701 void do_filter(t_x11 *x11, t_manager *man, t_filter *filter)
702 {
703     int      i;
704     atom_id  j;
705
706     for (i = 0; (i < man->natom); i++)
707     {
708         man->bVis[i] = false;
709     }
710     for (i = 0; (i < filter->grps->nr); i++)
711     {
712         if (filter->bShow[i])
713         {
714             for (j = filter->grps->index[i]; (j < filter->grps->index[i+1]); j++)
715             {
716                 man->bVis[filter->grps->a[j]] = true;
717             }
718         }
719     }
720
721     ExposeWin(x11->disp, man->wd.self);
722 }