6e640d69ce6bf817197c6d03157692e4e0d22f2e
[alexxy/gromacs.git] / src / gromacs / math / vecdump.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,2016,2017 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 #include "vecdump.h"
41
42 #include <cstdio>
43 #include <cstdlib>
44
45 #include "gromacs/math/vec.h"
46 #include "gromacs/utility/strconvert.h"
47 #include "gromacs/utility/txtdump.h"
48
49 void pr_ivec(FILE* fp, int indent, const char* title, const int vec[], int n, gmx_bool bShowNumbers)
50 {
51     int i;
52
53     if (available(fp, vec, indent, title))
54     {
55         indent = pr_title_n(fp, indent, title, n);
56         for (i = 0; i < n; i++)
57         {
58             pr_indent(fp, indent);
59             fprintf(fp, "%s[%d]=%d\n", title, bShowNumbers ? i : -1, vec[i]);
60         }
61     }
62 }
63
64 void pr_ivec_block(FILE* fp, int indent, const char* title, const int vec[], int n, gmx_bool bShowNumbers)
65 {
66     int i, j;
67
68     if (available(fp, vec, indent, title))
69     {
70         indent = pr_title_n(fp, indent, title, n);
71         i      = 0;
72         while (i < n)
73         {
74             j = i + 1;
75             while (j < n && vec[j] == vec[j - 1] + 1)
76             {
77                 j++;
78             }
79             /* Print consecutive groups of 3 or more as blocks */
80             if (j - i < 3)
81             {
82                 while (i < j)
83                 {
84                     pr_indent(fp, indent);
85                     fprintf(fp, "%s[%d]=%d\n", title, bShowNumbers ? i : -1, vec[i]);
86                     i++;
87                 }
88             }
89             else
90             {
91                 pr_indent(fp, indent);
92                 fprintf(fp, "%s[%d,...,%d] = {%d,...,%d}\n", title, bShowNumbers ? i : -1,
93                         bShowNumbers ? j - 1 : -1, vec[i], vec[j - 1]);
94                 i = j;
95             }
96         }
97     }
98 }
99
100 void pr_bvec(FILE* fp, int indent, const char* title, const gmx_bool vec[], int n, gmx_bool bShowNumbers)
101 {
102     int i;
103
104     if (available(fp, vec, indent, title))
105     {
106         indent = pr_title_n(fp, indent, title, n);
107         for (i = 0; i < n; i++)
108         {
109             pr_indent(fp, indent);
110             fprintf(fp, "%s[%d]=%s\n", title, bShowNumbers ? i : -1, gmx::boolToString(vec[i]));
111         }
112     }
113 }
114
115 void pr_ivecs(FILE* fp, int indent, const char* title, const ivec vec[], int n, gmx_bool bShowNumbers)
116 {
117     int i, j;
118
119     if (available(fp, vec, indent, title))
120     {
121         indent = pr_title_nxn(fp, indent, title, n, DIM);
122         for (i = 0; i < n; i++)
123         {
124             pr_indent(fp, indent);
125             fprintf(fp, "%s[%d]={", title, bShowNumbers ? i : -1);
126             for (j = 0; j < DIM; j++)
127             {
128                 if (j != 0)
129                 {
130                     fprintf(fp, ", ");
131                 }
132                 fprintf(fp, "%d", vec[i][j]);
133             }
134             fprintf(fp, "}\n");
135         }
136     }
137 }
138
139 template<typename T>
140 static void printRealVector(FILE* fp, int indent, const char* title, const T vec[], int n, gmx_bool bShowNumbers)
141 {
142     if (available(fp, vec, indent, title))
143     {
144         indent = pr_title_n(fp, indent, title, n);
145         for (int i = 0; i < n; i++)
146         {
147             pr_indent(fp, indent);
148             fprintf(fp, "%s[%d]=%12.5e\n", title, bShowNumbers ? i : -1, vec[i]);
149         }
150     }
151 }
152
153 void pr_rvec(FILE* fp, int indent, const char* title, const real vec[], int n, gmx_bool bShowNumbers)
154 {
155     printRealVector<real>(fp, indent, title, vec, n, bShowNumbers);
156 }
157
158 void pr_fvec(FILE* fp, int indent, const char* title, const float vec[], int n, gmx_bool bShowNumbers)
159 {
160     printRealVector<float>(fp, indent, title, vec, n, bShowNumbers);
161 }
162
163 void pr_dvec(FILE* fp, int indent, const char* title, const double vec[], int n, gmx_bool bShowNumbers)
164 {
165     printRealVector<double>(fp, indent, title, vec, n, bShowNumbers);
166 }
167
168
169 void pr_rvecs_len(FILE* fp, int indent, const char* title, const rvec vec[], int n)
170 {
171     int i, j;
172
173     if (available(fp, vec, indent, title))
174     {
175         indent = pr_title_nxn(fp, indent, title, n, DIM);
176         for (i = 0; i < n; i++)
177         {
178             pr_indent(fp, indent);
179             fprintf(fp, "%s[%5d]={", title, i);
180             for (j = 0; j < DIM; j++)
181             {
182                 if (j != 0)
183                 {
184                     fprintf(fp, ", ");
185                 }
186                 fprintf(fp, "%12.5e", vec[i][j]);
187             }
188             fprintf(fp, "} len=%12.5e\n", norm(vec[i]));
189         }
190     }
191 }
192
193 void pr_rvecs(FILE* fp, int indent, const char* title, const rvec vec[], int n)
194 {
195     const char* fshort = "%12.5e";
196     const char* flong  = "%15.8e";
197     const char* format;
198     int         i, j;
199
200     if (getenv("GMX_PRINT_LONGFORMAT") != nullptr)
201     {
202         format = flong;
203     }
204     else
205     {
206         format = fshort;
207     }
208
209     if (available(fp, vec, indent, title))
210     {
211         indent = pr_title_nxn(fp, indent, title, n, DIM);
212         for (i = 0; i < n; i++)
213         {
214             pr_indent(fp, indent);
215             fprintf(fp, "%s[%5d]={", title, i);
216             for (j = 0; j < DIM; j++)
217             {
218                 if (j != 0)
219                 {
220                     fprintf(fp, ", ");
221                 }
222                 fprintf(fp, format, vec[i][j]);
223             }
224             fprintf(fp, "}\n");
225         }
226     }
227 }
228
229
230 void pr_rvecs_of_dim(FILE* fp, int indent, const char* title, const rvec vec[], int n, int dim)
231 {
232     const char* fshort = "%12.5e";
233     const char* flong  = "%15.8e";
234     const char* format;
235     int         i, j;
236
237     if (getenv("GMX_PRINT_LONGFORMAT") != nullptr)
238     {
239         format = flong;
240     }
241     else
242     {
243         format = fshort;
244     }
245
246     if (available(fp, vec, indent, title))
247     {
248         indent = pr_title_nxn(fp, indent, title, n, dim);
249         for (i = 0; i < n; i++)
250         {
251             pr_indent(fp, indent);
252             fprintf(fp, "%s[%5d]={", title, i);
253             for (j = 0; j < dim; j++)
254             {
255                 if (j != 0)
256                 {
257                     fprintf(fp, ", ");
258                 }
259                 fprintf(fp, format, vec[i][j]);
260             }
261             fprintf(fp, "}\n");
262         }
263     }
264 }