Split lines with many copyright years
[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, 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     int i;
52
53     for (i = 0; i < n; i++)
54     {
55         fprintf(fp, " ");
56     }
57     return n;
58 }
59
60 bool available(FILE* fp, const void* p, int indent, const char* title)
61 {
62     if (!p)
63     {
64         if (indent > 0)
65         {
66             pr_indent(fp, indent);
67         }
68         fprintf(fp, "%s: not available\n", title);
69     }
70     return (p != nullptr);
71 }
72
73 int pr_title(FILE* fp, int indent, const char* title)
74 {
75     pr_indent(fp, indent);
76     fprintf(fp, "%s:\n", title);
77     return (indent + INDENT);
78 }
79
80 int pr_title_n(FILE* fp, int indent, const char* title, int n)
81 {
82     pr_indent(fp, indent);
83     fprintf(fp, "%s (%d):\n", title, n);
84     return (indent + INDENT);
85 }
86
87 int pr_title_nxn(FILE* fp, int indent, const char* title, int n1, int n2)
88 {
89     pr_indent(fp, indent);
90     fprintf(fp, "%s (%dx%d):\n", title, n1, n2);
91     return (indent + INDENT);
92 }
93
94 void pr_reals(FILE* fp, int indent, const char* title, const real* vec, int n)
95 {
96     int i;
97
98     if (available(fp, vec, indent, title))
99     {
100         pr_indent(fp, indent);
101         fprintf(fp, "%s:\t", title);
102         for (i = 0; i < n; i++)
103         {
104             fprintf(fp, "  %10g", vec[i]);
105         }
106         fprintf(fp, "\n");
107     }
108 }
109
110 void pr_doubles(FILE* fp, int indent, const char* title, const double* vec, int n)
111 {
112     int i;
113
114     if (available(fp, vec, indent, title))
115     {
116         pr_indent(fp, indent);
117         fprintf(fp, "%s:\t", title);
118         for (i = 0; i < n; i++)
119         {
120             fprintf(fp, "  %10g", vec[i]);
121         }
122         fprintf(fp, "\n");
123     }
124 }
125
126 void pr_reals_of_dim(FILE* fp, int indent, const char* title, const real* vec, int n, int dim)
127 {
128     int         i, j;
129     const char* fshort = "%12.5e";
130     const char* flong  = "%15.8e";
131     const char* format;
132
133     if (getenv("GMX_PRINT_LONGFORMAT") != nullptr)
134     {
135         format = flong;
136     }
137     else
138     {
139         format = fshort;
140     }
141
142     if (available(fp, vec, indent, title))
143     {
144         indent = pr_title_nxn(fp, indent, title, n, dim);
145         for (i = 0; i < n; i++)
146         {
147             pr_indent(fp, indent);
148             fprintf(fp, "%s[%5d]={", title, i);
149             for (j = 0; j < dim; j++)
150             {
151                 if (j != 0)
152                 {
153                     fprintf(fp, ", ");
154                 }
155                 fprintf(fp, format, vec[i * dim + j]);
156             }
157             fprintf(fp, "}\n");
158         }
159     }
160 }
161
162 void pr_int(FILE* fp, int indent, const char* title, int i)
163 {
164     pr_indent(fp, indent);
165     fprintf(fp, "%-30s = %d\n", title, i);
166 }
167
168 void pr_int64(FILE* fp, int indent, const char* title, int64_t i)
169 {
170     char buf[STEPSTRSIZE];
171
172     pr_indent(fp, indent);
173     fprintf(fp, "%-30s = %s\n", title, gmx_step_str(i, buf));
174 }
175
176 void pr_real(FILE* fp, int indent, const char* title, real r)
177 {
178     pr_indent(fp, indent);
179     fprintf(fp, "%-30s = %g\n", title, r);
180 }
181
182 void pr_double(FILE* fp, int indent, const char* title, double d)
183 {
184     pr_indent(fp, indent);
185     fprintf(fp, "%-30s = %g\n", title, d);
186 }
187
188 void pr_str(FILE* fp, int indent, const char* title, const char* s)
189 {
190     pr_indent(fp, indent);
191     fprintf(fp, "%-30s = %s\n", title, s);
192 }
193
194 void pr_strings(FILE* fp, int indent, const char* title, char*** nm, int n, gmx_bool bShowNumbers)
195 {
196     int i;
197
198     if (available(fp, nm, indent, title))
199     {
200         indent = pr_title_n(fp, indent, title, n);
201         for (i = 0; i < n; i++)
202         {
203             pr_indent(fp, indent);
204             fprintf(fp, "%s[%d]={name=\"%s\"}\n", title, bShowNumbers ? i : -1, *(nm[i]));
205         }
206     }
207 }