905b3443cb003cd59d9283724f4b3fafc71b4a41
[alexxy/gromacs.git] / src / gromacs / tools / convert_tpr.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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,2016,2017,2018,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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "convert_tpr.h"
40
41 #include <cmath>
42
43 #include "gromacs/commandline/pargs.h"
44 #include "gromacs/fileio/checkpoint.h"
45 #include "gromacs/fileio/enxio.h"
46 #include "gromacs/fileio/tpxio.h"
47 #include "gromacs/fileio/trrio.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/mdtypes/inputrec.h"
50 #include "gromacs/mdtypes/md_enums.h"
51 #include "gromacs/mdtypes/state.h"
52 #include "gromacs/random/seed.h"
53 #include "gromacs/topology/ifunc.h"
54 #include "gromacs/topology/index.h"
55 #include "gromacs/topology/mtop_util.h"
56 #include "gromacs/topology/topology.h"
57 #include "gromacs/utility/arraysize.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/fatalerror.h"
60 #include "gromacs/utility/futil.h"
61 #include "gromacs/utility/real.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/stringutil.h"
64
65 #define RANGECHK(i, n)                                                                           \
66     if ((i) >= (n))                                                                              \
67     gmx_fatal(FARGS,                                                                             \
68               "Your index file contains atomnumbers (e.g. %d)\nthat are larger than the number " \
69               "of atoms in the tpr file (%d)",                                                   \
70               (i), (n))
71
72 static std::vector<bool> bKeepIt(int gnx, int natoms, int index[])
73 {
74     std::vector<bool> b(natoms);
75
76     for (int i = 0; (i < gnx); i++)
77     {
78         RANGECHK(index[i], natoms);
79         b[index[i]] = true;
80     }
81
82     return b;
83 }
84
85 static std::vector<int> invind(int gnx, int natoms, int index[])
86 {
87     std::vector<int> inv(natoms);
88
89     for (int i = 0; (i < gnx); i++)
90     {
91         RANGECHK(index[i], natoms);
92         inv[index[i]] = i;
93     }
94
95     return inv;
96 }
97
98 static gmx::ListOfLists<int> reduce_listoflists(gmx::ArrayRef<const int>     invindex,
99                                                 const std::vector<bool>&     bKeep,
100                                                 const gmx::ListOfLists<int>& src,
101                                                 const char*                  name)
102 {
103     gmx::ListOfLists<int> lists;
104
105     std::vector<int> exclusionsForAtom;
106     for (gmx::index i = 0; i < src.ssize(); i++)
107     {
108         if (bKeep[i])
109         {
110             exclusionsForAtom.clear();
111             for (const int j : src[i])
112             {
113                 if (bKeep[j])
114                 {
115                     exclusionsForAtom.push_back(invindex[j]);
116                 }
117             }
118             lists.pushBack(exclusionsForAtom);
119         }
120     }
121
122     fprintf(stderr, "Reduced block %8s from %6zu to %6zu index-, %6d to %6d a-entries\n", name,
123             src.size(), lists.size(), src.numElements(), lists.numElements());
124
125     return lists;
126 }
127
128 static void reduce_rvec(int gnx, const int index[], rvec vv[])
129 {
130     rvec* ptr;
131     int   i;
132
133     snew(ptr, gnx);
134     for (i = 0; (i < gnx); i++)
135     {
136         copy_rvec(vv[index[i]], ptr[i]);
137     }
138     for (i = 0; (i < gnx); i++)
139     {
140         copy_rvec(ptr[i], vv[i]);
141     }
142     sfree(ptr);
143 }
144
145 static void reduce_atom(int gnx, const int index[], t_atom atom[], char*** atomname, int* nres, t_resinfo* resinfo)
146 {
147     t_atom*    ptr;
148     char***    aname;
149     t_resinfo* rinfo;
150     int        i, nr;
151
152     snew(ptr, gnx);
153     snew(aname, gnx);
154     snew(rinfo, atom[index[gnx - 1]].resind + 1);
155     for (i = 0; (i < gnx); i++)
156     {
157         ptr[i]   = atom[index[i]];
158         aname[i] = atomname[index[i]];
159     }
160     nr = -1;
161     for (i = 0; (i < gnx); i++)
162     {
163         atom[i]     = ptr[i];
164         atomname[i] = aname[i];
165         if ((i == 0) || (atom[i].resind != atom[i - 1].resind))
166         {
167             nr++;
168             rinfo[nr] = resinfo[atom[i].resind];
169         }
170         atom[i].resind = nr;
171     }
172     nr++;
173     for (i = 0; (i < nr); i++)
174     {
175         resinfo[i] = rinfo[i];
176     }
177     *nres = nr;
178
179     sfree(aname);
180     sfree(ptr);
181     sfree(rinfo);
182 }
183
184 static void reduce_ilist(gmx::ArrayRef<const int> invindex,
185                          const std::vector<bool>& bKeep,
186                          t_ilist*                 il,
187                          int                      nratoms,
188                          const char*              name)
189 {
190     t_iatom* ia;
191     int      i, j, newnr;
192     gmx_bool bB;
193
194     if (il->nr)
195     {
196         snew(ia, il->nr);
197         newnr = 0;
198         for (i = 0; (i < il->nr); i += nratoms + 1)
199         {
200             bB = TRUE;
201             for (j = 1; (j <= nratoms); j++)
202             {
203                 bB = bB && bKeep[il->iatoms[i + j]];
204             }
205             if (bB)
206             {
207                 ia[newnr++] = il->iatoms[i];
208                 for (j = 1; (j <= nratoms); j++)
209                 {
210                     ia[newnr++] = invindex[il->iatoms[i + j]];
211                 }
212             }
213         }
214         fprintf(stderr, "Reduced ilist %8s from %6d to %6d entries\n", name, il->nr / (nratoms + 1),
215                 newnr / (nratoms + 1));
216
217         il->nr = newnr;
218         for (i = 0; (i < newnr); i++)
219         {
220             il->iatoms[i] = ia[i];
221         }
222
223         sfree(ia);
224     }
225 }
226
227 static void reduce_topology_x(int gnx, int index[], gmx_mtop_t* mtop, rvec x[], rvec v[])
228 {
229     gmx_localtop_t top;
230     gmx_mtop_generate_local_top(*mtop, &top, false);
231     t_atoms atoms = gmx_mtop_global_atoms(mtop);
232
233     const std::vector<bool> bKeep    = bKeepIt(gnx, atoms.nr, index);
234     const std::vector<int>  invindex = invind(gnx, atoms.nr, index);
235
236     reduce_rvec(gnx, index, x);
237     reduce_rvec(gnx, index, v);
238     reduce_atom(gnx, index, atoms.atom, atoms.atomname, &(atoms.nres), atoms.resinfo);
239
240     for (int i = 0; (i < F_NRE); i++)
241     {
242         reduce_ilist(invindex, bKeep, &(top.idef.il[i]), interaction_function[i].nratoms,
243                      interaction_function[i].name);
244     }
245
246     atoms.nr = gnx;
247
248     mtop->moltype.resize(1);
249     mtop->moltype[0].name  = mtop->name;
250     mtop->moltype[0].atoms = atoms;
251     mtop->moltype[0].excls = reduce_listoflists(invindex, bKeep, top.excls, "excls");
252     for (int i = 0; i < F_NRE; i++)
253     {
254         InteractionList& ilist = mtop->moltype[0].ilist[i];
255         ilist.iatoms.resize(top.idef.il[i].nr);
256         for (int j = 0; j < top.idef.il[i].nr; j++)
257         {
258             ilist.iatoms[j] = top.idef.il[i].iatoms[j];
259         }
260     }
261
262     mtop->molblock.resize(1);
263     mtop->molblock[0].type = 0;
264     mtop->molblock[0].nmol = 1;
265
266     mtop->natoms = atoms.nr;
267 }
268
269 static void zeroq(const int index[], gmx_mtop_t* mtop)
270 {
271     for (gmx_moltype_t& moltype : mtop->moltype)
272     {
273         for (int i = 0; i < moltype.atoms.nr; i++)
274         {
275             moltype.atoms.atom[index[i]].q  = 0;
276             moltype.atoms.atom[index[i]].qB = 0;
277         }
278     }
279 }
280
281 int gmx_convert_tpr(int argc, char* argv[])
282 {
283     const char* desc[] = {
284         "[THISMODULE] can edit run input files in three ways.[PAR]",
285         "[BB]1.[bb] by modifying the number of steps in a run input file",
286         "with options [TT]-extend[tt], [TT]-until[tt] or [TT]-nsteps[tt]",
287         "(nsteps=-1 means unlimited number of steps)[PAR]",
288         "[BB]2.[bb] by creating a [REF].tpx[ref] file for a subset of your original",
289         "tpx file, which is useful when you want to remove the solvent from",
290         "your [REF].tpx[ref] file, or when you want to make e.g. a pure C[GRK]alpha[grk] ",
291         "[REF].tpx[ref] file.",
292         "Note that you may need to use [TT]-nsteps -1[tt] (or similar) to get",
293         "this to work.",
294         "[BB]WARNING: this [REF].tpx[ref] file is not fully functional[bb].[PAR]",
295         "[BB]3.[bb] by setting the charges of a specified group",
296         "to zero. This is useful when doing free energy estimates",
297         "using the LIE (Linear Interaction Energy) method."
298     };
299
300     const char*       top_fn;
301     int               i;
302     int64_t           nsteps_req, run_step;
303     double            run_t, state_t;
304     gmx_bool          bSel;
305     gmx_bool          bNsteps, bExtend, bUntil;
306     gmx_mtop_t        mtop;
307     t_atoms           atoms;
308     t_state           state;
309     int               gnx;
310     char*             grpname;
311     int*              index = nullptr;
312     char              buf[200], buf2[200];
313     gmx_output_env_t* oenv;
314     t_filenm          fnm[] = { { efTPR, nullptr, nullptr, ffREAD },
315                        { efNDX, nullptr, nullptr, ffOPTRD },
316                        { efTPR, "-o", "tprout", ffWRITE } };
317 #define NFILE asize(fnm)
318
319     /* Command line options */
320     static int      nsteps_req_int = 0;
321     static real     extend_t = 0.0, until_t = 0.0;
322     static gmx_bool bZeroQ = FALSE;
323     static t_pargs  pa[]   = {
324         { "-extend", FALSE, etREAL, { &extend_t }, "Extend runtime by this amount (ps)" },
325         { "-until", FALSE, etREAL, { &until_t }, "Extend runtime until this ending time (ps)" },
326         { "-nsteps", FALSE, etINT, { &nsteps_req_int }, "Change the number of steps" },
327         { "-zeroq",
328           FALSE,
329           etBOOL,
330           { &bZeroQ },
331           "Set the charges of a group (from the index) to zero" }
332     };
333
334     /* Parse the command line */
335     if (!parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, nullptr, &oenv))
336     {
337         return 0;
338     }
339
340     /* Convert int to int64_t */
341     nsteps_req = nsteps_req_int;
342     bNsteps    = opt2parg_bSet("-nsteps", asize(pa), pa);
343     bExtend    = opt2parg_bSet("-extend", asize(pa), pa);
344     bUntil     = opt2parg_bSet("-until", asize(pa), pa);
345
346     top_fn = ftp2fn(efTPR, NFILE, fnm);
347     fprintf(stderr, "Reading toplogy and stuff from %s\n", top_fn);
348
349     t_inputrec  irInstance;
350     t_inputrec* ir = &irInstance;
351     read_tpx_state(top_fn, ir, &state, &mtop);
352     run_step = ir->init_step;
353     run_t    = ir->init_step * ir->delta_t + ir->init_t;
354
355     if (bNsteps)
356     {
357         fprintf(stderr, "Setting nsteps to %s\n", gmx_step_str(nsteps_req, buf));
358         ir->nsteps = nsteps_req;
359     }
360     else
361     {
362         /* Determine total number of steps remaining */
363         if (bExtend)
364         {
365             ir->nsteps = ir->nsteps - (run_step - ir->init_step) + gmx::roundToInt64(extend_t / ir->delta_t);
366             printf("Extending remaining runtime of by %g ps (now %s steps)\n", extend_t,
367                    gmx_step_str(ir->nsteps, buf));
368         }
369         else if (bUntil)
370         {
371             printf("nsteps = %s, run_step = %s, current_t = %g, until = %g\n",
372                    gmx_step_str(ir->nsteps, buf), gmx_step_str(run_step, buf2), run_t, until_t);
373             ir->nsteps = gmx::roundToInt64((until_t - run_t) / ir->delta_t);
374             printf("Extending remaining runtime until %g ps (now %s steps)\n", until_t,
375                    gmx_step_str(ir->nsteps, buf));
376         }
377         else
378         {
379             ir->nsteps -= run_step - ir->init_step;
380             /* Print message */
381             printf("%s steps (%g ps) remaining from first run.\n", gmx_step_str(ir->nsteps, buf),
382                    ir->nsteps * ir->delta_t);
383         }
384     }
385
386     if (bNsteps || bZeroQ || (ir->nsteps > 0))
387     {
388         ir->init_step = run_step;
389
390         if (ftp2bSet(efNDX, NFILE, fnm) || !(bNsteps || bExtend || bUntil))
391         {
392             atoms = gmx_mtop_global_atoms(&mtop);
393             get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &gnx, &index, &grpname);
394             if (!bZeroQ)
395             {
396                 bSel = (gnx != state.natoms);
397                 for (i = 0; ((i < gnx) && (!bSel)); i++)
398                 {
399                     bSel = (i != index[i]);
400                 }
401             }
402             else
403             {
404                 bSel = FALSE;
405             }
406             if (bSel)
407             {
408                 fprintf(stderr,
409                         "Will write subset %s of original tpx containing %d "
410                         "atoms\n",
411                         grpname, gnx);
412                 reduce_topology_x(gnx, index, &mtop, state.x.rvec_array(), state.v.rvec_array());
413                 state.natoms = gnx;
414             }
415             else if (bZeroQ)
416             {
417                 zeroq(index, &mtop);
418                 fprintf(stderr, "Zero-ing charges for group %s\n", grpname);
419             }
420             else
421             {
422                 fprintf(stderr, "Will write full tpx file (no selection)\n");
423             }
424         }
425
426         state_t = ir->init_t + ir->init_step * ir->delta_t;
427         sprintf(buf, "Writing statusfile with starting step %s%s and length %s%s steps...\n", "%10",
428                 PRId64, "%10", PRId64);
429         fprintf(stderr, buf, ir->init_step, ir->nsteps);
430         fprintf(stderr, "                                 time %10.3f and length %10.3f ps\n",
431                 state_t, ir->nsteps * ir->delta_t);
432         write_tpx_state(opt2fn("-o", NFILE, fnm), ir, &state, &mtop);
433     }
434     else
435     {
436         printf("You've simulated long enough. Not writing tpr file\n");
437     }
438
439     return 0;
440 }