source: src/Descriptors/unittests/AtomDescriptorUnitTest.cpp@ bbab87

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 bbab87 was bbab87, checked in by Frederik Heber <heber@…>, 13 years ago

Added test for new descriptor AtomsWithinDistanceOf to AtomDescriptorUnitTest.

  • in AtomDescriptorTest::AtomsWithinDistanceOfTest(): ids must not necessarily be in order, added functions that make comparison based on simple N2 search and helper functions.
  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-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 * AtomDescriptorUnitTest.cpp
10 *
11 * Created on: Feb 9, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "AtomDescriptorUnitTest.hpp"
21
22#include <cppunit/CompilerOutputter.h>
23#include <cppunit/extensions/TestFactoryRegistry.h>
24#include <cppunit/ui/text/TestRunner.h>
25#include <iostream>
26
27#include <Descriptors/AtomDescriptor.hpp>
28#include <Descriptors/AtomIdDescriptor.hpp>
29#include <Descriptors/AtomOfMoleculeDescriptor.hpp>
30#include <Descriptors/AtomsWithinDistanceOfDescriptor.hpp>
31
32#include "World.hpp"
33#include "Atom/atom.hpp"
34#include "molecule.hpp"
35#include "LinearAlgebra/Vector.hpp"
36
37#ifdef HAVE_TESTRUNNER
38#include "UnitTestMain.hpp"
39#endif /*HAVE_TESTRUNNER*/
40
41/********************************************** Test classes **************************************/
42// Registers the fixture into the 'registry'
43CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest );
44
45// set up and tear down
46void AtomDescriptorTest::setUp()
47{
48 World::getInstance();
49 for(int i=0;i<ATOM_COUNT;++i){
50 atoms[i]= World::getInstance().createAtom();
51 atomIds[i]= atoms[i]->getId();
52 }
53}
54
55void AtomDescriptorTest::tearDown()
56{
57 World::purgeInstance();
58}
59
60// some helper functions
61static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>())
62{
63 for(int i=0;i<ATOM_COUNT;++i){
64 atomId_t id = ids[i];
65 if(!excluded.count(id)){
66 std::vector<atom*>::iterator iter;
67 bool res=false;
68 for(iter=atoms.begin();iter!=atoms.end();++iter){
69 res |= (*iter)->getId() == id;
70 }
71 if(!res) {
72 cout << "Atom " << id << " missing in returned list" << endl;
73 return false;
74 }
75 }
76 }
77 return true;
78}
79
80static bool hasNoDuplicateAtoms(std::vector<atom*> atoms)
81{
82 std::set<atomId_t> found;
83 std::vector<atom*>::iterator iter;
84 for(iter=atoms.begin();iter!=atoms.end();++iter){
85 int id = (*iter)->getId();
86 if(found.count(id))
87 return false;
88 found.insert(id);
89 }
90 return true;
91}
92
93
94void AtomDescriptorTest::AtomBaseSetsTest()
95{
96 std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
97 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds));
98 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms));
99
100 std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms());
101 CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
102}
103
104void AtomDescriptorTest::AtomIdTest()
105{
106 // test Atoms from boundaries and middle of the set
107 atom* testAtom;
108 testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
109 CPPUNIT_ASSERT(testAtom);
110 CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
111 testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2]));
112 CPPUNIT_ASSERT(testAtom);
113 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
114 testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1]));
115 CPPUNIT_ASSERT(testAtom);
116 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
117
118 // find some ID that has not been created
119 atomId_t outsideId=0;
120 bool res = false;
121 for(outsideId=0;!res;++outsideId) {
122 res = true;
123 for(int i = 0; i < ATOM_COUNT; ++i){
124 res &= atomIds[i]!=outsideId;
125 }
126 }
127 // test from outside of set
128 testAtom = World::getInstance().getAtom(AtomById(outsideId));
129 CPPUNIT_ASSERT(!testAtom);
130}
131
132void AtomDescriptorTest::AtomOfMoleculeTest()
133{
134 // test Atoms from boundaries and middle of the set
135 atom* testAtom;
136 testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
137 CPPUNIT_ASSERT(testAtom);
138 CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
139
140 // create some molecule and associate atom to it
141 testAtom->setType(1);
142 molecule * newmol = World::getInstance().createMolecule();
143 newmol->AddAtom(testAtom);
144 CPPUNIT_ASSERT_EQUAL(newmol->getId(), testAtom->getMolecule()->getId());
145
146 // get atom by descriptor
147 World::AtomComposite atoms = World::getInstance().getAllAtoms(AtomOfMolecule(newmol->getId()));
148 CPPUNIT_ASSERT_EQUAL( (size_t)1, atoms.size() );
149 CPPUNIT_ASSERT_EQUAL( (*atoms.begin())->getId(), testAtom->getId() );
150
151 // remove molecule again
152 World::getInstance().destroyMolecule(newmol);
153}
154
155std::set<atomId_t> getDistanceList(const double distance, const Vector &position, atom **list)
156{
157 const double distanceSquared = distance*distance;
158 std::set<atomId_t> reflist;
159 for (size_t i=0; i<ATOM_COUNT;++i)
160 if (list[i]->getPosition().DistanceSquared(position) < distanceSquared)
161 reflist.insert ( list[i]->getId() );
162 return reflist;
163}
164
165
166std::set<atomId_t> getIdList(const World::AtomComposite &list)
167{
168 std::set<atomId_t> testlist;
169 for (World::AtomComposite::const_iterator iter = list.begin();
170 iter != list.end(); ++iter)
171 testlist.insert( (*iter)->getId() );
172 return testlist;
173}
174
175void AtomDescriptorTest::AtomsWithinDistanceOfTest()
176{
177 // align atoms along an axis
178 for(int i=0;i<ATOM_COUNT;++i) {
179 atoms[i]->setPosition(Vector((double)i, 0., 0.));
180 //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
181 }
182
183 // get atom by descriptor ...
184 // ... from origin up to 2.5
185 {
186 const double distance = 1.5;
187 Vector position(0.,0.,0.);
188 World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, position));
189 CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
190 std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
191 std::set<atomId_t> testlist = getIdList(atomlist);
192 CPPUNIT_ASSERT_EQUAL( reflist, testlist );
193 }
194 // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
195 {
196 const double distance = 2.9;
197 World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, Vector(4.,0.,0.)));
198 CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
199 std::set<atomId_t> reflist = getDistanceList(distance, Vector(4.,0.,0.), atoms);
200 std::set<atomId_t> testlist = getIdList(atomlist);
201 CPPUNIT_ASSERT_EQUAL( reflist, testlist );
202 }
203 // ... from (10,0,0) up to 1.5
204 {
205 const double distance = 1.5;
206 Vector *position = new Vector(10.,0.,0.);
207 World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, *position));
208 CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
209 std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
210 std::set<atomId_t> testlist = getIdList(atomlist);
211 CPPUNIT_ASSERT_EQUAL( reflist, testlist );
212 delete position;
213 }
214}
215
216void AtomDescriptorTest::AtomCalcTest()
217{
218 // test some elementary set operations
219 {
220 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms());
221 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
222 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
223 }
224
225 {
226 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms());
227 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
228 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
229 }
230
231 {
232 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms());
233 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
234 }
235
236 {
237 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms());
238 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
239 }
240
241 {
242 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms());
243 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
244 }
245
246 {
247 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms());
248 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
249 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
250 }
251 // exclude and include some atoms
252 {
253 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
254 std::set<atomId_t> excluded;
255 excluded.insert(atomIds[ATOM_COUNT/2]);
256 CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded));
257 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
258 CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
259 }
260
261 {
262 std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
263 CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
264 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
265 }
266}
Note: See TracBrowser for help on using the repository browser.