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