source: src/Descriptors/unittests/AtomDescriptorUnitTest.cpp@ 990a62

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 990a62 was 94d5ac6, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: As we use GSL internally, we are as of now required to use GPL v2 license.

  • GNU Scientific Library is used at every place in the code, especially the sub-package LinearAlgebra is based on it which in turn is used really everywhere in the remainder of MoleCuilder. Hence, we have to use the GPL license for the whole of MoleCuilder. In effect, GPL's COPYING was present all along and stated the terms of the GPL v2 license.
  • Hence, I added the default GPL v2 disclaimer to every source file and removed the note about a (actually missing) LICENSE file.
  • also, I added a help-redistribute action which again gives the disclaimer of the GPL v2.
  • also, I changed in the disclaimer that is printed at every program start in builder_init.cpp.
  • TEST: Added check on GPL statement present in every module to test CodeChecks project-disclaimer.
  • Property mode set to 100644
File size: 11.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-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 * AtomDescriptorUnitTest.cpp
25 *
26 * Created on: Feb 9, 2010
27 * Author: crueger
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "AtomDescriptorUnitTest.hpp"
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40#include <iostream>
41
42#include <Descriptors/AtomDescriptor.hpp>
43#include <Descriptors/AtomIdDescriptor.hpp>
44#include <Descriptors/AtomOfMoleculeDescriptor.hpp>
45#include <Descriptors/AtomOrderDescriptor.hpp>
46#include <Descriptors/AtomsWithinDistanceOfDescriptor.hpp>
47
48#include "World.hpp"
49#include "Atom/atom.hpp"
50#include "molecule.hpp"
51#include "LinearAlgebra/Vector.hpp"
52
53#ifdef HAVE_TESTRUNNER
54#include "UnitTestMain.hpp"
55#endif /*HAVE_TESTRUNNER*/
56
57/********************************************** Test classes **************************************/
58// Registers the fixture into the 'registry'
59CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest );
60
61// set up and tear down
62void AtomDescriptorTest::setUp()
63{
64 World::getInstance();
65 for(int i=0;i<ATOM_COUNT;++i){
66 atoms[i]= World::getInstance().createAtom();
67 atomIds[i]= atoms[i]->getId();
68 }
69}
70
71void AtomDescriptorTest::tearDown()
72{
73 World::purgeInstance();
74}
75
76// some helper functions
77static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>())
78{
79 for(int i=0;i<ATOM_COUNT;++i){
80 atomId_t id = ids[i];
81 if(!excluded.count(id)){
82 std::vector<atom*>::iterator iter;
83 bool res=false;
84 for(iter=atoms.begin();iter!=atoms.end();++iter){
85 res |= (*iter)->getId() == id;
86 }
87 if(!res) {
88 cout << "Atom " << id << " missing in returned list" << endl;
89 return false;
90 }
91 }
92 }
93 return true;
94}
95
96static bool hasNoDuplicateAtoms(std::vector<atom*> atoms)
97{
98 std::set<atomId_t> found;
99 std::vector<atom*>::iterator iter;
100 for(iter=atoms.begin();iter!=atoms.end();++iter){
101 int id = (*iter)->getId();
102 if(found.count(id))
103 return false;
104 found.insert(id);
105 }
106 return true;
107}
108
109
110void AtomDescriptorTest::AtomBaseSetsTest()
111{
112 std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
113 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds));
114 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms));
115
116 std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms());
117 CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
118}
119
120void AtomDescriptorTest::AtomIdTest()
121{
122 // test Atoms from boundaries and middle of the set
123 atom* testAtom;
124 testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
125 CPPUNIT_ASSERT(testAtom);
126 CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
127 testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2]));
128 CPPUNIT_ASSERT(testAtom);
129 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
130 testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1]));
131 CPPUNIT_ASSERT(testAtom);
132 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
133
134 // find some ID that has not been created
135 atomId_t outsideId=0;
136 bool res = false;
137 for(outsideId=0;!res;++outsideId) {
138 res = true;
139 for(int i = 0; i < ATOM_COUNT; ++i){
140 res &= atomIds[i]!=outsideId;
141 }
142 }
143 // test from outside of set
144 testAtom = World::getInstance().getAtom(AtomById(outsideId));
145 CPPUNIT_ASSERT(!testAtom);
146}
147
148void AtomDescriptorTest::AtomOfMoleculeTest()
149{
150 // test Atoms from boundaries and middle of the set
151 atom* testAtom;
152 testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
153 CPPUNIT_ASSERT(testAtom);
154 CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
155
156 // create some molecule and associate atom to it
157 testAtom->setType(1);
158 molecule * newmol = World::getInstance().createMolecule();
159 newmol->AddAtom(testAtom);
160 CPPUNIT_ASSERT_EQUAL(newmol->getId(), testAtom->getMolecule()->getId());
161
162 // get atom by descriptor
163 World::AtomComposite atoms = World::getInstance().getAllAtoms(AtomOfMolecule(newmol->getId()));
164 CPPUNIT_ASSERT_EQUAL( (size_t)1, atoms.size() );
165 CPPUNIT_ASSERT_EQUAL( (*atoms.begin())->getId(), testAtom->getId() );
166
167 // remove molecule again
168 World::getInstance().destroyMolecule(newmol);
169}
170
171void AtomDescriptorTest::AtomOrderTest()
172{
173 atom* testAtom;
174
175 // test in normal order: 1, 2, ...
176 for(int i=1;i<=ATOM_COUNT;++i){
177 testAtom = World::getInstance().getAtom(AtomByOrder(i));
178 CPPUNIT_ASSERT_EQUAL( atomIds[i-1], testAtom->getId());
179 }
180
181 // test in reverse order: -1, -2, ...
182 for(int i=1; i<= ATOM_COUNT;++i){
183 testAtom = World::getInstance().getAtom(AtomByOrder(-i));
184 CPPUNIT_ASSERT_EQUAL( atomIds[(int)ATOM_COUNT-i], testAtom->getId());
185 }
186
187 // test from outside of set
188 testAtom = World::getInstance().getAtom(AtomByOrder(ATOM_COUNT+1));
189 CPPUNIT_ASSERT(!testAtom);
190 testAtom = World::getInstance().getAtom(AtomByOrder(-ATOM_COUNT-1));
191 CPPUNIT_ASSERT(!testAtom);
192}
193
194
195std::set<atomId_t> getDistanceList(const double distance, const Vector &position, atom **list)
196{
197 const double distanceSquared = distance*distance;
198 std::set<atomId_t> reflist;
199 for (size_t i=0; i<ATOM_COUNT;++i)
200 if (list[i]->getPosition().DistanceSquared(position) < distanceSquared)
201 reflist.insert ( list[i]->getId() );
202 return reflist;
203}
204
205
206std::set<atomId_t> getIdList(const World::AtomComposite &list)
207{
208 std::set<atomId_t> testlist;
209 for (World::AtomComposite::const_iterator iter = list.begin();
210 iter != list.end(); ++iter)
211 testlist.insert( (*iter)->getId() );
212 return testlist;
213}
214
215//void AtomDescriptorTest::AtomsShapeTest()
216//{
217// // align atoms along an axis
218// for(int i=0;i<ATOM_COUNT;++i) {
219// atoms[i]->setPosition(Vector((double)i, 0., 0.));
220// //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
221// }
222//
223// // get atom by descriptor ...
224// // ... from origin up to 2.5
225// {
226// const double distance = 1.5;
227// Vector position(0.,0.,0.);
228// Shape s = Sphere(position, distance);
229// World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsByShape(s));
230// CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
231// std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
232// std::set<atomId_t> testlist = getIdList(atomlist);
233// CPPUNIT_ASSERT_EQUAL( reflist, testlist );
234// }
235// // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
236// {
237// const double distance = 2.9;
238// Vector position(4.,0.,0.);
239// Shape s = Sphere(position, distance);
240// World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsByShape(s));
241// CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
242// std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
243// std::set<atomId_t> testlist = getIdList(atomlist);
244// CPPUNIT_ASSERT_EQUAL( reflist, testlist );
245// }
246// // ... from (10,0,0) up to 1.5
247// {
248// const double distance = 1.5;
249// Vector *position = new Vector(10.,0.,0.);
250// Shape s = Sphere(position, distance);
251// World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsByShape(s));
252// CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
253// std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
254// std::set<atomId_t> testlist = getIdList(atomlist);
255// CPPUNIT_ASSERT_EQUAL( reflist, testlist );
256// delete position;
257// }
258//}
259
260void AtomDescriptorTest::AtomsWithinDistanceOfTest()
261{
262 // align atoms along an axis
263 for(int i=0;i<ATOM_COUNT;++i) {
264 atoms[i]->setPosition(Vector((double)i, 0., 0.));
265 //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
266 }
267
268 // get atom by descriptor ...
269 // ... from origin up to 2.5
270 {
271 const double distance = 1.5;
272 Vector position(0.,0.,0.);
273 World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, position));
274 CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
275 std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
276 std::set<atomId_t> testlist = getIdList(atomlist);
277 CPPUNIT_ASSERT_EQUAL( reflist, testlist );
278 }
279 // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
280 {
281 const double distance = 2.9;
282 World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, Vector(4.,0.,0.)));
283 CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
284 std::set<atomId_t> reflist = getDistanceList(distance, Vector(4.,0.,0.), atoms);
285 std::set<atomId_t> testlist = getIdList(atomlist);
286 CPPUNIT_ASSERT_EQUAL( reflist, testlist );
287 }
288 // ... from (10,0,0) up to 1.5
289 {
290 const double distance = 1.5;
291 Vector *position = new Vector(10.,0.,0.);
292 World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, *position));
293 CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
294 std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
295 std::set<atomId_t> testlist = getIdList(atomlist);
296 CPPUNIT_ASSERT_EQUAL( reflist, testlist );
297 delete position;
298 }
299}
300
301void AtomDescriptorTest::AtomCalcTest()
302{
303 // test some elementary set operations
304 {
305 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms());
306 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
307 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
308 }
309
310 {
311 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms());
312 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
313 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
314 }
315
316 {
317 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms());
318 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
319 }
320
321 {
322 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms());
323 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
324 }
325
326 {
327 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms());
328 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
329 }
330
331 {
332 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms());
333 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
334 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
335 }
336 // exclude and include some atoms
337 {
338 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
339 std::set<atomId_t> excluded;
340 excluded.insert(atomIds[ATOM_COUNT/2]);
341 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded));
342 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
343 CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
344 }
345
346 {
347 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
348 CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
349 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
350 }
351}
Note: See TracBrowser for help on using the repository browser.