ba671f9a334dcef0c3aed7c664d105d6f7f59c35
[alexxy/gromacs.git] / include / histogram.h
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-2009, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * 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 /*! \file
39  * \brief API for calculation of histograms with error estimates.
40  *
41  * The API is documented in more detail on a separate page:
42  * \ref histograms
43  *
44  * The functions within this file can be used and developed independently of
45  * the other parts of the library.
46  * Other parts of the library do not reference these functions.
47  */
48 #ifndef HISTOGRAM_H
49 #define HISTOGRAM_H
50
51 #include "typedefs.h"
52
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif
57
58 /** Type of histogram. */
59 typedef enum
60 {
61     /*! \brief
62      * Simple histogram.
63      *
64      * Use gmx_histogram_increment() or gmx_histogram_increment_bin()
65      * to sample.
66      */
67     HIST_SIMPLE,
68     /*! \brief
69      * Weighted histogram where different points contribute different amounts.
70      *
71      * Use gmx_histogram_add() or gmx_histogram_add_to_bin() to sample.
72      */
73     HIST_WEIGHT,
74     /*! \brief
75      * Calculate averages within each bin.
76      *
77      * Use gmx_histogram_add_item() or gmx_histogram_add_item_to_bin() to sample.
78      */
79     HIST_BINAVER 
80 } e_histogram_t;
81
82 /** Whether bins are centered at integer values. */
83 #define HIST_INTEGERBINS  1
84 /** Whether the values outside the range should be included in the histogram. */
85 #define HIST_ALL          2
86 /** Whether the initialization used binwidths. */
87 #define HIST_INITBW       128
88
89 /** Stores data for a histogram. */
90 typedef struct gmx_histogram_t gmx_histogram_t;
91
92 /*! \name Initialization functions
93  */
94 /*@{*/
95 /** Initialize calculation of a histogram. */
96 int
97 gmx_histogram_create(gmx_histogram_t **h, e_histogram_t type, int nbins);
98 /** Initialize calculation of a histogram for a range. */
99 int
100 gmx_histogram_create_range(gmx_histogram_t **h, e_histogram_t type,
101                            real start, real end, real binw, gmx_bool bIntegerBins);
102 /** Clears the bins in the histogram. */
103 void
104 gmx_histogram_clear(gmx_histogram_t *h);
105 /** Frees the memory allocated for a histogram. */
106 void
107 gmx_histogram_free(gmx_histogram_t *h);
108 /** Sets histogram range using a starting point and a bin width. */
109 int
110 gmx_histogram_set_binwidth(gmx_histogram_t *h, real start, real binw);
111 /** Sets histogram range using endpoint values. */
112 int
113 gmx_histogram_set_range(gmx_histogram_t *h, real start, real end);
114 /** Sets histogram bins to center at integer values. */
115 void
116 gmx_histogram_set_integerbins(gmx_histogram_t *h, gmx_bool bIntegerBins);
117 /** Sets histogram to include outlying values in the bins at the edges. */
118 void
119 gmx_histogram_set_all(gmx_histogram_t *h, gmx_bool bAll);
120 /** Sets block size for histogram averaging. */
121 int
122 gmx_histogram_set_blocksize(gmx_histogram_t *h, int bsize);
123 /** Sets output file for block histograms. */
124 int
125 gmx_histogram_set_block_output(gmx_histogram_t *h, FILE *fp);
126 /*@}*/
127
128 /*! \name Access functions
129  */
130 /*@{*/
131 /** Finds the histogram bin corresponding to a value. */
132 int
133 gmx_histogram_find_bin(gmx_histogram_t *h, real pos);
134 /** Returns the number of bins in a histogram. */
135 int
136 gmx_histogram_get_nbins(gmx_histogram_t *h);
137 /** Returns the bin width of a histogram. */
138 real
139 gmx_histogram_get_binwidth(gmx_histogram_t *h);
140 /** Returns the value of the histogram at a certain position. */
141 void
142 gmx_histogram_get_value(gmx_histogram_t *h, real pos, double *val, double *err);
143 /** Returns the value of the histogram in a certain bin. */
144 void
145 gmx_histogram_get_bin_value(gmx_histogram_t *h, int bin, double *val, double *err);
146 /** Returns an array of values for the histogram. */
147 double *
148 gmx_histogram_get_values(gmx_histogram_t *h);
149 /** Returns an array of error values for the histogram. */
150 double *
151 gmx_histogram_get_errors(gmx_histogram_t *h);
152 /*@}*/
153
154 /*! \name Sampling functions
155  */
156 /*@{*/
157 /** Increments the count in a histogram bin corresponding to \p pos. */
158 void
159 gmx_histogram_increment(gmx_histogram_t *h, real pos);
160 /** Increments the count in a histogram bin. */
161 void
162 gmx_histogram_increment_bin(gmx_histogram_t *h, int bin);
163
164 /** Adds a value to a histogram bin corresponding to \p pos. */
165 void
166 gmx_histogram_add(gmx_histogram_t *h, real pos, double value);
167 /** Adds a value to a histogram bin. */
168 void
169 gmx_histogram_add_to_bin(gmx_histogram_t *h, int bin, double value);
170
171 /** Adds a value to a histogram bin corresponding to \p pos. */
172 void
173 gmx_histogram_add_item(gmx_histogram_t *h, real pos, double value);
174 /** Adds a value to a histogram bin. */
175 void
176 gmx_histogram_add_item_to_bin(gmx_histogram_t *h, int bin, double value);
177
178 /** Finishes histogram sampling for a frame. */
179 void
180 gmx_histogram_finish_frame(gmx_histogram_t *h);
181 /** Normalizes a histogram. */
182 void
183 gmx_histogram_finish(gmx_histogram_t *h);
184 /*@}*/
185
186
187 /*! \name Post-processing functions
188  */
189 /*@{*/
190 /** Creates a new histogram with double the binwidth. */
191 void
192 gmx_histogram_resample_dblbw(gmx_histogram_t **dest, gmx_histogram_t *src,
193                              gmx_bool bIntegerBins);
194 /** Makes a clone of a histogram. */
195 void
196 gmx_histogram_clone(gmx_histogram_t **dest, gmx_histogram_t *src);
197 /** Normalizes a histogram to a probability distribution. */
198 void
199 gmx_histogram_normalize_prob(gmx_histogram_t *h);
200 /** Scales a histogram with a custom normalization factor. */
201 void
202 gmx_histogram_scale(gmx_histogram_t *h, real norm);
203 /** Scales a histogram with a custom non-uniform normalization factor. */
204 void
205 gmx_histogram_scale_vec(gmx_histogram_t *h, real norm[]);
206 /** Writes a single histogram to a file. */
207 void
208 gmx_histogram_write(FILE *fp, gmx_histogram_t *h, gmx_bool bErrors);
209 /** Writes a set of histograms to a file. */
210 void
211 gmx_histogram_write_array(FILE *fp, int n, gmx_histogram_t *h[],
212                               gmx_bool bValue, gmx_bool bErrors);
213 /** Writes a set of cumulative histograms to a file. */
214 void
215 gmx_histogram_write_cum_array(FILE *fp, int n, gmx_histogram_t *h[],
216                               gmx_bool bValue, gmx_bool bErrors);
217 /*@}*/
218
219 #ifdef __cplusplus
220 }
221 #endif
222
223 #endif