2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Tests selection neighborhood searching.
40 * Increase coverage of these tests for different corner cases: other PBC cases
41 * than full 3D, large cutoffs (larger than half the box size), etc.
42 * At least some of these probably don't work correctly.
44 * \author Teemu Murtola <teemu.murtola@gmail.com>
45 * \ingroup module_selection
49 #include "gromacs/selection/nbsearch.h"
59 #include <gtest/gtest.h>
61 #include "gromacs/math/functions.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/pbcutil/pbc.h"
64 #include "gromacs/random/threefry.h"
65 #include "gromacs/random/uniformrealdistribution.h"
66 #include "gromacs/topology/block.h"
67 #include "gromacs/utility/smalloc.h"
68 #include "gromacs/utility/stringutil.h"
70 #include "testutils/testasserts.h"
76 /********************************************************************
77 * NeighborhoodSearchTestData
80 class NeighborhoodSearchTestData
85 RefPair(int refIndex, real distance)
86 : refIndex(refIndex), distance(distance), bFound(false),
87 bExcluded(false), bIndexed(true)
91 bool operator<(const RefPair &other) const
93 return refIndex < other.refIndex;
98 // The variables below are state variables that are only used
99 // during the actual testing after creating a copy of the reference
100 // pair list, not as part of the reference data.
101 // Simpler to have just a single structure for both purposes.
109 explicit TestPosition(const rvec x)
110 : refMinDist(0.0), refNearestPoint(-1)
112 copy_rvec(x, this->x);
118 std::vector<RefPair> refPairs;
121 typedef std::vector<TestPosition> TestPositionList;
123 NeighborhoodSearchTestData(gmx_uint64_t seed, real cutoff);
125 gmx::AnalysisNeighborhoodPositions refPositions() const
127 return gmx::AnalysisNeighborhoodPositions(refPos_);
129 gmx::AnalysisNeighborhoodPositions testPositions() const
131 if (testPos_.empty())
133 testPos_.reserve(testPositions_.size());
134 for (size_t i = 0; i < testPositions_.size(); ++i)
136 testPos_.emplace_back(testPositions_[i].x);
139 return gmx::AnalysisNeighborhoodPositions(testPos_);
141 gmx::AnalysisNeighborhoodPositions testPosition(int index) const
143 return testPositions().selectSingleFromArray(index);
146 void addTestPosition(const rvec x)
148 GMX_RELEASE_ASSERT(testPos_.empty(),
149 "Cannot add positions after testPositions() call");
150 testPositions_.emplace_back(x);
152 gmx::RVec generateRandomPosition();
153 std::vector<int> generateIndex(int count, gmx_uint64_t seed) const;
154 void generateRandomRefPositions(int count);
155 void generateRandomTestPositions(int count);
156 void useRefPositionsAsTestPositions();
157 void computeReferences(t_pbc *pbc)
159 computeReferencesInternal(pbc, false);
161 void computeReferencesXY(t_pbc *pbc)
163 computeReferencesInternal(pbc, true);
166 bool containsPair(int testIndex, const RefPair &pair) const
168 const std::vector<RefPair> &refPairs = testPositions_[testIndex].refPairs;
169 std::vector<RefPair>::const_iterator foundRefPair
170 = std::lower_bound(refPairs.begin(), refPairs.end(), pair);
171 if (foundRefPair == refPairs.end() || foundRefPair->refIndex != pair.refIndex)
178 // Return a tolerance that accounts for the magnitudes of the coordinates
179 // when doing subtractions, so that we set the ULP tolerance relative to the
180 // coordinate values rather than their difference.
181 // i.e., 10.0-9.9999999 will achieve a few ULP accuracy relative
182 // to 10.0, but a much larger error relative to the difference.
183 gmx::test::FloatingPointTolerance relativeTolerance() const
185 real magnitude = std::max(box_[XX][XX], std::max(box_[YY][YY], box_[ZZ][ZZ]));
186 return gmx::test::relativeToleranceAsUlp(magnitude, 4);
189 gmx::DefaultRandomEngine rng_;
194 std::vector<gmx::RVec> refPos_;
195 TestPositionList testPositions_;
198 void computeReferencesInternal(t_pbc *pbc, bool bXY);
200 mutable std::vector<gmx::RVec> testPos_;
203 //! Shorthand for a collection of reference pairs.
204 typedef std::vector<NeighborhoodSearchTestData::RefPair> RefPairList;
206 NeighborhoodSearchTestData::NeighborhoodSearchTestData(gmx_uint64_t seed, real cutoff)
207 : rng_(seed), cutoff_(cutoff), refPosCount_(0)
210 set_pbc(&pbc_, epbcNONE, box_);
213 gmx::RVec NeighborhoodSearchTestData::generateRandomPosition()
215 gmx::UniformRealDistribution<real> dist;
221 // Add a small displacement to allow positions outside the box
222 x[XX] += 0.2 * dist(rng_) - 0.1;
223 x[YY] += 0.2 * dist(rng_) - 0.1;
224 x[ZZ] += 0.2 * dist(rng_) - 0.1;
228 std::vector<int> NeighborhoodSearchTestData::generateIndex(int count, gmx_uint64_t seed) const
230 gmx::DefaultRandomEngine rngIndex(seed);
231 gmx::UniformRealDistribution<real> dist;
232 std::vector<int> result;
234 for (int i = 0; i < count; ++i)
236 if (dist(rngIndex) > 0.5)
244 void NeighborhoodSearchTestData::generateRandomRefPositions(int count)
246 refPosCount_ = count;
247 refPos_.reserve(count);
248 for (int i = 0; i < count; ++i)
250 refPos_.push_back(generateRandomPosition());
254 void NeighborhoodSearchTestData::generateRandomTestPositions(int count)
256 testPositions_.reserve(count);
257 for (int i = 0; i < count; ++i)
259 addTestPosition(generateRandomPosition());
263 void NeighborhoodSearchTestData::useRefPositionsAsTestPositions()
265 testPositions_.reserve(refPosCount_);
266 for (const auto &refPos : refPos_)
268 addTestPosition(refPos);
272 void NeighborhoodSearchTestData::computeReferencesInternal(t_pbc *pbc, bool bXY)
274 real cutoff = cutoff_;
277 cutoff = std::numeric_limits<real>::max();
279 for (TestPosition &testPos : testPositions_)
281 testPos.refMinDist = cutoff;
282 testPos.refNearestPoint = -1;
283 testPos.refPairs.clear();
284 for (int j = 0; j < refPosCount_; ++j)
289 pbc_dx(pbc, testPos.x, refPos_[j], dx);
293 rvec_sub(testPos.x, refPos_[j], dx);
295 // TODO: This may not work intuitively for 2D with the third box
296 // vector not parallel to the Z axis, but neither does the actual
297 // neighborhood search.
299 !bXY ? norm(dx) : std::hypot(dx[XX], dx[YY]);
300 if (dist < testPos.refMinDist)
302 testPos.refMinDist = dist;
303 testPos.refNearestPoint = j;
305 if (dist > 0 && dist <= cutoff)
307 RefPair pair(j, dist);
308 GMX_RELEASE_ASSERT(testPos.refPairs.empty() || testPos.refPairs.back() < pair,
309 "Reference pairs should be generated in sorted order");
310 testPos.refPairs.push_back(pair);
316 /********************************************************************
320 class ExclusionsHelper
323 static void markExcludedPairs(RefPairList *refPairs, int testIndex,
324 const t_blocka *excls);
326 ExclusionsHelper(int refPosCount, int testPosCount);
328 void generateExclusions();
330 const t_blocka *exclusions() const { return &excls_; }
332 gmx::ArrayRef<const int> refPosIds() const
334 return gmx::makeArrayRef(exclusionIds_).subArray(0, refPosCount_);
336 gmx::ArrayRef<const int> testPosIds() const
338 return gmx::makeArrayRef(exclusionIds_).subArray(0, testPosCount_);
344 std::vector<int> exclusionIds_;
345 std::vector<int> exclsIndex_;
346 std::vector<int> exclsAtoms_;
351 void ExclusionsHelper::markExcludedPairs(RefPairList *refPairs, int testIndex,
352 const t_blocka *excls)
355 for (int i = excls->index[testIndex]; i < excls->index[testIndex + 1]; ++i)
357 const int excludedIndex = excls->a[i];
358 NeighborhoodSearchTestData::RefPair searchPair(excludedIndex, 0.0);
359 RefPairList::iterator excludedRefPair
360 = std::lower_bound(refPairs->begin(), refPairs->end(), searchPair);
361 if (excludedRefPair != refPairs->end()
362 && excludedRefPair->refIndex == excludedIndex)
364 excludedRefPair->bFound = true;
365 excludedRefPair->bExcluded = true;
371 ExclusionsHelper::ExclusionsHelper(int refPosCount, int testPosCount)
372 : refPosCount_(refPosCount), testPosCount_(testPosCount)
374 // Generate an array of 0, 1, 2, ...
375 // TODO: Make the tests work also with non-trivial exclusion IDs,
377 exclusionIds_.resize(std::max(refPosCount, testPosCount), 1);
378 exclusionIds_[0] = 0;
379 std::partial_sum(exclusionIds_.begin(), exclusionIds_.end(),
380 exclusionIds_.begin());
383 excls_.index = nullptr;
386 excls_.nalloc_index = 0;
390 void ExclusionsHelper::generateExclusions()
392 // TODO: Consider a better set of test data, where the density of the
393 // particles would be higher, or where the exclusions would not be random,
394 // to make a higher percentage of the exclusions to actually be within the
396 exclsIndex_.reserve(testPosCount_ + 1);
397 exclsAtoms_.reserve(testPosCount_ * 20);
398 exclsIndex_.push_back(0);
399 for (int i = 0; i < testPosCount_; ++i)
401 for (int j = 0; j < 20; ++j)
403 exclsAtoms_.push_back(i + j*3);
405 exclsIndex_.push_back(exclsAtoms_.size());
407 excls_.nr = exclsIndex_.size();
408 excls_.index = exclsIndex_.data();
409 excls_.nra = exclsAtoms_.size();
410 excls_.a = exclsAtoms_.data();
413 /********************************************************************
414 * NeighborhoodSearchTest
417 class NeighborhoodSearchTest : public ::testing::Test
420 void testIsWithin(gmx::AnalysisNeighborhoodSearch *search,
421 const NeighborhoodSearchTestData &data);
422 void testMinimumDistance(gmx::AnalysisNeighborhoodSearch *search,
423 const NeighborhoodSearchTestData &data);
424 void testNearestPoint(gmx::AnalysisNeighborhoodSearch *search,
425 const NeighborhoodSearchTestData &data);
426 void testPairSearch(gmx::AnalysisNeighborhoodSearch *search,
427 const NeighborhoodSearchTestData &data);
428 void testPairSearchIndexed(gmx::AnalysisNeighborhood *nb,
429 const NeighborhoodSearchTestData &data,
431 void testPairSearchFull(gmx::AnalysisNeighborhoodSearch *search,
432 const NeighborhoodSearchTestData &data,
433 const gmx::AnalysisNeighborhoodPositions &pos,
434 const t_blocka *excls,
435 const gmx::ArrayRef<const int> &refIndices,
436 const gmx::ArrayRef<const int> &testIndices,
439 gmx::AnalysisNeighborhood nb_;
442 void NeighborhoodSearchTest::testIsWithin(
443 gmx::AnalysisNeighborhoodSearch *search,
444 const NeighborhoodSearchTestData &data)
446 NeighborhoodSearchTestData::TestPositionList::const_iterator i;
447 for (i = data.testPositions_.begin(); i != data.testPositions_.end(); ++i)
449 const bool bWithin = (i->refMinDist <= data.cutoff_);
450 EXPECT_EQ(bWithin, search->isWithin(i->x))
451 << "Distance is " << i->refMinDist;
455 void NeighborhoodSearchTest::testMinimumDistance(
456 gmx::AnalysisNeighborhoodSearch *search,
457 const NeighborhoodSearchTestData &data)
459 NeighborhoodSearchTestData::TestPositionList::const_iterator i;
461 for (i = data.testPositions_.begin(); i != data.testPositions_.end(); ++i)
463 const real refDist = i->refMinDist;
464 EXPECT_REAL_EQ_TOL(refDist, search->minimumDistance(i->x), data.relativeTolerance());
468 void NeighborhoodSearchTest::testNearestPoint(
469 gmx::AnalysisNeighborhoodSearch *search,
470 const NeighborhoodSearchTestData &data)
472 NeighborhoodSearchTestData::TestPositionList::const_iterator i;
473 for (i = data.testPositions_.begin(); i != data.testPositions_.end(); ++i)
475 const gmx::AnalysisNeighborhoodPair pair = search->nearestPoint(i->x);
478 EXPECT_EQ(i->refNearestPoint, pair.refIndex());
479 EXPECT_EQ(0, pair.testIndex());
480 EXPECT_REAL_EQ_TOL(i->refMinDist, std::sqrt(pair.distance2()), data.relativeTolerance());
484 EXPECT_EQ(i->refNearestPoint, -1);
489 //! Helper function for formatting test failure messages.
490 std::string formatVector(const rvec x)
492 return gmx::formatString("[%.3f, %.3f, %.3f]", x[XX], x[YY], x[ZZ]);
496 * Helper function to check that all expected pairs were found.
498 void checkAllPairsFound(const RefPairList &refPairs,
499 const std::vector<gmx::RVec> &refPos,
500 int testPosIndex, const rvec testPos)
502 // This could be elegantly expressed with Google Mock matchers, but that
503 // has a significant effect on the runtime of the tests...
505 RefPairList::const_iterator first;
506 for (RefPairList::const_iterator i = refPairs.begin(); i != refPairs.end(); ++i)
517 << "Some pairs (" << count << "/" << refPairs.size() << ") "
518 << "within the cutoff were not found. First pair:\n"
519 << " Ref: " << first->refIndex << " at "
520 << formatVector(refPos[first->refIndex]) << "\n"
521 << "Test: " << testPosIndex << " at " << formatVector(testPos) << "\n"
522 << "Dist: " << first->distance;
526 void NeighborhoodSearchTest::testPairSearch(
527 gmx::AnalysisNeighborhoodSearch *search,
528 const NeighborhoodSearchTestData &data)
530 testPairSearchFull(search, data, data.testPositions(), nullptr,
531 gmx::EmptyArrayRef(), gmx::EmptyArrayRef(), false);
534 void NeighborhoodSearchTest::testPairSearchIndexed(
535 gmx::AnalysisNeighborhood *nb,
536 const NeighborhoodSearchTestData &data,
539 std::vector<int> refIndices(data.generateIndex(data.refPos_.size(), seed++));
540 std::vector<int> testIndices(data.generateIndex(data.testPositions_.size(), seed++));
541 gmx::AnalysisNeighborhoodSearch search =
542 nb->initSearch(&data.pbc_,
543 data.refPositions().indexed(refIndices));
544 testPairSearchFull(&search, data, data.testPositions(), nullptr,
545 refIndices, testIndices, false);
548 void NeighborhoodSearchTest::testPairSearchFull(
549 gmx::AnalysisNeighborhoodSearch *search,
550 const NeighborhoodSearchTestData &data,
551 const gmx::AnalysisNeighborhoodPositions &pos,
552 const t_blocka *excls,
553 const gmx::ArrayRef<const int> &refIndices,
554 const gmx::ArrayRef<const int> &testIndices,
557 std::map<int, RefPairList> refPairs;
558 // TODO: Some parts of this code do not work properly if pos does not
559 // initially contain all the test positions.
560 if (testIndices.empty())
562 for (size_t i = 0; i < data.testPositions_.size(); ++i)
564 refPairs[i] = data.testPositions_[i].refPairs;
569 for (int index : testIndices)
571 refPairs[index] = data.testPositions_[index].refPairs;
574 if (excls != nullptr)
576 GMX_RELEASE_ASSERT(!selfPairs, "Self-pairs testing not implemented with exclusions");
577 for (auto &entry : refPairs)
579 const int testIndex = entry.first;
580 ExclusionsHelper::markExcludedPairs(&entry.second, testIndex, excls);
583 if (!refIndices.empty())
585 GMX_RELEASE_ASSERT(!selfPairs, "Self-pairs testing not implemented with indexing");
586 for (auto &entry : refPairs)
588 for (auto &refPair : entry.second)
590 refPair.bIndexed = false;
592 for (int index : refIndices)
594 NeighborhoodSearchTestData::RefPair searchPair(index, 0.0);
595 auto refPair = std::lower_bound(entry.second.begin(), entry.second.end(), searchPair);
596 if (refPair != entry.second.end() && refPair->refIndex == index)
598 refPair->bIndexed = true;
601 for (auto &refPair : entry.second)
603 if (!refPair.bIndexed)
605 refPair.bFound = true;
611 gmx::AnalysisNeighborhoodPositions posCopy(pos);
612 if (!testIndices.empty())
614 posCopy.indexed(testIndices);
616 gmx::AnalysisNeighborhoodPairSearch pairSearch
618 ? search->startSelfPairSearch()
619 : search->startPairSearch(posCopy);
620 gmx::AnalysisNeighborhoodPair pair;
621 while (pairSearch.findNextPair(&pair))
623 const int testIndex =
624 (testIndices.empty() ? pair.testIndex() : testIndices[pair.testIndex()]);
626 (refIndices.empty() ? pair.refIndex() : refIndices[pair.refIndex()]);
628 if (refPairs.count(testIndex) == 0)
631 << "Expected: No pairs are returned for test position " << testIndex << ".\n"
632 << " Actual: Pair with ref " << refIndex << " is returned.";
635 NeighborhoodSearchTestData::RefPair searchPair(refIndex,
636 std::sqrt(pair.distance2()));
637 const auto foundRefPair
638 = std::lower_bound(refPairs[testIndex].begin(), refPairs[testIndex].end(),
640 if (foundRefPair == refPairs[testIndex].end() || foundRefPair->refIndex != refIndex)
643 << "Expected: Pair (ref: " << refIndex << ", test: " << testIndex
644 << ") is not within the cutoff.\n"
645 << " Actual: It is returned.";
647 else if (foundRefPair->bExcluded)
650 << "Expected: Pair (ref: " << refIndex << ", test: " << testIndex
651 << ") is excluded from the search.\n"
652 << " Actual: It is returned.";
654 else if (!foundRefPair->bIndexed)
657 << "Expected: Pair (ref: " << refIndex << ", test: " << testIndex
658 << ") is not part of the indexed set.\n"
659 << " Actual: It is returned.";
661 else if (foundRefPair->bFound)
664 << "Expected: Pair (ref: " << refIndex << ", test: " << testIndex
665 << ") is returned only once.\n"
666 << " Actual: It is returned multiple times.";
671 foundRefPair->bFound = true;
673 EXPECT_REAL_EQ_TOL(foundRefPair->distance, searchPair.distance, data.relativeTolerance())
674 << "Distance computed by the neighborhood search does not match.";
677 searchPair = NeighborhoodSearchTestData::RefPair(testIndex, 0.0);
678 const auto otherRefPair
679 = std::lower_bound(refPairs[refIndex].begin(), refPairs[refIndex].end(),
681 GMX_RELEASE_ASSERT(otherRefPair != refPairs[refIndex].end(),
682 "Precomputed reference data is not symmetric");
683 otherRefPair->bFound = true;
688 for (auto &entry : refPairs)
690 const int testIndex = entry.first;
691 checkAllPairsFound(entry.second, data.refPos_, testIndex,
692 data.testPositions_[testIndex].x);
696 /********************************************************************
697 * Test data generation
700 class TrivialTestData
703 static const NeighborhoodSearchTestData &get()
705 static TrivialTestData singleton;
706 return singleton.data_;
709 TrivialTestData() : data_(12345, 1.0)
711 // Make the box so small we are virtually guaranteed to have
712 // several neighbors for the five test positions
713 data_.box_[XX][XX] = 3.0;
714 data_.box_[YY][YY] = 3.0;
715 data_.box_[ZZ][ZZ] = 3.0;
716 data_.generateRandomRefPositions(10);
717 data_.generateRandomTestPositions(5);
718 set_pbc(&data_.pbc_, epbcXYZ, data_.box_);
719 data_.computeReferences(&data_.pbc_);
723 NeighborhoodSearchTestData data_;
726 class TrivialSelfPairsTestData
729 static const NeighborhoodSearchTestData &get()
731 static TrivialSelfPairsTestData singleton;
732 return singleton.data_;
735 TrivialSelfPairsTestData() : data_(12345, 1.0)
737 data_.box_[XX][XX] = 3.0;
738 data_.box_[YY][YY] = 3.0;
739 data_.box_[ZZ][ZZ] = 3.0;
740 data_.generateRandomRefPositions(20);
741 data_.useRefPositionsAsTestPositions();
742 set_pbc(&data_.pbc_, epbcXYZ, data_.box_);
743 data_.computeReferences(&data_.pbc_);
747 NeighborhoodSearchTestData data_;
750 class TrivialNoPBCTestData
753 static const NeighborhoodSearchTestData &get()
755 static TrivialNoPBCTestData singleton;
756 return singleton.data_;
759 TrivialNoPBCTestData() : data_(12345, 1.0)
761 data_.generateRandomRefPositions(10);
762 data_.generateRandomTestPositions(5);
763 data_.computeReferences(nullptr);
767 NeighborhoodSearchTestData data_;
770 class RandomBoxFullPBCData
773 static const NeighborhoodSearchTestData &get()
775 static RandomBoxFullPBCData singleton;
776 return singleton.data_;
779 RandomBoxFullPBCData() : data_(12345, 1.0)
781 data_.box_[XX][XX] = 10.0;
782 data_.box_[YY][YY] = 5.0;
783 data_.box_[ZZ][ZZ] = 7.0;
784 // TODO: Consider whether manually picking some positions would give better
786 data_.generateRandomRefPositions(1000);
787 data_.generateRandomTestPositions(100);
788 set_pbc(&data_.pbc_, epbcXYZ, data_.box_);
789 data_.computeReferences(&data_.pbc_);
793 NeighborhoodSearchTestData data_;
796 class RandomBoxSelfPairsData
799 static const NeighborhoodSearchTestData &get()
801 static RandomBoxSelfPairsData singleton;
802 return singleton.data_;
805 RandomBoxSelfPairsData() : data_(12345, 1.0)
807 data_.box_[XX][XX] = 10.0;
808 data_.box_[YY][YY] = 5.0;
809 data_.box_[ZZ][ZZ] = 7.0;
810 data_.generateRandomRefPositions(1000);
811 data_.useRefPositionsAsTestPositions();
812 set_pbc(&data_.pbc_, epbcXYZ, data_.box_);
813 data_.computeReferences(&data_.pbc_);
817 NeighborhoodSearchTestData data_;
820 class RandomBoxXYFullPBCData
823 static const NeighborhoodSearchTestData &get()
825 static RandomBoxXYFullPBCData singleton;
826 return singleton.data_;
829 RandomBoxXYFullPBCData() : data_(54321, 1.0)
831 data_.box_[XX][XX] = 10.0;
832 data_.box_[YY][YY] = 5.0;
833 data_.box_[ZZ][ZZ] = 7.0;
834 // TODO: Consider whether manually picking some positions would give better
836 data_.generateRandomRefPositions(1000);
837 data_.generateRandomTestPositions(100);
838 set_pbc(&data_.pbc_, epbcXYZ, data_.box_);
839 data_.computeReferencesXY(&data_.pbc_);
843 NeighborhoodSearchTestData data_;
846 class RandomTriclinicFullPBCData
849 static const NeighborhoodSearchTestData &get()
851 static RandomTriclinicFullPBCData singleton;
852 return singleton.data_;
855 RandomTriclinicFullPBCData() : data_(12345, 1.0)
857 data_.box_[XX][XX] = 5.0;
858 data_.box_[YY][XX] = 2.5;
859 data_.box_[YY][YY] = 2.5*std::sqrt(3.0);
860 data_.box_[ZZ][XX] = 2.5;
861 data_.box_[ZZ][YY] = 2.5*std::sqrt(1.0/3.0);
862 data_.box_[ZZ][ZZ] = 5.0*std::sqrt(2.0/3.0);
863 // TODO: Consider whether manually picking some positions would give better
865 data_.generateRandomRefPositions(1000);
866 data_.generateRandomTestPositions(100);
867 set_pbc(&data_.pbc_, epbcXYZ, data_.box_);
868 data_.computeReferences(&data_.pbc_);
872 NeighborhoodSearchTestData data_;
875 class RandomBox2DPBCData
878 static const NeighborhoodSearchTestData &get()
880 static RandomBox2DPBCData singleton;
881 return singleton.data_;
884 RandomBox2DPBCData() : data_(12345, 1.0)
886 data_.box_[XX][XX] = 10.0;
887 data_.box_[YY][YY] = 7.0;
888 data_.box_[ZZ][ZZ] = 5.0;
889 // TODO: Consider whether manually picking some positions would give better
891 data_.generateRandomRefPositions(1000);
892 data_.generateRandomTestPositions(100);
893 set_pbc(&data_.pbc_, epbcXY, data_.box_);
894 data_.computeReferences(&data_.pbc_);
898 NeighborhoodSearchTestData data_;
901 class RandomBoxNoPBCData
904 static const NeighborhoodSearchTestData &get()
906 static RandomBoxNoPBCData singleton;
907 return singleton.data_;
910 RandomBoxNoPBCData() : data_(12345, 1.0)
912 data_.box_[XX][XX] = 10.0;
913 data_.box_[YY][YY] = 5.0;
914 data_.box_[ZZ][ZZ] = 7.0;
915 // TODO: Consider whether manually picking some positions would give better
917 data_.generateRandomRefPositions(1000);
918 data_.generateRandomTestPositions(100);
919 set_pbc(&data_.pbc_, epbcNONE, data_.box_);
920 data_.computeReferences(nullptr);
924 NeighborhoodSearchTestData data_;
927 /********************************************************************
931 TEST_F(NeighborhoodSearchTest, SimpleSearch)
933 const NeighborhoodSearchTestData &data = RandomBoxFullPBCData::get();
935 nb_.setCutoff(data.cutoff_);
936 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Simple);
937 gmx::AnalysisNeighborhoodSearch search =
938 nb_.initSearch(&data.pbc_, data.refPositions());
939 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Simple, search.mode());
941 testIsWithin(&search, data);
942 testMinimumDistance(&search, data);
943 testNearestPoint(&search, data);
944 testPairSearch(&search, data);
947 testPairSearchIndexed(&nb_, data, 123);
950 TEST_F(NeighborhoodSearchTest, SimpleSearchXY)
952 const NeighborhoodSearchTestData &data = RandomBoxXYFullPBCData::get();
954 nb_.setCutoff(data.cutoff_);
955 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Simple);
957 gmx::AnalysisNeighborhoodSearch search =
958 nb_.initSearch(&data.pbc_, data.refPositions());
959 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Simple, search.mode());
961 testIsWithin(&search, data);
962 testMinimumDistance(&search, data);
963 testNearestPoint(&search, data);
964 testPairSearch(&search, data);
967 TEST_F(NeighborhoodSearchTest, GridSearchBox)
969 const NeighborhoodSearchTestData &data = RandomBoxFullPBCData::get();
971 nb_.setCutoff(data.cutoff_);
972 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Grid);
973 gmx::AnalysisNeighborhoodSearch search =
974 nb_.initSearch(&data.pbc_, data.refPositions());
975 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Grid, search.mode());
977 testIsWithin(&search, data);
978 testMinimumDistance(&search, data);
979 testNearestPoint(&search, data);
980 testPairSearch(&search, data);
983 testPairSearchIndexed(&nb_, data, 456);
986 TEST_F(NeighborhoodSearchTest, GridSearchTriclinic)
988 const NeighborhoodSearchTestData &data = RandomTriclinicFullPBCData::get();
990 nb_.setCutoff(data.cutoff_);
991 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Grid);
992 gmx::AnalysisNeighborhoodSearch search =
993 nb_.initSearch(&data.pbc_, data.refPositions());
994 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Grid, search.mode());
996 testPairSearch(&search, data);
999 TEST_F(NeighborhoodSearchTest, GridSearch2DPBC)
1001 const NeighborhoodSearchTestData &data = RandomBox2DPBCData::get();
1003 nb_.setCutoff(data.cutoff_);
1004 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Grid);
1005 gmx::AnalysisNeighborhoodSearch search =
1006 nb_.initSearch(&data.pbc_, data.refPositions());
1007 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Grid, search.mode());
1009 testIsWithin(&search, data);
1010 testMinimumDistance(&search, data);
1011 testNearestPoint(&search, data);
1012 testPairSearch(&search, data);
1015 TEST_F(NeighborhoodSearchTest, GridSearchNoPBC)
1017 const NeighborhoodSearchTestData &data = RandomBoxNoPBCData::get();
1019 nb_.setCutoff(data.cutoff_);
1020 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Grid);
1021 gmx::AnalysisNeighborhoodSearch search =
1022 nb_.initSearch(&data.pbc_, data.refPositions());
1023 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Grid, search.mode());
1025 testPairSearch(&search, data);
1028 TEST_F(NeighborhoodSearchTest, GridSearchXYBox)
1030 const NeighborhoodSearchTestData &data = RandomBoxXYFullPBCData::get();
1032 nb_.setCutoff(data.cutoff_);
1033 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Grid);
1034 nb_.setXYMode(true);
1035 gmx::AnalysisNeighborhoodSearch search =
1036 nb_.initSearch(&data.pbc_, data.refPositions());
1037 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Grid, search.mode());
1039 testIsWithin(&search, data);
1040 testMinimumDistance(&search, data);
1041 testNearestPoint(&search, data);
1042 testPairSearch(&search, data);
1045 TEST_F(NeighborhoodSearchTest, SimpleSelfPairsSearch)
1047 const NeighborhoodSearchTestData &data = TrivialSelfPairsTestData::get();
1049 nb_.setCutoff(data.cutoff_);
1050 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Simple);
1051 gmx::AnalysisNeighborhoodSearch search =
1052 nb_.initSearch(&data.pbc_, data.refPositions());
1053 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Simple, search.mode());
1055 testPairSearchFull(&search, data, data.testPositions(), nullptr,
1056 gmx::EmptyArrayRef(), gmx::EmptyArrayRef(), true);
1059 TEST_F(NeighborhoodSearchTest, GridSelfPairsSearch)
1061 const NeighborhoodSearchTestData &data = RandomBoxSelfPairsData::get();
1063 nb_.setCutoff(data.cutoff_);
1064 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Grid);
1065 gmx::AnalysisNeighborhoodSearch search =
1066 nb_.initSearch(&data.pbc_, data.refPositions());
1067 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Grid, search.mode());
1069 testPairSearchFull(&search, data, data.testPositions(), nullptr,
1070 gmx::EmptyArrayRef(), gmx::EmptyArrayRef(), true);
1073 TEST_F(NeighborhoodSearchTest, HandlesConcurrentSearches)
1075 const NeighborhoodSearchTestData &data = TrivialTestData::get();
1077 nb_.setCutoff(data.cutoff_);
1078 gmx::AnalysisNeighborhoodSearch search1 =
1079 nb_.initSearch(&data.pbc_, data.refPositions());
1080 gmx::AnalysisNeighborhoodSearch search2 =
1081 nb_.initSearch(&data.pbc_, data.refPositions());
1083 // These checks are fragile, and unfortunately depend on the random
1084 // engine used to create the test positions. There is no particular reason
1085 // why exactly particles 0 & 2 should have neighbors, but in this case they do.
1086 gmx::AnalysisNeighborhoodPairSearch pairSearch1 =
1087 search1.startPairSearch(data.testPosition(0));
1088 gmx::AnalysisNeighborhoodPairSearch pairSearch2 =
1089 search1.startPairSearch(data.testPosition(2));
1091 testPairSearch(&search2, data);
1093 gmx::AnalysisNeighborhoodPair pair;
1094 ASSERT_TRUE(pairSearch1.findNextPair(&pair))
1095 << "Test data did not contain any pairs for position 0 (problem in the test).";
1096 EXPECT_EQ(0, pair.testIndex());
1098 NeighborhoodSearchTestData::RefPair searchPair(pair.refIndex(), std::sqrt(pair.distance2()));
1099 EXPECT_TRUE(data.containsPair(0, searchPair));
1102 ASSERT_TRUE(pairSearch2.findNextPair(&pair))
1103 << "Test data did not contain any pairs for position 2 (problem in the test).";
1104 EXPECT_EQ(2, pair.testIndex());
1106 NeighborhoodSearchTestData::RefPair searchPair(pair.refIndex(), std::sqrt(pair.distance2()));
1107 EXPECT_TRUE(data.containsPair(2, searchPair));
1111 TEST_F(NeighborhoodSearchTest, HandlesNoPBC)
1113 const NeighborhoodSearchTestData &data = TrivialNoPBCTestData::get();
1115 nb_.setCutoff(data.cutoff_);
1116 gmx::AnalysisNeighborhoodSearch search =
1117 nb_.initSearch(&data.pbc_, data.refPositions());
1118 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Simple, search.mode());
1120 testIsWithin(&search, data);
1121 testMinimumDistance(&search, data);
1122 testNearestPoint(&search, data);
1123 testPairSearch(&search, data);
1126 TEST_F(NeighborhoodSearchTest, HandlesNullPBC)
1128 const NeighborhoodSearchTestData &data = TrivialNoPBCTestData::get();
1130 nb_.setCutoff(data.cutoff_);
1131 gmx::AnalysisNeighborhoodSearch search =
1132 nb_.initSearch(nullptr, data.refPositions());
1133 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Simple, search.mode());
1135 testIsWithin(&search, data);
1136 testMinimumDistance(&search, data);
1137 testNearestPoint(&search, data);
1138 testPairSearch(&search, data);
1141 TEST_F(NeighborhoodSearchTest, HandlesSkippingPairs)
1143 const NeighborhoodSearchTestData &data = TrivialTestData::get();
1145 nb_.setCutoff(data.cutoff_);
1146 gmx::AnalysisNeighborhoodSearch search =
1147 nb_.initSearch(&data.pbc_, data.refPositions());
1148 gmx::AnalysisNeighborhoodPairSearch pairSearch =
1149 search.startPairSearch(data.testPositions());
1150 gmx::AnalysisNeighborhoodPair pair;
1151 // TODO: This test needs to be adjusted if the grid search gets optimized
1152 // to loop over the test positions in cell order (first, the ordering
1153 // assumption here breaks, and second, it then needs to be tested
1154 // separately for simple and grid searches).
1155 int currentIndex = 0;
1156 while (pairSearch.findNextPair(&pair))
1158 while (currentIndex < pair.testIndex())
1162 EXPECT_EQ(currentIndex, pair.testIndex());
1163 NeighborhoodSearchTestData::RefPair searchPair(pair.refIndex(), std::sqrt(pair.distance2()));
1164 EXPECT_TRUE(data.containsPair(currentIndex, searchPair));
1165 pairSearch.skipRemainingPairsForTestPosition();
1170 TEST_F(NeighborhoodSearchTest, SimpleSearchExclusions)
1172 const NeighborhoodSearchTestData &data = RandomBoxFullPBCData::get();
1174 ExclusionsHelper helper(data.refPosCount_, data.testPositions_.size());
1175 helper.generateExclusions();
1177 nb_.setCutoff(data.cutoff_);
1178 nb_.setTopologyExclusions(helper.exclusions());
1179 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Simple);
1180 gmx::AnalysisNeighborhoodSearch search =
1181 nb_.initSearch(&data.pbc_,
1182 data.refPositions().exclusionIds(helper.refPosIds()));
1183 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Simple, search.mode());
1185 testPairSearchFull(&search, data,
1186 data.testPositions().exclusionIds(helper.testPosIds()),
1187 helper.exclusions(), gmx::EmptyArrayRef(),
1188 gmx::EmptyArrayRef(), false);
1191 TEST_F(NeighborhoodSearchTest, GridSearchExclusions)
1193 const NeighborhoodSearchTestData &data = RandomBoxFullPBCData::get();
1195 ExclusionsHelper helper(data.refPosCount_, data.testPositions_.size());
1196 helper.generateExclusions();
1198 nb_.setCutoff(data.cutoff_);
1199 nb_.setTopologyExclusions(helper.exclusions());
1200 nb_.setMode(gmx::AnalysisNeighborhood::eSearchMode_Grid);
1201 gmx::AnalysisNeighborhoodSearch search =
1202 nb_.initSearch(&data.pbc_,
1203 data.refPositions().exclusionIds(helper.refPosIds()));
1204 ASSERT_EQ(gmx::AnalysisNeighborhood::eSearchMode_Grid, search.mode());
1206 testPairSearchFull(&search, data,
1207 data.testPositions().exclusionIds(helper.testPosIds()),
1208 helper.exclusions(), gmx::EmptyArrayRef(),
1209 gmx::EmptyArrayRef(), false);