2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014, 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 the position mapping engine.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
44 #include "gromacs/selection/poscalc.h"
48 #include <gtest/gtest.h>
50 #include "gromacs/fileio/trx.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/selection/indexutil.h"
53 #include "gromacs/selection/position.h"
54 #include "gromacs/topology/topology.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/uniqueptr.h"
58 #include "testutils/refdata.h"
65 /********************************************************************
66 * PositionCalculationTest
69 class PositionCalculationTest : public ::testing::Test
72 PositionCalculationTest();
73 ~PositionCalculationTest();
75 void generateCoordinates();
77 gmx_ana_poscalc_t *createCalculation(e_poscalc_t type, int flags);
78 void setMaximumGroup(gmx_ana_poscalc_t *pc,
79 int count, const int atoms[]);
80 gmx_ana_pos_t *initPositions(gmx_ana_poscalc_t *pc, const char *name);
82 void checkInitialized();
83 void updateAndCheck(gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p,
84 int count, const int atoms[],
85 gmx::test::TestReferenceChecker *checker,
88 void testSingleStatic(e_poscalc_t type, int flags, bool bExpectTop,
89 int atomCount, const int atoms[]);
90 void testSingleDynamic(e_poscalc_t type, int flags, bool bExpectTop,
91 int initCount, const int initAtoms[],
92 int evalCount, const int evalAtoms[]);
95 void setMaximumGroup(gmx_ana_poscalc_t *pc, const int (&atoms)[count])
97 setMaximumGroup(pc, count, atoms);
100 void updateAndCheck(gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p,
101 const int (&atoms)[count],
102 gmx::test::TestReferenceChecker *checker,
105 updateAndCheck(pc, p, count, atoms, checker, name);
107 template <int atomCount>
108 void testSingleStatic(e_poscalc_t type, int flags, bool bExpectTop,
109 const int (&atoms)[atomCount])
111 testSingleStatic(type, flags, bExpectTop, atomCount, atoms);
113 template <int initCount, int evalCount>
114 void testSingleDynamic(e_poscalc_t type, int flags, bool bExpectTop,
115 const int (&initAtoms)[initCount],
116 const int (&evalAtoms)[evalCount])
118 testSingleDynamic(type, flags, bExpectTop,
119 initCount, initAtoms, evalCount, evalAtoms);
122 gmx::test::TestReferenceData data_;
123 gmx::test::TestReferenceChecker checker_;
124 gmx::test::TopologyManager topManager_;
125 gmx::PositionCalculationCollection pcc_;
128 typedef gmx::gmx_unique_ptr<gmx_ana_pos_t>::type PositionPointer;
132 PositionTest(PositionPointer pos, gmx_ana_poscalc_t *pc,
134 : pos(gmx::move(pos)), pc(pc), name(name)
139 gmx_ana_poscalc_t *pc;
143 typedef std::vector<PositionTest> PositionTestList;
145 void setTopologyIfRequired();
146 void checkPositions(gmx::test::TestReferenceChecker *checker,
147 const char *name, gmx_ana_pos_t *p,
150 std::vector<gmx_ana_poscalc_t *> pcList_;
151 PositionTestList posList_;
155 PositionCalculationTest::PositionCalculationTest()
156 : checker_(data_.rootChecker()), bTopSet_(false)
158 topManager_.requestFrame();
161 PositionCalculationTest::~PositionCalculationTest()
163 std::vector<gmx_ana_poscalc_t *>::reverse_iterator pci;
164 for (pci = pcList_.rbegin(); pci != pcList_.rend(); ++pci)
166 gmx_ana_poscalc_free(*pci);
170 void PositionCalculationTest::generateCoordinates()
172 t_topology *top = topManager_.topology();
173 t_trxframe *frame = topManager_.frame();
174 for (int i = 0; i < top->atoms.nr; ++i)
177 frame->x[i][YY] = top->atoms.atom[i].resind;
178 frame->x[i][ZZ] = 0.0;
181 copy_rvec(frame->x[i], frame->v[i]);
182 frame->v[i][ZZ] = 1.0;
186 copy_rvec(frame->x[i], frame->f[i]);
187 frame->f[i][ZZ] = -1.0;
193 PositionCalculationTest::createCalculation(e_poscalc_t type, int flags)
195 pcList_.reserve(pcList_.size() + 1);
196 pcList_.push_back(pcc_.createCalculation(type, flags));
197 return pcList_.back();
200 void PositionCalculationTest::setMaximumGroup(gmx_ana_poscalc_t *pc,
201 int count, const int atoms[])
203 setTopologyIfRequired();
206 g.index = const_cast<int *>(atoms);
207 gmx_ana_poscalc_set_maxindex(pc, &g);
211 PositionCalculationTest::initPositions(gmx_ana_poscalc_t *pc, const char *name)
213 posList_.reserve(posList_.size() + 1);
214 PositionPointer p(new gmx_ana_pos_t());
215 gmx_ana_pos_t *result = p.get();
216 posList_.push_back(PositionTest(gmx::move(p), pc, name));
217 gmx_ana_poscalc_init_pos(pc, result);
221 void PositionCalculationTest::checkInitialized()
223 gmx::test::TestReferenceChecker compound(
224 checker_.checkCompound("InitializedPositions", NULL));
225 PositionTestList::const_iterator pi;
226 for (pi = posList_.begin(); pi != posList_.end(); ++pi)
228 checkPositions(&compound, pi->name, pi->pos.get(), false);
232 void PositionCalculationTest::updateAndCheck(
233 gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p, int count, const int atoms[],
234 gmx::test::TestReferenceChecker *checker, const char *name)
238 g.index = const_cast<int *>(atoms);
239 gmx_ana_poscalc_update(pc, p, &g, topManager_.frame(), NULL);
240 checkPositions(checker, name, p, true);
243 void PositionCalculationTest::testSingleStatic(
244 e_poscalc_t type, int flags, bool bExpectTop,
245 int atomCount, const int atoms[])
247 t_trxframe *frame = topManager_.frame();
250 flags |= POS_VELOCITIES;
256 gmx_ana_poscalc_t *pc = createCalculation(type, flags);
257 EXPECT_EQ(bExpectTop, gmx_ana_poscalc_requires_top(pc));
258 setMaximumGroup(pc, atomCount, atoms);
259 gmx_ana_pos_t *p = initPositions(pc, NULL);
262 pcc_.initEvaluation();
264 generateCoordinates();
265 gmx::test::TestReferenceChecker frameCompound(
266 checker_.checkCompound("EvaluatedPositions", "Frame0"));
267 updateAndCheck(pc, p, atomCount, atoms, &frameCompound, NULL);
271 void PositionCalculationTest::testSingleDynamic(
272 e_poscalc_t type, int flags, bool bExpectTop,
273 int initCount, const int initAtoms[],
274 int evalCount, const int evalAtoms[])
276 gmx_ana_poscalc_t *pc = createCalculation(type, flags | POS_DYNAMIC);
277 EXPECT_EQ(bExpectTop, gmx_ana_poscalc_requires_top(pc));
278 setMaximumGroup(pc, initCount, initAtoms);
279 gmx_ana_pos_t *p = initPositions(pc, NULL);
282 pcc_.initEvaluation();
284 generateCoordinates();
285 gmx::test::TestReferenceChecker frameCompound(
286 checker_.checkCompound("EvaluatedPositions", "Frame0"));
287 updateAndCheck(pc, p, evalCount, evalAtoms, &frameCompound, NULL);
291 void PositionCalculationTest::setTopologyIfRequired()
297 std::vector<gmx_ana_poscalc_t *>::const_iterator pci;
298 for (pci = pcList_.begin(); pci != pcList_.end(); ++pci)
300 if (gmx_ana_poscalc_requires_top(*pci))
303 pcc_.setTopology(topManager_.topology());
309 void PositionCalculationTest::checkPositions(
310 gmx::test::TestReferenceChecker *checker,
311 const char *name, gmx_ana_pos_t *p, bool bCoordinates)
313 gmx::test::TestReferenceChecker compound(
314 checker->checkCompound("Positions", name));
315 compound.checkInteger(p->count(), "Count");
316 const char *type = "???";
319 case INDEX_UNKNOWN: type = "unknown"; break;
320 case INDEX_ATOM: type = "atoms"; break;
321 case INDEX_RES: type = "residues"; break;
322 case INDEX_MOL: type = "molecules"; break;
323 case INDEX_ALL: type = "single"; break;
325 compound.checkString(type, "Type");
326 compound.checkSequenceArray(p->count() + 1, p->m.mapb.index, "Block");
327 for (int i = 0; i < p->count(); ++i)
329 gmx::test::TestReferenceChecker posCompound(
330 compound.checkCompound("Position", NULL));
331 posCompound.checkSequence(&p->m.mapb.a[p->m.mapb.index[i]],
332 &p->m.mapb.a[p->m.mapb.index[i+1]],
334 posCompound.checkInteger(p->m.refid[i], "RefId");
337 posCompound.checkVector(p->x[i], "Coordinates");
339 if (bCoordinates && p->v != NULL)
341 posCompound.checkVector(p->v[i], "Velocity");
343 if (bCoordinates && p->f != NULL)
345 posCompound.checkVector(p->f[i], "Force");
347 int originalIdIndex = (p->m.refid[i] != -1 ? p->m.refid[i] : i);
348 EXPECT_EQ(p->m.orgid[originalIdIndex], p->m.mapid[i]);
352 /********************************************************************
356 TEST_F(PositionCalculationTest, ComputesAtomPositions)
358 const int group[] = { 0, 1, 2, 3 };
359 topManager_.requestVelocities();
360 topManager_.requestForces();
361 topManager_.initAtoms(4);
362 testSingleStatic(POS_ATOM, 0, false, group);
365 TEST_F(PositionCalculationTest, ComputesResidueCOGPositions)
367 const int group[] = { 0, 1, 2, 3, 4, 8 };
368 topManager_.requestVelocities();
369 topManager_.requestForces();
370 topManager_.initAtoms(9);
371 topManager_.initUniformResidues(3);
372 testSingleStatic(POS_RES, 0, true, group);
375 TEST_F(PositionCalculationTest, ComputesResidueCOMPositions)
377 const int group[] = { 0, 1, 2, 3, 4, 8 };
378 topManager_.requestVelocities();
379 topManager_.requestForces();
380 topManager_.initAtoms(9);
381 topManager_.initUniformResidues(3);
382 testSingleStatic(POS_RES, POS_MASS, true, group);
385 TEST_F(PositionCalculationTest, ComputesGroupCOGPositions)
387 const int group[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
388 topManager_.requestVelocities();
389 topManager_.requestForces();
390 topManager_.initAtoms(9);
391 // Topology (masses) is requires for computing the force
392 testSingleStatic(POS_ALL, 0, true, group);
395 TEST_F(PositionCalculationTest, ComputesGroupCOMPositions)
397 const int group[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
398 topManager_.requestVelocities();
399 topManager_.requestForces();
400 topManager_.initAtoms(9);
401 testSingleStatic(POS_ALL, POS_MASS, true, group);
404 TEST_F(PositionCalculationTest, ComputesPositionsWithCompleteWhole)
406 const int group[] = { 0, 1, 2, 3, 4, 8 };
407 topManager_.initAtoms(9);
408 topManager_.initUniformResidues(3);
409 testSingleStatic(POS_RES, POS_COMPLWHOLE, true, group);
412 TEST_F(PositionCalculationTest, ComputesPositionsWithCompleteMax)
414 const int maxGroup[] = { 0, 1, 4, 5, 6, 8 };
415 const int evalGroup[] = { 0, 1, 5, 6 };
416 topManager_.initAtoms(9);
417 topManager_.initUniformResidues(3);
418 testSingleDynamic(POS_RES, POS_COMPLMAX, true, maxGroup, evalGroup);
421 TEST_F(PositionCalculationTest, ComputesPositionMask)
423 const int maxGroup[] = { 0, 1, 2, 3, 4, 5 };
424 const int evalGroup[] = { 1, 2, 4 };
425 topManager_.initAtoms(6);
426 testSingleDynamic(POS_ATOM, POS_MASKONLY, false, maxGroup, evalGroup);
429 // TODO: Check for POS_ALL_PBC
431 TEST_F(PositionCalculationTest, HandlesIdenticalStaticCalculations)
433 const int group[] = { 0, 1, 4, 5, 6, 7 };
434 topManager_.initAtoms(9);
435 topManager_.initUniformResidues(3);
437 gmx_ana_poscalc_t *pc1 = createCalculation(POS_RES, 0);
438 gmx_ana_poscalc_t *pc2 = createCalculation(POS_RES, 0);
439 gmx_ana_poscalc_t *pc3 = createCalculation(POS_RES, 0);
440 setMaximumGroup(pc1, group);
441 setMaximumGroup(pc2, group);
442 setMaximumGroup(pc3, group);
443 gmx_ana_pos_t *p1 = initPositions(pc1, "Positions");
444 gmx_ana_pos_t *p2 = initPositions(pc2, "Positions");
445 gmx_ana_pos_t *p3 = initPositions(pc3, "Positions");
448 pcc_.initEvaluation();
450 generateCoordinates();
451 gmx::test::TestReferenceChecker frameCompound(
452 checker_.checkCompound("EvaluatedPositions", "Frame0"));
453 updateAndCheck(pc1, p1, group, &frameCompound, "Positions");
454 updateAndCheck(pc2, p2, group, &frameCompound, "Positions");
455 updateAndCheck(pc3, p3, group, &frameCompound, "Positions");
459 TEST_F(PositionCalculationTest, HandlesOverlappingStaticCalculations)
461 const int group1[] = { 0, 1, 4, 5 };
462 const int group2[] = { 4, 5, 7, 8 };
463 topManager_.initAtoms(9);
464 topManager_.initUniformResidues(3);
466 gmx_ana_poscalc_t *pc1 = createCalculation(POS_RES, 0);
467 gmx_ana_poscalc_t *pc2 = createCalculation(POS_RES, 0);
468 setMaximumGroup(pc1, group1);
469 setMaximumGroup(pc2, group2);
470 gmx_ana_pos_t *p1 = initPositions(pc1, "P1");
471 gmx_ana_pos_t *p2 = initPositions(pc2, "P2");
474 pcc_.initEvaluation();
476 generateCoordinates();
477 gmx::test::TestReferenceChecker frameCompound(
478 checker_.checkCompound("EvaluatedPositions", "Frame0"));
479 updateAndCheck(pc1, p1, group1, &frameCompound, "P1");
480 updateAndCheck(pc2, p2, group2, &frameCompound, "P2");
484 // TODO: Check for handling of more multiple calculation cases