| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2017 Frederik Heber. 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 | * BondVectorsUnitTest.cpp
|
|---|
| 25 | *
|
|---|
| 26 | * Created on: Jun 29, 2017
|
|---|
| 27 | * Author: heber
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | // include config.h
|
|---|
| 31 | #ifdef HAVE_CONFIG_H
|
|---|
| 32 | #include <config.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 36 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 37 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "CodePatterns/Assert.hpp"
|
|---|
| 40 | #include "CodePatterns/Log.hpp"
|
|---|
| 41 |
|
|---|
| 42 | #include <boost/assign.hpp>
|
|---|
| 43 |
|
|---|
| 44 | #include "BondVectorsUnitTest.hpp"
|
|---|
| 45 |
|
|---|
| 46 | #include "Atom/atom.hpp"
|
|---|
| 47 | #include "Bond/bond.hpp"
|
|---|
| 48 | #include "Dynamics/BondVectors.hpp"
|
|---|
| 49 | #include "Element/periodentafel.hpp"
|
|---|
| 50 | #include "World.hpp"
|
|---|
| 51 | #include "WorldTime.hpp"
|
|---|
| 52 |
|
|---|
| 53 | #ifdef HAVE_TESTRUNNER
|
|---|
| 54 | #include "UnitTestMain.hpp"
|
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 56 |
|
|---|
| 57 | using namespace boost::assign;
|
|---|
| 58 |
|
|---|
| 59 | /********************************************** Test classes **************************************/
|
|---|
| 60 |
|
|---|
| 61 | // Registers the fixture into the 'registry'
|
|---|
| 62 | CPPUNIT_TEST_SUITE_REGISTRATION( BondVectorsTest );
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | void BondVectorsTest::setUp()
|
|---|
| 66 | {
|
|---|
| 67 | // failing asserts should be thrown
|
|---|
| 68 | ASSERT_DO(Assert::Throw);
|
|---|
| 69 |
|
|---|
| 70 | // create an atom
|
|---|
| 71 | carbon = World::getInstance().getPeriode()->FindElement(6);
|
|---|
| 72 | CPPUNIT_ASSERT(carbon != NULL);
|
|---|
| 73 |
|
|---|
| 74 | _atom = World::getInstance().createAtom();
|
|---|
| 75 | _atom->setType(carbon);
|
|---|
| 76 | _atom->setPosition( zeroVec );
|
|---|
| 77 | atoms.push_back(_atom);
|
|---|
| 78 | _atom = World::getInstance().createAtom();
|
|---|
| 79 | _atom->setType(carbon);
|
|---|
| 80 | _atom->setPosition( Vector(1.6,0.,0.) );
|
|---|
| 81 | atoms.push_back(_atom);
|
|---|
| 82 | _atom = World::getInstance().createAtom();
|
|---|
| 83 | _atom->setType(carbon);
|
|---|
| 84 | _atom->setPosition( Vector(3.2,0.,0.) );
|
|---|
| 85 | atoms.push_back(_atom);
|
|---|
| 86 | _atom = World::getInstance().createAtom();
|
|---|
| 87 | _atom->setType(carbon);
|
|---|
| 88 | _atom->setPosition( Vector(1.6,1.6,0.) );
|
|---|
| 89 | atoms.push_back(_atom);
|
|---|
| 90 | _atom = World::getInstance().createAtom();
|
|---|
| 91 | _atom->setType(carbon);
|
|---|
| 92 | _atom->setPosition( Vector(2.8,2.8,0.) );
|
|---|
| 93 | atoms.push_back(_atom);
|
|---|
| 94 | _atom = World::getInstance().createAtom();
|
|---|
| 95 | _atom->setType(carbon);
|
|---|
| 96 | _atom->setPosition( Vector(1.6,-1.6,0.) );
|
|---|
| 97 | atoms.push_back(_atom);
|
|---|
| 98 | _atom = World::getInstance().createAtom();
|
|---|
| 99 | _atom->setType(carbon);
|
|---|
| 100 | _atom->setPosition( Vector(2.8,-1.2,0.) );
|
|---|
| 101 | atoms.push_back(_atom);
|
|---|
| 102 |
|
|---|
| 103 | bv = new BondVectors;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static void clearbondvector(
|
|---|
| 107 | std::vector<bond::ptr> &_bondvector)
|
|---|
| 108 | {
|
|---|
| 109 | // remove bonds
|
|---|
| 110 | for (std::vector<bond::ptr>::iterator iter = _bondvector.begin();
|
|---|
| 111 | !_bondvector.empty(); iter = _bondvector.begin()) {
|
|---|
| 112 | (*iter)->leftatom->removeBond((*iter)->rightatom);
|
|---|
| 113 | _bondvector.erase(iter);
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 | void BondVectorsTest::tearDown()
|
|---|
| 119 | {
|
|---|
| 120 | delete bv;
|
|---|
| 121 |
|
|---|
| 122 | atoms.clear();
|
|---|
| 123 | atomvector.clear();
|
|---|
| 124 | clearbondvector(bondvector);
|
|---|
| 125 | carbon = NULL;
|
|---|
| 126 |
|
|---|
| 127 | World::purgeInstance();
|
|---|
| 128 | WorldTime::purgeInstance();
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** Test whether current_mapped is kept up-to-date
|
|---|
| 132 | *
|
|---|
| 133 | */
|
|---|
| 134 | void BondVectorsTest::current_mappedTest()
|
|---|
| 135 | {
|
|---|
| 136 | {
|
|---|
| 137 | // gather atoms
|
|---|
| 138 | atomvector += atoms[center], atoms[left];
|
|---|
| 139 | // create bonds
|
|---|
| 140 | bondvector += atoms[center]->addBond(atoms[left]);
|
|---|
| 141 | // prepare bondvectors
|
|---|
| 142 | bv->setFromAtomRange< std::vector<atom *> >(atomvector.begin(), atomvector.end(), WorldTime::getTime());
|
|---|
| 143 | // get bond vectors
|
|---|
| 144 | const std::vector<Vector> Bondvectors =
|
|---|
| 145 | bv->getAtomsBondVectorsAtStep(*atoms[center], WorldTime::getTime());
|
|---|
| 146 | // check number of bond vectors
|
|---|
| 147 | CPPUNIT_ASSERT_EQUAL( Bondvectors.size(), (size_t)1 );
|
|---|
| 148 | // check norm of bond vector
|
|---|
| 149 | CPPUNIT_ASSERT( fabs(Bondvectors[0].Norm() - 1.) < MYEPSILON );
|
|---|
| 150 |
|
|---|
| 151 | // clear set of atoms and use a different one
|
|---|
| 152 | clearbondvector(bondvector);
|
|---|
| 153 | atomvector.clear();
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | {
|
|---|
| 157 | // gather atoms
|
|---|
| 158 | atomvector += atoms[center], atoms[left], atoms[right], atoms[top];
|
|---|
| 159 | // create bonds
|
|---|
| 160 | bondvector +=
|
|---|
| 161 | atoms[center]->addBond(atoms[left]),
|
|---|
| 162 | atoms[center]->addBond(atoms[right]),
|
|---|
| 163 | atoms[center]->addBond(atoms[top]);
|
|---|
| 164 | // prepare bondvectors
|
|---|
| 165 | bv->setFromAtomRange< std::vector<atom *> >(atomvector.begin(), atomvector.end(), WorldTime::getTime());
|
|---|
| 166 | // get bond vectors
|
|---|
| 167 | const std::vector<Vector> Bondvectors =
|
|---|
| 168 | bv->getAtomsBondVectorsAtStep(*atoms[center], WorldTime::getTime());
|
|---|
| 169 | // check number of bond vectors
|
|---|
| 170 | CPPUNIT_ASSERT_EQUAL( Bondvectors.size(), (size_t)3 );
|
|---|
| 171 | // check norm of bond vector
|
|---|
| 172 | for (size_t i=0;i<3;++i)
|
|---|
| 173 | CPPUNIT_ASSERT( fabs(Bondvectors[i].Norm() - 1.) < MYEPSILON );
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /** Test whether calculating weights works on single bond
|
|---|
| 178 | *
|
|---|
| 179 | */
|
|---|
| 180 | void BondVectorsTest::weights_singlebondTest()
|
|---|
| 181 | {
|
|---|
| 182 | // gather atoms
|
|---|
| 183 | atomvector += atoms[center], atoms[left];
|
|---|
| 184 | // create bonds
|
|---|
| 185 | bondvector += atoms[center]->addBond(atoms[left]);
|
|---|
| 186 | // prepare bondvectors
|
|---|
| 187 | bv->setFromAtomRange< std::vector<atom *> >(atomvector.begin(), atomvector.end(), WorldTime::getTime());
|
|---|
| 188 | // calculate weights
|
|---|
| 189 | BondVectors::weights_t weights = bv->getWeightsForAtomAtStep(*atoms[center], WorldTime::getTime());
|
|---|
| 190 | LOG(2, "DEBUG: Single bond weights are " << weights);
|
|---|
| 191 | // check number of weights
|
|---|
| 192 | CPPUNIT_ASSERT_EQUAL( weights.size(), (size_t)1 );
|
|---|
| 193 | // check sum of weights
|
|---|
| 194 | const double weight_sum = std::accumulate(weights.begin(), weights.end(), 0.);
|
|---|
| 195 | CPPUNIT_ASSERT( fabs(weight_sum - 1.) < MYEPSILON );
|
|---|
| 196 | // check weight
|
|---|
| 197 | CPPUNIT_ASSERT_EQUAL( weights[0], 1. );
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /** Test whether calculating weights works on linear chain config
|
|---|
| 201 | *
|
|---|
| 202 | */
|
|---|
| 203 | void BondVectorsTest::weights_linearchainTest()
|
|---|
| 204 | {
|
|---|
| 205 | // gather atoms
|
|---|
| 206 | atomvector += atoms[center], atoms[left], atoms[right];
|
|---|
| 207 | // create bonds
|
|---|
| 208 | bondvector += atoms[center]->addBond(atoms[left]), atoms[center]->addBond(atoms[right]);
|
|---|
| 209 | // prepare bondvectors
|
|---|
| 210 | bv->setFromAtomRange< std::vector<atom *> >(atomvector.begin(), atomvector.end(), WorldTime::getTime());
|
|---|
| 211 | // calculate weights
|
|---|
| 212 | BondVectors::weights_t weights = bv->getWeightsForAtomAtStep(*atoms[center], WorldTime::getTime());
|
|---|
| 213 | LOG(2, "DEBUG: Linear chain weights are " << weights);
|
|---|
| 214 | // check number of weights
|
|---|
| 215 | CPPUNIT_ASSERT_EQUAL( weights.size(), (size_t)2 );
|
|---|
| 216 | // check sum of weights
|
|---|
| 217 | const double weight_sum = std::accumulate(weights.begin(), weights.end(), 0.);
|
|---|
| 218 | CPPUNIT_ASSERT( fabs(weight_sum - 1.) < 1e-10 );
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /** Test whether calculating weights works on right angle config
|
|---|
| 222 | *
|
|---|
| 223 | */
|
|---|
| 224 | void BondVectorsTest::weights_rightangleTest()
|
|---|
| 225 | {
|
|---|
| 226 | // gather atoms
|
|---|
| 227 | atomvector += atoms[center], atoms[left], atoms[top];
|
|---|
| 228 | // create bonds
|
|---|
| 229 | bondvector +=
|
|---|
| 230 | atoms[center]->addBond(atoms[left]),
|
|---|
| 231 | atoms[center]->addBond(atoms[top]);
|
|---|
| 232 | // prepare bondvectors
|
|---|
| 233 | bv->setFromAtomRange< std::vector<atom *> >(atomvector.begin(), atomvector.end(), WorldTime::getTime());
|
|---|
| 234 | // calculate weights
|
|---|
| 235 | BondVectors::weights_t weights = bv->getWeightsForAtomAtStep(*atoms[center], WorldTime::getTime());
|
|---|
| 236 | LOG(2, "DEBUG: Right angle weights are " << weights);
|
|---|
| 237 | // check number of weights
|
|---|
| 238 | CPPUNIT_ASSERT_EQUAL( weights.size(), (size_t)2 );
|
|---|
| 239 | // check sum of weights: two independent vectors == 1+1
|
|---|
| 240 | const double weight_sum = std::accumulate(weights.begin(), weights.end(), 0.);
|
|---|
| 241 | CPPUNIT_ASSERT( fabs(weight_sum - 2.) < 1e-10 );
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /** Test whether calculating weights works on triangle config
|
|---|
| 245 | *
|
|---|
| 246 | */
|
|---|
| 247 | void BondVectorsTest::weights_triangleTest()
|
|---|
| 248 | {
|
|---|
| 249 | // gather atoms
|
|---|
| 250 | atomvector += atoms[center], atoms[left], atoms[right], atoms[top];
|
|---|
| 251 | // create bonds
|
|---|
| 252 | bondvector +=
|
|---|
| 253 | atoms[center]->addBond(atoms[left]),
|
|---|
| 254 | atoms[center]->addBond(atoms[right]),
|
|---|
| 255 | atoms[center]->addBond(atoms[top]);
|
|---|
| 256 | // prepare bondvectors
|
|---|
| 257 | bv->setFromAtomRange< std::vector<atom *> >(atomvector.begin(), atomvector.end(), WorldTime::getTime());
|
|---|
| 258 | // calculate weights
|
|---|
| 259 | BondVectors::weights_t weights = bv->getWeightsForAtomAtStep(*atoms[center], WorldTime::getTime());
|
|---|
| 260 | LOG(2, "DEBUG: triangle weights are " << weights);
|
|---|
| 261 | // check number of weights
|
|---|
| 262 | CPPUNIT_ASSERT_EQUAL( weights.size(), (size_t)3 );
|
|---|
| 263 | // check sum of weights: one linear independent, two dependent vectors = 1 + 2*0.5
|
|---|
| 264 | const double weight_sum = std::accumulate(weights.begin(), weights.end(), 0.);
|
|---|
| 265 | CPPUNIT_ASSERT( fabs(weight_sum - 2.) < 1e-10 );
|
|---|
| 266 | }
|
|---|