| 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_ControllerUnitTest.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Nov 29, 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 "Atom/atom.hpp"
 | 
|---|
| 42 | #include "Box.hpp"
 | 
|---|
| 43 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 44 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 45 | #include "LinkedCell/LinkedCell_Controller.hpp"
 | 
|---|
| 46 | #include "LinkedCell/LinkedCell_View.hpp"
 | 
|---|
| 47 | #include "LinkedCell/LinkedCell_View_ModelWrapper.hpp"
 | 
|---|
| 48 | #include "LinkedCell/unittests/defs.hpp"
 | 
|---|
| 49 | #include "LinkedCell/PointCloudAdaptor.hpp"
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #include "LinkedCell_ControllerUnitTest.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 54 | #include "UnitTestMain.hpp"
 | 
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /********************************************** Test classes **************************************/
 | 
|---|
| 58 | 
 | 
|---|
| 59 | // Registers the fixture into the 'registry'
 | 
|---|
| 60 | CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCell_ControllerTest );
 | 
|---|
| 61 | 
 | 
|---|
| 62 | 
 | 
|---|
| 63 | void LinkedCell_ControllerTest::setUp()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   // failing asserts should be thrown
 | 
|---|
| 66 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   // create diag(20.) matrix
 | 
|---|
| 69 |   BoxM = new RealSpaceMatrix;
 | 
|---|
| 70 |   BoxM->setIdentity();
 | 
|---|
| 71 |   (*BoxM) *= DOMAINLENGTH;
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   // create Box with this matrix
 | 
|---|
| 74 |   domain = new Box(*BoxM);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   controller = new LinkedCell::LinkedCell_Controller(*domain);
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   // create empty set
 | 
|---|
| 79 |   emptyset = new PointCloudAdaptor< std::vector<atom *> >(&emptyvector, std::string("emptyset"));
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | 
 | 
|---|
| 83 | void LinkedCell_ControllerTest::tearDown()
 | 
|---|
| 84 | {
 | 
|---|
| 85 |   delete controller;
 | 
|---|
| 86 |   delete domain;
 | 
|---|
| 87 |   delete emptyset;
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | /** UnitTest for LinkedCell_Controller's lower and upper thresholds.
 | 
|---|
| 91 |  */
 | 
|---|
| 92 | void LinkedCell_ControllerTest::thresholdTest()
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   /// re-create instances
 | 
|---|
| 95 |   delete controller;
 | 
|---|
| 96 |   delete domain;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   /// create diag(..) matrix beyond upper_threshold
 | 
|---|
| 99 |   const double old_threshold = controller->upper_threshold;
 | 
|---|
| 100 |   controller->lower_threshold = old_threshold*0.9;
 | 
|---|
| 101 |   RealSpaceMatrix BoxM;
 | 
|---|
| 102 |   BoxM.setIdentity();
 | 
|---|
| 103 |   BoxM *= controller->upper_threshold*.5;
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   /// create Box with this matrix
 | 
|---|
| 106 |   domain = new Box(BoxM);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   controller = new LinkedCell::LinkedCell_Controller(*domain);
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   /// check that thresholds have been adapted
 | 
|---|
| 111 |   CPPUNIT_ASSERT( controller->upper_threshold != old_threshold );
 | 
|---|
| 112 |   CPPUNIT_ASSERT( controller->lower_threshold != old_threshold*0.9 );
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | /** UnitTest for LinkedCell_Controller::getHeuristicRange().
 | 
|---|
| 116 |  */
 | 
|---|
| 117 | void LinkedCell_ControllerTest::getHeuristicRangeTest()
 | 
|---|
| 118 | {
 | 
|---|
| 119 |   /// re-implementing function to check is nonsense here, instead try some
 | 
|---|
| 120 |   /// hard-coded, working values;
 | 
|---|
| 121 |   controller->lower_threshold = 1.;
 | 
|---|
| 122 |   controller->upper_threshold = 20.;
 | 
|---|
| 123 |   const double inbetween = 9.5; // half and twice is definitely within both thresholds.
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   /// check distance in between
 | 
|---|
| 126 |   range<double> interval = controller->getHeuristicRange(inbetween);
 | 
|---|
| 127 |   CPPUNIT_ASSERT ( interval.first != controller->lower_threshold );
 | 
|---|
| 128 |   CPPUNIT_ASSERT ( interval.last != controller->upper_threshold );
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | /** UnitTest for LinkedCell_Controller::getViewTest() for getting twice the same view.
 | 
|---|
| 132 |  */
 | 
|---|
| 133 | void LinkedCell_ControllerTest::getView_SameViewTest()
 | 
|---|
| 134 | {
 | 
|---|
| 135 |   /// obtain a view
 | 
|---|
| 136 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() );
 | 
|---|
| 137 |   LinkedCell::LinkedCell_View view = controller->getView(2., *emptyset);
 | 
|---|
| 138 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   {
 | 
|---|
| 141 |     /// get same view again and check that now new instance appears
 | 
|---|
| 142 |     LinkedCell::LinkedCell_View view_again = controller->getView(2., *emptyset);
 | 
|---|
| 143 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 144 |   }
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking two different views.
 | 
|---|
| 148 |  */
 | 
|---|
| 149 | void LinkedCell_ControllerTest::getView_DifferentViewTest()
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   /// obtain a view
 | 
|---|
| 152 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() );
 | 
|---|
| 153 |   LinkedCell::LinkedCell_View view = controller->getView(2., *emptyset);
 | 
|---|
| 154 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   {
 | 
|---|
| 157 |     /// pick another view that is not close enough
 | 
|---|
| 158 |     LinkedCell::LinkedCell_View view_other = controller->getView(5., *emptyset);
 | 
|---|
| 159 |     CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() );
 | 
|---|
| 160 |   }
 | 
|---|
| 161 | }
 | 
|---|
| 162 | 
 | 
|---|
| 163 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking further views in range of present one.
 | 
|---|
| 164 |  */
 | 
|---|
| 165 | void LinkedCell_ControllerTest::getView_InRangeViewTest()
 | 
|---|
| 166 | {
 | 
|---|
| 167 |   /// obtain a view
 | 
|---|
| 168 |   const double edgelength = 2.;
 | 
|---|
| 169 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() );
 | 
|---|
| 170 |   LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset);
 | 
|---|
| 171 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 172 | 
 | 
|---|
| 173 |   /// pick views that are close enough
 | 
|---|
| 174 |   range<double> interval = controller->getHeuristicRange(edgelength);
 | 
|---|
| 175 |   {
 | 
|---|
| 176 |     /// ... at half lower interval half
 | 
|---|
| 177 |     LinkedCell::LinkedCell_View view_lowerhalf = controller->getView((edgelength + interval.first)/2., *emptyset);
 | 
|---|
| 178 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 179 |   }
 | 
|---|
| 180 |   {
 | 
|---|
| 181 |     /// ... at half upper interval half
 | 
|---|
| 182 |     LinkedCell::LinkedCell_View view_upperhalf = controller->getView((interval.last + edgelength)/2., *emptyset);
 | 
|---|
| 183 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 184 |   }
 | 
|---|
| 185 |   {
 | 
|---|
| 186 |     /// ... close to lower boundary
 | 
|---|
| 187 |     LinkedCell::LinkedCell_View view_closelower = controller->getView(interval.first + std::numeric_limits<double>::round_error(), *emptyset);
 | 
|---|
| 188 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 189 |   }
 | 
|---|
| 190 |   {
 | 
|---|
| 191 |     /// ... close to upper boundary
 | 
|---|
| 192 |     LinkedCell::LinkedCell_View view_closerupper = controller->getView(interval.last - std::numeric_limits<double>::round_error(), *emptyset);
 | 
|---|
| 193 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 194 |   }
 | 
|---|
| 195 |   {
 | 
|---|
| 196 |     /// on lower boundary
 | 
|---|
| 197 |     LinkedCell::LinkedCell_View view_onlower = controller->getView(interval.first, *emptyset);
 | 
|---|
| 198 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 199 |   }
 | 
|---|
| 200 | }
 | 
|---|
| 201 | 
 | 
|---|
| 202 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking further views outside range.
 | 
|---|
| 203 |  */
 | 
|---|
| 204 | void LinkedCell_ControllerTest::getView_OutOfRangeViewTest()
 | 
|---|
| 205 | {
 | 
|---|
| 206 |   /// Here we need half of the edge length to be greater than lower_threshold
 | 
|---|
| 207 |   const double edgelength = 2.5;
 | 
|---|
| 208 |   CPPUNIT_ASSERT( (edgelength/2.) > controller->lower_threshold );
 | 
|---|
| 209 |   /// obtain a view
 | 
|---|
| 210 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() );
 | 
|---|
| 211 |   LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset);
 | 
|---|
| 212 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 213 | 
 | 
|---|
| 214 |   /// pick views that are not close enough and check for new instance
 | 
|---|
| 215 |   range<double> interval = controller->getHeuristicRange(edgelength);
 | 
|---|
| 216 |   {
 | 
|---|
| 217 |     /// ... outside lower boundary
 | 
|---|
| 218 |     LinkedCell::LinkedCell_View view_outsidelower = controller->getView(interval.first - std::numeric_limits<double>::round_error(), *emptyset);
 | 
|---|
| 219 |     CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() );
 | 
|---|
| 220 |   }
 | 
|---|
| 221 |   {
 | 
|---|
| 222 |     /// ... on upper boundary
 | 
|---|
| 223 |     LinkedCell::LinkedCell_View view_onupper = controller->getView(interval.last, *emptyset);
 | 
|---|
| 224 |     CPPUNIT_ASSERT_EQUAL( (size_t)3, controller->ModelsMap.size() );
 | 
|---|
| 225 |   }
 | 
|---|
| 226 | }
 | 
|---|
| 227 | 
 | 
|---|
| 228 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking views beneath lower threshold.
 | 
|---|
| 229 |  */
 | 
|---|
| 230 | void LinkedCell_ControllerTest::getView_LowerThresholdViewTest()
 | 
|---|
| 231 | {
 | 
|---|
| 232 |   /// obtain a view
 | 
|---|
| 233 |   const double edgelength = 1.9*controller->lower_threshold;
 | 
|---|
| 234 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() );
 | 
|---|
| 235 |   LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset);
 | 
|---|
| 236 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 237 | 
 | 
|---|
| 238 |   {
 | 
|---|
| 239 |     /// get a view at threshold and check that no new instance has been created
 | 
|---|
| 240 |     LinkedCell::LinkedCell_View view_onlower = controller->getView(controller->lower_threshold, *emptyset);
 | 
|---|
| 241 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 242 |   }
 | 
|---|
| 243 |   {
 | 
|---|
| 244 |     /// pick a view below 1.
 | 
|---|
| 245 |     LinkedCell::LinkedCell_View view_beneathlower = controller->getView(0.1*controller->lower_threshold, *emptyset);
 | 
|---|
| 246 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 247 |   }
 | 
|---|
| 248 | }
 | 
|---|
| 249 | 
 | 
|---|
| 250 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking views above upper threshold.
 | 
|---|
| 251 |  */
 | 
|---|
| 252 | void LinkedCell_ControllerTest::getView_UpperThresholdViewTest()
 | 
|---|
| 253 | {
 | 
|---|
| 254 |   /// obtain a view
 | 
|---|
| 255 |   const double edgelength = controller->upper_threshold;
 | 
|---|
| 256 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() );
 | 
|---|
| 257 |   LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset);
 | 
|---|
| 258 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 259 | 
 | 
|---|
| 260 |   {
 | 
|---|
| 261 |     /// get a view beyond threshold and check that no new instance has been created
 | 
|---|
| 262 |     LinkedCell::LinkedCell_View view_beyondupper = controller->getView(1.1*controller->upper_threshold, *emptyset);
 | 
|---|
| 263 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 264 |   }
 | 
|---|
| 265 | 
 | 
|---|
| 266 |   {
 | 
|---|
| 267 |     /// pick a view below threshold and check for new instance (if we make it outside acceptable range)
 | 
|---|
| 268 |     range<double> interval = controller->getHeuristicRange(edgelength);
 | 
|---|
| 269 |     if ( !interval.isInRange(0.1*controller->upper_threshold) ) {
 | 
|---|
| 270 |       LinkedCell::LinkedCell_View view_beneathupper = controller->getView(0.1*controller->upper_threshold, *emptyset);
 | 
|---|
| 271 |       CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() );
 | 
|---|
| 272 |     }
 | 
|---|
| 273 |   }
 | 
|---|
| 274 | }
 | 
|---|
| 275 | 
 | 
|---|
| 276 | /** UnitTest for LinkedCell_Controller::updateModels().
 | 
|---|
| 277 |  */
 | 
|---|
| 278 | void LinkedCell_ControllerTest::updateBoxTest()
 | 
|---|
| 279 | {
 | 
|---|
| 280 |   /// obtain a view
 | 
|---|
| 281 |   const double edgelength = controller->upper_threshold;
 | 
|---|
| 282 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() );
 | 
|---|
| 283 |   LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset);
 | 
|---|
| 284 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() );
 | 
|---|
| 285 |   const LinkedCell::LinkedCell_Model * const model = view.LC->getModel();
 | 
|---|
| 286 | 
 | 
|---|
| 287 |   /// change box matrix
 | 
|---|
| 288 |   domain->setM(*BoxM);
 | 
|---|
| 289 | 
 | 
|---|
| 290 |   /// check that model has changed
 | 
|---|
| 291 |   CPPUNIT_ASSERT( model != view.LC->getModel() );
 | 
|---|
| 292 | }
 | 
|---|