| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | *
|
|---|
| 7 | * This file is part of MoleCuilder.
|
|---|
| 8 | *
|
|---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
|---|
| 10 | * it under the terms of the GNU General Public License as published by
|
|---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 12 | * (at your option) any later version.
|
|---|
| 13 | *
|
|---|
| 14 | * MoleCuilder 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
|
|---|
| 17 | * GNU General Public License for more details.
|
|---|
| 18 | *
|
|---|
| 19 | * You should have received a copy of the GNU General Public License
|
|---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * AtomDescriptorUnitTest.cpp
|
|---|
| 25 | *
|
|---|
| 26 | * Created on: Feb 9, 2010
|
|---|
| 27 | * Author: crueger
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | // include config.h
|
|---|
| 31 | #ifdef HAVE_CONFIG_H
|
|---|
| 32 | #include <config.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | #include "AtomDescriptorUnitTest.hpp"
|
|---|
| 36 |
|
|---|
| 37 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 40 | #include <iostream>
|
|---|
| 41 |
|
|---|
| 42 | #include <Descriptors/AtomDescriptor.hpp>
|
|---|
| 43 | #include <Descriptors/AtomIdDescriptor.hpp>
|
|---|
| 44 | #include <Descriptors/AtomOfMoleculeDescriptor.hpp>
|
|---|
| 45 | #include <Descriptors/AtomOrderDescriptor.hpp>
|
|---|
| 46 | #include <Descriptors/AtomsWithinDistanceOfDescriptor.hpp>
|
|---|
| 47 |
|
|---|
| 48 | #include "World.hpp"
|
|---|
| 49 | #include "Atom/atom.hpp"
|
|---|
| 50 | #include "molecule.hpp"
|
|---|
| 51 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 52 |
|
|---|
| 53 | #ifdef HAVE_TESTRUNNER
|
|---|
| 54 | #include "UnitTestMain.hpp"
|
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 56 |
|
|---|
| 57 | /********************************************** Test classes **************************************/
|
|---|
| 58 | // Registers the fixture into the 'registry'
|
|---|
| 59 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest );
|
|---|
| 60 |
|
|---|
| 61 | // set up and tear down
|
|---|
| 62 | void AtomDescriptorTest::setUp()
|
|---|
| 63 | {
|
|---|
| 64 | World::getInstance();
|
|---|
| 65 | for(int i=0;i<ATOM_COUNT;++i){
|
|---|
| 66 | atoms[i]= World::getInstance().createAtom();
|
|---|
| 67 | atomIds[i]= atoms[i]->getId();
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void AtomDescriptorTest::tearDown()
|
|---|
| 72 | {
|
|---|
| 73 | World::purgeInstance();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | // some helper functions
|
|---|
| 77 | static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>())
|
|---|
| 78 | {
|
|---|
| 79 | for(int i=0;i<ATOM_COUNT;++i){
|
|---|
| 80 | atomId_t id = ids[i];
|
|---|
| 81 | if(!excluded.count(id)){
|
|---|
| 82 | std::vector<atom*>::iterator iter;
|
|---|
| 83 | bool res=false;
|
|---|
| 84 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
|---|
| 85 | res |= (*iter)->getId() == id;
|
|---|
| 86 | }
|
|---|
| 87 | if(!res) {
|
|---|
| 88 | cout << "Atom " << id << " missing in returned list" << endl;
|
|---|
| 89 | return false;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | return true;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | static bool hasNoDuplicateAtoms(std::vector<atom*> atoms)
|
|---|
| 97 | {
|
|---|
| 98 | std::set<atomId_t> found;
|
|---|
| 99 | std::vector<atom*>::iterator iter;
|
|---|
| 100 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
|---|
| 101 | int id = (*iter)->getId();
|
|---|
| 102 | if(found.count(id))
|
|---|
| 103 | return false;
|
|---|
| 104 | found.insert(id);
|
|---|
| 105 | }
|
|---|
| 106 | return true;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | void AtomDescriptorTest::AtomBaseSetsTest()
|
|---|
| 111 | {
|
|---|
| 112 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
|---|
| 113 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds));
|
|---|
| 114 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms));
|
|---|
| 115 |
|
|---|
| 116 | std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms());
|
|---|
| 117 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void AtomDescriptorTest::AtomIdTest()
|
|---|
| 121 | {
|
|---|
| 122 | // test Atoms from boundaries and middle of the set
|
|---|
| 123 | atom* testAtom;
|
|---|
| 124 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
|
|---|
| 125 | CPPUNIT_ASSERT(testAtom);
|
|---|
| 126 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
|---|
| 127 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2]));
|
|---|
| 128 | CPPUNIT_ASSERT(testAtom);
|
|---|
| 129 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
|
|---|
| 130 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1]));
|
|---|
| 131 | CPPUNIT_ASSERT(testAtom);
|
|---|
| 132 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
|
|---|
| 133 |
|
|---|
| 134 | // find some ID that has not been created
|
|---|
| 135 | atomId_t outsideId=0;
|
|---|
| 136 | bool res = false;
|
|---|
| 137 | for(outsideId=0;!res;++outsideId) {
|
|---|
| 138 | res = true;
|
|---|
| 139 | for(int i = 0; i < ATOM_COUNT; ++i){
|
|---|
| 140 | res &= atomIds[i]!=outsideId;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | // test from outside of set
|
|---|
| 144 | testAtom = World::getInstance().getAtom(AtomById(outsideId));
|
|---|
| 145 | CPPUNIT_ASSERT(!testAtom);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | void AtomDescriptorTest::AtomOfMoleculeTest()
|
|---|
| 149 | {
|
|---|
| 150 | // test Atoms from boundaries and middle of the set
|
|---|
| 151 | atom* testAtom;
|
|---|
| 152 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
|
|---|
| 153 | CPPUNIT_ASSERT(testAtom);
|
|---|
| 154 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
|---|
| 155 |
|
|---|
| 156 | // create some molecule and associate atom to it
|
|---|
| 157 | testAtom->setType(1);
|
|---|
| 158 | molecule * newmol = World::getInstance().createMolecule();
|
|---|
| 159 | newmol->AddAtom(testAtom);
|
|---|
| 160 | CPPUNIT_ASSERT_EQUAL(newmol->getId(), testAtom->getMolecule()->getId());
|
|---|
| 161 |
|
|---|
| 162 | // get atom by descriptor
|
|---|
| 163 | World::AtomComposite atoms = World::getInstance().getAllAtoms(AtomOfMolecule(newmol->getId()));
|
|---|
| 164 | CPPUNIT_ASSERT_EQUAL( (size_t)1, atoms.size() );
|
|---|
| 165 | CPPUNIT_ASSERT_EQUAL( (*atoms.begin())->getId(), testAtom->getId() );
|
|---|
| 166 |
|
|---|
| 167 | // remove molecule again
|
|---|
| 168 | World::getInstance().destroyMolecule(newmol);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | void AtomDescriptorTest::AtomOrderTest()
|
|---|
| 172 | {
|
|---|
| 173 | atom* testAtom;
|
|---|
| 174 |
|
|---|
| 175 | // test in normal order: 1, 2, ...
|
|---|
| 176 | for(int i=1;i<=ATOM_COUNT;++i){
|
|---|
| 177 | testAtom = World::getInstance().getAtom(AtomByOrder(i));
|
|---|
| 178 | CPPUNIT_ASSERT_EQUAL( atomIds[i-1], testAtom->getId());
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | // test in reverse order: -1, -2, ...
|
|---|
| 182 | for(int i=1; i<= ATOM_COUNT;++i){
|
|---|
| 183 | testAtom = World::getInstance().getAtom(AtomByOrder(-i));
|
|---|
| 184 | CPPUNIT_ASSERT_EQUAL( atomIds[(int)ATOM_COUNT-i], testAtom->getId());
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | // test from outside of set
|
|---|
| 188 | testAtom = World::getInstance().getAtom(AtomByOrder(ATOM_COUNT+1));
|
|---|
| 189 | CPPUNIT_ASSERT(!testAtom);
|
|---|
| 190 | testAtom = World::getInstance().getAtom(AtomByOrder(-ATOM_COUNT-1));
|
|---|
| 191 | CPPUNIT_ASSERT(!testAtom);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 | std::set<atomId_t> getDistanceList(const double distance, const Vector &position, atom **list)
|
|---|
| 196 | {
|
|---|
| 197 | const double distanceSquared = distance*distance;
|
|---|
| 198 | std::set<atomId_t> reflist;
|
|---|
| 199 | for (size_t i=0; i<ATOM_COUNT;++i)
|
|---|
| 200 | if (list[i]->getPosition().DistanceSquared(position) < distanceSquared)
|
|---|
| 201 | reflist.insert ( list[i]->getId() );
|
|---|
| 202 | return reflist;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 | std::set<atomId_t> getIdList(const World::AtomComposite &list)
|
|---|
| 207 | {
|
|---|
| 208 | std::set<atomId_t> testlist;
|
|---|
| 209 | for (World::AtomComposite::const_iterator iter = list.begin();
|
|---|
| 210 | iter != list.end(); ++iter)
|
|---|
| 211 | testlist.insert( (*iter)->getId() );
|
|---|
| 212 | return testlist;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | //void AtomDescriptorTest::AtomsShapeTest()
|
|---|
| 216 | //{
|
|---|
| 217 | // // align atoms along an axis
|
|---|
| 218 | // for(int i=0;i<ATOM_COUNT;++i) {
|
|---|
| 219 | // atoms[i]->setPosition(Vector((double)i, 0., 0.));
|
|---|
| 220 | // //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
|
|---|
| 221 | // }
|
|---|
| 222 | //
|
|---|
| 223 | // // get atom by descriptor ...
|
|---|
| 224 | // // ... from origin up to 2.5
|
|---|
| 225 | // {
|
|---|
| 226 | // const double distance = 1.5;
|
|---|
| 227 | // Vector position(0.,0.,0.);
|
|---|
| 228 | // Shape s = Sphere(position, distance);
|
|---|
| 229 | // World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsByShape(s));
|
|---|
| 230 | // CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
|
|---|
| 231 | // std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
|
|---|
| 232 | // std::set<atomId_t> testlist = getIdList(atomlist);
|
|---|
| 233 | // CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
|---|
| 234 | // }
|
|---|
| 235 | // // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
|
|---|
| 236 | // {
|
|---|
| 237 | // const double distance = 2.9;
|
|---|
| 238 | // Vector position(4.,0.,0.);
|
|---|
| 239 | // Shape s = Sphere(position, distance);
|
|---|
| 240 | // World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsByShape(s));
|
|---|
| 241 | // CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
|
|---|
| 242 | // std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
|
|---|
| 243 | // std::set<atomId_t> testlist = getIdList(atomlist);
|
|---|
| 244 | // CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
|---|
| 245 | // }
|
|---|
| 246 | // // ... from (10,0,0) up to 1.5
|
|---|
| 247 | // {
|
|---|
| 248 | // const double distance = 1.5;
|
|---|
| 249 | // Vector *position = new Vector(10.,0.,0.);
|
|---|
| 250 | // Shape s = Sphere(position, distance);
|
|---|
| 251 | // World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsByShape(s));
|
|---|
| 252 | // CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
|
|---|
| 253 | // std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
|
|---|
| 254 | // std::set<atomId_t> testlist = getIdList(atomlist);
|
|---|
| 255 | // CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
|---|
| 256 | // delete position;
|
|---|
| 257 | // }
|
|---|
| 258 | //}
|
|---|
| 259 |
|
|---|
| 260 | void AtomDescriptorTest::AtomsWithinDistanceOfTest()
|
|---|
| 261 | {
|
|---|
| 262 | // align atoms along an axis
|
|---|
| 263 | for(int i=0;i<ATOM_COUNT;++i) {
|
|---|
| 264 | atoms[i]->setPosition(Vector((double)i, 0., 0.));
|
|---|
| 265 | //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | // get atom by descriptor ...
|
|---|
| 269 | // ... from origin up to 2.5
|
|---|
| 270 | {
|
|---|
| 271 | const double distance = 1.5;
|
|---|
| 272 | Vector position(0.,0.,0.);
|
|---|
| 273 | World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, position));
|
|---|
| 274 | CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
|
|---|
| 275 | std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
|
|---|
| 276 | std::set<atomId_t> testlist = getIdList(atomlist);
|
|---|
| 277 | CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
|---|
| 278 | }
|
|---|
| 279 | // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
|
|---|
| 280 | {
|
|---|
| 281 | const double distance = 2.9;
|
|---|
| 282 | World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, Vector(4.,0.,0.)));
|
|---|
| 283 | CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
|
|---|
| 284 | std::set<atomId_t> reflist = getDistanceList(distance, Vector(4.,0.,0.), atoms);
|
|---|
| 285 | std::set<atomId_t> testlist = getIdList(atomlist);
|
|---|
| 286 | CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
|---|
| 287 | }
|
|---|
| 288 | // ... from (10,0,0) up to 1.5
|
|---|
| 289 | {
|
|---|
| 290 | const double distance = 1.5;
|
|---|
| 291 | Vector *position = new Vector(10.,0.,0.);
|
|---|
| 292 | World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, *position));
|
|---|
| 293 | CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
|
|---|
| 294 | std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
|
|---|
| 295 | std::set<atomId_t> testlist = getIdList(atomlist);
|
|---|
| 296 | CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
|---|
| 297 | delete position;
|
|---|
| 298 | }
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | void AtomDescriptorTest::AtomCalcTest()
|
|---|
| 302 | {
|
|---|
| 303 | // test some elementary set operations
|
|---|
| 304 | {
|
|---|
| 305 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms());
|
|---|
| 306 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
|---|
| 307 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | {
|
|---|
| 311 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms());
|
|---|
| 312 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
|---|
| 313 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | {
|
|---|
| 317 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms());
|
|---|
| 318 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | {
|
|---|
| 322 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms());
|
|---|
| 323 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | {
|
|---|
| 327 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms());
|
|---|
| 328 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | {
|
|---|
| 332 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms());
|
|---|
| 333 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
|---|
| 334 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
|---|
| 335 | }
|
|---|
| 336 | // exclude and include some atoms
|
|---|
| 337 | {
|
|---|
| 338 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
|
|---|
| 339 | std::set<atomId_t> excluded;
|
|---|
| 340 | excluded.insert(atomIds[ATOM_COUNT/2]);
|
|---|
| 341 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded));
|
|---|
| 342 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
|---|
| 343 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | {
|
|---|
| 347 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
|
|---|
| 348 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
|
|---|
| 349 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|