| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [1e45f1f] | 5 |  * Copyright (C)  2013-2014 Frederik Heber. All rights reserved.
 | 
|---|
| [94d5ac6] | 6 |  * 
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 13 |  *    (at your option) any later version.
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 18 |  *    GNU General Public License for more details.
 | 
|---|
 | 19 |  *
 | 
|---|
 | 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
| [97ebf8] | 24 | /*
 | 
|---|
 | 25 |  * VerletIntegrationAction.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: May 10, 2010
 | 
|---|
 | 28 |  *      Author: heber
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
| [bf3817] | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
| [ad011c] | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 37 | 
 | 
|---|
| [1e45f1f] | 38 | #include "Actions/UndoRedoHelpers.hpp"
 | 
|---|
| [6f0841] | 39 | #include "Atom/atom.hpp"
 | 
|---|
| [1e45f1f] | 40 | #include "Atom/AtomicInfo.hpp"
 | 
|---|
| [6f0841] | 41 | #include "Atom/AtomSet.hpp"
 | 
|---|
| [ad011c] | 42 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 43 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [435065] | 44 | #include "Dynamics/VerletForceIntegration.hpp"
 | 
|---|
 | 45 | #include "molecule.hpp"
 | 
|---|
| [1a3c26] | 46 | #include "World.hpp"
 | 
|---|
| [bcb593] | 47 | #include "WorldTime.hpp"
 | 
|---|
| [97ebf8] | 48 | 
 | 
|---|
| [435065] | 49 | #include <vector>
 | 
|---|
| [97ebf8] | 50 | #include <iostream>
 | 
|---|
 | 51 | #include <fstream>
 | 
|---|
 | 52 | #include <string>
 | 
|---|
 | 53 | 
 | 
|---|
| [1fd675] | 54 | #include "Actions/MoleculeAction/VerletIntegrationAction.hpp"
 | 
|---|
| [bcd16a] | 55 | 
 | 
|---|
| [ce7fdc] | 56 | using namespace MoleCuilder;
 | 
|---|
 | 57 | 
 | 
|---|
| [8cc22f] | 58 | enum VectorIndexType {
 | 
|---|
| [1e45f1f] | 59 |   PositionIndex=0,
 | 
|---|
 | 60 |   VelocityIndex=1,
 | 
|---|
 | 61 |   ForceIndex=2,
 | 
|---|
 | 62 |   MAXINDEX
 | 
|---|
| [8cc22f] | 63 | };
 | 
|---|
| [1e45f1f] | 64 | 
 | 
|---|
| [1fd675] | 65 | // and construct the stuff
 | 
|---|
 | 66 | #include "VerletIntegrationAction.def"
 | 
|---|
 | 67 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 68 | /** =========== define the function ====================== */
 | 
|---|
| [b5b01e] | 69 | ActionState::ptr MoleculeVerletIntegrationAction::performCall() {
 | 
|---|
| [435065] | 70 |   AtomSetMixin<std::vector<atom *> > set(World::getInstance().getSelectedAtoms());
 | 
|---|
| [51cdfd] | 71 |   if (set.empty()) {
 | 
|---|
 | 72 |     LOG(0, "STATUS: No atoms selected.");
 | 
|---|
 | 73 |     return Action::failure;
 | 
|---|
 | 74 |   }
 | 
|---|
| [bcb593] | 75 |   // we always operate relative to current time step
 | 
|---|
| [1e45f1f] | 76 |   const size_t CurrentStep = WorldTime::getInstance().getTime();
 | 
|---|
| [bcb593] | 77 |   VerletForceIntegration<std::vector<atom *> > Verlet(set, params.Deltat.get(), true);
 | 
|---|
 | 78 |   // parse forces into next step
 | 
|---|
 | 79 |   if (!params.forcesfile.get().string().empty()) {
 | 
|---|
 | 80 |     LOG(1, "Parsing forces file.");
 | 
|---|
 | 81 |     if (!Verlet.parseForcesFile(params.forcesfile.get().string().c_str(), CurrentStep))
 | 
|---|
| [f10b0c] | 82 |       LOG(2, "File " << params.forcesfile.get() << " not found.");
 | 
|---|
| [97ebf8] | 83 |     else
 | 
|---|
| [f10b0c] | 84 |       LOG(2, "File " << params.forcesfile.get() << " found and parsed.");
 | 
|---|
| [97ebf8] | 85 |   }
 | 
|---|
| [1e45f1f] | 86 | 
 | 
|---|
 | 87 |   // create undo state for all selected atoms (undo info)
 | 
|---|
 | 88 |   std::vector<AtomicInfo> UndoInfo;
 | 
|---|
 | 89 |   UndoInfo.reserve(set.size());
 | 
|---|
 | 90 |   {
 | 
|---|
 | 91 |     for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
 | 92 |         iter != World::getInstance().endAtomSelection();
 | 
|---|
 | 93 |         ++iter)
 | 
|---|
 | 94 |       UndoInfo.push_back(AtomicInfo(*(iter->second)));
 | 
|---|
 | 95 |   }
 | 
|---|
 | 96 | 
 | 
|---|
| [bcb593] | 97 |   // perform velocity verlet update
 | 
|---|
| [1e45f1f] | 98 |   Verlet(CurrentStep+1, 1, 0, params.FixedCenterOfMass.get());
 | 
|---|
 | 99 |   LOG(0, "STATUS: Successfully performed updates on velocity and position.");
 | 
|---|
| [72e40d0] | 100 | //  // increment to next time step: re-creates bond graph
 | 
|---|
 | 101 | //  World::getInstance().setTime(CurrentStep+1);
 | 
|---|
| [1e45f1f] | 102 | 
 | 
|---|
 | 103 |   // create undo state for all selected atoms (redo info):
 | 
|---|
 | 104 |   // we need:
 | 
|---|
 | 105 |   // -# forces from last step (possible parsing forces file, already in UndoInfo)
 | 
|---|
 | 106 |   // -# velocities from last step (..UpdateU(), already in UndoInfo)
 | 
|---|
 | 107 |   // -# current position (..UpdateX())
 | 
|---|
 | 108 |   std::vector<Vectors_t> UpdatedStep(MAXINDEX);
 | 
|---|
 | 109 |   UpdatedStep[PositionIndex].reserve(set.size());
 | 
|---|
 | 110 |   UpdatedStep[VelocityIndex].reserve(set.size());
 | 
|---|
 | 111 |   UpdatedStep[ForceIndex].reserve(set.size());
 | 
|---|
 | 112 |   {
 | 
|---|
 | 113 |     for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
 | 114 |         iter != World::getInstance().endAtomSelection();
 | 
|---|
 | 115 |         ++iter) {
 | 
|---|
 | 116 |       UpdatedStep[PositionIndex].push_back(iter->second->getPositionAtStep(CurrentStep+1));
 | 
|---|
 | 117 |       UpdatedStep[VelocityIndex].push_back(iter->second->getAtomicVelocityAtStep(CurrentStep));
 | 
|---|
 | 118 |       UpdatedStep[ForceIndex].push_back(iter->second->getAtomicForceAtStep(CurrentStep));
 | 
|---|
 | 119 |     }
 | 
|---|
| [51cdfd] | 120 |   }
 | 
|---|
| [1e45f1f] | 121 |   MoleculeVerletIntegrationState *UndoState =
 | 
|---|
 | 122 |       new MoleculeVerletIntegrationState(UndoInfo, UpdatedStep, params);
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 |   return ActionState::ptr(UndoState);
 | 
|---|
| [97ebf8] | 125 | }
 | 
|---|
 | 126 | 
 | 
|---|
| [b5b01e] | 127 | ActionState::ptr MoleculeVerletIntegrationAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| [1e45f1f] | 128 |   MoleculeVerletIntegrationState *state =
 | 
|---|
 | 129 |       assert_cast<MoleculeVerletIntegrationState*>(_state.get());
 | 
|---|
| [97ebf8] | 130 | 
 | 
|---|
| [1e45f1f] | 131 |   // go back one step
 | 
|---|
 | 132 |   const size_t CurrentStep = WorldTime::getInstance().getTime();
 | 
|---|
| [72e40d0] | 133 | //  World::getInstance().setTime(CurrentStep-1);
 | 
|---|
| [97ebf8] | 134 | 
 | 
|---|
| [1e45f1f] | 135 |   // remove current step for all modified atoms
 | 
|---|
| [72e40d0] | 136 |   removeLastStep(getIdsFromAtomicInfo(state->UndoInfo), CurrentStep+1);
 | 
|---|
| [1e45f1f] | 137 | 
 | 
|---|
 | 138 |   // and set back the old step (forces have been changed)
 | 
|---|
 | 139 |   SetAtomsFromAtomicInfo(state->UndoInfo);
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 |   return ActionState::ptr(_state);
 | 
|---|
| [97ebf8] | 142 | }
 | 
|---|
 | 143 | 
 | 
|---|
| [b5b01e] | 144 | ActionState::ptr MoleculeVerletIntegrationAction::performRedo(ActionState::ptr _state){
 | 
|---|
| [1e45f1f] | 145 |   MoleculeVerletIntegrationState *state =
 | 
|---|
 | 146 |       assert_cast<MoleculeVerletIntegrationState*>(_state.get());
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 |   // set forces and velocities
 | 
|---|
 | 149 |   ResetAtomVelocity(state->UndoInfo, state->UpdatedStep[VelocityIndex]);
 | 
|---|
 | 150 |   ResetAtomForce(state->UndoInfo, state->UpdatedStep[ForceIndex]);
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 |   // set stored new state
 | 
|---|
| [72e40d0] | 153 |   size_t CurrentStep = WorldTime::getInstance().getTime();
 | 
|---|
 | 154 |   addNewStep(state->UndoInfo, CurrentStep+1);
 | 
|---|
| [1e45f1f] | 155 | 
 | 
|---|
 | 156 |   // add a new time step
 | 
|---|
| [72e40d0] | 157 |   World::getInstance().setTime(CurrentStep+1);
 | 
|---|
| [1e45f1f] | 158 | 
 | 
|---|
 | 159 |   // and set positions of the new step
 | 
|---|
 | 160 |   ResetAtomPosition(state->UndoInfo, state->UpdatedStep[PositionIndex]);
 | 
|---|
 | 161 | 
 | 
|---|
| [72e40d0] | 162 |   // step back to old time step
 | 
|---|
 | 163 |   World::getInstance().setTime(CurrentStep-1);
 | 
|---|
 | 164 | 
 | 
|---|
| [1e45f1f] | 165 |   return ActionState::ptr(_state);
 | 
|---|
| [97ebf8] | 166 | }
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 | bool MoleculeVerletIntegrationAction::canUndo() {
 | 
|---|
| [e69c87] | 169 |   return true;
 | 
|---|
| [97ebf8] | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 | bool MoleculeVerletIntegrationAction::shouldUndo() {
 | 
|---|
| [e69c87] | 173 |   return true;
 | 
|---|
| [97ebf8] | 174 | }
 | 
|---|
| [1fd675] | 175 | /** =========== end of function ====================== */
 | 
|---|