/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010-2011 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * AddEmptyBoundaryAction.cpp * * Created on: May 8, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif // include headers that implement a archive in simple text format #include #include #include "boost/serialization/vector.hpp" #include "CodePatterns/MemDebug.hpp" #include "atom.hpp" #include "Box.hpp" #include "CodePatterns/Log.hpp" #include "LinearAlgebra/MatrixContent.hpp" #include "LinearAlgebra/RealSpaceMatrix.hpp" #include "LinearAlgebra/Vector.hpp" #include "World.hpp" #include #include #include #include "Actions/WorldAction/AddEmptyBoundaryAction.hpp" using namespace MoleCuilder; // and construct the stuff #include "AddEmptyBoundaryAction.def" #include "Action_impl_pre.hpp" /** =========== define the function ====================== */ Action::state_ptr WorldAddEmptyBoundaryAction::performCall() { Vector Min; Vector Max; int j=0; // obtain information getParametersfromValueStorage(); // create undo domain std::stringstream undostream; boost::archive::text_oarchive oa(undostream); const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM(); oa << matrix; // get maximum and minimum std::vector AllAtoms = World::getInstance().getAllAtoms(); ASSERT(AllAtoms.size() > 0, "There must be atoms present for AddingEmptyBoundary."); std::vector::iterator AtomRunner = AllAtoms.begin(); Min = (*AtomRunner)->getPosition(); Max = (*AtomRunner)->getPosition(); for (; AtomRunner != AllAtoms.end(); ++AtomRunner) { for (int i=0;iat(i) > Max[i]) Max[i] = (*AtomRunner)->at(i); if ((*AtomRunner)->at(i) < Min[i]) Min[i] = (*AtomRunner)->at(i); } } // set new box size double * const cell_size = new double[6]; for (j=0;j<6;j++) cell_size[j] = 0.; j=-1; for (int i=0;i::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner) *(*AtomRunner) -= Min - params.boundary; // give final box size LOG(0, "Box domain is now " << World::getInstance().getDomain().getM()); // create undo state WorldAddEmptyBoundaryState *UndoState = new WorldAddEmptyBoundaryState( undostream.str(), World::getInstance().getDomain().getM(), Min, params ); return Action::state_ptr(UndoState); } Action::state_ptr WorldAddEmptyBoundaryAction::performUndo(Action::state_ptr _state) { WorldAddEmptyBoundaryState *state = assert_cast(_state.get()); // restore domain RealSpaceMatrix matrix; std::stringstream undostream(state->undostring); boost::archive::text_iarchive ia(undostream); ia >> matrix; World::getInstance().setDomain(matrix); // give final box size LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM()); // restore atoms std::vector AllAtoms = World::getInstance().getAllAtoms(); for (std::vector::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner) *(*AtomRunner) += state->Min - state->params.boundary; return Action::state_ptr(_state); } Action::state_ptr WorldAddEmptyBoundaryAction::performRedo(Action::state_ptr _state){ WorldAddEmptyBoundaryState *state = assert_cast(_state.get()); World::getInstance().setDomain(state->newdomain); // give final box size LOG(0, "Box domain is again " << World::getInstance().getDomain().getM()); // shift atoms std::vector AllAtoms = World::getInstance().getAllAtoms(); for (std::vector::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner) *(*AtomRunner) -= state->Min - state->params.boundary; return Action::state_ptr(_state); } bool WorldAddEmptyBoundaryAction::canUndo() { return true; } bool WorldAddEmptyBoundaryAction::shouldUndo() { return true; } /** =========== end of function ====================== */