3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
48 * Template analysis data structure.
52 gmx_ana_selection_t
*refsel
;
56 gmx_ana_nbsearch_t
*nb
;
60 * Function that does the analysis for a single frame.
62 * It is called once for each frame.
65 analyze_frame(t_topology
*top
, t_trxframe
*fr
, t_pbc
*pbc
,
66 int nr
, gmx_ana_selection_t
*sel
[], void *data
)
68 t_analysisdata
*d
= (t_analysisdata
*)data
;
73 /* Here, you can do whatever analysis your program requires for a frame. */
76 fprintf(d
->fp
, "%10.3f", fr
->time
);
78 rc
= gmx_ana_nbsearch_pos_init(d
->nb
, pbc
, &d
->refsel
->p
);
81 gmx_fatal(FARGS
, "Neighborhood search initialization failed");
83 for (g
= 0; g
< nr
; ++g
)
86 for (i
= 0; i
< sel
[g
]->p
.nr
; ++i
)
88 frave
+= gmx_ana_nbsearch_pos_mindist(d
->nb
, &sel
[g
]->p
, i
);
91 d
->n
[g
] += sel
[g
]->p
.nr
;
94 frave
/= sel
[g
]->p
.nr
;
95 fprintf(d
->fp
, " %.3f", frave
);
100 fprintf(d
->fp
, "\n");
102 /* We need to return 0 to tell that everything went OK */
107 * Function that implements the analysis tool.
109 * Following the style of Gromacs analysis tools, this function is called
113 gmx_template(int argc
, char *argv
[])
115 const char *desc
[] = {
116 "This is a template for writing your own analysis tools for",
117 "Gromacs. The advantage of using Gromacs for this is that you",
118 "have access to all information in the topology, and your",
119 "program will be able to handle all types of coordinates and",
120 "trajectory files supported by Gromacs. In addition,",
121 "you get a lot of functionality for free from the trajectory",
122 "analysis library, including support for flexible dynamic",
123 "selections. Go ahead an try it![PAR]",
124 "To get started with implementing your own analysis program,",
125 "follow the instructions in the README file provided.",
126 "This template implements a simple analysis programs that calculates",
127 "average distances from the a reference group to one or more",
131 /* Command-line arguments */
135 {"-cutoff", FALSE
, etREAL
, {&cutoff
},
136 "Cutoff for distance calculation (0 = no cutoff)"},
137 {"-arg2", FALSE
, etBOOL
, {&bArg
},
138 "Example argument 2"},
140 /* The second argument is for demonstration purposes only */
144 {efXVG
, "-o", "avedist", ffOPTWR
},
146 #define NFILE asize(fnm)
152 gmx_ana_selection_t
**sel
;
156 CopyRight(stderr
, argv
[0]);
157 /* Here, we can use flags to specify requirements for the selections and/or
158 * other features of the library. */
159 gmx_ana_traj_create(&trj
, ANA_REQUIRE_TOP
);
160 gmx_ana_set_nrefgrps(trj
, 1);
161 gmx_ana_set_nanagrps(trj
, -1);
162 /* If required, other functions can also be used to configure the library
163 * before calling parse_trjana_args(). */
164 parse_trjana_args(trj
, &argc
, argv
, PCA_CAN_VIEW
,
165 NFILE
, fnm
, asize(pa
), pa
, asize(desc
), desc
, 0, NULL
,
168 /* You can now do any initialization you wish, using the information
169 * from the trj structure.
170 * In particular, you should store any command-line parameter values that
171 * the analysis part requires into d for them to be accessible. */
173 /* First, we get some selection information from the structure */
174 gmx_ana_get_refsel(trj
, 0, &d
.refsel
);
175 gmx_ana_get_nanagrps(trj
, &ngrps
);
176 gmx_ana_get_anagrps(trj
, &sel
);
178 /* First, we initialize the neighborhood search for the first index
180 rc
= gmx_ana_nbsearch_create(&d
.nb
, cutoff
, d
.refsel
->p
.nr
);
183 gmx_fatal(FARGS
, "neighborhood search initialization failed");
186 /* We then allocate memory in d to store intermediate data
187 * and initialize the counters to zero */
191 /* We also open the output file if the user provided it */
193 if (opt2bSet("-o", NFILE
, fnm
))
195 d
.fp
= xvgropen(opt2fn("-o", NFILE
, fnm
), "Average distance",
196 "Time [ps]", "Distance [nm]", oenv
);
197 xvgr_selections(d
.fp
, trj
);
200 /* Now, we do the actual analysis */
201 gmx_ana_do(trj
, 0, &analyze_frame
, &d
);
203 /* Now, the analysis has been done for all frames, and you can access the
204 * results in d. Here, you should post-process your data and write out any
205 * averaged properties. */
207 /* For the template, we close the output file if one was opened */
213 /* We also calculate the average distance for each group */
214 for (g
= 0; g
< ngrps
; ++g
)
217 fprintf(stderr
, "Average distance for '%s': %.3f nm\n",
218 sel
[g
]->name
, d
.ave
[g
]);
221 /* Here, we could free some memory, but this usually not necessary as we
222 * are going to quit anyways. */
231 * In Gromacs, most analysis programs are implemented such that the \p main
232 * function is only a wrapper for a \p gmx_something function that does all
233 * the work, and that convention is also followed here. */
235 main(int argc
, char *argv
[])
237 gmx_template(argc
, argv
);