Replace rounding using int(x+0.5) with roundToInt
[alexxy/gromacs.git] / src / gromacs / domdec / domdec_box.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2014,2015,2017,2018, 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
36 /*! \internal \file
37  *
38  * \brief This file defines functions used by the domdec module
39  * for (bounding) box and pbc information generation.
40  *
41  * \author Berk Hess <hess@kth.se>
42  * \ingroup module_domdec
43  */
44
45 #include "gmxpre.h"
46
47 #include "config.h"
48
49 #include "gromacs/domdec/domdec.h"
50 #include "gromacs/domdec/domdec_network.h"
51 #include "gromacs/domdec/domdec_struct.h"
52 #include "gromacs/gmxlib/network.h"
53 #include "gromacs/math/functions.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/mdlib/nsgrid.h"
56 #include "gromacs/mdtypes/commrec.h"
57 #include "gromacs/mdtypes/inputrec.h"
58 #include "gromacs/pbcutil/pbc.h"
59 #include "gromacs/topology/block.h"
60 #include "gromacs/utility/basedefinitions.h"
61 #include "gromacs/utility/fatalerror.h"
62
63 #include "domdec_internal.h"
64
65 /*! \brief Calculates the average and standard deviation in 3D of n atoms */
66 static void calc_pos_av_stddev(int n, const rvec *x,
67                                rvec av, rvec stddev,
68                                const MPI_Comm *mpiCommunicator)
69 {
70     dvec   s1, s2;
71
72     clear_dvec(s1);
73     clear_dvec(s2);
74
75     for (int i = 0; i < n; i++)
76     {
77         for (int d = 0; d < DIM; d++)
78         {
79             s1[d] += x[i][d];
80             s2[d] += x[i][d]*x[i][d];
81         }
82     }
83
84 #if GMX_MPI
85     if (mpiCommunicator)
86     {
87         constexpr int c_bufSize = 7;
88         double        sendBuffer[c_bufSize];
89         double        receiveBuffer[c_bufSize];
90
91         for (int d = 0; d < DIM; d++)
92         {
93             sendBuffer[d]       = s1[d];
94             sendBuffer[DIM + d] = s2[d];
95         }
96         sendBuffer[6] = n;
97
98         MPI_Allreduce(sendBuffer, receiveBuffer, c_bufSize, MPI_DOUBLE,
99                       MPI_SUM, *mpiCommunicator);
100
101         for (int d = 0; d < DIM; d++)
102         {
103             s1[d] = receiveBuffer[d];
104             s2[d] = receiveBuffer[DIM + d];
105         }
106         n = gmx::roundToInt(receiveBuffer[6]);
107     }
108 #else  // GMX_MPI
109     GMX_UNUSED_VALUE(mpiCommunicator);
110 #endif // GMX_MPI
111
112     dsvmul(1.0/n, s1, s1);
113     dsvmul(1.0/n, s2, s2);
114
115     for (int d = 0; d < DIM; d++)
116     {
117         av[d]     = s1[d];
118         stddev[d] = std::sqrt(s2[d] - s1[d]*s1[d]);
119     }
120 }
121
122 /*! \brief Determines if dimensions require triclinic treatment and stores this info in ddbox */
123 static void set_tric_dir(const ivec *dd_nc, gmx_ddbox_t *ddbox, const matrix box)
124 {
125     int   npbcdim, d, i, j;
126     rvec *v, *normal;
127     real  dep, inv_skew_fac2;
128
129     npbcdim = ddbox->npbcdim;
130     normal  = ddbox->normal;
131     for (d = 0; d < DIM; d++)
132     {
133         ddbox->tric_dir[d] = 0;
134         for (j = d+1; j < npbcdim; j++)
135         {
136             if (box[j][d] != 0)
137             {
138                 ddbox->tric_dir[d] = 1;
139                 if (dd_nc != nullptr && (*dd_nc)[j] > 1 && (*dd_nc)[d] == 1)
140                 {
141                     gmx_fatal(FARGS, "Domain decomposition has not been implemented for box vectors that have non-zero components in directions that do not use domain decomposition: ncells = %d %d %d, box vector[%d] = %f %f %f",
142                               (*dd_nc)[XX], (*dd_nc)[YY], (*dd_nc)[ZZ],
143                               j+1, box[j][XX], box[j][YY], box[j][ZZ]);
144                 }
145             }
146         }
147
148         /* Construct vectors v for dimension d that are perpendicular
149          * to the triclinic plane of dimension d. Each vector v[i] has
150          * v[i][i]=1 and v[i][d]!=0 for triclinic dimensions, while the third
151          * component is zero. These are used for computing the distance
152          * to a triclinic plane given the distance along dimension d.
153          * Set the trilinic skewing factor that translates
154          * the thickness of a slab perpendicular to this dimension
155          * into the real thickness of the slab.
156          */
157         if (ddbox->tric_dir[d])
158         {
159             inv_skew_fac2 = 1;
160             v             = ddbox->v[d];
161             if (d == XX || d == YY)
162             {
163                 /* Normalize such that the "diagonal" is 1 */
164                 svmul(1/box[d+1][d+1], box[d+1], v[d+1]);
165                 for (i = 0; i < d; i++)
166                 {
167                     v[d+1][i] = 0;
168                 }
169                 inv_skew_fac2 += gmx::square(v[d+1][d]);
170                 if (d == XX)
171                 {
172                     /* Normalize such that the "diagonal" is 1 */
173                     svmul(1/box[d+2][d+2], box[d+2], v[d+2]);
174                     /* Set v[d+2][d+1] to zero by shifting along v[d+1] */
175                     dep = v[d+2][d+1]/v[d+1][d+1];
176                     for (i = 0; i < DIM; i++)
177                     {
178                         v[d+2][i] -= dep*v[d+1][i];
179                     }
180                     inv_skew_fac2 += gmx::square(v[d+2][d]);
181
182                     cprod(v[d+1], v[d+2], normal[d]);
183                 }
184                 else
185                 {
186                     /* cross product with (1,0,0) */
187                     normal[d][XX] =  0;
188                     normal[d][YY] =  v[d+1][ZZ];
189                     normal[d][ZZ] = -v[d+1][YY];
190                 }
191                 if (debug)
192                 {
193                     fprintf(debug, "box[%d]  %.3f %.3f %.3f\n",
194                             d, box[d][XX], box[d][YY], box[d][ZZ]);
195                     for (i = d+1; i < DIM; i++)
196                     {
197                         fprintf(debug, "  v[%d]  %.3f %.3f %.3f\n",
198                                 i, v[i][XX], v[i][YY], v[i][ZZ]);
199                     }
200                 }
201             }
202             ddbox->skew_fac[d] = 1.0/std::sqrt(inv_skew_fac2);
203             /* Set the normal vector length to skew_fac */
204             dep = ddbox->skew_fac[d]/norm(normal[d]);
205             svmul(dep, normal[d], normal[d]);
206
207             if (debug)
208             {
209                 fprintf(debug, "skew_fac[%d] = %f\n", d, ddbox->skew_fac[d]);
210                 fprintf(debug, "normal[%d]  %.3f %.3f %.3f\n",
211                         d, normal[d][XX], normal[d][YY], normal[d][ZZ]);
212             }
213         }
214         else
215         {
216             ddbox->skew_fac[d] = 1;
217
218             for (i = 0; i < DIM; i++)
219             {
220                 clear_rvec(ddbox->v[d][i]);
221                 ddbox->v[d][i][i] = 1;
222             }
223             clear_rvec(normal[d]);
224             normal[d][d] = 1;
225         }
226     }
227 }
228
229 /*! \brief This function calculates bounding box and pbc info and populates ddbox */
230 static void low_set_ddbox(const t_inputrec *ir, const ivec *dd_nc, const matrix box,
231                           bool calculateUnboundedSize,
232                           int numAtoms, const rvec *x,
233                           const MPI_Comm *mpiCommunicator,
234                           gmx_ddbox_t *ddbox)
235 {
236     rvec av, stddev;
237     real b0, b1;
238     int  d;
239
240     ddbox->npbcdim     = ePBC2npbcdim(ir->ePBC);
241     ddbox->nboundeddim = inputrec2nboundeddim(ir);
242
243     for (d = 0; d < ddbox->nboundeddim; d++)
244     {
245         ddbox->box0[d]     = 0;
246         ddbox->box_size[d] = box[d][d];
247     }
248
249     if (ddbox->nboundeddim < DIM && calculateUnboundedSize)
250     {
251         calc_pos_av_stddev(numAtoms, x, av, stddev, mpiCommunicator);
252
253         /* GRID_STDDEV_FAC * stddev
254          * gives a uniform load for a rectangular block of cg's.
255          * For a sphere it is not a bad approximation for 4x1x1 up to 4x2x2.
256          */
257         for (d = ddbox->nboundeddim; d < DIM; d++)
258         {
259             b0 = av[d] - GRID_STDDEV_FAC*stddev[d];
260             b1 = av[d] + GRID_STDDEV_FAC*stddev[d];
261             if (debug)
262             {
263                 fprintf(debug, "Setting global DD grid boundaries to %f - %f\n",
264                         b0, b1);
265             }
266             ddbox->box0[d]     = b0;
267             ddbox->box_size[d] = b1 - b0;
268         }
269     }
270
271     set_tric_dir(dd_nc, ddbox, box);
272 }
273
274 void set_ddbox(gmx_domdec_t *dd, bool masterRankHasTheSystemState,
275                const t_inputrec *ir, const matrix box,
276                bool calculateUnboundedSize,
277                gmx::ArrayRef<const gmx::RVec> x,
278                gmx_ddbox_t *ddbox)
279 {
280     if (!masterRankHasTheSystemState || DDMASTER(dd))
281     {
282         bool needToReduceCoordinateData = (!masterRankHasTheSystemState && dd->nnodes > 1);
283
284         low_set_ddbox(ir, &dd->nc, box, calculateUnboundedSize,
285                       masterRankHasTheSystemState ? x.size() : dd->comm->atomRanges.numHomeAtoms(), as_rvec_array(x.data()),
286                       needToReduceCoordinateData ? &dd->mpi_comm_all : nullptr,
287                       ddbox);
288     }
289
290     if (masterRankHasTheSystemState)
291     {
292         dd_bcast(dd, sizeof(gmx_ddbox_t), ddbox);
293     }
294 }
295
296 void set_ddbox_cr(const t_commrec *cr, const ivec *dd_nc,
297                   const t_inputrec *ir, const matrix box,
298                   gmx::ArrayRef<const gmx::RVec> x,
299                   gmx_ddbox_t *ddbox)
300 {
301     if (MASTER(cr))
302     {
303         low_set_ddbox(ir, dd_nc, box, true, x.size(), as_rvec_array(x.data()), nullptr, ddbox);
304     }
305
306     gmx_bcast(sizeof(gmx_ddbox_t), ddbox, cr);
307 }