[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[bcf653] | 21 | */
|
---|
| 22 |
|
---|
[97ebf8] | 23 | /*
|
---|
| 24 | * CenterInBoxAction.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: May 8, 2010
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[bf3817] | 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[23526c] | 35 | // include headers that implement a archive in simple text format
|
---|
| 36 | #include <boost/archive/text_oarchive.hpp>
|
---|
| 37 | #include <boost/archive/text_iarchive.hpp>
|
---|
| 38 | #include "boost/serialization/vector.hpp"
|
---|
| 39 |
|
---|
[ad011c] | 40 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 41 |
|
---|
[23526c] | 42 | #include <boost/shared_ptr.hpp>
|
---|
| 43 |
|
---|
[4aece0] | 44 | #include "Box.hpp"
|
---|
[ad011c] | 45 | #include "CodePatterns/Log.hpp"
|
---|
[23526c] | 46 | #include "LinearAlgebra/MatrixContent.hpp"
|
---|
[3bd460a] | 47 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[97ebf8] | 48 | #include "molecule.hpp"
|
---|
| 49 | #include "World.hpp"
|
---|
| 50 |
|
---|
| 51 | #include <iostream>
|
---|
| 52 | #include <string>
|
---|
[23526c] | 53 | #include <vector>
|
---|
[97ebf8] | 54 |
|
---|
[1fd675] | 55 | #include "Actions/WorldAction/CenterInBoxAction.hpp"
|
---|
[4aece0] | 56 |
|
---|
[ce7fdc] | 57 | using namespace MoleCuilder;
|
---|
| 58 |
|
---|
[1fd675] | 59 | // and construct the stuff
|
---|
| 60 | #include "CenterInBoxAction.def"
|
---|
| 61 | #include "Action_impl_pre.hpp"
|
---|
| 62 | /** =========== define the function ====================== */
|
---|
[4aece0] | 63 | Action::state_ptr WorldCenterInBoxAction::performCall() {
|
---|
[23526c] | 64 | // create undo state
|
---|
| 65 | std::stringstream undostream;
|
---|
| 66 | boost::archive::text_oarchive oa(undostream);
|
---|
[68c923] | 67 | const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
|
---|
[23526c] | 68 | oa << matrix;
|
---|
| 69 | std::vector< boost::shared_ptr<Vector> > OldPositions;
|
---|
| 70 | std::vector<molecule*> AllMolecules = World::getInstance().getAllMolecules();
|
---|
| 71 | for (std::vector<molecule*>::iterator MolRunner = AllMolecules.begin();
|
---|
| 72 | MolRunner != AllMolecules.end();
|
---|
| 73 | ++MolRunner) {
|
---|
| 74 | for(molecule::const_iterator AtomRunner = (*MolRunner)->begin();
|
---|
| 75 | AtomRunner != (*MolRunner)->end();
|
---|
| 76 | ++AtomRunner) {
|
---|
| 77 | OldPositions.push_back(
|
---|
| 78 | boost::shared_ptr<Vector>(new Vector(
|
---|
| 79 | (*AtomRunner)->getPosition()
|
---|
| 80 | ))
|
---|
| 81 | );
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // set new domain
|
---|
[7d9416] | 86 | World::getInstance().setDomain(params.cell_size.get());
|
---|
[4aece0] | 87 |
|
---|
[23526c] | 88 | // center atoms
|
---|
| 89 | for (std::vector<molecule*>::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
|
---|
[4aece0] | 90 | (*MolRunner)->CenterInBox();
|
---|
[97ebf8] | 91 | }
|
---|
[3bd460a] | 92 |
|
---|
| 93 | // give final box size
|
---|
| 94 | LOG(0, "Box domain is now " << World::getInstance().getDomain().getM());
|
---|
| 95 |
|
---|
[23526c] | 96 | // create undo state
|
---|
| 97 | WorldCenterInBoxState *UndoState =
|
---|
| 98 | new WorldCenterInBoxState(
|
---|
| 99 | undostream.str(),
|
---|
| 100 | OldPositions,
|
---|
| 101 | params
|
---|
| 102 | );
|
---|
| 103 |
|
---|
| 104 | return Action::state_ptr(UndoState);
|
---|
[97ebf8] | 105 | }
|
---|
| 106 |
|
---|
| 107 | Action::state_ptr WorldCenterInBoxAction::performUndo(Action::state_ptr _state) {
|
---|
[23526c] | 108 | WorldCenterInBoxState *state = assert_cast<WorldCenterInBoxState*>(_state.get());
|
---|
| 109 |
|
---|
| 110 | // restore domain
|
---|
| 111 | RealSpaceMatrix matrix;
|
---|
| 112 | std::stringstream undostream(state->undostring);
|
---|
| 113 | boost::archive::text_iarchive ia(undostream);
|
---|
| 114 | ia >> matrix;
|
---|
| 115 | World::getInstance().setDomain(matrix);
|
---|
| 116 |
|
---|
| 117 | // place atoms on old positions
|
---|
| 118 | std::vector< boost::shared_ptr<Vector> >::const_iterator OldPositionsIter = state->OldPositions.begin();
|
---|
| 119 | std::vector<molecule*> AllMolecules = World::getInstance().getAllMolecules();
|
---|
| 120 | for (std::vector<molecule*>::iterator MolRunner = AllMolecules.begin();
|
---|
| 121 | MolRunner != AllMolecules.end();
|
---|
| 122 | ++MolRunner) {
|
---|
[59fff1] | 123 | for(molecule::iterator AtomRunner = (*MolRunner)->begin();
|
---|
[23526c] | 124 | AtomRunner != (*MolRunner)->end();
|
---|
| 125 | ++AtomRunner) {
|
---|
| 126 | ASSERT(OldPositionsIter != state->OldPositions.end(),
|
---|
| 127 | "WorldBoundInBoxAction::performUndo() - too few positions stored in UndoState.");
|
---|
| 128 | (*AtomRunner)->setPosition(**(OldPositionsIter++));
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | // give final box size
|
---|
| 133 | LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM());
|
---|
[97ebf8] | 134 |
|
---|
[23526c] | 135 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 136 | }
|
---|
| 137 |
|
---|
| 138 | Action::state_ptr WorldCenterInBoxAction::performRedo(Action::state_ptr _state){
|
---|
[23526c] | 139 | WorldCenterInBoxState *state = assert_cast<WorldCenterInBoxState*>(_state.get());
|
---|
| 140 |
|
---|
| 141 | // set new domain
|
---|
[7d9416] | 142 | World::getInstance().setDomain(state->params.cell_size.get());
|
---|
[23526c] | 143 |
|
---|
| 144 | // center atoms
|
---|
| 145 | std::vector<molecule *> AllMolecules = World::getInstance().getAllMolecules();
|
---|
| 146 | for (std::vector<molecule*>::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
|
---|
| 147 | (*MolRunner)->CenterInBox();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // give final box size
|
---|
| 151 | LOG(0, "Box domain is again " << World::getInstance().getDomain().getM());
|
---|
| 152 |
|
---|
| 153 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 154 | }
|
---|
| 155 |
|
---|
| 156 | bool WorldCenterInBoxAction::canUndo() {
|
---|
[23526c] | 157 | return true;
|
---|
[97ebf8] | 158 | }
|
---|
| 159 |
|
---|
| 160 | bool WorldCenterInBoxAction::shouldUndo() {
|
---|
[23526c] | 161 | return true;
|
---|
[97ebf8] | 162 | }
|
---|
[1fd675] | 163 | /** =========== end of function ====================== */
|
---|