| 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 |  * CreateAdjacencyAction.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 "CodePatterns/Log.hpp"
 | 
|---|
| 29 | #include "CodePatterns/Range.hpp"
 | 
|---|
| 30 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 31 | #include "config.hpp"
 | 
|---|
| 32 | #include "linkedcell.hpp"
 | 
|---|
| 33 | #include "molecule.hpp"
 | 
|---|
| 34 | #include "PointCloudAdaptor.hpp"
 | 
|---|
| 35 | #include "World.hpp"
 | 
|---|
| 36 | #include "WorldTime.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include <iostream>
 | 
|---|
| 39 | #include <list>
 | 
|---|
| 40 | #include <string>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
 | 
|---|
| 43 | 
 | 
|---|
| 44 | using namespace std;
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "Actions/FragmentationAction/CreateAdjacencyAction.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | // and construct the stuff
 | 
|---|
| 49 | #include "CreateAdjacencyAction.def"
 | 
|---|
| 50 | #include "Action_impl_pre.hpp"
 | 
|---|
| 51 | /** =========== define the function ====================== */
 | 
|---|
| 52 | Action::state_ptr FragmentationCreateAdjacencyAction::performCall() {
 | 
|---|
| 53 |   // obtain information
 | 
|---|
| 54 |   getParametersfromValueStorage();
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   DoLog(1) && (Log() << Verbose(1) << "Constructing bond graph for selected atoms ... " << endl);
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   BondGraph *BG = World::getInstance().getBondGraph();
 | 
|---|
| 59 |   ASSERT(BG != NULL, "FragmentationCreateAdjacencyAction: BondGraph is NULL.");
 | 
|---|
| 60 |   double BondDistance = BG->getMaxPossibleBondDistance(AtomSetMixin<std::vector<atom *> >(World::getInstance().getSelectedAtoms()));
 | 
|---|
| 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;
 | 
|---|
| 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 |     TesselPointSTLList list;
 | 
|---|
| 89 |     for (World::AtomSelectionIterator AtomRunner = World::getInstance().beginAtomSelection();
 | 
|---|
| 90 |         AtomRunner != World::getInstance().endAtomSelection();
 | 
|---|
| 91 |         ++AtomRunner) {
 | 
|---|
| 92 |       list.push_back(AtomRunner->second);
 | 
|---|
| 93 |     }
 | 
|---|
| 94 |     PointCloudAdaptor< TesselPointSTLList > cloud(&list, "AtomSelection");
 | 
|---|
| 95 |     LC = new LinkedCell(cloud, BondDistance);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |     // create a list to map Tesselpoint::Nr to atom *
 | 
|---|
| 98 |     DoLog(2) && (Log() << Verbose(2) << "Creating TesselPoint to atom map ... " << endl);
 | 
|---|
| 99 | 
 | 
|---|
| 100 |     // set numbers for atoms that can later be used
 | 
|---|
| 101 |     std::map<TesselPoint *, int> AtomIds;
 | 
|---|
| 102 |     int i=0;
 | 
|---|
| 103 |     for (World::AtomSelectionIterator AtomRunner = World::getInstance().beginAtomSelection();
 | 
|---|
| 104 |         AtomRunner != World::getInstance().endAtomSelection();
 | 
|---|
| 105 |         ++AtomRunner) {
 | 
|---|
| 106 |       AtomIds.insert(pair<TesselPoint *, int> (AtomRunner->second, i++) );
 | 
|---|
| 107 |     }
 | 
|---|
| 108 | 
 | 
|---|
| 109 |     // 3a. go through every cell
 | 
|---|
| 110 |     DoLog(2) && (Log() << Verbose(2) << "Celling ... " << endl);
 | 
|---|
| 111 |     for (LC->n[0] = 0; LC->n[0] < LC->N[0]; LC->n[0]++)
 | 
|---|
| 112 |       for (LC->n[1] = 0; LC->n[1] < LC->N[1]; LC->n[1]++)
 | 
|---|
| 113 |         for (LC->n[2] = 0; LC->n[2] < LC->N[2]; LC->n[2]++) {
 | 
|---|
| 114 |           const TesselPointSTLList *List = LC->GetCurrentCell();
 | 
|---|
| 115 | //          Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl;
 | 
|---|
| 116 |           if (List != NULL) {
 | 
|---|
| 117 |             for (TesselPointSTLList::const_iterator Runner = List->begin();
 | 
|---|
| 118 |                 Runner != List->end();
 | 
|---|
| 119 |                 Runner++) {
 | 
|---|
| 120 |               Walker = dynamic_cast<atom*>(*Runner);
 | 
|---|
| 121 |               ASSERT(Walker,"Tesselpoint that was not an atom retrieved from LinkedNode");
 | 
|---|
| 122 |               //Log() << Verbose(0) << "Current Atom is " << *Walker << "." << endl;
 | 
|---|
| 123 |               // 3c. check for possible bond between each atom in this and every one in the 27 cells
 | 
|---|
| 124 |               for (n[0] = -1; n[0] <= 1; n[0]++)
 | 
|---|
| 125 |                 for (n[1] = -1; n[1] <= 1; n[1]++)
 | 
|---|
| 126 |                   for (n[2] = -1; n[2] <= 1; n[2]++) {
 | 
|---|
| 127 |                     const TesselPointSTLList *OtherList = LC->GetRelativeToCurrentCell(n);
 | 
|---|
| 128 | //                    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;
 | 
|---|
| 129 |                     if (OtherList != NULL) {
 | 
|---|
| 130 |                       for (TesselPointSTLList::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) {
 | 
|---|
| 131 |                         if (AtomIds.find(*OtherRunner)->second > AtomIds.find(Walker)->second) {
 | 
|---|
| 132 |                           OtherWalker = dynamic_cast<atom*>(*OtherRunner);
 | 
|---|
| 133 |                           ASSERT(OtherWalker,"TesselPoint that was not an atom retrieved from LinkedNode");
 | 
|---|
| 134 |                           //Log() << Verbose(1) << "Checking distance " << OtherWalker->x.PeriodicDistanceSquared(&(Walker->x), cell_size) << " against typical bond length of " << bonddistance*bonddistance << "." << endl;
 | 
|---|
| 135 |                           range<double> MinMaxDistanceSquared(0.,0.);
 | 
|---|
| 136 |                           BG->getMinMaxDistanceSquared(Walker, OtherWalker, MinMaxDistanceSquared);
 | 
|---|
| 137 |                           const double distance = domain.periodicDistanceSquared(OtherWalker->getPosition(),Walker->getPosition());
 | 
|---|
| 138 |                           const bool status = MinMaxDistanceSquared.isInRange(distance);
 | 
|---|
| 139 | //                          LOG3, "INFO: MinMaxDistanceSquared interval is " << MinMaxDistanceSquared << ".");
 | 
|---|
| 140 |                           if (AtomIds[OtherWalker->father] > AtomIds[Walker->father]) {
 | 
|---|
| 141 |                             if (status) { // create bond if distance is smaller
 | 
|---|
| 142 | //                              Log() << Verbose(1) << "Adding Bond between " << *Walker << " and " << *OtherWalker << " in distance " << sqrt(distance) << "." << endl;
 | 
|---|
| 143 |                               bond * const Binder = new bond(Walker->father, OtherWalker->father, 1, BondCount++);
 | 
|---|
| 144 |                               Walker->father->RegisterBond(WorldTime::getTime(),Binder);
 | 
|---|
| 145 |                               OtherWalker->father->RegisterBond(WorldTime::getTime(),Binder);
 | 
|---|
| 146 |                             } else {
 | 
|---|
| 147 | //                              Log() << Verbose(1) << "Not Adding: distance too great." << endl;
 | 
|---|
| 148 |                             }
 | 
|---|
| 149 |                           } else {
 | 
|---|
| 150 | //                            Log() << Verbose(1) << "Not Adding: Wrong order of labels." << endl;
 | 
|---|
| 151 |                           }
 | 
|---|
| 152 |                         }
 | 
|---|
| 153 |                       }
 | 
|---|
| 154 |                     }
 | 
|---|
| 155 |                   }
 | 
|---|
| 156 |             }
 | 
|---|
| 157 |           }
 | 
|---|
| 158 |         }
 | 
|---|
| 159 |     delete (LC);
 | 
|---|
| 160 |     DoLog(1) && (Log() << Verbose(1) << "I detected " << BondCount << " bonds in the molecule with distance " << BondDistance << "." << endl);
 | 
|---|
| 161 | 
 | 
|---|
| 162 |     // correct bond degree by comparing valence and bond degree
 | 
|---|
| 163 |     DoLog(2) && (Log() << Verbose(2) << "Correcting bond degree ... " << endl);
 | 
|---|
| 164 |     //CorrectBondDegree();
 | 
|---|
| 165 | 
 | 
|---|
| 166 |   } else
 | 
|---|
| 167 |     DoLog(1) && (Log() << Verbose(1) << "AtomCount is " << AtomCount << ", thus no bonds, no connections!." << endl);
 | 
|---|
| 168 |   DoLog(0) && (Log() << Verbose(0) << "End of CreateAdjacencyList." << endl);
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   return Action::success;
 | 
|---|
| 171 | }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | Action::state_ptr FragmentationCreateAdjacencyAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 174 | //  FragmentationCreateAdjacencyState *state = assert_cast<FragmentationCreateAdjacencyState*>(_state.get());
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   return Action::success;
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | Action::state_ptr FragmentationCreateAdjacencyAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 180 |   return Action::success;
 | 
|---|
| 181 | }
 | 
|---|
| 182 | 
 | 
|---|
| 183 | bool FragmentationCreateAdjacencyAction::canUndo() {
 | 
|---|
| 184 |   return false;
 | 
|---|
| 185 | }
 | 
|---|
| 186 | 
 | 
|---|
| 187 | bool FragmentationCreateAdjacencyAction::shouldUndo() {
 | 
|---|
| 188 |   return false;
 | 
|---|
| 189 | }
 | 
|---|
| 190 | /** =========== end of function ====================== */
 | 
|---|