source: src/unittests/CountBondsUnitTest.cpp@ dfe8ef

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

BUGFIX: CountHydrogenBridgeBonds() was half-broken, CountBondsOfThree() double-counted.

  • CountBondsOfThree() was counting O-H molecules as having a H-O-H and also a O-H-O triple bond because of a missing break.
  • CountHydrogenBridgeBonds() was broken in several places:
    • Checking for Hydrogen on the other oxygen atom is not part of the criteria by Marry, Rotenberg and Turq.
    • HydrogenFlag could have been set by previous atom making next hydrogen also a h-bridge bond.

Found thanks to newly implemented unit test CountBondsUnitTest.

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * CountBondsUnitTest.cpp
3 *
4 * Created on: Mar 30, 2010
5 * Author: heber
6 */
7
8
9using namespace std;
10
11#include <cppunit/CompilerOutputter.h>
12#include <cppunit/extensions/TestFactoryRegistry.h>
13#include <cppunit/ui/text/TestRunner.h>
14
15#include <iostream>
16#include <stdio.h>
17#include <cstring>
18
19#include "atom.hpp"
20#include "bond.hpp"
21#include "bondgraph.hpp"
22#include "element.hpp"
23#include "molecule.hpp"
24#include "periodentafel.hpp"
25#include "CountBondsUnitTest.hpp"
26
27/********************************************** Test classes **************************************/
28
29// Registers the fixture into the 'registry'
30CPPUNIT_TEST_SUITE_REGISTRATION( CountBondsTest );
31
32
33void CountBondsTest::setUp()
34{
35 atom *Walker = NULL;
36
37 // init private all pointers to zero
38 molecules = NULL;
39 TestMolecule1 = NULL;
40 TestMolecule2 = NULL;
41 hydrogen = NULL;
42 oxygen = NULL;
43 tafel = NULL;
44
45 // construct element
46 hydrogen = new element;
47 hydrogen->Z = 1;
48 hydrogen->CovalentRadius = 0.23;
49 strcpy(hydrogen->name, "hydrogen");
50 strcpy(hydrogen->symbol, "H");
51 oxygen = new element;
52 oxygen->Z = 8;
53 oxygen->CovalentRadius = 0.68;
54 strcpy(oxygen->name, "oxygen");
55 strcpy(oxygen->symbol, "O");
56
57 // construct periodentafel
58 tafel = new periodentafel;
59 tafel->AddElement(hydrogen);
60 tafel->AddElement(oxygen);
61
62 // construct molecule (water molecule)
63 molecules = new MoleculeListClass;
64 TestMolecule1 = new molecule(tafel);
65 Walker = new atom();
66 Walker->type = hydrogen;
67 Walker->node->Init(-0.2418, 0.9350, 0. );
68 TestMolecule1->AddAtom(Walker);
69 Walker = new atom();
70 Walker->type = hydrogen;
71 Walker->node->Init(0.9658, 0., 0. );
72 TestMolecule1->AddAtom(Walker);
73 Walker = new atom();
74 Walker->type = oxygen;
75 Walker->node->Init(0., 0., 0. );
76 TestMolecule1->AddAtom(Walker);
77 molecules->insert(TestMolecule1);
78
79 TestMolecule2 = new molecule(tafel);
80 Walker = new atom();
81 Walker->type = hydrogen;
82 Walker->node->Init(-0.2418, 0.9350, 0. );
83 TestMolecule2->AddAtom(Walker);
84 Walker = new atom();
85 Walker->type = hydrogen;
86 Walker->node->Init(0.9658, 0., 0. );
87 TestMolecule2->AddAtom(Walker);
88 Walker = new atom();
89 Walker->type = oxygen;
90 Walker->node->Init(0., 0., 0. );
91 TestMolecule2->AddAtom(Walker);
92 molecules->insert(TestMolecule2);
93
94 // check that TestMolecule was correctly constructed
95 CPPUNIT_ASSERT_EQUAL( TestMolecule1->AtomCount, 3 );
96 Walker = TestMolecule1->start->next;
97 CPPUNIT_ASSERT( TestMolecule1->end != Walker );
98 CPPUNIT_ASSERT_EQUAL( TestMolecule2->AtomCount, 3 );
99 Walker = TestMolecule2->start->next;
100 CPPUNIT_ASSERT( TestMolecule2->end != Walker );
101
102 // create a small file with table
103 BG = new BondGraph(true);
104
105 // construct bond graphs
106 CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule1) );
107 CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule2) );
108// TestMolecule1->Output((ofstream *)&cout);
109// TestMolecule1->OutputBondsList();
110};
111
112
113void CountBondsTest::tearDown()
114{
115 // remove the file
116 delete(BG);
117
118 // remove molecule
119 delete(molecules);
120 // note that all the atoms are cleaned by TestMolecule
121 delete(tafel);
122 // note that element is cleaned by periodentafel
123};
124
125/** UnitTest for CountBondsTest::BondsOfTwoTest().
126 */
127void CountBondsTest::BondsOfTwoTest()
128{
129 CPPUNIT_ASSERT_EQUAL( 4 , CountBondsOfTwo(molecules, hydrogen, oxygen) );
130 CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, hydrogen, hydrogen) );
131 CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, oxygen, oxygen) );
132};
133
134/** UnitTest for CountBondsTest::BondsOfThreeTest().
135 */
136void CountBondsTest::BondsOfThreeTest()
137{
138 CPPUNIT_ASSERT_EQUAL( 2 , CountBondsOfThree(molecules, hydrogen, oxygen, hydrogen) );
139 CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfThree(molecules, oxygen, hydrogen, oxygen) );
140};
141
142/** UnitTest for CountBondsTest::HydrogenBridgeBondsTest().
143 */
144void CountBondsTest::HydrogenBridgeBondsTest()
145{
146 double *mirror = new double[3];
147 for (int i=0;i<3;i++)
148 mirror[i] = -1.;
149 Vector Translator;
150 // offset of (3,0,0), hence angles are (104.5, 0, 75.5, 180) < 30
151 Translator.Init(3,0,0);
152 TestMolecule2->Translate(&Translator);
153 CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
154 CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, oxygen) );
155 Translator.Init(-3,0,0);
156 TestMolecule2->Translate(&Translator);
157
158 // offset of (0,3,0), hence angle are (14.5, 165.5, 90) < 30 (only three, because other 90 is missing due to first H01 only fulfilling H-bond criteria)
159 Translator.Init(0,3,0);
160 TestMolecule2->Translate(&Translator);
161 CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
162 Translator.Init(0,-3,0);
163 TestMolecule2->Translate(&Translator);
164
165 // offset of (0,-3,0) and mirror, hence angle are (165.5, 90, 165.5, 90) > 30
166 Translator.Init(0,-3,0);
167 TestMolecule2->Scale((const double ** const)&mirror);
168 TestMolecule2->Translate(&Translator);
169 CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
170 Translator.Init(0,3,0);
171 TestMolecule2->Translate(&Translator);
172 TestMolecule2->Scale((const double ** const)&mirror);
173
174 // offset of (2,1,0), hence angle are (78, 26.6, 102, 153.4) < 30
175 Translator.Init(2,1,0);
176 TestMolecule2->Translate(&Translator);
177 CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
178 Translator.Init(-2,-1,0);
179 TestMolecule2->Translate(&Translator);
180
181 // offset of (0,0,3), hence angle are (90, 90, 90, 90) > 30
182 Translator.Init(0,0,3);
183 TestMolecule2->Translate(&Translator);
184 CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
185 Translator.Init(0,0,-3);
186 TestMolecule2->Translate(&Translator);
187
188 // offset of (-3,0,0) and mirror, hence angle are (75.5, 180, 104.5, 180) > 30
189 Translator.Init(-3,0,0);
190 TestMolecule2->Scale((const double ** const)&mirror);
191 TestMolecule2->Translate(&Translator);
192 CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
193 Translator.Init(3,0,0);
194 TestMolecule2->Translate(&Translator);
195 TestMolecule2->Scale((const double ** const)&mirror);
196 delete(mirror);
197};
198
199
200/********************************************** Main routine **************************************/
201
202int main(int argc, char **argv)
203{
204 // Get the top level suite from the registry
205 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
206
207 // Adds the test to the list of test to run
208 CppUnit::TextUi::TestRunner runner;
209 runner.addTest( suite );
210
211 // Change the default outputter to a compiler error format outputter
212 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
213 std::cerr ) );
214 // Run the tests.
215 bool wasSucessful = runner.run();
216
217 // Return error code 1 if the one of test failed.
218 return wasSucessful ? 0 : 1;
219};
Note: See TracBrowser for help on using the repository browser.