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