Remove utility -> fileio dependencies
[alexxy/gromacs.git] / src / gromacs / tools / dump.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-2013, 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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <math.h>
44 #include <assert.h>
45 #include "macros.h"
46 #include "gromacs/commandline/pargs.h"
47 #include "sysstuff.h"
48 #include "txtdump.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/utility/smalloc.h"
51 #include "names.h"
52 #include "txtdump.h"
53 #include "gromacs/gmxpreprocess/gmxcpp.h"
54 #include "checkpoint.h"
55 #include "mtop_util.h"
56 #include "gromacs/fileio/xtcio.h"
57 #include "gromacs/fileio/enxio.h"
58 #include "gromacs/fileio/gmxfio.h"
59 #include "gromacs/fileio/tpxio.h"
60 #include "gromacs/fileio/trnio.h"
61 #include "gromacs/utility/futil.h"
62 #include "gromacs/fileio/tngio.h"
63 #include "gromacs/fileio/tngio_for_tools.h"
64
65 #ifdef HAVE_UNISTD_H
66 #include <unistd.h>
67 #endif
68
69 #include "gromacs/linearalgebra/mtxio.h"
70 #include "gromacs/linearalgebra/sparsematrix.h"
71
72
73 static void list_tpx(const char *fn, gmx_bool bShowNumbers, const char *mdpfn,
74                      gmx_bool bSysTop)
75 {
76     FILE         *gp;
77     int           fp, indent, i, j, **gcount, atot;
78     t_state       state;
79     rvec         *f = NULL;
80     t_inputrec    ir;
81     t_tpxheader   tpx;
82     gmx_mtop_t    mtop;
83     gmx_groups_t *groups;
84     t_topology    top;
85
86     read_tpxheader(fn, &tpx, TRUE, NULL, NULL);
87
88     read_tpx_state(fn,
89                    tpx.bIr  ? &ir : NULL,
90                    &state, tpx.bF ? f : NULL,
91                    tpx.bTop ? &mtop : NULL);
92
93     if (mdpfn && tpx.bIr)
94     {
95         gp = gmx_fio_fopen(mdpfn, "w");
96         pr_inputrec(gp, 0, NULL, &(ir), TRUE);
97         gmx_fio_fclose(gp);
98     }
99
100     if (!mdpfn)
101     {
102         if (bSysTop)
103         {
104             top = gmx_mtop_t_to_t_topology(&mtop);
105         }
106
107         if (available(stdout, &tpx, 0, fn))
108         {
109             indent = 0;
110             indent = pr_title(stdout, indent, fn);
111             pr_inputrec(stdout, 0, "inputrec", tpx.bIr ? &(ir) : NULL, FALSE);
112
113             indent = 0;
114             pr_header(stdout, indent, "header", &(tpx));
115
116             if (!bSysTop)
117             {
118                 pr_mtop(stdout, indent, "topology", &(mtop), bShowNumbers);
119             }
120             else
121             {
122                 pr_top(stdout, indent, "topology", &(top), bShowNumbers);
123             }
124
125             pr_rvecs(stdout, indent, "box", tpx.bBox ? state.box : NULL, DIM);
126             pr_rvecs(stdout, indent, "box_rel", tpx.bBox ? state.box_rel : NULL, DIM);
127             pr_rvecs(stdout, indent, "boxv", tpx.bBox ? state.boxv : NULL, DIM);
128             pr_rvecs(stdout, indent, "pres_prev", tpx.bBox ? state.pres_prev : NULL, DIM);
129             pr_rvecs(stdout, indent, "svir_prev", tpx.bBox ? state.svir_prev : NULL, DIM);
130             pr_rvecs(stdout, indent, "fvir_prev", tpx.bBox ? state.fvir_prev : NULL, DIM);
131             /* leave nosehoover_xi in for now to match the tpr version */
132             pr_doubles(stdout, indent, "nosehoover_xi", state.nosehoover_xi, state.ngtc);
133             /*pr_doubles(stdout,indent,"nosehoover_vxi",state.nosehoover_vxi,state.ngtc);*/
134             /*pr_doubles(stdout,indent,"therm_integral",state.therm_integral,state.ngtc);*/
135             pr_rvecs(stdout, indent, "x", tpx.bX ? state.x : NULL, state.natoms);
136             pr_rvecs(stdout, indent, "v", tpx.bV ? state.v : NULL, state.natoms);
137             if (tpx.bF)
138             {
139                 pr_rvecs(stdout, indent, "f", f, state.natoms);
140             }
141         }
142
143         groups = &mtop.groups;
144
145         snew(gcount, egcNR);
146         for (i = 0; (i < egcNR); i++)
147         {
148             snew(gcount[i], groups->grps[i].nr);
149         }
150
151         for (i = 0; (i < mtop.natoms); i++)
152         {
153             for (j = 0; (j < egcNR); j++)
154             {
155                 gcount[j][ggrpnr(groups, j, i)]++;
156             }
157         }
158         printf("Group statistics\n");
159         for (i = 0; (i < egcNR); i++)
160         {
161             atot = 0;
162             printf("%-12s: ", gtypes[i]);
163             for (j = 0; (j < groups->grps[i].nr); j++)
164             {
165                 printf("  %5d", gcount[i][j]);
166                 atot += gcount[i][j];
167             }
168             printf("  (total %d atoms)\n", atot);
169             sfree(gcount[i]);
170         }
171         sfree(gcount);
172     }
173     done_state(&state);
174     sfree(f);
175 }
176
177 static void list_top(const char *fn)
178 {
179     int       status, done;
180 #define BUFLEN 256
181     char      buf[BUFLEN];
182     gmx_cpp_t handle;
183     char     *cppopts[] = { NULL };
184
185     status = cpp_open_file(fn, &handle, cppopts);
186     if (status != 0)
187     {
188         gmx_fatal(FARGS, cpp_error(&handle, status));
189     }
190     do
191     {
192         status = cpp_read_line(&handle, BUFLEN, buf);
193         done   = (status == eCPP_EOF);
194         if (!done)
195         {
196             if (status != eCPP_OK)
197             {
198                 gmx_fatal(FARGS, cpp_error(&handle, status));
199             }
200             else
201             {
202                 printf("%s\n", buf);
203             }
204         }
205     }
206     while (!done);
207     status = cpp_close_file(&handle);
208     if (status != eCPP_OK)
209     {
210         gmx_fatal(FARGS, cpp_error(&handle, status));
211     }
212 }
213
214 static void list_trn(const char *fn)
215 {
216     t_fileio       *fpread, *fpwrite;
217     int             nframe, indent;
218     char            buf[256];
219     rvec           *x, *v, *f;
220     matrix          box;
221     t_trnheader     trn;
222     gmx_bool        bOK;
223
224     fpread  = open_trn(fn, "r");
225     fpwrite = open_tpx(NULL, "w");
226     gmx_fio_setdebug(fpwrite, TRUE);
227
228     nframe = 0;
229     while (fread_trnheader(fpread, &trn, &bOK))
230     {
231         snew(x, trn.natoms);
232         snew(v, trn.natoms);
233         snew(f, trn.natoms);
234         if (fread_htrn(fpread, &trn,
235                        trn.box_size ? box : NULL,
236                        trn.x_size   ? x : NULL,
237                        trn.v_size   ? v : NULL,
238                        trn.f_size   ? f : NULL))
239         {
240             sprintf(buf, "%s frame %d", fn, nframe);
241             indent = 0;
242             indent = pr_title(stdout, indent, buf);
243             pr_indent(stdout, indent);
244             fprintf(stdout, "natoms=%10d  step=%10d  time=%12.7e  lambda=%10g\n",
245                     trn.natoms, trn.step, trn.t, trn.lambda);
246             if (trn.box_size)
247             {
248                 pr_rvecs(stdout, indent, "box", box, DIM);
249             }
250             if (trn.x_size)
251             {
252                 pr_rvecs(stdout, indent, "x", x, trn.natoms);
253             }
254             if (trn.v_size)
255             {
256                 pr_rvecs(stdout, indent, "v", v, trn.natoms);
257             }
258             if (trn.f_size)
259             {
260                 pr_rvecs(stdout, indent, "f", f, trn.natoms);
261             }
262         }
263         else
264         {
265             fprintf(stderr, "\nWARNING: Incomplete frame: nr %d, t=%g\n",
266                     nframe, trn.t);
267         }
268
269         sfree(x);
270         sfree(v);
271         sfree(f);
272         nframe++;
273     }
274     if (!bOK)
275     {
276         fprintf(stderr, "\nWARNING: Incomplete frame header: nr %d, t=%g\n",
277                 nframe, trn.t);
278     }
279     close_tpx(fpwrite);
280     close_trn(fpread);
281 }
282
283 void list_xtc(const char *fn)
284 {
285     t_fileio  *xd;
286     int        indent;
287     char       buf[256];
288     rvec      *x;
289     matrix     box;
290     int        nframe, natoms, step;
291     real       prec, time;
292     gmx_bool   bOK;
293
294     xd = open_xtc(fn, "r");
295     read_first_xtc(xd, &natoms, &step, &time, box, &x, &prec, &bOK);
296
297     nframe = 0;
298     do
299     {
300         sprintf(buf, "%s frame %d", fn, nframe);
301         indent = 0;
302         indent = pr_title(stdout, indent, buf);
303         pr_indent(stdout, indent);
304         fprintf(stdout, "natoms=%10d  step=%10d  time=%12.7e  prec=%10g\n",
305                 natoms, step, time, prec);
306         pr_rvecs(stdout, indent, "box", box, DIM);
307         pr_rvecs(stdout, indent, "x", x, natoms);
308         nframe++;
309     }
310     while (read_next_xtc(xd, natoms, &step, &time, box, x, &prec, &bOK));
311     if (!bOK)
312     {
313         fprintf(stderr, "\nWARNING: Incomplete frame at time %g\n", time);
314     }
315     sfree(x);
316     close_xtc(xd);
317 }
318
319 /*! \brief Callback used by list_tng_for_gmx_dump. */
320 static void list_tng_inner(const char *fn,
321                            gmx_bool    bFirstFrame,
322                            real       *values,
323                            gmx_int64_t step,
324                            double      frame_time,
325                            gmx_int64_t n_values_per_frame,
326                            gmx_int64_t n_atoms,
327                            real        prec,
328                            gmx_int64_t nframe,
329                            char       *block_name)
330 {
331     char                 buf[256];
332     int                  indent = 0;
333
334     if (bFirstFrame)
335     {
336         sprintf(buf, "%s frame %" GMX_PRId64, fn, nframe);
337         indent = 0;
338         indent = pr_title(stdout, indent, buf);
339         pr_indent(stdout, indent);
340         fprintf(stdout, "natoms=%10" GMX_PRId64 "  step=%10" GMX_PRId64 "  time=%12.7e",
341                 n_atoms, step, frame_time);
342         if (prec > 0)
343         {
344             fprintf(stdout, "  prec=%10g", prec);
345         }
346         fprintf(stdout, "\n");
347     }
348     pr_reals_of_dim(stdout, indent, block_name, values, n_atoms, n_values_per_frame);
349 }
350
351 static void list_tng(const char gmx_unused *fn)
352 {
353 #ifdef GMX_USE_TNG
354     tng_trajectory_t     tng;
355     gmx_int64_t          nframe = 0;
356     gmx_int64_t          i, *block_ids = NULL, step, ndatablocks;
357     gmx_bool             bOK;
358
359     gmx_tng_open(fn, 'r', &tng);
360     gmx_print_tng_molecule_system(tng, stdout);
361
362     bOK    = gmx_get_tng_data_block_types_of_next_frame(tng, -1,
363                                                         0,
364                                                         NULL,
365                                                         &step, &ndatablocks,
366                                                         &block_ids);
367     do
368     {
369         for (i = 0; i < ndatablocks; i++)
370         {
371             double               frame_time;
372             real                 prec, *values = NULL;
373             gmx_int64_t          n_values_per_frame, n_atoms;
374             char                 block_name[STRLEN];
375
376             gmx_get_tng_data_next_frame_of_block_type(tng, block_ids[i], &values,
377                                                       &step, &frame_time,
378                                                       &n_values_per_frame, &n_atoms,
379                                                       &prec,
380                                                       block_name, STRLEN, &bOK);
381             if (!bOK)
382             {
383                 /* Can't write any output because we don't know what
384                    arrays are valid. */
385                 fprintf(stderr, "\nWARNING: Incomplete frame at time %g, will not write output\n", frame_time);
386                 list_tng_inner(fn, (0 == i), values, step, frame_time,
387                                n_values_per_frame, n_atoms, prec, nframe, block_name);
388             }
389         }
390         nframe++;
391     }
392     while (gmx_get_tng_data_block_types_of_next_frame(tng, step,
393                                                       0,
394                                                       NULL,
395                                                       &step,
396                                                       &ndatablocks,
397                                                       &block_ids));
398
399     if (block_ids)
400     {
401         sfree(block_ids);
402     }
403
404     gmx_tng_close(&tng);
405 #endif
406 }
407
408 void list_trx(const char *fn)
409 {
410     int ftp;
411
412     ftp = fn2ftp(fn);
413     if (ftp == efXTC)
414     {
415         list_xtc(fn);
416     }
417     else if ((ftp == efTRR) || (ftp == efTRJ))
418     {
419         list_trn(fn);
420     }
421     else if (ftp == efTNG)
422     {
423         list_tng(fn);
424     }
425     else
426     {
427         fprintf(stderr, "File %s is of an unsupported type. Try using the command\n 'less %s'\n",
428                 fn, fn);
429     }
430 }
431
432 void list_ene(const char *fn)
433 {
434     int            ndr;
435     ener_file_t    in;
436     gmx_bool       bCont;
437     gmx_enxnm_t   *enm = NULL;
438     t_enxframe    *fr;
439     int            i, j, nre, b;
440     real           rav, minthird;
441     char           buf[22];
442
443     printf("gmx dump: %s\n", fn);
444     in = open_enx(fn, "r");
445     do_enxnms(in, &nre, &enm);
446     assert(enm);
447
448     printf("energy components:\n");
449     for (i = 0; (i < nre); i++)
450     {
451         printf("%5d  %-24s (%s)\n", i, enm[i].name, enm[i].unit);
452     }
453
454     minthird = -1.0/3.0;
455     snew(fr, 1);
456     do
457     {
458         bCont = do_enx(in, fr);
459
460         if (bCont)
461         {
462             printf("\n%24s  %12.5e  %12s  %12s\n", "time:",
463                    fr->t, "step:", gmx_step_str(fr->step, buf));
464             printf("%24s  %12s  %12s  %12s\n",
465                    "", "", "nsteps:", gmx_step_str(fr->nsteps, buf));
466             printf("%24s  %12.5e  %12s  %12s\n",
467                    "delta_t:", fr->dt, "sum steps:", gmx_step_str(fr->nsum, buf));
468             if (fr->nre == nre)
469             {
470                 printf("%24s  %12s  %12s  %12s\n",
471                        "Component", "Energy", "Av. Energy", "Sum Energy");
472                 if (fr->nsum > 0)
473                 {
474                     for (i = 0; (i < nre); i++)
475                     {
476                         printf("%24s  %12.5e  %12.5e  %12.5e\n",
477                                enm[i].name, fr->ener[i].e, fr->ener[i].eav,
478                                fr->ener[i].esum);
479                     }
480                 }
481                 else
482                 {
483                     for (i = 0; (i < nre); i++)
484                     {
485                         printf("%24s  %12.5e\n",
486                                enm[i].name, fr->ener[i].e);
487                     }
488                 }
489             }
490             for (b = 0; b < fr->nblock; b++)
491             {
492                 const char *typestr = "";
493
494                 t_enxblock *eb = &(fr->block[b]);
495                 printf("Block data %2d (%3d subblocks, id=%d)\n",
496                        b, eb->nsub, eb->id);
497
498                 if (eb->id < enxNR)
499                 {
500                     typestr = enx_block_id_name[eb->id];
501                 }
502                 printf("  id='%s'\n", typestr);
503                 for (i = 0; i < eb->nsub; i++)
504                 {
505                     t_enxsubblock *sb = &(eb->sub[i]);
506                     printf("  Sub block %3d (%5d elems, type=%s) values:\n",
507                            i, sb->nr, xdr_datatype_names[sb->type]);
508
509                     switch (sb->type)
510                     {
511                         case xdr_datatype_float:
512                             for (j = 0; j < sb->nr; j++)
513                             {
514                                 printf("%14d   %8.4f\n", j, sb->fval[j]);
515                             }
516                             break;
517                         case xdr_datatype_double:
518                             for (j = 0; j < sb->nr; j++)
519                             {
520                                 printf("%14d   %10.6f\n", j, sb->dval[j]);
521                             }
522                             break;
523                         case xdr_datatype_int:
524                             for (j = 0; j < sb->nr; j++)
525                             {
526                                 printf("%14d %10d\n", j, sb->ival[j]);
527                             }
528                             break;
529                         case xdr_datatype_int64:
530                             for (j = 0; j < sb->nr; j++)
531                             {
532                                 printf("%14d %s\n",
533                                        j, gmx_step_str(sb->lval[j], buf));
534                             }
535                             break;
536                         case xdr_datatype_char:
537                             for (j = 0; j < sb->nr; j++)
538                             {
539                                 printf("%14d %1c\n", j, sb->cval[j]);
540                             }
541                             break;
542                         case xdr_datatype_string:
543                             for (j = 0; j < sb->nr; j++)
544                             {
545                                 printf("%14d %80s\n", j, sb->sval[j]);
546                             }
547                             break;
548                         default:
549                             gmx_incons("Unknown subblock type");
550                     }
551                 }
552             }
553         }
554     }
555     while (bCont);
556
557     close_enx(in);
558
559     free_enxframe(fr);
560     sfree(fr);
561     sfree(enm);
562 }
563
564 static void list_mtx(const char *fn)
565 {
566     int                  nrow, ncol, i, j, k;
567     real                *full   = NULL, value;
568     gmx_sparsematrix_t * sparse = NULL;
569
570     gmx_mtxio_read(fn, &nrow, &ncol, &full, &sparse);
571
572     if (full == NULL)
573     {
574         snew(full, nrow*ncol);
575         for (i = 0; i < nrow*ncol; i++)
576         {
577             full[i] = 0;
578         }
579
580         for (i = 0; i < sparse->nrow; i++)
581         {
582             for (j = 0; j < sparse->ndata[i]; j++)
583             {
584                 k              = sparse->data[i][j].col;
585                 value          = sparse->data[i][j].value;
586                 full[i*ncol+k] = value;
587                 full[k*ncol+i] = value;
588             }
589         }
590         gmx_sparsematrix_destroy(sparse);
591     }
592
593     printf("%d %d\n", nrow, ncol);
594     for (i = 0; i < nrow; i++)
595     {
596         for (j = 0; j < ncol; j++)
597         {
598             printf(" %g", full[i*ncol+j]);
599         }
600         printf("\n");
601     }
602
603     sfree(full);
604 }
605
606 int gmx_dump(int argc, char *argv[])
607 {
608     const char *desc[] = {
609         "[THISMODULE] reads a run input file ([TT].tpa[tt]/[TT].tpr[tt]/[TT].tpb[tt]),",
610         "a trajectory ([TT].trj[tt]/[TT].trr[tt]/[TT].xtc[tt]), an energy",
611         "file ([TT].ene[tt]/[TT].edr[tt]), or a checkpoint file ([TT].cpt[tt])",
612         "and prints that to standard output in a readable format.",
613         "This program is essential for checking your run input file in case of",
614         "problems.[PAR]",
615         "The program can also preprocess a topology to help finding problems.",
616         "Note that currently setting [TT]GMXLIB[tt] is the only way to customize",
617         "directories used for searching include files.",
618     };
619     const char *bugs[] = {
620         "Position restraint output from -sys -s is broken"
621     };
622     t_filenm    fnm[] = {
623         { efTPX, "-s", NULL, ffOPTRD },
624         { efTRX, "-f", NULL, ffOPTRD },
625         { efEDR, "-e", NULL, ffOPTRD },
626         { efCPT, NULL, NULL, ffOPTRD },
627         { efTOP, "-p", NULL, ffOPTRD },
628         { efMTX, "-mtx", "hessian", ffOPTRD },
629         { efMDP, "-om", NULL, ffOPTWR }
630     };
631 #define NFILE asize(fnm)
632
633     output_env_t    oenv;
634     /* Command line options */
635     static gmx_bool bShowNumbers = TRUE;
636     static gmx_bool bSysTop      = FALSE;
637     t_pargs         pa[]         = {
638         { "-nr", FALSE, etBOOL, {&bShowNumbers}, "Show index numbers in output (leaving them out makes comparison easier, but creates a useless topology)" },
639         { "-sys", FALSE, etBOOL, {&bSysTop}, "List the atoms and bonded interactions for the whole system instead of for each molecule type" }
640     };
641
642     if (!parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa), pa,
643                            asize(desc), desc, asize(bugs), bugs, &oenv))
644     {
645         return 0;
646     }
647
648
649     if (ftp2bSet(efTPX, NFILE, fnm))
650     {
651         list_tpx(ftp2fn(efTPX, NFILE, fnm), bShowNumbers,
652                  ftp2fn_null(efMDP, NFILE, fnm), bSysTop);
653     }
654     else if (ftp2bSet(efTRX, NFILE, fnm))
655     {
656         list_trx(ftp2fn(efTRX, NFILE, fnm));
657     }
658     else if (ftp2bSet(efEDR, NFILE, fnm))
659     {
660         list_ene(ftp2fn(efEDR, NFILE, fnm));
661     }
662     else if (ftp2bSet(efCPT, NFILE, fnm))
663     {
664         list_checkpoint(ftp2fn(efCPT, NFILE, fnm), stdout);
665     }
666     else if (ftp2bSet(efTOP, NFILE, fnm))
667     {
668         list_top(ftp2fn(efTOP, NFILE, fnm));
669     }
670     else if (ftp2bSet(efMTX, NFILE, fnm))
671     {
672         list_mtx(ftp2fn(efMTX, NFILE, fnm));
673     }
674
675     return 0;
676 }