| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2013 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 | * PartialNucleiChargeFitterUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: May 12, 2013 | 
|---|
| 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 "PartialNucleiChargeFitterUnitTest.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | #include <boost/assign.hpp> | 
|---|
| 44 | #include <vector> | 
|---|
| 45 |  | 
|---|
| 46 | #include "CodePatterns/Assert.hpp" | 
|---|
| 47 | #include "LinearAlgebra/MatrixContent.hpp" | 
|---|
| 48 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp" | 
|---|
| 51 | #include "Potentials/PartialNucleiChargeFitter.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | using namespace boost::assign; | 
|---|
| 54 |  | 
|---|
| 55 | #ifdef HAVE_TESTRUNNER | 
|---|
| 56 | #include "UnitTestMain.hpp" | 
|---|
| 57 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 58 |  | 
|---|
| 59 | /********************************************** Test classes **************************************/ | 
|---|
| 60 |  | 
|---|
| 61 | #define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3)) | 
|---|
| 62 | #define DOMAINVOLUME(l) (size_t)pow(l,3) | 
|---|
| 63 |  | 
|---|
| 64 | // Registers the fixture into the 'registry' | 
|---|
| 65 | CPPUNIT_TEST_SUITE_REGISTRATION( PartialNucleiChargeFitterTest ); | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | void PartialNucleiChargeFitterTest::setUp() | 
|---|
| 69 | { | 
|---|
| 70 | // failing asserts should be thrown | 
|---|
| 71 | ASSERT_DO(Assert::Throw); | 
|---|
| 72 |  | 
|---|
| 73 | // create the grid with domain [0:1]^3 | 
|---|
| 74 | const double begin[3] = { 0., 0., 0. }; | 
|---|
| 75 | const double end[3] = { 1., 1., 1. }; | 
|---|
| 76 | SamplingGrid::sampledvalues_t values; | 
|---|
| 77 | for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i) | 
|---|
| 78 | values += 0.; | 
|---|
| 79 | SamplingGrid grid(begin, end, 2, values); | 
|---|
| 80 |  | 
|---|
| 81 | // create position(s) in center of domain | 
|---|
| 82 | PartialNucleiChargeFitter::positions_t positions(1, Vector(.5,.5,.5)); | 
|---|
| 83 | fitter = new PartialNucleiChargeFitter(grid, positions); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 | void PartialNucleiChargeFitterTest::tearDown() | 
|---|
| 88 | { | 
|---|
| 89 | delete fitter; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | /** UnitTest for constructMatrix() | 
|---|
| 93 | */ | 
|---|
| 94 | void PartialNucleiChargeFitterTest::constructMatrixTest() | 
|---|
| 95 | { | 
|---|
| 96 | // construct potential | 
|---|
| 97 | MatrixContent testMatrix(DOMAINVOLUME(1)*NUMBEROFSAMPLES(2), 1); | 
|---|
| 98 | Vector center(.5,.5,.5); | 
|---|
| 99 | const double delta = 1./pow(2,2); | 
|---|
| 100 | Vector N; | 
|---|
| 101 | size_t index=0; | 
|---|
| 102 | for(N[0]=0.; N[0] < 1.; N[0]+=delta) | 
|---|
| 103 | for(N[1]=0.; N[1] < 1.; N[1]+=delta) | 
|---|
| 104 | for(N[2]=0.; N[2] < 1.; N[2]+=delta) { | 
|---|
| 105 | const double distance = N.distance(center); | 
|---|
| 106 | if (fabs(distance) > std::numeric_limits<double>::epsilon()*1e4 ) | 
|---|
| 107 | testMatrix.at(index++, 0) = 1./(distance); | 
|---|
| 108 | else | 
|---|
| 109 | testMatrix.at(index++, 0) = 0.; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | // check whether potential matrix is fine | 
|---|
| 113 | fitter->constructMatrix(); | 
|---|
| 114 | const MatrixContent &ProblemMatrix = fitter->getMatrix(); | 
|---|
| 115 | CPPUNIT_ASSERT_EQUAL( testMatrix, ProblemMatrix ); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | /** UnitTest for operator() | 
|---|
| 119 | */ | 
|---|
| 120 | void PartialNucleiChargeFitterTest::operatorTest() | 
|---|
| 121 | { | 
|---|
| 122 | // solution is zero everywhere | 
|---|
| 123 | PartialNucleiChargeFitter::charges_t charges(1, 0.); | 
|---|
| 124 |  | 
|---|
| 125 | // true solution is ... | 
|---|
| 126 | (*fitter)(); | 
|---|
| 127 | PartialNucleiChargeFitter::charges_t return_charges = fitter->getSolutionAsCharges_t(); | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL(charges, return_charges); | 
|---|
| 129 | } | 
|---|