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-2007, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,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/filenm.h"
45 #include "gromacs/commandline/pargs.h"
46 #include "gromacs/fileio/matio.h"
47 #include "gromacs/fileio/tpxio.h"
48 #include "gromacs/fileio/trxio.h"
49 #include "gromacs/fileio/xvgr.h"
50 #include "gromacs/gmxana/gmx_ana.h"
51 #include "gromacs/gmxana/gstat.h"
52 #include "gromacs/gmxlib/nrnb.h"
53 #include "gromacs/math/units.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/pbcutil/pbc.h"
56 #include "gromacs/topology/index.h"
57 #include "gromacs/topology/mtop_lookup.h"
58 #include "gromacs/topology/mtop_util.h"
59 #include "gromacs/topology/topology.h"
60 #include "gromacs/trajectory/trajectoryframe.h"
61 #include "gromacs/utility/arraysize.h"
62 #include "gromacs/utility/cstringutil.h"
63 #include "gromacs/utility/fatalerror.h"
64 #include "gromacs/utility/futil.h"
65 #include "gromacs/utility/gmxassert.h"
66 #include "gromacs/utility/smalloc.h"
68 static void clust_size(const char* ndx,
87 const gmx_output_env_t* oenv)
89 FILE * fp, *gp, *hp, *tp;
93 rvec * x = nullptr, *v = nullptr, dx;
95 gmx_bool bSame, bTPRwarn = TRUE;
100 PbcType pbcType = PbcType::Unset;
103 /* Cluster size distribution (matrix) */
104 real** cs_dist = nullptr;
105 real tf, dx2, cut2, *t_x = nullptr, *t_y, cmid, cmax, cav, ekin;
106 int i, j, k, ai, aj, ci, cj, nframe, nclust, n_x, max_size = 0;
107 int * clust_index, *clust_size, max_clust_size, max_clust_ind, nav, nhisto;
108 t_rgb rlo = { 1.0, 1.0, 1.0 };
109 int frameCounter = 0;
112 clear_trxframe(&fr, TRUE);
113 auto timeLabel = output_env_get_time_label(oenv);
114 tf = output_env_get_time_factor(oenv);
115 fp = xvgropen(ncl, "Number of clusters", timeLabel, "N", oenv);
116 gp = xvgropen(acl, "Average cluster size", timeLabel, "#molecules", oenv);
117 hp = xvgropen(mcl, "Max cluster size", timeLabel, "#molecules", oenv);
118 tp = xvgropen(tempf, "Temperature of largest cluster", timeLabel, "T (K)", oenv);
120 if (!read_first_frame(oenv, &status, trx, &fr, TRX_NEED_X | TRX_READ_V))
130 tpxh = readTpxHeader(tpr, true);
131 if (tpxh.natoms != natoms)
133 gmx_fatal(FARGS, "tpr (%d atoms) and trajectory (%d atoms) do not match!", tpxh.natoms, natoms);
135 pbcType = read_tpx(tpr, nullptr, nullptr, &natoms, nullptr, nullptr, &mtop);
143 tfac = ndf / (3.0 * natoms);
146 gmx::RangePartitioning mols;
151 printf("Using molecules rather than atoms. Not reading index file %s\n", ndx);
153 GMX_RELEASE_ASSERT(tpr, "Cannot access topology without having read it from TPR");
154 mols = gmx_mtop_molecules(mtop);
156 /* Make dummy index */
157 nindex = mols.numBlocks();
159 for (i = 0; (i < nindex); i++)
167 rd_index(ndx, 1, &nindex, &index, &gname);
171 snew(clust_index, nindex);
172 snew(clust_size, nindex);
177 for (i = 0; (i < nindex); i++)
186 if ((nskip == 0) || ((nskip > 0) && ((nframe % nskip) == 0)))
190 set_pbc(&pbc, pbcType, fr.box);
195 /* Put all atoms/molecules in their own cluster, with size 1 */
196 for (i = 0; (i < nindex); i++)
198 /* Cluster index is indexed with atom index number */
200 /* Cluster size is indexed with cluster number */
204 /* Loop over atoms */
205 for (i = 0; (i < nindex); i++)
210 /* Loop over atoms (only half a matrix) */
211 for (j = i + 1; (j < nindex); j++)
215 /* If they are not in the same cluster already */
220 /* Compute distance */
223 GMX_RELEASE_ASSERT(mols.numBlocks() > 0,
224 "Cannot access index[] from empty mols");
226 for (ii = mols.block(ai).begin(); !bSame && ii < mols.block(ai).end(); ii++)
228 for (jj = mols.block(aj).begin(); !bSame && jj < mols.block(aj).end(); jj++)
232 pbc_dx(&pbc, x[ii], x[jj], dx);
236 rvec_sub(x[ii], x[jj], dx);
239 bSame = (dx2 < cut2);
247 pbc_dx(&pbc, x[ai], x[aj], dx);
251 rvec_sub(x[ai], x[aj], dx);
254 bSame = (dx2 < cut2);
256 /* If distance less than cut-off */
259 /* Merge clusters: check for all atoms whether they are in
260 * cluster cj and if so, put them in ci
262 for (k = 0; (k < nindex); k++)
264 if (clust_index[k] == cj)
266 if (clust_size[cj] <= 0)
269 "negative cluster size %d for element %d",
294 frameTime = ++frameCounter;
296 t_x[n_x - 1] = frameTime * tf;
297 srenew(cs_dist, n_x);
298 snew(cs_dist[n_x - 1], nindex);
302 for (i = 0; (i < nindex); i++)
305 if (ci > max_clust_size)
313 cs_dist[n_x - 1][ci - 1] += 1.0;
314 max_size = std::max(max_size, ci);
322 fprintf(fp, "%14.6e %10d\n", frameTime, nclust);
325 fprintf(gp, "%14.6e %10.3f\n", frameTime, cav / nav);
327 fprintf(hp, "%14.6e %10d\n", frameTime, max_clust_size);
329 /* Analyse velocities, if present */
336 printf("You need a [REF].tpr[ref] file to analyse temperatures\n");
343 /* Loop over clusters and for each cluster compute 1/2 m v^2 */
344 if (max_clust_ind >= 0)
347 for (i = 0; (i < nindex); i++)
349 if (clust_index[i] == max_clust_ind)
352 real m = mtopGetAtomMass(mtop, ai, &molb);
353 ekin += 0.5 * m * iprod(v[ai], v[ai]);
356 temp = (ekin * 2.0) / (3.0 * tfac * max_clust_size * gmx::c_boltz);
357 fprintf(tp, "%10.3f %10.3f\n", frameTime, temp);
362 } while (read_next_frame(oenv, status, &fr));
370 if (max_clust_ind >= 0)
372 fp = gmx_ffopen(mcn, "w");
373 fprintf(fp, "[ max_clust ]\n");
374 for (i = 0; (i < nindex); i++)
376 if (clust_index[i] == max_clust_ind)
380 GMX_RELEASE_ASSERT(mols.numBlocks() > 0,
381 "Cannot access index[] from empty mols");
382 for (int j : mols.block(i))
384 fprintf(fp, "%d\n", j + 1);
389 fprintf(fp, "%d\n", index[i] + 1);
396 /* Print the real distribution cluster-size/numer, averaged over the trajectory. */
397 fp = xvgropen(histo, "Cluster size distribution", "Cluster size", "()", oenv);
399 fprintf(fp, "%5d %8.3f\n", 0, 0.0);
400 for (j = 0; (j < max_size); j++)
403 for (i = 0; (i < n_x); i++)
405 nelem += cs_dist[i][j];
407 fprintf(fp, "%5d %8.3f\n", j + 1, nelem / n_x);
408 nhisto += static_cast<int>((j + 1) * nelem / n_x);
410 fprintf(fp, "%5d %8.3f\n", j + 1, 0.0);
413 fprintf(stderr, "Total number of atoms in clusters = %d\n", nhisto);
415 /* Look for the smallest entry that is not zero
416 * This will make that zero is white, and not zero is coloured.
420 for (i = 0; (i < n_x); i++)
422 for (j = 0; (j < max_size); j++)
424 if ((cs_dist[i][j] > 0) && (cs_dist[i][j] < cmid))
426 cmid = cs_dist[i][j];
428 cmax = std::max(cs_dist[i][j], cmax);
431 fprintf(stderr, "cmid: %g, cmax: %g, max_size: %d\n", cmid, cmax, max_size);
433 fp = gmx_ffopen(xpm, "w");
436 "Cluster size distribution",
455 for (i = 0; (i < n_x); i++)
457 for (j = 0; (j < max_size); j++)
459 cs_dist[i][j] *= (j + 1);
460 if ((cs_dist[i][j] > 0) && (cs_dist[i][j] < cmid))
462 cmid = cs_dist[i][j];
464 cmax = std::max(cs_dist[i][j], cmax);
467 fprintf(stderr, "cmid: %g, cmax: %g, max_size: %d\n", cmid, cmax, max_size);
468 fp = gmx_ffopen(xpmw, "w");
471 "Weighted cluster size distribution",
490 for (i = 0; (i < n_x); i++)
500 int gmx_clustsize(int argc, char* argv[])
502 const char* desc[] = {
503 "[THISMODULE] computes the size distributions of molecular/atomic clusters in",
504 "the gas phase. The output is given in the form of an [REF].xpm[ref] file.",
505 "The total number of clusters is written to an [REF].xvg[ref] file.[PAR]",
506 "When the [TT]-mol[tt] option is given clusters will be made out of",
507 "molecules rather than atoms, which allows clustering of large molecules.",
508 "In this case an index file would still contain atom numbers",
509 "or your calculation will die with a SEGV.[PAR]",
510 "When velocities are present in your trajectory, the temperature of",
511 "the largest cluster will be printed in a separate [REF].xvg[ref] file assuming",
512 "that the particles are free to move. If you are using constraints,",
513 "please correct the temperature. For instance water simulated with SHAKE",
514 "or SETTLE will yield a temperature that is 1.5 times too low. You can",
515 "compensate for this with the [TT]-ndf[tt] option. Remember to take the removal",
516 "of center of mass motion into account.[PAR]",
517 "The [TT]-mc[tt] option will produce an index file containing the",
518 "atom numbers of the largest cluster."
525 gmx_bool bMol = FALSE;
526 gmx_bool bPBC = TRUE;
527 rvec rlo = { 1.0, 1.0, 0.0 };
528 rvec rhi = { 0.0, 0.0, 1.0 };
530 gmx_output_env_t* oenv;
537 "Largest distance (nm) to be considered in a cluster" },
542 "Cluster molecules rather than atoms (needs [REF].tpr[ref] file)" },
543 { "-pbc", FALSE, etBOOL, { &bPBC }, "Use periodic boundary conditions" },
544 { "-nskip", FALSE, etINT, { &nskip }, "Number of frames to skip between writing" },
549 "Number of levels of grey in [REF].xpm[ref] output" },
554 "Number of degrees of freedom of the entire system for temperature calculation. "
555 "If not set, the number of atoms times three is used." },
560 "RGB values for the color of the lowest occupied cluster size" },
565 "RGB values for the color of the highest occupied cluster size" }
567 #define NPA asize(pa)
568 const char *fnNDX, *fnTPR;
572 { efTRX, "-f", nullptr, ffREAD }, { efTPR, nullptr, nullptr, ffOPTRD },
573 { efNDX, nullptr, nullptr, ffOPTRD }, { efXPM, "-o", "csize", ffWRITE },
574 { efXPM, "-ow", "csizew", ffWRITE }, { efXVG, "-nc", "nclust", ffWRITE },
575 { efXVG, "-mc", "maxclust", ffWRITE }, { efXVG, "-ac", "avclust", ffWRITE },
576 { efXVG, "-hc", "histo-clust", ffWRITE }, { efXVG, "-temp", "temp", ffOPTWR },
577 { efNDX, "-mcn", "maxclust", ffOPTWR }
579 #define NFILE asize(fnm)
581 if (!parse_common_args(
582 &argc, argv, PCA_CAN_VIEW | PCA_CAN_TIME | PCA_TIME_UNIT, NFILE, fnm, NPA, pa, asize(desc), desc, 0, nullptr, &oenv))
587 fnNDX = ftp2fn_null(efNDX, NFILE, fnm);
595 fnTPR = ftp2fn_null(efTPR, NFILE, fnm);
598 gmx_fatal(FARGS, "You need a tpr file for the -mol option");
602 ftp2fn(efTRX, NFILE, fnm),
603 opt2fn("-o", NFILE, fnm),
604 opt2fn("-ow", NFILE, fnm),
605 opt2fn("-nc", NFILE, fnm),
606 opt2fn("-ac", NFILE, fnm),
607 opt2fn("-mc", NFILE, fnm),
608 opt2fn("-hc", NFILE, fnm),
609 opt2fn("-temp", NFILE, fnm),
610 opt2fn("-mcn", NFILE, fnm),
622 output_env_done(oenv);