source: src/unittests/CountBondsUnitTest.cpp@ fe238c

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

Enhanced CountHydrogenBridgeBonds() a bit.

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