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