Changeset e462b3 for molecuilder/src
- Timestamp:
- Nov 7, 2009, 12:25:32 PM (16 years ago)
- Children:
- e960b1
- Parents:
- 1fcedd
- git-author:
- Frederik Heber <heber@…> (11/07/09 12:18:56)
- git-committer:
- Frederik Heber <heber@…> (11/07/09 12:25:32)
- Location:
- molecuilder/src
- Files:
-
- 4 edited
-
analysis_bonds.cpp (modified) (1 diff)
-
analysis_bonds.hpp (modified) (1 diff)
-
unittests/analysisbondsunittest.cpp (modified) (7 diffs)
-
unittests/analysisbondsunittest.hpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/analysis_bonds.cpp
r1fcedd re462b3 6 6 */ 7 7 8 #include "analysis_bonds.hpp" 9 #include "atom.hpp" 10 #include "bond.hpp" 11 #include "log.hpp" 12 #include "molecule.hpp" 13 14 /** Calculates the min, mean and maximum bond counts for the given molecule. 15 * \param *mol molecule with atoms and atom::ListOfBonds 16 * \param &Min minimum count on return 17 * \param &Mean mean count on return 18 * \param &Max maximum count on return 19 */ 20 void GetMaxMinMeanBondCount(const molecule * const mol, double &Min, double &Mean, double &Max) 21 { 22 Min = 2e+6; 23 Max = -2e+5; 24 Mean = 0.; 25 26 atom *Walker = mol->start; 27 int AtomCount = 0; 28 while (Walker->next != mol->end) { 29 Walker = Walker->next; 30 const int count = Walker->ListOfBonds.size(); 31 if (Max < count) 32 Max = count; 33 if (Min > count) 34 Min = count; 35 Mean += count; 36 AtomCount++; 37 } 38 if (((int)Mean % 2) != 0) 39 eLog() << Verbose(1) << "Something is wrong with the bond structure, the number of bonds is not even!" << endl; 40 Mean /= (double)AtomCount; 41 }; 42 43 /** Calculates the min and max bond distance of all atoms of two given elements. 44 * \param *mol molecule with atoms 45 * \param *type1 one element 46 * \param *type2 other element 47 * \param &Min minimum distance on return, 0 if no bond between the two elements 48 * \param &Mean mean distance (i.e. sum of distance for matching element pairs, divided by number) on return, 0 if no bond between the two elements 49 * \param &Max maximum distance on return, 0 if no bond between the two elements 50 */ 51 void MinMeanMaxBondDistanceBetweenElements(const molecule *mol, element *type1, element *type2, double &Min, double &Mean, double &Max) 52 { 53 Min = 2e+6; 54 Mean = 0.; 55 Max = -2e+6; 56 57 int AtomNo = 0; 58 atom *Walker = mol->start; 59 while (Walker->next != mol->end) { 60 Walker = Walker->next; 61 if (Walker->type == type1) 62 for (BondList::const_iterator BondRunner = Walker->ListOfBonds.begin(); BondRunner != Walker->ListOfBonds.end(); BondRunner++) 63 if ((*BondRunner)->GetOtherAtom(Walker)->type == type2) { 64 const double distance = (*BondRunner)->GetDistanceSquared(); 65 if (Min > distance) 66 Min = distance; 67 if (Max < distance) 68 Max = distance; 69 Mean += sqrt(distance); 70 AtomNo++; 71 } 72 } 73 if (Max < 0) { 74 Max = Min = 0.; 75 } else { 76 Max = sqrt(Max); 77 Min = sqrt(Min); 78 Mean = Mean/(double)AtomNo; 79 } 80 }; -
molecuilder/src/analysis_bonds.hpp
r1fcedd re462b3 21 21 /****************************************** forward declarations *****************************/ 22 22 23 class element; 24 class molecule; 23 25 24 26 /********************************************** declarations *******************************/ 25 27 28 void GetMaxMinMeanBondCount(const molecule * const mol, double &Min, double &Mean, double &Max); 29 void MinMeanMaxBondDistanceBetweenElements(const molecule *mol, element *type1, element *type2, double &Min, double &Mean, double &Max); 30 26 31 #endif /* ANALYSIS_BONDS_HPP_ */ -
molecuilder/src/unittests/analysisbondsunittest.cpp
r1fcedd re462b3 15 15 #include <stdio.h> 16 16 17 #include "analysis_bonds.hpp" 18 #include "analysisbondsunittest.hpp" 17 19 #include "atom.hpp" 18 20 #include "bond.hpp" … … 21 23 #include "molecule.hpp" 22 24 #include "periodentafel.hpp" 23 #include "analysisbondsunittest.hpp"24 25 25 26 /********************************************** Test classes **************************************/ … … 41 42 hydrogen = new element; 42 43 hydrogen->Z = 1; 44 hydrogen->Valence = 1; 45 hydrogen->NoValenceOrbitals = 1; 43 46 strcpy(hydrogen->name, "hydrogen"); 44 47 strcpy(hydrogen->symbol, "H"); 45 48 carbon = new element; 46 49 carbon->Z = 1; 50 carbon->Valence = 4; 51 carbon->NoValenceOrbitals = 4; 47 52 strcpy(carbon->name, "carbon"); 48 53 strcpy(carbon->symbol, "C"); … … 58 63 Walker = new atom(); 59 64 Walker->type = hydrogen; 60 Walker->node->Init(1. , 0., 1.);65 Walker->node->Init(1.5, 0., 1.5 ); 61 66 TestMolecule->AddAtom(Walker); 62 67 Walker = new atom(); 63 68 Walker->type = hydrogen; 64 Walker->node->Init(0., 1. , 1.);69 Walker->node->Init(0., 1.5, 1.5 ); 65 70 TestMolecule->AddAtom(Walker); 66 71 Walker = new atom(); 67 72 Walker->type = hydrogen; 68 Walker->node->Init(1. , 1., 0. );73 Walker->node->Init(1.5, 1.5, 0. ); 69 74 TestMolecule->AddAtom(Walker); 70 75 Walker = new atom(); … … 72 77 Walker->node->Init(0., 0., 0. ); 73 78 TestMolecule->AddAtom(Walker); 79 Walker = new atom(); 80 Walker->type = carbon; 81 Walker->node->Init(0.5, 0.5, 0.5 ); 82 TestMolecule->AddAtom(Walker); 74 83 75 84 // check that TestMolecule was correctly constructed 76 CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4);85 CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 ); 77 86 78 87 // create a small file with table … … 82 91 test << "H\t1.\t1.2\n"; 83 92 test << "C\t1.2\t1.5\n"; 93 test.close(); 84 94 BG = new BondGraph(true); 95 96 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) ); 97 CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) ); 98 CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) ); 99 CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) ); 100 101 BG->ConstructBondGraph(TestMolecule); 85 102 }; 86 103 … … 100 117 }; 101 118 102 /** UnitTest for AnalysisBondsTest::LoadBondLengthTable().119 /** UnitTest for GetMaxMinMeanBondCount(). 103 120 */ 104 void AnalysisBondsTest:: BondsTest()121 void AnalysisBondsTest::GetMaxMinMeanBondCountTest() 105 122 { 106 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) ); 107 CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) ); 108 CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) ); 109 CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) ); 110 111 CPPUNIT_ASSERT_EQUAL( true , true ); 123 double Min = 20.; // check that initialization resets these arbitrary values 124 double Mean = 200.; 125 double Max = 1e-6; 126 GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max); 127 CPPUNIT_ASSERT_EQUAL( 1., Min ); 128 CPPUNIT_ASSERT_EQUAL( 1.6, Mean ); 129 CPPUNIT_ASSERT_EQUAL( 4., Max ); 112 130 113 131 }; 132 133 /** UnitTest for MinMaxBondDistanceBetweenElements(). 134 */ 135 void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest() 136 { 137 double Min = 20.; // check that initialization resets these arbitrary values 138 double Mean = 2e+6; 139 double Max = 1e-6; 140 double Min2 = 20.; 141 double Mean2 = 2e+6; 142 double Max2 = 1e-6; 143 const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5); 144 const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5); 145 const double meanbondlength = (minbondlength+3.*maxbondlength)/4.; 146 // check bond lengths C-H 147 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max); 148 CPPUNIT_ASSERT_EQUAL( minbondlength , Min ); 149 CPPUNIT_ASSERT_EQUAL( meanbondlength , Mean ); 150 CPPUNIT_ASSERT_EQUAL( maxbondlength , Max ); 151 152 // check that elements are symmetric, i.e. C-H == H-C 153 MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2); 154 CPPUNIT_ASSERT_EQUAL( Min , Min2 ); 155 CPPUNIT_ASSERT_EQUAL( Mean , Mean2 ); 156 CPPUNIT_ASSERT_EQUAL( Max , Max2 ); 157 158 // check no bond case (no bonds H-H in system!) 159 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max); 160 CPPUNIT_ASSERT_EQUAL( 0. , Min ); 161 CPPUNIT_ASSERT_EQUAL( 0. , Mean ); 162 CPPUNIT_ASSERT_EQUAL( 0. , Max ); 163 }; 164 114 165 115 166 /********************************************** Main routine **************************************/ -
molecuilder/src/unittests/analysisbondsunittest.hpp
r1fcedd re462b3 11 11 #include <cppunit/extensions/HelperMacros.h> 12 12 13 #include "bondgraph.hpp" 14 13 class BondGraph; 15 14 class element; 16 15 class molecule; … … 22 21 { 23 22 CPPUNIT_TEST_SUITE( AnalysisBondsTest) ; 24 CPPUNIT_TEST ( BondsTest ); 23 CPPUNIT_TEST ( GetMaxMinMeanBondCountTest ); 24 CPPUNIT_TEST ( MinMeanMaxBondDistanceBetweenElementsTest ); 25 25 CPPUNIT_TEST_SUITE_END(); 26 26 … … 28 28 void setUp(); 29 29 void tearDown(); 30 void BondsTest(); 30 void GetMaxMinMeanBondCountTest(); 31 void MinMeanMaxBondDistanceBetweenElementsTest(); 31 32 32 33 private:
Note:
See TracChangeset
for help on using the changeset viewer.
