2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
7 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
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.
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.
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.
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.
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.
38 /* This file is completely threadsafe - keep it that way! */
46 #include "gromacs/domdec/dlb.h"
47 #include "gromacs/domdec/domdec.h"
48 #include "gromacs/domdec/domdec_struct.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/utility/fatalerror.h"
52 /***********************************
54 ***********************************/
56 static void calc_x_av_stddev(int n, rvec* x, rvec av, rvec stddev)
64 for (i = 0; i < n; i++)
66 for (d = 0; d < DIM; d++)
69 s2[d] += x[i][d] * x[i][d];
73 dsvmul(1.0 / n, s1, s1);
74 dsvmul(1.0 / n, s2, s2);
76 for (d = 0; d < DIM; d++)
79 stddev[d] = std::sqrt(s2[d] - s1[d] * s1[d]);
83 static void get_nsgrid_boundaries_vac(real av, real stddev, real* bound0, real* bound1, real* bdens0, real* bdens1)
85 /* Set the grid to 2 times the standard deviation of
86 * the charge group centers in both directions.
87 * For a uniform bounded distribution the width is sqrt(3)*stddev,
88 * so all charge groups fall within the width.
89 * For a sphere stddev is r/sqrt(5): 99.2% falls within the width.
90 * For a Gaussian distribution 98% fall within the width.
92 *bound0 = av - NSGRID_STDDEV_FAC * stddev;
93 *bound1 = av + NSGRID_STDDEV_FAC * stddev;
95 *bdens0 = av - GRID_STDDEV_FAC * stddev;
96 *bdens1 = av + GRID_STDDEV_FAC * stddev;
99 static void dd_box_bounds_to_ns_bounds(real box0, real box_size, real* gr0, real* gr1)
103 /* Redetermine av and stddev from the DD box boundaries */
104 av = box0 + 0.5 * box_size;
105 stddev = 0.5 * box_size / GRID_STDDEV_FAC;
107 *gr0 = av - NSGRID_STDDEV_FAC * stddev;
108 *gr1 = av + NSGRID_STDDEV_FAC * stddev;
111 void get_nsgrid_boundaries(int nboundeddim,
124 real vol, bdens0, bdens1;
127 if (nboundeddim < DIM)
129 calc_x_av_stddev(ncg, cgcm, av, stddev);
133 for (d = 0; d < DIM; d++)
137 grid_x0[d] = (gr0 != nullptr ? (*gr0)[d] : 0);
138 grid_x1[d] = (gr1 != nullptr ? (*gr1)[d] : box[d][d]);
139 vol *= (grid_x1[d] - grid_x0[d]);
143 if (ddbox == nullptr)
145 get_nsgrid_boundaries_vac(av[d], stddev[d], &grid_x0[d], &grid_x1[d], &bdens0, &bdens1);
149 /* Temporary fix which uses global ddbox boundaries
150 * for unbounded dimensions.
151 * Should be replaced by local boundaries, which makes
152 * the ns grid smaller and does not require global comm.
154 dd_box_bounds_to_ns_bounds(ddbox->box0[d], ddbox->box_size[d], &grid_x0[d], &grid_x1[d]);
158 /* Check for a DD cell not at a lower edge */
159 if (dd != nullptr && gr0 != nullptr && dd->ci[d] > 0)
161 grid_x0[d] = (*gr0)[d];
164 /* Check for a DD cell not at a higher edge */
165 if (dd != nullptr && gr1 != nullptr && dd->ci[d] < dd->numCells[d] - 1)
167 grid_x1[d] = (*gr1)[d];
170 vol *= (bdens1 - bdens0);
175 fprintf(debug, "Set grid boundaries dim %d: %f %f\n", d, grid_x0[d], grid_x1[d]);
179 *grid_density = ncg / vol;