/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2014 Frederik Heber. All rights reserved. * * * This file is part of MoleCuilder. * * MoleCuilder is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * MoleCuilder is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MoleCuilder. If not, see . */ /* * ForceAnnealingAction.cpp * * Created on: Aug 02, 2014 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif //#include "CodePatterns/MemDebug.hpp" #include "Actions/UndoRedoHelpers.hpp" #include "Atom/atom.hpp" #include "Atom/AtomicInfo.hpp" #include "Atom/AtomSet.hpp" #include "CodePatterns/Log.hpp" #include "CodePatterns/Verbose.hpp" #include "Dynamics/ForceAnnealing.hpp" #include "molecule.hpp" #include "World.hpp" #include "WorldTime.hpp" #include #include #include #include #include "Actions/MoleculeAction/ForceAnnealingAction.hpp" using namespace MoleCuilder; enum VectorIndexType { PositionIndex=0, VelocityIndex=1, ForceIndex=2 }; // and construct the stuff #include "ForceAnnealingAction.def" #include "Action_impl_pre.hpp" /** =========== define the function ====================== */ ActionState::ptr MoleculeForceAnnealingAction::performCall() { AtomSetMixin > set(World::getInstance().getSelectedAtoms()); if (set.empty()) { STATUS("No atoms selected."); return Action::failure; } size_t CurrentStep = WorldTime::getInstance().getTime(); if (CurrentStep == 0) { ELOG(1, "WorldTime must be at least at step 1 already, use step-world-time if necessary."); return Action::failure; } // first, we need to sort the mixin according to their ids (as selected atoms are sorted // according to their arbitrary address in memory) set.sortByIds(); // create undo state for all selected atoms (undo info) std::vector< std::vector > UndoInfo(2); for (int i=0;i<2;++i) { UndoInfo[i].reserve(set.size()); { for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) UndoInfo[i].push_back(AtomicInfo(*(iter->second), CurrentStep-i)); } } // instantiate optimizer ForceAnnealing > optimizer( set, params.deltat.get(), true, params.steps.get(), params.MaxDistance.get(), params.DampingFactor.get()); // parse forces into last step (assuming we stepped on already) if (!params.forcesfile.get().string().empty()) { LOG(1, "Parsing forces file."); if (!optimizer.parseForcesFile(params.forcesfile.get().string().c_str(), CurrentStep-1)) LOG(2, "File " << params.forcesfile.get() << " not found."); else LOG(2, "File " << params.forcesfile.get() << " found and parsed."); } // perform optimization step LOG(1, "Structural optimization."); optimizer(CurrentStep-1, 1, params.UseBondGraph.get()); STATUS("Successfully optimized structure by one step."); std::vector< std::vector > RedoInfo(2); for (int i=0;i<2;++i) { RedoInfo[i].reserve(set.size()); { for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) RedoInfo[i].push_back(AtomicInfo(*(iter->second), CurrentStep-i)); } } MoleculeForceAnnealingState *UndoState = new MoleculeForceAnnealingState(UndoInfo, RedoInfo, params); return ActionState::ptr(UndoState); } ActionState::ptr MoleculeForceAnnealingAction::performUndo(ActionState::ptr _state) { MoleculeForceAnnealingState *state = assert_cast(_state.get()); const size_t CurrentStep = WorldTime::getInstance().getTime(); // set stored old state for (int i=0;i<2;++i) SetAtomsFromAtomicInfo(state->UndoInfo[i], CurrentStep-i); return ActionState::ptr(_state); } ActionState::ptr MoleculeForceAnnealingAction::performRedo(ActionState::ptr _state){ MoleculeForceAnnealingState *state = assert_cast(_state.get()); const size_t CurrentStep = WorldTime::getInstance().getTime(); // set stored new state for (int i=0;i<2;++i) SetAtomsFromAtomicInfo(state->RedoInfo[i], CurrentStep-i); return ActionState::ptr(_state); } bool MoleculeForceAnnealingAction::canUndo() { return true; } bool MoleculeForceAnnealingAction::shouldUndo() { return true; } /** =========== end of function ====================== */