Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / gmxlib / selection / test_selection.c
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-2009, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \internal \file
39  * \brief Testing/debugging tool for the selection engine.
40  */
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include <copyrite.h>
46 #include <filenm.h>
47 #include <macros.h>
48 #include <smalloc.h>
49 #include <statutil.h>
50
51 #include <trajana.h>
52
53 typedef struct
54 {
55     gmx_bool                     bFrameTree;
56     int                      nmaxind;
57     gmx_ana_selcollection_t *sc;
58 } t_dumpdata;
59
60 int
61 dump_frame(t_topology * top, t_trxframe * fr, t_pbc * pbc,
62            int nr, gmx_ana_selection_t *sel[], void *data)
63 {
64     t_dumpdata         *d = (t_dumpdata *)data;
65     int                 g, i, n;
66
67     fprintf(stderr, "\n");
68     if (d->bFrameTree)
69     {
70         gmx_ana_selcollection_print_tree(stderr, d->sc, TRUE);
71     }
72     for (g = 0; g < nr; ++g)
73     {
74         gmx_ana_index_dump(sel[g]->g, g, d->nmaxind);
75         fprintf(stderr, "  Positions (%d pcs):\n", sel[g]->p.nr);
76         n = sel[g]->p.nr;
77         if (d->nmaxind >= 0 && n > d->nmaxind)
78         {
79             n = d->nmaxind;
80         }
81         for (i = 0; i < n; ++i)
82         {
83             fprintf(stderr, "    (%.2f,%.2f,%.2f) r=%d, m=%d, b=%d-%d\n",
84                     sel[g]->p.x[i][XX], sel[g]->p.x[i][YY], sel[g]->p.x[i][ZZ],
85                     sel[g]->p.m.refid[i], sel[g]->p.m.mapid[i],
86                     sel[g]->p.m.mapb.index[i]+1,
87                     sel[g]->p.m.mapb.index[i+1]);
88         }
89         if (n < sel[g]->p.nr)
90         {
91             fprintf(stderr, "    ...\n");
92         }
93     }
94     fprintf(stderr, "\n");
95     return 0;
96 }
97
98 static void
99 print_selections(int nr, gmx_ana_selection_t **sel, int nmaxind)
100 {
101     int                 g, i, n;
102
103     fprintf(stderr, "\nSelections:\n");
104     for (g = 0; g < nr; ++g)
105     {
106         fprintf(stderr, "  ");
107         gmx_ana_selection_print_info(sel[g]);
108         fprintf(stderr, "    ");
109         gmx_ana_index_dump(sel[g]->g, g, nmaxind);
110
111         fprintf(stderr, "    Block (size=%d):", sel[g]->p.m.mapb.nr);
112         if (!sel[g]->p.m.mapb.index)
113         {
114             fprintf(stderr, " (null)");
115         }
116         else
117         {
118             n = sel[g]->p.m.mapb.nr;
119             if (nmaxind >= 0 && n > nmaxind)
120                 n = nmaxind;
121             for (i = 0; i <= n; ++i)
122                 fprintf(stderr, " %d", sel[g]->p.m.mapb.index[i]);
123             if (n < sel[g]->p.m.mapb.nr)
124                 fprintf(stderr, " ...");
125         }
126         fprintf(stderr, "\n");
127
128         n = sel[g]->p.m.nr;
129         if (nmaxind >= 0 && n > nmaxind)
130             n = nmaxind;
131         fprintf(stderr, "    RefId:");
132         if (!sel[g]->p.m.refid)
133         {
134             fprintf(stderr, " (null)");
135         }
136         else
137         {
138             for (i = 0; i < n; ++i)
139                 fprintf(stderr, " %d", sel[g]->p.m.refid[i]);
140             if (n < sel[g]->p.m.nr)
141                 fprintf(stderr, " ...");
142         }
143         fprintf(stderr, "\n");
144
145         fprintf(stderr, "    MapId:");
146         if (!sel[g]->p.m.mapid)
147         {
148             fprintf(stderr, " (null)");
149         }
150         else
151         {
152             for (i = 0; i < n; ++i)
153                 fprintf(stderr, " %d", sel[g]->p.m.mapid[i]);
154             if (n < sel[g]->p.m.nr)
155                 fprintf(stderr, " ...");
156         }
157         fprintf(stderr, "\n");
158     }
159     fprintf(stderr, "\n");
160 }
161
162 int
163 gmx_test_selection(int argc, char *argv[])
164 {
165     const char         *desc[] = {
166         "This is a test program for selections.",
167     };
168
169     gmx_bool                bMaskOnly  = FALSE;
170     gmx_bool                bFrameTree = FALSE;
171     gmx_bool                bDebugCompile = FALSE;
172     int                 nref       = 0;
173     int                 nmaxind    = 20;
174     t_pargs             pa[] = {
175         {"-mask",   FALSE, etBOOL, {&bMaskOnly},
176          "Test position mask functionality"},
177         {"-compdebug", FALSE, etBOOL, {&bDebugCompile},
178          "Print intermediate trees during compilation"},
179         {"-frtree", FALSE, etBOOL, {&bFrameTree},
180          "Print the whole evaluation tree for each frame"},
181         {"-nref",   FALSE, etINT,  {&nref},
182          "Number of reference selections to ask for"},
183         {"-pmax",   FALSE, etINT,  {&nmaxind},
184          "Maximum number of indices to print in lists (-1 = print all)"},
185     };
186
187     t_filenm            fnm[] = {
188         {efDAT, "-o", "debug", ffOPTWR},
189     };
190
191     gmx_ana_traj_t       *trj;
192     t_dumpdata            d;
193     int                   ngrps;
194     gmx_ana_selection_t **sel;
195     output_env_t          oenv;
196
197 #define NFILE asize(fnm)
198
199     CopyRight(stderr, argv[0]);
200
201     gmx_ana_traj_create(&trj, ANA_DEBUG_SELECTION | ANA_USER_SELINIT | ANA_USE_FULLGRPS);
202     gmx_ana_get_selcollection(trj, &d.sc);
203     gmx_ana_set_nanagrps(trj, -1);
204     parse_trjana_args(trj, &argc, argv, 0,
205                       NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL,
206                       &oenv);
207     if (bMaskOnly)
208     {
209         gmx_ana_add_flags(trj, ANA_USE_POSMASK);
210         gmx_ana_selcollection_set_outpostype(d.sc, NULL, TRUE);
211     }
212     gmx_ana_selcollection_set_compile_debug(d.sc, bDebugCompile);
213     gmx_ana_set_nrefgrps(trj, nref);
214     gmx_ana_init_selections(trj);
215     gmx_ana_get_ngrps(trj, &ngrps);
216     gmx_ana_get_anagrps(trj, &sel);
217
218     d.bFrameTree = bFrameTree;
219     d.nmaxind    = nmaxind;
220
221     print_selections(ngrps, sel, d.nmaxind);
222
223     gmx_ana_do(trj, 0, &dump_frame, &d);
224
225     print_selections(ngrps, sel, d.nmaxind);
226
227     gmx_ana_traj_free(trj);
228     done_filenms(NFILE, fnm);
229
230     return 0;
231 }
232
233 int
234 main(int argc, char *argv[])
235 {
236     gmx_test_selection(argc, argv);
237     return 0;
238 }