| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  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 | * LinkedCell_ViewUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Nov 18, 2011 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include <algorithm> | 
|---|
| 42 | #include <list> | 
|---|
| 43 |  | 
|---|
| 44 | #include "Box.hpp" | 
|---|
| 45 | #include "CodePatterns/Assert.hpp" | 
|---|
| 46 | #include "CodePatterns/Log.hpp" | 
|---|
| 47 | #include "LinearAlgebra/defs.hpp" | 
|---|
| 48 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| 49 | #include "LinkedCell/LinkedCell.hpp" | 
|---|
| 50 | #include "LinkedCell/LinkedCell_Model.hpp" | 
|---|
| 51 | #include "LinkedCell/LinkedCell_View.hpp" | 
|---|
| 52 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 53 | #include "LinkedCell/unittests/defs.hpp" | 
|---|
| 54 | #include "World.hpp" | 
|---|
| 55 | #include "WorldTime.hpp" | 
|---|
| 56 |  | 
|---|
| 57 | #include "LinkedCell_ViewUnitTest.hpp" | 
|---|
| 58 |  | 
|---|
| 59 | #ifdef HAVE_TESTRUNNER | 
|---|
| 60 | #include "UnitTestMain.hpp" | 
|---|
| 61 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 62 |  | 
|---|
| 63 | /********************************************** Test classes **************************************/ | 
|---|
| 64 |  | 
|---|
| 65 | // Registers the fixture into the 'registry' | 
|---|
| 66 | CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCell_ViewTest ); | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | void LinkedCell_ViewTest::setUp() | 
|---|
| 70 | { | 
|---|
| 71 | // failing asserts should be thrown | 
|---|
| 72 | ASSERT_DO(Assert::Throw); | 
|---|
| 73 |  | 
|---|
| 74 | //setVerbosity(3); | 
|---|
| 75 |  | 
|---|
| 76 | // create diag(20.) matrix | 
|---|
| 77 | RealSpaceMatrix BoxM; | 
|---|
| 78 | BoxM.setIdentity(); | 
|---|
| 79 | BoxM *= 20.; | 
|---|
| 80 |  | 
|---|
| 81 | // create Box with this matrix | 
|---|
| 82 | domain = new Box(BoxM); | 
|---|
| 83 |  | 
|---|
| 84 | // create LinkedCell structure with this Box | 
|---|
| 85 | LCImpl = new LinkedCell::LinkedCell_Model(EDGELENGTH, *domain); | 
|---|
| 86 |  | 
|---|
| 87 | // create a list of nodes and add to LCImpl | 
|---|
| 88 | std::vector< Vector > VectorList; | 
|---|
| 89 | for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i) | 
|---|
| 90 | VectorList.push_back(Vector((double)i*EDGELENGTH,(double)i*EDGELENGTH,(double)i*EDGELENGTH)); | 
|---|
| 91 | for (size_t i=0;i<VectorList.size();++i) { | 
|---|
| 92 | TesselPoint * Walker = new TesselPoint(); | 
|---|
| 93 | Walker->setName(std::string("Walker")+toString(i)); | 
|---|
| 94 | Walker->setPosition(VectorList[i]); | 
|---|
| 95 | NodeList.insert(Walker); | 
|---|
| 96 | LCImpl->addNode(Walker); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | // create LinkedCell_View from that | 
|---|
| 100 | LC = new LinkedCell::LinkedCell_View(*LCImpl); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | void LinkedCell_ViewTest::tearDown() | 
|---|
| 105 | { | 
|---|
| 106 | delete LC; | 
|---|
| 107 | delete LCImpl; | 
|---|
| 108 | delete domain; | 
|---|
| 109 |  | 
|---|
| 110 | // remove all nodes again | 
|---|
| 111 | for (PointSet::iterator iter = NodeList.begin(); | 
|---|
| 112 | !NodeList.empty(); | 
|---|
| 113 | iter = NodeList.begin()) { | 
|---|
| 114 | delete *iter; | 
|---|
| 115 | NodeList.erase(iter); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | // delete in correct order | 
|---|
| 119 | World::purgeInstance(); | 
|---|
| 120 | WorldTime::purgeInstance(); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | /** UnitTest for getAllNeighbors() | 
|---|
| 125 | */ | 
|---|
| 126 | void LinkedCell_ViewTest::getAllNeighborsTest() | 
|---|
| 127 | { | 
|---|
| 128 | // define some center vector | 
|---|
| 129 | Vector center(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.); | 
|---|
| 130 |  | 
|---|
| 131 | // get LinkedList from LC | 
|---|
| 132 | const double distance = 2.; | 
|---|
| 133 | LinkedCell::LinkedList NeighborList = LC->getAllNeighbors(distance, center); | 
|---|
| 134 | //  for (LinkedCell::LinkedList::const_iterator iter = NeighborList.begin(); | 
|---|
| 135 | //      iter != NeighborList.end(); ++iter) | 
|---|
| 136 | //    std::cout << **iter << " is in returned neighbor list." << std::endl; | 
|---|
| 137 |  | 
|---|
| 138 | // gather points from NodeList | 
|---|
| 139 | LinkedCell::LinkedList ComparisonList; | 
|---|
| 140 | for (PointSet::const_iterator iter = NodeList.begin(); iter != NodeList.end(); ++iter) | 
|---|
| 141 | if (center.DistanceSquared((*iter)->getPosition()) <= distance*distance) { | 
|---|
| 142 | ComparisonList.insert(*iter); | 
|---|
| 143 | //std::cout << **iter << " is inside of " << center << " plus " << distance << "." << std::endl; | 
|---|
| 144 | } | 
|---|
| 145 | // check that we get at least as many as needed | 
|---|
| 146 | CPPUNIT_ASSERT(ComparisonList.size() <= NeighborList.size()); | 
|---|
| 147 |  | 
|---|
| 148 | // check element-wise and skip unrequired ones | 
|---|
| 149 | LinkedCell::LinkedList::iterator iter1 = ComparisonList.begin(); | 
|---|
| 150 | LinkedCell::LinkedList::iterator iter2 = NeighborList.begin(); | 
|---|
| 151 | for(;(iter1 != ComparisonList.end()) && (iter2 != NeighborList.end()); ++iter1, ++iter2) { | 
|---|
| 152 | while (*iter1 != *iter2) { | 
|---|
| 153 | CPPUNIT_ASSERT( iter2 != NeighborList.end() ); | 
|---|
| 154 | ++iter2; | 
|---|
| 155 | } | 
|---|
| 156 | //std::cout << **iter1 << " == " << **iter2 << std::endl; | 
|---|
| 157 | CPPUNIT_ASSERT( iter2 != NeighborList.end() ); | 
|---|
| 158 | } | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | /** UnitTest for getPointsInsideSphere() | 
|---|
| 162 | */ | 
|---|
| 163 | void LinkedCell_ViewTest::getPointsInsideSphereTest() | 
|---|
| 164 | { | 
|---|
| 165 | // define some center vector | 
|---|
| 166 | Vector center(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.); | 
|---|
| 167 |  | 
|---|
| 168 | // get LinkedList from LC | 
|---|
| 169 | const double distance = 3.; | 
|---|
| 170 | LinkedCell::LinkedList NeighborList = LC->getPointsInsideSphere(distance, center); | 
|---|
| 171 | //  for (LinkedCell::LinkedList::const_iterator iter = NeighborList.begin(); | 
|---|
| 172 | //      iter != NeighborList.end(); ++iter) | 
|---|
| 173 | //    std::cout << **iter << " is in returned restricted neighbor list." << std::endl; | 
|---|
| 174 |  | 
|---|
| 175 | // gather points from NodeList | 
|---|
| 176 | LinkedCell::LinkedList ComparisonList; | 
|---|
| 177 | for (PointSet::const_iterator iter = NodeList.begin(); iter != NodeList.end(); ++iter) | 
|---|
| 178 | if (center.DistanceSquared((*iter)->getPosition()) <= distance*distance) { | 
|---|
| 179 | ComparisonList.insert(*iter); | 
|---|
| 180 | //std::cout << **iter << " is inside of " << center << " plus " << distance << "." << std::endl; | 
|---|
| 181 | } | 
|---|
| 182 | // check that we get at least as many as needed | 
|---|
| 183 | CPPUNIT_ASSERT(ComparisonList.size() == NeighborList.size()); | 
|---|
| 184 |  | 
|---|
| 185 | // check element-wise and skip unrequired ones | 
|---|
| 186 | LinkedCell::LinkedList::iterator iter1 = ComparisonList.begin(); | 
|---|
| 187 | LinkedCell::LinkedList::iterator iter2 = NeighborList.begin(); | 
|---|
| 188 | for(;(iter1 != ComparisonList.end()) && (iter2 != NeighborList.end()); ++iter1, ++iter2) { | 
|---|
| 189 | //std::cout << **iter1 << " == " << **iter2 << std::endl; | 
|---|
| 190 | CPPUNIT_ASSERT( *iter1 == *iter2 ); | 
|---|
| 191 | } | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 | LinkedCell::LinkedCell_View returnView(LinkedCell::LinkedCell_View &view) | 
|---|
| 196 | { | 
|---|
| 197 | return view; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | LinkedCell::LinkedCell_View returnCopiedView(LinkedCell::LinkedCell_View &view) | 
|---|
| 201 | { | 
|---|
| 202 | LinkedCell::LinkedCell_View retview(view); | 
|---|
| 203 | return retview; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | /** UnitTest on whether counting in RAIIMap works | 
|---|
| 207 | */ | 
|---|
| 208 | void LinkedCell_ViewTest::RAIIMapTest() | 
|---|
| 209 | { | 
|---|
| 210 | CPPUNIT_ASSERT_EQUAL( (size_t)1, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 211 |  | 
|---|
| 212 | // check that we are present | 
|---|
| 213 | LinkedCell::LinkedCell_View::ModelInstanceMap::iterator iter = | 
|---|
| 214 | LinkedCell::LinkedCell_View::RAIIMap.find(LC); | 
|---|
| 215 | CPPUNIT_ASSERT( iter != LinkedCell::LinkedCell_View::RAIIMap.end() ); | 
|---|
| 216 | CPPUNIT_ASSERT( *iter == LC ); | 
|---|
| 217 |  | 
|---|
| 218 | // check that we are the only value present | 
|---|
| 219 | ++iter; | 
|---|
| 220 | CPPUNIT_ASSERT( iter == LinkedCell::LinkedCell_View::RAIIMap.end() ); | 
|---|
| 221 |  | 
|---|
| 222 | // add another view and check that there is not assertion | 
|---|
| 223 | LinkedCell::LinkedCell_View *view = NULL; | 
|---|
| 224 | CPPUNIT_ASSERT_NO_THROW( view = new LinkedCell::LinkedCell_View(*LCImpl) ); | 
|---|
| 225 | CPPUNIT_ASSERT_EQUAL( (size_t)2, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 226 | delete view; | 
|---|
| 227 | CPPUNIT_ASSERT_EQUAL( (size_t)1, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 228 |  | 
|---|
| 229 | // copy current view | 
|---|
| 230 | { | 
|---|
| 231 | LinkedCell::LinkedCell_View view(*LC); | 
|---|
| 232 | CPPUNIT_ASSERT_EQUAL( (size_t)2, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 233 | LinkedCell::LinkedCell_View view2 = *LC; | 
|---|
| 234 | CPPUNIT_ASSERT_EQUAL( (size_t)3, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 235 | } | 
|---|
| 236 | CPPUNIT_ASSERT_EQUAL( (size_t)1, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 237 |  | 
|---|
| 238 | // with function returning view | 
|---|
| 239 | { | 
|---|
| 240 | LinkedCell::LinkedCell_View view = returnView(*LC); | 
|---|
| 241 | CPPUNIT_ASSERT_EQUAL( (size_t)2, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 242 | } | 
|---|
| 243 | CPPUNIT_ASSERT_EQUAL( (size_t)1, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 244 |  | 
|---|
| 245 | // with function returning copied view | 
|---|
| 246 | { | 
|---|
| 247 | LinkedCell::LinkedCell_View view = returnCopiedView(*LC); | 
|---|
| 248 | CPPUNIT_ASSERT_EQUAL( (size_t)2, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 249 | } | 
|---|
| 250 | CPPUNIT_ASSERT_EQUAL( (size_t)1, LinkedCell::LinkedCell_View::RAIIMap.size() ); | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|