3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Green Red Orange Magenta Azure Cyan Skyblue
47 #include "gmx_fatal.h"
49 #include "gromacs/fileio/futil.h"
52 #include "gromacs/fileio/pdbio.h"
53 #include "gromacs/fileio/tpxio.h"
54 #include "gromacs/fileio/trxio.h"
62 int gmx_nmens(int argc, char *argv[])
64 const char *desc[] = {
65 "[THISMODULE] generates an ensemble around an average structure",
66 "in a subspace that is defined by a set of normal modes (eigenvectors).",
67 "The eigenvectors are assumed to be mass-weighted.",
68 "The position along each eigenvector is randomly taken from a Gaussian",
69 "distribution with variance kT/eigenvalue.[PAR]",
70 "By default the starting eigenvector is set to 7, since the first six",
71 "normal modes are the translational and rotational degrees of freedom."
73 static int nstruct = 100, first = 7, last = -1, seed = -1;
74 static real temp = 300.0;
76 { "-temp", FALSE, etREAL, {&temp},
77 "Temperature in Kelvin" },
78 { "-seed", FALSE, etINT, {&seed},
79 "Random seed, -1 generates a seed from time and pid" },
80 { "-num", FALSE, etINT, {&nstruct},
81 "Number of structures to generate" },
82 { "-first", FALSE, etINT, {&first},
83 "First eigenvector to use (-1 is select)" },
84 { "-last", FALSE, etINT, {&last},
85 "Last eigenvector to use (-1 is till the last)" }
94 rvec *xtop, *xref, *xav, *xout1, *xout2;
95 gmx_bool bDMR, bDMA, bFit;
96 int nvec, *eignr = NULL;
99 real *eigval, totmass, *invsqrtm, t, disp;
101 char *grpname, title[STRLEN];
102 const char *indexfile;
104 int nout, *iout, noutvec, *outvec;
106 real rfac, invfr, rhalf, jr;
111 const unsigned long im = 0xffff;
112 const unsigned long ia = 1093;
113 const unsigned long ic = 18257;
117 { efTRN, "-v", "eigenvec", ffREAD },
118 { efXVG, "-e", "eigenval", ffREAD },
119 { efTPS, NULL, NULL, ffREAD },
120 { efNDX, NULL, NULL, ffOPTRD },
121 { efTRO, "-o", "ensemble", ffWRITE }
123 #define NFILE asize(fnm)
125 if (!parse_common_args(&argc, argv, PCA_BE_NICE,
126 NFILE, fnm, NPA, pa, asize(desc), desc, 0, NULL, &oenv))
131 indexfile = ftp2fn_null(efNDX, NFILE, fnm);
133 read_eigenvectors(opt2fn("-v", NFILE, fnm), &natoms, &bFit,
134 &xref, &bDMR, &xav, &bDMA, &nvec, &eignr, &eigvec, &eigval);
136 read_tps_conf(ftp2fn(efTPS, NFILE, fnm), title, &top, &ePBC, &xtop, NULL, box, bDMA);
139 printf("\nSelect an index group of %d elements that corresponds to the eigenvectors\n", natoms);
140 get_index(atoms, indexfile, 1, &i, &index, &grpname);
143 gmx_fatal(FARGS, "you selected a group with %d elements instead of %d",
148 snew(invsqrtm, natoms);
151 for (i = 0; (i < natoms); i++)
153 invsqrtm[i] = gmx_invsqrt(atoms->atom[index[i]].m);
158 for (i = 0; (i < natoms); i++)
170 /* make an index from first to last */
173 for (i = 0; i < nout; i++)
180 printf("Select eigenvectors for output, end your selection with 0\n");
186 srenew(iout, nout+1);
187 if (1 != scanf("%d", &iout[nout]))
189 gmx_fatal(FARGS, "Error reading user input");
193 while (iout[nout] >= 0);
197 /* make an index of the eigenvectors which are present */
200 for (i = 0; i < nout; i++)
203 while ((j < nvec) && (eignr[j] != iout[i]))
207 if ((j < nvec) && (eignr[j] == iout[i]))
210 iout[noutvec] = iout[i];
215 fprintf(stderr, "%d eigenvectors selected for output\n", noutvec);
221 fprintf(stderr, "Using seed %d and a temperature of %g K\n", seed, temp);
224 snew(xout2, atoms->nr);
225 out = open_trx(ftp2fn(efTRO, NFILE, fnm), "w");
226 jran = (unsigned long)((real)im*rando(&seed));
227 for (s = 0; s < nstruct; s++)
229 for (i = 0; i < natoms; i++)
231 copy_rvec(xav[i], xout1[i]);
233 for (j = 0; j < noutvec; j++)
236 /* (r-0.5) n times: var_n = n * var_1 = n/12
237 n=4: var_n = 1/3, so multiply with 3 */
239 rfac = sqrt(3.0 * BOLTZ*temp/eigval[iout[j]]);
241 rfac = rfac/(real)im;
243 jran = (jran*ia+ic) & im;
245 jran = (jran*ia+ic) & im;
247 jran = (jran*ia+ic) & im;
249 jran = (jran*ia+ic) & im;
251 disp = rfac * jr - rhalf;
253 for (i = 0; i < natoms; i++)
255 for (d = 0; d < DIM; d++)
257 xout1[i][d] += disp*eigvec[v][i][d]*invsqrtm[i];
261 for (i = 0; i < natoms; i++)
263 copy_rvec(xout1[i], xout2[index[i]]);
266 write_trx(out, natoms, index, atoms, 0, t, box, xout2, NULL, NULL);
267 fprintf(stderr, "\rGenerated %d structures", s+1);
269 fprintf(stderr, "\n");