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