1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. 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 | * StretchBondAction.cpp
|
---|
25 | *
|
---|
26 | * Created on: Sep 26, 2012
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include <boost/graph/adjacency_list.hpp>
|
---|
36 | #include <boost/graph/breadth_first_search.hpp>
|
---|
37 | #include <boost/graph/subgraph.hpp>
|
---|
38 |
|
---|
39 | //#include "CodePatterns/MemDebug.hpp"
|
---|
40 |
|
---|
41 | #include "Actions/MoleculeAction/StretchBondAction.hpp"
|
---|
42 |
|
---|
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 "Descriptors/AtomIdDescriptor.hpp"
|
---|
51 | #include "molecule.hpp"
|
---|
52 | #include "World.hpp"
|
---|
53 |
|
---|
54 | using namespace MoleCuilder;
|
---|
55 |
|
---|
56 | // and construct the stuff
|
---|
57 | #include "StretchBondAction.def"
|
---|
58 | #include "Action_impl_pre.hpp"
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * I have no idea why this is so complicated with BGL ...
|
---|
63 | *
|
---|
64 | * This is taken from the book "The Boost Graph Library: User Guide and Reference Manual, Portable Documents",
|
---|
65 | * chapter "Basic Graph Algorithms", example on calculating the bacon number.
|
---|
66 | */
|
---|
67 | template <typename DistanceMap>
|
---|
68 | class distance_recorder : public boost::default_bfs_visitor
|
---|
69 | {
|
---|
70 | public:
|
---|
71 | distance_recorder(DistanceMap dist) : d(dist) {}
|
---|
72 |
|
---|
73 | template <typename Edge, typename Graph>
|
---|
74 | void tree_edge(Edge e, const Graph &g) const {
|
---|
75 | typename boost::graph_traits<Graph>::vertex_descriptor u = source(e,g), v = target(e,g);
|
---|
76 | d[v] = d[u] + 1;
|
---|
77 | }
|
---|
78 |
|
---|
79 | private:
|
---|
80 | DistanceMap d;
|
---|
81 | };
|
---|
82 |
|
---|
83 | template <typename DistanceMap>
|
---|
84 | distance_recorder<DistanceMap> record_distance(DistanceMap d)
|
---|
85 | {
|
---|
86 | return distance_recorder<DistanceMap>(d);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** =========== define the function ====================== */
|
---|
90 | ActionState::ptr MoleculeStretchBondAction::performCall()
|
---|
91 | {
|
---|
92 | // check preconditions
|
---|
93 | const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
94 | if (atoms.size() != 2) {
|
---|
95 | STATUS("Exactly two atoms must be selected.");
|
---|
96 | return Action::failure;
|
---|
97 | }
|
---|
98 | std::vector<atomId_t> atomids(2);
|
---|
99 | atomids[0] = atoms[0]->getId();
|
---|
100 | atomids[1] = atoms[1]->getId();
|
---|
101 | std::sort(atomids.begin(), atomids.end());
|
---|
102 | LOG(1, "DEBUG: Selected nodes are " << atomids);
|
---|
103 | molecule *mol = World::getInstance().
|
---|
104 | getMolecule(MoleculeById(atoms[0]->getMolecule()->getId()));
|
---|
105 | if (mol != atoms[1]->getMolecule()) {
|
---|
106 | STATUS("The two selected atoms must belong to the same molecule.");
|
---|
107 | return Action::failure;
|
---|
108 | }
|
---|
109 | // gather undo information
|
---|
110 | const double olddistance = atoms[0]->getPosition().distance(atoms[1]->getPosition());
|
---|
111 | const double newdistance = params.bonddistance.get();
|
---|
112 | LOG(1, "INFO: Old bond distance is " << olddistance << ", stretching to " << newdistance << ".");
|
---|
113 |
|
---|
114 | // Assume the selected bond splits the molecule into two parts, each one on
|
---|
115 | // either side of the bond. We need to perform a BFS from each bond partner
|
---|
116 | // not using the selected bond. Therefrom, we obtain two sets of atoms/nodes.
|
---|
117 | // If both are disjoint, the bond is not contained in a cycle and we simply
|
---|
118 | // shift either set as desired. If not, then we simply shift each atom,
|
---|
119 | // leaving the other positions untouched.
|
---|
120 | typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS,
|
---|
121 | boost::no_property, boost::no_property > UndirectedGraph;
|
---|
122 |
|
---|
123 | // convert BondGraph into boost::graph
|
---|
124 | UndirectedGraph molgraph(mol->getAtomCount());
|
---|
125 | size_t no_edges = 0;
|
---|
126 | for(molecule::const_iterator iter = const_cast<const molecule * const>(mol)->begin();
|
---|
127 | iter != const_cast<const molecule * const>(mol)->end(); ++iter) {
|
---|
128 | LOG(2, "DEBUG: Looking at node " << (*iter)->getId());
|
---|
129 | const BondList& ListOfBonds = (*iter)->getListOfBonds();
|
---|
130 | for(BondList::const_iterator bonditer = ListOfBonds.begin();
|
---|
131 | bonditer != ListOfBonds.end(); ++bonditer) {
|
---|
132 | const unsigned int &leftid = (*bonditer)->leftatom->getId();
|
---|
133 | const unsigned int &rightid = (*bonditer)->rightatom->getId();
|
---|
134 | // only add each edge once and do not add selected edge
|
---|
135 | if ((leftid == (*iter)->getId())
|
---|
136 | && ((leftid != atomids[0]) || (rightid != atomids[1]))) {
|
---|
137 | LOG(1, "DEBUG: ADDING edge " << leftid << " <-> " << rightid);
|
---|
138 | boost::add_edge(leftid, rightid, molgraph);
|
---|
139 | ++no_edges;
|
---|
140 | } else {
|
---|
141 | LOG(1, "DEBUG: Discarding edge " << leftid << " <-> " << rightid);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | typedef boost::property_map < boost::adjacency_list <>, boost::vertex_index_t >::type index_map_t;
|
---|
146 | index_map_t index_map = boost::get(boost::vertex_index, molgraph);
|
---|
147 | const size_t num_vertices = boost::num_vertices(molgraph);
|
---|
148 | LOG(2, "DEBUG: We have " << num_vertices << " nodes and " << no_edges
|
---|
149 | << " edges in the molecule graph.");
|
---|
150 |
|
---|
151 | std::vector< std::vector<size_t> > distances;
|
---|
152 | for (size_t i=0;i<2;++i) {
|
---|
153 | distances.push_back(std::vector<size_t>(num_vertices, num_vertices+1)); // set distance to num+1
|
---|
154 | distances[i][atomids[i]] = 0;
|
---|
155 | boost::breadth_first_search(
|
---|
156 | molgraph,
|
---|
157 | boost::vertex(atomids[i], molgraph),
|
---|
158 | boost::visitor(record_distance(&(distances[i][0]))));
|
---|
159 | LOG(3, "DEBUG: From atom #" << atomids[i]
|
---|
160 | << " BFS discovered the following distances " << distances[i]);
|
---|
161 | }
|
---|
162 |
|
---|
163 | const Vector NormalVector = (atoms[0]->getPosition() - atoms[1]->getPosition())* (1./olddistance);
|
---|
164 | const double shift = 0.5*(newdistance - olddistance);
|
---|
165 | std::vector<Vector> Shift(2);
|
---|
166 | Shift[0] = shift * NormalVector;
|
---|
167 | Shift[1] = -shift * NormalVector;
|
---|
168 | Box &domain = World::getInstance().getDomain();
|
---|
169 | std::vector< std::vector<size_t> > bondside_sets(2);
|
---|
170 |
|
---|
171 | // Check whether there are common nodes in each set of distances
|
---|
172 | for (size_t i=0;i<num_vertices;++i)
|
---|
173 | if ((distances[0][i] != (num_vertices+1)) && (distances[1][i] != (num_vertices+1))) {
|
---|
174 | ELOG(2, "Node #" << i << " is reachable from either side of bond, hence must by cyclic."
|
---|
175 | << " Shifting only bond partners.");
|
---|
176 | for(size_t j=0;j<2;++j) {
|
---|
177 | bondside_sets[j].push_back(atoms[j]->getId());
|
---|
178 | const Vector &position = atoms[j]->getPosition();
|
---|
179 | atoms[j]->setPosition( domain.enforceBoundaryConditions(position+Shift[j]) );
|
---|
180 | }
|
---|
181 | break;
|
---|
182 | }
|
---|
183 |
|
---|
184 | // go through the molecule and stretch each atom in either set of nodes
|
---|
185 | if (bondside_sets[0].empty()) {
|
---|
186 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
---|
187 | const Vector &position = (*iter)->getPosition();
|
---|
188 | // for each atom determine in which set of nodes it is and shift accordingly
|
---|
189 | size_t i=0;
|
---|
190 | const size_t nodeindex = boost::get(index_map, boost::vertex((*iter)->getId(), molgraph));
|
---|
191 | for (;i<2;++i) {
|
---|
192 | if (distances[i][nodeindex] != (num_vertices+1)) {
|
---|
193 | (*iter)->setPosition( domain.enforceBoundaryConditions(position+Shift[i]) );
|
---|
194 | bondside_sets[i].push_back((*iter)->getId());
|
---|
195 | break;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | if (i==2) {
|
---|
199 | ELOG(1, "Atom " << *iter << " is not contained on either side of bond? Undoing done shifts");
|
---|
200 | // Have to undo shifts
|
---|
201 | for (i=0;i<2;++i) {
|
---|
202 | for (std::vector<size_t>::const_iterator iter = bondside_sets[i].begin();
|
---|
203 | iter != bondside_sets[i].end(); ++iter) {
|
---|
204 | atom &walker = *World::getInstance().getAtom(AtomById(*iter));
|
---|
205 | const Vector &position = walker.getPosition();
|
---|
206 | walker.setPosition( domain.enforceBoundaryConditions(position-Shift[i]) );
|
---|
207 | }
|
---|
208 | }
|
---|
209 | return Action::failure;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | MoleculeStretchBondState *UndoState = new MoleculeStretchBondState(Shift, bondside_sets, mol, params);
|
---|
215 | return ActionState::ptr(UndoState);
|
---|
216 | }
|
---|
217 |
|
---|
218 | ActionState::ptr MoleculeStretchBondAction::performUndo(ActionState::ptr _state) {
|
---|
219 | MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get());
|
---|
220 |
|
---|
221 | // use given plane to undo
|
---|
222 | Box &domain = World::getInstance().getDomain();
|
---|
223 | for (size_t i=0;i<2;++i) {
|
---|
224 | for (std::vector<size_t>::const_iterator iter = state->bondside_sets[i].begin();
|
---|
225 | iter != state->bondside_sets[i].end(); ++iter) {
|
---|
226 | atom &walker = *World::getInstance().getAtom(AtomById(*iter));
|
---|
227 | const Vector &position = walker.getPosition();
|
---|
228 | walker.setPosition( domain.enforceBoundaryConditions(position-state->Shift[i]) );
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | return ActionState::ptr(_state);
|
---|
233 | }
|
---|
234 |
|
---|
235 | ActionState::ptr MoleculeStretchBondAction::performRedo(ActionState::ptr _state){
|
---|
236 | MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get());
|
---|
237 |
|
---|
238 | Box &domain = World::getInstance().getDomain();
|
---|
239 | for (size_t i=0;i<2;++i) {
|
---|
240 | for (std::vector<size_t>::const_iterator iter = state->bondside_sets[i].begin();
|
---|
241 | iter != state->bondside_sets[i].end(); ++iter) {
|
---|
242 | atom &walker = *World::getInstance().getAtom(AtomById(*iter));
|
---|
243 | const Vector &position = walker.getPosition();
|
---|
244 | walker.setPosition( domain.enforceBoundaryConditions(position+state->Shift[i]) );
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | return ActionState::ptr(_state);
|
---|
249 | }
|
---|
250 |
|
---|
251 | bool MoleculeStretchBondAction::canUndo() {
|
---|
252 | return true;
|
---|
253 | }
|
---|
254 |
|
---|
255 | bool MoleculeStretchBondAction::shouldUndo() {
|
---|
256 | return true;
|
---|
257 | }
|
---|
258 | /** =========== end of function ====================== */
|
---|