source: src/Actions/MoleculeAction/ChangeBondAngleAction.cpp@ d93d2c

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 d93d2c was 26b4d62, checked in by Frederik Heber <heber@…>, 11 years ago

All Actions now give correct failure status via STATUS() macro.

  • Property mode set to 100644
File size: 6.6 KB
Line 
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
117ActionState::ptr MoleculeChangeBondAngleAction::performCall()
118{
119 // check precondition: three atoms
120 const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
121 if (atoms.size() != 3) {
122 STATUS("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 STATUS("Selected atoms must be linearly bonded/form a chain");
129 return Action::failure;
130 }
131 const molecule *mol = atoms[0]->getMolecule();
132 if ((mol != atoms[1]->getMolecule()) || (mol != atoms[2]->getMolecule())) {
133 STATUS("The two selected atoms must belong to the same molecule.");
134 return Action::failure;
135 }
136
137 // gather undo info
138 const Vector firstPosition = atoms[ indices[1] ]->getPosition();
139 const Vector secondPosition = atoms[ indices[2] ]->getPosition();
140
141 // change the bond angle
142 const double newangle = .5* (M_PI / 180.) * params.bondangle.get();
143 LOG(1, "INFO: New angle is " << (180. / M_PI) * (2. * newangle) << ".");
144 ChangeBondAngleSymmetrically(indices, newangle, atoms);
145
146 // create undo information
147 MoleculeChangeBondAngleState *UndoState = new MoleculeChangeBondAngleState(
148 indices[1], indices[2],
149 firstPosition, secondPosition,
150 atoms[ indices[1] ]->getPosition(), atoms[ indices[2] ]->getPosition(),
151 params);
152 return ActionState::ptr(UndoState);
153}
154
155ActionState::ptr MoleculeChangeBondAngleAction::performUndo(ActionState::ptr _state) {
156 MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
157
158 // undo both position changes
159 atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
160 atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
161 first->setPosition(state->firstOldPosition);
162 second->setPosition(state->secondOldPosition);
163
164 return ActionState::ptr(_state);
165}
166
167ActionState::ptr MoleculeChangeBondAngleAction::performRedo(ActionState::ptr _state){
168 MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
169
170 // redo both position changes
171 atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
172 atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
173 first->setPosition(state->firstNewPosition);
174 second->setPosition(state->secondNewPosition);
175
176 return ActionState::ptr(_state);
177}
178
179bool MoleculeChangeBondAngleAction::canUndo() {
180 return true;
181}
182
183bool MoleculeChangeBondAngleAction::shouldUndo() {
184 return true;
185}
186/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.