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,2017,2018 by the GROMACS development team.
7 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
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.
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.
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.
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.
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.
44 #include "gromacs/commandline/pargs.h"
45 #include "gromacs/fileio/confio.h"
46 #include "gromacs/fileio/trxio.h"
47 #include "gromacs/fileio/xvgr.h"
48 #include "gromacs/gmxana/gmx_ana.h"
49 #include "gromacs/math/functions.h"
50 #include "gromacs/math/units.h"
51 #include "gromacs/math/utilities.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/pbcutil/rmpbc.h"
54 #include "gromacs/topology/index.h"
55 #include "gromacs/topology/topology.h"
56 #include "gromacs/trajectory/trajectoryframe.h"
57 #include "gromacs/utility/arraysize.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/futil.h"
60 #include "gromacs/utility/smalloc.h"
74 static void rotate_ends(t_bundle* bun, rvec axis, int c0, int c1)
80 for (end = 0; end < bun->nend; end++)
82 for (i = 0; i < bun->n; i++)
84 copy_rvec(bun->end[end][i], tmp);
85 bun->end[end][i][c0] = ax[c1] * tmp[c0] - ax[c0] * tmp[c1];
86 bun->end[end][i][c1] = ax[c0] * tmp[c0] + ax[c1] * tmp[c1];
90 axis[c0] = ax[c1] * tmp[c0] - ax[c0] * tmp[c1];
91 axis[c1] = ax[c0] * tmp[c0] + ax[c1] * tmp[c1];
94 static void calc_axes(rvec x[], t_atom atom[], const int gnx[], int* index[], gmx_bool bRot, t_bundle* bun)
98 rvec axis[MAX_ENDS], cent;
105 for (end = 0; end < bun->nend; end++)
107 for (i = 0; i < bun->n; i++)
109 clear_rvec(bun->end[end][i]);
112 div = gnx[end] / bun->n;
113 for (i = 0; i < gnx[end]; i++)
115 m = atom[index[end][i]].m;
116 for (d = 0; d < DIM; d++)
118 bun->end[end][i / div][d] += m * x[index[end][i]][d];
122 clear_rvec(axis[end]);
123 for (i = 0; i < bun->n; i++)
125 svmul(1.0 / mtot[i], bun->end[end][i], bun->end[end][i]);
126 rvec_inc(axis[end], bun->end[end][i]);
128 svmul(1.0 / bun->n, axis[end], axis[end]);
132 rvec_add(axis[0], axis[1], cent);
133 svmul(0.5, cent, cent);
134 /* center the bundle on the origin */
135 for (end = 0; end < bun->nend; end++)
137 rvec_dec(axis[end], cent);
138 for (i = 0; i < bun->n; i++)
140 rvec_dec(bun->end[end][i], cent);
145 /* rotate the axis parallel to the z-axis */
146 rotate_ends(bun, axis[0], YY, ZZ);
147 rotate_ends(bun, axis[0], XX, ZZ);
149 for (i = 0; i < bun->n; i++)
151 rvec_add(bun->end[0][i], bun->end[1][i], bun->mid[i]);
152 svmul(0.5, bun->mid[i], bun->mid[i]);
153 rvec_sub(bun->end[0][i], bun->end[1][i], bun->dir[i]);
154 bun->len[i] = norm(bun->dir[i]);
155 unitv(bun->dir[i], bun->dir[i]);
159 static void dump_axes(t_trxstatus* status, t_trxframe* fr, t_atoms* outat, t_bundle* bun)
162 static std::vector<gmx::RVec> xout;
165 GMX_ASSERT(outat->nr >= bun->n, "");
168 xout.resize(outat->nr);
171 for (i = 0; i < bun->n; i++)
173 copy_rvec(bun->end[0][i], xout[3 * i]);
176 copy_rvec(bun->end[2][i], xout[3 * i + 1]);
180 copy_rvec(bun->mid[i], xout[3 * i + 1]);
182 copy_rvec(bun->end[1][i], xout[3 * i + 2]);
189 frout.natoms = outat->nr;
191 frout.x = as_rvec_array(xout.data());
192 write_trxframe(status, &frout, nullptr);
195 int gmx_bundle(int argc, char* argv[])
197 const char* desc[] = {
198 "[THISMODULE] analyzes bundles of axes. The axes can be for instance",
199 "helix axes. The program reads two index groups and divides both",
200 "of them in [TT]-na[tt] parts. The centers of mass of these parts",
201 "define the tops and bottoms of the axes.",
202 "Several quantities are written to file:",
203 "the axis length, the distance and the z-shift of the axis mid-points",
204 "with respect to the average center of all axes, the total tilt,",
205 "the radial tilt and the lateral tilt with respect to the average axis.",
207 "With options [TT]-ok[tt], [TT]-okr[tt] and [TT]-okl[tt] the total,",
208 "radial and lateral kinks of the axes are plotted. An extra index",
209 "group of kink atoms is required, which is also divided into [TT]-na[tt]",
210 "parts. The kink angle is defined as the angle between the kink-top and",
211 "the bottom-kink vectors.",
213 "With option [TT]-oa[tt] the top, mid (or kink when [TT]-ok[tt] is set)",
214 "and bottom points of each axis",
215 "are written to a [REF].pdb[ref] file each frame. The residue numbers correspond",
216 "to the axis numbers. When viewing this file with Rasmol, use the",
217 "command line option [TT]-nmrpdb[tt], and type [TT]set axis true[tt] to",
218 "display the reference axis."
221 static gmx_bool bZ = FALSE;
222 t_pargs pa[] = { { "-na", FALSE, etINT, { &n }, "Number of axes" },
227 "Use the [IT]z[it]-axis as reference instead of the average axis" } };
228 FILE * flen, *fdist, *fz, *ftilt, *ftiltr, *ftiltl;
229 FILE * fkink = nullptr, *fkinkr = nullptr, *fkinkl = nullptr;
239 char* grpname[MAX_ENDS];
240 /* FIXME: The constness should not be cast away */
241 char * anm = const_cast<char*>("CA"), *rnm = const_cast<char*>("GLY");
242 int i, gnx[MAX_ENDS];
243 int* index[MAX_ENDS];
246 rvec va, vb, vc, vr, vl;
247 gmx_output_env_t* oenv;
248 gmx_rmpbc_t gpbc = nullptr;
250 #define NLEG asize(leg)
252 { efTRX, "-f", nullptr, ffREAD }, { efTPS, nullptr, nullptr, ffREAD },
253 { efNDX, nullptr, nullptr, ffOPTRD }, { efXVG, "-ol", "bun_len", ffWRITE },
254 { efXVG, "-od", "bun_dist", ffWRITE }, { efXVG, "-oz", "bun_z", ffWRITE },
255 { efXVG, "-ot", "bun_tilt", ffWRITE }, { efXVG, "-otr", "bun_tiltr", ffWRITE },
256 { efXVG, "-otl", "bun_tiltl", ffWRITE }, { efXVG, "-ok", "bun_kink", ffOPTWR },
257 { efXVG, "-okr", "bun_kinkr", ffOPTWR }, { efXVG, "-okl", "bun_kinkl", ffOPTWR },
258 { efPDB, "-oa", "axes", ffOPTWR }
260 #define NFILE asize(fnm)
262 if (!parse_common_args(
263 &argc, argv, PCA_CAN_TIME | PCA_TIME_UNIT, NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, nullptr, &oenv))
268 read_tps_conf(ftp2fn(efTPS, NFILE, fnm), &top, &pbcType, &xtop, nullptr, box, TRUE);
270 bKink = opt2bSet("-ok", NFILE, fnm) || opt2bSet("-okr", NFILE, fnm) || opt2bSet("-okl", NFILE, fnm);
280 fprintf(stderr, "Select a group of top and a group of bottom ");
283 fprintf(stderr, "and a group of kink ");
285 fprintf(stderr, "atoms\n");
286 get_index(&top.atoms, ftp2fn_null(efNDX, NFILE, fnm), bun.nend, gnx, index, grpname);
288 if (n <= 0 || gnx[0] % n || gnx[1] % n || (bKink && gnx[2] % n))
290 gmx_fatal(FARGS, "The size of one of your index groups is not a multiple of n");
304 opt2fn("-ol", NFILE, fnm), "Axis lengths", output_env_get_xvgr_tlabel(oenv), "(nm)", oenv);
305 fdist = xvgropen(opt2fn("-od", NFILE, fnm),
306 "Distance of axis centers",
307 output_env_get_xvgr_tlabel(oenv),
310 fz = xvgropen(opt2fn("-oz", NFILE, fnm),
311 "Z-shift of axis centers",
312 output_env_get_xvgr_tlabel(oenv),
316 opt2fn("-ot", NFILE, fnm), "Axis tilts", output_env_get_xvgr_tlabel(oenv), "(degrees)", oenv);
317 ftiltr = xvgropen(opt2fn("-otr", NFILE, fnm),
319 output_env_get_xvgr_tlabel(oenv),
322 ftiltl = xvgropen(opt2fn("-otl", NFILE, fnm),
323 "Lateral axis tilts",
324 output_env_get_xvgr_tlabel(oenv),
330 fkink = xvgropen(opt2fn("-ok", NFILE, fnm),
332 output_env_get_xvgr_tlabel(oenv),
335 fkinkr = xvgropen(opt2fn("-okr", NFILE, fnm),
336 "Radial kink angles",
337 output_env_get_xvgr_tlabel(oenv),
340 if (output_env_get_print_xvgr_codes(oenv))
342 fprintf(fkinkr, "@ subtitle \"+ = ) ( - = ( )\"\n");
344 fkinkl = xvgropen(opt2fn("-okl", NFILE, fnm),
345 "Lateral kink angles",
346 output_env_get_xvgr_tlabel(oenv),
351 if (opt2bSet("-oa", NFILE, fnm))
353 init_t_atoms(&outatoms, 3 * n, FALSE);
355 for (i = 0; i < 3 * n; i++)
357 outatoms.atomname[i] = &anm;
358 outatoms.atom[i].resind = i / 3;
359 outatoms.resinfo[i / 3].name = &rnm;
360 outatoms.resinfo[i / 3].nr = i / 3 + 1;
361 outatoms.resinfo[i / 3].ic = ' ';
363 fpdb = open_trx(opt2fn("-oa", NFILE, fnm), "w");
370 read_first_frame(oenv, &status, ftp2fn(efTRX, NFILE, fnm), &fr, TRX_NEED_X);
371 gpbc = gmx_rmpbc_init(&top.idef, pbcType, fr.natoms);
375 gmx_rmpbc_trxfr(gpbc, &fr);
376 calc_axes(fr.x, top.atoms.atom, gnx, index, !bZ, &bun);
377 t = output_env_conv_time(oenv, fr.time);
378 fprintf(flen, " %10g", t);
379 fprintf(fdist, " %10g", t);
380 fprintf(fz, " %10g", t);
381 fprintf(ftilt, " %10g", t);
382 fprintf(ftiltr, " %10g", t);
383 fprintf(ftiltl, " %10g", t);
386 fprintf(fkink, " %10g", t);
387 fprintf(fkinkr, " %10g", t);
388 fprintf(fkinkl, " %10g", t);
391 for (i = 0; i < bun.n; i++)
393 fprintf(flen, " %6g", bun.len[i]);
394 fprintf(fdist, " %6g", norm(bun.mid[i]));
395 fprintf(fz, " %6g", bun.mid[i][ZZ]);
396 fprintf(ftilt, " %6g", gmx::c_rad2Deg * acos(bun.dir[i][ZZ]));
397 comp = bun.mid[i][XX] * bun.dir[i][XX] + bun.mid[i][YY] * bun.dir[i][YY];
398 fprintf(ftiltr, " %6g", gmx::c_rad2Deg * std::asin(comp / std::hypot(comp, bun.dir[i][ZZ])));
399 comp = bun.mid[i][YY] * bun.dir[i][XX] - bun.mid[i][XX] * bun.dir[i][YY];
400 fprintf(ftiltl, " %6g", gmx::c_rad2Deg * std::asin(comp / std::hypot(comp, bun.dir[i][ZZ])));
403 rvec_sub(bun.end[0][i], bun.end[2][i], va);
404 rvec_sub(bun.end[2][i], bun.end[1][i], vb);
407 fprintf(fkink, " %6g", gmx::c_rad2Deg * acos(iprod(va, vb)));
409 copy_rvec(bun.mid[i], vr);
412 fprintf(fkinkr, " %6g", gmx::c_rad2Deg * std::asin(iprod(vc, vr)));
416 fprintf(fkinkl, " %6g", gmx::c_rad2Deg * std::asin(iprod(vc, vl)));
420 fprintf(fdist, "\n");
422 fprintf(ftilt, "\n");
423 fprintf(ftiltr, "\n");
424 fprintf(ftiltl, "\n");
427 fprintf(fkink, "\n");
428 fprintf(fkinkr, "\n");
429 fprintf(fkinkl, "\n");
433 dump_axes(fpdb, &fr, &outatoms, &bun);
435 } while (read_next_frame(oenv, status, &fr));
436 gmx_rmpbc_done(gpbc);