Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / statistics / statistics_test.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-2008, The GROMACS development team.
6  * Copyright (c) 2011,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 #include "gmxpre.h"
38
39 #include <stdio.h>
40
41 #include "gromacs/math/vec.h"
42 #include "gromacs/random/random.h"
43 #include "gromacs/statistics/statistics.h"
44 #include "gromacs/utility/real.h"
45 #include "gromacs/utility/smalloc.h"
46
47 static void horizontal()
48 {
49     gmx_rng_t   rng;
50     gmx_stats_t straight;
51     int         i, ok, n = 1000;
52     real        y, a, b, da, db, aver, sigma, error, chi2, R, *xh, *yh;
53     FILE       *fp;
54
55     rng      = gmx_rng_init(13);
56     straight = gmx_stats_init();
57     for (i = 0; (i < n); i++)
58     {
59         y = gmx_rng_uniform_real(rng);
60         if ((ok = gmx_stats_add_point(straight, i, y, 0, 0)) != estatsOK)
61         {
62             fprintf(stderr, "%s\n", gmx_stats_message(ok));
63         }
64     }
65     /* Horizontal test */
66     if ((ok = gmx_stats_get_ase(straight, &aver, &sigma, &error)) != estatsOK)
67     {
68         fprintf(stderr, "%s\n", gmx_stats_message(ok));
69     }
70     fp = fopen("straight.xvg", "w");
71     if ((ok = gmx_stats_dump_xy(straight, fp)) != estatsOK)
72     {
73         fprintf(stderr, "%s\n", gmx_stats_message(ok));
74     }
75     fclose(fp);
76     printf("Horizontal line: average %g, sigma %g, error %g\n", aver, sigma, error);
77     if ((ok = gmx_stats_done(straight)) != estatsOK)
78     {
79         fprintf(stderr, "%s\n", gmx_stats_message(ok));
80     }
81 }
82
83 static void line()
84 {
85     gmx_rng_t   rng;
86     gmx_stats_t line;
87     int         i, dy, ok, n = 1000;
88     real        y, a, b, da, db, aver, sigma, error, chi2, R, rfit;
89     const real  a0 = 0.23, b0 = 2.7;
90     FILE       *fp;
91
92     for (dy = 0; (dy < 2); dy++)
93     {
94         rng      = gmx_rng_init(13);
95         line     = gmx_stats_init();
96         for (i = 0; (i < n); i++)
97         {
98             y = a0*i+b0+50*(gmx_rng_uniform_real(rng)-0.5);
99             if ((ok = gmx_stats_add_point(line, i, y, 0, dy*0.1)) != estatsOK)
100             {
101                 fprintf(stderr, "%s\n", gmx_stats_message(ok));
102             }
103         }
104         /* Line with slope test */
105         if ((ok = gmx_stats_get_ab(line, elsqWEIGHT_NONE, &a, &b, &da, &db, &chi2, &rfit)) != estatsOK)
106         {
107             fprintf(stderr, "%s\n", gmx_stats_message(ok));
108         }
109         if ((ok = gmx_stats_get_corr_coeff(line, &R)) != estatsOK)
110         {
111             fprintf(stderr, "%s\n", gmx_stats_message(ok));
112         }
113         if (dy == 0)
114         {
115             fp = fopen("line0.xvg", "w");
116         }
117         else
118         {
119             fp = fopen("line1.xvg", "w");
120         }
121         if ((ok = gmx_stats_dump_xy(line, fp)) != estatsOK)
122         {
123             fprintf(stderr, "%s\n", gmx_stats_message(ok));
124         }
125         fclose(fp);
126         printf("Line with eqn. y = %gx + %g with noise%s\n", a0, b0,
127                (dy == 0) ? "" : " and uncertainties");
128         printf("Found: a = %g +/- %g, b = %g +/- %g\n", a, da, b, db);
129         if ((ok = gmx_stats_done(line)) != estatsOK)
130         {
131             fprintf(stderr, "%s\n", gmx_stats_message(ok));
132         }
133         gmx_rng_destroy(rng);
134     }
135 }
136
137 static void histogram()
138 {
139     gmx_rng_t   rng;
140     gmx_stats_t camel;
141     int         i, ok, n = 1000, norm;
142     real        y, a, b, da, db, aver, sigma, error, chi2, R, *xh, *yh;
143     const real  a0 = 0.23, b0 = 2.7;
144     FILE       *fp;
145     char        fn[256];
146
147     for (norm = 0; (norm < 2); norm++)
148     {
149         rng      = gmx_rng_init(13);
150         camel    = gmx_stats_init();
151         for (i = 0; (i < n); i++)
152         {
153             y = sqr(gmx_rng_uniform_real(rng));
154             if ((ok = gmx_stats_add_point(camel, i, y+1, 0, 0)) != estatsOK)
155             {
156                 fprintf(stderr, "%s\n", gmx_stats_message(ok));
157             }
158             y = sqr(gmx_rng_uniform_real(rng));
159             if ((ok = gmx_stats_add_point(camel, i+0.5, y+2, 0, 0)) != estatsOK)
160             {
161                 fprintf(stderr, "%s\n", gmx_stats_message(ok));
162             }
163         }
164         /* Histogram test */
165         if ((ok = gmx_stats_make_histogram(camel, 0, 101, norm, &xh, &yh)) != estatsOK)
166         {
167             fprintf(stderr, "%s\n", gmx_stats_message(ok));
168         }
169         sprintf(fn, "histo%d-data.xvg", norm);
170         fp = fopen(fn, "w");
171         gmx_stats_dump_xy(camel, fp);
172         fclose(fp);
173         sprintf(fn, "histo%d.xvg", norm);
174         fp = fopen(fn, "w");
175         for (i = 0; (i < 101); i++)
176         {
177             fprintf(fp, "%12g  %12g\n", xh[i], yh[i]);
178         }
179         fclose(fp);
180         sfree(xh);
181         sfree(yh);
182     }
183 }
184
185 int main(int argc, char *argv[])
186 {
187     line();
188     horizontal();
189     histogram();
190
191     return 0;
192 }