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,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.
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.
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.
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.
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.
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.
38 * \brief Implements gmx dump utility.
40 * \ingroup module_tools
53 #include "gromacs/commandline/cmdlineoptionsmodule.h"
54 #include "gromacs/fileio/checkpoint.h"
55 #include "gromacs/fileio/enxio.h"
56 #include "gromacs/fileio/filetypes.h"
57 #include "gromacs/fileio/gmxfio.h"
58 #include "gromacs/fileio/mtxio.h"
59 #include "gromacs/fileio/tngio.h"
60 #include "gromacs/fileio/tpxio.h"
61 #include "gromacs/fileio/trrio.h"
62 #include "gromacs/fileio/xtcio.h"
63 #include "gromacs/gmxpreprocess/gmxcpp.h"
64 #include "gromacs/math/vecdump.h"
65 #include "gromacs/mdrun/mdmodules.h"
66 #include "gromacs/mdtypes/forcerec.h"
67 #include "gromacs/mdtypes/inputrec.h"
68 #include "gromacs/mdtypes/md_enums.h"
69 #include "gromacs/mdtypes/state.h"
70 #include "gromacs/options/basicoptions.h"
71 #include "gromacs/options/filenameoption.h"
72 #include "gromacs/options/ioptionscontainer.h"
73 #include "gromacs/topology/mtop_util.h"
74 #include "gromacs/topology/topology.h"
75 #include "gromacs/trajectory/energyframe.h"
76 #include "gromacs/trajectory/trajectoryframe.h"
77 #include "gromacs/utility/arraysize.h"
78 #include "gromacs/utility/basedefinitions.h"
79 #include "gromacs/utility/fatalerror.h"
80 #include "gromacs/utility/futil.h"
81 #include "gromacs/utility/smalloc.h"
82 #include "gromacs/utility/txtdump.h"
91 void list_tpr(const char *fn,
92 gmx_bool bShowNumbers,
93 gmx_bool bShowParameters,
96 gmx_bool bOriginalInputrec)
105 read_tpxheader(fn, &tpx, TRUE);
108 tpx.bIr ? &ir : nullptr,
110 tpx.bTop ? &mtop : nullptr);
111 if (tpx.bIr && !bOriginalInputrec)
113 MDModules().adjustInputrecBasedOnModules(&ir);
116 if (mdpfn && tpx.bIr)
118 gp = gmx_fio_fopen(mdpfn, "w");
119 pr_inputrec(gp, 0, nullptr, &ir, TRUE);
127 top = gmx_mtop_t_to_t_topology(&mtop, false);
130 if (available(stdout, &tpx, 0, fn))
133 pr_title(stdout, indent, fn);
134 pr_inputrec(stdout, 0, "inputrec", tpx.bIr ? &ir : nullptr, FALSE);
136 pr_tpxheader(stdout, indent, "header", &(tpx));
140 pr_mtop(stdout, indent, "topology", &(mtop), bShowNumbers, bShowParameters);
144 pr_top(stdout, indent, "topology", &(top), bShowNumbers, bShowParameters);
147 pr_rvecs(stdout, indent, "box", tpx.bBox ? state.box : nullptr, DIM);
148 pr_rvecs(stdout, indent, "box_rel", tpx.bBox ? state.box_rel : nullptr, DIM);
149 pr_rvecs(stdout, indent, "boxv", tpx.bBox ? state.boxv : nullptr, DIM);
150 pr_rvecs(stdout, indent, "pres_prev", tpx.bBox ? state.pres_prev : nullptr, DIM);
151 pr_rvecs(stdout, indent, "svir_prev", tpx.bBox ? state.svir_prev : nullptr, DIM);
152 pr_rvecs(stdout, indent, "fvir_prev", tpx.bBox ? state.fvir_prev : nullptr, DIM);
153 /* leave nosehoover_xi in for now to match the tpr version */
154 pr_doubles(stdout, indent, "nosehoover_xi", state.nosehoover_xi.data(), state.ngtc);
155 /*pr_doubles(stdout,indent,"nosehoover_vxi",state.nosehoover_vxi,state.ngtc);*/
156 /*pr_doubles(stdout,indent,"therm_integral",state.therm_integral,state.ngtc);*/
157 pr_rvecs(stdout, indent, "x", tpx.bX ? state.x.rvec_array() : nullptr, state.natoms);
158 pr_rvecs(stdout, indent, "v", tpx.bV ? state.v.rvec_array() : nullptr, state.natoms);
161 const SimulationGroups &groups = mtop.groups;
163 gmx::EnumerationArray < SimulationAtomGroupType, std::vector < int>> gcount;
164 for (auto group : keysOf(gcount))
166 gcount[group].resize(groups.groups[group].nr);
169 for (int i = 0; (i < mtop.natoms); i++)
171 for (auto group : keysOf(gcount))
173 gcount[group][getGroupType(groups, group, i)]++;
176 printf("Group statistics\n");
177 for (auto group : keysOf(gcount))
180 printf("%-12s: ", shortName(group));
181 for (int j = 0; (j < groups.groups[group].nr); j++)
183 printf(" %5d", gcount[group][j]);
184 atot += gcount[group][j];
186 printf(" (total %d atoms)\n", atot);
191 //! Dump a topology file
192 void list_top(const char *fn)
195 // Legacy string length macro
198 char *cppopts[] = { nullptr };
200 status = cpp_open_file(fn, &handle, cppopts);
203 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
207 status = cpp_read_line(&handle, STRLEN, buf);
208 done = static_cast<int>(status == eCPP_EOF);
211 if (status != eCPP_OK)
213 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
222 status = cpp_close_file(&handle);
223 if (status != eCPP_OK)
225 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
230 void list_trr(const char *fn)
237 gmx_trr_header_t trrheader;
240 fpread = gmx_trr_open(fn, "r");
243 while (gmx_trr_read_frame_header(fpread, &trrheader, &bOK))
245 snew(x, trrheader.natoms);
246 snew(v, trrheader.natoms);
247 snew(f, trrheader.natoms);
248 if (gmx_trr_read_frame_data(fpread, &trrheader,
249 trrheader.box_size ? box : nullptr,
250 trrheader.x_size ? x : nullptr,
251 trrheader.v_size ? v : nullptr,
252 trrheader.f_size ? f : nullptr))
254 sprintf(buf, "%s frame %d", fn, nframe);
256 indent = pr_title(stdout, indent, buf);
257 pr_indent(stdout, indent);
258 fprintf(stdout, "natoms=%10d step=%10" PRId64 " time=%12.7e lambda=%10g\n",
259 trrheader.natoms, trrheader.step, trrheader.t, trrheader.lambda);
260 if (trrheader.box_size)
262 pr_rvecs(stdout, indent, "box", box, DIM);
264 if (trrheader.x_size)
266 pr_rvecs(stdout, indent, "x", x, trrheader.natoms);
268 if (trrheader.v_size)
270 pr_rvecs(stdout, indent, "v", v, trrheader.natoms);
272 if (trrheader.f_size)
274 pr_rvecs(stdout, indent, "f", f, trrheader.natoms);
279 fprintf(stderr, "\nWARNING: Incomplete frame: nr %d, t=%g\n",
280 nframe, trrheader.t);
290 fprintf(stderr, "\nWARNING: Incomplete frame header: nr %d, t=%g\n",
291 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",
320 natoms, step, time, prec);
321 pr_rvecs(stdout, indent, "box", box, DIM);
322 pr_rvecs(stdout, indent, "x", x, natoms);
325 while (read_next_xtc(xd, natoms, &step, &time, box, x, &prec, &bOK) != 0);
328 fprintf(stderr, "\nWARNING: Incomplete frame at time %g\n", time);
336 /*! \brief Callback used by list_tng_for_gmx_dump. */
337 void list_tng_inner(const char *fn,
338 gmx_bool bFirstFrame,
342 int64_t n_values_per_frame,
353 sprintf(buf, "%s frame %" PRId64, fn, nframe);
355 indent = pr_title(stdout, indent, buf);
356 pr_indent(stdout, indent);
357 fprintf(stdout, "natoms=%10" PRId64 " step=%10" PRId64 " time=%12.7e",
358 n_atoms, step, frame_time);
361 fprintf(stdout, " prec=%10g", prec);
363 fprintf(stdout, "\n");
365 pr_reals_of_dim(stdout, indent, block_name, values, n_atoms, n_values_per_frame);
371 void list_tng(const char *fn)
374 gmx_tng_trajectory_t tng;
376 int64_t i, *block_ids = nullptr, step, ndatablocks;
378 real *values = nullptr;
380 gmx_tng_open(fn, 'r', &tng);
381 gmx_print_tng_molecule_system(tng, stdout);
383 bOK = gmx_get_tng_data_block_types_of_next_frame(tng, -1,
390 for (i = 0; i < ndatablocks; i++)
394 int64_t n_values_per_frame, n_atoms;
395 char block_name[STRLEN];
397 gmx_get_tng_data_next_frame_of_block_type(tng, block_ids[i], &values,
399 &n_values_per_frame, &n_atoms,
401 block_name, STRLEN, &bOK);
404 /* Can't write any output because we don't know what
406 fprintf(stderr, "\nWARNING: Incomplete frame at time %g, will not write output\n", frame_time);
410 list_tng_inner(fn, (0 == i), values, step, frame_time,
411 n_values_per_frame, n_atoms, prec, nframe, block_name);
416 while (gmx_get_tng_data_block_types_of_next_frame(tng, step,
430 GMX_UNUSED_VALUE(fn);
434 //! Dump a trajectory file
435 void list_trx(const char *fn)
449 fprintf(stderr, "File %s is of an unsupported type. Try using the command\n 'less %s'\n",
454 //! Dump an energy file
455 void list_ene(const char *fn)
459 gmx_enxnm_t *enm = nullptr;
464 printf("gmx dump: %s\n", fn);
465 in = open_enx(fn, "r");
466 do_enxnms(in, &nre, &enm);
469 printf("energy components:\n");
470 for (i = 0; (i < nre); i++)
472 printf("%5d %-24s (%s)\n", i, enm[i].name, enm[i].unit);
478 bCont = do_enx(in, fr);
482 printf("\n%24s %12.5e %12s %12s\n", "time:",
483 fr->t, "step:", gmx_step_str(fr->step, buf));
484 printf("%24s %12s %12s %12s\n",
485 "", "", "nsteps:", gmx_step_str(fr->nsteps, buf));
486 printf("%24s %12.5e %12s %12s\n",
487 "delta_t:", fr->dt, "sum steps:", gmx_step_str(fr->nsum, buf));
490 printf("%24s %12s %12s %12s\n",
491 "Component", "Energy", "Av. Energy", "Sum Energy");
494 for (i = 0; (i < nre); i++)
496 printf("%24s %12.5e %12.5e %12.5e\n",
497 enm[i].name, fr->ener[i].e, fr->ener[i].eav,
503 for (i = 0; (i < nre); i++)
505 printf("%24s %12.5e\n",
506 enm[i].name, fr->ener[i].e);
510 for (b = 0; b < fr->nblock; b++)
512 const char *typestr = "";
514 t_enxblock *eb = &(fr->block[b]);
515 printf("Block data %2d (%3d subblocks, id=%d)\n",
516 b, eb->nsub, eb->id);
520 typestr = enx_block_id_name[eb->id];
522 printf(" id='%s'\n", typestr);
523 for (i = 0; i < eb->nsub; i++)
525 t_enxsubblock *sb = &(eb->sub[i]);
526 printf(" Sub block %3d (%5d elems, type=%s) values:\n",
527 i, sb->nr, xdr_datatype_names[sb->type]);
531 case xdr_datatype_float:
532 for (j = 0; j < sb->nr; j++)
534 printf("%14d %8.4f\n", j, sb->fval[j]);
537 case xdr_datatype_double:
538 for (j = 0; j < sb->nr; j++)
540 printf("%14d %10.6f\n", j, sb->dval[j]);
543 case xdr_datatype_int:
544 for (j = 0; j < sb->nr; j++)
546 printf("%14d %10d\n", j, sb->ival[j]);
549 case xdr_datatype_int64:
550 for (j = 0; j < sb->nr; j++)
553 j, gmx_step_str(sb->lval[j], buf));
556 case xdr_datatype_char:
557 for (j = 0; j < sb->nr; j++)
559 printf("%14d %1c\n", j, sb->cval[j]);
562 case xdr_datatype_string:
563 for (j = 0; j < sb->nr; j++)
565 printf("%14d %80s\n", j, sb->sval[j]);
569 gmx_incons("Unknown subblock type");
584 //! Dump a (Hessian) matrix file
585 void list_mtx(const char *fn)
587 int nrow, ncol, i, j, k;
588 real *full = nullptr, value;
589 gmx_sparsematrix_t * sparse = nullptr;
591 gmx_mtxio_read(fn, &nrow, &ncol, &full, &sparse);
595 snew(full, nrow*ncol);
596 for (i = 0; i < nrow*ncol; i++)
601 for (i = 0; i < sparse->nrow; i++)
603 for (j = 0; j < sparse->ndata[i]; j++)
605 k = sparse->data[i][j].col;
606 value = sparse->data[i][j].value;
607 full[i*ncol+k] = value;
608 full[k*ncol+i] = value;
611 gmx_sparsematrix_destroy(sparse);
614 printf("%d %d\n", nrow, ncol);
615 for (i = 0; i < nrow; i++)
617 for (j = 0; j < ncol; j++)
619 printf(" %g", full[i*ncol+j]);
627 class Dump : public ICommandLineOptionsModule
633 // From ICommandLineOptionsModule
634 void init(CommandLineModuleSettings * /*settings*/) override
638 void initOptions(IOptionsContainer *options,
639 ICommandLineOptionsModuleSettings *settings) override;
641 void optionsFinished() override;
646 //! Commandline options
648 bool bShowNumbers_ = true;
649 bool bShowParams_ = false;
650 bool bSysTop_ = false;
651 bool bOriginalInputrec_ = false;
653 //! Commandline file options
655 std::string inputTprFilename_;
656 std::string inputTrajectoryFilename_;
657 std::string inputEnergyFilename_;
658 std::string inputCheckpointFilename_;
659 std::string inputTopologyFilename_;
660 std::string inputMatrixFilename_;
661 std::string outputMdpFilename_;
665 void Dump::initOptions(IOptionsContainer *options,
666 ICommandLineOptionsModuleSettings *settings)
668 const char *desc[] = {
669 "[THISMODULE] reads a run input file ([REF].tpr[ref]),",
670 "a trajectory ([REF].trr[ref]/[REF].xtc[ref]/[TT]tng[tt]), an energy",
671 "file ([REF].edr[ref]), a checkpoint file ([REF].cpt[ref])",
672 "or topology file ([REF].top[ref])",
673 "and prints that to standard output in a readable format.",
674 "This program is essential for checking your run input file in case of",
677 settings->setHelpText(desc);
678 // TODO If this ancient note acknowledging a bug is still true,
679 // fix it or block that run path:
680 // Position restraint output from -sys -s is broken
682 options->addOption(FileNameOption("s")
683 .filetype(eftRunInput).inputFile()
684 .store(&inputTprFilename_)
685 .description("Run input file to dump"));
686 options->addOption(FileNameOption("f")
687 .filetype(eftTrajectory).inputFile()
688 .store(&inputTrajectoryFilename_)
689 .description("Trajectory file to dump"));
690 options->addOption(FileNameOption("e")
691 .filetype(eftEnergy).inputFile()
692 .store(&inputEnergyFilename_)
693 .description("Energy file to dump"));
694 options->addOption(FileNameOption("cp")
695 .legacyType(efCPT).inputFile()
696 .store(&inputCheckpointFilename_)
697 .description("Checkpoint file to dump"));
698 options->addOption(FileNameOption("p")
699 .legacyType(efTOP).inputFile()
700 .store(&inputTopologyFilename_)
701 .description("Topology file to dump"));
702 options->addOption(FileNameOption("mtx")
703 .legacyType(efMTX).inputFile()
704 .store(&inputMatrixFilename_)
705 .description("Hessian matrix to dump"));
706 options->addOption(FileNameOption("om")
707 .legacyType(efMDP).outputFile()
708 .store(&outputMdpFilename_)
709 .description("grompp input file from run input file"));
711 options->addOption(BooleanOption("nr")
712 .store(&bShowNumbers_).defaultValue(true)
713 .description("Show index numbers in output (leaving them out makes comparison easier, but creates a useless topology)"));
714 options->addOption(BooleanOption("param")
715 .store(&bShowParams_).defaultValue(false)
716 .description("Show parameters for each bonded interaction (for comparing dumps, it is useful to combine this with -nonr)"));
717 options->addOption(BooleanOption("sys")
718 .store(&bShowParams_).defaultValue(false)
719 .description("List the atoms and bonded interactions for the whole system instead of for each molecule type"));
720 options->addOption(BooleanOption("orgir")
721 .store(&bShowParams_).defaultValue(false)
722 .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"));
725 void Dump::optionsFinished()
727 // TODO Currently gmx dump ignores user input that seeks to dump
728 // multiple files. Here, we could enforce that the user only asks
734 if (!inputTprFilename_.empty())
736 list_tpr(inputTprFilename_.c_str(), bShowNumbers_, bShowParams_,
737 outputMdpFilename_.c_str(), bSysTop_, bOriginalInputrec_);
739 else if (!inputTrajectoryFilename_.empty())
741 list_trx(inputTrajectoryFilename_.c_str());
743 else if (!inputEnergyFilename_.empty())
745 list_ene(inputEnergyFilename_.c_str());
747 else if (!inputCheckpointFilename_.empty())
749 list_checkpoint(inputCheckpointFilename_.c_str(), stdout);
751 else if (!inputTopologyFilename_.empty())
753 list_top(inputTopologyFilename_.c_str());
755 else if (!inputMatrixFilename_.empty())
757 list_mtx(inputMatrixFilename_.c_str());
765 const char DumpInfo::name[] = "dump";
766 const char DumpInfo::shortDescription[] =
767 "Make binary files human readable";
768 ICommandLineOptionsModulePointer DumpInfo::create()
770 return std::make_unique<Dump>();