| 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 | 
 | 
|---|
| 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 |   }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   size_t CurrentStep = WorldTime::getInstance().getTime();
 | 
|---|
| 77 |   if (CurrentStep == 0) {
 | 
|---|
| 78 |     ELOG(1, "WorldTime must be at least at step 1 already, use step-world-time if necessary.");
 | 
|---|
| 79 |     return Action::failure;
 | 
|---|
| 80 |   }
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   // first, we need to sort the mixin according to their ids (as selected atoms are sorted
 | 
|---|
| 83 |   // according to their arbitrary address in memory)
 | 
|---|
| 84 |   set.sortByIds();
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   // create undo state for all selected atoms (undo info)
 | 
|---|
| 87 |   std::vector< std::vector<AtomicInfo> > UndoInfo(2);
 | 
|---|
| 88 |   for (int i=0;i<2;++i) {
 | 
|---|
| 89 |     UndoInfo[i].reserve(set.size());
 | 
|---|
| 90 |     {
 | 
|---|
| 91 |       for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
| 92 |           iter != World::getInstance().endAtomSelection();
 | 
|---|
| 93 |           ++iter)
 | 
|---|
| 94 |           UndoInfo[i].push_back(AtomicInfo(*(iter->second), CurrentStep-i));
 | 
|---|
| 95 |     }
 | 
|---|
| 96 |   }
 | 
|---|
| 97 |   std::vector<int> UndoTrajectorySize;
 | 
|---|
| 98 |   UndoTrajectorySize.reserve(set.size());
 | 
|---|
| 99 |   for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
| 100 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
| 101 |       ++iter)
 | 
|---|
| 102 |     UndoTrajectorySize.push_back(iter->second->getTrajectorySize());
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   // instantiate optimizer
 | 
|---|
| 105 |   ForceAnnealing<std::vector<atom *> > optimizer(
 | 
|---|
| 106 |       set,
 | 
|---|
| 107 |       params.deltat.get(),
 | 
|---|
| 108 |       true,
 | 
|---|
| 109 |       params.steps.get(),
 | 
|---|
| 110 |       params.MaxDistance.get(),
 | 
|---|
| 111 |       params.DampingFactor.get());
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   // parse forces into last step (assuming we stepped on already)
 | 
|---|
| 114 |   if (!params.forcesfile.get().string().empty()) {
 | 
|---|
| 115 |     LOG(1, "Parsing forces file.");
 | 
|---|
| 116 |     if (!optimizer.parseForcesFile(params.forcesfile.get().string().c_str(), CurrentStep-1))
 | 
|---|
| 117 |       LOG(2, "File " << params.forcesfile.get() << " not found.");
 | 
|---|
| 118 |     else
 | 
|---|
| 119 |       LOG(2, "File " << params.forcesfile.get() << " found and parsed.");
 | 
|---|
| 120 |   }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   // perform optimization step
 | 
|---|
| 123 |   LOG(1, "Structural optimization.");
 | 
|---|
| 124 |   const bool StopStatus = optimizer(CurrentStep, 1, params.UseBondGraph.get());
 | 
|---|
| 125 |   STATUS("Successfully optimized structure by one step.");
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   if (StopStatus && ActionQueue::getInstance().isMakroAction()) {
 | 
|---|
| 128 |     // send stop signal if we are taking part in MakroAction
 | 
|---|
| 129 |     MakroAction * const makroaction =
 | 
|---|
| 130 |         dynamic_cast<MakroAction *>(
 | 
|---|
| 131 |             const_cast<Action *>(
 | 
|---|
| 132 |                 &ActionQueue::getInstance().getCurrentAction()));
 | 
|---|
| 133 |     if (makroaction != NULL) {
 | 
|---|
| 134 |       makroaction->setLoop(makroaction->getStep());
 | 
|---|
| 135 |     } else {
 | 
|---|
| 136 |       ELOG(2, "ActionQueue said we are inside process, but current Action is not a process?");
 | 
|---|
| 137 |       // do nothing
 | 
|---|
| 138 |     }
 | 
|---|
| 139 |   }
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   std::vector< std::vector<AtomicInfo> > RedoInfo(2);
 | 
|---|
| 142 |   for (int i=0;i<2;++i) {
 | 
|---|
| 143 |     RedoInfo[i].reserve(set.size());
 | 
|---|
| 144 |     {
 | 
|---|
| 145 |       for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
| 146 |           iter != World::getInstance().endAtomSelection();
 | 
|---|
| 147 |           ++iter)
 | 
|---|
| 148 |         RedoInfo[i].push_back(AtomicInfo(*(iter->second), CurrentStep-i));
 | 
|---|
| 149 |     }
 | 
|---|
| 150 |   }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |   MoleculeForceAnnealingState *UndoState =
 | 
|---|
| 153 |     new MoleculeForceAnnealingState(UndoInfo, UndoTrajectorySize, RedoInfo, params);
 | 
|---|
| 154 | 
 | 
|---|
| 155 |   return ActionState::ptr(UndoState);
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | ActionState::ptr MoleculeForceAnnealingAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| 159 |   MoleculeForceAnnealingState *state =
 | 
|---|
| 160 |       assert_cast<MoleculeForceAnnealingState*>(_state.get());
 | 
|---|
| 161 |   const size_t CurrentStep = WorldTime::getInstance().getTime();
 | 
|---|
| 162 | 
 | 
|---|
| 163 |   // set stored old state and remove current one from trajectory if we set it
 | 
|---|
| 164 |   for (int i=0;i<2;++i) {
 | 
|---|
| 165 |     for(size_t j=0;j<state->UndoInfo[i].size();++j) {
 | 
|---|
| 166 |       const AtomicInfo &_atominfo = state->UndoInfo[i][j];
 | 
|---|
| 167 |       const atomId_t id = _atominfo.getId();
 | 
|---|
| 168 |       atom * const _atom = World::getInstance().getAtom(AtomById(id));
 | 
|---|
| 169 |       ASSERT( _atom != NULL,
 | 
|---|
| 170 |           "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id "
 | 
|---|
| 171 |           +toString(id)+" in the world.");
 | 
|---|
| 172 |       if (state->UndoTrajectorySize[j] > CurrentStep-i)
 | 
|---|
| 173 |         _atominfo.setAtom( *_atom, CurrentStep-i );
 | 
|---|
| 174 |       else
 | 
|---|
| 175 |         _atom->removeSteps(state->UndoTrajectorySize[j], CurrentStep-i);
 | 
|---|
| 176 |     }
 | 
|---|
| 177 |   }
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   return ActionState::ptr(_state);
 | 
|---|
| 180 | }
 | 
|---|
| 181 | 
 | 
|---|
| 182 | ActionState::ptr MoleculeForceAnnealingAction::performRedo(ActionState::ptr _state){
 | 
|---|
| 183 |   MoleculeForceAnnealingState *state =
 | 
|---|
| 184 |       assert_cast<MoleculeForceAnnealingState*>(_state.get());
 | 
|---|
| 185 |   const size_t CurrentStep = WorldTime::getInstance().getTime();
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   // set stored new state
 | 
|---|
| 188 |   for (int i=0;i<2;++i)
 | 
|---|
| 189 |     SetAtomsFromAtomicInfo(state->RedoInfo[i], CurrentStep-i);
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   return ActionState::ptr(_state);
 | 
|---|
| 192 | }
 | 
|---|
| 193 | 
 | 
|---|
| 194 | bool MoleculeForceAnnealingAction::canUndo() {
 | 
|---|
| 195 |   return true;
 | 
|---|
| 196 | }
 | 
|---|
| 197 | 
 | 
|---|
| 198 | bool MoleculeForceAnnealingAction::shouldUndo() {
 | 
|---|
| 199 |   return true;
 | 
|---|
| 200 | }
 | 
|---|
| 201 | /** =========== end of function ====================== */
 | 
|---|