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-2013, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020,2021, 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.
39 * \brief Implements gmx dump utility.
41 * \ingroup module_tools
54 #include "gromacs/commandline/cmdlineoptionsmodule.h"
55 #include "gromacs/fileio/checkpoint.h"
56 #include "gromacs/fileio/enxio.h"
57 #include "gromacs/fileio/filetypes.h"
58 #include "gromacs/fileio/gmxfio.h"
59 #include "gromacs/fileio/mtxio.h"
60 #include "gromacs/fileio/tngio.h"
61 #include "gromacs/fileio/tpxio.h"
62 #include "gromacs/fileio/trrio.h"
63 #include "gromacs/fileio/xtcio.h"
64 #include "gromacs/gmxpreprocess/gmxcpp.h"
65 #include "gromacs/math/vecdump.h"
66 #include "gromacs/mdrun/mdmodules.h"
67 #include "gromacs/mdtypes/forcerec.h"
68 #include "gromacs/mdtypes/inputrec.h"
69 #include "gromacs/mdtypes/md_enums.h"
70 #include "gromacs/mdtypes/state.h"
71 #include "gromacs/options/basicoptions.h"
72 #include "gromacs/options/filenameoption.h"
73 #include "gromacs/options/ioptionscontainer.h"
74 #include "gromacs/topology/mtop_util.h"
75 #include "gromacs/topology/topology.h"
76 #include "gromacs/trajectory/energyframe.h"
77 #include "gromacs/trajectory/trajectoryframe.h"
78 #include "gromacs/utility/arraysize.h"
79 #include "gromacs/utility/basedefinitions.h"
80 #include "gromacs/utility/fatalerror.h"
81 #include "gromacs/utility/futil.h"
82 #include "gromacs/utility/smalloc.h"
83 #include "gromacs/utility/txtdump.h"
92 void list_tpr(const char* fn,
93 gmx_bool bShowNumbers,
94 gmx_bool bShowParameters,
97 gmx_bool bOriginalInputrec)
105 TpxFileHeader tpx = readTpxHeader(fn, true);
108 read_tpx_state(fn, tpx.bIr ? &ir : nullptr, &state, tpx.bTop ? &mtop : nullptr);
109 if (tpx.bIr && !bOriginalInputrec)
111 MDModules().adjustInputrecBasedOnModules(&ir);
114 if (mdpfn && tpx.bIr)
116 gp = gmx_fio_fopen(mdpfn, "w");
117 pr_inputrec(gp, 0, nullptr, &ir, TRUE);
125 top = gmx_mtop_t_to_t_topology(&mtop, false);
128 if (available(stdout, &tpx, 0, fn))
131 pr_title(stdout, indent, fn);
132 pr_inputrec(stdout, 0, "inputrec", tpx.bIr ? &ir : nullptr, FALSE);
134 pr_tpxheader(stdout, indent, "header", &(tpx));
138 pr_mtop(stdout, indent, "topology", &(mtop), bShowNumbers, bShowParameters);
142 pr_top(stdout, indent, "topology", &(top), bShowNumbers, bShowParameters);
145 pr_rvecs(stdout, indent, "box", tpx.bBox ? state.box : nullptr, DIM);
146 pr_rvecs(stdout, indent, "box_rel", tpx.bBox ? state.box_rel : nullptr, DIM);
147 pr_rvecs(stdout, indent, "boxv", tpx.bBox ? state.boxv : nullptr, DIM);
148 pr_rvecs(stdout, indent, "pres_prev", tpx.bBox ? state.pres_prev : nullptr, DIM);
149 pr_rvecs(stdout, indent, "svir_prev", tpx.bBox ? state.svir_prev : nullptr, DIM);
150 pr_rvecs(stdout, indent, "fvir_prev", tpx.bBox ? state.fvir_prev : nullptr, DIM);
151 /* leave nosehoover_xi in for now to match the tpr version */
152 pr_doubles(stdout, indent, "nosehoover_xi", state.nosehoover_xi.data(), state.ngtc);
153 /*pr_doubles(stdout,indent,"nosehoover_vxi",state.nosehoover_vxi,state.ngtc);*/
154 /*pr_doubles(stdout,indent,"therm_integral",state.therm_integral,state.ngtc);*/
155 pr_rvecs(stdout, indent, "x", tpx.bX ? state.x.rvec_array() : nullptr, state.natoms);
156 pr_rvecs(stdout, indent, "v", tpx.bV ? state.v.rvec_array() : nullptr, state.natoms);
159 const SimulationGroups& groups = mtop.groups;
161 gmx::EnumerationArray<SimulationAtomGroupType, std::vector<int>> gcount;
162 for (auto group : keysOf(gcount))
164 gcount[group].resize(groups.groups[group].size());
167 for (int i = 0; (i < mtop.natoms); i++)
169 for (auto group : keysOf(gcount))
171 gcount[group][getGroupType(groups, group, i)]++;
174 printf("Group statistics\n");
175 for (auto group : keysOf(gcount))
178 printf("%-12s: ", shortName(group));
179 for (const auto& entry : gcount[group])
181 printf(" %5d", entry);
184 printf(" (total %d atoms)\n", atot);
189 //! Dump a topology file
190 void list_top(const char* fn)
193 // Legacy string length macro
196 char* cppopts[] = { nullptr };
198 status = cpp_open_file(fn, &handle, cppopts);
201 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
205 status = cpp_read_line(&handle, STRLEN, buf);
206 done = static_cast<int>(status == eCPP_EOF);
209 if (status != eCPP_OK)
211 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
219 status = cpp_close_file(&handle);
220 if (status != eCPP_OK)
222 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
227 void list_trr(const char* fn)
234 gmx_trr_header_t trrheader;
237 fpread = gmx_trr_open(fn, "r");
240 while (gmx_trr_read_frame_header(fpread, &trrheader, &bOK))
242 snew(x, trrheader.natoms);
243 snew(v, trrheader.natoms);
244 snew(f, trrheader.natoms);
245 if (gmx_trr_read_frame_data(fpread,
247 trrheader.box_size ? box : nullptr,
248 trrheader.x_size ? x : nullptr,
249 trrheader.v_size ? v : nullptr,
250 trrheader.f_size ? f : nullptr))
252 sprintf(buf, "%s frame %d", fn, nframe);
254 indent = pr_title(stdout, indent, buf);
255 pr_indent(stdout, indent);
257 "natoms=%10d step=%10" PRId64 " time=%12.7e lambda=%10g\n",
262 if (trrheader.box_size)
264 pr_rvecs(stdout, indent, "box", box, DIM);
266 if (trrheader.x_size)
268 pr_rvecs(stdout, indent, "x", x, trrheader.natoms);
270 if (trrheader.v_size)
272 pr_rvecs(stdout, indent, "v", v, trrheader.natoms);
274 if (trrheader.f_size)
276 pr_rvecs(stdout, indent, "f", f, trrheader.natoms);
281 fprintf(stderr, "\nWARNING: Incomplete frame: nr %d, t=%g\n", nframe, trrheader.t);
291 fprintf(stderr, "\nWARNING: Incomplete frame header: nr %d, t=%g\n", nframe, trrheader.t);
293 gmx_trr_close(fpread);
297 void list_xtc(const char* fn)
309 xd = open_xtc(fn, "r");
310 read_first_xtc(xd, &natoms, &step, &time, box, &x, &prec, &bOK);
315 sprintf(buf, "%s frame %d", fn, nframe);
317 indent = pr_title(stdout, indent, buf);
318 pr_indent(stdout, indent);
319 fprintf(stdout, "natoms=%10d step=%10" PRId64 " time=%12.7e prec=%10g\n", natoms, step, time, prec);
320 pr_rvecs(stdout, indent, "box", box, DIM);
321 pr_rvecs(stdout, indent, "x", x, natoms);
323 } while (read_next_xtc(xd, natoms, &step, &time, box, x, &prec, &bOK) != 0);
326 fprintf(stderr, "\nWARNING: Incomplete frame at time %g\n", time);
334 /*! \brief Callback used by list_tng_for_gmx_dump. */
335 void list_tng_inner(const char* fn,
336 gmx_bool bFirstFrame,
340 int64_t n_values_per_frame,
351 sprintf(buf, "%s frame %" PRId64, fn, nframe);
353 indent = pr_title(stdout, indent, buf);
354 pr_indent(stdout, indent);
355 fprintf(stdout, "natoms=%10" PRId64 " step=%10" PRId64 " time=%12.7e", n_atoms, step, frame_time);
358 fprintf(stdout, " prec=%10g", prec);
360 fprintf(stdout, "\n");
362 pr_reals_of_dim(stdout, indent, block_name, values, n_atoms, n_values_per_frame);
368 void list_tng(const char* fn)
371 gmx_tng_trajectory_t tng;
373 int64_t i, *block_ids = nullptr, step, ndatablocks;
375 real* values = nullptr;
377 gmx_tng_open(fn, 'r', &tng);
378 gmx_print_tng_molecule_system(tng, stdout);
380 bOK = gmx_get_tng_data_block_types_of_next_frame(tng, -1, 0, nullptr, &step, &ndatablocks, &block_ids);
383 for (i = 0; i < ndatablocks; i++)
387 int64_t n_values_per_frame, n_atoms;
388 char block_name[STRLEN];
390 gmx_get_tng_data_next_frame_of_block_type(tng,
403 /* Can't write any output because we don't know what
405 fprintf(stderr, "\nWARNING: Incomplete frame at time %g, will not write output\n", frame_time);
410 fn, (0 == i), values, step, frame_time, n_values_per_frame, n_atoms, prec, nframe, block_name);
414 } while (gmx_get_tng_data_block_types_of_next_frame(
415 tng, step, 0, nullptr, &step, &ndatablocks, &block_ids));
424 GMX_UNUSED_VALUE(fn);
428 //! Dump a trajectory file
429 void list_trx(const char* fn)
433 case efXTC: list_xtc(fn); break;
434 case efTRR: list_trr(fn); break;
435 case efTNG: list_tng(fn); break;
437 fprintf(stderr, "File %s is of an unsupported type. Try using the command\n 'less %s'\n", fn, fn);
441 //! Dump an energy file
442 void list_ene(const char* fn)
446 gmx_enxnm_t* enm = nullptr;
451 printf("gmx dump: %s\n", fn);
452 in = open_enx(fn, "r");
453 do_enxnms(in, &nre, &enm);
456 printf("energy components:\n");
457 for (i = 0; (i < nre); i++)
459 printf("%5d %-24s (%s)\n", i, enm[i].name, enm[i].unit);
465 bCont = do_enx(in, fr);
469 printf("\n%24s %12.5e %12s %12s\n", "time:", fr->t, "step:", gmx_step_str(fr->step, buf));
470 printf("%24s %12s %12s %12s\n", "", "", "nsteps:", gmx_step_str(fr->nsteps, buf));
471 printf("%24s %12.5e %12s %12s\n", "delta_t:", fr->dt, "sum steps:", gmx_step_str(fr->nsum, buf));
474 printf("%24s %12s %12s %12s\n",
481 for (i = 0; (i < nre); i++)
483 printf("%24s %12.5e %12.5e %12.5e\n",
492 for (i = 0; (i < nre); i++)
494 printf("%24s %12.5e\n", enm[i].name, fr->ener[i].e);
498 for (b = 0; b < fr->nblock; b++)
500 const char* typestr = "";
502 t_enxblock* eb = &(fr->block[b]);
503 printf("Block data %2d (%3d subblocks, id=%d)\n", b, eb->nsub, eb->id);
507 typestr = enx_block_id_name[eb->id];
509 printf(" id='%s'\n", typestr);
510 for (i = 0; i < eb->nsub; i++)
512 t_enxsubblock* sb = &(eb->sub[i]);
513 printf(" Sub block %3d (%5d elems, type=%s) values:\n",
516 enumValueToString(sb->type));
520 case XdrDataType::Float:
521 for (j = 0; j < sb->nr; j++)
523 printf("%14d %8.4f\n", j, sb->fval[j]);
526 case XdrDataType::Double:
527 for (j = 0; j < sb->nr; j++)
529 printf("%14d %10.6f\n", j, sb->dval[j]);
532 case XdrDataType::Int:
533 for (j = 0; j < sb->nr; j++)
535 printf("%14d %10d\n", j, sb->ival[j]);
538 case XdrDataType::Int64:
539 for (j = 0; j < sb->nr; j++)
541 printf("%14d %s\n", j, gmx_step_str(sb->lval[j], buf));
544 case XdrDataType::Char:
545 for (j = 0; j < sb->nr; j++)
547 printf("%14d %1c\n", j, sb->cval[j]);
550 case XdrDataType::String:
551 for (j = 0; j < sb->nr; j++)
553 printf("%14d %80s\n", j, sb->sval[j]);
556 default: gmx_incons("Unknown subblock type");
570 //! Dump a (Hessian) matrix file
571 void list_mtx(const char* fn)
573 int nrow, ncol, i, j, k;
574 real * full = nullptr, value;
575 gmx_sparsematrix_t* sparse = nullptr;
577 gmx_mtxio_read(fn, &nrow, &ncol, &full, &sparse);
581 snew(full, nrow * ncol);
582 for (i = 0; i < nrow * ncol; i++)
587 for (i = 0; i < sparse->nrow; i++)
589 for (j = 0; j < sparse->ndata[i]; j++)
591 k = sparse->data[i][j].col;
592 value = sparse->data[i][j].value;
593 full[i * ncol + k] = value;
594 full[k * ncol + i] = value;
597 gmx_sparsematrix_destroy(sparse);
600 printf("%d %d\n", nrow, ncol);
601 for (i = 0; i < nrow; i++)
603 for (j = 0; j < ncol; j++)
605 printf(" %g", full[i * ncol + j]);
613 class Dump : public ICommandLineOptionsModule
618 // From ICommandLineOptionsModule
619 void init(CommandLineModuleSettings* /*settings*/) override {}
621 void initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings) override;
623 void optionsFinished() override;
628 //! Commandline options
630 bool bShowNumbers_ = true;
631 bool bShowParams_ = false;
632 bool bSysTop_ = false;
633 bool bOriginalInputrec_ = false;
635 //! Commandline file options
637 std::string inputTprFilename_;
638 std::string inputTrajectoryFilename_;
639 std::string inputEnergyFilename_;
640 std::string inputCheckpointFilename_;
641 std::string inputTopologyFilename_;
642 std::string inputMatrixFilename_;
643 std::string outputMdpFilename_;
647 void Dump::initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings)
649 const char* desc[] = { "[THISMODULE] reads a run input file ([REF].tpr[ref]),",
650 "a trajectory ([REF].trr[ref]/[REF].xtc[ref]/[TT]tng[tt]), an energy",
651 "file ([REF].edr[ref]), a checkpoint file ([REF].cpt[ref])",
652 "or topology file ([REF].top[ref])",
653 "and prints that to standard output in a readable format.",
654 "This program is essential for checking your run input file in case of",
656 settings->setHelpText(desc);
658 const char* bugs[] = {
659 "The [REF].mdp[ref] file produced by [TT]-om[tt] can not be read by grompp."
661 settings->setBugText(bugs);
662 // TODO If this ancient note acknowledging a bug is still true,
663 // fix it or block that run path:
664 // Position restraint output from -sys -s is broken
666 options->addOption(FileNameOption("s")
667 .filetype(OptionFileType::RunInput)
669 .store(&inputTprFilename_)
670 .description("Run input file to dump"));
671 options->addOption(FileNameOption("f")
672 .filetype(OptionFileType::Trajectory)
674 .store(&inputTrajectoryFilename_)
675 .description("Trajectory file to dump"));
676 options->addOption(FileNameOption("e")
677 .filetype(OptionFileType::Energy)
679 .store(&inputEnergyFilename_)
680 .description("Energy file to dump"));
682 FileNameOption("cp").legacyType(efCPT).inputFile().store(&inputCheckpointFilename_).description("Checkpoint file to dump"));
684 FileNameOption("p").legacyType(efTOP).inputFile().store(&inputTopologyFilename_).description("Topology file to dump"));
686 FileNameOption("mtx").legacyType(efMTX).inputFile().store(&inputMatrixFilename_).description("Hessian matrix to dump"));
687 options->addOption(FileNameOption("om")
690 .store(&outputMdpFilename_)
691 .description("grompp input file from run input file"));
694 BooleanOption("nr").store(&bShowNumbers_).defaultValue(true).description("Show index numbers in output (leaving them out makes comparison easier, but creates a useless topology)"));
696 BooleanOption("param").store(&bShowParams_).defaultValue(false).description("Show parameters for each bonded interaction (for comparing dumps, it is useful to combine this with -nonr)"));
698 BooleanOption("sys").store(&bShowParams_).defaultValue(false).description("List the atoms and bonded interactions for the whole system instead of for each molecule type"));
700 BooleanOption("orgir").store(&bShowParams_).defaultValue(false).description("Show input parameters from tpr as they were written by the version that produced the file, instead of how the current version reads them"));
703 void Dump::optionsFinished()
705 // TODO Currently gmx dump ignores user input that seeks to dump
706 // multiple files. Here, we could enforce that the user only asks
712 if (!inputTprFilename_.empty())
714 list_tpr(inputTprFilename_.c_str(),
717 outputMdpFilename_.empty() ? nullptr : outputMdpFilename_.c_str(),
721 else if (!inputTrajectoryFilename_.empty())
723 list_trx(inputTrajectoryFilename_.c_str());
725 else if (!inputEnergyFilename_.empty())
727 list_ene(inputEnergyFilename_.c_str());
729 else if (!inputCheckpointFilename_.empty())
731 list_checkpoint(inputCheckpointFilename_.c_str(), stdout);
733 else if (!inputTopologyFilename_.empty())
735 list_top(inputTopologyFilename_.c_str());
737 else if (!inputMatrixFilename_.empty())
739 list_mtx(inputMatrixFilename_.c_str());
747 const char DumpInfo::name[] = "dump";
748 const char DumpInfo::shortDescription[] = "Make binary files human readable";
749 ICommandLineOptionsModulePointer DumpInfo::create()
751 return std::make_unique<Dump>();