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