source: src/Actions/MoleculeAction/ChangeBondAngleAction.cpp@ 4882d5

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 4882d5 was 8b886f, checked in by Frederik Heber <heber@…>, 12 years ago

Added ChangeBondAngleAction that changes the bond angle while keeping the distance.

  • also added regression tests with undo/redo.
  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[8b886f]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 * ChangeBondAngleAction.cpp
25 *
26 * Created on: Nov 14, 2012
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include "Actions/MoleculeAction/ChangeBondAngleAction.hpp"
38
39#include <boost/assign.hpp>
40
41#include "CodePatterns/Log.hpp"
42#include "CodePatterns/Verbose.hpp"
43
44#include "LinearAlgebra/Plane.hpp"
45#include "LinearAlgebra/Vector.hpp"
46
47#include "Atom/atom.hpp"
48#include "Bond/bond.hpp"
49#include "CodePatterns/Log.hpp"
50#include "CodePatterns/Verbose.hpp"
51#include "Descriptors/AtomIdDescriptor.hpp"
52#include "molecule.hpp"
53#include "World.hpp"
54#include "WorldTime.hpp"
55
56using namespace boost::assign;
57using namespace MoleCuilder;
58
59// and construct the stuff
60#include "ChangeBondAngleAction.def"
61#include "Action_impl_pre.hpp"
62/** =========== define the function ====================== */
63
64const std::vector<size_t> sortIndicesFromCentralAtom(const std::vector< atom *> &atoms)
65{
66 std::vector<size_t> indices;
67 {
68 std::vector<size_t> matches;
69 size_t index = 0;
70 for (; index < 3; ++index)
71 if ((atoms[index]->IsBondedTo(WorldTime::getTime(), atoms[(index+1)%3]))
72 && (atoms[index]->IsBondedTo(WorldTime::getTime(), atoms[(index+2)%3])))
73 matches.push_back(index);
74 if (matches.size() != 1) {
75 ELOG(1, "The three atoms are not linearly bonded.");
76 } else {
77 LOG(1, "INFO: Picking " << *atoms[ matches[0] ] << " as the central atom.");
78 indices += matches[0], (matches[0]+1)%3, (matches[0]+2)%3;
79 }
80 }
81 return indices;
82}
83
84void ChangeBondAngleSymmetrically(
85 const std::vector<size_t> &indices,
86 const double newangle,
87 const std::vector<atom*> &atoms)
88{
89 const Vector firstPosition = atoms[ indices[1] ]->getPosition();
90 const Vector secondPosition = atoms[ indices[2] ]->getPosition();
91
92 // create the bond plane and mid-distance
93 const double normaldistance = firstPosition.distance(secondPosition);
94 const Vector NormalVector = (1. / normaldistance) * (firstPosition - secondPosition);
95 const Vector OffsetVector = atoms[indices[0]]->getPosition();
96 Plane midplane(NormalVector, OffsetVector);
97 // go through the molecule and stretch each atom relative two plane
98 for (size_t index = 1; index < 3; ++index) {
99 const Vector &position = atoms[indices[index]]->getPosition();
100 const Vector planespot = midplane.getClosestPoint(position);
101 const double opposite_side = position.distance(planespot);
102 const double adjacent_side = OffsetVector.distance(planespot);
103 const double hypotenuse = position.distance(OffsetVector);
104 if (fabs(hypotenuse) > MYEPSILON) {
105 const double angle = acos(adjacent_side / hypotenuse);
106 LOG(1, "INFO: Old angle is " << (180. / M_PI) * (2. * angle) << ".");
107 const double new_opposite_side = hypotenuse * sin(newangle);
108 const double new_adjacent_side = hypotenuse * cos(newangle);
109 Vector newposition = (new_opposite_side / opposite_side) * (position - planespot);
110 newposition += (new_adjacent_side / adjacent_side) * (planespot - OffsetVector);
111 newposition += OffsetVector;
112 atoms[indices[index]]->setPosition(newposition);
113 }
114 }
115}
116
117Action::state_ptr MoleculeChangeBondAngleAction::performCall()
118{
119 // check precondition: three atoms
120 const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
121 if (atoms.size() != 3) {
122 ELOG(1, "Exactly three atoms must be selected.");
123 return Action::failure;
124 }
125 // check precondition: linearly bonded atoms
126 const std::vector<size_t> indices = sortIndicesFromCentralAtom(atoms);
127 if (indices.size() != 3)
128 return Action::failure;
129 const molecule *mol = atoms[0]->getMolecule();
130 if ((mol != atoms[1]->getMolecule()) || (mol != atoms[2]->getMolecule())) {
131 ELOG(1, "The two selected atoms must belong to the same molecule.");
132 return Action::failure;
133 }
134
135 // gather undo info
136 const Vector firstPosition = atoms[ indices[1] ]->getPosition();
137 const Vector secondPosition = atoms[ indices[2] ]->getPosition();
138
139 // change the bond angle
140 const double newangle = .5* (M_PI / 180.) * params.bondangle.get();
141 LOG(1, "INFO: New angle is " << (180. / M_PI) * (2. * newangle) << ".");
142 ChangeBondAngleSymmetrically(indices, newangle, atoms);
143
144 // create undo information
145 MoleculeChangeBondAngleState *UndoState = new MoleculeChangeBondAngleState(
146 indices[1], indices[2],
147 firstPosition, secondPosition,
148 atoms[ indices[1] ]->getPosition(), atoms[ indices[2] ]->getPosition(),
149 params);
150 return Action::state_ptr(UndoState);
151}
152
153Action::state_ptr MoleculeChangeBondAngleAction::performUndo(Action::state_ptr _state) {
154 MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
155
156 // undo both position changes
157 atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
158 atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
159 first->setPosition(state->firstOldPosition);
160 second->setPosition(state->secondOldPosition);
161
162 return Action::state_ptr(_state);
163}
164
165Action::state_ptr MoleculeChangeBondAngleAction::performRedo(Action::state_ptr _state){
166 MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
167
168 // redo both position changes
169 atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
170 atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
171 first->setPosition(state->firstNewPosition);
172 second->setPosition(state->secondNewPosition);
173
174 return Action::state_ptr(_state);
175}
176
177bool MoleculeChangeBondAngleAction::canUndo() {
178 return true;
179}
180
181bool MoleculeChangeBondAngleAction::shouldUndo() {
182 return true;
183}
184/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.