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