| [1a48d2] | 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 |
|
|---|
| [9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp"
|
|---|
| [1a48d2] | 36 |
|
|---|
| [1e4f0a] | 37 | #include "Actions/ActionExceptions.hpp"
|
|---|
| 38 | #include "Actions/MakroAction.hpp"
|
|---|
| [1a48d2] | 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 |
|
|---|
| 57 | using namespace MoleCuilder;
|
|---|
| 58 |
|
|---|
| 59 | enum 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 ====================== */
|
|---|
| 69 | ActionState::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 | }
|
|---|
| [1050ac] | 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 |
|
|---|
| [26de2a] | 89 | // instantiate optimizer
|
|---|
| [1a48d2] | 90 | ForceAnnealing<std::vector<atom *> > optimizer(
|
|---|
| 91 | set,
|
|---|
| [6dfa37] | 92 | params.deltat.get(),
|
|---|
| [1a48d2] | 93 | true,
|
|---|
| [58787b] | 94 | params.steps.get(),
|
|---|
| [539845] | 95 | params.MaxDistance.get(),
|
|---|
| 96 | params.DampingFactor.get());
|
|---|
| [26de2a] | 97 | size_t CurrentStep = WorldTime::getInstance().getTime();
|
|---|
| 98 |
|
|---|
| 99 | // parse forces into current step
|
|---|
| [1a48d2] | 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 |
|
|---|
| [26de2a] | 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
|
|---|
| [5f18f1] | 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));
|
|---|
| [26de2a] | 122 | }
|
|---|
| 123 | // increment to next time step: re-creates bond graph
|
|---|
| 124 | ++CurrentStep;
|
|---|
| 125 | World::getInstance().setTime(CurrentStep);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| [1a48d2] | 128 | // perform optimization step
|
|---|
| 129 | LOG(1, "Structural optimization.");
|
|---|
| [1e4f0a] | 130 | const bool StopStatus = optimizer(CurrentStep, 1, params.UseBondGraph.get());
|
|---|
| [1a48d2] | 131 | STATUS("Successfully optimized structure by one step.");
|
|---|
| 132 |
|
|---|
| [1e4f0a] | 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 |
|
|---|
| [1a48d2] | 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 |
|
|---|
| 161 | ActionState::ptr MoleculeForceAnnealingAction::performUndo(ActionState::ptr _state) {
|
|---|
| 162 | MoleculeForceAnnealingState *state =
|
|---|
| 163 | assert_cast<MoleculeForceAnnealingState*>(_state.get());
|
|---|
| [cee565] | 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);
|
|---|
| [1a48d2] | 169 |
|
|---|
| 170 | // set stored old state
|
|---|
| 171 | SetAtomsFromAtomicInfo(state->UndoInfo);
|
|---|
| 172 |
|
|---|
| 173 | return ActionState::ptr(_state);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | ActionState::ptr MoleculeForceAnnealingAction::performRedo(ActionState::ptr _state){
|
|---|
| 177 | MoleculeForceAnnealingState *state =
|
|---|
| 178 | assert_cast<MoleculeForceAnnealingState*>(_state.get());
|
|---|
| [cee565] | 179 | const size_t CurrentStep = WorldTime::getInstance().getTime();
|
|---|
| 180 | World::getInstance().setTime(CurrentStep+1);
|
|---|
| [1a48d2] | 181 |
|
|---|
| 182 | // set stored new state
|
|---|
| 183 | SetAtomsFromAtomicInfo(state->RedoInfo);
|
|---|
| 184 |
|
|---|
| 185 | return ActionState::ptr(_state);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | bool MoleculeForceAnnealingAction::canUndo() {
|
|---|
| 189 | return true;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | bool MoleculeForceAnnealingAction::shouldUndo() {
|
|---|
| 193 | return true;
|
|---|
| 194 | }
|
|---|
| 195 | /** =========== end of function ====================== */
|
|---|