Fix remaining copyright headers
[alexxy/gromacs.git] / src / gromacs / gmxlib / rmpbc.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-2004, The GROMACS development team.
6  * Copyright (c) 2013, 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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "sysstuff.h"
42 #include "typedefs.h"
43 #include "smalloc.h"
44 #include "mshift.h"
45 #include "pbc.h"
46 #include "gstat.h"
47 #include "gromacs/fileio/futil.h"
48 #include "vec.h"
49
50 typedef struct {
51     int      natoms;
52     t_graph *gr;
53 } rmpbc_graph_t;
54
55 typedef struct gmx_rmpbc {
56     t_idef        *idef;
57     int            natoms_init;
58     int            ePBC;
59     int            ngraph;
60     rmpbc_graph_t *graph;
61 } koeiepoep;
62
63 static t_graph *gmx_rmpbc_get_graph(gmx_rmpbc_t gpbc, int ePBC, int natoms)
64 {
65     int            i;
66     rmpbc_graph_t *gr;
67
68     if (ePBC == epbcNONE
69         || NULL == gpbc
70         || NULL == gpbc->idef
71         || gpbc->idef->ntypes <= 0)
72     {
73         return NULL;
74     }
75
76     gr = NULL;
77     for (i = 0; i < gpbc->ngraph; i++)
78     {
79         if (natoms == gpbc->graph[i].natoms)
80         {
81             gr = &gpbc->graph[i];
82         }
83     }
84     if (gr == NULL)
85     {
86         /* We'd like to check with the number of atoms in the topology,
87          * but we don't have that available.
88          * So we check against the number of atoms that gmx_rmpbc_init
89          * was called with.
90          */
91         if (natoms > gpbc->natoms_init)
92         {
93             gmx_fatal(FARGS, "Structure or trajectory file has more atoms (%d) than the topology (%d)", natoms, gpbc->natoms_init);
94         }
95         gpbc->ngraph++;
96         srenew(gpbc->graph, gpbc->ngraph);
97         gr         = &gpbc->graph[gpbc->ngraph-1];
98         gr->natoms = natoms;
99         gr->gr     = mk_graph(NULL, gpbc->idef, 0, natoms, FALSE, FALSE);
100     }
101
102     return gr->gr;
103 }
104
105 gmx_rmpbc_t gmx_rmpbc_init(t_idef *idef, int ePBC, int natoms)
106 {
107     gmx_rmpbc_t gpbc;
108
109     snew(gpbc, 1);
110
111     gpbc->natoms_init = natoms;
112
113     /* This sets pbc when we now it,
114      * otherwise we guess it from the instantaneous box in the trajectory.
115      */
116     gpbc->ePBC = ePBC;
117
118     gpbc->idef = idef;
119     if (gpbc->idef->ntypes <= 0)
120     {
121         fprintf(stderr,
122                 "\n"
123                 "WARNING: If there are molecules in the input trajectory file\n"
124                 "         that are broken across periodic boundaries, they\n"
125                 "         cannot be made whole (or treated as whole) without\n"
126                 "         you providing a run input file.\n\n");
127     }
128
129     return gpbc;
130 }
131
132 void gmx_rmpbc_done(gmx_rmpbc_t gpbc)
133 {
134     int i;
135
136     if (NULL != gpbc)
137     {
138         for (i = 0; i < gpbc->ngraph; i++)
139         {
140             done_graph(gpbc->graph[i].gr);
141             sfree(gpbc->graph[i].gr);
142         }
143         if (gpbc->graph != NULL)
144         {
145             sfree(gpbc->graph);
146         }
147         sfree(gpbc);
148     }
149 }
150
151 static int gmx_rmpbc_ePBC(gmx_rmpbc_t gpbc, matrix box)
152 {
153     if (NULL != gpbc && gpbc->ePBC >= 0)
154     {
155         return gpbc->ePBC;
156     }
157     else
158     {
159         return guess_ePBC(box);
160     }
161 }
162
163 void gmx_rmpbc(gmx_rmpbc_t gpbc, int natoms, matrix box, rvec x[])
164 {
165     int      ePBC;
166     t_graph *gr;
167
168     ePBC = gmx_rmpbc_ePBC(gpbc, box);
169     gr   = gmx_rmpbc_get_graph(gpbc, ePBC, natoms);
170     if (gr != NULL)
171     {
172         mk_mshift(stdout, gr, ePBC, box, x);
173         shift_self(gr, box, x);
174     }
175 }
176
177 void gmx_rmpbc_copy(gmx_rmpbc_t gpbc, int natoms, matrix box, rvec x[], rvec x_s[])
178 {
179     int      ePBC;
180     t_graph *gr;
181     int      i;
182
183     ePBC = gmx_rmpbc_ePBC(gpbc, box);
184     gr   = gmx_rmpbc_get_graph(gpbc, ePBC, natoms);
185     if (gr != NULL)
186     {
187         mk_mshift(stdout, gr, ePBC, box, x);
188         shift_x(gr, box, x, x_s);
189     }
190     else
191     {
192         for (i = 0; i < natoms; i++)
193         {
194             copy_rvec(x[i], x_s[i]);
195         }
196     }
197 }
198
199 void gmx_rmpbc_trxfr(gmx_rmpbc_t gpbc, t_trxframe *fr)
200 {
201     int      ePBC;
202     t_graph *gr;
203
204     if (fr->bX && fr->bBox)
205     {
206         ePBC = gmx_rmpbc_ePBC(gpbc, fr->box);
207         gr   = gmx_rmpbc_get_graph(gpbc, ePBC, fr->natoms);
208         if (gr != NULL)
209         {
210             mk_mshift(stdout, gr, ePBC, fr->box, fr->x);
211             shift_self(gr, fr->box, fr->x);
212         }
213     }
214 }
215
216 void rm_gropbc(t_atoms *atoms, rvec x[], matrix box)
217 {
218     real dist;
219     int  n, m, d;
220
221     /* check periodic boundary */
222     for (n = 1; (n < atoms->nr); n++)
223     {
224         for (m = DIM-1; m >= 0; m--)
225         {
226             dist = x[n][m]-x[n-1][m];
227             if (fabs(dist) > 0.9*box[m][m])
228             {
229                 if (dist >  0)
230                 {
231                     for (d = 0; d <= m; d++)
232                     {
233                         x[n][d] -= box[m][d];
234                     }
235                 }
236                 else
237                 {
238                     for (d = 0; d <= m; d++)
239                     {
240                         x[n][d] += box[m][d];
241                     }
242                 }
243             }
244         }
245     }
246 }