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, 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/utilities.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/pbcutil/pbc.h"
54 #include "gromacs/selection/nbsearch.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/gmxassert.h"
57 #include "gromacs/utility/smalloc.h"
61 #define UNSP_ICO_DOD 9
62 #define UNSP_ICO_ARC 10
64 #define FOURPI (4. * M_PI)
65 #define TORAD(A) ((A)*0.017453293)
68 static real safe_asin(real f)
74 GMX_ASSERT(fabs(f) - 1.0 > DP_TOL, "Invalid argument");
78 /* routines for dot distributions on the surface of the unit sphere */
79 static real icosaeder_vertices(real* xus)
81 const real rh = std::sqrt(1. - 2. * cos(TORAD(72.))) / (1. - cos(TORAD(72.)));
82 const real rg = cos(TORAD(72.)) / (1. - cos(TORAD(72.)));
83 /* icosaeder vertices */
87 xus[3] = rh * cos(TORAD(72.));
88 xus[4] = rh * sin(TORAD(72.));
90 xus[6] = rh * cos(TORAD(144.));
91 xus[7] = rh * sin(TORAD(144.));
93 xus[9] = rh * cos(TORAD(216.));
94 xus[10] = rh * sin(TORAD(216.));
96 xus[12] = rh * cos(TORAD(288.));
97 xus[13] = rh * sin(TORAD(288.));
102 xus[18] = rh * cos(TORAD(36.));
103 xus[19] = rh * sin(TORAD(36.));
105 xus[21] = rh * cos(TORAD(108.));
106 xus[22] = rh * sin(TORAD(108.));
111 xus[27] = rh * cos(TORAD(252.));
112 xus[28] = rh * sin(TORAD(252.));
114 xus[30] = rh * cos(TORAD(324.));
115 xus[31] = rh * sin(TORAD(324.));
125 divarc(real x1, real y1, real z1, real x2, real y2, real z2, int div1, int div2, real* xr, real* yr, real* zr)
128 real xd, yd, zd, dd, d1, d2, s, x, y, z;
129 real phi, sphi, cphi;
131 xd = y1 * z2 - y2 * z1;
132 yd = z1 * x2 - z2 * x1;
133 zd = x1 * y2 - x2 * y1;
134 dd = std::sqrt(xd * xd + yd * yd + zd * zd);
135 GMX_ASSERT(dd >= DP_TOL, "Rotation axis vector too short");
137 d1 = x1 * x1 + y1 * y1 + z1 * z1;
138 d2 = x2 * x2 + y2 * y2 + z2 * z2;
139 GMX_ASSERT(d1 >= 0.5, "Vector 1 too short");
140 GMX_ASSERT(d2 >= 0.5, "Vector 2 too short");
142 phi = safe_asin(dd / std::sqrt(d1 * d2));
143 phi = phi * (static_cast<real>(div1)) / (static_cast<real>(div2));
146 s = (x1 * xd + y1 * yd + z1 * zd) / dd;
148 x = xd * s * (1. - cphi) / dd + x1 * cphi + (yd * z1 - y1 * zd) * sphi / dd;
149 y = yd * s * (1. - cphi) / dd + y1 * cphi + (zd * x1 - z1 * xd) * sphi / dd;
150 z = zd * s * (1. - cphi) / dd + z1 * cphi + (xd * y1 - x1 * yd) * sphi / dd;
151 dd = std::sqrt(x * x + y * y + z * z);
157 /* densit...required dots per unit sphere */
158 static std::vector<real> ico_dot_arc(int densit)
160 /* dot distribution on a unit sphere based on an icosaeder *
161 * great circle average refining of icosahedral face */
163 int i, j, k, tl, tl2, tn;
164 real a, d, x, y, z, x2, y2, z2, x3, y3, z3;
165 real xij, yij, zij, xji, yji, zji, xik, yik, zik, xki, yki, zki, xjk, yjk, zjk, xkj, ykj, zkj;
167 /* calculate tessalation level */
168 a = std::sqrt(((static_cast<real>(densit)) - 2.) / 10.);
169 const int tess = static_cast<int>(ceil(a));
170 const int ndot = 10 * tess * tess + 2;
171 GMX_RELEASE_ASSERT(ndot >= densit, "Inconsistent surface dot formula");
173 std::vector<real> xus(3 * ndot);
174 const real rh = icosaeder_vertices(xus.data());
179 a = rh * rh * 2. * (1. - cos(TORAD(72.)));
180 /* calculate tessalation of icosaeder edges */
181 for (i = 0; i < 11; i++)
183 for (j = i + 1; j < 12; j++)
185 x = xus[3 * i] - xus[3 * j];
186 y = xus[1 + 3 * i] - xus[1 + 3 * j];
187 z = xus[2 + 3 * i] - xus[2 + 3 * j];
188 d = x * x + y * y + z * z;
189 if (std::fabs(a - d) > DP_TOL)
193 for (tl = 1; tl < tess; tl++)
195 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
211 /* calculate tessalation of icosaeder faces */
212 for (i = 0; i < 10; i++)
214 for (j = i + 1; j < 11; j++)
216 x = xus[3 * i] - xus[3 * j];
217 y = xus[1 + 3 * i] - xus[1 + 3 * j];
218 z = xus[2 + 3 * i] - xus[2 + 3 * j];
219 d = x * x + y * y + z * z;
220 if (std::fabs(a - d) > DP_TOL)
225 for (k = j + 1; k < 12; k++)
227 x = xus[3 * i] - xus[3 * k];
228 y = xus[1 + 3 * i] - xus[1 + 3 * k];
229 z = xus[2 + 3 * i] - xus[2 + 3 * k];
230 d = x * x + y * y + z * z;
231 if (std::fabs(a - d) > DP_TOL)
235 x = xus[3 * j] - xus[3 * k];
236 y = xus[1 + 3 * j] - xus[1 + 3 * k];
237 z = xus[2 + 3 * j] - xus[2 + 3 * k];
238 d = x * x + y * y + z * z;
239 if (std::fabs(a - d) > DP_TOL)
243 for (tl = 1; tl < tess - 1; tl++)
268 for (tl2 = 1; tl2 < tess - tl; tl2++)
314 divarc(xki, yki, zki, xji, yji, zji, tl2, tess - tl, &x, &y, &z);
315 divarc(xkj, ykj, zkj, xij, yij, zij, tl, tess - tl2, &x2, &y2, &z2);
316 divarc(xjk, yjk, zjk, xik, yik, zik, tl, tl + tl2, &x3, &y3, &z3);
317 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
321 d = std::sqrt(x * x + y * y + z * z);
323 xus[1 + 3 * tn] = y / d;
324 xus[2 + 3 * tn] = z / d;
331 GMX_ASSERT(tn == ndot, "Inconsistent precomputed surface dot count");
332 } /* end of if (tess > 1) */
335 } /* end of routine ico_dot_arc */
337 /* densit...required dots per unit sphere */
338 static std::vector<real> ico_dot_dod(int densit)
340 /* dot distribution on a unit sphere based on an icosaeder *
341 * great circle average refining of icosahedral face */
343 int i, j, k, tl, tl2, tn, tess, j1, j2;
344 real a, d, x, y, z, x2, y2, z2, x3, y3, z3, ai_d, adod;
345 real xij, yij, zij, xji, yji, zji, xik, yik, zik, xki, yki, zki, xjk, yjk, zjk, xkj, ykj, zkj;
347 /* calculate tesselation level */
348 a = std::sqrt(((static_cast<real>(densit)) - 2.) / 30.);
349 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 (i = 0; i < 10; i++)
362 for (j = i + 1; j < 11; j++)
364 x = xus[3 * i] - xus[3 * j];
365 y = xus[1 + 3 * i] - xus[1 + 3 * j];
366 z = xus[2 + 3 * i] - xus[2 + 3 * j];
367 d = x * x + y * y + z * z;
368 if (std::fabs(a - d) > DP_TOL)
372 for (k = j + 1; k < 12; k++)
374 x = xus[3 * i] - xus[3 * k];
375 y = xus[1 + 3 * i] - xus[1 + 3 * k];
376 z = xus[2 + 3 * i] - xus[2 + 3 * k];
377 d = x * x + y * y + z * z;
378 if (std::fabs(a - d) > DP_TOL)
382 x = xus[3 * j] - xus[3 * k];
383 y = xus[1 + 3 * j] - xus[1 + 3 * k];
384 z = xus[2 + 3 * j] - xus[2 + 3 * k];
385 d = x * x + y * y + z * z;
386 if (std::fabs(a - d) > DP_TOL)
390 x = xus[3 * i] + xus[3 * j] + xus[3 * k];
391 y = xus[1 + 3 * i] + xus[1 + 3 * j] + xus[1 + 3 * k];
392 z = xus[2 + 3 * i] + xus[2 + 3 * j] + xus[2 + 3 * k];
393 d = std::sqrt(x * x + y * y + z * z);
395 xus[1 + 3 * tn] = y / d;
396 xus[2 + 3 * tn] = z / d;
405 /* square of the edge of an dodecaeder */
406 adod = 4. * (cos(TORAD(108.)) - cos(TORAD(120.))) / (1. - cos(TORAD(120.)));
407 /* square of the distance of two adjacent vertices of ico- and dodecaeder */
408 ai_d = 2. * (1. - std::sqrt(1. - a / 3.));
410 /* calculate tessalation of mixed edges */
411 for (i = 0; i < 31; i++)
421 for (j = j1; j < j2; j++)
423 x = xus[3 * i] - xus[3 * j];
424 y = xus[1 + 3 * i] - xus[1 + 3 * j];
425 z = xus[2 + 3 * i] - xus[2 + 3 * j];
426 d = x * x + y * y + z * z;
427 if (std::fabs(a - d) > DP_TOL)
431 for (tl = 1; tl < tess; tl++)
433 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
449 /* calculate tessalation of pentakisdodecahedron faces */
450 for (i = 0; i < 12; i++)
452 for (j = 12; j < 31; j++)
454 x = xus[3 * i] - xus[3 * j];
455 y = xus[1 + 3 * i] - xus[1 + 3 * j];
456 z = xus[2 + 3 * i] - xus[2 + 3 * j];
457 d = x * x + y * y + z * z;
458 if (std::fabs(ai_d - d) > DP_TOL)
463 for (k = j + 1; k < 32; k++)
465 x = xus[3 * i] - xus[3 * k];
466 y = xus[1 + 3 * i] - xus[1 + 3 * k];
467 z = xus[2 + 3 * i] - xus[2 + 3 * k];
468 d = x * x + y * y + z * z;
469 if (std::fabs(ai_d - d) > DP_TOL)
473 x = xus[3 * j] - xus[3 * k];
474 y = xus[1 + 3 * j] - xus[1 + 3 * k];
475 z = xus[2 + 3 * j] - xus[2 + 3 * k];
476 d = x * x + y * y + z * z;
477 if (std::fabs(adod - d) > DP_TOL)
481 for (tl = 1; tl < tess - 1; tl++)
506 for (tl2 = 1; tl2 < tess - tl; tl2++)
552 divarc(xki, yki, zki, xji, yji, zji, tl2, tess - tl, &x, &y, &z);
553 divarc(xkj, ykj, zkj, xij, yij, zij, tl, tess - tl2, &x2, &y2, &z2);
554 divarc(xjk, yjk, zjk, xik, yik, zik, tl, tl + tl2, &x3, &y3, &z3);
555 GMX_ASSERT(tn < ndot, "Inconsistent precomputed surface dot count");
559 d = std::sqrt(x * x + y * y + z * z);
561 xus[1 + 3 * tn] = y / d;
562 xus[2 + 3 * tn] = z / d;
569 GMX_ASSERT(tn == ndot, "Inconsistent precomputed surface dot count");
570 } /* end of if (tess > 1) */
573 } /* end of routine ico_dot_dod */
575 static int unsp_type(int densit)
579 while (10 * i1 * i1 + 2 < densit)
584 while (30 * i2 * i2 + 2 < densit)
588 if (10 * i1 * i1 - 2 < 30 * i2 * i2 - 2)
598 static std::vector<real> make_unsp(int densit, int cubus)
600 int *ico_wk, *ico_pt;
601 int ico_cube, ico_cube_cb, i, j, k, l, ijk, tn, tl, tl2;
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 ico_cube_cb = ico_cube * ico_cube * ico_cube;
637 const real del_cube = 2. / (static_cast<real>(ico_cube));
639 for (l = 0; l < ndot; l++)
641 i = std::max(static_cast<int>(floor((1. + xus[3 * l]) / del_cube)), 0);
646 j = std::max(static_cast<int>(floor((1. + xus[1 + 3 * l]) / del_cube)), 0);
651 k = std::max(static_cast<int>(floor((1. + xus[2 + 3 * l]) / del_cube)), 0);
656 ijk = i + j * ico_cube + k * ico_cube * ico_cube;
660 snew(ico_wk, 2 * ico_cube_cb + 1);
662 ico_pt = ico_wk + ico_cube_cb;
663 for (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 (i = 0; i < ico_cube; i++)
672 for (j = 0; j < ico_cube; j++)
674 for (k = 0; k < ico_cube; k++)
678 ijk = i + ico_cube * j + ico_cube * ico_cube * k;
679 *(ico_pt + ijk) = tn;
680 for (l = tl2; l < ndot; 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 + ijk) = tl;
710 static void nsc_dclm_pbc(const rvec* coords,
711 const ArrayRef<const real>& radius,
722 AnalysisNeighborhood* nb,
725 const real dotarea = FOURPI / static_cast<real>(n_dot);
729 fprintf(debug, "nsc_dclm: n_dot=%5d %9.3f\n", n_dot, dotarea);
732 /* start with neighbour list */
733 /* calculate neighbour list with the box algorithm */
738 real area = 0.0, vol = 0.0;
739 real *dots = nullptr, *atom_area = nullptr;
740 int lfnr = 0, maxdots = 0;
741 if (mode & FLAG_VOLUME)
745 if (mode & FLAG_DOTS)
747 maxdots = (3 * n_dot * nat) / 10;
751 if (mode & FLAG_ATOM_AREA)
753 snew(atom_area, nat);
756 // Compute the center of the molecule for volume calculation.
757 // In principle, the center should not influence the results, but that is
758 // only true at the limit of infinite dot density, so this makes the
759 // results translation-invariant.
760 // With PBC, if the molecule is broken across the boundary, the computation
761 // is broken in other ways as well, so it does not need to be considered
763 real xs = 0.0, ys = 0.0, zs = 0.0;
764 for (int i = 0; i < nat; ++i)
766 const int iat = index[i];
767 xs += coords[iat][XX];
768 ys += coords[iat][YY];
769 zs += coords[iat][ZZ];
775 AnalysisNeighborhoodPositions pos(coords, radius.size());
776 pos.indexed(constArrayRefFromArray(index, nat));
777 AnalysisNeighborhoodSearch nbsearch(nb->initSearch(pbc, pos));
779 std::vector<int> wkdot(n_dot);
781 for (int i = 0; i < nat; ++i)
783 const int iat = index[i];
784 const real ai = radius[iat];
785 const real aisq = ai * ai;
786 AnalysisNeighborhoodPairSearch pairSearch(nbsearch.startPairSearch(coords[iat]));
787 AnalysisNeighborhoodPair pair;
788 std::fill(wkdot.begin(), wkdot.end(), 1);
789 int currDotCount = n_dot;
790 while (currDotCount > 0 && pairSearch.findNextPair(&pair))
792 const int jat = index[pair.refIndex()];
793 const real aj = radius[jat];
794 const real d2 = pair.distance2();
795 if (iat == jat || d2 > gmx::square(ai + aj))
799 const rvec& dx = pair.dx();
800 const real refdot = (d2 + aisq - aj * aj) / (2 * ai);
801 // TODO: Consider whether micro-optimizations from the old
802 // implementation would be useful, compared to the complexity that
803 // they bring: instead of this direct loop, the neighbors were
804 // stored into a temporary array, the loop order was
805 // reversed (first over dots, then over neighbors), and for each
806 // dot, it was first checked whether the same neighbor that
807 // resulted in marking the previous dot covered would also cover
808 // this dot. This presumably plays together with sorting of the
809 // surface dots (done in make_unsp) to avoid some of the looping.
810 // Alternatively, we could keep a skip list here to avoid
811 // repeatedly looping over dots that have already marked as
813 for (int j = 0; j < n_dot; ++j)
815 if (wkdot[j] && iprod(&xus[3 * j], dx) > refdot)
823 const real a = aisq * dotarea * currDotCount;
825 if (mode & FLAG_ATOM_AREA)
829 const real xi = coords[iat][XX];
830 const real yi = coords[iat][YY];
831 const real zi = coords[iat][ZZ];
832 if (mode & FLAG_DOTS)
834 for (int l = 0; l < n_dot; l++)
839 if (maxdots <= 3 * lfnr + 1)
841 maxdots = maxdots + n_dot * 3;
842 srenew(dots, maxdots);
844 dots[3 * lfnr - 3] = ai * xus[3 * l] + xi;
845 dots[3 * lfnr - 2] = ai * xus[1 + 3 * l] + yi;
846 dots[3 * lfnr - 1] = ai * xus[2 + 3 * l] + zi;
850 if (mode & FLAG_VOLUME)
852 real dx = 0.0, dy = 0.0, dz = 0.0;
853 for (int l = 0; l < n_dot; l++)
857 dx = dx + xus[3 * l];
858 dy = dy + xus[1 + 3 * l];
859 dz = dz + xus[2 + 3 * l];
862 vol = vol + aisq * (dx * (xi - xs) + dy * (yi - ys) + dz * (zi - zs) + ai * currDotCount);
866 if (mode & FLAG_VOLUME)
868 *value_of_vol = vol * FOURPI / (3. * n_dot);
870 if (mode & FLAG_DOTS)
872 GMX_RELEASE_ASSERT(nu_dots != nullptr, "Must have valid nu_dots pointer");
874 GMX_RELEASE_ASSERT(lidots != nullptr, "Must have valid lidots pointer");
877 if (mode & FLAG_ATOM_AREA)
879 GMX_RELEASE_ASSERT(at_area != nullptr, "Must have valid at_area pointer");
880 *at_area = atom_area;
882 *value_of_area = area;
886 fprintf(debug, "area=%8.3f\n", area);
893 class SurfaceAreaCalculator::Impl
896 Impl() : flags_(0) {}
898 std::vector<real> unitSphereDots_;
899 ArrayRef<const real> radius_;
901 mutable AnalysisNeighborhood nb_;
904 SurfaceAreaCalculator::SurfaceAreaCalculator() : impl_(new Impl()) {}
906 SurfaceAreaCalculator::~SurfaceAreaCalculator() {}
908 void SurfaceAreaCalculator::setDotCount(int dotCount)
910 impl_->unitSphereDots_ = make_unsp(dotCount, 4);
913 void SurfaceAreaCalculator::setRadii(const ArrayRef<const real>& radius)
915 impl_->radius_ = radius;
918 const real maxRadius = *std::max_element(radius.begin(), radius.end());
919 impl_->nb_.setCutoff(2 * maxRadius);
923 void SurfaceAreaCalculator::setCalculateVolume(bool bVolume)
927 impl_->flags_ |= FLAG_VOLUME;
931 impl_->flags_ &= ~FLAG_VOLUME;
935 void SurfaceAreaCalculator::setCalculateAtomArea(bool bAtomArea)
939 impl_->flags_ |= FLAG_ATOM_AREA;
943 impl_->flags_ &= ~FLAG_ATOM_AREA;
947 void SurfaceAreaCalculator::setCalculateSurfaceDots(bool bDots)
951 impl_->flags_ |= FLAG_DOTS;
955 impl_->flags_ &= ~FLAG_DOTS;
959 void SurfaceAreaCalculator::calculate(const rvec* x,
970 flags |= impl_->flags_;
972 if (volume == nullptr)
974 flags &= ~FLAG_VOLUME;
980 if (at_area == nullptr)
982 flags &= ~FLAG_ATOM_AREA;
988 if (lidots == nullptr)
996 if (n_dots == nullptr)
1007 &impl_->unitSphereDots_[0],
1008 impl_->unitSphereDots_.size() / 3,