Improve MessageStringCollector
[alexxy/gromacs.git] / src / gromacs / utility / txtdump.cpp
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,2015,2017,2018 by the GROMACS development team.
7  * Copyright (c) 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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #include "gmxpre.h"
39
40 /* This file is completely threadsafe - please keep it that way! */
41
42 #include "txtdump.h"
43
44 #include <cstdio>
45 #include <cstdlib>
46
47 #include "gromacs/utility/cstringutil.h"
48
49 int pr_indent(FILE* fp, int n)
50 {
51     for (int i = 0; i < n; i++)
52     {
53         fprintf(fp, " ");
54     }
55     return n;
56 }
57
58 bool available(FILE* fp, const void* p, int indent, const char* title)
59 {
60     if (!p)
61     {
62         if (indent > 0)
63         {
64             pr_indent(fp, indent);
65         }
66         fprintf(fp, "%s: not available\n", title);
67     }
68     return (p != nullptr);
69 }
70
71 int pr_title(FILE* fp, int indent, const char* title)
72 {
73     pr_indent(fp, indent);
74     fprintf(fp, "%s:\n", title);
75     return (indent + INDENT);
76 }
77
78 int pr_title_n(FILE* fp, int indent, const char* title, int n)
79 {
80     pr_indent(fp, indent);
81     fprintf(fp, "%s (%d):\n", title, n);
82     return (indent + INDENT);
83 }
84
85 int pr_title_nxn(FILE* fp, int indent, const char* title, int n1, int n2)
86 {
87     pr_indent(fp, indent);
88     fprintf(fp, "%s (%dx%d):\n", title, n1, n2);
89     return (indent + INDENT);
90 }
91
92 void pr_reals(FILE* fp, int indent, const char* title, const real* vec, int n)
93 {
94     if (available(fp, vec, indent, title))
95     {
96         pr_indent(fp, indent);
97         fprintf(fp, "%s:\t", title);
98         for (int i = 0; i < n; i++)
99         {
100             fprintf(fp, "  %10g", vec[i]);
101         }
102         fprintf(fp, "\n");
103     }
104 }
105
106 void pr_doubles(FILE* fp, int indent, const char* title, const double* vec, int n)
107 {
108     if (available(fp, vec, indent, title))
109     {
110         pr_indent(fp, indent);
111         fprintf(fp, "%s:\t", title);
112         for (int i = 0; i < n; i++)
113         {
114             fprintf(fp, "  %10g", vec[i]);
115         }
116         fprintf(fp, "\n");
117     }
118 }
119
120 void pr_reals_of_dim(FILE* fp, int indent, const char* title, const real* vec, int n, int dim)
121 {
122     const char* fshort = "%12.5e";
123     const char* flong  = "%15.8e";
124     const char* format = (getenv("GMX_PRINT_LONGFORMAT") != nullptr) ? flong : fshort;
125
126     if (available(fp, vec, indent, title))
127     {
128         indent = pr_title_nxn(fp, indent, title, n, dim);
129         for (int i = 0; i < n; i++)
130         {
131             pr_indent(fp, indent);
132             fprintf(fp, "%s[%5d]={", title, i);
133             for (int j = 0; j < dim; j++)
134             {
135                 if (j != 0)
136                 {
137                     fprintf(fp, ", ");
138                 }
139                 fprintf(fp, format, vec[i * dim + j]);
140             }
141             fprintf(fp, "}\n");
142         }
143     }
144 }
145
146 void pr_int(FILE* fp, int indent, const char* title, int i)
147 {
148     pr_indent(fp, indent);
149     fprintf(fp, "%-30s = %d\n", title, i);
150 }
151
152 void pr_int64(FILE* fp, int indent, const char* title, int64_t i)
153 {
154     char buf[STEPSTRSIZE];
155
156     pr_indent(fp, indent);
157     fprintf(fp, "%-30s = %s\n", title, gmx_step_str(i, buf));
158 }
159
160 void pr_real(FILE* fp, int indent, const char* title, real r)
161 {
162     pr_indent(fp, indent);
163     fprintf(fp, "%-30s = %g\n", title, r);
164 }
165
166 void pr_double(FILE* fp, int indent, const char* title, double d)
167 {
168     pr_indent(fp, indent);
169     fprintf(fp, "%-30s = %g\n", title, d);
170 }
171
172 void pr_str(FILE* fp, int indent, const char* title, const char* s)
173 {
174     pr_indent(fp, indent);
175     fprintf(fp, "%-30s = %s\n", title, s);
176 }
177
178 void pr_strings(FILE* fp, int indent, const char* title, char*** nm, int n, gmx_bool bShowNumbers)
179 {
180     if (available(fp, nm, indent, title))
181     {
182         indent = pr_title_n(fp, indent, title, n);
183         for (int i = 0; i < n; i++)
184         {
185             pr_indent(fp, indent);
186             fprintf(fp, "%s[%d]={name=\"%s\"}\n", title, bShowNumbers ? i : -1, *(nm[i]));
187         }
188     }
189 }