| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2020 Frederik Heber. All rights reserved.
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * StretchBond.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Oct 4, 2020
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "StretchBond.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <boost/bind.hpp>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 43 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 44 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "Atom/atom.hpp"
 | 
|---|
| 49 | #include "Bond/bond.hpp"
 | 
|---|
| 50 | #include "Box.hpp"
 | 
|---|
| 51 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 52 | #include "Descriptors/MoleculeIdDescriptor.hpp"
 | 
|---|
| 53 | #include "Element/element.hpp"
 | 
|---|
| 54 | #include "Graph/BondGraph.hpp"
 | 
|---|
| 55 | #include "Graph/BoostGraphCreator.hpp"
 | 
|---|
| 56 | #include "Graph/BreadthFirstSearchGatherer.hpp"
 | 
|---|
| 57 | #include "molecule.hpp"
 | 
|---|
| 58 | #include "World.hpp"
 | 
|---|
| 59 | 
 | 
|---|
| 60 | StretchBondUtil::StretchBondUtil(
 | 
|---|
| 61 |     const std::vector< atom *> _atoms
 | 
|---|
| 62 |     ) :
 | 
|---|
| 63 |     atoms(_atoms),
 | 
|---|
| 64 |     mol(NULL),
 | 
|---|
| 65 |     Shift(2, zeroVec),
 | 
|---|
| 66 |     atomids(2, -1),
 | 
|---|
| 67 |     domain(World::getInstance().getDomain()),
 | 
|---|
| 68 |     bondside_sets(2, BoostGraphHelpers::Nodeset_t())
 | 
|---|
| 69 | {
 | 
|---|
| 70 |   // check preconditions
 | 
|---|
| 71 |   ASSERT( atoms.size() == (size_t)2,
 | 
|---|
| 72 |       "StretchBondUtil::StretchBondUtil() - exactly two atoms must be selected.");
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   olddistance = atoms[0]->getPosition().distance(atoms[1]->getPosition());
 | 
|---|
| 75 |   LOG(1, "INFO: Old bond distance is " << olddistance << ".");
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   // gather sorted ids
 | 
|---|
| 78 |   atomids[0] = atoms[0]->getId();
 | 
|---|
| 79 |   atomids[1] = atoms[1]->getId();
 | 
|---|
| 80 |   std::sort(atomids.begin(), atomids.end());
 | 
|---|
| 81 |   LOG(1, "DEBUG: Selected nodes are " << atomids);
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   mol = World::getInstance().getMolecule(MoleculeById(atoms[0]->getMolecule()->getId()));
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | bool StretchBondUtil::addEdgePredicate(
 | 
|---|
| 87 |     const bond &_bond,
 | 
|---|
| 88 |     const std::vector<atomId_t> &_atomids)
 | 
|---|
| 89 | {
 | 
|---|
| 90 |   ASSERT(_atomids.size() == (size_t)2,
 | 
|---|
| 91 |       "addEdgePredicate() - atomids must contain exactly two ids.");
 | 
|---|
| 92 |   // do not add selected edge
 | 
|---|
| 93 |   return ((_bond.leftatom->getId() != _atomids[0])
 | 
|---|
| 94 |       || (_bond.rightatom->getId() != _atomids[1]));
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | bool StretchBondUtil::operator()(double newdistance)
 | 
|---|
| 98 | {
 | 
|---|
| 99 |   if (newdistance <= 0.) {
 | 
|---|
| 100 |     // get typical distance from table
 | 
|---|
| 101 |     BondGraph *&BG = World::getInstance().getBondGraph();
 | 
|---|
| 102 |     newdistance = BG->GetBondLength(
 | 
|---|
| 103 |         atoms[0]->getElementNo(),
 | 
|---|
| 104 |         atoms[1]->getElementNo());
 | 
|---|
| 105 |     LOG(1, "INFO: Using typical bond distance of " << newdistance
 | 
|---|
| 106 |         << " between " << atoms[0]->getElement().getSymbol() << " and "
 | 
|---|
| 107 |         << atoms[1]->getElement().getSymbol() << ".");
 | 
|---|
| 108 |   }
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   const Vector NormalVector = (atoms[0]->getPosition() - atoms[1]->getPosition())* (1./olddistance);
 | 
|---|
| 111 |   const double shift = 0.5*(newdistance - olddistance);
 | 
|---|
| 112 |   Shift[0] = shift * NormalVector;
 | 
|---|
| 113 |   Shift[1] = -shift * NormalVector;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |  if (mol != atoms[1]->getMolecule()) {
 | 
|---|
| 116 |     ELOG(1, "The two selected atoms must belong to the same molecule.");
 | 
|---|
| 117 |     return false;
 | 
|---|
| 118 |   }
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   // Assume the selected bond splits the molecule into two parts, each one on
 | 
|---|
| 121 |   // either side of the bond. We need to perform a BFS from each bond partner
 | 
|---|
| 122 |   // not using the selected bond. Therefrom, we obtain two sets of atoms/nodes.
 | 
|---|
| 123 |   // If both are disjoint, the bond is not contained in a cycle and we simply
 | 
|---|
| 124 |   // shift either set as desired. If not, then we simply shift each atom,
 | 
|---|
| 125 |   // leaving the other positions untouched.
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   // get nodes on either side of selected bond via BFS discovery
 | 
|---|
| 128 |   BoostGraphCreator BGcreator;
 | 
|---|
| 129 |   BGcreator.createFromMolecule(*mol,
 | 
|---|
| 130 |       boost::bind(addEdgePredicate, _1, boost::ref(atomids)));
 | 
|---|
| 131 |   BreadthFirstSearchGatherer NodeGatherer(BGcreator);
 | 
|---|
| 132 |   for(size_t j=0;j<2;++j) {
 | 
|---|
| 133 |     bondside_sets[j] = NodeGatherer(atoms[j]->getId());
 | 
|---|
| 134 |     std::sort(bondside_sets[j].begin(), bondside_sets[j].end());
 | 
|---|
| 135 |   }
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   // simple test whether bond has split the system in two disjoint sets or not
 | 
|---|
| 138 |   bool isCyclic = false;
 | 
|---|
| 139 |   if ((bondside_sets[0].size() + bondside_sets[1].size()) > BGcreator.getNumVertices()) {
 | 
|---|
| 140 |       // Check whether there are common nodes in each set of distances
 | 
|---|
| 141 |     if (BoostGraphHelpers::isCommonNodeInVector(bondside_sets[0], bondside_sets[1])) {
 | 
|---|
| 142 |       ELOG(2, "Sets contain common node, hence bond must have been by cyclic."
 | 
|---|
| 143 |           << " Shifting only bond partners.");
 | 
|---|
| 144 |       for(size_t j=0;j<2;++j) {
 | 
|---|
| 145 |         bondside_sets[j].clear();
 | 
|---|
| 146 |         bondside_sets[j].push_back( atomids[j] );
 | 
|---|
| 147 |         const Vector &position = atoms[j]->getPosition();
 | 
|---|
| 148 |         atoms[j]->setPosition( domain.enforceBoundaryConditions(position+Shift[j]) );
 | 
|---|
| 149 |       }
 | 
|---|
| 150 |       isCyclic = true;
 | 
|---|
| 151 |     }
 | 
|---|
| 152 |   }
 | 
|---|
| 153 | 
 | 
|---|
| 154 |   // go through the molecule and stretch each atom in either set of nodes
 | 
|---|
| 155 |   if (!isCyclic) {
 | 
|---|
| 156 |     for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
| 157 |       const Vector &position = (*iter)->getPosition();
 | 
|---|
| 158 |       // for each atom determine in which set of nodes it is and shift accordingly
 | 
|---|
| 159 |       const atomId_t &atomid = (*iter)->getId();
 | 
|---|
| 160 |       if (std::binary_search(bondside_sets[0].begin(), bondside_sets[0].end(), atomid)) {
 | 
|---|
| 161 |         (*iter)->setPosition( domain.enforceBoundaryConditions(position+Shift[0]) );
 | 
|---|
| 162 |       } else if (std::binary_search(bondside_sets[1].begin(), bondside_sets[1].end(), atomid)) {
 | 
|---|
| 163 |         (*iter)->setPosition( domain.enforceBoundaryConditions(position+Shift[1]) );
 | 
|---|
| 164 |       } else {
 | 
|---|
| 165 |         ELOG(1, "Atom " << *iter << " is not contained on either side of bond? Undoing done shifts");
 | 
|---|
| 166 |         // Have to undo shifts
 | 
|---|
| 167 |         for (size_t i=0;i<2;++i) {
 | 
|---|
| 168 |           for (BoostGraphHelpers::Nodeset_t::const_iterator iter = bondside_sets[i].begin();
 | 
|---|
| 169 |               iter != bondside_sets[i].end(); ++iter) {
 | 
|---|
| 170 |             atom &walker = *World::getInstance().getAtom(AtomById(*iter));
 | 
|---|
| 171 |             const Vector &position = walker.getPosition();
 | 
|---|
| 172 |             walker.setPosition( domain.enforceBoundaryConditions(position-Shift[i]) );
 | 
|---|
| 173 |           }
 | 
|---|
| 174 |         }
 | 
|---|
| 175 |         return false;
 | 
|---|
| 176 |       }
 | 
|---|
| 177 |     }
 | 
|---|
| 178 |   }
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   return true;
 | 
|---|
| 181 | }
 | 
|---|