| [13a953] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | /* | 
|---|
|  | 9 | * CheckAgainstAdjacencyFile.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Mar 3, 2011 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include <iostream> | 
|---|
|  | 23 |  | 
|---|
|  | 24 | #include "CheckAgainstAdjacencyFile.hpp" | 
|---|
|  | 25 |  | 
|---|
|  | 26 | #include "atom.hpp" | 
|---|
|  | 27 | #include "Bond/bond.hpp" | 
|---|
|  | 28 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 29 |  | 
|---|
|  | 30 | CheckAgainstAdjacencyFile::CheckAgainstAdjacencyFile() : | 
|---|
|  | 31 | CurrentBonds(new int[MAXBONDS]), | 
|---|
|  | 32 | status(true), | 
|---|
|  | 33 | NonMatchNumber(0) | 
|---|
|  | 34 | { | 
|---|
|  | 35 | for(int i=0;i<MAXBONDS;i++) | 
|---|
|  | 36 | CurrentBonds[i] = 0; | 
|---|
|  | 37 | } | 
|---|
|  | 38 |  | 
|---|
|  | 39 | CheckAgainstAdjacencyFile::~CheckAgainstAdjacencyFile() | 
|---|
|  | 40 | { | 
|---|
|  | 41 | delete[](CurrentBonds); | 
|---|
|  | 42 | } | 
|---|
|  | 43 |  | 
|---|
|  | 44 | void CheckAgainstAdjacencyFile::CompareBonds(const atom *&Walker, size_t &CurrentBondsOfAtom, int AtomNr, std::map<int, atom*> &ListOfAtoms) | 
|---|
|  | 45 | { | 
|---|
|  | 46 | size_t j = 0; | 
|---|
|  | 47 | int id = -1; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | //Log() << Verbose(2) << "Walker is " << *Walker << ", bond partners: "; | 
|---|
|  | 50 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
|  | 51 | if (CurrentBondsOfAtom == ListOfBonds.size()) { | 
|---|
|  | 52 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 53 | Runner != ListOfBonds.end(); | 
|---|
|  | 54 | ++Runner) { | 
|---|
|  | 55 | id = (*Runner)->GetOtherAtom(Walker)->getNr(); | 
|---|
|  | 56 | j = 0; | 
|---|
|  | 57 | for (; (j < CurrentBondsOfAtom) && (CurrentBonds[j++] != id);) | 
|---|
|  | 58 | ; // check against all parsed bonds | 
|---|
|  | 59 | if (CurrentBonds[j - 1] != id) { // no match ? Then mark in ListOfAtoms | 
|---|
|  | 60 | ListOfAtoms[AtomNr] = NULL; | 
|---|
|  | 61 | NonMatchNumber++; | 
|---|
|  | 62 | status = false; | 
|---|
|  | 63 | ELOG(2, id << " can not be found in list." << endl); | 
|---|
|  | 64 | } else { | 
|---|
|  | 65 | //Log() << Verbose(0) << "[" << id << "]\t"; | 
|---|
|  | 66 | } | 
|---|
|  | 67 | } | 
|---|
|  | 68 | //Log() << Verbose(0) << endl; | 
|---|
|  | 69 | } else { | 
|---|
|  | 70 | LOG(0, "STATUS: Number of bonds for Atom " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << ListOfBonds.size() << "."); | 
|---|
|  | 71 | status = false; | 
|---|
|  | 72 | } | 
|---|
|  | 73 | } | 
|---|
|  | 74 | ; | 
|---|
|  | 75 |  | 
|---|
|  | 76 | /** Checks contents of adjacency file against bond structure in structure molecule. | 
|---|
|  | 77 | * \param File file to parser | 
|---|
|  | 78 | * \param ListOfAtoms map from int (index in file) to atom | 
|---|
|  | 79 | * \return true - structure is equal, false - not equivalence | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | bool CheckAgainstAdjacencyFile::operator()(std::ifstream &File, std::map<int, atom*> ListOfAtoms) | 
|---|
|  | 82 | { | 
|---|
|  | 83 | LOG(0, "STATUS: Looking at bond structure stored in adjacency file and comparing to present one ... "); | 
|---|
|  | 84 | if (File.fail()) { | 
|---|
|  | 85 | LOG(1, "STATUS: Adjacency file not found." << endl); | 
|---|
|  | 86 | return false; | 
|---|
|  | 87 | } | 
|---|
|  | 88 |  | 
|---|
|  | 89 | char buffer[MAXSTRINGSIZE]; | 
|---|
|  | 90 | int tmp; | 
|---|
|  | 91 | // Parse the file line by line and count the bonds | 
|---|
|  | 92 | while (!File.eof()) { | 
|---|
|  | 93 | File.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 94 | stringstream line; | 
|---|
|  | 95 | line.str(buffer); | 
|---|
|  | 96 | int AtomNr = -1; | 
|---|
|  | 97 | line >> AtomNr; | 
|---|
|  | 98 | size_t CurrentBondsOfAtom = -1; // we count one too far due to line end | 
|---|
|  | 99 | // parse into structure | 
|---|
|  | 100 | if (AtomNr >= 0) { | 
|---|
|  | 101 | ASSERT(ListOfAtoms.count(AtomNr), | 
|---|
|  | 102 | "CheckAgainstAdjacencyFile::operator() - index " | 
|---|
|  | 103 | +toString(AtomNr)+" not present in ListOfAtoms."); | 
|---|
|  | 104 | const atom *Walker = ListOfAtoms[AtomNr]; | 
|---|
|  | 105 | while (line >> ws >> tmp) { | 
|---|
|  | 106 | LOG(3, "INFO: Recognized bond partner " << tmp); | 
|---|
|  | 107 | CurrentBonds[++CurrentBondsOfAtom] = tmp; | 
|---|
|  | 108 | ASSERT(CurrentBondsOfAtom < MAXBONDS, | 
|---|
|  | 109 | "molecule::CheckAdjacencyFileAgainstMolecule() - encountered more bonds than allowed: " | 
|---|
|  | 110 | +toString(CurrentBondsOfAtom)+" >= "+toString(int(MAXBONDS))+"!"); | 
|---|
|  | 111 | } | 
|---|
|  | 112 | // compare against present bonds | 
|---|
|  | 113 | CompareBonds(Walker, CurrentBondsOfAtom, AtomNr, ListOfAtoms); | 
|---|
|  | 114 | } else { | 
|---|
|  | 115 | if (AtomNr != -1) | 
|---|
|  | 116 | ELOG(2, AtomNr << " is negative."); | 
|---|
|  | 117 | } | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | if (status) { // if equal we parse the KeySetFile | 
|---|
|  | 121 | LOG(0, "STATUS: Equal."); | 
|---|
|  | 122 | } else | 
|---|
|  | 123 | LOG(0, "STATUS: Not equal by " << NonMatchNumber << " atoms."); | 
|---|
|  | 124 | return status; | 
|---|
|  | 125 | } | 
|---|
|  | 126 | ; | 
|---|