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