2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020, 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.
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.
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.
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.
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.
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.
38 * Tests for the surface area calculation used by the `sasa` analysis module.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_trajectoryanalysis
45 #include "gromacs/trajectoryanalysis/modules/surfacearea.h"
49 #include <gtest/gtest.h>
51 #include "gromacs/math/utilities.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/pbcutil/pbc.h"
54 #include "gromacs/random/threefry.h"
55 #include "gromacs/random/uniformrealdistribution.h"
56 #include "gromacs/utility/arrayref.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/smalloc.h"
60 #include "testutils/refdata.h"
61 #include "testutils/testasserts.h"
66 /********************************************************************
70 class SurfaceAreaTest : public ::testing::Test
83 ~SurfaceAreaTest() override
89 void addSphere(real x, real y, real z, real radius, bool bAddToIndex = true)
93 index_.push_back(x_.size());
95 x_.emplace_back(x, y, z);
96 radius_.push_back(radius);
99 void generateRandomPosition(rvec x, real* radius)
102 gmx::UniformRealDistribution<real> dist;
108 *radius = 1.5 * dist(rng_) + 0.5;
111 void generateRandomPositions(int count)
114 radius_.reserve(count);
115 index_.reserve(count);
116 for (int i = 0; i < count; ++i)
120 generateRandomPosition(x, &radius);
121 addSphere(x[XX], x[YY], x[ZZ], radius);
124 void translatePoints(real x, real y, real z)
126 for (size_t i = 0; i < x_.size(); ++i)
134 void calculate(int ndots, int flags, bool bPBC)
145 set_pbc(&pbc, PbcType::Xyz, box_);
147 gmx::SurfaceAreaCalculator calculator;
148 calculator.setDotCount(ndots);
149 calculator.setRadii(radius_);
150 calculator.calculate(as_rvec_array(x_.data()), bPBC ? &pbc : nullptr, index_.size(),
151 index_.data(), flags, &area_, &volume_, &atomArea_, &dots_, &dotCount_);
153 real resultArea() const { return area_; }
154 real resultVolume() const { return volume_; }
155 real atomArea(int index) const { return atomArea_[index]; }
157 void checkReference(gmx::test::TestReferenceChecker* checker, const char* id, bool checkDotCoordinates)
159 gmx::test::TestReferenceChecker compound(checker->checkCompound("SASA", id));
160 compound.checkReal(area_, "Area");
163 compound.checkReal(volume_, "Volume");
165 if (atomArea_ != nullptr)
167 compound.checkSequenceArray(index_.size(), atomArea_, "AtomArea");
169 if (dots_ != nullptr)
171 if (checkDotCoordinates)
173 // The algorithm may produce the dots in different order in
174 // single and double precision due to some internal
176 std::qsort(dots_, dotCount_, sizeof(rvec), &dotComparer);
177 compound.checkSequenceArray(3 * dotCount_, dots_, "Dots");
181 compound.checkInteger(dotCount_, "DotCount");
186 gmx::test::TestReferenceData data_;
190 static int dotComparer(const void* a, const void* b)
192 for (int d = DIM - 1; d >= 0; --d)
194 const real ad = reinterpret_cast<const real*>(a)[d];
195 const real bd = reinterpret_cast<const real*>(b)[d];
196 // A fudge factor is needed to get an ordering that is the same
197 // in single and double precision, since the points are not
198 // exactly on the same Z plane even though in exact arithmetic
199 // they probably would be.
204 else if (ad > bd + 0.001)
212 gmx::DefaultRandomEngine rng_;
213 std::vector<gmx::RVec> x_;
214 std::vector<real> radius_;
215 std::vector<int> index_;
224 TEST_F(SurfaceAreaTest, ComputesSinglePoint)
226 gmx::test::FloatingPointTolerance tolerance(gmx::test::defaultRealTolerance());
227 addSphere(1, 1, 1, 1);
228 ASSERT_NO_FATAL_FAILURE(calculate(24, FLAG_VOLUME | FLAG_ATOM_AREA, false));
229 EXPECT_REAL_EQ_TOL(4 * M_PI, resultArea(), tolerance);
230 EXPECT_REAL_EQ_TOL(4 * M_PI, atomArea(0), tolerance);
231 EXPECT_REAL_EQ_TOL(4 * M_PI / 3, resultVolume(), tolerance);
234 TEST_F(SurfaceAreaTest, ComputesTwoPoints)
236 gmx::test::FloatingPointTolerance tolerance(gmx::test::relativeToleranceAsFloatingPoint(1.0, 0.005));
237 addSphere(1, 1, 1, 1);
238 addSphere(2, 1, 1, 1);
239 ASSERT_NO_FATAL_FAILURE(calculate(1000, FLAG_ATOM_AREA, false));
240 EXPECT_REAL_EQ_TOL(2 * 2 * M_PI * 1.5, resultArea(), tolerance);
241 EXPECT_REAL_EQ_TOL(2 * M_PI * 1.5, atomArea(0), tolerance);
242 EXPECT_REAL_EQ_TOL(2 * M_PI * 1.5, atomArea(1), tolerance);
245 TEST_F(SurfaceAreaTest, ComputesTwoPointsOfUnequalRadius)
247 gmx::test::FloatingPointTolerance tolerance(gmx::test::relativeToleranceAsFloatingPoint(1.0, 0.005));
248 // Spheres of radius 1 and 2 with intersection at 1.5
249 const real dist = 0.5 + sqrt(3.25);
250 addSphere(1.0, 1.0, 1.0, 1);
251 addSphere(1.0 + dist, 1.0, 1.0, 2);
252 ASSERT_NO_FATAL_FAILURE(calculate(1000, FLAG_ATOM_AREA, false));
253 EXPECT_REAL_EQ_TOL(2 * M_PI * (1.5 + (dist - 0.5 + 2) * 2), resultArea(), tolerance);
254 EXPECT_REAL_EQ_TOL(2 * M_PI * 1.5, atomArea(0), tolerance);
255 EXPECT_REAL_EQ_TOL(2 * M_PI * (dist - 0.5 + 2) * 2, atomArea(1), tolerance);
258 TEST_F(SurfaceAreaTest, SurfacePoints12)
260 gmx::test::TestReferenceChecker checker(data_.rootChecker());
261 addSphere(0, 0, 0, 1);
262 ASSERT_NO_FATAL_FAILURE(calculate(12, FLAG_DOTS, false));
263 checkReference(&checker, "Surface", true);
266 TEST_F(SurfaceAreaTest, SurfacePoints32)
268 gmx::test::TestReferenceChecker checker(data_.rootChecker());
269 addSphere(0, 0, 0, 1);
270 ASSERT_NO_FATAL_FAILURE(calculate(32, FLAG_DOTS, false));
271 checkReference(&checker, "Surface", true);
274 TEST_F(SurfaceAreaTest, SurfacePoints42)
276 gmx::test::TestReferenceChecker checker(data_.rootChecker());
277 addSphere(0, 0, 0, 1);
278 ASSERT_NO_FATAL_FAILURE(calculate(42, FLAG_DOTS, false));
279 checkReference(&checker, "Surface", true);
282 TEST_F(SurfaceAreaTest, SurfacePoints122)
284 gmx::test::TestReferenceChecker checker(data_.rootChecker());
285 addSphere(0, 0, 0, 1);
286 ASSERT_NO_FATAL_FAILURE(calculate(122, FLAG_DOTS, false));
287 checkReference(&checker, "Surface", true);
290 TEST_F(SurfaceAreaTest, Computes100Points)
292 gmx::test::TestReferenceChecker checker(data_.rootChecker());
293 checker.setDefaultTolerance(gmx::test::absoluteTolerance(0.001));
297 generateRandomPositions(100);
298 ASSERT_NO_FATAL_FAILURE(calculate(24, FLAG_VOLUME | FLAG_ATOM_AREA | FLAG_DOTS, false));
299 checkReference(&checker, "100Points", false);
302 TEST_F(SurfaceAreaTest, Computes100PointsWithRectangularPBC)
304 // TODO: It would be nice to check that this produces the same result as
305 // without PBC, without duplicating the reference files.
306 gmx::test::TestReferenceChecker checker(data_.rootChecker());
307 checker.setDefaultTolerance(gmx::test::absoluteTolerance(0.001));
311 generateRandomPositions(100);
315 const int flags = FLAG_ATOM_AREA | FLAG_VOLUME | FLAG_DOTS;
316 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
317 checkReference(&checker, "100Points", false);
319 translatePoints(15.0, 0, 0);
320 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
321 checkReference(&checker, "100Points", false);
323 translatePoints(-15.0, 15.0, 0);
324 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
325 checkReference(&checker, "100Points", false);
327 translatePoints(0, -15.0, 15.0);
328 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
329 checkReference(&checker, "100Points", false);
332 TEST_F(SurfaceAreaTest, Computes100PointsWithTriclinicPBC)
334 // TODO: It would be nice to check that this produces the same result as
335 // without PBC, without duplicating the reference files.
336 gmx::test::TestReferenceChecker checker(data_.rootChecker());
337 checker.setDefaultTolerance(gmx::test::absoluteTolerance(0.001));
341 generateRandomPositions(100);
344 box_[YY][YY] = 10.0 * sqrt(3.0);
346 box_[ZZ][YY] = 10.0 * sqrt(1.0 / 3.0);
347 box_[ZZ][ZZ] = 20.0 * sqrt(2.0 / 3.0);
349 const int flags = FLAG_ATOM_AREA | FLAG_VOLUME | FLAG_DOTS;
350 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
351 checkReference(&checker, "100Points", false);
353 translatePoints(15.0, 0, 0);
354 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
355 checkReference(&checker, "100Points", false);
357 translatePoints(-15.0, box_[YY][YY] - 5.0, 0);
358 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
359 checkReference(&checker, "100Points", false);
361 translatePoints(0, -(box_[YY][YY] - 5.0), 15.0);
362 ASSERT_NO_FATAL_FAILURE(calculate(24, flags, true));
363 checkReference(&checker, "100Points", false);