source: src/Actions/MoleculeAction/ForceAnnealingAction.cpp@ 1db3520

Last change on this file since 1db3520 was 1e4f0a, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

ForceAnnealing returns whether to stop and ForceAnnealingAction stops MakroAction if inside such.

  • this is done by setting the MakroAction's loop variable to the current step which is now accessible.
  • ForceAnnealing currently has a fixed value of 1e-8 when the force components are below to stop annealing.
  • Property mode set to 100644
File size: 6.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2014 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 * ForceAnnealingAction.cpp
25 *
26 * Created on: Aug 02, 2014
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/ActionExceptions.hpp"
38#include "Actions/MakroAction.hpp"
39#include "Actions/UndoRedoHelpers.hpp"
40#include "Atom/atom.hpp"
41#include "Atom/AtomicInfo.hpp"
42#include "Atom/AtomSet.hpp"
43#include "CodePatterns/Log.hpp"
44#include "CodePatterns/Verbose.hpp"
45#include "Dynamics/ForceAnnealing.hpp"
46#include "molecule.hpp"
47#include "World.hpp"
48#include "WorldTime.hpp"
49
50#include <vector>
51#include <iostream>
52#include <fstream>
53#include <string>
54
55#include "Actions/MoleculeAction/ForceAnnealingAction.hpp"
56
57using namespace MoleCuilder;
58
59enum VectorIndexType {
60 PositionIndex=0,
61 VelocityIndex=1,
62 ForceIndex=2
63};
64
65// and construct the stuff
66#include "ForceAnnealingAction.def"
67#include "Action_impl_pre.hpp"
68/** =========== define the function ====================== */
69ActionState::ptr MoleculeForceAnnealingAction::performCall() {
70 AtomSetMixin<std::vector<atom *> > set(World::getInstance().getSelectedAtoms());
71 if (set.empty()) {
72 STATUS("No atoms selected.");
73 return Action::failure;
74 }
75 // first, we need to sort the mixin according to their ids (as selected atoms are sorted
76 // according to their arbitrary address in memory)
77 set.sortByIds();
78
79 // create undo state for all selected atoms (undo info)
80 std::vector<AtomicInfo> UndoInfo;
81 UndoInfo.reserve(set.size());
82 {
83 for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
84 iter != World::getInstance().endAtomSelection();
85 ++iter)
86 UndoInfo.push_back(AtomicInfo(*(iter->second)));
87 }
88
89 // instantiate optimizer
90 ForceAnnealing<std::vector<atom *> > optimizer(
91 set,
92 params.deltat.get(),
93 true,
94 params.steps.get(),
95 params.MaxDistance.get(),
96 params.DampingFactor.get());
97 size_t CurrentStep = WorldTime::getInstance().getTime();
98
99 // parse forces into current step
100 if (!params.forcesfile.get().string().empty()) {
101 LOG(1, "Parsing forces file.");
102 if (!optimizer.parseForcesFile(params.forcesfile.get().string().c_str(), CurrentStep))
103 LOG(2, "File " << params.forcesfile.get() << " not found.");
104 else
105 LOG(2, "File " << params.forcesfile.get() << " found and parsed.");
106 }
107
108 // copy current time step to new one and and proceed on this one
109 {
110 for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection();
111 iter != World::getInstance().endAtomSelection();
112 ++iter) {
113 atom * const Walker = iter->second;
114 Walker->setPositionAtStep(CurrentStep+1,
115 Walker->getPositionAtStep(CurrentStep));
116 // force have already been calculated, hence copy them
117 // the force to look at is always the one from the last step
118// Walker->setAtomicVelocityAtStep(CurrentStep+1,
119// Walker->getAtomicVelocityAtStep(CurrentStep));
120// Walker->setAtomicForceAtStep(CurrentStep+1,
121// Walker->getAtomicForceAtStep(CurrentStep));
122 }
123 // increment to next time step: re-creates bond graph
124 ++CurrentStep;
125 World::getInstance().setTime(CurrentStep);
126 }
127
128 // perform optimization step
129 LOG(1, "Structural optimization.");
130 const bool StopStatus = optimizer(CurrentStep, 1, params.UseBondGraph.get());
131 STATUS("Successfully optimized structure by one step.");
132
133 if (StopStatus && ActionQueue::getInstance().isMakroAction()) {
134 // send stop signal if we are taking part in MakroAction
135 MakroAction * const makroaction =
136 dynamic_cast<MakroAction *>(
137 const_cast<Action *>(
138 &ActionQueue::getInstance().getCurrentAction()));
139 if (makroaction != NULL) {
140 makroaction->setLoop(makroaction->getStep());
141 } else {
142 ELOG(2, "ActionQueue said we are inside process, but current Action is not a process?");
143 // do nothing
144 }
145 }
146
147 std::vector<AtomicInfo> RedoInfo;
148 RedoInfo.reserve(set.size());
149 {
150 for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
151 iter != World::getInstance().endAtomSelection();
152 ++iter)
153 RedoInfo.push_back(AtomicInfo(*(iter->second)));
154 }
155 MoleculeForceAnnealingState *UndoState =
156 new MoleculeForceAnnealingState(UndoInfo, RedoInfo, params);
157
158 return ActionState::ptr(UndoState);
159}
160
161ActionState::ptr MoleculeForceAnnealingAction::performUndo(ActionState::ptr _state) {
162 MoleculeForceAnnealingState *state =
163 assert_cast<MoleculeForceAnnealingState*>(_state.get());
164 const size_t CurrentStep = WorldTime::getInstance().getTime();
165 World::getInstance().setTime(CurrentStep-1);
166
167 // remove current step for all modified atoms
168 removeSteps(getIdsFromAtomicInfo(state->UndoInfo), CurrentStep, CurrentStep);
169
170 // set stored old state
171 SetAtomsFromAtomicInfo(state->UndoInfo);
172
173 return ActionState::ptr(_state);
174}
175
176ActionState::ptr MoleculeForceAnnealingAction::performRedo(ActionState::ptr _state){
177 MoleculeForceAnnealingState *state =
178 assert_cast<MoleculeForceAnnealingState*>(_state.get());
179 const size_t CurrentStep = WorldTime::getInstance().getTime();
180 World::getInstance().setTime(CurrentStep+1);
181
182 // set stored new state
183 SetAtomsFromAtomicInfo(state->RedoInfo);
184
185 return ActionState::ptr(_state);
186}
187
188bool MoleculeForceAnnealingAction::canUndo() {
189 return true;
190}
191
192bool MoleculeForceAnnealingAction::shouldUndo() {
193 return true;
194}
195/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.