Fix random typos
[alexxy/gromacs.git] / src / gromacs / domdec / box.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2014,2015,2017 by the GROMACS development team.
5  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36
37 /*! \internal \file
38  *
39  * \brief This file defines functions used by the domdec module
40  * for (bounding) box and pbc information generation.
41  *
42  * \author Berk Hess <hess@kth.se>
43  * \ingroup module_domdec
44  */
45
46 #include "gmxpre.h"
47
48 #include "box.h"
49
50 #include "config.h"
51
52 #include "gromacs/domdec/domdec.h"
53 #include "gromacs/domdec/domdec_network.h"
54 #include "gromacs/domdec/domdec_struct.h"
55 #include "gromacs/domdec/nsgrid.h"
56 #include "gromacs/gmxlib/network.h"
57 #include "gromacs/math/functions.h"
58 #include "gromacs/math/vec.h"
59 #include "gromacs/mdtypes/commrec.h"
60 #include "gromacs/mdtypes/inputrec.h"
61 #include "gromacs/pbcutil/pbc.h"
62 #include "gromacs/topology/block.h"
63 #include "gromacs/utility/basedefinitions.h"
64 #include "gromacs/utility/fatalerror.h"
65
66 #include "domdec_internal.h"
67
68 /*! \brief Calculates the average and standard deviation in 3D of atoms */
69 static void calc_pos_av_stddev(gmx::ArrayRef<const gmx::RVec> x, rvec av, rvec stddev, const MPI_Comm* mpiCommunicator)
70 {
71     dvec s1, s2;
72
73     clear_dvec(s1);
74     clear_dvec(s2);
75
76     for (const gmx::RVec& coord : x)
77     {
78         for (int d = 0; d < DIM; d++)
79         {
80             s1[d] += coord[d];
81             s2[d] += coord[d] * coord[d];
82         }
83     }
84
85     /* With mpiCommunicator != nullptr, x.size() is the home atom count */
86     int numAtoms = x.size();
87 #if GMX_MPI
88     if (mpiCommunicator)
89     {
90         constexpr int c_bufSize = 7;
91         double        sendBuffer[c_bufSize];
92         double        receiveBuffer[c_bufSize];
93
94         for (int d = 0; d < DIM; d++)
95         {
96             sendBuffer[d]       = s1[d];
97             sendBuffer[DIM + d] = s2[d];
98         }
99         sendBuffer[6] = numAtoms;
100
101         MPI_Allreduce(sendBuffer, receiveBuffer, c_bufSize, MPI_DOUBLE, MPI_SUM, *mpiCommunicator);
102
103         for (int d = 0; d < DIM; d++)
104         {
105             s1[d] = receiveBuffer[d];
106             s2[d] = receiveBuffer[DIM + d];
107         }
108         numAtoms = gmx::roundToInt(receiveBuffer[6]);
109     }
110 #else  // GMX_MPI
111     GMX_UNUSED_VALUE(mpiCommunicator);
112 #endif // GMX_MPI
113
114     dsvmul(1.0 / numAtoms, s1, s1);
115     dsvmul(1.0 / numAtoms, s2, s2);
116
117     for (int d = 0; d < DIM; d++)
118     {
119         av[d]     = s1[d];
120         stddev[d] = std::sqrt(s2[d] - s1[d] * s1[d]);
121     }
122 }
123
124 /*! \brief Determines if dimensions require triclinic treatment and stores this info in ddbox */
125 static void set_tric_dir(const ivec* dd_nc, gmx_ddbox_t* ddbox, const matrix box)
126 {
127     int   npbcdim = ddbox->npbcdim;
128     rvec* normal  = ddbox->normal;
129     for (int d = 0; d < DIM; d++)
130     {
131         ddbox->tric_dir[d] = 0;
132         for (int j = d + 1; j < npbcdim; j++)
133         {
134             if (box[j][d] != 0)
135             {
136                 ddbox->tric_dir[d] = 1;
137                 if (dd_nc != nullptr && (*dd_nc)[j] > 1 && (*dd_nc)[d] == 1)
138                 {
139                     gmx_fatal(FARGS,
140                               "Domain decomposition has not been implemented for box vectors that "
141                               "have non-zero components in directions that do not use domain "
142                               "decomposition: ncells = %d %d %d, box vector[%d] = %f %f %f",
143                               (*dd_nc)[XX],
144                               (*dd_nc)[YY],
145                               (*dd_nc)[ZZ],
146                               j + 1,
147                               box[j][XX],
148                               box[j][YY],
149                               box[j][ZZ]);
150                 }
151             }
152         }
153
154         /* Construct vectors v for dimension d that are perpendicular
155          * to the triclinic plane of dimension d. Each vector v[i] has
156          * v[i][i]=1 and v[i][d]!=0 for triclinic dimensions, while the third
157          * component is zero. These are used for computing the distance
158          * to a triclinic plane given the distance along dimension d.
159          * Set the trilinic skewing factor that translates
160          * the thickness of a slab perpendicular to this dimension
161          * into the real thickness of the slab.
162          */
163         if (ddbox->tric_dir[d])
164         {
165             real  inv_skew_fac2 = 1;
166             rvec* v             = ddbox->v[d];
167             if (d == XX || d == YY)
168             {
169                 /* Normalize such that the "diagonal" is 1 */
170                 svmul(1 / box[d + 1][d + 1], box[d + 1], v[d + 1]);
171                 for (int i = 0; i < d; i++)
172                 {
173                     v[d + 1][i] = 0;
174                 }
175                 inv_skew_fac2 += gmx::square(v[d + 1][d]);
176                 if (d == XX)
177                 {
178                     /* Normalize such that the "diagonal" is 1 */
179                     svmul(1 / box[d + 2][d + 2], box[d + 2], v[d + 2]);
180                     /* Set v[d+2][d+1] to zero by shifting along v[d+1] */
181                     const real dep = v[d + 2][d + 1] / v[d + 1][d + 1];
182                     for (int i = 0; i < DIM; i++)
183                     {
184                         v[d + 2][i] -= dep * v[d + 1][i];
185                     }
186                     inv_skew_fac2 += gmx::square(v[d + 2][d]);
187
188                     cprod(v[d + 1], v[d + 2], normal[d]);
189                 }
190                 else
191                 {
192                     /* cross product with (1,0,0) */
193                     normal[d][XX] = 0;
194                     normal[d][YY] = v[d + 1][ZZ];
195                     normal[d][ZZ] = -v[d + 1][YY];
196                 }
197                 if (debug)
198                 {
199                     fprintf(debug, "box[%d]  %.3f %.3f %.3f\n", d, box[d][XX], box[d][YY], box[d][ZZ]);
200                     for (int i = d + 1; i < DIM; i++)
201                     {
202                         fprintf(debug, "  v[%d]  %.3f %.3f %.3f\n", i, v[i][XX], v[i][YY], v[i][ZZ]);
203                     }
204                 }
205             }
206             ddbox->skew_fac[d] = 1.0 / std::sqrt(inv_skew_fac2);
207             /* Set the normal vector length to skew_fac */
208             const real dep = ddbox->skew_fac[d] / norm(normal[d]);
209             svmul(dep, normal[d], normal[d]);
210
211             if (debug)
212             {
213                 fprintf(debug, "skew_fac[%d] = %f\n", d, ddbox->skew_fac[d]);
214                 fprintf(debug, "normal[%d]  %.3f %.3f %.3f\n", d, normal[d][XX], normal[d][YY], normal[d][ZZ]);
215             }
216         }
217         else
218         {
219             ddbox->skew_fac[d] = 1;
220
221             for (int i = 0; i < DIM; i++)
222             {
223                 clear_rvec(ddbox->v[d][i]);
224                 ddbox->v[d][i][i] = 1;
225             }
226             clear_rvec(normal[d]);
227             normal[d][d] = 1;
228         }
229     }
230 }
231
232 /*! \brief This function calculates bounding box and pbc info and populates ddbox */
233 static void low_set_ddbox(int                            numPbcDimensions,
234                           int                            numBoundedDimensions,
235                           const ivec*                    dd_nc,
236                           const matrix                   box,
237                           bool                           calculateUnboundedSize,
238                           gmx::ArrayRef<const gmx::RVec> x,
239                           const MPI_Comm*                mpiCommunicator,
240                           gmx_ddbox_t*                   ddbox)
241 {
242     rvec av, stddev;
243
244     ddbox->npbcdim     = numPbcDimensions;
245     ddbox->nboundeddim = numBoundedDimensions;
246
247     for (int d = 0; d < numBoundedDimensions; d++)
248     {
249         ddbox->box0[d]     = 0;
250         ddbox->box_size[d] = box[d][d];
251     }
252
253     if (ddbox->nboundeddim < DIM && calculateUnboundedSize)
254     {
255         calc_pos_av_stddev(x, av, stddev, mpiCommunicator);
256
257         /* c_gridStdDevFactor * stddev
258          * gives a uniform load for a rectangular block of cg's.
259          * For a sphere it is not a bad approximation for 4x1x1 up to 4x2x2.
260          */
261         for (int d = ddbox->nboundeddim; d < DIM; d++)
262         {
263             const real b0 = av[d] - c_gridStdDevFactor * stddev[d];
264             const real b1 = av[d] + c_gridStdDevFactor * stddev[d];
265             if (debug)
266             {
267                 fprintf(debug, "Setting global DD grid boundaries to %f - %f\n", b0, b1);
268             }
269             ddbox->box0[d]     = b0;
270             ddbox->box_size[d] = b1 - b0;
271         }
272     }
273
274     set_tric_dir(dd_nc, ddbox, box);
275 }
276
277 void set_ddbox(const gmx_domdec_t&            dd,
278                bool                           masterRankHasTheSystemState,
279                const matrix                   box,
280                bool                           calculateUnboundedSize,
281                gmx::ArrayRef<const gmx::RVec> x,
282                gmx_ddbox_t*                   ddbox)
283 {
284     if (!masterRankHasTheSystemState || DDMASTER(dd))
285     {
286         bool needToReduceCoordinateData     = (!masterRankHasTheSystemState && dd.nnodes > 1);
287         gmx::ArrayRef<const gmx::RVec> xRef = constArrayRefFromArray(
288                 x.data(), masterRankHasTheSystemState ? x.size() : dd.comm->atomRanges.numHomeAtoms());
289
290         low_set_ddbox(dd.unitCellInfo.npbcdim,
291                       dd.unitCellInfo.numBoundedDimensions,
292                       &dd.numCells,
293                       box,
294                       calculateUnboundedSize,
295                       xRef,
296                       needToReduceCoordinateData ? &dd.mpi_comm_all : nullptr,
297                       ddbox);
298     }
299
300     if (masterRankHasTheSystemState)
301     {
302         dd_bcast(&dd, sizeof(gmx_ddbox_t), ddbox);
303     }
304 }
305
306 void set_ddbox_cr(DDRole                         ddRole,
307                   MPI_Comm                       communicator,
308                   const ivec*                    dd_nc,
309                   const t_inputrec&              ir,
310                   const matrix                   box,
311                   gmx::ArrayRef<const gmx::RVec> x,
312                   gmx_ddbox_t*                   ddbox)
313 {
314     if (ddRole == DDRole::Master)
315     {
316         low_set_ddbox(
317                 numPbcDimensions(ir.pbcType), inputrec2nboundeddim(&ir), dd_nc, box, true, x, nullptr, ddbox);
318     }
319
320     gmx_bcast(sizeof(gmx_ddbox_t), ddbox, communicator);
321 }