source: src/Bond/StretchBond.cpp@ 9f55b9

Candidate_v1.7.0 stable
Last change on this file since 9f55b9 was 9f55b9, checked in by Frederik Heber <frederik.heber@…>, 5 years ago

Extracted functor StretchBondUtil from StretchBondAction.

  • Property mode set to 100644
File size: 6.0 KB
Line 
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 "Graph/BoostGraphCreator.hpp"
54#include "Graph/BreadthFirstSearchGatherer.hpp"
55#include "molecule.hpp"
56#include "World.hpp"
57
58StretchBondUtil::StretchBondUtil(
59 const std::vector< atom *> _atoms
60 ) :
61 atoms(_atoms),
62 mol(NULL),
63 Shift(2, zeroVec),
64 atomids(2, -1),
65 domain(World::getInstance().getDomain()),
66 bondside_sets(2, BoostGraphHelpers::Nodeset_t())
67{
68 // check preconditions
69 ASSERT( atoms.size() == (size_t)2,
70 "StretchBondUtil::StretchBondUtil() - exactly two atoms must be selected.");
71
72 olddistance = atoms[0]->getPosition().distance(atoms[1]->getPosition());
73 LOG(1, "INFO: Old bond distance is " << olddistance << ".");
74
75 // gather sorted ids
76 atomids[0] = atoms[0]->getId();
77 atomids[1] = atoms[1]->getId();
78 std::sort(atomids.begin(), atomids.end());
79 LOG(1, "DEBUG: Selected nodes are " << atomids);
80
81 mol = World::getInstance().getMolecule(MoleculeById(atoms[0]->getMolecule()->getId()));
82}
83
84bool StretchBondUtil::addEdgePredicate(
85 const bond &_bond,
86 const std::vector<atomId_t> &_atomids)
87{
88 ASSERT(_atomids.size() == (size_t)2,
89 "addEdgePredicate() - atomids must contain exactly two ids.");
90 // do not add selected edge
91 return ((_bond.leftatom->getId() != _atomids[0])
92 || (_bond.rightatom->getId() != _atomids[1]));
93}
94
95bool StretchBondUtil::operator()(const double newdistance)
96{
97 const Vector NormalVector = (atoms[0]->getPosition() - atoms[1]->getPosition())* (1./olddistance);
98 const double shift = 0.5*(newdistance - olddistance);
99 Shift[0] = shift * NormalVector;
100 Shift[1] = -shift * NormalVector;
101
102 if (mol != atoms[1]->getMolecule()) {
103 ELOG(1, "The two selected atoms must belong to the same molecule.");
104 return false;
105 }
106
107 // Assume the selected bond splits the molecule into two parts, each one on
108 // either side of the bond. We need to perform a BFS from each bond partner
109 // not using the selected bond. Therefrom, we obtain two sets of atoms/nodes.
110 // If both are disjoint, the bond is not contained in a cycle and we simply
111 // shift either set as desired. If not, then we simply shift each atom,
112 // leaving the other positions untouched.
113
114 // get nodes on either side of selected bond via BFS discovery
115 BoostGraphCreator BGcreator;
116 BGcreator.createFromMolecule(*mol,
117 boost::bind(addEdgePredicate, _1, boost::ref(atomids)));
118 BreadthFirstSearchGatherer NodeGatherer(BGcreator);
119 for(size_t j=0;j<2;++j) {
120 bondside_sets[j] = NodeGatherer(atoms[j]->getId());
121 std::sort(bondside_sets[j].begin(), bondside_sets[j].end());
122 }
123
124 // simple test whether bond has split the system in two disjoint sets or not
125 bool isCyclic = false;
126 if ((bondside_sets[0].size() + bondside_sets[1].size()) > BGcreator.getNumVertices()) {
127 // Check whether there are common nodes in each set of distances
128 if (BoostGraphHelpers::isCommonNodeInVector(bondside_sets[0], bondside_sets[1])) {
129 ELOG(2, "Sets contain common node, hence bond must have been by cyclic."
130 << " Shifting only bond partners.");
131 for(size_t j=0;j<2;++j) {
132 bondside_sets[j].clear();
133 bondside_sets[j].push_back( atomids[j] );
134 const Vector &position = atoms[j]->getPosition();
135 atoms[j]->setPosition( domain.enforceBoundaryConditions(position+Shift[j]) );
136 }
137 isCyclic = true;
138 }
139 }
140
141 // go through the molecule and stretch each atom in either set of nodes
142 if (!isCyclic) {
143 for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
144 const Vector &position = (*iter)->getPosition();
145 // for each atom determine in which set of nodes it is and shift accordingly
146 const atomId_t &atomid = (*iter)->getId();
147 if (std::binary_search(bondside_sets[0].begin(), bondside_sets[0].end(), atomid)) {
148 (*iter)->setPosition( domain.enforceBoundaryConditions(position+Shift[0]) );
149 } else if (std::binary_search(bondside_sets[1].begin(), bondside_sets[1].end(), atomid)) {
150 (*iter)->setPosition( domain.enforceBoundaryConditions(position+Shift[1]) );
151 } else {
152 ELOG(1, "Atom " << *iter << " is not contained on either side of bond? Undoing done shifts");
153 // Have to undo shifts
154 for (size_t i=0;i<2;++i) {
155 for (BoostGraphHelpers::Nodeset_t::const_iterator iter = bondside_sets[i].begin();
156 iter != bondside_sets[i].end(); ++iter) {
157 atom &walker = *World::getInstance().getAtom(AtomById(*iter));
158 const Vector &position = walker.getPosition();
159 walker.setPosition( domain.enforceBoundaryConditions(position-Shift[i]) );
160 }
161 }
162 return false;
163 }
164 }
165 }
166
167 return true;
168}
Note: See TracBrowser for help on using the repository browser.