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.
46 #include "gromacs/utility/cstringutil.h"
48 #include "gromacs/utility/smalloc.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/fileio/xvgr.h"
55 #include "gromacs/utility/futil.h"
56 #include "gromacs/commandline/pargs.h"
58 #include "gromacs/fileio/tpxio.h"
59 #include "gromacs/fileio/trxio.h"
60 #include "gromacs/math/units.h"
64 #include "gromacs/utility/fatalerror.h"
71 /****************************************************************************/
72 /* This program calculates the partial density across the box. */
73 /* Peter Tieleman, Mei 1995 */
74 /****************************************************************************/
76 /* used for sorting the list */
77 int compare(void *a, void *b)
79 t_electron *tmp1, *tmp2;
80 tmp1 = (t_electron *)a; tmp2 = (t_electron *)b;
82 return strcmp(tmp1->atomname, tmp2->atomname);
85 int get_electrons(t_electron **eltab, const char *fn)
87 char buffer[256]; /* to read in a line */
88 char tempname[80]; /* buffer to hold name */
92 int nr; /* number of atomstypes to read */
95 if (!(in = gmx_ffopen(fn, "r")))
97 gmx_fatal(FARGS, "Couldn't open %s. Exiting.\n", fn);
100 if (NULL == fgets(buffer, 255, in))
102 gmx_fatal(FARGS, "Error reading from file %s", fn);
105 if (sscanf(buffer, "%d", &nr) != 1)
107 gmx_fatal(FARGS, "Invalid number of atomtypes in datafile\n");
112 for (i = 0; i < nr; i++)
114 if (fgets(buffer, 255, in) == NULL)
116 gmx_fatal(FARGS, "reading datafile. Check your datafile.\n");
118 if (sscanf(buffer, "%s = %d", tempname, &tempnr) != 2)
120 gmx_fatal(FARGS, "Invalid line in datafile at line %d\n", i+1);
122 (*eltab)[i].nr_el = tempnr;
123 (*eltab)[i].atomname = strdup(tempname);
128 fprintf(stderr, "Sorting list..\n");
129 qsort ((void*)*eltab, nr, sizeof(t_electron),
130 (int(*)(const void*, const void*))compare);
135 void center_coords(t_atoms *atoms, matrix box, rvec x0[], int axis)
139 rvec com, shift, box_center;
143 for (i = 0; (i < atoms->nr); i++)
145 mm = atoms->atom[i].m;
147 for (m = 0; (m < DIM); m++)
149 com[m] += mm*x0[i][m];
152 for (m = 0; (m < DIM); m++)
156 calc_box_center(ecenterDEF, box, box_center);
157 rvec_sub(box_center, com, shift);
158 shift[axis] -= box_center[axis];
160 for (i = 0; (i < atoms->nr); i++)
162 rvec_dec(x0[i], shift);
166 void calc_electron_density(const char *fn, atom_id **index, int gnx[],
167 double ***slDensity, int *nslices, t_topology *top,
169 int axis, int nr_grps, real *slWidth,
170 t_electron eltab[], int nr, gmx_bool bCenter,
171 const output_env_t oenv)
173 rvec *x0; /* coordinates without pbc */
174 matrix box; /* box (3x3) */
176 int natoms; /* nr. atoms in trj */
178 int i, n, /* loop indices */
179 nr_frames = 0, /* number of frames */
180 slice; /* current slice */
181 t_electron *found; /* found by bsearch */
182 t_electron sought; /* thingie thought by bsearch */
183 gmx_rmpbc_t gpbc = NULL;
188 if (axis < 0 || axis >= DIM)
190 gmx_fatal(FARGS, "Invalid axes. Terminating\n");
193 if ((natoms = read_first_x(oenv, &status, fn, &t, &x0, box)) == 0)
195 gmx_fatal(FARGS, "Could not read coordinates from statusfile\n");
200 *nslices = (int)(box[axis][axis] * 10); /* default value */
202 fprintf(stderr, "\nDividing the box in %d slices\n", *nslices);
204 snew(*slDensity, nr_grps);
205 for (i = 0; i < nr_grps; i++)
207 snew((*slDensity)[i], *nslices);
210 gpbc = gmx_rmpbc_init(&top->idef, ePBC, top->atoms.nr);
211 /*********** Start processing trajectory ***********/
214 gmx_rmpbc(gpbc, natoms, box, x0);
218 center_coords(&top->atoms, box, x0, axis);
221 *slWidth = box[axis][axis]/(*nslices);
222 invvol = *nslices/(box[XX][XX]*box[YY][YY]*box[ZZ][ZZ]);
224 for (n = 0; n < nr_grps; n++)
226 for (i = 0; i < gnx[n]; i++) /* loop over all atoms in index file */
228 z = x0[index[n][i]][axis];
231 z += box[axis][axis];
233 while (z > box[axis][axis])
235 z -= box[axis][axis];
238 /* determine which slice atom is in */
239 slice = (z / (*slWidth));
241 sought.atomname = strdup(*(top->atoms.atomname[index[n][i]]));
243 /* now find the number of electrons. This is not efficient. */
244 found = (t_electron *)
245 bsearch((const void *)&sought,
246 (const void *)eltab, nr, sizeof(t_electron),
247 (int(*)(const void*, const void*))compare);
251 fprintf(stderr, "Couldn't find %s. Add it to the .dat file\n",
252 *(top->atoms.atomname[index[n][i]]));
256 (*slDensity)[n][slice] += (found->nr_el -
257 top->atoms.atom[index[n][i]].q)*invvol;
259 free(sought.atomname);
264 while (read_next_x(oenv, status, &t, x0, box));
265 gmx_rmpbc_done(gpbc);
267 /*********** done with status file **********/
270 /* slDensity now contains the total number of electrons per slice, summed
271 over all frames. Now divide by nr_frames and volume of slice
274 fprintf(stderr, "\nRead %d frames from trajectory. Counting electrons\n",
277 for (n = 0; n < nr_grps; n++)
279 for (i = 0; i < *nslices; i++)
281 (*slDensity)[n][i] /= nr_frames;
285 sfree(x0); /* free memory used by coordinate array */
288 void calc_density(const char *fn, atom_id **index, int gnx[],
289 double ***slDensity, int *nslices, t_topology *top, int ePBC,
290 int axis, int nr_grps, real *slWidth, gmx_bool bCenter,
291 const output_env_t oenv)
293 rvec *x0; /* coordinates without pbc */
294 matrix box; /* box (3x3) */
296 int natoms; /* nr. atoms in trj */
298 int **slCount, /* nr. of atoms in one slice for a group */
299 i, j, n, /* loop indices */
302 nr_frames = 0, /* number of frames */
303 slice; /* current slice */
306 char *buf; /* for tmp. keeping atomname */
307 gmx_rmpbc_t gpbc = NULL;
309 if (axis < 0 || axis >= DIM)
311 gmx_fatal(FARGS, "Invalid axes. Terminating\n");
314 if ((natoms = read_first_x(oenv, &status, fn, &t, &x0, box)) == 0)
316 gmx_fatal(FARGS, "Could not read coordinates from statusfile\n");
321 *nslices = (int)(box[axis][axis] * 10); /* default value */
322 fprintf(stderr, "\nDividing the box in %d slices\n", *nslices);
325 snew(*slDensity, nr_grps);
326 for (i = 0; i < nr_grps; i++)
328 snew((*slDensity)[i], *nslices);
331 gpbc = gmx_rmpbc_init(&top->idef, ePBC, top->atoms.nr);
332 /*********** Start processing trajectory ***********/
335 gmx_rmpbc(gpbc, natoms, box, x0);
339 center_coords(&top->atoms, box, x0, axis);
342 *slWidth = box[axis][axis]/(*nslices);
343 invvol = *nslices/(box[XX][XX]*box[YY][YY]*box[ZZ][ZZ]);
346 for (n = 0; n < nr_grps; n++)
348 for (i = 0; i < gnx[n]; i++) /* loop over all atoms in index file */
350 z = x0[index[n][i]][axis];
353 z += box[axis][axis];
355 while (z > box[axis][axis])
357 z -= box[axis][axis];
360 /* determine which slice atom is in */
361 slice = (int)(z / (*slWidth));
362 (*slDensity)[n][slice] += top->atoms.atom[index[n][i]].m*invvol;
367 while (read_next_x(oenv, status, &t, x0, box));
368 gmx_rmpbc_done(gpbc);
370 /*********** done with status file **********/
373 /* slDensity now contains the total mass per slice, summed over all
374 frames. Now divide by nr_frames and volume of slice
377 fprintf(stderr, "\nRead %d frames from trajectory. Calculating density\n",
380 for (n = 0; n < nr_grps; n++)
382 for (i = 0; i < *nslices; i++)
384 (*slDensity)[n][i] /= nr_frames;
388 sfree(x0); /* free memory used by coordinate array */
391 void plot_density(double *slDensity[], const char *afile, int nslices,
392 int nr_grps, char *grpname[], real slWidth,
393 const char **dens_opt,
394 gmx_bool bSymmetrize, const output_env_t oenv)
397 const char *ylabel = NULL;
401 switch (dens_opt[0][0])
403 case 'm': ylabel = "Density (kg m\\S-3\\N)"; break;
404 case 'n': ylabel = "Number density (nm\\S-3\\N)"; break;
405 case 'c': ylabel = "Charge density (e nm\\S-3\\N)"; break;
406 case 'e': ylabel = "Electron density (e nm\\S-3\\N)"; break;
409 den = xvgropen(afile, "Partial densities", "Box (nm)", ylabel, oenv);
411 xvgr_legend(den, nr_grps, (const char**)grpname, oenv);
413 for (slice = 0; (slice < nslices); slice++)
415 fprintf(den, "%12g ", slice * slWidth);
416 for (n = 0; (n < nr_grps); n++)
420 ddd = (slDensity[n][slice]+slDensity[n][nslices-slice-1])*0.5;
424 ddd = slDensity[n][slice];
426 if (dens_opt[0][0] == 'm')
428 fprintf(den, " %12g", ddd*AMU/(NANO*NANO*NANO));
432 fprintf(den, " %12g", ddd);
441 int gmx_density(int argc, char *argv[])
443 const char *desc[] = {
444 "[THISMODULE] computes partial densities across the box, using an index file.[PAR]",
445 "For the total density of NPT simulations, use [gmx-energy] instead.",
447 "Densities are in kg/m^3, and number densities or electron densities can also be",
448 "calculated. For electron densities, a file describing the number of",
449 "electrons for each type of atom should be provided using [TT]-ei[tt].",
450 "It should look like:[BR]",
452 " [TT]atomname = nrelectrons[tt][BR]",
453 " [TT]atomname = nrelectrons[tt][BR]",
454 "The first line contains the number of lines to read from the file.",
455 "There should be one line for each unique atom name in your system.",
456 "The number of electrons for each atom is modified by its atomic",
461 static const char *dens_opt[] =
462 { NULL, "mass", "number", "charge", "electron", NULL };
463 static int axis = 2; /* normal to memb. default z */
464 static const char *axtitle = "Z";
465 static int nslices = 50; /* nr of slices defined */
466 static int ngrps = 1; /* nr. of groups */
467 static gmx_bool bSymmetrize = FALSE;
468 static gmx_bool bCenter = FALSE;
470 { "-d", FALSE, etSTR, {&axtitle},
471 "Take the normal on the membrane in direction X, Y or Z." },
472 { "-sl", FALSE, etINT, {&nslices},
473 "Divide the box in this number of slices." },
474 { "-dens", FALSE, etENUM, {dens_opt},
476 { "-ng", FALSE, etINT, {&ngrps},
477 "Number of groups of which to compute densities." },
478 { "-symm", FALSE, etBOOL, {&bSymmetrize},
479 "Symmetrize the density along the axis, with respect to the center. Useful for bilayers." },
480 { "-center", FALSE, etBOOL, {&bCenter},
481 "Shift the center of mass along the axis to zero. This means if your axis is Z and your box is bX, bY, bZ, the center of mass will be at bX/2, bY/2, 0."}
484 const char *bugs[] = {
485 "When calculating electron densities, atomnames are used instead of types. This is bad.",
488 double **density; /* density per slice */
489 real slWidth; /* width of one slice */
490 char **grpname; /* groupnames */
491 int nr_electrons; /* nr. electrons */
492 int *ngx; /* sizes of groups */
493 t_electron *el_tab; /* tabel with nr. of electrons*/
494 t_topology *top; /* topology */
496 atom_id **index; /* indices for all groups */
499 t_filenm fnm[] = { /* files for g_density */
500 { efTRX, "-f", NULL, ffREAD },
501 { efNDX, NULL, NULL, ffOPTRD },
502 { efTPX, NULL, NULL, ffREAD },
503 { efDAT, "-ei", "electrons", ffOPTRD }, /* file with nr. of electrons */
504 { efXVG, "-o", "density", ffWRITE },
507 #define NFILE asize(fnm)
509 if (!parse_common_args(&argc, argv, PCA_CAN_VIEW | PCA_CAN_TIME | PCA_BE_NICE,
510 NFILE, fnm, asize(pa), pa, asize(desc), desc, asize(bugs), bugs,
516 if (bSymmetrize && !bCenter)
518 fprintf(stderr, "Can not symmetrize without centering. Turning on -center\n");
522 axis = toupper(axtitle[0]) - 'X';
524 top = read_top(ftp2fn(efTPX, NFILE, fnm), &ePBC); /* read topology file */
525 if (dens_opt[0][0] == 'n')
527 for (i = 0; (i < top->atoms.nr); i++)
529 top->atoms.atom[i].m = 1;
532 else if (dens_opt[0][0] == 'c')
534 for (i = 0; (i < top->atoms.nr); i++)
536 top->atoms.atom[i].m = top->atoms.atom[i].q;
540 snew(grpname, ngrps);
544 get_index(&top->atoms, ftp2fn_null(efNDX, NFILE, fnm), ngrps, ngx, index, grpname);
546 if (dens_opt[0][0] == 'e')
548 nr_electrons = get_electrons(&el_tab, ftp2fn(efDAT, NFILE, fnm));
549 fprintf(stderr, "Read %d atomtypes from datafile\n", nr_electrons);
551 calc_electron_density(ftp2fn(efTRX, NFILE, fnm), index, ngx, &density,
552 &nslices, top, ePBC, axis, ngrps, &slWidth, el_tab,
553 nr_electrons, bCenter, oenv);
557 calc_density(ftp2fn(efTRX, NFILE, fnm), index, ngx, &density, &nslices, top,
558 ePBC, axis, ngrps, &slWidth, bCenter, oenv);
561 plot_density(density, opt2fn("-o", NFILE, fnm),
562 nslices, ngrps, grpname, slWidth, dens_opt,
565 do_view(oenv, opt2fn("-o", NFILE, fnm), "-nxy"); /* view xvgr file */