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, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
44 #include "gromacs/commandline/pargs.h"
47 #include "gromacs/utility/smalloc.h"
51 #include "gromacs/fileio/futil.h"
58 #include "gromacs/fileio/tpxio.h"
59 #include "gromacs/fileio/trxio.h"
61 #include "gromacs/fileio/matio.h"
65 #include "gromacs/legacyheaders/gmx_fatal.h"
67 int gmx_densmap(int argc, char *argv[])
69 const char *desc[] = {
70 "[THISMODULE] computes 2D number-density maps.",
71 "It can make planar and axial-radial density maps.",
72 "The output [TT].xpm[tt] file can be visualized with for instance xv",
73 "and can be converted to postscript with [TT]xpm2ps[tt].",
74 "Optionally, output can be in text form to a [TT].dat[tt] file with [TT]-od[tt], instead of the usual [TT].xpm[tt] file with [TT]-o[tt].",
76 "The default analysis is a 2-D number-density map for a selected",
77 "group of atoms in the x-y plane.",
78 "The averaging direction can be changed with the option [TT]-aver[tt].",
79 "When [TT]-xmin[tt] and/or [TT]-xmax[tt] are set only atoms that are",
80 "within the limit(s) in the averaging direction are taken into account.",
81 "The grid spacing is set with the option [TT]-bin[tt].",
82 "When [TT]-n1[tt] or [TT]-n2[tt] is non-zero, the grid",
83 "size is set by this option.",
84 "Box size fluctuations are properly taken into account.",
86 "When options [TT]-amax[tt] and [TT]-rmax[tt] are set, an axial-radial",
87 "number-density map is made. Three groups should be supplied, the centers",
88 "of mass of the first two groups define the axis, the third defines the",
89 "analysis group. The axial direction goes from -amax to +amax, where",
90 "the center is defined as the midpoint between the centers of mass and",
91 "the positive direction goes from the first to the second center of mass.",
92 "The radial direction goes from 0 to rmax or from -rmax to +rmax",
93 "when the [TT]-mirror[tt] option has been set.",
95 "The normalization of the output is set with the [TT]-unit[tt] option.",
96 "The default produces a true number density. Unit [TT]nm-2[tt] leaves out",
97 "the normalization for the averaging or the angular direction.",
98 "Option [TT]count[tt] produces the count for each grid cell.",
99 "When you do not want the scale in the output to go",
100 "from zero to the maximum density, you can set the maximum",
101 "with the option [TT]-dmax[tt]."
103 static int n1 = 0, n2 = 0;
104 static real xmin = -1, xmax = -1, bin = 0.02, dmin = 0, dmax = 0, amax = 0, rmax = 0;
105 static gmx_bool bMirror = FALSE, bSums = FALSE;
106 static const char *eaver[] = { NULL, "z", "y", "x", NULL };
107 static const char *eunit[] = { NULL, "nm-3", "nm-2", "count", NULL };
110 { "-bin", FALSE, etREAL, {&bin},
112 { "-aver", FALSE, etENUM, {eaver},
113 "The direction to average over" },
114 { "-xmin", FALSE, etREAL, {&xmin},
115 "Minimum coordinate for averaging" },
116 { "-xmax", FALSE, etREAL, {&xmax},
117 "Maximum coordinate for averaging" },
118 { "-n1", FALSE, etINT, {&n1},
119 "Number of grid cells in the first direction" },
120 { "-n2", FALSE, etINT, {&n2},
121 "Number of grid cells in the second direction" },
122 { "-amax", FALSE, etREAL, {&amax},
123 "Maximum axial distance from the center"},
124 { "-rmax", FALSE, etREAL, {&rmax},
125 "Maximum radial distance" },
126 { "-mirror", FALSE, etBOOL, {&bMirror},
127 "Add the mirror image below the axial axis" },
128 { "-sums", FALSE, etBOOL, {&bSums},
129 "Print density sums (1D map) to stdout" },
130 { "-unit", FALSE, etENUM, {eunit},
131 "Unit for the output" },
132 { "-dmin", FALSE, etREAL, {&dmin},
133 "Minimum density in output"},
134 { "-dmax", FALSE, etREAL, {&dmax},
135 "Maximum density in output (0 means calculate it)"},
137 gmx_bool bXmin, bXmax, bRadial;
142 rvec *x, xcom[2], direction, center, dx;
146 int cav = 0, c1 = 0, c2 = 0, natoms;
147 char **grpname, title[256], buf[STRLEN];
149 int i, j, k, l, ngrps, anagrp, *gnx = NULL, nindex, nradial = 0, nfr, nmpower;
150 atom_id **ind = NULL, *index;
151 real **grid, maxgrid, m1, m2, box1, box2, *tickx, *tickz, invcellvol;
152 real invspa = 0, invspz = 0, axial, r, vol_old, vol, rowsum;
154 t_rgb rlo = {1, 1, 1}, rhi = {0, 0, 0};
156 const char *label[] = { "x (nm)", "y (nm)", "z (nm)" };
158 { efTRX, "-f", NULL, ffREAD },
159 { efTPS, NULL, NULL, ffOPTRD },
160 { efNDX, NULL, NULL, ffOPTRD },
161 { efDAT, "-od", "densmap", ffOPTWR },
162 { efXPM, "-o", "densmap", ffWRITE }
164 #define NFILE asize(fnm)
169 if (!parse_common_args(&argc, argv, PCA_CAN_TIME | PCA_CAN_VIEW | PCA_BE_NICE,
170 NFILE, fnm, npargs, pa, asize(desc), desc, 0, NULL, &oenv))
175 bXmin = opt2parg_bSet("-xmin", npargs, pa);
176 bXmax = opt2parg_bSet("-xmax", npargs, pa);
177 bRadial = (amax > 0 || rmax > 0);
180 if (amax <= 0 || rmax <= 0)
182 gmx_fatal(FARGS, "Both amax and rmax should be larger than zero");
186 if (strcmp(eunit[0], "nm-3") == 0)
191 else if (strcmp(eunit[0], "nm-2") == 0)
202 if (ftp2bSet(efTPS, NFILE, fnm) || !ftp2bSet(efNDX, NFILE, fnm))
204 read_tps_conf(ftp2fn(efTPS, NFILE, fnm), title, &top, &ePBC, &x, NULL, box,
210 fprintf(stderr, "\nSelect an analysis group\n");
216 "\nSelect two groups to define the axis and an analysis group\n");
219 snew(grpname, ngrps);
221 get_index(&top.atoms, ftp2fn_null(efNDX, NFILE, fnm), ngrps, gnx, ind, grpname);
223 nindex = gnx[anagrp];
227 if ((gnx[0] > 1 || gnx[1] > 1) && !ftp2bSet(efTPS, NFILE, fnm))
229 gmx_fatal(FARGS, "No run input file was supplied (option -s), this is required for the center of mass calculation");
235 case 'x': cav = XX; c1 = YY; c2 = ZZ; break;
236 case 'y': cav = YY; c1 = XX; c2 = ZZ; break;
237 case 'z': cav = ZZ; c1 = XX; c2 = YY; break;
240 natoms = read_first_x(oenv, &status, ftp2fn(efTRX, NFILE, fnm), &t, &x, box);
246 n1 = (int)(box[c1][c1]/bin + 0.5);
250 n2 = (int)(box[c2][c2]/bin + 0.5);
255 n1 = (int)(2*amax/bin + 0.5);
256 nradial = (int)(rmax/bin + 0.5);
257 invspa = n1/(2*amax);
258 invspz = nradial/rmax;
270 for (i = 0; i < n1; i++)
287 invcellvol /= det(box);
289 else if (nmpower == -2)
291 invcellvol /= box[c1][c1]*box[c2][c2];
293 for (i = 0; i < nindex; i++)
296 if ((!bXmin || x[j][cav] >= xmin) &&
297 (!bXmax || x[j][cav] <= xmax))
299 m1 = x[j][c1]/box[c1][c1];
308 m2 = x[j][c2]/box[c2][c2];
317 grid[(int)(m1*n1)][(int)(m2*n2)] += invcellvol;
323 set_pbc(&pbc, ePBC, box);
324 for (i = 0; i < 2; i++)
328 /* One atom, just copy the coordinates */
329 copy_rvec(x[ind[i][0]], xcom[i]);
333 /* Calculate the center of mass */
336 for (j = 0; j < gnx[i]; j++)
339 m = top.atoms.atom[k].m;
340 for (l = 0; l < DIM; l++)
342 xcom[i][l] += m*x[k][l];
346 svmul(1/mtot, xcom[i], xcom[i]);
349 pbc_dx(&pbc, xcom[1], xcom[0], direction);
350 for (i = 0; i < DIM; i++)
352 center[i] = xcom[0][i] + 0.5*direction[i];
354 unitv(direction, direction);
355 for (i = 0; i < nindex; i++)
358 pbc_dx(&pbc, x[j], center, dx);
359 axial = iprod(dx, direction);
360 r = sqrt(norm2(dx) - axial*axial);
361 if (axial >= -amax && axial < amax && r < rmax)
367 grid[(int)((axial + amax)*invspa)][(int)(r*invspz)] += 1;
373 while (read_next_x(oenv, status, &t, x, box));
376 /* normalize gridpoints */
380 for (i = 0; i < n1; i++)
382 for (j = 0; j < n2; j++)
385 if (grid[i][j] > maxgrid)
387 maxgrid = grid[i][j];
394 for (i = 0; i < n1; i++)
397 for (j = 0; j < nradial; j++)
401 case -3: vol = M_PI*(j+1)*(j+1)/(invspz*invspz*invspa); break;
402 case -2: vol = (j+1)/(invspz*invspa); break;
403 default: vol = j+1; break;
413 grid[i][k] /= nfr*(vol - vol_old);
416 grid[i][nradial-1-j] = grid[i][k];
419 if (grid[i][k] > maxgrid)
421 maxgrid = grid[i][k];
426 fprintf(stdout, "\n The maximum density is %f %s\n", maxgrid, unit);
436 /* normalize box-axes */
439 for (i = 0; i <= n1; i++)
441 tickx[i] = i*box1/n1;
443 for (i = 0; i <= n2; i++)
445 tickz[i] = i*box2/n2;
450 for (i = 0; i <= n1; i++)
452 tickx[i] = i/invspa - amax;
456 for (i = 0; i <= n2; i++)
458 tickz[i] = i/invspz - rmax;
463 for (i = 0; i <= n2; i++)
472 for (i = 0; i < n1; ++i)
474 fprintf(stdout, "Density sums:\n");
476 for (j = 0; j < n2; ++j)
478 rowsum += grid[i][j];
480 fprintf(stdout, "%g\t", rowsum);
482 fprintf(stdout, "\n");
485 sprintf(buf, "%s number density", grpname[anagrp]);
486 if (!bRadial && (bXmin || bXmax))
490 sprintf(buf+strlen(buf), ", %c > %g nm", eaver[0][0], xmin);
494 sprintf(buf+strlen(buf), ", %c < %g nm", eaver[0][0], xmax);
498 sprintf(buf+strlen(buf), ", %c: %g - %g nm", eaver[0][0], xmin, xmax);
501 if (ftp2bSet(efDAT, NFILE, fnm))
503 fp = gmx_ffopen(ftp2fn(efDAT, NFILE, fnm), "w");
504 /*optional text form output: first row is tickz; first col is tickx */
506 for (j = 0; j < n2; ++j)
508 fprintf(fp, "%g\t", tickz[j]);
512 for (i = 0; i < n1; ++i)
514 fprintf(fp, "%g\t", tickx[i]);
515 for (j = 0; j < n2; ++j)
517 fprintf(fp, "%g\t", grid[i][j]);
525 fp = gmx_ffopen(ftp2fn(efXPM, NFILE, fnm), "w");
526 write_xpm(fp, MAT_SPATIAL_X | MAT_SPATIAL_Y, buf, unit,
527 bRadial ? "axial (nm)" : label[c1], bRadial ? "r (nm)" : label[c2],
528 n1, n2, tickx, tickz, grid, dmin, maxgrid, rlo, rhi, &nlev);
532 do_view(oenv, opt2fn("-o", NFILE, fnm), NULL);