Changes in src/molecule.cpp [ead4e6:a67d19]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/molecule.cpp
read4e6 ra67d19 6 6 7 7 #include <cstring> 8 #include <boost/bind.hpp> 9 10 #include "World.hpp" 8 11 9 #include "atom.hpp" 12 10 #include "bond.hpp" … … 25 23 #include "tesselation.hpp" 26 24 #include "vector.hpp" 25 #include "World.hpp" 27 26 28 27 /************************************* Functions for class molecule *********************************/ … … 31 30 * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero. 32 31 */ 33 molecule::molecule(const periodentafel * const teil) : elemente(teil), start( World::getInstance().createAtom()), end(World::getInstance().createAtom()),32 molecule::molecule(const periodentafel * const teil) : elemente(teil), start(new atom), end(new atom), 34 33 first(new bond(start, end, 1, -1)), last(new bond(start, end, 1, -1)), MDSteps(0), AtomCount(0), 35 34 BondCount(0), ElementCount(0), NoNonHydrogen(0), NoNonBonds(0), NoCyclicBonds(0), BondDistance(0.), 36 ActiveFlag(false), IndexNr(-1), 37 formula(this,boost::bind(&molecule::calcFormula,this)), 38 last_atom(0), 39 InternalPointer(start) 35 ActiveFlag(false), IndexNr(-1), last_atom(0), InternalPointer(start) 40 36 { 41 37 // init atom chain list … … 50 46 for(int i=MAX_ELEMENTS;i--;) 51 47 ElementsInMolecule[i] = 0; 52 cell_size[0] = cell_size[2] = cell_size[5]= 20.; 53 cell_size[1] = cell_size[3] = cell_size[4]= 0.; 54 strcpy(name,"none"); 55 }; 56 57 molecule *NewMolecule(){ 58 return new molecule(World::getInstance().getPeriode()); 59 } 48 strcpy(name,World::get()->DefaultName); 49 }; 60 50 61 51 /** Destructor of class molecule. … … 67 57 delete(first); 68 58 delete(last); 69 end->getWorld()->destroyAtom(end); 70 start->getWorld()->destroyAtom(start); 71 }; 72 73 74 void DeleteMolecule(molecule *mol){ 75 delete mol; 76 } 77 78 // getter and setter 79 const std::string molecule::getName(){ 80 return std::string(name); 81 } 82 83 void molecule::setName(const std::string _name){ 84 OBSERVE; 85 strncpy(name,_name.c_str(),MAXSTRINGSIZE); 86 } 87 88 moleculeId_t molecule::getId(){ 89 return id; 90 } 91 92 void molecule::setId(moleculeId_t _id){ 93 id =_id; 94 } 95 96 const std::string molecule::getFormula(){ 97 return *formula; 98 } 99 100 std::string molecule::calcFormula(){ 101 std::map<atomicNumber_t,unsigned int> counts; 102 stringstream sstr; 103 periodentafel *periode = World::getInstance().getPeriode(); 104 for(atom *Walker = start; Walker != end; Walker = Walker->next) { 105 counts[Walker->type->getNumber()]++; 106 } 107 std::map<atomicNumber_t,unsigned int>::reverse_iterator iter; 108 for(iter = counts.rbegin(); iter != counts.rend(); ++iter) { 109 atomicNumber_t Z = (*iter).first; 110 sstr << periode->FindElement(Z)->symbol << (*iter).second; 111 } 112 return sstr.str(); 113 } 59 delete(end); 60 delete(start); 61 }; 114 62 115 63 … … 121 69 bool molecule::AddAtom(atom *pointer) 122 70 { 123 bool retval = false;124 OBSERVE;125 71 if (pointer != NULL) { 126 72 pointer->sort = &pointer->nr; … … 139 85 } 140 86 } 141 ret val =add(pointer, end);142 } 143 return retval;87 return add(pointer, end); 88 } else 89 return false; 144 90 }; 145 91 … … 151 97 atom * molecule::AddCopyAtom(atom *pointer) 152 98 { 153 atom *retval = NULL;154 OBSERVE;155 99 if (pointer != NULL) { 156 atom *walker = pointer->clone();100 atom *walker = new atom(pointer); 157 101 walker->Name = Malloc<char>(strlen(pointer->Name) + 1, "atom::atom: *Name"); 158 102 strcpy (walker->Name, pointer->Name); … … 162 106 NoNonHydrogen++; 163 107 AtomCount++; 164 ret val=walker;165 } 166 return retval;108 return walker; 109 } else 110 return NULL; 167 111 }; 168 112 … … 203 147 bool molecule::AddHydrogenReplacementAtom(bond *TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem) 204 148 { 205 bool AllWentWell = true; // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit206 OBSERVE;207 149 double bondlength; // bond length of the bond to be replaced/cut 208 150 double bondangle; // bond angle of the bond to be replaced/cut 209 151 double BondRescale; // rescale value for the hydrogen bond length 152 bool AllWentWell = true; // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit 210 153 bond *FirstBond = NULL, *SecondBond = NULL; // Other bonds in double bond case to determine "other" plane 211 154 atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added … … 215 158 double *matrix = NULL; 216 159 bond *Binder = NULL; 160 double * const cell_size = World::get()->cell_size; 217 161 218 162 // Log() << Verbose(3) << "Begin of AddHydrogenReplacementAtom." << endl; … … 250 194 BondRescale = TopOrigin->type->HBondDistance[TopBond->BondDegree-1]; 251 195 if (BondRescale == -1) { 252 eLog() << Verbose(1) << "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;196 DoeLog(1) && (eLog()<< Verbose(1) << "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl); 253 197 return false; 254 198 BondRescale = bondlength; … … 261 205 switch(TopBond->BondDegree) { 262 206 case 1: 263 FirstOtherAtom = World::getInstance().createAtom(); // new atom207 FirstOtherAtom = new atom(); // new atom 264 208 FirstOtherAtom->type = elemente->FindElement(1); // element is Hydrogen 265 209 FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity … … 293 237 SecondOtherAtom = (*Runner)->GetOtherAtom(TopOrigin); 294 238 } else { 295 eLog() << Verbose(2) << "Detected more than four bonds for atom " << TopOrigin->Name;239 DoeLog(2) && (eLog()<< Verbose(2) << "Detected more than four bonds for atom " << TopOrigin->Name); 296 240 } 297 241 } … … 318 262 319 263 // create the two Hydrogens ... 320 FirstOtherAtom = World::getInstance().createAtom();321 SecondOtherAtom = World::getInstance().createAtom();264 FirstOtherAtom = new atom(); 265 SecondOtherAtom = new atom(); 322 266 FirstOtherAtom->type = elemente->FindElement(1); 323 267 SecondOtherAtom->type = elemente->FindElement(1); … … 330 274 bondangle = TopOrigin->type->HBondAngle[1]; 331 275 if (bondangle == -1) { 332 eLog() << Verbose(1) << "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;276 DoeLog(1) && (eLog()<< Verbose(1) << "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl); 333 277 return false; 334 278 bondangle = 0; … … 373 317 case 3: 374 318 // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid) 375 FirstOtherAtom = World::getInstance().createAtom();376 SecondOtherAtom = World::getInstance().createAtom();377 ThirdOtherAtom = World::getInstance().createAtom();319 FirstOtherAtom = new atom(); 320 SecondOtherAtom = new atom(); 321 ThirdOtherAtom = new atom(); 378 322 FirstOtherAtom->type = elemente->FindElement(1); 379 323 SecondOtherAtom->type = elemente->FindElement(1); … … 452 396 break; 453 397 default: 454 eLog() << Verbose(1) << "BondDegree does not state single, double or triple bond!" << endl;398 DoeLog(1) && (eLog()<< Verbose(1) << "BondDegree does not state single, double or triple bond!" << endl); 455 399 AllWentWell = false; 456 400 break; … … 469 413 bool molecule::AddXYZFile(string filename) 470 414 { 471 472 415 istringstream *input = NULL; 473 416 int NumberOfAtoms = 0; // atom number in xyz read … … 483 426 return false; 484 427 485 OBSERVE;486 428 getline(xyzfile,line,'\n'); // Read numer of atoms in file 487 429 input = new istringstream(line); 488 430 *input >> NumberOfAtoms; 489 Log() << Verbose(0) << "Parsing " << NumberOfAtoms << " atoms in file." << endl;431 DoLog(0) && (Log() << Verbose(0) << "Parsing " << NumberOfAtoms << " atoms in file." << endl); 490 432 getline(xyzfile,line,'\n'); // Read comment 491 Log() << Verbose(1) << "Comment: " << line << endl;433 DoLog(1) && (Log() << Verbose(1) << "Comment: " << line << endl); 492 434 493 435 if (MDSteps == 0) // no atoms yet present 494 436 MDSteps++; 495 437 for(i=0;i<NumberOfAtoms;i++){ 496 Walker = World::getInstance().createAtom();438 Walker = new atom; 497 439 getline(xyzfile,line,'\n'); 498 440 istringstream *item = new istringstream(line); … … 505 447 Walker->type = elemente->FindElement(shorthand); 506 448 if (Walker->type == NULL) { 507 eLog() << Verbose(1) << "Could not parse the element at line: '" << line << "', setting to H.";449 DoeLog(1) && (eLog()<< Verbose(1) << "Could not parse the element at line: '" << line << "', setting to H."); 508 450 Walker->type = elemente->FindElement(1); 509 451 } … … 601 543 add(Binder, last); 602 544 } else { 603 eLog() << Verbose(1) << "Could not add bond between " << atom1->Name << " and " << atom2->Name << " as one or both are not present in the molecule." << endl;545 DoeLog(1) && (eLog()<< Verbose(1) << "Could not add bond between " << atom1->Name << " and " << atom2->Name << " as one or both are not present in the molecule." << endl); 604 546 } 605 547 return Binder; … … 613 555 bool molecule::RemoveBond(bond *pointer) 614 556 { 615 // eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;557 //DoeLog(1) && (eLog()<< Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl); 616 558 pointer->leftatom->RegisterBond(pointer); 617 559 pointer->rightatom->RegisterBond(pointer); … … 627 569 bool molecule::RemoveBonds(atom *BondPartner) 628 570 { 629 // eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;571 //DoeLog(1) && (eLog()<< Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl); 630 572 BondList::const_iterator ForeRunner; 631 573 while (!BondPartner->ListOfBonds.empty()) { … … 661 603 void molecule::SetBoxDimension(Vector *dim) 662 604 { 605 double * const cell_size = World::get()->cell_size; 663 606 cell_size[0] = dim->x[0]; 664 607 cell_size[1] = 0.; … … 679 622 AtomCount--; 680 623 } else 681 eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;624 DoeLog(1) && (eLog()<< Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl); 682 625 if (ElementsInMolecule[pointer->type->Z] == 0) // was last atom of this element? 683 626 ElementCount--; … … 697 640 ElementsInMolecule[pointer->type->Z]--; // decrease number of atom of this element 698 641 else 699 eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;642 DoeLog(1) && (eLog()<< Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl); 700 643 if (ElementsInMolecule[pointer->type->Z] == 0) // was last atom of this element? 701 644 ElementCount--; … … 722 665 return walker; 723 666 } else { 724 Log() << Verbose(0) << "Atom not found in list." << endl;667 DoLog(0) && (Log() << Verbose(0) << "Atom not found in list." << endl); 725 668 return NULL; 726 669 } … … 738 681 //mol->Output((ofstream *)&cout); 739 682 //Log() << Verbose(0) << "===============================================" << endl; 740 Log() << Verbose(0) << text;683 DoLog(0) && (Log() << Verbose(0) << text); 741 684 cin >> No; 742 685 ion = this->FindAtom(No); … … 751 694 bool molecule::CheckBounds(const Vector *x) const 752 695 { 696 double * const cell_size = World::get()->cell_size; 753 697 bool result = true; 754 698 int j =-1; … … 826 770 void molecule::OutputListOfBonds() const 827 771 { 828 Log() << Verbose(2) << endl << "From Contents of ListOfBonds, all non-hydrogen atoms:" << endl;772 DoLog(2) && (Log() << Verbose(2) << endl << "From Contents of ListOfBonds, all non-hydrogen atoms:" << endl); 829 773 ActOnAllAtoms (&atom::OutputBondOfAtom ); 830 Log() << Verbose(0) << endl;774 DoLog(0) && (Log() << Verbose(0) << endl); 831 775 }; 832 776 … … 885 829 } 886 830 if ((AtomCount == 0) || (i != AtomCount)) { 887 Log() << Verbose(3) << "Mismatch in AtomCount " << AtomCount << " and recounted number " << i << ", renaming all." << endl;831 DoLog(3) && (Log() << Verbose(3) << "Mismatch in AtomCount " << AtomCount << " and recounted number " << i << ", renaming all." << endl); 888 832 AtomCount = i; 889 833 … … 901 845 Walker->Name = Malloc<char>(6, "molecule::CountAtoms: *walker->Name"); 902 846 sprintf(Walker->Name, "%2s%02d", Walker->type->symbol, Walker->nr+1); 903 Log() << Verbose(3) << "Naming atom nr. " << Walker->nr << " " << Walker->Name << "." << endl;847 DoLog(3) && (Log() << Verbose(3) << "Naming atom nr. " << Walker->nr << " " << Walker->Name << "." << endl); 904 848 i++; 905 849 } 906 850 } else 907 Log() << Verbose(3) << "AtomCount is still " << AtomCount << ", thus counting nothing." << endl;851 DoLog(3) && (Log() << Verbose(3) << "AtomCount is still " << AtomCount << ", thus counting nothing." << endl); 908 852 } 909 853 }; … … 965 909 bool result = true; // status of comparison 966 910 967 Log() << Verbose(3) << "Begin of IsEqualToWithinThreshold." << endl;911 DoLog(3) && (Log() << Verbose(3) << "Begin of IsEqualToWithinThreshold." << endl); 968 912 /// first count both their atoms and elements and update lists thereby ... 969 913 //Log() << Verbose(0) << "Counting atoms, updating list" << endl; … … 977 921 if (result) { 978 922 if (AtomCount != OtherMolecule->AtomCount) { 979 Log() << Verbose(4) << "AtomCounts don't match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;923 DoLog(4) && (Log() << Verbose(4) << "AtomCounts don't match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl); 980 924 result = false; 981 925 } else Log() << Verbose(4) << "AtomCounts match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl; … … 984 928 if (result) { 985 929 if (ElementCount != OtherMolecule->ElementCount) { 986 Log() << Verbose(4) << "ElementCount don't match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;930 DoLog(4) && (Log() << Verbose(4) << "ElementCount don't match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl); 987 931 result = false; 988 932 } else Log() << Verbose(4) << "ElementCount match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl; … … 996 940 } 997 941 if (flag < MAX_ELEMENTS) { 998 Log() << Verbose(4) << "ElementsInMolecule don't match." << endl;942 DoLog(4) && (Log() << Verbose(4) << "ElementsInMolecule don't match." << endl); 999 943 result = false; 1000 944 } else Log() << Verbose(4) << "ElementsInMolecule match." << endl; … … 1002 946 /// then determine and compare center of gravity for each molecule ... 1003 947 if (result) { 1004 Log() << Verbose(5) << "Calculating Centers of Gravity" << endl;948 DoLog(5) && (Log() << Verbose(5) << "Calculating Centers of Gravity" << endl); 1005 949 DeterminePeriodicCenter(CenterOfGravity); 1006 950 OtherMolecule->DeterminePeriodicCenter(OtherCenterOfGravity); 1007 Log() << Verbose(5) << "Center of Gravity: ";951 DoLog(5) && (Log() << Verbose(5) << "Center of Gravity: "); 1008 952 CenterOfGravity.Output(); 1009 Log() << Verbose(0) << endl << Verbose(5) << "Other Center of Gravity: ";953 DoLog(0) && (Log() << Verbose(0) << endl << Verbose(5) << "Other Center of Gravity: "); 1010 954 OtherCenterOfGravity.Output(); 1011 Log() << Verbose(0) << endl;955 DoLog(0) && (Log() << Verbose(0) << endl); 1012 956 if (CenterOfGravity.DistanceSquared(&OtherCenterOfGravity) > threshold*threshold) { 1013 Log() << Verbose(4) << "Centers of gravity don't match." << endl;957 DoLog(4) && (Log() << Verbose(4) << "Centers of gravity don't match." << endl); 1014 958 result = false; 1015 959 } … … 1018 962 /// ... then make a list with the euclidian distance to this center for each atom of both molecules 1019 963 if (result) { 1020 Log() << Verbose(5) << "Calculating distances" << endl;964 DoLog(5) && (Log() << Verbose(5) << "Calculating distances" << endl); 1021 965 Distances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: Distances"); 1022 966 OtherDistances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: OtherDistances"); … … 1025 969 1026 970 /// ... sort each list (using heapsort (o(N log N)) from GSL) 1027 Log() << Verbose(5) << "Sorting distances" << endl;971 DoLog(5) && (Log() << Verbose(5) << "Sorting distances" << endl); 1028 972 PermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermMap"); 1029 973 OtherPermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *OtherPermMap"); … … 1031 975 gsl_heapsort_index (OtherPermMap, OtherDistances, AtomCount, sizeof(double), CompareDoubles); 1032 976 PermutationMap = Calloc<int>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermutationMap"); 1033 Log() << Verbose(5) << "Combining Permutation Maps" << endl;977 DoLog(5) && (Log() << Verbose(5) << "Combining Permutation Maps" << endl); 1034 978 for(int i=AtomCount;i--;) 1035 979 PermutationMap[PermMap[i]] = (int) OtherPermMap[i]; 1036 980 1037 981 /// ... and compare them step by step, whether the difference is individually(!) below \a threshold for all 1038 Log() << Verbose(4) << "Comparing distances" << endl;982 DoLog(4) && (Log() << Verbose(4) << "Comparing distances" << endl); 1039 983 flag = 0; 1040 984 for (int i=0;i<AtomCount;i++) { 1041 Log() << Verbose(5) << "Distances squared: |" << Distances[PermMap[i]] << " - " << OtherDistances[OtherPermMap[i]] << "| = " << fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) << " ?<? " << threshold << endl;985 DoLog(5) && (Log() << Verbose(5) << "Distances squared: |" << Distances[PermMap[i]] << " - " << OtherDistances[OtherPermMap[i]] << "| = " << fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) << " ?<? " << threshold << endl); 1042 986 if (fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) > threshold*threshold) 1043 987 flag = 1; … … 1055 999 } 1056 1000 /// return pointer to map if all distances were below \a threshold 1057 Log() << Verbose(3) << "End of IsEqualToWithinThreshold." << endl;1001 DoLog(3) && (Log() << Verbose(3) << "End of IsEqualToWithinThreshold." << endl); 1058 1002 if (result) { 1059 Log() << Verbose(3) << "Result: Equal." << endl;1003 DoLog(3) && (Log() << Verbose(3) << "Result: Equal." << endl); 1060 1004 return PermutationMap; 1061 1005 } else { 1062 Log() << Verbose(3) << "Result: Not equal." << endl;1006 DoLog(3) && (Log() << Verbose(3) << "Result: Not equal." << endl); 1063 1007 return NULL; 1064 1008 } … … 1075 1019 { 1076 1020 atom *Walker = NULL, *OtherWalker = NULL; 1077 Log() << Verbose(3) << "Begin of GetFatherAtomicMap." << endl;1021 DoLog(3) && (Log() << Verbose(3) << "Begin of GetFatherAtomicMap." << endl); 1078 1022 int *AtomicMap = Malloc<int>(AtomCount, "molecule::GetAtomicMap: *AtomicMap"); 1079 1023 for (int i=AtomCount;i--;) … … 1082 1026 for (int i=AtomCount;i--;) // no need as -1 means already that there is trivial correspondence 1083 1027 AtomicMap[i] = i; 1084 Log() << Verbose(4) << "Map is trivial." << endl;1028 DoLog(4) && (Log() << Verbose(4) << "Map is trivial." << endl); 1085 1029 } else { 1086 Log() << Verbose(4) << "Map is ";1030 DoLog(4) && (Log() << Verbose(4) << "Map is "); 1087 1031 Walker = start; 1088 1032 while (Walker->next != end) { … … 1101 1045 } 1102 1046 } 1103 Log() << Verbose(0) << AtomicMap[Walker->nr] << "\t";1104 } 1105 Log() << Verbose(0) << endl;1106 } 1107 Log() << Verbose(3) << "End of GetFatherAtomicMap." << endl;1047 DoLog(0) && (Log() << Verbose(0) << AtomicMap[Walker->nr] << "\t"); 1048 } 1049 DoLog(0) && (Log() << Verbose(0) << endl); 1050 } 1051 DoLog(3) && (Log() << Verbose(3) << "End of GetFatherAtomicMap." << endl); 1108 1052 return AtomicMap; 1109 1053 }; … … 1141 1085 } 1142 1086 }; 1143 1144 void molecule::flipActiveFlag(){1145 ActiveFlag = !ActiveFlag;1146 }
Note:
See TracChangeset
for help on using the changeset viewer.