| [c449d9] | 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 |  * ConstructBondGraphAction.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: May 9, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
 | 23 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | #include "atom.hpp"
 | 
|---|
 | 26 | #include "bond.hpp"
 | 
|---|
 | 27 | #include "bondgraph.hpp"
 | 
|---|
 | 28 | #include "config.hpp"
 | 
|---|
 | 29 | #include "linkedcell.hpp"
 | 
|---|
 | 30 | #include "Helpers/Log.hpp"
 | 
|---|
 | 31 | #include "Helpers/Verbose.hpp"
 | 
|---|
 | 32 | #include "molecule.hpp"
 | 
|---|
 | 33 | #include "World.hpp"
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include <iostream>
 | 
|---|
 | 36 | #include <string>
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | using namespace std;
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | #include "Actions/FragmentationAction/ConstructBondGraphAction.hpp"
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | // and construct the stuff
 | 
|---|
 | 45 | #include "ConstructBondGraphAction.def"
 | 
|---|
 | 46 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 47 | /** =========== define the function ====================== */
 | 
|---|
 | 48 | Action::state_ptr FragmentationConstructBondGraphAction::performCall() {
 | 
|---|
 | 49 |   // obtain information
 | 
|---|
 | 50 |   getParametersfromValueStorage();
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |   DoLog(1) && (Log() << Verbose(1) << "Constructing bond graph for selected atoms ... " << endl);
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 |   config *configuration = World::getInstance().getConfig();
 | 
|---|
 | 55 |   BondGraph *BG = configuration->BG;
 | 
|---|
 | 56 |   ASSERT(BG != NULL, "FragmentationConstructBondGraphAction: BondGraph is NULL.");
 | 
|---|
 | 57 |   double BondDistance = BG->getMaxDistance();
 | 
|---|
 | 58 |   bool IsAngstroem = configuration->GetIsAngstroem();
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 |   atom *Walker = NULL;
 | 
|---|
 | 61 |   atom *OtherWalker = NULL;
 | 
|---|
 | 62 |   int n[NDIM];
 | 
|---|
 | 63 |   double MinDistance, MaxDistance;
 | 
|---|
 | 64 |   LinkedCell *LC = NULL;
 | 
|---|
 | 65 |   Box &domain = World::getInstance().getDomain();
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |   // remove every bond from the selected atoms' list
 | 
|---|
 | 68 |   int AtomCount = 0;
 | 
|---|
 | 69 |   for (World::AtomSelectionIterator AtomRunner = World::getInstance().beginAtomSelection(); AtomRunner != World::getInstance().endAtomSelection(); ++AtomRunner) {
 | 
|---|
 | 70 |     AtomCount++;
 | 
|---|
 | 71 |     for(BondList::iterator BondRunner = (AtomRunner->second)->ListOfBonds.begin(); !(AtomRunner->second)->ListOfBonds.empty(); BondRunner = (AtomRunner->second)->ListOfBonds.begin())
 | 
|---|
 | 72 |       if ((*BondRunner)->leftatom == AtomRunner->second)
 | 
|---|
 | 73 |         delete((*BondRunner));
 | 
|---|
 | 74 |   }
 | 
|---|
 | 75 |   int BondCount = 0;
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |   // count atoms in molecule = dimension of matrix (also give each unique name and continuous numbering)
 | 
|---|
 | 78 |   DoLog(1) && (Log() << Verbose(1) << "AtomCount " << AtomCount << " and bonddistance is " << BondDistance << "." << endl);
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 |   if ((AtomCount > 1) && (BondDistance > 1.)) {
 | 
|---|
 | 81 |     DoLog(2) && (Log() << Verbose(2) << "Creating Linked Cell structure ... " << endl);
 | 
|---|
 | 82 |     LinkedCell::LinkedNodes list;
 | 
|---|
 | 83 |     for (World::AtomSelectionIterator AtomRunner = World::getInstance().beginAtomSelection(); AtomRunner != World::getInstance().endAtomSelection(); ++AtomRunner) {
 | 
|---|
 | 84 |       list.push_back(AtomRunner->second);
 | 
|---|
 | 85 |     }
 | 
|---|
 | 86 |     LC = new LinkedCell(list, BondDistance);
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |     // create a list to map Tesselpoint::nr to atom *
 | 
|---|
 | 89 |     DoLog(2) && (Log() << Verbose(2) << "Creating TesselPoint to atom map ... " << endl);
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |     // set numbers for atoms that can later be used
 | 
|---|
 | 92 |     std::map<TesselPoint *, int> AtomIds;
 | 
|---|
 | 93 |     int i=0;
 | 
|---|
 | 94 |     for (World::AtomSelectionIterator AtomRunner = World::getInstance().beginAtomSelection(); AtomRunner != World::getInstance().endAtomSelection(); ++AtomRunner) {
 | 
|---|
 | 95 |       AtomIds.insert(pair<TesselPoint *, int> (AtomRunner->second, i++) );
 | 
|---|
 | 96 |     }
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |     // 3a. go through every cell
 | 
|---|
 | 99 |     DoLog(2) && (Log() << Verbose(2) << "Celling ... " << endl);
 | 
|---|
 | 100 |     for (LC->n[0] = 0; LC->n[0] < LC->N[0]; LC->n[0]++)
 | 
|---|
 | 101 |       for (LC->n[1] = 0; LC->n[1] < LC->N[1]; LC->n[1]++)
 | 
|---|
 | 102 |         for (LC->n[2] = 0; LC->n[2] < LC->N[2]; LC->n[2]++) {
 | 
|---|
 | 103 |           const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
 | 104 | //          Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl;
 | 
|---|
 | 105 |           if (List != NULL) {
 | 
|---|
 | 106 |             for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
 | 107 |               Walker = dynamic_cast<atom*>(*Runner);
 | 
|---|
 | 108 |               ASSERT(Walker,"Tesselpoint that was not an atom retrieved from LinkedNode");
 | 
|---|
 | 109 |               //Log() << Verbose(0) << "Current Atom is " << *Walker << "." << endl;
 | 
|---|
 | 110 |               // 3c. check for possible bond between each atom in this and every one in the 27 cells
 | 
|---|
 | 111 |               for (n[0] = -1; n[0] <= 1; n[0]++)
 | 
|---|
 | 112 |                 for (n[1] = -1; n[1] <= 1; n[1]++)
 | 
|---|
 | 113 |                   for (n[2] = -1; n[2] <= 1; n[2]++) {
 | 
|---|
 | 114 |                     const LinkedCell::LinkedNodes *OtherList = LC->GetRelativeToCurrentCell(n);
 | 
|---|
 | 115 | //                    Log() << Verbose(2) << "Current relative cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl;
 | 
|---|
 | 116 |                     if (OtherList != NULL) {
 | 
|---|
 | 117 |                       for (LinkedCell::LinkedNodes::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) {
 | 
|---|
 | 118 |                         if (AtomIds.find(*OtherRunner)->second > AtomIds.find(Walker)->second) {
 | 
|---|
 | 119 |                           OtherWalker = dynamic_cast<atom*>(*OtherRunner);
 | 
|---|
 | 120 |                           ASSERT(OtherWalker,"TesselPoint that was not an atom retrieved from LinkedNode");
 | 
|---|
 | 121 |                           //Log() << Verbose(1) << "Checking distance " << OtherWalker->x.PeriodicDistanceSquared(&(Walker->x), cell_size) << " against typical bond length of " << bonddistance*bonddistance << "." << endl;
 | 
|---|
 | 122 |                           BG->CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
 | 
|---|
 | 123 |                           const double distance = domain.periodicDistanceSquared(OtherWalker->getPosition(),Walker->getPosition());
 | 
|---|
 | 124 |                           const bool status = (distance <= MaxDistance * MaxDistance) && (distance >= MinDistance * MinDistance);
 | 
|---|
 | 125 | //                          Log() << Verbose(1) << "MinDistance is " << MinDistance << " and MaxDistance is " << MaxDistance << "." << endl;
 | 
|---|
 | 126 |                           if (AtomIds[OtherWalker->father] > AtomIds[Walker->father]) {
 | 
|---|
 | 127 |                             if (status) { // create bond if distance is smaller
 | 
|---|
 | 128 | //                              Log() << Verbose(1) << "Adding Bond between " << *Walker << " and " << *OtherWalker << " in distance " << sqrt(distance) << "." << endl;
 | 
|---|
 | 129 |                               bond * const Binder = new bond(Walker->father, OtherWalker->father, 1, BondCount++);
 | 
|---|
 | 130 |                               Walker->father->RegisterBond(Binder);
 | 
|---|
 | 131 |                               OtherWalker->father->RegisterBond(Binder);
 | 
|---|
 | 132 |                             } else {
 | 
|---|
 | 133 | //                              Log() << Verbose(1) << "Not Adding: distance too great." << endl;
 | 
|---|
 | 134 |                             }
 | 
|---|
 | 135 |                           } else {
 | 
|---|
 | 136 | //                            Log() << Verbose(1) << "Not Adding: Wrong order of labels." << endl;
 | 
|---|
 | 137 |                           }
 | 
|---|
 | 138 |                         }
 | 
|---|
 | 139 |                       }
 | 
|---|
 | 140 |                     }
 | 
|---|
 | 141 |                   }
 | 
|---|
 | 142 |             }
 | 
|---|
 | 143 |           }
 | 
|---|
 | 144 |         }
 | 
|---|
 | 145 |     delete (LC);
 | 
|---|
 | 146 |     DoLog(1) && (Log() << Verbose(1) << "I detected " << BondCount << " bonds in the molecule with distance " << BondDistance << "." << endl);
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 |     // correct bond degree by comparing valence and bond degree
 | 
|---|
 | 149 |     DoLog(2) && (Log() << Verbose(2) << "Correcting bond degree ... " << endl);
 | 
|---|
 | 150 |     //CorrectBondDegree();
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 |   } else
 | 
|---|
 | 153 |     DoLog(1) && (Log() << Verbose(1) << "AtomCount is " << AtomCount << ", thus no bonds, no connections!." << endl);
 | 
|---|
 | 154 |   DoLog(0) && (Log() << Verbose(0) << "End of CreateAdjacencyList." << endl);
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 |   return Action::success;
 | 
|---|
 | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | Action::state_ptr FragmentationConstructBondGraphAction::performUndo(Action::state_ptr _state) {
 | 
|---|
 | 160 | //  FragmentationConstructBondGraphState *state = assert_cast<FragmentationConstructBondGraphState*>(_state.get());
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 |   return Action::success;
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | Action::state_ptr FragmentationConstructBondGraphAction::performRedo(Action::state_ptr _state){
 | 
|---|
 | 166 |   return Action::success;
 | 
|---|
 | 167 | }
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 | bool FragmentationConstructBondGraphAction::canUndo() {
 | 
|---|
 | 170 |   return false;
 | 
|---|
 | 171 | }
 | 
|---|
 | 172 | 
 | 
|---|
 | 173 | bool FragmentationConstructBondGraphAction::shouldUndo() {
 | 
|---|
 | 174 |   return false;
 | 
|---|
 | 175 | }
 | 
|---|
 | 176 | /** =========== end of function ====================== */
 | 
|---|