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,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
40 #include "convert_tpr.h"
44 #include "gromacs/commandline/cmdlineoptionsmodule.h"
45 #include "gromacs/fileio/checkpoint.h"
46 #include "gromacs/fileio/enxio.h"
47 #include "gromacs/fileio/tpxio.h"
48 #include "gromacs/fileio/trrio.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/mdtypes/inputrec.h"
51 #include "gromacs/mdtypes/md_enums.h"
52 #include "gromacs/mdtypes/state.h"
53 #include "gromacs/options/basicoptions.h"
54 #include "gromacs/options/filenameoption.h"
55 #include "gromacs/options/ioptionscontainer.h"
56 #include "gromacs/random/seed.h"
57 #include "gromacs/topology/ifunc.h"
58 #include "gromacs/topology/index.h"
59 #include "gromacs/topology/mtop_util.h"
60 #include "gromacs/topology/topology.h"
61 #include "gromacs/utility/arraysize.h"
62 #include "gromacs/utility/cstringutil.h"
63 #include "gromacs/utility/fatalerror.h"
64 #include "gromacs/utility/futil.h"
65 #include "gromacs/utility/real.h"
66 #include "gromacs/utility/smalloc.h"
67 #include "gromacs/utility/stringutil.h"
69 static void rangeCheck(int numberInIndexFile, int maxAtomNumber)
71 if ((numberInIndexFile) >= (maxAtomNumber))
74 "Your index file contains atomnumbers (e.g. %d)\nthat are larger than the number "
75 "of atoms in the tpr file (%d)",
76 (numberInIndexFile), (maxAtomNumber));
80 static std::vector<bool> bKeepIt(int gnx, int natoms, int index[])
82 std::vector<bool> b(natoms);
84 for (int i = 0; (i < gnx); i++)
86 rangeCheck(index[i], natoms);
93 static std::vector<int> invind(int gnx, int natoms, int index[])
95 std::vector<int> inv(natoms);
97 for (int i = 0; (i < gnx); i++)
99 rangeCheck(index[i], natoms);
106 static gmx::ListOfLists<int> reduce_listoflists(gmx::ArrayRef<const int> invindex,
107 const std::vector<bool>& bKeep,
108 const gmx::ListOfLists<int>& src,
111 gmx::ListOfLists<int> lists;
113 std::vector<int> exclusionsForAtom;
114 for (gmx::index i = 0; i < src.ssize(); i++)
118 exclusionsForAtom.clear();
119 for (const int j : src[i])
123 exclusionsForAtom.push_back(invindex[j]);
126 lists.pushBack(exclusionsForAtom);
130 fprintf(stderr, "Reduced block %8s from %6zu to %6zu index-, %6d to %6d a-entries\n", name,
131 src.size(), lists.size(), src.numElements(), lists.numElements());
136 static void reduce_rvec(int gnx, const int index[], rvec vv[])
142 for (i = 0; (i < gnx); i++)
144 copy_rvec(vv[index[i]], ptr[i]);
146 for (i = 0; (i < gnx); i++)
148 copy_rvec(ptr[i], vv[i]);
153 static void reduce_atom(int gnx, const int index[], t_atom atom[], char*** atomname, int* nres, t_resinfo* resinfo)
162 snew(rinfo, atom[index[gnx - 1]].resind + 1);
163 for (i = 0; (i < gnx); i++)
165 ptr[i] = atom[index[i]];
166 aname[i] = atomname[index[i]];
169 for (i = 0; (i < gnx); i++)
172 atomname[i] = aname[i];
173 if ((i == 0) || (atom[i].resind != atom[i - 1].resind))
176 rinfo[nr] = resinfo[atom[i].resind];
181 for (i = 0; (i < nr); i++)
183 resinfo[i] = rinfo[i];
192 static void reduce_ilist(gmx::ArrayRef<const int> invindex,
193 const std::vector<bool>& bKeep,
200 std::vector<int> newAtoms(nratoms);
201 InteractionList ilReduced;
202 for (int i = 0; i < il->size(); i += nratoms + 1)
205 for (int j = 0; j < nratoms; j++)
207 bB = bB && bKeep[il->iatoms[i + 1 + j]];
211 for (int j = 0; j < nratoms; j++)
213 newAtoms[j] = invindex[il->iatoms[i + 1 + j]];
215 ilReduced.push_back(il->iatoms[i], nratoms, newAtoms.data());
218 fprintf(stderr, "Reduced ilist %8s from %6d to %6d entries\n", name,
219 il->size() / (nratoms + 1), ilReduced.size() / (nratoms + 1));
221 *il = std::move(ilReduced);
225 static void reduce_topology_x(int gnx, int index[], gmx_mtop_t* mtop, rvec x[], rvec v[])
227 gmx_localtop_t top(mtop->ffparams);
228 gmx_mtop_generate_local_top(*mtop, &top, false);
229 t_atoms atoms = gmx_mtop_global_atoms(mtop);
231 const std::vector<bool> bKeep = bKeepIt(gnx, atoms.nr, index);
232 const std::vector<int> invindex = invind(gnx, atoms.nr, index);
234 reduce_rvec(gnx, index, x);
235 reduce_rvec(gnx, index, v);
236 reduce_atom(gnx, index, atoms.atom, atoms.atomname, &(atoms.nres), atoms.resinfo);
238 for (int i = 0; (i < F_NRE); i++)
240 reduce_ilist(invindex, bKeep, &(top.idef.il[i]), interaction_function[i].nratoms,
241 interaction_function[i].name);
246 mtop->moltype.resize(1);
247 mtop->moltype[0].name = mtop->name;
248 mtop->moltype[0].atoms = atoms;
249 mtop->moltype[0].excls = reduce_listoflists(invindex, bKeep, top.excls, "excls");
250 for (int i = 0; i < F_NRE; i++)
252 mtop->moltype[0].ilist[i] = std::move(top.idef.il[i]);
255 mtop->molblock.resize(1);
256 mtop->molblock[0].type = 0;
257 mtop->molblock[0].nmol = 1;
259 mtop->natoms = atoms.nr;
262 static void zeroq(const int index[], gmx_mtop_t* mtop)
264 for (gmx_moltype_t& moltype : mtop->moltype)
266 for (int i = 0; i < moltype.atoms.nr; i++)
268 moltype.atoms.atom[index[i]].q = 0;
269 moltype.atoms.atom[index[i]].qB = 0;
280 class ConvertTpr : public ICommandLineOptionsModule
285 // From ICommandLineOptionsModule
286 void init(CommandLineModuleSettings* /*settings*/) override {}
287 void initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings) override;
288 void optionsFinished() override;
292 //! Name of input tpr file.
293 std::string inputTprFileName_;
294 //! Name of input index file.
295 std::string inputIndexFileName_;
296 //! Name of output tpr file.
297 std::string outputTprFileName_;
298 //! If we have read in an index file.
299 bool haveReadIndexFile_ = false;
300 //! Time to extend simulation by.
301 real extendTime_ = 0;
302 //! If the option to extend simulation time is set.
303 bool extendTimeIsSet_ = false;
304 //! Final run time value.
305 real runToMaxTime_ = 0;
306 //! If the option to run simulation until specified time is set.
307 bool runToMaxTimeIsSet_ = false;
308 //! Maximum number of steps to run.
309 int64_t maxSteps_ = 0;
310 //! If the option to use maximumstep number is set.
311 bool maxStepsIsSet_ = false;
312 //! If the option to zero charge is set.
313 bool zeroQIsSet_ = false;
316 void ConvertTpr::initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings)
318 std::vector<const char*> desc = {
319 "[THISMODULE] can edit run input files in three ways.[PAR]",
320 "[BB]1.[bb] by modifying the number of steps in a run input file",
321 "with options [TT]-extend[tt], [TT]-until[tt] or [TT]-nsteps[tt]",
322 "(nsteps=-1 means unlimited number of steps)[PAR]",
323 "[BB]2.[bb] by creating a [REF].tpx[ref] file for a subset of your original",
324 "tpx file, which is useful when you want to remove the solvent from",
325 "your [REF].tpx[ref] file, or when you want to make e.g. a pure C[GRK]alpha[grk] ",
326 "[REF].tpx[ref] file.",
327 "Note that you may need to use [TT]-nsteps -1[tt] (or similar) to get",
329 "[BB]WARNING: this [REF].tpx[ref] file is not fully functional[bb].[PAR]",
330 "[BB]3.[bb] by setting the charges of a specified group",
331 "to zero. This is useful when doing free energy estimates",
332 "using the LIE (Linear Interaction Energy) method."
335 settings->setHelpText(desc);
337 options->addOption(FileNameOption("s")
338 .filetype(eftTopology)
341 .store(&inputTprFileName_)
342 .defaultBasename("topol")
343 .description("Run input file to modify"));
344 options->addOption(FileNameOption("n")
347 .store(&inputIndexFileName_)
348 .storeIsSet(&haveReadIndexFile_)
349 .defaultBasename("index")
350 .description("File containing additional index groups"));
351 options->addOption(FileNameOption("o")
352 .filetype(eftTopology)
354 .store(&outputTprFileName_)
355 .defaultBasename("tprout")
356 .description("Generated modified run input file"));
357 options->addOption(RealOption("extend")
359 .storeIsSet(&extendTimeIsSet_)
361 .description("Extend runtime by this amount (ps)"));
362 options->addOption(RealOption("until")
363 .store(&runToMaxTime_)
364 .storeIsSet(&runToMaxTimeIsSet_)
366 .description("Extend runtime until this ending time (ps)"));
368 Int64Option("nsteps").store(&maxSteps_).storeIsSet(&maxStepsIsSet_).description("Change the number of steps"));
370 BooleanOption("zeroq").store(&zeroQIsSet_).description("Set the charges of a group (from the index) to zero"));
373 void ConvertTpr::optionsFinished() {}
375 int ConvertTpr::run()
380 char buf[200], buf2[200];
382 fprintf(stderr, "Reading toplogy and stuff from %s\n", inputTprFileName_.c_str());
384 t_inputrec irInstance;
385 t_inputrec* ir = &irInstance;
386 read_tpx_state(inputTprFileName_.c_str(), ir, &state, &mtop);
387 int64_t currentMaxStep = ir->init_step;
388 double currentRunTime = ir->init_step * ir->delta_t + ir->init_t;
389 real currentMaxRunTime = 0.0;
393 fprintf(stderr, "Setting nsteps to %s\n", gmx_step_str(maxSteps_, buf));
394 ir->nsteps = maxSteps_;
398 /* Determine total number of steps remaining */
399 if (extendTimeIsSet_)
401 ir->nsteps = ir->nsteps - (currentMaxStep - ir->init_step)
402 + gmx::roundToInt64(extendTime_ / ir->delta_t);
403 printf("Extending remaining runtime of by %g ps (now %s steps)\n", extendTime_,
404 gmx_step_str(ir->nsteps, buf));
406 else if (runToMaxTimeIsSet_)
408 printf("nsteps = %s, run_step = %s, current_t = %g, until = %g\n",
409 gmx_step_str(ir->nsteps, buf), gmx_step_str(currentMaxStep, buf2),
410 currentRunTime, runToMaxTime_);
411 ir->nsteps = gmx::roundToInt64((currentMaxRunTime - currentRunTime) / ir->delta_t);
412 printf("Extending remaining runtime until %g ps (now %s steps)\n", currentMaxRunTime,
413 gmx_step_str(ir->nsteps, buf));
417 ir->nsteps -= currentMaxStep - ir->init_step;
419 printf("%s steps (%g ps) remaining from first run.\n", gmx_step_str(ir->nsteps, buf),
420 ir->nsteps * ir->delta_t);
424 if (maxStepsIsSet_ || zeroQIsSet_ || (ir->nsteps > 0))
426 ir->init_step = currentMaxStep;
428 if (haveReadIndexFile_ || !(maxStepsIsSet_ || extendTimeIsSet_ || runToMaxTimeIsSet_))
430 atoms = gmx_mtop_global_atoms(&mtop);
432 int* index = nullptr;
433 char* grpname = nullptr;
434 get_index(&atoms, inputIndexFileName_.c_str(), 1, &gnx, &index, &grpname);
438 bSel = (gnx != state.natoms);
439 for (int i = 0; ((i < gnx) && (!bSel)); i++)
441 bSel = (i != index[i]);
451 "Will write subset %s of original tpx containing %d "
454 reduce_topology_x(gnx, index, &mtop, state.x.rvec_array(), state.v.rvec_array());
457 else if (zeroQIsSet_)
460 fprintf(stderr, "Zero-ing charges for group %s\n", grpname);
464 fprintf(stderr, "Will write full tpx file (no selection)\n");
468 double stateTime = ir->init_t + ir->init_step * ir->delta_t;
469 sprintf(buf, "Writing statusfile with starting step %s%s and length %s%s steps...\n", "%10",
470 PRId64, "%10", PRId64);
471 fprintf(stderr, buf, ir->init_step, ir->nsteps);
472 fprintf(stderr, " time %10.3f and length %10.3f ps\n",
473 stateTime, ir->nsteps * ir->delta_t);
474 write_tpx_state(outputTprFileName_.c_str(), ir, &state, &mtop);
478 printf("You've simulated long enough. Not writing tpr file\n");
486 const char ConvertTprInfo::name[] = "convert-tpr";
487 const char ConvertTprInfo::shortDescription[] = "Make a modifed run-input file";
488 ICommandLineOptionsModulePointer ConvertTprInfo::create()
490 return ICommandLineOptionsModulePointer(std::make_unique<ConvertTpr>());