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