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-2007, 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.
40 #include "surfacearea.h"
50 #include "gromacs/math/functions.h"
51 #include "gromacs/math/units.h"
52 #include "gromacs/math/utilities.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/pbcutil/pbc.h"
55 #include "gromacs/selection/nbsearch.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/smalloc.h"
62 #define UNSP_ICO_DOD 9
63 #define UNSP_ICO_ARC 10
65 #define FOURPI (4. * M_PI)
66 #define TORAD(A) ((A)*0.017453293)
69 static real safe_asin(real f)
75 GMX_ASSERT(fabs(f) - 1.0 > DP_TOL, "Invalid argument");
79 /* routines for dot distributions on the surface of the unit sphere */
80 static real icosaeder_vertices(real* xus)
82 const real rh = std::sqrt(1. - 2. * cos(TORAD(72.))) / (1. - cos(TORAD(72.)));
83 const real rg = cos(TORAD(72.)) / (1. - cos(TORAD(72.)));
84 /* icosaeder vertices */
88 xus[3] = rh * cos(TORAD(72.));
89 xus[4] = rh * sin(TORAD(72.));
91 xus[6] = rh * cos(TORAD(144.));
92 xus[7] = rh * sin(TORAD(144.));
94 xus[9] = rh * cos(TORAD(216.));
95 xus[10] = rh * sin(TORAD(216.));
97 xus[12] = rh * cos(TORAD(288.));
98 xus[13] = rh * sin(TORAD(288.));
103 xus[18] = rh * cos(TORAD(36.));
104 xus[19] = rh * sin(TORAD(36.));
106 xus[21] = rh * cos(TORAD(108.));
107 xus[22] = rh * sin(TORAD(108.));
112 xus[27] = rh * cos(TORAD(252.));
113 xus[28] = rh * sin(TORAD(252.));
115 xus[30] = rh * cos(TORAD(324.));
116 xus[31] = rh * sin(TORAD(324.));
126 divarc(real x1, real y1, real z1, real x2, real y2, real z2, int div1, int div2, real* xr, real* yr, real* zr)
129 const real xd = y1 * z2 - y2 * z1;
130 const real yd = z1 * x2 - z2 * x1;
131 const real zd = x1 * y2 - x2 * y1;
132 const real dd = std::sqrt(xd * xd + yd * yd + zd * zd);
133 GMX_ASSERT(dd >= DP_TOL, "Rotation axis vector too short");
135 const real d1 = x1 * x1 + y1 * y1 + z1 * z1;
136 const real d2 = x2 * x2 + y2 * y2 + z2 * z2;
137 GMX_ASSERT(d1 >= 0.5, "Vector 1 too short");
138 GMX_ASSERT(d2 >= 0.5, "Vector 2 too short");
140 real phi = safe_asin(dd / std::sqrt(d1 * d2));
141 phi = phi * (static_cast<real>(div1)) / (static_cast<real>(div2));
142 const real sphi = sin(phi);
143 const real cphi = cos(phi);
144 const real s = (x1 * xd + y1 * yd + z1 * zd) / dd;
146 const real x = xd * s * (1. - cphi) / dd + x1 * cphi + (yd * z1 - y1 * zd) * sphi / dd;
147 const real y = yd * s * (1. - cphi) / dd + y1 * cphi + (zd * x1 - z1 * xd) * sphi / dd;
148 const real z = zd * s * (1. - cphi) / dd + z1 * cphi + (xd * y1 - x1 * yd) * sphi / dd;
149 const real dd2 = std::sqrt(x * x + y * y + z * z);
155 /* densit...required dots per unit sphere */
156 static std::vector<real> ico_dot_arc(int densit)
158 /* dot distribution on a unit sphere based on an icosaeder *
159 * great circle average refining of icosahedral face */
161 real x2 = NAN, y2 = NAN, z2 = NAN, x3 = NAN, y3 = NAN, z3 = NAN;
162 real xij = NAN, yij = NAN, zij = NAN, xji = NAN, yji = NAN, zji = NAN, xik = NAN, yik = NAN,
163 zik = NAN, xki = NAN, yki = NAN, zki = NAN, xjk = NAN, yjk = NAN, zjk = NAN, xkj = NAN,
164 ykj = NAN, zkj = NAN;
166 /* calculate tessalation level */
167 const real a = std::sqrt(((static_cast<real>(densit)) - 2.) / 10.);
168 const int tess = static_cast<int>(ceil(a));
169 const int ndot = 10 * tess * tess + 2;
170 GMX_RELEASE_ASSERT(ndot >= densit, "Inconsistent surface dot formula");
172 std::vector<real> xus(3 * ndot);
173 const real rh = icosaeder_vertices(xus.data());
178 const real a = rh * rh * 2. * (1. - cos(TORAD(72.)));
179 /* calculate tessalation of icosaeder edges */
180 for (int i = 0; i < 11; i++)
182 for (int j = i + 1; j < 12; j++)
184 const real x = xus[3 * i] - xus[3 * j];
185 const real y = xus[1 + 3 * i] - xus[1 + 3 * j];
186 const real z = xus[2 + 3 * i] - xus[2 + 3 * j];
187 const real d = x * x + y * y + z * z;
188 if (std::fabs(a - d) > DP_TOL)
192 for (int tl = 1; tl < tess; tl++)
194 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
210 /* calculate tessalation of icosaeder faces */
211 for (int i = 0; i < 10; i++)
213 for (int j = i + 1; j < 11; j++)
215 real x = xus[3 * i] - xus[3 * j];
216 real y = xus[1 + 3 * i] - xus[1 + 3 * j];
217 real z = xus[2 + 3 * i] - xus[2 + 3 * j];
218 real d = x * x + y * y + z * z;
219 if (std::fabs(a - d) > DP_TOL)
224 for (int k = j + 1; k < 12; k++)
226 x = xus[3 * i] - xus[3 * k];
227 y = xus[1 + 3 * i] - xus[1 + 3 * k];
228 z = xus[2 + 3 * i] - xus[2 + 3 * k];
229 d = x * x + y * y + z * z;
230 if (std::fabs(a - d) > DP_TOL)
234 x = xus[3 * j] - xus[3 * k];
235 y = xus[1 + 3 * j] - xus[1 + 3 * k];
236 z = xus[2 + 3 * j] - xus[2 + 3 * k];
237 d = x * x + y * y + z * z;
238 if (std::fabs(a - d) > DP_TOL)
242 for (int tl = 1; tl < tess - 1; tl++)
267 for (int tl2 = 1; tl2 < tess - tl; tl2++)
313 divarc(xki, yki, zki, xji, yji, zji, tl2, tess - tl, &x, &y, &z);
314 divarc(xkj, ykj, zkj, xij, yij, zij, tl, tess - tl2, &x2, &y2, &z2);
315 divarc(xjk, yjk, zjk, xik, yik, zik, tl, tl + tl2, &x3, &y3, &z3);
316 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
320 d = std::sqrt(x * x + y * y + z * z);
322 xus[1 + 3 * tn] = y / d;
323 xus[2 + 3 * tn] = z / d;
330 GMX_ASSERT(tn == ndot, "Inconsistent precomputed surface dot count");
331 } /* end of if (tess > 1) */
334 } /* end of routine ico_dot_arc */
336 /* densit...required dots per unit sphere */
337 static std::vector<real> ico_dot_dod(int densit)
339 /* dot distribution on a unit sphere based on an icosaeder *
340 * great circle average refining of icosahedral face */
342 real x2 = NAN, y2 = NAN, z2 = NAN, x3 = NAN, y3 = NAN, z3 = NAN;
343 real xij = NAN, yij = NAN, zij = NAN, xji = NAN, yji = NAN, zji = NAN, xik = NAN, yik = NAN,
344 zik = NAN, xki = NAN, yki = NAN, zki = NAN, xjk = NAN, yjk = NAN, zjk = NAN, xkj = NAN,
345 ykj = NAN, zkj = NAN;
347 /* calculate tesselation level */
348 real a = std::sqrt(((static_cast<real>(densit)) - 2.) / 30.);
349 const int tess = std::max(static_cast<int>(ceil(a)), 1);
350 const int ndot = 30 * tess * tess + 2;
351 GMX_RELEASE_ASSERT(ndot >= densit, "Inconsistent surface dot formula");
353 std::vector<real> xus(3 * ndot);
354 const real rh = icosaeder_vertices(xus.data());
357 /* square of the edge of an icosaeder */
358 a = rh * rh * 2. * (1. - cos(TORAD(72.)));
359 /* dodecaeder vertices */
360 for (int i = 0; i < 10; i++)
362 for (int j = i + 1; j < 11; j++)
364 const real x = xus[3 * i] - xus[3 * j];
365 const real y = xus[1 + 3 * i] - xus[1 + 3 * j];
366 const real z = xus[2 + 3 * i] - xus[2 + 3 * j];
367 const real d = x * x + y * y + z * z;
368 if (std::fabs(a - d) > DP_TOL)
372 for (int k = j + 1; k < 12; k++)
375 const real x = xus[3 * i] - xus[3 * k];
376 const real y = xus[1 + 3 * i] - xus[1 + 3 * k];
377 const real z = xus[2 + 3 * i] - xus[2 + 3 * k];
378 const real d = x * x + y * y + z * z;
379 if (std::fabs(a - d) > DP_TOL)
385 const real x = xus[3 * j] - xus[3 * k];
386 const real y = xus[1 + 3 * j] - xus[1 + 3 * k];
387 const real z = xus[2 + 3 * j] - xus[2 + 3 * k];
388 const real d = x * x + y * y + z * z;
389 if (std::fabs(a - d) > DP_TOL)
394 const real x = xus[3 * i] + xus[3 * j] + xus[3 * k];
395 const real y = xus[1 + 3 * i] + xus[1 + 3 * j] + xus[1 + 3 * k];
396 const real z = xus[2 + 3 * i] + xus[2 + 3 * j] + xus[2 + 3 * k];
397 const real d = std::sqrt(x * x + y * y + z * z);
399 xus[1 + 3 * tn] = y / d;
400 xus[2 + 3 * tn] = z / d;
409 /* square of the edge of an dodecaeder */
410 const real adod = 4. * (cos(TORAD(108.)) - cos(TORAD(120.))) / (1. - cos(TORAD(120.)));
411 /* square of the distance of two adjacent vertices of ico- and dodecaeder */
412 const real ai_d = 2. * (1. - std::sqrt(1. - a / 3.));
414 /* calculate tessalation of mixed edges */
415 for (int i = 0; i < 31; i++)
425 for (int j = j1; j < j2; j++)
427 const real x = xus[3 * i] - xus[3 * j];
428 const real y = xus[1 + 3 * i] - xus[1 + 3 * j];
429 const real z = xus[2 + 3 * i] - xus[2 + 3 * j];
430 const real d = x * x + y * y + z * z;
431 if (std::fabs(a - d) > DP_TOL)
435 for (int tl = 1; tl < tess; tl++)
437 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
453 /* calculate tessalation of pentakisdodecahedron faces */
454 for (int i = 0; i < 12; i++)
456 for (int j = 12; j < 31; j++)
458 const real x = xus[3 * i] - xus[3 * j];
459 const real y = xus[1 + 3 * i] - xus[1 + 3 * j];
460 const real z = xus[2 + 3 * i] - xus[2 + 3 * j];
461 const real d = x * x + y * y + z * z;
462 if (std::fabs(ai_d - d) > DP_TOL)
467 for (int k = j + 1; k < 32; k++)
469 real x = xus[3 * i] - xus[3 * k];
470 real y = xus[1 + 3 * i] - xus[1 + 3 * k];
471 real z = xus[2 + 3 * i] - xus[2 + 3 * k];
472 real d = x * x + y * y + z * z;
473 if (std::fabs(ai_d - d) > DP_TOL)
477 x = xus[3 * j] - xus[3 * k];
478 y = xus[1 + 3 * j] - xus[1 + 3 * k];
479 z = xus[2 + 3 * j] - xus[2 + 3 * k];
480 d = x * x + y * y + z * z;
481 if (std::fabs(adod - d) > DP_TOL)
485 for (int tl = 1; tl < tess - 1; tl++)
510 for (int tl2 = 1; tl2 < tess - tl; tl2++)
556 divarc(xki, yki, zki, xji, yji, zji, tl2, tess - tl, &x, &y, &z);
557 divarc(xkj, ykj, zkj, xij, yij, zij, tl, tess - tl2, &x2, &y2, &z2);
558 divarc(xjk, yjk, zjk, xik, yik, zik, tl, tl + tl2, &x3, &y3, &z3);
559 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
563 d = std::sqrt(x * x + y * y + z * z);
565 xus[1 + 3 * tn] = y / d;
566 xus[2 + 3 * tn] = z / d;
573 GMX_ASSERT(tn == ndot, "Inconsistent precomputed surface dot count");
574 } /* end of if (tess > 1) */
577 } /* end of routine ico_dot_dod */
579 static int unsp_type(int densit)
582 while (10 * i1 * i1 + 2 < densit)
587 while (30 * i2 * i2 + 2 < densit)
591 if (10 * i1 * i1 - 2 < 30 * i2 * i2 - 2)
601 static std::vector<real> make_unsp(int densit, int cubus)
605 int mode = unsp_type(densit);
606 std::vector<real> xus;
607 if (mode == UNSP_ICO_ARC)
609 xus = ico_dot_arc(densit);
611 else if (mode == UNSP_ICO_DOD)
613 xus = ico_dot_dod(densit);
617 GMX_RELEASE_ASSERT(false, "Invalid unit sphere mode");
620 const int ndot = ssize(xus) / 3;
622 /* determine distribution of points in elementary cubes */
630 while (i * i * i * 2 < ndot)
634 ico_cube = std::max(i - 1, 0);
636 int ico_cube_cb = ico_cube * ico_cube * ico_cube;
637 const real del_cube = 2. / (static_cast<real>(ico_cube));
638 std::vector<int> work;
639 for (int l = 0; l < ndot; l++)
641 int i = std::max(static_cast<int>(floor((1. + xus[3 * l]) / del_cube)), 0);
646 int j = std::max(static_cast<int>(floor((1. + xus[1 + 3 * l]) / del_cube)), 0);
651 int k = std::max(static_cast<int>(floor((1. + xus[2 + 3 * l]) / del_cube)), 0);
656 int ijk = i + j * ico_cube + k * ico_cube * ico_cube;
657 work.emplace_back(ijk);
660 std::vector<int> ico_wk(2 * ico_cube_cb + 1);
662 int* ico_pt = ico_wk.data() + ico_cube_cb;
663 for (int l = 0; l < ndot; l++)
665 ico_wk[work[l]]++; /* dots per elementary cube */
668 /* reordering of the coordinate array in accordance with box number */
670 for (int i = 0; i < ico_cube; i++)
672 for (int j = 0; j < ico_cube; j++)
674 for (int k = 0; k < ico_cube; k++)
678 int ijk = i + ico_cube * j + ico_cube * ico_cube * k;
679 *(ico_pt + ijk) = tn;
680 for (int l = tl2; l < ndot; l++)
684 const real x = xus[3 * l];
685 const real y = xus[1 + 3 * l];
686 const real z = xus[2 + 3 * l];
687 xus[3 * l] = xus[3 * tn];
688 xus[1 + 3 * l] = xus[1 + 3 * tn];
689 xus[2 + 3 * l] = xus[2 + 3 * tn];
700 *(ico_wk.data() + ijk) = tl;
708 static void nsc_dclm_pbc(const rvec* coords,
709 const ArrayRef<const real>& radius,
720 AnalysisNeighborhood* nb,
723 const real dotarea = FOURPI / static_cast<real>(n_dot);
727 fprintf(debug, "nsc_dclm: n_dot=%5d %9.3f\n", n_dot, dotarea);
730 /* start with neighbour list */
731 /* calculate neighbour list with the box algorithm */
736 real area = 0.0, vol = 0.0;
737 real *dots = nullptr, *atom_area = nullptr;
738 int lfnr = 0, maxdots = 0;
739 if (mode & FLAG_VOLUME)
743 if (mode & FLAG_DOTS)
745 maxdots = (3 * n_dot * nat) / 10;
749 if (mode & FLAG_ATOM_AREA)
751 snew(atom_area, nat);
754 // Compute the center of the molecule for volume calculation.
755 // In principle, the center should not influence the results, but that is
756 // only true at the limit of infinite dot density, so this makes the
757 // results translation-invariant.
758 // With PBC, if the molecule is broken across the boundary, the computation
759 // is broken in other ways as well, so it does not need to be considered
761 real xs = 0.0, ys = 0.0, zs = 0.0;
762 for (int i = 0; i < nat; ++i)
764 const int iat = index[i];
765 xs += coords[iat][XX];
766 ys += coords[iat][YY];
767 zs += coords[iat][ZZ];
773 AnalysisNeighborhoodPositions pos(coords, radius.size());
774 pos.indexed(constArrayRefFromArray(index, nat));
775 AnalysisNeighborhoodSearch nbsearch(nb->initSearch(pbc, pos));
777 std::vector<int> wkdot(n_dot);
779 for (int i = 0; i < nat; ++i)
781 const int iat = index[i];
782 const real ai = radius[iat];
783 const real aisq = ai * ai;
784 AnalysisNeighborhoodPairSearch pairSearch(nbsearch.startPairSearch(coords[iat]));
785 AnalysisNeighborhoodPair pair;
786 std::fill(wkdot.begin(), wkdot.end(), 1);
787 int currDotCount = n_dot;
788 while (currDotCount > 0 && pairSearch.findNextPair(&pair))
790 const int jat = index[pair.refIndex()];
791 const real aj = radius[jat];
792 const real d2 = pair.distance2();
793 if (iat == jat || d2 > gmx::square(ai + aj))
797 const rvec& dx = pair.dx();
798 const real refdot = (d2 + aisq - aj * aj) / (2 * ai);
799 // TODO: Consider whether micro-optimizations from the old
800 // implementation would be useful, compared to the complexity that
801 // they bring: instead of this direct loop, the neighbors were
802 // stored into a temporary array, the loop order was
803 // reversed (first over dots, then over neighbors), and for each
804 // dot, it was first checked whether the same neighbor that
805 // resulted in marking the previous dot covered would also cover
806 // this dot. This presumably plays together with sorting of the
807 // surface dots (done in make_unsp) to avoid some of the looping.
808 // Alternatively, we could keep a skip list here to avoid
809 // repeatedly looping over dots that have already marked as
811 for (int j = 0; j < n_dot; ++j)
813 if (wkdot[j] && iprod(&xus[3 * j], dx) > refdot)
821 const real a = aisq * dotarea * currDotCount;
823 if (mode & FLAG_ATOM_AREA)
827 const real xi = coords[iat][XX];
828 const real yi = coords[iat][YY];
829 const real zi = coords[iat][ZZ];
830 if (mode & FLAG_DOTS)
832 for (int l = 0; l < n_dot; l++)
837 if (maxdots <= 3 * lfnr + 1)
839 maxdots = maxdots + n_dot * 3;
840 srenew(dots, maxdots);
842 dots[3 * lfnr - 3] = ai * xus[3 * l] + xi;
843 dots[3 * lfnr - 2] = ai * xus[1 + 3 * l] + yi;
844 dots[3 * lfnr - 1] = ai * xus[2 + 3 * l] + zi;
848 if (mode & FLAG_VOLUME)
850 real dx = 0.0, dy = 0.0, dz = 0.0;
851 for (int l = 0; l < n_dot; l++)
855 dx = dx + xus[3 * l];
856 dy = dy + xus[1 + 3 * l];
857 dz = dz + xus[2 + 3 * l];
860 vol = vol + aisq * (dx * (xi - xs) + dy * (yi - ys) + dz * (zi - zs) + ai * currDotCount);
864 if (mode & FLAG_VOLUME)
866 *value_of_vol = vol * FOURPI / (3. * n_dot);
868 if (mode & FLAG_DOTS)
870 GMX_RELEASE_ASSERT(nu_dots != nullptr, "Must have valid nu_dots pointer");
872 GMX_RELEASE_ASSERT(lidots != nullptr, "Must have valid lidots pointer");
875 if (mode & FLAG_ATOM_AREA)
877 GMX_RELEASE_ASSERT(at_area != nullptr, "Must have valid at_area pointer");
878 *at_area = atom_area;
880 *value_of_area = area;
884 fprintf(debug, "area=%8.3f\n", area);
891 class SurfaceAreaCalculator::Impl
894 Impl() : flags_(0) {}
896 std::vector<real> unitSphereDots_;
897 ArrayRef<const real> radius_;
899 mutable AnalysisNeighborhood nb_;
902 SurfaceAreaCalculator::SurfaceAreaCalculator() : impl_(new Impl()) {}
904 SurfaceAreaCalculator::~SurfaceAreaCalculator() {}
906 void SurfaceAreaCalculator::setDotCount(int dotCount)
908 impl_->unitSphereDots_ = make_unsp(dotCount, 4);
911 void SurfaceAreaCalculator::setRadii(const ArrayRef<const real>& radius)
913 impl_->radius_ = radius;
916 const real maxRadius = *std::max_element(radius.begin(), radius.end());
917 impl_->nb_.setCutoff(2 * maxRadius);
921 void SurfaceAreaCalculator::setCalculateVolume(bool bVolume)
925 impl_->flags_ |= FLAG_VOLUME;
929 impl_->flags_ &= ~FLAG_VOLUME;
933 void SurfaceAreaCalculator::setCalculateAtomArea(bool bAtomArea)
937 impl_->flags_ |= FLAG_ATOM_AREA;
941 impl_->flags_ &= ~FLAG_ATOM_AREA;
945 void SurfaceAreaCalculator::setCalculateSurfaceDots(bool bDots)
949 impl_->flags_ |= FLAG_DOTS;
953 impl_->flags_ &= ~FLAG_DOTS;
957 void SurfaceAreaCalculator::calculate(const rvec* x,
968 flags |= impl_->flags_;
970 if (volume == nullptr)
972 flags &= ~FLAG_VOLUME;
978 if (at_area == nullptr)
980 flags &= ~FLAG_ATOM_AREA;
986 if (lidots == nullptr)
994 if (n_dots == nullptr)
1005 &impl_->unitSphereDots_[0],
1006 impl_->unitSphereDots_.size() / 3,