SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / programs / view / filter.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2013, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2017,2019,2020, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include <cstdio>
40 #include <cstring>
41
42 #include <algorithm>
43
44 #include "gromacs/topology/index.h"
45 #include "gromacs/utility/cstringutil.h"
46 #include "gromacs/utility/dir_separator.h"
47 #include "gromacs/utility/fatalerror.h"
48 #include "gromacs/utility/futil.h"
49 #include "gromacs/utility/smalloc.h"
50
51 #include "dialogs.h"
52 #include "xdlghi.h"
53
54 t_filter* init_filter(t_atoms* atoms, const char* fn, int natom_trx)
55 {
56     t_filter* f;
57     int       g, i;
58
59     snew(f, 1);
60     if (fn != nullptr)
61     {
62         f->grps = init_index(fn, &f->grpnames);
63     }
64     else
65     {
66         snew(f->grps, 1);
67         snew(f->grps->index, 1);
68         analyse(atoms, f->grps, &f->grpnames, false, false);
69     }
70     snew(f->bDisable, f->grps->nr);
71     for (g = 0; g < f->grps->nr; g++)
72     {
73         for (i = f->grps->index[g]; i < f->grps->index[g + 1] && !f->bDisable[g]; i++)
74         {
75             f->bDisable[g] = (f->grps->a[i] >= natom_trx);
76         }
77     }
78
79     snew(f->bShow, f->grps->nr);
80
81     return f;
82 }
83
84 static void FilterCB(t_x11* x11, int dlg_mess, int /*item_id*/, char* set, void* data)
85 {
86     int       nset;
87     t_filter* f;
88     t_gmx*    gmx;
89     t_dlg*    dlg;
90
91     gmx = (t_gmx*)data;
92     dlg = gmx->dlgs[edFilter];
93     f   = gmx->filter;
94
95 #ifdef DEBUG
96     std::printf("item_id: %d, set: %s\n", item_id, set);
97 #endif
98     switch (dlg_mess)
99     {
100         case DLG_SET:
101             if (set)
102             {
103                 if (sscanf(set, "%10d", &nset) == 1)
104                 {
105                     f->bShow[nset] = !f->bShow[nset];
106                 }
107             }
108             break;
109         case DLG_EXIT:
110             HideDlg(dlg);
111             write_gmx(x11, gmx, IDDOFILTER);
112             break;
113     }
114 }
115
116 t_dlg* select_filter(t_x11* x11, t_gmx* gmx)
117 {
118     static const char* title = "Group";
119     static const char* dummy = "\"FALSE\"";
120     static const char* ok    = "\"Ok\"";
121     FILE*              tmp;
122     t_dlg*             dlg;
123     char               tmpfile[STRLEN];
124     int                i, j, k, len, tlen, ht, ncol, nrow, x0;
125
126     len = std::strlen(title);
127     for (i = 0; (i < (int)gmx->filter->grps->nr); i++)
128     {
129         len = std::max(len, static_cast<int>(std::strlen(gmx->filter->grpnames[i])));
130     }
131     len += 2;
132
133     ncol = 1 + (gmx->filter->grps->nr / 15);
134     nrow = gmx->filter->grps->nr / ncol;
135     if (nrow * ncol < gmx->filter->grps->nr)
136     {
137         nrow++;
138     }
139     if (ncol > 1)
140     {
141         ht = 1 + (nrow + 1) * 2 + 3;
142     }
143     else
144     {
145         ht = 1 + (gmx->filter->grps->nr + 1) * 2 + 3;
146     }
147     std::strcpy(tmpfile, "filterXXXXXX");
148     tmp = gmx_fopen_temporary(tmpfile);
149 #ifdef DEBUG
150     std::fprintf(stderr, "file: %s\n", tmpfile);
151 #endif
152     tlen = 1 + ncol * (1 + len);
153     std::fprintf(tmp, "grid %d %d {\n\n", tlen, ht);
154
155     for (k = j = 0, x0 = 1; (j < ncol); j++, x0 += len + 1)
156     {
157         std::fprintf(tmp, "group \"%s-%d\" %d 1 %d %d {\n", title, j + 1, x0, len, ht - 5);
158         for (i = 0; (i < nrow) && (k < gmx->filter->grps->nr); i++, k++)
159         {
160             if (!gmx->filter->bDisable[k])
161             {
162                 std::fprintf(
163                         tmp, "checkbox \"%s\" \"%d\" %s %s %s\n", gmx->filter->grpnames[k], k, dummy, dummy, dummy);
164             }
165             else
166             {
167                 std::fprintf(tmp,
168                              "statictext { \"  %s\" } \"%d\" %s %s %s\n",
169                              gmx->filter->grpnames[k],
170                              k,
171                              dummy,
172                              dummy,
173                              dummy);
174             }
175         }
176         std::fprintf(tmp, "}\n\n");
177     }
178     std::fprintf(tmp, "simple 1 %d %d 2 {\n", ht - 3, tlen - 2);
179     std::fprintf(tmp, "defbutton %s %s %s %s %s\n", ok, ok, dummy, dummy, dummy);
180     std::fprintf(tmp, "}\n\n}\n");
181     gmx_ffclose(tmp);
182
183     dlg = ReadDlg(x11, gmx->wd->self, title, tmpfile, 0, 0, true, false, FilterCB, gmx);
184
185     std::remove(tmpfile);
186
187     return dlg;
188 }