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 <gtest/gtest.h>
48 #include "gromacs/fileio/trx.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/selection/indexutil.h"
51 #include "gromacs/selection/poscalc.h"
52 #include "gromacs/selection/position.h"
53 #include "gromacs/topology/topology.h"
54 #include "gromacs/utility/smalloc.h"
55 #include "gromacs/utility/uniqueptr.h"
57 #include "testutils/refdata.h"
64 /********************************************************************
65 * PositionCalculationTest
68 class PositionCalculationTest : public ::testing::Test
71 PositionCalculationTest();
72 ~PositionCalculationTest();
74 void generateCoordinates();
76 gmx_ana_poscalc_t *createCalculation(e_poscalc_t type, int flags);
77 void setMaximumGroup(gmx_ana_poscalc_t *pc,
78 int count, const int atoms[]);
79 gmx_ana_pos_t *initPositions(gmx_ana_poscalc_t *pc, const char *name);
81 void checkInitialized();
82 void updateAndCheck(gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p,
83 int count, const int atoms[],
84 gmx::test::TestReferenceChecker *checker,
87 void testSingleStatic(e_poscalc_t type, int flags, bool bExpectTop,
88 int atomCount, const int atoms[]);
89 void testSingleDynamic(e_poscalc_t type, int flags, bool bExpectTop,
90 int initCount, const int initAtoms[],
91 int evalCount, const int evalAtoms[]);
94 void setMaximumGroup(gmx_ana_poscalc_t *pc, const int (&atoms)[count])
96 setMaximumGroup(pc, count, atoms);
99 void updateAndCheck(gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p,
100 const int (&atoms)[count],
101 gmx::test::TestReferenceChecker *checker,
104 updateAndCheck(pc, p, count, atoms, checker, name);
106 template <int atomCount>
107 void testSingleStatic(e_poscalc_t type, int flags, bool bExpectTop,
108 const int (&atoms)[atomCount])
110 testSingleStatic(type, flags, bExpectTop, atomCount, atoms);
112 template <int initCount, int evalCount>
113 void testSingleDynamic(e_poscalc_t type, int flags, bool bExpectTop,
114 const int (&initAtoms)[initCount],
115 const int (&evalAtoms)[evalCount])
117 testSingleDynamic(type, flags, bExpectTop,
118 initCount, initAtoms, evalCount, evalAtoms);
121 gmx::test::TestReferenceData data_;
122 gmx::test::TestReferenceChecker checker_;
123 gmx::test::TopologyManager topManager_;
124 gmx::PositionCalculationCollection pcc_;
127 typedef gmx::gmx_unique_ptr<gmx_ana_pos_t>::type PositionPointer;
131 PositionTest(PositionPointer pos, gmx_ana_poscalc_t *pc,
133 : pos(gmx::move(pos)), pc(pc), name(name)
138 gmx_ana_poscalc_t *pc;
142 typedef std::vector<PositionTest> PositionTestList;
144 void setTopologyIfRequired();
145 void checkPositions(gmx::test::TestReferenceChecker *checker,
146 const char *name, gmx_ana_pos_t *p,
149 std::vector<gmx_ana_poscalc_t *> pcList_;
150 PositionTestList posList_;
154 PositionCalculationTest::PositionCalculationTest()
155 : checker_(data_.rootChecker()), bTopSet_(false)
157 topManager_.requestFrame();
160 PositionCalculationTest::~PositionCalculationTest()
162 std::vector<gmx_ana_poscalc_t *>::reverse_iterator pci;
163 for (pci = pcList_.rbegin(); pci != pcList_.rend(); ++pci)
165 gmx_ana_poscalc_free(*pci);
169 void PositionCalculationTest::generateCoordinates()
171 t_topology *top = topManager_.topology();
172 t_trxframe *frame = topManager_.frame();
173 for (int i = 0; i < top->atoms.nr; ++i)
176 frame->x[i][YY] = top->atoms.atom[i].resind;
177 frame->x[i][ZZ] = 0.0;
180 copy_rvec(frame->x[i], frame->v[i]);
181 frame->v[i][ZZ] = 1.0;
185 copy_rvec(frame->x[i], frame->f[i]);
186 frame->f[i][ZZ] = -1.0;
192 PositionCalculationTest::createCalculation(e_poscalc_t type, int flags)
194 pcList_.reserve(pcList_.size() + 1);
195 pcList_.push_back(pcc_.createCalculation(type, flags));
196 return pcList_.back();
199 void PositionCalculationTest::setMaximumGroup(gmx_ana_poscalc_t *pc,
200 int count, const int atoms[])
202 setTopologyIfRequired();
205 g.index = const_cast<int *>(atoms);
206 gmx_ana_poscalc_set_maxindex(pc, &g);
210 PositionCalculationTest::initPositions(gmx_ana_poscalc_t *pc, const char *name)
212 posList_.reserve(posList_.size() + 1);
213 PositionPointer p(new gmx_ana_pos_t());
214 gmx_ana_pos_t *result = p.get();
215 posList_.push_back(PositionTest(gmx::move(p), pc, name));
216 gmx_ana_poscalc_init_pos(pc, result);
220 void PositionCalculationTest::checkInitialized()
222 gmx::test::TestReferenceChecker compound(
223 checker_.checkCompound("InitializedPositions", NULL));
224 PositionTestList::const_iterator pi;
225 for (pi = posList_.begin(); pi != posList_.end(); ++pi)
227 checkPositions(&compound, pi->name, pi->pos.get(), false);
231 void PositionCalculationTest::updateAndCheck(
232 gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p, int count, const int atoms[],
233 gmx::test::TestReferenceChecker *checker, const char *name)
237 g.index = const_cast<int *>(atoms);
238 gmx_ana_poscalc_update(pc, p, &g, topManager_.frame(), NULL);
239 checkPositions(checker, name, p, true);
242 void PositionCalculationTest::testSingleStatic(
243 e_poscalc_t type, int flags, bool bExpectTop,
244 int atomCount, const int atoms[])
246 t_trxframe *frame = topManager_.frame();
249 flags |= POS_VELOCITIES;
255 gmx_ana_poscalc_t *pc = createCalculation(type, flags);
256 EXPECT_EQ(bExpectTop, gmx_ana_poscalc_requires_top(pc));
257 setMaximumGroup(pc, atomCount, atoms);
258 gmx_ana_pos_t *p = initPositions(pc, NULL);
261 pcc_.initEvaluation();
263 generateCoordinates();
264 gmx::test::TestReferenceChecker frameCompound(
265 checker_.checkCompound("EvaluatedPositions", "Frame0"));
266 updateAndCheck(pc, p, atomCount, atoms, &frameCompound, NULL);
270 void PositionCalculationTest::testSingleDynamic(
271 e_poscalc_t type, int flags, bool bExpectTop,
272 int initCount, const int initAtoms[],
273 int evalCount, const int evalAtoms[])
275 gmx_ana_poscalc_t *pc = createCalculation(type, flags | POS_DYNAMIC);
276 EXPECT_EQ(bExpectTop, gmx_ana_poscalc_requires_top(pc));
277 setMaximumGroup(pc, initCount, initAtoms);
278 gmx_ana_pos_t *p = initPositions(pc, NULL);
281 pcc_.initEvaluation();
283 generateCoordinates();
284 gmx::test::TestReferenceChecker frameCompound(
285 checker_.checkCompound("EvaluatedPositions", "Frame0"));
286 updateAndCheck(pc, p, evalCount, evalAtoms, &frameCompound, NULL);
290 void PositionCalculationTest::setTopologyIfRequired()
296 std::vector<gmx_ana_poscalc_t *>::const_iterator pci;
297 for (pci = pcList_.begin(); pci != pcList_.end(); ++pci)
299 if (gmx_ana_poscalc_requires_top(*pci))
302 pcc_.setTopology(topManager_.topology());
308 void PositionCalculationTest::checkPositions(
309 gmx::test::TestReferenceChecker *checker,
310 const char *name, gmx_ana_pos_t *p, bool bCoordinates)
312 gmx::test::TestReferenceChecker compound(
313 checker->checkCompound("Positions", name));
314 compound.checkInteger(p->count(), "Count");
315 const char *type = "???";
318 case INDEX_UNKNOWN: type = "unknown"; break;
319 case INDEX_ATOM: type = "atoms"; break;
320 case INDEX_RES: type = "residues"; break;
321 case INDEX_MOL: type = "molecules"; break;
322 case INDEX_ALL: type = "single"; break;
324 compound.checkString(type, "Type");
325 compound.checkSequenceArray(p->count() + 1, p->m.mapb.index, "Block");
326 for (int i = 0; i < p->count(); ++i)
328 gmx::test::TestReferenceChecker posCompound(
329 compound.checkCompound("Position", NULL));
330 posCompound.checkSequence(&p->m.mapb.a[p->m.mapb.index[i]],
331 &p->m.mapb.a[p->m.mapb.index[i+1]],
333 posCompound.checkInteger(p->m.refid[i], "RefId");
336 posCompound.checkVector(p->x[i], "Coordinates");
338 if (bCoordinates && p->v != NULL)
340 posCompound.checkVector(p->v[i], "Velocity");
342 if (bCoordinates && p->f != NULL)
344 posCompound.checkVector(p->f[i], "Force");
346 int originalIdIndex = (p->m.refid[i] != -1 ? p->m.refid[i] : i);
347 EXPECT_EQ(p->m.orgid[originalIdIndex], p->m.mapid[i]);
351 /********************************************************************
355 TEST_F(PositionCalculationTest, ComputesAtomPositions)
357 const int group[] = { 0, 1, 2, 3 };
358 topManager_.requestVelocities();
359 topManager_.requestForces();
360 topManager_.initAtoms(4);
361 testSingleStatic(POS_ATOM, 0, false, group);
364 TEST_F(PositionCalculationTest, ComputesResidueCOGPositions)
366 const int group[] = { 0, 1, 2, 3, 4, 8 };
367 topManager_.requestVelocities();
368 topManager_.requestForces();
369 topManager_.initAtoms(9);
370 topManager_.initUniformResidues(3);
371 testSingleStatic(POS_RES, 0, true, group);
374 TEST_F(PositionCalculationTest, ComputesResidueCOMPositions)
376 const int group[] = { 0, 1, 2, 3, 4, 8 };
377 topManager_.requestVelocities();
378 topManager_.requestForces();
379 topManager_.initAtoms(9);
380 topManager_.initUniformResidues(3);
381 testSingleStatic(POS_RES, POS_MASS, true, group);
384 TEST_F(PositionCalculationTest, ComputesGroupCOGPositions)
386 const int group[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
387 topManager_.requestVelocities();
388 topManager_.requestForces();
389 topManager_.initAtoms(9);
390 // Topology (masses) is requires for computing the force
391 testSingleStatic(POS_ALL, 0, true, group);
394 TEST_F(PositionCalculationTest, ComputesGroupCOMPositions)
396 const int group[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
397 topManager_.requestVelocities();
398 topManager_.requestForces();
399 topManager_.initAtoms(9);
400 testSingleStatic(POS_ALL, POS_MASS, true, group);
403 TEST_F(PositionCalculationTest, ComputesPositionsWithCompleteWhole)
405 const int group[] = { 0, 1, 2, 3, 4, 8 };
406 topManager_.initAtoms(9);
407 topManager_.initUniformResidues(3);
408 testSingleStatic(POS_RES, POS_COMPLWHOLE, true, group);
411 TEST_F(PositionCalculationTest, ComputesPositionsWithCompleteMax)
413 const int maxGroup[] = { 0, 1, 4, 5, 6, 8 };
414 const int evalGroup[] = { 0, 1, 5, 6 };
415 topManager_.initAtoms(9);
416 topManager_.initUniformResidues(3);
417 testSingleDynamic(POS_RES, POS_COMPLMAX, true, maxGroup, evalGroup);
420 TEST_F(PositionCalculationTest, ComputesPositionMask)
422 const int maxGroup[] = { 0, 1, 2, 3, 4, 5 };
423 const int evalGroup[] = { 1, 2, 4 };
424 topManager_.initAtoms(6);
425 testSingleDynamic(POS_ATOM, POS_MASKONLY, false, maxGroup, evalGroup);
428 // TODO: Check for POS_ALL_PBC
430 TEST_F(PositionCalculationTest, HandlesIdenticalStaticCalculations)
432 const int group[] = { 0, 1, 4, 5, 6, 7 };
433 topManager_.initAtoms(9);
434 topManager_.initUniformResidues(3);
436 gmx_ana_poscalc_t *pc1 = createCalculation(POS_RES, 0);
437 gmx_ana_poscalc_t *pc2 = createCalculation(POS_RES, 0);
438 gmx_ana_poscalc_t *pc3 = createCalculation(POS_RES, 0);
439 setMaximumGroup(pc1, group);
440 setMaximumGroup(pc2, group);
441 setMaximumGroup(pc3, group);
442 gmx_ana_pos_t *p1 = initPositions(pc1, "Positions");
443 gmx_ana_pos_t *p2 = initPositions(pc2, "Positions");
444 gmx_ana_pos_t *p3 = initPositions(pc3, "Positions");
447 pcc_.initEvaluation();
449 generateCoordinates();
450 gmx::test::TestReferenceChecker frameCompound(
451 checker_.checkCompound("EvaluatedPositions", "Frame0"));
452 updateAndCheck(pc1, p1, group, &frameCompound, "Positions");
453 updateAndCheck(pc2, p2, group, &frameCompound, "Positions");
454 updateAndCheck(pc3, p3, group, &frameCompound, "Positions");
458 TEST_F(PositionCalculationTest, HandlesOverlappingStaticCalculations)
460 const int group1[] = { 0, 1, 4, 5 };
461 const int group2[] = { 4, 5, 7, 8 };
462 topManager_.initAtoms(9);
463 topManager_.initUniformResidues(3);
465 gmx_ana_poscalc_t *pc1 = createCalculation(POS_RES, 0);
466 gmx_ana_poscalc_t *pc2 = createCalculation(POS_RES, 0);
467 setMaximumGroup(pc1, group1);
468 setMaximumGroup(pc2, group2);
469 gmx_ana_pos_t *p1 = initPositions(pc1, "P1");
470 gmx_ana_pos_t *p2 = initPositions(pc2, "P2");
473 pcc_.initEvaluation();
475 generateCoordinates();
476 gmx::test::TestReferenceChecker frameCompound(
477 checker_.checkCompound("EvaluatedPositions", "Frame0"));
478 updateAndCheck(pc1, p1, group1, &frameCompound, "P1");
479 updateAndCheck(pc2, p2, group2, &frameCompound, "P2");
483 // TODO: Check for handling of more multiple calculation cases