b535cd07ac9177d7eeb7f21f4f6310f0657d838f
[alexxy/gromacs.git] / src / gromacs / mdtypes / df_history.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /* This file is completely threadsafe - keep it that way! */
36 #include "gmxpre.h"
37
38 #include "df_history.h"
39
40 #include "gromacs/mdtypes/state.h"
41 #include "gromacs/utility/smalloc.h"
42
43 void init_df_history(df_history_t *dfhist, int nlambda)
44 {
45     int i;
46
47     dfhist->nlambda  = nlambda;
48     dfhist->bEquil   = 0;
49     dfhist->wl_delta = 0;
50
51     if (nlambda > 0)
52     {
53         snew(dfhist->sum_weights, dfhist->nlambda);
54         snew(dfhist->sum_dg, dfhist->nlambda);
55         snew(dfhist->sum_minvar, dfhist->nlambda);
56         snew(dfhist->sum_variance, dfhist->nlambda);
57         snew(dfhist->n_at_lam, dfhist->nlambda);
58         snew(dfhist->wl_histo, dfhist->nlambda);
59
60         /* allocate transition matrices here */
61         snew(dfhist->Tij, dfhist->nlambda);
62         snew(dfhist->Tij_empirical, dfhist->nlambda);
63
64         /* allocate accumulators for various transition matrix
65            free energy methods here */
66         snew(dfhist->accum_p, dfhist->nlambda);
67         snew(dfhist->accum_m, dfhist->nlambda);
68         snew(dfhist->accum_p2, dfhist->nlambda);
69         snew(dfhist->accum_m2, dfhist->nlambda);
70
71         for (i = 0; i < dfhist->nlambda; i++)
72         {
73             snew(dfhist->Tij[i], dfhist->nlambda);
74             snew(dfhist->Tij_empirical[i], dfhist->nlambda);
75             snew((dfhist->accum_p)[i], dfhist->nlambda);
76             snew((dfhist->accum_m)[i], dfhist->nlambda);
77             snew((dfhist->accum_p2)[i], dfhist->nlambda);
78             snew((dfhist->accum_m2)[i], dfhist->nlambda);
79         }
80     }
81 }
82
83 void copy_df_history(df_history_t *df_dest, df_history_t *df_source)
84 {
85     int i, j;
86
87     /* Currently, there should not be any difference in nlambda between the two,
88        but this is included for completeness for potential later functionality */
89     df_dest->nlambda  = df_source->nlambda;
90     df_dest->bEquil   = df_source->bEquil;
91     df_dest->wl_delta = df_source->wl_delta;
92
93     for (i = 0; i < df_dest->nlambda; i++)
94     {
95         df_dest->sum_weights[i]  = df_source->sum_weights[i];
96         df_dest->sum_dg[i]       = df_source->sum_dg[i];
97         df_dest->sum_minvar[i]   = df_source->sum_minvar[i];
98         df_dest->sum_variance[i] = df_source->sum_variance[i];
99         df_dest->n_at_lam[i]     = df_source->n_at_lam[i];
100         df_dest->wl_histo[i]     = df_source->wl_histo[i];
101     }
102
103     for (i = 0; i < df_dest->nlambda; i++)
104     {
105         for (j = 0; j < df_dest->nlambda; j++)
106         {
107             df_dest->accum_p[i][j]        = df_source->accum_p[i][j];
108             df_dest->accum_m[i][j]        = df_source->accum_m[i][j];
109             df_dest->accum_p2[i][j]       = df_source->accum_p2[i][j];
110             df_dest->accum_m2[i][j]       = df_source->accum_m2[i][j];
111             df_dest->Tij[i][j]            = df_source->Tij[i][j];
112             df_dest->Tij_empirical[i][j]  = df_source->Tij_empirical[i][j];
113         }
114     }
115 }
116
117 void done_df_history(df_history_t *dfhist)
118 {
119     int i;
120
121     if (dfhist->nlambda > 0)
122     {
123         sfree(dfhist->n_at_lam);
124         sfree(dfhist->wl_histo);
125         sfree(dfhist->sum_weights);
126         sfree(dfhist->sum_dg);
127         sfree(dfhist->sum_minvar);
128         sfree(dfhist->sum_variance);
129
130         for (i = 0; i < dfhist->nlambda; i++)
131         {
132             sfree(dfhist->Tij[i]);
133             sfree(dfhist->Tij_empirical[i]);
134             sfree(dfhist->accum_p[i]);
135             sfree(dfhist->accum_m[i]);
136             sfree(dfhist->accum_p2[i]);
137             sfree(dfhist->accum_m2[i]);
138         }
139     }
140     dfhist->bEquil   = 0;
141     dfhist->nlambda  = 0;
142     dfhist->wl_delta = 0;
143 }