58921b2cc2198badda41b5659e784ead2a9d768f
[alexxy/gromacs.git] / src / gromacs / tools / convert_tpr.c
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, 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 "config.h"
40
41 #include <math.h>
42
43 #include "gromacs/legacyheaders/types/inputrec.h"
44 #include "gromacs/legacyheaders/types/simple.h"
45 #include "gromacs/legacyheaders/types/state.h"
46 #include "gromacs/topology/index.h"
47 #include "gromacs/legacyheaders/macros.h"
48 #include "gromacs/legacyheaders/names.h"
49 #include "gromacs/gmxpreprocess/readir.h"
50 #include "gromacs/topology/mtop_util.h"
51 #include "gromacs/legacyheaders/checkpoint.h"
52 #include "gromacs/fileio/tpxio.h"
53 #include "gromacs/fileio/trnio.h"
54 #include "gromacs/fileio/enxio.h"
55 #include "gromacs/utility/futil.h"
56
57 #include "gromacs/commandline/pargs.h"
58 #include "gromacs/math/vec.h"
59 #include "gromacs/random/random.h"
60 #include "gromacs/topology/topology.h"
61 #include "gromacs/utility/fatalerror.h"
62 #include "gromacs/utility/smalloc.h"
63
64 #define RANGECHK(i, n) if ((i) >= (n)) gmx_fatal(FARGS, "Your index file contains atomnumbers (e.g. %d)\nthat are larger than the number of atoms in the tpr file (%d)", (i), (n))
65
66 static gmx_bool *bKeepIt(int gnx, int natoms, atom_id index[])
67 {
68     gmx_bool *b;
69     int       i;
70
71     snew(b, natoms);
72     for (i = 0; (i < gnx); i++)
73     {
74         RANGECHK(index[i], natoms);
75         b[index[i]] = TRUE;
76     }
77
78     return b;
79 }
80
81 static atom_id *invind(int gnx, int natoms, atom_id index[])
82 {
83     atom_id *inv;
84     int      i;
85
86     snew(inv, natoms);
87     for (i = 0; (i < gnx); i++)
88     {
89         RANGECHK(index[i], natoms);
90         inv[index[i]] = i;
91     }
92
93     return inv;
94 }
95
96 static void reduce_block(gmx_bool bKeep[], t_block *block,
97                          const char *name)
98 {
99     atom_id *index;
100     int      i, j, newi, newj;
101
102     snew(index, block->nr);
103
104     newi = newj = 0;
105     for (i = 0; (i < block->nr); i++)
106     {
107         for (j = block->index[i]; (j < block->index[i+1]); j++)
108         {
109             if (bKeep[j])
110             {
111                 newj++;
112             }
113         }
114         if (newj > index[newi])
115         {
116             newi++;
117             index[newi] = newj;
118         }
119     }
120
121     fprintf(stderr, "Reduced block %8s from %6d to %6d index-, %6d to %6d a-entries\n",
122             name, block->nr, newi, block->index[block->nr], newj);
123     block->index = index;
124     block->nr    = newi;
125 }
126
127 static void reduce_blocka(atom_id invindex[], gmx_bool bKeep[], t_blocka *block,
128                           const char *name)
129 {
130     atom_id *index, *a;
131     int      i, j, k, newi, newj;
132
133     snew(index, block->nr);
134     snew(a, block->nra);
135
136     newi = newj = 0;
137     for (i = 0; (i < block->nr); i++)
138     {
139         for (j = block->index[i]; (j < block->index[i+1]); j++)
140         {
141             k = block->a[j];
142             if (bKeep[k])
143             {
144                 a[newj] = invindex[k];
145                 newj++;
146             }
147         }
148         if (newj > index[newi])
149         {
150             newi++;
151             index[newi] = newj;
152         }
153     }
154
155     fprintf(stderr, "Reduced block %8s from %6d to %6d index-, %6d to %6d a-entries\n",
156             name, block->nr, newi, block->nra, newj);
157     block->index = index;
158     block->a     = a;
159     block->nr    = newi;
160     block->nra   = newj;
161 }
162
163 static void reduce_rvec(int gnx, atom_id index[], rvec vv[])
164 {
165     rvec *ptr;
166     int   i;
167
168     snew(ptr, gnx);
169     for (i = 0; (i < gnx); i++)
170     {
171         copy_rvec(vv[index[i]], ptr[i]);
172     }
173     for (i = 0; (i < gnx); i++)
174     {
175         copy_rvec(ptr[i], vv[i]);
176     }
177     sfree(ptr);
178 }
179
180 static void reduce_atom(int gnx, atom_id index[], t_atom atom[], char ***atomname,
181                         int *nres, t_resinfo *resinfo)
182 {
183     t_atom    *ptr;
184     char    ***aname;
185     t_resinfo *rinfo;
186     int        i, nr;
187
188     snew(ptr, gnx);
189     snew(aname, gnx);
190     snew(rinfo, atom[index[gnx-1]].resind+1);
191     for (i = 0; (i < gnx); i++)
192     {
193         ptr[i]   = atom[index[i]];
194         aname[i] = atomname[index[i]];
195     }
196     nr = -1;
197     for (i = 0; (i < gnx); i++)
198     {
199         atom[i]     = ptr[i];
200         atomname[i] = aname[i];
201         if ((i == 0) || (atom[i].resind != atom[i-1].resind))
202         {
203             nr++;
204             rinfo[nr] = resinfo[atom[i].resind];
205         }
206         atom[i].resind = nr;
207     }
208     nr++;
209     for (i = 0; (i < nr); i++)
210     {
211         resinfo[i] = rinfo[i];
212     }
213     *nres = nr;
214
215     sfree(aname);
216     sfree(ptr);
217     sfree(rinfo);
218 }
219
220 static void reduce_ilist(atom_id invindex[], gmx_bool bKeep[],
221                          t_ilist *il, int nratoms, const char *name)
222 {
223     t_iatom *ia;
224     int      i, j, newnr;
225     gmx_bool bB;
226
227     if (il->nr)
228     {
229         snew(ia, il->nr);
230         newnr = 0;
231         for (i = 0; (i < il->nr); i += nratoms+1)
232         {
233             bB = TRUE;
234             for (j = 1; (j <= nratoms); j++)
235             {
236                 bB = bB && bKeep[il->iatoms[i+j]];
237             }
238             if (bB)
239             {
240                 ia[newnr++] = il->iatoms[i];
241                 for (j = 1; (j <= nratoms); j++)
242                 {
243                     ia[newnr++] = invindex[il->iatoms[i+j]];
244                 }
245             }
246         }
247         fprintf(stderr, "Reduced ilist %8s from %6d to %6d entries\n",
248                 name, il->nr/(nratoms+1),
249                 newnr/(nratoms+1));
250
251         il->nr = newnr;
252         for (i = 0; (i < newnr); i++)
253         {
254             il->iatoms[i] = ia[i];
255         }
256
257         sfree(ia);
258     }
259 }
260
261 static void reduce_topology_x(int gnx, atom_id index[],
262                               gmx_mtop_t *mtop, rvec x[], rvec v[])
263 {
264     t_topology   top;
265     gmx_bool    *bKeep;
266     atom_id     *invindex;
267     int          i;
268
269     top      = gmx_mtop_t_to_t_topology(mtop);
270     bKeep    = bKeepIt(gnx, top.atoms.nr, index);
271     invindex = invind(gnx, top.atoms.nr, index);
272
273     reduce_block(bKeep, &(top.cgs), "cgs");
274     reduce_block(bKeep, &(top.mols), "mols");
275     reduce_blocka(invindex, bKeep, &(top.excls), "excls");
276     reduce_rvec(gnx, index, x);
277     reduce_rvec(gnx, index, v);
278     reduce_atom(gnx, index, top.atoms.atom, top.atoms.atomname,
279                 &(top.atoms.nres), top.atoms.resinfo);
280
281     for (i = 0; (i < F_NRE); i++)
282     {
283         reduce_ilist(invindex, bKeep, &(top.idef.il[i]),
284                      interaction_function[i].nratoms,
285                      interaction_function[i].name);
286     }
287
288     top.atoms.nr = gnx;
289
290     mtop->nmoltype = 1;
291     snew(mtop->moltype, mtop->nmoltype);
292     mtop->moltype[0].name  = mtop->name;
293     mtop->moltype[0].atoms = top.atoms;
294     for (i = 0; i < F_NRE; i++)
295     {
296         mtop->moltype[0].ilist[i] = top.idef.il[i];
297     }
298     mtop->moltype[0].atoms = top.atoms;
299     mtop->moltype[0].cgs   = top.cgs;
300     mtop->moltype[0].excls = top.excls;
301
302     mtop->nmolblock = 1;
303     snew(mtop->molblock, mtop->nmolblock);
304     mtop->molblock[0].type       = 0;
305     mtop->molblock[0].nmol       = 1;
306     mtop->molblock[0].natoms_mol = top.atoms.nr;
307     mtop->molblock[0].nposres_xA = 0;
308     mtop->molblock[0].nposres_xB = 0;
309
310     mtop->natoms                 = top.atoms.nr;
311 }
312
313 static void zeroq(atom_id index[], gmx_mtop_t *mtop)
314 {
315     int mt, i;
316
317     for (mt = 0; mt < mtop->nmoltype; mt++)
318     {
319         for (i = 0; (i < mtop->moltype[mt].atoms.nr); i++)
320         {
321             mtop->moltype[mt].atoms.atom[index[i]].q  = 0;
322             mtop->moltype[mt].atoms.atom[index[i]].qB = 0;
323         }
324     }
325 }
326
327 int gmx_convert_tpr(int argc, char *argv[])
328 {
329     const char       *desc[] = {
330         "[THISMODULE] can edit run input files in four ways.[PAR]",
331         "[BB]1.[bb] by modifying the number of steps in a run input file",
332         "with options [TT]-extend[tt], [TT]-until[tt] or [TT]-nsteps[tt]",
333         "(nsteps=-1 means unlimited number of steps)[PAR]",
334         "[BB]2.[bb] (OBSOLETE) by creating a run input file",
335         "for a continuation run when your simulation has crashed due to e.g.",
336         "a full disk, or by making a continuation run input file.",
337         "This option is obsolete, since mdrun now writes and reads",
338         "checkpoint files.",
339         "[BB]Note[bb] that a frame with coordinates and velocities is needed.",
340         "When pressure and/or Nose-Hoover temperature coupling is used",
341         "an energy file can be supplied to get an exact continuation",
342         "of the original run.[PAR]",
343         "[BB]3.[bb] by creating a [TT].tpx[tt] file for a subset of your original",
344         "tpx file, which is useful when you want to remove the solvent from",
345         "your [TT].tpx[tt] file, or when you want to make e.g. a pure C[GRK]alpha[grk] [TT].tpx[tt] file.",
346         "Note that you may need to use [TT]-nsteps -1[tt] (or similar) to get",
347         "this to work.",
348         "[BB]WARNING: this [TT].tpx[tt] file is not fully functional[bb].[PAR]",
349         "[BB]4.[bb] by setting the charges of a specified group",
350         "to zero. This is useful when doing free energy estimates",
351         "using the LIE (Linear Interaction Energy) method."
352     };
353
354     const char       *top_fn, *frame_fn;
355     t_fileio         *fp;
356     ener_file_t       fp_ener = NULL;
357     t_trnheader       head;
358     int               i;
359     gmx_int64_t       nsteps_req, run_step, frame;
360     double            run_t, state_t;
361     gmx_bool          bOK, bNsteps, bExtend, bUntil, bTime, bTraj;
362     gmx_bool          bFrame, bUse, bSel, bNeedEner, bReadEner, bScanEner, bFepState;
363     gmx_mtop_t        mtop;
364     t_atoms           atoms;
365     t_inputrec       *ir, *irnew = NULL;
366     t_gromppopts     *gopts;
367     t_state           state;
368     rvec             *newx = NULL, *newv = NULL, *tmpx, *tmpv;
369     matrix            newbox;
370     int               gnx;
371     char             *grpname;
372     atom_id          *index = NULL;
373     int               nre;
374     gmx_enxnm_t      *enm     = NULL;
375     t_enxframe       *fr_ener = NULL;
376     char              buf[200], buf2[200];
377     output_env_t      oenv;
378     t_filenm          fnm[] = {
379         { efTPX, NULL,  NULL,    ffREAD  },
380         { efTRN, "-f",  NULL,    ffOPTRD },
381         { efEDR, "-e",  NULL,    ffOPTRD },
382         { efNDX, NULL,  NULL,    ffOPTRD },
383         { efTPX, "-o",  "tpxout", ffWRITE }
384     };
385 #define NFILE asize(fnm)
386
387     /* Command line options */
388     static int      nsteps_req_int = 0;
389     static real     start_t        = -1.0, extend_t = 0.0, until_t = 0.0;
390     static int      init_fep_state = 0;
391     static gmx_bool bContinuation  = TRUE, bZeroQ = FALSE, bVel = TRUE;
392     static t_pargs  pa[]           = {
393         { "-extend",        FALSE, etREAL, {&extend_t},
394           "Extend runtime by this amount (ps)" },
395         { "-until",         FALSE, etREAL, {&until_t},
396           "Extend runtime until this ending time (ps)" },
397         { "-nsteps",        FALSE, etINT,  {&nsteps_req_int},
398           "Change the number of steps" },
399         { "-time",          FALSE, etREAL, {&start_t},
400           "Continue from frame at this time (ps) instead of the last frame" },
401         { "-zeroq",         FALSE, etBOOL, {&bZeroQ},
402           "Set the charges of a group (from the index) to zero" },
403         { "-vel",           FALSE, etBOOL, {&bVel},
404           "Require velocities from trajectory" },
405         { "-cont",          FALSE, etBOOL, {&bContinuation},
406           "For exact continuation, the constraints should not be applied before the first step" },
407         { "-init_fep_state", FALSE, etINT, {&init_fep_state},
408           "fep state to initialize from" },
409     };
410     int             nerror = 0;
411
412     /* Parse the command line */
413     if (!parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa), pa,
414                            asize(desc), desc, 0, NULL, &oenv))
415     {
416         return 0;
417     }
418
419     /* Convert int to gmx_int64_t */
420     nsteps_req = nsteps_req_int;
421     bNsteps    = opt2parg_bSet("-nsteps", asize(pa), pa);
422     bExtend    = opt2parg_bSet("-extend", asize(pa), pa);
423     bUntil     = opt2parg_bSet("-until", asize(pa), pa);
424     bFepState  = opt2parg_bSet("-init_fep_state", asize(pa), pa);
425     bTime      = opt2parg_bSet("-time", asize(pa), pa);
426     bTraj      = (opt2bSet("-f", NFILE, fnm) || bTime);
427
428     top_fn = ftp2fn(efTPX, NFILE, fnm);
429     fprintf(stderr, "Reading toplogy and stuff from %s\n", top_fn);
430
431     snew(ir, 1);
432     read_tpx_state(top_fn, ir, &state, NULL, &mtop);
433     run_step = ir->init_step;
434     run_t    = ir->init_step*ir->delta_t + ir->init_t;
435
436     if (!EI_STATE_VELOCITY(ir->eI))
437     {
438         bVel = FALSE;
439     }
440
441     if (bTraj)
442     {
443         fprintf(stderr, "\n"
444                 "NOTE: Reading the state from trajectory is an obsolete feature of gmx convert-tpr.\n"
445                 "      Continuation should be done by loading a checkpoint file with mdrun -cpi\n"
446                 "      This guarantees that all state variables are transferred.\n"
447                 "      gmx convert-tpr is now only useful for increasing nsteps,\n"
448                 "      but even that can often be avoided by using mdrun -maxh\n"
449                 "\n");
450
451         if (ir->bContinuation != bContinuation)
452         {
453             fprintf(stderr, "Modifying ir->bContinuation to %s\n",
454                     bool_names[bContinuation]);
455         }
456         ir->bContinuation = bContinuation;
457
458
459         bNeedEner = (ir->epc == epcPARRINELLORAHMAN || ir->etc == etcNOSEHOOVER);
460         bReadEner = (bNeedEner && ftp2bSet(efEDR, NFILE, fnm));
461         bScanEner = (bReadEner && !bTime);
462
463         if (ir->epc != epcNO || EI_SD(ir->eI) || ir->eI == eiBD)
464         {
465             fprintf(stderr, "NOTE: The simulation uses pressure coupling and/or stochastic dynamics.\n"
466                     "gmx convert-tpr can not provide binary identical continuation.\n"
467                     "If you want that, supply a checkpoint file to mdrun\n\n");
468         }
469
470         if (EI_SD(ir->eI) || ir->eI == eiBD)
471         {
472             fprintf(stderr, "\nChanging ld-seed from %"GMX_PRId64 " ", ir->ld_seed);
473             ir->ld_seed = (gmx_int64_t)gmx_rng_make_seed();
474             fprintf(stderr, "to %"GMX_PRId64 "\n\n", ir->ld_seed);
475         }
476
477         frame_fn = ftp2fn(efTRN, NFILE, fnm);
478
479         if (fn2ftp(frame_fn) == efCPT)
480         {
481             int sim_part;
482
483             fprintf(stderr,
484                     "\nREADING STATE FROM CHECKPOINT %s...\n\n",
485                     frame_fn);
486
487             read_checkpoint_state(frame_fn, &sim_part,
488                                   &run_step, &run_t, &state);
489         }
490         else
491         {
492             fprintf(stderr,
493                     "\nREADING COORDS, VELS AND BOX FROM TRAJECTORY %s...\n\n",
494                     frame_fn);
495
496             fp = open_trn(frame_fn, "r");
497             if (bScanEner)
498             {
499                 fp_ener = open_enx(ftp2fn(efEDR, NFILE, fnm), "r");
500                 do_enxnms(fp_ener, &nre, &enm);
501                 snew(fr_ener, 1);
502                 fr_ener->t = -1e-12;
503             }
504
505             /* Now scan until the last set of x and v (step == 0)
506              * or the ones at step step.
507              */
508             bFrame = TRUE;
509             frame  = 0;
510             while (bFrame)
511             {
512                 bFrame = fread_trnheader(fp, &head, &bOK);
513                 if (bOK && frame == 0)
514                 {
515                     if (mtop.natoms != head.natoms)
516                     {
517                         gmx_fatal(FARGS, "Number of atoms in Topology (%d) "
518                                   "is not the same as in Trajectory (%d)\n",
519                                   mtop.natoms, head.natoms);
520                     }
521                     snew(newx, head.natoms);
522                     snew(newv, head.natoms);
523                 }
524                 bFrame = bFrame && bOK;
525                 if (bFrame)
526                 {
527                     bOK = fread_htrn(fp, &head, newbox, newx, newv, NULL);
528                 }
529                 bFrame = bFrame && bOK;
530                 bUse   = FALSE;
531                 if (bFrame &&
532                     (head.x_size) && (head.v_size || !bVel))
533                 {
534                     bUse = TRUE;
535                     if (bScanEner)
536                     {
537                         /* Read until the energy time is >= the trajectory time */
538                         while (fr_ener->t < head.t && do_enx(fp_ener, fr_ener))
539                         {
540                             ;
541                         }
542                         bUse = (fr_ener->t == head.t);
543                     }
544                     if (bUse)
545                     {
546                         tmpx                  = newx;
547                         newx                  = state.x;
548                         state.x               = tmpx;
549                         tmpv                  = newv;
550                         newv                  = state.v;
551                         state.v               = tmpv;
552                         run_t                 = head.t;
553                         run_step              = head.step;
554                         state.fep_state       = head.fep_state;
555                         state.lambda[efptFEP] = head.lambda;
556                         copy_mat(newbox, state.box);
557                     }
558                 }
559                 if (bFrame || !bOK)
560                 {
561                     sprintf(buf, "\r%s %s frame %s%s: step %s%s time %s",
562                             "%s", "%s", "%6", GMX_PRId64, "%6", GMX_PRId64, " %8.3f");
563                     fprintf(stderr, buf,
564                             bUse ? "Read   " : "Skipped", ftp2ext(fn2ftp(frame_fn)),
565                             frame, head.step, head.t);
566                     frame++;
567                     if (bTime && (head.t >= start_t))
568                     {
569                         bFrame = FALSE;
570                     }
571                 }
572             }
573             if (bScanEner)
574             {
575                 close_enx(fp_ener);
576                 free_enxframe(fr_ener);
577                 free_enxnms(nre, enm);
578             }
579             close_trn(fp);
580             fprintf(stderr, "\n");
581
582             if (!bOK)
583             {
584                 fprintf(stderr, "%s frame %s (step %s, time %g) is incomplete\n",
585                         ftp2ext(fn2ftp(frame_fn)), gmx_step_str(frame-1, buf2),
586                         gmx_step_str(head.step, buf), head.t);
587             }
588             fprintf(stderr, "\nUsing frame of step %s time %g\n",
589                     gmx_step_str(run_step, buf), run_t);
590
591             if (bNeedEner)
592             {
593                 if (bReadEner)
594                 {
595                     get_enx_state(ftp2fn(efEDR, NFILE, fnm), run_t, &mtop.groups, ir, &state);
596                 }
597                 else
598                 {
599                     fprintf(stderr, "\nWARNING: The simulation uses %s temperature and/or %s pressure coupling,\n"
600                             "         the continuation will only be exact when an energy file is supplied\n\n",
601                             ETCOUPLTYPE(etcNOSEHOOVER),
602                             EPCOUPLTYPE(epcPARRINELLORAHMAN));
603                 }
604             }
605             if (bFepState)
606             {
607                 ir->fepvals->init_fep_state = init_fep_state;
608             }
609         }
610     }
611
612     if (bNsteps)
613     {
614         fprintf(stderr, "Setting nsteps to %s\n", gmx_step_str(nsteps_req, buf));
615         ir->nsteps = nsteps_req;
616     }
617     else
618     {
619         /* Determine total number of steps remaining */
620         if (bExtend)
621         {
622             ir->nsteps = ir->nsteps - (run_step - ir->init_step) + (gmx_int64_t)(extend_t/ir->delta_t + 0.5);
623             printf("Extending remaining runtime of by %g ps (now %s steps)\n",
624                    extend_t, gmx_step_str(ir->nsteps, buf));
625         }
626         else if (bUntil)
627         {
628             printf("nsteps = %s, run_step = %s, current_t = %g, until = %g\n",
629                    gmx_step_str(ir->nsteps, buf),
630                    gmx_step_str(run_step, buf2),
631                    run_t, until_t);
632             ir->nsteps = (gmx_int64_t)((until_t - run_t)/ir->delta_t + 0.5);
633             printf("Extending remaining runtime until %g ps (now %s steps)\n",
634                    until_t, gmx_step_str(ir->nsteps, buf));
635         }
636         else
637         {
638             ir->nsteps -= run_step - ir->init_step;
639             /* Print message */
640             printf("%s steps (%g ps) remaining from first run.\n",
641                    gmx_step_str(ir->nsteps, buf), ir->nsteps*ir->delta_t);
642         }
643     }
644
645     if (bNsteps || bZeroQ || (ir->nsteps > 0))
646     {
647         ir->init_step = run_step;
648
649         if (ftp2bSet(efNDX, NFILE, fnm) ||
650             !(bNsteps || bExtend || bUntil || bTraj))
651         {
652             atoms = gmx_mtop_global_atoms(&mtop);
653             get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1,
654                       &gnx, &index, &grpname);
655             if (!bZeroQ)
656             {
657                 bSel = (gnx != state.natoms);
658                 for (i = 0; ((i < gnx) && (!bSel)); i++)
659                 {
660                     bSel = (i != index[i]);
661                 }
662             }
663             else
664             {
665                 bSel = FALSE;
666             }
667             if (bSel)
668             {
669                 fprintf(stderr, "Will write subset %s of original tpx containing %d "
670                         "atoms\n", grpname, gnx);
671                 reduce_topology_x(gnx, index, &mtop, state.x, state.v);
672                 state.natoms = gnx;
673             }
674             else if (bZeroQ)
675             {
676                 zeroq(index, &mtop);
677                 fprintf(stderr, "Zero-ing charges for group %s\n", grpname);
678             }
679             else
680             {
681                 fprintf(stderr, "Will write full tpx file (no selection)\n");
682             }
683         }
684
685         state_t = ir->init_t + ir->init_step*ir->delta_t;
686         sprintf(buf,   "Writing statusfile with starting step %s%s and length %s%s steps...\n", "%10", GMX_PRId64, "%10", GMX_PRId64);
687         fprintf(stderr, buf, ir->init_step, ir->nsteps);
688         fprintf(stderr, "                                 time %10.3f and length %10.3f ps\n",
689                 state_t, ir->nsteps*ir->delta_t);
690         write_tpx_state(opt2fn("-o", NFILE, fnm), ir, &state, &mtop);
691     }
692     else
693     {
694         printf("You've simulated long enough. Not writing tpr file\n");
695     }
696
697     return 0;
698 }