Moved second half of gmxana tools to C++
[gromacs.git] / src / gromacs / gmxana / gmx_nmens.cpp
blobb53f3b0d602d44ecfdc9765187efc2a90f563465
1 /*
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, 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.
37 #include "gmxpre.h"
39 #include <cmath>
40 #include <cstring>
42 #include "gromacs/commandline/pargs.h"
43 #include "gromacs/fileio/confio.h"
44 #include "gromacs/fileio/pdbio.h"
45 #include "gromacs/fileio/trxio.h"
46 #include "gromacs/gmxana/eigio.h"
47 #include "gromacs/gmxana/gmx_ana.h"
48 #include "gromacs/legacyheaders/macros.h"
49 #include "gromacs/legacyheaders/txtdump.h"
50 #include "gromacs/legacyheaders/typedefs.h"
51 #include "gromacs/math/units.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/random/random.h"
54 #include "gromacs/topology/index.h"
55 #include "gromacs/utility/cstringutil.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/smalloc.h"
61 int gmx_nmens(int argc, char *argv[])
63 const char *desc[] = {
64 "[THISMODULE] generates an ensemble around an average structure",
65 "in a subspace that is defined by a set of normal modes (eigenvectors).",
66 "The eigenvectors are assumed to be mass-weighted.",
67 "The position along each eigenvector is randomly taken from a Gaussian",
68 "distribution with variance kT/eigenvalue.[PAR]",
69 "By default the starting eigenvector is set to 7, since the first six",
70 "normal modes are the translational and rotational degrees of freedom."
72 static int nstruct = 100, first = 7, last = -1, seed = -1;
73 static real temp = 300.0;
74 t_pargs pa[] = {
75 { "-temp", FALSE, etREAL, {&temp},
76 "Temperature in Kelvin" },
77 { "-seed", FALSE, etINT, {&seed},
78 "Random seed, -1 generates a seed from time and pid" },
79 { "-num", FALSE, etINT, {&nstruct},
80 "Number of structures to generate" },
81 { "-first", FALSE, etINT, {&first},
82 "First eigenvector to use (-1 is select)" },
83 { "-last", FALSE, etINT, {&last},
84 "Last eigenvector to use (-1 is till the last)" }
86 #define NPA asize(pa)
88 t_trxstatus *out;
89 t_topology top;
90 int ePBC;
91 t_atoms *atoms;
92 rvec *xtop, *xref, *xav, *xout1, *xout2;
93 gmx_bool bDMR, bDMA, bFit;
94 int nvec, *eignr = NULL;
95 rvec **eigvec = NULL;
96 matrix box;
97 real *eigval, *invsqrtm, t, disp;
98 int natoms;
99 char *grpname, title[STRLEN];
100 const char *indexfile;
101 int i, j, d, s, v;
102 int nout, *iout, noutvec, *outvec;
103 atom_id *index;
104 real rfac, rhalf, jr;
105 output_env_t oenv;
106 gmx_rng_t rng;
107 int jran;
108 const unsigned long im = 0xffff;
109 const unsigned long ia = 1093;
110 const unsigned long ic = 18257;
113 t_filenm fnm[] = {
114 { efTRN, "-v", "eigenvec", ffREAD },
115 { efXVG, "-e", "eigenval", ffREAD },
116 { efTPS, NULL, NULL, ffREAD },
117 { efNDX, NULL, NULL, ffOPTRD },
118 { efTRO, "-o", "ensemble", ffWRITE }
120 #define NFILE asize(fnm)
122 if (!parse_common_args(&argc, argv, 0,
123 NFILE, fnm, NPA, pa, asize(desc), desc, 0, NULL, &oenv))
125 return 0;
128 indexfile = ftp2fn_null(efNDX, NFILE, fnm);
130 read_eigenvectors(opt2fn("-v", NFILE, fnm), &natoms, &bFit,
131 &xref, &bDMR, &xav, &bDMA, &nvec, &eignr, &eigvec, &eigval);
133 read_tps_conf(ftp2fn(efTPS, NFILE, fnm), title, &top, &ePBC, &xtop, NULL, box, bDMA);
134 atoms = &top.atoms;
136 printf("\nSelect an index group of %d elements that corresponds to the eigenvectors\n", natoms);
137 get_index(atoms, indexfile, 1, &i, &index, &grpname);
138 if (i != natoms)
140 gmx_fatal(FARGS, "you selected a group with %d elements instead of %d",
141 i, natoms);
143 printf("\n");
145 snew(invsqrtm, natoms);
146 if (bDMA)
148 for (i = 0; (i < natoms); i++)
150 invsqrtm[i] = gmx_invsqrt(atoms->atom[index[i]].m);
153 else
155 for (i = 0; (i < natoms); i++)
157 invsqrtm[i] = 1.0;
161 if (last == -1)
163 last = natoms*DIM;
165 if (first > -1)
167 /* make an index from first to last */
168 nout = last-first+1;
169 snew(iout, nout);
170 for (i = 0; i < nout; i++)
172 iout[i] = first-1+i;
175 else
177 printf("Select eigenvectors for output, end your selection with 0\n");
178 nout = -1;
179 iout = NULL;
182 nout++;
183 srenew(iout, nout+1);
184 if (1 != scanf("%d", &iout[nout]))
186 gmx_fatal(FARGS, "Error reading user input");
188 iout[nout]--;
190 while (iout[nout] >= 0);
191 printf("\n");
194 /* make an index of the eigenvectors which are present */
195 snew(outvec, nout);
196 noutvec = 0;
197 for (i = 0; i < nout; i++)
199 j = 0;
200 while ((j < nvec) && (eignr[j] != iout[i]))
202 j++;
204 if ((j < nvec) && (eignr[j] == iout[i]))
206 outvec[noutvec] = j;
207 iout[noutvec] = iout[i];
208 noutvec++;
212 fprintf(stderr, "%d eigenvectors selected for output\n", noutvec);
214 if (seed == -1)
216 seed = static_cast<int>(gmx_rng_make_seed());
217 rng = gmx_rng_init(seed);
219 else
221 rng = gmx_rng_init(seed);
223 fprintf(stderr, "Using seed %d and a temperature of %g K\n", seed, temp);
225 snew(xout1, natoms);
226 snew(xout2, atoms->nr);
227 out = open_trx(ftp2fn(efTRO, NFILE, fnm), "w");
228 jran = static_cast<int>(im*gmx_rng_uniform_real(rng));
229 gmx_rng_destroy(rng);
230 for (s = 0; s < nstruct; s++)
232 for (i = 0; i < natoms; i++)
234 copy_rvec(xav[i], xout1[i]);
236 for (j = 0; j < noutvec; j++)
238 v = outvec[j];
239 /* (r-0.5) n times: var_n = n * var_1 = n/12
240 n=4: var_n = 1/3, so multiply with 3 */
242 rfac = std::sqrt(3.0 * BOLTZ*temp/eigval[iout[j]]);
243 rhalf = 2.0*rfac;
244 rfac = rfac/im;
246 jran = (jran*ia+ic) & im;
247 jr = jran;
248 jran = (jran*ia+ic) & im;
249 jr += jran;
250 jran = (jran*ia+ic) & im;
251 jr += jran;
252 jran = (jran*ia+ic) & im;
253 jr += jran;
254 disp = rfac * jr - rhalf;
256 for (i = 0; i < natoms; i++)
258 for (d = 0; d < DIM; d++)
260 xout1[i][d] += disp*eigvec[v][i][d]*invsqrtm[i];
264 for (i = 0; i < natoms; i++)
266 copy_rvec(xout1[i], xout2[index[i]]);
268 t = s+1;
269 write_trx(out, natoms, index, atoms, 0, t, box, xout2, NULL, NULL);
270 fprintf(stderr, "\rGenerated %d structures", s+1);
272 fprintf(stderr, "\n");
273 close_trx(out);
275 return 0;