source: src/unittests/analysisbondsunittest.cpp@ 02ee15

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 02ee15 was 46d958, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made the world solely responsible for creating and destroying atoms.

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * analysisbondsunittest.cpp
3 *
4 * Created on: Nov 7, 2009
5 * Author: heber
6 */
7
8using namespace std;
9
10#include <cppunit/CompilerOutputter.h>
11#include <cppunit/extensions/TestFactoryRegistry.h>
12#include <cppunit/ui/text/TestRunner.h>
13
14#include <iostream>
15#include <stdio.h>
16#include <cstring>
17
18#include "World.hpp"
19#include "analysis_bonds.hpp"
20#include "analysisbondsunittest.hpp"
21#include "atom.hpp"
22#include "bond.hpp"
23#include "bondgraph.hpp"
24#include "element.hpp"
25#include "molecule.hpp"
26#include "periodentafel.hpp"
27
28/********************************************** Test classes **************************************/
29
30// Registers the fixture into the 'registry'
31CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest );
32
33
34void AnalysisBondsTest::setUp()
35{
36 atom *Walker = NULL;
37
38 // init private all pointers to zero
39 TestMolecule = NULL;
40 hydrogen = NULL;
41 tafel = NULL;
42
43 // construct element
44 hydrogen = new element;
45 hydrogen->Z = 1;
46 hydrogen->Valence = 1;
47 hydrogen->NoValenceOrbitals = 1;
48 strcpy(hydrogen->name, "hydrogen");
49 strcpy(hydrogen->symbol, "H");
50 carbon = new element;
51 carbon->Z = 1;
52 carbon->Valence = 4;
53 carbon->NoValenceOrbitals = 4;
54 strcpy(carbon->name, "carbon");
55 strcpy(carbon->symbol, "C");
56
57
58 // construct periodentafel
59 tafel = new periodentafel;
60 tafel->AddElement(hydrogen);
61 tafel->AddElement(carbon);
62
63 // construct molecule (tetraeder of hydrogens)
64 TestMolecule = new molecule(tafel);
65 Walker = World::get()->createAtom();
66 Walker->type = hydrogen;
67 Walker->node->Init(1.5, 0., 1.5 );
68 TestMolecule->AddAtom(Walker);
69 Walker = World::get()->createAtom();
70 Walker->type = hydrogen;
71 Walker->node->Init(0., 1.5, 1.5 );
72 TestMolecule->AddAtom(Walker);
73 Walker = World::get()->createAtom();
74 Walker->type = hydrogen;
75 Walker->node->Init(1.5, 1.5, 0. );
76 TestMolecule->AddAtom(Walker);
77 Walker = World::get()->createAtom();
78 Walker->type = hydrogen;
79 Walker->node->Init(0., 0., 0. );
80 TestMolecule->AddAtom(Walker);
81 Walker = World::get()->createAtom();
82 Walker->type = carbon;
83 Walker->node->Init(0.5, 0.5, 0.5 );
84 TestMolecule->AddAtom(Walker);
85
86 // check that TestMolecule was correctly constructed
87 CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 );
88
89 // create a small file with table
90 filename = new string("test.dat");
91 ofstream test(filename->c_str());
92 test << ".\tH\tC\n";
93 test << "H\t1.\t1.2\n";
94 test << "C\t1.2\t1.5\n";
95 test.close();
96 BG = new BondGraph(true);
97
98 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
99 CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
100 CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
101 CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
102
103 BG->ConstructBondGraph(TestMolecule);
104};
105
106
107void AnalysisBondsTest::tearDown()
108{
109 // remove the file
110 remove(filename->c_str());
111 delete(filename);
112 delete(BG);
113
114 // remove molecule
115 delete(TestMolecule);
116 // note that all the atoms are cleaned by TestMolecule
117 delete(tafel);
118 // note that element is cleaned by periodentafel
119};
120
121/** UnitTest for GetMaxMinMeanBondCount().
122 */
123void AnalysisBondsTest::GetMaxMinMeanBondCountTest()
124{
125 double Min = 20.; // check that initialization resets these arbitrary values
126 double Mean = 200.;
127 double Max = 1e-6;
128 GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max);
129 CPPUNIT_ASSERT_EQUAL( 1., Min );
130 CPPUNIT_ASSERT_EQUAL( 1.6, Mean );
131 CPPUNIT_ASSERT_EQUAL( 4., Max );
132
133};
134
135/** UnitTest for MinMaxBondDistanceBetweenElements().
136 */
137void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest()
138{
139 double Min = 20.; // check that initialization resets these arbitrary values
140 double Mean = 2e+6;
141 double Max = 1e-6;
142 double Min2 = 20.;
143 double Mean2 = 2e+6;
144 double Max2 = 1e-6;
145 const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5);
146 const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5);
147 const double meanbondlength = (minbondlength+3.*maxbondlength)/4.;
148 // check bond lengths C-H
149 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max);
150 CPPUNIT_ASSERT_EQUAL( minbondlength , Min );
151 CPPUNIT_ASSERT_EQUAL( meanbondlength , Mean );
152 CPPUNIT_ASSERT_EQUAL( maxbondlength , Max );
153
154 // check that elements are symmetric, i.e. C-H == H-C
155 MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2);
156 CPPUNIT_ASSERT_EQUAL( Min , Min2 );
157 CPPUNIT_ASSERT_EQUAL( Mean , Mean2 );
158 CPPUNIT_ASSERT_EQUAL( Max , Max2 );
159
160 // check no bond case (no bonds H-H in system!)
161 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max);
162 CPPUNIT_ASSERT_EQUAL( 0. , Min );
163 CPPUNIT_ASSERT_EQUAL( 0. , Mean );
164 CPPUNIT_ASSERT_EQUAL( 0. , Max );
165};
166
167
168/********************************************** Main routine **************************************/
169
170int main(int argc, char **argv)
171{
172 // Get the top level suite from the registry
173 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
174
175 // Adds the test to the list of test to run
176 CppUnit::TextUi::TestRunner runner;
177 runner.addTest( suite );
178
179 // Change the default outputter to a compiler error format outputter
180 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
181 std::cerr ) );
182 // Run the tests.
183 bool wasSucessful = runner.run();
184
185 // Return error code 1 if the one of test failed.
186 return wasSucessful ? 0 : 1;
187};
Note: See TracBrowser for help on using the repository browser.