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