| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2011 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 |
|
|---|
| 39 | #include "LinkedCell_ViewUnitTest.hpp"
|
|---|
| 40 |
|
|---|
| 41 | #ifdef HAVE_TESTRUNNER
|
|---|
| 42 | #include "UnitTestMain.hpp"
|
|---|
| 43 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 44 |
|
|---|
| 45 | /********************************************** Test classes **************************************/
|
|---|
| 46 |
|
|---|
| 47 | #define DOMAINLENGTH 20.
|
|---|
| 48 | #define EDGELENGTH 2.
|
|---|
| 49 | #define NUMBERCELLS DOMAINLENGTH/EDGELENGTH
|
|---|
| 50 |
|
|---|
| 51 | // Registers the fixture into the 'registry'
|
|---|
| 52 | CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCell_ViewTest );
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | void LinkedCell_ViewTest::setUp()
|
|---|
| 56 | {
|
|---|
| 57 | // failing asserts should be thrown
|
|---|
| 58 | ASSERT_DO(Assert::Throw);
|
|---|
| 59 |
|
|---|
| 60 | //setVerbosity(3);
|
|---|
| 61 |
|
|---|
| 62 | // create diag(20.) matrix
|
|---|
| 63 | RealSpaceMatrix BoxM;
|
|---|
| 64 | BoxM.setIdentity();
|
|---|
| 65 | BoxM *= 20.;
|
|---|
| 66 |
|
|---|
| 67 | // create Box with this matrix
|
|---|
| 68 | domain = new Box(BoxM);
|
|---|
| 69 |
|
|---|
| 70 | // create LinkedCell structure with this Box
|
|---|
| 71 | LCImpl = new LinkedCell::LinkedCell_Model(EDGELENGTH, *domain);
|
|---|
| 72 |
|
|---|
| 73 | // create a list of nodes and add to LCImpl
|
|---|
| 74 | std::vector< Vector > VectorList;
|
|---|
| 75 | for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i)
|
|---|
| 76 | VectorList.push_back(Vector((double)i*EDGELENGTH,(double)i*EDGELENGTH,(double)i*EDGELENGTH));
|
|---|
| 77 | for (size_t i=0;i<VectorList.size();++i) {
|
|---|
| 78 | TesselPoint * Walker = new TesselPoint();
|
|---|
| 79 | Walker->setName(std::string("Walker")+toString(i));
|
|---|
| 80 | Walker->setPosition(VectorList[i]);
|
|---|
| 81 | NodeList.insert(Walker);
|
|---|
| 82 | LCImpl->addNode(Walker);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // create LinkedCell_View from that
|
|---|
| 86 | LC = new LinkedCell::LinkedCell_View(*LCImpl);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | void LinkedCell_ViewTest::tearDown()
|
|---|
| 91 | {
|
|---|
| 92 | delete LC;
|
|---|
| 93 | delete LCImpl;
|
|---|
| 94 | delete domain;
|
|---|
| 95 |
|
|---|
| 96 | // remove all nodes again
|
|---|
| 97 | for (PointSet::iterator iter = NodeList.begin();
|
|---|
| 98 | !NodeList.empty();
|
|---|
| 99 | iter = NodeList.begin()) {
|
|---|
| 100 | delete *iter;
|
|---|
| 101 | NodeList.erase(iter);
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | /** UnitTest for getAllNeighbors()
|
|---|
| 107 | */
|
|---|
| 108 | void LinkedCell_ViewTest::getAllNeighborsTest()
|
|---|
| 109 | {
|
|---|
| 110 | // define some center vector
|
|---|
| 111 | Vector center(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.);
|
|---|
| 112 |
|
|---|
| 113 | // get LinkedList from LC
|
|---|
| 114 | const double distance = 2.;
|
|---|
| 115 | LinkedCell::LinkedList NeighborList = LC->getAllNeighbors(distance, center);
|
|---|
| 116 | // for (LinkedCell::LinkedList::const_iterator iter = NeighborList.begin();
|
|---|
| 117 | // iter != NeighborList.end(); ++iter)
|
|---|
| 118 | // std::cout << **iter << " is in returned neighbor list." << std::endl;
|
|---|
| 119 |
|
|---|
| 120 | // gather points from NodeList
|
|---|
| 121 | LinkedCell::LinkedList ComparisonList;
|
|---|
| 122 | for (PointSet::const_iterator iter = NodeList.begin(); iter != NodeList.end(); ++iter)
|
|---|
| 123 | if (center.DistanceSquared((*iter)->getPosition()) <= distance*distance) {
|
|---|
| 124 | ComparisonList.push_back(*iter);
|
|---|
| 125 | //std::cout << **iter << " is inside of " << center << " plus " << distance << "." << std::endl;
|
|---|
| 126 | }
|
|---|
| 127 | // check that we get at least as many as needed
|
|---|
| 128 | CPPUNIT_ASSERT(ComparisonList.size() <= NeighborList.size());
|
|---|
| 129 |
|
|---|
| 130 | // sort lists to ease comparison
|
|---|
| 131 | ComparisonList.sort();
|
|---|
| 132 | NeighborList.sort();
|
|---|
| 133 |
|
|---|
| 134 | // check element-wise and skip unrequired ones
|
|---|
| 135 | LinkedCell::LinkedList::iterator iter1 = ComparisonList.begin();
|
|---|
| 136 | LinkedCell::LinkedList::iterator iter2 = NeighborList.begin();
|
|---|
| 137 | for(;(iter1 != ComparisonList.end()) && (iter2 != NeighborList.end()); ++iter1, ++iter2) {
|
|---|
| 138 | while (*iter1 != *iter2)
|
|---|
| 139 | ++iter2;
|
|---|
| 140 | //std::cout << **iter1 << " == " << **iter2 << std::endl;
|
|---|
| 141 | CPPUNIT_ASSERT( iter2 != NeighborList.end() );
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** UnitTest for getPointsInsideSphere()
|
|---|
| 146 | */
|
|---|
| 147 | void LinkedCell_ViewTest::getPointsInsideSphereTest()
|
|---|
| 148 | {
|
|---|
| 149 | // define some center vector
|
|---|
| 150 | Vector center(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.);
|
|---|
| 151 |
|
|---|
| 152 | // get LinkedList from LC
|
|---|
| 153 | const double distance = 2.;
|
|---|
| 154 | LinkedCell::LinkedList NeighborList = LC->getPointsInsideSphere(distance, center);
|
|---|
| 155 | // for (LinkedCell::LinkedList::const_iterator iter = NeighborList.begin();
|
|---|
| 156 | // iter != NeighborList.end(); ++iter)
|
|---|
| 157 | // std::cout << **iter << " is in returned restricted neighbor list." << std::endl;
|
|---|
| 158 |
|
|---|
| 159 | // gather points from NodeList
|
|---|
| 160 | LinkedCell::LinkedList ComparisonList;
|
|---|
| 161 | for (PointSet::const_iterator iter = NodeList.begin(); iter != NodeList.end(); ++iter)
|
|---|
| 162 | if (center.DistanceSquared((*iter)->getPosition()) <= distance*distance) {
|
|---|
| 163 | ComparisonList.push_back(*iter);
|
|---|
| 164 | //std::cout << **iter << " is inside of " << center << " plus " << distance << "." << std::endl;
|
|---|
| 165 | }
|
|---|
| 166 | // check that we get at least as many as needed
|
|---|
| 167 | CPPUNIT_ASSERT(ComparisonList.size() == NeighborList.size());
|
|---|
| 168 |
|
|---|
| 169 | // sort lists to ease comparison
|
|---|
| 170 | ComparisonList.sort();
|
|---|
| 171 | NeighborList.sort();
|
|---|
| 172 |
|
|---|
| 173 | // check element-wise and skip unrequired ones
|
|---|
| 174 | LinkedCell::LinkedList::iterator iter1 = ComparisonList.begin();
|
|---|
| 175 | LinkedCell::LinkedList::iterator iter2 = NeighborList.begin();
|
|---|
| 176 | for(;(iter1 != ComparisonList.end()) && (iter2 != NeighborList.end()); ++iter1, ++iter2) {
|
|---|
| 177 | //std::cout << **iter1 << " == " << **iter2 << std::endl;
|
|---|
| 178 | CPPUNIT_ASSERT( *iter1 == *iter2 );
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|