Remove unused function generate_excls and make clean_excls static
[gromacs.git] / src / gromacs / gmxana / gmx_nmtraj.cpp
blobb7777611e854aeda2e672b04e4147c45af640059
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,2017,2019, 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 <cctype>
40 #include <cmath>
41 #include <cstdlib>
42 #include <cstring>
44 #include "gromacs/commandline/pargs.h"
45 #include "gromacs/fileio/confio.h"
46 #include "gromacs/fileio/pdbio.h"
47 #include "gromacs/fileio/trxio.h"
48 #include "gromacs/gmxana/eigio.h"
49 #include "gromacs/gmxana/gmx_ana.h"
50 #include "gromacs/math/functions.h"
51 #include "gromacs/math/units.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/topology/topology.h"
54 #include "gromacs/utility/arraysize.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"
59 #include "gromacs/utility/strconvert.h"
60 #include "gromacs/utility/stringutil.h"
62 int gmx_nmtraj(int argc, char *argv[])
64 const char *desc[] =
66 "[THISMODULE] generates an virtual trajectory from an eigenvector, ",
67 "corresponding to a harmonic Cartesian oscillation around the average ",
68 "structure. The eigenvectors should normally be mass-weighted, but you can ",
69 "use non-weighted eigenvectors to generate orthogonal motions. ",
70 "The output frames are written as a trajectory file covering an entire period, and ",
71 "the first frame is the average structure. If you write the trajectory in (or convert to) ",
72 "PDB format you can view it directly in PyMol and also render a photorealistic movie. ",
73 "Motion amplitudes are calculated from the eigenvalues and a preset temperature, ",
74 "assuming equipartition of the energy over all modes. To make the motion clearly visible ",
75 "in PyMol you might want to amplify it by setting an unrealistically high temperature. ",
76 "However, be aware that both the linear Cartesian displacements and mass weighting will ",
77 "lead to serious structure deformation for high amplitudes - this is is simply a limitation ",
78 "of the Cartesian normal mode model. By default the selected eigenvector is set to 7, since ",
79 "the first six normal modes are the translational and rotational degrees of freedom."
82 static real refamplitude = 0.25;
83 static int nframes = 30;
84 static real temp = 300.0;
85 static const char *eignrvec = "7";
86 static const char *phasevec = "0.0";
88 t_pargs pa[] =
90 { "-eignr", FALSE, etSTR, {&eignrvec}, "String of eigenvectors to use (first is 1)" },
91 { "-phases", FALSE, etSTR, {&phasevec}, "String of phases (default is 0.0)" },
92 { "-temp", FALSE, etREAL, {&temp}, "Temperature (K)" },
93 { "-amplitude", FALSE, etREAL, {&refamplitude}, "Amplitude for modes with eigenvalue<=0" },
94 { "-nframes", FALSE, etINT, {&nframes}, "Number of frames to generate" }
97 #define NPA asize(pa)
99 t_trxstatus *out;
100 t_topology top;
101 int ePBC;
102 t_atoms *atoms;
103 rvec *xtop, *xref, *xav, *xout;
104 int nvec, *eignr = nullptr;
105 rvec **eigvec = nullptr;
106 matrix box;
107 int natoms;
108 int i, j, k, kmode, d;
109 gmx_bool bDMR, bDMA, bFit;
111 real * eigval;
112 int * dummy;
113 real * invsqrtm;
114 int *out_eigidx;
115 rvec * this_eigvec;
116 real omega, Ekin, m, vel;
117 real *amplitude;
118 gmx_output_env_t *oenv;
120 t_filenm fnm[] =
122 { efTPS, nullptr, nullptr, ffREAD },
123 { efTRN, "-v", "eigenvec", ffREAD },
124 { efTRO, "-o", "nmtraj", ffWRITE }
127 #define NFILE asize(fnm)
129 if (!parse_common_args(&argc, argv, 0,
130 NFILE, fnm, NPA, pa, asize(desc), desc, 0, nullptr, &oenv))
132 return 0;
135 read_eigenvectors(opt2fn("-v", NFILE, fnm), &natoms, &bFit,
136 &xref, &bDMR, &xav, &bDMA, &nvec, &eignr, &eigvec, &eigval);
138 read_tps_conf(ftp2fn(efTPS, NFILE, fnm), &top, &ePBC, &xtop, nullptr, box, bDMA);
140 /* Find vectors and phases */
142 std::vector<int> imodes;
143 for (const auto &imodeString : gmx::splitString(eignrvec))
145 imodes.emplace_back(gmx::fromStdString<int>(imodeString));
147 int nmodes = gmx::ssize(imodes);
149 std::vector<real> phases;
150 phases.reserve(nmodes);
151 for (const auto &phaseString : gmx::splitString(phasevec))
153 phases.emplace_back(gmx::fromStdString<int>(phaseString));
155 int nphases = gmx::ssize(phases);
157 if (nphases > nmodes)
159 gmx_fatal(FARGS, "More phases than eigenvector indices specified.\n");
162 if (nmodes > nphases)
164 printf("Warning: Setting phase of last %d modes to zero...\n", nmodes-nphases);
165 phases.resize(nmodes, 0);
168 atoms = &top.atoms;
170 if (atoms->nr != natoms)
172 gmx_fatal(FARGS, "Different number of atoms in topology and eigenvectors.\n");
175 snew(dummy, natoms);
176 for (i = 0; i < natoms; i++)
178 dummy[i] = i;
181 /* Find the eigenvalue/vector to match our select one */
182 snew(out_eigidx, nmodes);
183 for (i = 0; i < nmodes; i++)
185 out_eigidx[i] = -1;
188 for (i = 0; i < nvec; i++)
190 for (j = 0; j < nmodes; j++)
192 if (imodes[j] == eignr[i])
194 out_eigidx[j] = i;
198 for (i = 0; i < nmodes; i++)
200 if (out_eigidx[i] == -1)
202 gmx_fatal(FARGS, "Could not find mode %d in eigenvector file.\n", imodes[i]);
207 snew(invsqrtm, natoms);
209 if (bDMA)
211 for (i = 0; (i < natoms); i++)
213 invsqrtm[i] = gmx::invsqrt(atoms->atom[i].m);
216 else
218 for (i = 0; (i < natoms); i++)
220 invsqrtm[i] = 1.0;
224 snew(xout, natoms);
225 snew(amplitude, nmodes);
227 printf("mode phases: %g %g\n", phases[0], phases[1]);
229 for (i = 0; i < nmodes; i++)
231 kmode = out_eigidx[i];
232 this_eigvec = eigvec[kmode];
234 if ( (kmode >= 6) && (eigval[kmode] > 0))
236 /* Derive amplitude from temperature and eigenvalue if we can */
238 /* Convert eigenvalue to angular frequency, in units s^(-1) */
239 omega = std::sqrt(eigval[kmode]*1.0E21/(AVOGADRO*AMU));
240 /* Harmonic motion will be x=x0 + A*sin(omega*t)*eigenvec.
241 * The velocity is thus:
243 * v = A*omega*cos(omega*t)*eigenvec.
245 * And the average kinetic energy the integral of mass*v*v/2 over a
246 * period:
248 * (1/4)*mass*A*omega*eigenvec
250 * For t =2*pi*n, all energy will be kinetic, and v=A*omega*eigenvec.
251 * The kinetic energy will be sum(0.5*mass*v*v) if we temporarily set A to 1,
252 * and the average over a period half of this.
255 Ekin = 0;
256 for (k = 0; k < natoms; k++)
258 m = atoms->atom[k].m;
259 for (d = 0; d < DIM; d++)
261 vel = omega*this_eigvec[k][d];
262 Ekin += 0.5*0.5*m*vel*vel;
266 /* Convert Ekin from amu*(nm/s)^2 to J, i.e., kg*(m/s)^2
267 * This will also be proportional to A^2
269 Ekin *= AMU*1E-18;
271 /* Set the amplitude so the energy is kT/2 */
272 amplitude[i] = std::sqrt(0.5*BOLTZMANN*temp/Ekin);
274 else
276 amplitude[i] = refamplitude;
280 out = open_trx(ftp2fn(efTRO, NFILE, fnm), "w");
282 /* Write a sine oscillation around the average structure,
283 * modulated by the eigenvector with selected amplitude.
286 for (i = 0; i < nframes; i++)
288 real fraction = static_cast<real>(i)/static_cast<real>(nframes);
289 for (j = 0; j < natoms; j++)
291 copy_rvec(xav[j], xout[j]);
294 for (k = 0; k < nmodes; k++)
296 kmode = out_eigidx[k];
297 this_eigvec = eigvec[kmode];
299 for (j = 0; j < natoms; j++)
301 for (d = 0; d < DIM; d++)
303 xout[j][d] += amplitude[k]*std::sin(2*M_PI*(fraction+phases[k]/360.0))*this_eigvec[j][d];
307 write_trx(out, natoms, dummy, atoms, i, fraction, box, xout, nullptr, nullptr);
310 fprintf(stderr, "\n");
311 close_trx(out);
313 return 0;