2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
51 # include <unistd.h> // for usleep()
54 #include "gromacs/fileio/tpxio.h"
55 #include "gromacs/math/utilities.h"
56 #include "gromacs/math/vec.h"
57 #include "gromacs/mdtypes/md_enums.h"
58 #include "gromacs/pbcutil/pbc.h"
59 #include "gromacs/topology/atomprop.h"
60 #include "gromacs/topology/ifunc.h"
61 #include "gromacs/utility/coolstuff.h"
62 #include "gromacs/utility/cstringutil.h"
63 #include "gromacs/utility/fatalerror.h"
64 #include "gromacs/utility/futil.h"
65 #include "gromacs/utility/smalloc.h"
66 #include "gromacs/utility/stringutil.h"
71 static void add_object(t_manager* man, eObject eO, int ai, int aj)
73 srenew(man->obj, ++man->nobj);
74 man->obj[man->nobj - 1].eO = eO;
75 man->obj[man->nobj - 1].eV = eVNormal;
76 man->obj[man->nobj - 1].color = WHITE;
77 man->obj[man->nobj - 1].ai = ai;
78 man->obj[man->nobj - 1].aj = aj;
79 man->obj[man->nobj - 1].z = 0.0;
82 static void add_bonds(t_manager* man, const t_functype func[], t_ilist* b, bool bB[])
84 bool* bH = man->bHydro;
86 t_iatom type, ai, aj, ak;
90 std::fprintf(stderr, "Going to make bonds from an ilist with %d entries\n", b->nr);
93 for (i = 0; (i < b->nr);)
98 delta = interaction_function[ftype].nratoms;
100 if (ftype == F_SETTLE)
104 bB[ai] = bB[aj] = bB[ak] = true;
105 add_object(man, eOHBond, ai, aj);
106 add_object(man, eOHBond, ai, ak);
108 else if (IS_CHEMBOND(ftype))
112 std::fprintf(stderr, "Adding bond from %d to %d\n", ai, aj);
114 bB[ai] = bB[aj] = true;
115 if (!(bH[ai] == bH[aj]))
117 add_object(man, eOHBond, ai, aj);
119 else if (!bH[ai] && !bH[aj])
121 add_object(man, eOBond, ai, aj);
125 std::fprintf(stderr, "Type: %5d, delta: %5d\n", type, delta);
132 static void add_bpl(t_manager* man, t_idef* idef, bool bB[])
136 for (ftype = 0; ftype < F_NRE; ftype++)
138 if (IS_CHEMBOND(ftype) || ftype == F_SETTLE)
140 add_bonds(man, idef->functype, &idef->il[ftype], bB);
145 static int which_atom(t_manager* man, int x, int y)
151 for (i = 0; (i < man->natom); i++)
153 if ((std::abs(ix[i][XX] - x) < DELTA) && (std::abs(ix[i][YY] - y) < DELTA))
164 static void do_label(t_x11* x11, t_manager* man, int x, int y, bool bSet)
169 if ((ai = which_atom(man, x, y)) != -1)
173 if (bSet && !man->bLabel[ai])
176 man->bLabel[ai] = true;
178 else if (!bSet && man->bLabel[ai])
181 man->bLabel[ai] = false;
187 XSetForeground(x11->disp, x11->gc, col);
188 XDrawString(x11->disp, man->molw->wd.self, x11->gc, x + 2, y - 2, man->szLab[ai],
189 std::strlen(man->szLab[ai]));
190 XSetForeground(x11->disp, x11->gc, x11->fg);
194 static void show_label(t_x11* x11, t_manager* man, int x, int y)
196 do_label(x11, man, x, y, true);
199 static void hide_label(t_x11* x11, t_manager* man, int x, int y)
201 do_label(x11, man, x, y, false);
204 void set_file(t_x11* x11, t_manager* man, const char* trajectory, const char* status)
210 TpxFileHeader sh = readTpxHeader(status, true);
211 snew(man->ix, sh.natoms);
212 snew(man->zz, sh.natoms);
213 snew(man->col, sh.natoms);
214 snew(man->size, sh.natoms);
215 snew(man->vdw, sh.natoms);
216 snew(man->bLabel, sh.natoms);
217 snew(man->bVis, sh.natoms);
218 for (i = 0; (i < sh.natoms); i++)
220 man->bVis[i] = false;
225 snew(man->szLab, sh.natoms);
226 snew(man->bHydro, sh.natoms);
228 read_tpx_top(status, nullptr, man->box, &man->natom, nullptr, nullptr, &man->top);
229 man->gpbc = gmx_rmpbc_init(&man->top.idef, -1, man->natom);
231 man->natom = read_first_x(man->oenv, &man->status, trajectory, &(man->time), &(man->x), man->box);
232 man->trajfile = gmx_strdup(trajectory);
233 if (man->natom > man->top.atoms.nr)
236 "Topology %s (%d atoms) and trajectory %s (%d atoms) "
238 status, man->top.atoms.nr, trajectory, man->natom);
242 gmx_strdup(gmx::formatString("%s: %s", *man->top.name, gmx::getCoolQuote().c_str()).c_str());
243 man->view = init_view(man->box);
244 at = &(man->top.atoms);
246 for (i = 0; (i < man->natom); i++)
248 char* aname = *(at->atomname[i]);
249 t_resinfo* ri = &at->resinfo[at->atom[i].resind];
251 man->col[i] = Type2Color(aname);
252 snew(man->szLab[i], 20);
255 std::sprintf(man->szLab[i], "%s%d%c, %s", *ri->name, ri->nr, ri->ic, aname);
259 std::sprintf(man->szLab[i], "%s%d, %s", *ri->name, ri->nr, aname);
261 man->bHydro[i] = (toupper(aname[0]) == 'H');
266 else if (!aps.setAtomProperty(epropVDW, *ri->name, aname, &(man->vdw[i])))
271 add_bpl(man, &(man->top.idef), bB);
272 for (i = 0; (i < man->natom); i++)
276 add_object(man, eOSingle, i, 0);
281 ExposeWin(x11->disp, man->molw->wd.self);
284 void step_message(t_x11* x11, t_manager* man)
288 letter.type = ClientMessage;
289 letter.xclient.display = x11->disp;
290 letter.xclient.window = man->wd.self;
291 letter.xclient.message_type = 0;
292 letter.xclient.format = 32;
293 letter.xclient.data.l[0] = IDSTEP;
294 letter.xclient.data.l[1] = Button1;
295 XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
298 static void reset_mols(t_block* mols, matrix box, rvec x[])
304 for (i = 0; (i < mols->nr); i++)
307 m1 = mols->index[i + 1];
312 for (j = m0; (j < m1); j++)
316 for (m = 0; (m < DIM); m++)
320 for (m = 0; (m < DIM); m++)
326 else if (xcm[m] >= box[m][m])
331 ix = icm[XX], iy = icm[YY], iz = icm[ZZ];
333 if ((ix != 0) || (iy != 0) || (iz != 0))
335 for (j = m0; (j < m1); j++)
345 static bool step_man(t_manager* man, int* nat)
347 static int ncount = 0;
352 std::fprintf(stderr, "Not initiated yet!");
355 bEof = read_next_x(man->oenv, man->status, &man->time, man->x, man->box);
357 if (ncount == man->nSkip)
359 auto atomsArrayRef = gmx::arrayRefFromArray(reinterpret_cast<gmx::RVec*>(man->x), man->natom);
360 switch (man->molw->boxtype)
363 put_atoms_in_triclinic_unitcell(ecenterDEF, man->box, atomsArrayRef);
366 put_atoms_in_compact_unitcell(man->molw->ePBC, ecenterDEF, man->box, atomsArrayRef);
374 gmx_rmpbc(man->gpbc, man->natom, man->box, man->x);
375 reset_mols(&(man->top.mols), man->box, man->x);
384 return step_man(man, nat);
391 static void HandleClient(t_x11* x11, t_manager* man, const long data[])
393 int ID, button, x, y;
401 bPos = (button == Button1);
407 rotate_3d(man->view, ID - IDROTX, bPos);
413 fac = 0.8; /* Reduce distance between eye and origin */
420 /* zoom changed to scale by Berk Hess 3-7-96
421 if (zoom_3d(man->view,fac))
422 draw_mol(x11,man); */
423 man->view->sc_x /= fac;
424 man->view->sc_y /= fac;
430 translate_view(man->view, ID - IDTRANSX, bPos);
436 rewind_trj(man->status);
437 read_next_x(man->oenv, man->status, &(man->time), man->x, man->box);
447 if (!step_man(man, &nat))
457 usleep(man->nWait * 1000);
462 case IDFF: man->bStop = false; break;
463 case IDSTOP_ANI: man->bStop = true; break;
464 case IDDRAWMOL: draw_mol(x11, man); break;
469 case Button2: show_label(x11, man, x, y); break;
470 case Button3: hide_label(x11, man, x, y); break;
475 if (man->bAnimate && !man->bEof && !man->bStop)
477 step_message(x11, man);
481 static bool TitleCallBack(t_x11* x11, XEvent* event, Window /*w*/, void* data)
485 wd = static_cast<t_windata*>(data);
489 if (wd->text && (wd->width > 10))
491 XSetForeground(x11->disp, x11->gc, WHITE);
492 TextInWin(x11, wd, wd->text, eXCenter, eYCenter);
493 XDrawLine(x11->disp, wd->self, x11->gc, 0, wd->height, wd->width, wd->height);
496 case ConfigureNotify:
497 wd->width = event->xconfigure.width;
498 wd->height = event->xconfigure.height;
504 static bool ManCallBack(t_x11* x11, XEvent* event, Window /*w*/, void* data)
509 man = static_cast<t_manager*>(data);
512 case ConfigureNotify:
513 width = event->xconfigure.width;
514 height = event->xconfigure.height;
515 if ((width != man->wd.width) || (height != man->wd.height))
517 move_man(x11, man, width, height);
520 case ClientMessage: HandleClient(x11, man, event->xclient.data.l); break;
526 void no_labels(t_x11* x11, t_manager* man)
530 for (i = 0; (i < man->natom); i++)
532 man->bLabel[i] = false;
537 void move_man(t_x11* x11, t_manager* man, int width, int height)
539 int x0, y0, mw, mh, hb;
543 std::fprintf(stderr, "Move manager %dx%d\n", width, height);
545 man->wd.width = width;
546 man->wd.height = height;
548 /* Move all subwindows, resize only Mol window */
549 x0 = width - EWIDTH - AIR - 4 * BORDER; /* Starting of ewin etc. */
553 mw = x0 - 2 * AIR - 4 * BORDER;
554 mh = height - y0 - AIR - 2 * BORDER;
555 XMoveResizeWindow(x11->disp, man->molw->wd.self, AIR, y0, mw, mh);
558 th = XTextHeight(x11->font);
559 XMoveResizeWindow(x11->disp, man->title.self, 0, 0, mw, th + AIR);
562 XMoveResizeWindow(x11->disp, man->legw->wd.self, x0, y0, EWIDTH, LEGHEIGHT);
563 y0 += LEGHEIGHT + AIR + 2 * BORDER;
567 std::printf("Error: Windows falling out of main window!\n");
571 hb = height - y0 - AIR - 2 * BORDER;
572 XMoveResizeWindow(x11->disp, man->bbox->wd.self, x0, y0, EWIDTH, hb);
575 x0 = (mw - man->vbox->wd.width) / 2;
576 y0 = (mh - 2 - AIR - man->vbox->wd.height);
577 XMoveWindow(x11->disp, man->vbox->wd.self, x0, y0);
580 void map_man(t_x11* x11, t_manager* man)
582 XMapWindow(x11->disp, man->wd.self);
583 map_mw(x11, man->molw);
584 XMapWindow(x11->disp, man->title.self);
585 map_legw(x11, man->legw);
586 show_but(x11, man->bbox);
589 bool toggle_animate(t_x11* x11, t_manager* man)
593 man->bAnimate = !man->bAnimate;
598 show_but(x11, man->vbox);
602 hide_but(x11, man->vbox);
605 return man->bAnimate;
608 bool toggle_pbc(t_manager* man)
610 man->bPbc = !man->bPbc;
616 t_manager* init_man(t_x11* x11,
626 gmx_output_env_t* oenv)
631 man->status = nullptr;
635 InitWin(&(man->wd), x, y, width, height, 0, "Manager");
636 man->wd.self = XCreateSimpleWindow(x11->disp, Parent, man->wd.x, man->wd.y, man->wd.width,
637 man->wd.height, man->wd.bwidth, fg, bg);
638 x11->RegisterCallback(x11, man->wd.self, Parent, ManCallBack, man);
639 x11->SetInputMask(x11, man->wd.self, StructureNotifyMask | ExposureMask | ButtonPressMask);
641 /* The order of creating windows is important for the stacking order */
643 man->molw = init_mw(x11, man->wd.self, 0, 0, 1, 1, WHITE, BLUE, ePBC, box);
646 InitWin(&(man->title), 0, 0, 1, 1, 0, nullptr);
648 XCreateSimpleWindow(x11->disp, man->molw->wd.self, man->title.x, man->title.y,
649 man->title.width, man->title.height, man->title.bwidth, WHITE, BLUE);
650 x11->RegisterCallback(x11, man->title.self, man->molw->wd.self, TitleCallBack, &(man->title));
651 x11->SetInputMask(x11, man->title.self, ExposureMask | StructureNotifyMask);
654 man->bbox = init_bbox(x11, man->wd.self, man->wd.self, 1, WHITE, BLUE);
657 man->legw = init_legw(x11, man->wd.self, 0, 0, EWIDTH, LEGHEIGHT, WHITE, BLUE);
660 man->vbox = init_vbox(x11, man->molw->wd.self, man->wd.self, WHITE, BLUE);
665 void done_man(t_x11* x11, t_manager* man)
667 done_bbox(x11, man->vbox);
668 done_bbox(x11, man->bbox);
669 done_mw(x11, man->molw);
670 done_legw(x11, man->legw);
671 x11->UnRegisterCallback(x11, man->title.self);
672 x11->UnRegisterCallback(x11, man->wd.self);
682 void do_filter(t_x11* x11, t_manager* man, t_filter* filter)
687 for (i = 0; (i < man->natom); i++)
689 man->bVis[i] = false;
691 for (i = 0; (i < filter->grps->nr); i++)
693 if (filter->bShow[i])
695 for (j = filter->grps->index[i]; (j < filter->grps->index[i + 1]); j++)
697 man->bVis[filter->grps->a[j]] = true;
702 ExposeWin(x11->disp, man->wd.self);