| [bcf653] | 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| [0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
|---|
| [bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| [97ebf8] | 8 | /*
|
|---|
| 9 | * CenterOnEdgeAction.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: May 8, 2010
|
|---|
| 12 | * Author: heber
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| [bf3817] | 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| [de7d1d] | 20 | // include headers that implement a archive in simple text format
|
|---|
| 21 | #include <boost/archive/text_oarchive.hpp>
|
|---|
| 22 | #include <boost/archive/text_iarchive.hpp>
|
|---|
| 23 | #include "boost/serialization/vector.hpp"
|
|---|
| 24 |
|
|---|
| [ad011c] | 25 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| [112b09] | 26 |
|
|---|
| [6f0841] | 27 | #include "Atom/atom.hpp"
|
|---|
| [ad011c] | 28 | #include "CodePatterns/Log.hpp"
|
|---|
| [de7d1d] | 29 | #include "LinearAlgebra/MatrixContent.hpp"
|
|---|
| 30 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
|---|
| [57f243] | 31 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| [de7d1d] | 32 | #include "molecule.hpp"
|
|---|
| [97ebf8] | 33 | #include "World.hpp"
|
|---|
| 34 |
|
|---|
| 35 | #include <iostream>
|
|---|
| 36 | #include <string>
|
|---|
| [de7d1d] | 37 | #include <vector>
|
|---|
| [97ebf8] | 38 |
|
|---|
| [1fd675] | 39 | #include "Actions/WorldAction/CenterOnEdgeAction.hpp"
|
|---|
| [1ba67d] | 40 |
|
|---|
| [ce7fdc] | 41 | using namespace MoleCuilder;
|
|---|
| 42 |
|
|---|
| [1fd675] | 43 | // and construct the stuff
|
|---|
| 44 | #include "CenterOnEdgeAction.def"
|
|---|
| 45 | #include "Action_impl_pre.hpp"
|
|---|
| 46 | /** =========== define the function ====================== */
|
|---|
| [1ba67d] | 47 | Action::state_ptr WorldCenterOnEdgeAction::performCall() {
|
|---|
| [97ebf8] | 48 | Vector Min;
|
|---|
| 49 | Vector Max;
|
|---|
| 50 |
|
|---|
| [de7d1d] | 51 | // create undo state
|
|---|
| 52 | std::stringstream undostream;
|
|---|
| 53 | boost::archive::text_oarchive oa(undostream);
|
|---|
| 54 | const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
|
|---|
| 55 | oa << matrix;
|
|---|
| 56 | std::vector< boost::shared_ptr<Vector> > OldPositions;
|
|---|
| 57 | std::vector<atom *> AllAtoms = World::getInstance().getAllAtoms();
|
|---|
| 58 | for (std::vector<atom *>::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner)
|
|---|
| 59 | OldPositions.push_back(
|
|---|
| 60 | boost::shared_ptr<Vector>(new Vector(
|
|---|
| 61 | (*AtomRunner)->getPosition()
|
|---|
| 62 | ))
|
|---|
| 63 | );
|
|---|
| 64 |
|
|---|
| [1ba67d] | 65 | // get maximum and minimum
|
|---|
| 66 | ASSERT(AllAtoms.size() > 0, "For CenteronEdge atoms must be present.");
|
|---|
| [de7d1d] | 67 | std::vector<atom *>::iterator AtomRunner = AllAtoms.begin();
|
|---|
| [d74077] | 68 | Min = (*AtomRunner)->getPosition();
|
|---|
| 69 | Max = (*AtomRunner)->getPosition();
|
|---|
| [1ba67d] | 70 | for (; AtomRunner != AllAtoms.end(); ++AtomRunner) {
|
|---|
| [97ebf8] | 71 | for (int i=0;i<NDIM;i++) {
|
|---|
| [d74077] | 72 | if ((*AtomRunner)->at(i) > Max[i])
|
|---|
| 73 | Max[i] = (*AtomRunner)->at(i);
|
|---|
| 74 | if ((*AtomRunner)->at(i) < Min[i])
|
|---|
| 75 | Min[i] = (*AtomRunner)->at(i);
|
|---|
| [97ebf8] | 76 | }
|
|---|
| 77 | }
|
|---|
| [1ba67d] | 78 | // set new box size
|
|---|
| [cca9ef] | 79 | RealSpaceMatrix domain;
|
|---|
| [1ba67d] | 80 | for (int i=0;i<NDIM;i++) {
|
|---|
| 81 | double tmp = Max[i]-Min[i];
|
|---|
| 82 | tmp = fabs(tmp)>=1. ? tmp : 1.0;
|
|---|
| 83 | domain.at(i,i) = tmp;
|
|---|
| 84 | }
|
|---|
| 85 | World::getInstance().setDomain(domain);
|
|---|
| 86 | // translate all atoms, such that Min is aty (0,0,0)
|
|---|
| [de7d1d] | 87 | for (std::vector<atom*>::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner)
|
|---|
| [d74077] | 88 | *(*AtomRunner) -= Min;
|
|---|
| [1ba67d] | 89 |
|
|---|
| [3bd460a] | 90 | // give final box size
|
|---|
| 91 | LOG(0, "Box domain is now " << World::getInstance().getDomain().getM());
|
|---|
| 92 |
|
|---|
| [de7d1d] | 93 | // create undo state
|
|---|
| 94 | WorldCenterOnEdgeState *UndoState =
|
|---|
| 95 | new WorldCenterOnEdgeState(
|
|---|
| 96 | undostream.str(),
|
|---|
| 97 | Min,
|
|---|
| 98 | Max,
|
|---|
| 99 | params
|
|---|
| 100 | );
|
|---|
| 101 |
|
|---|
| 102 | return Action::state_ptr(UndoState);
|
|---|
| [97ebf8] | 103 | }
|
|---|
| 104 |
|
|---|
| 105 | Action::state_ptr WorldCenterOnEdgeAction::performUndo(Action::state_ptr _state) {
|
|---|
| [de7d1d] | 106 | WorldCenterOnEdgeState *state = assert_cast<WorldCenterOnEdgeState*>(_state.get());
|
|---|
| 107 |
|
|---|
| 108 | // restore domain
|
|---|
| 109 | RealSpaceMatrix matrix;
|
|---|
| 110 | std::stringstream undostream(state->undostring);
|
|---|
| 111 | boost::archive::text_iarchive ia(undostream);
|
|---|
| 112 | ia >> matrix;
|
|---|
| 113 | World::getInstance().setDomain(matrix);
|
|---|
| 114 |
|
|---|
| 115 | // translate all atoms back
|
|---|
| 116 | std::vector<atom *> AllAtoms = World::getInstance().getAllAtoms();
|
|---|
| 117 | for (vector<atom*>::iterator AtomRunner = AllAtoms.begin();
|
|---|
| 118 | AtomRunner != AllAtoms.end();
|
|---|
| 119 | ++AtomRunner)
|
|---|
| 120 | *(*AtomRunner) += state->Min;
|
|---|
| 121 |
|
|---|
| 122 | // give final box size
|
|---|
| 123 | LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM());
|
|---|
| [97ebf8] | 124 |
|
|---|
| [de7d1d] | 125 | return Action::state_ptr(_state);
|
|---|
| [97ebf8] | 126 | }
|
|---|
| 127 |
|
|---|
| 128 | Action::state_ptr WorldCenterOnEdgeAction::performRedo(Action::state_ptr _state){
|
|---|
| [de7d1d] | 129 | WorldCenterOnEdgeState *state = assert_cast<WorldCenterOnEdgeState*>(_state.get());
|
|---|
| 130 |
|
|---|
| 131 | // set new box size
|
|---|
| 132 | RealSpaceMatrix rmatrix;
|
|---|
| 133 | for (int i=0;i<NDIM;i++) {
|
|---|
| 134 | double tmp = state->Max[i]-state->Min[i];
|
|---|
| 135 | tmp = fabs(tmp)>=1. ? tmp : 1.0;
|
|---|
| 136 | rmatrix.at(i,i) = tmp;
|
|---|
| 137 | }
|
|---|
| 138 | World::getInstance().setDomain(rmatrix);
|
|---|
| 139 | // translate all atoms, such that Min is aty (0,0,0)
|
|---|
| 140 | std::vector<atom *> AllAtoms = World::getInstance().getAllAtoms();
|
|---|
| 141 | for (vector<atom*>::iterator AtomRunner = AllAtoms.begin();
|
|---|
| 142 | AtomRunner != AllAtoms.end();
|
|---|
| 143 | ++AtomRunner)
|
|---|
| 144 | *(*AtomRunner) -= state->Min;
|
|---|
| 145 |
|
|---|
| 146 | // give final box size
|
|---|
| 147 | LOG(0, "Box domain is again " << World::getInstance().getDomain().getM());
|
|---|
| 148 |
|
|---|
| 149 | return Action::state_ptr(_state);
|
|---|
| [97ebf8] | 150 | }
|
|---|
| 151 |
|
|---|
| 152 | bool WorldCenterOnEdgeAction::canUndo() {
|
|---|
| [de7d1d] | 153 | return true;
|
|---|
| [97ebf8] | 154 | }
|
|---|
| 155 |
|
|---|
| 156 | bool WorldCenterOnEdgeAction::shouldUndo() {
|
|---|
| [de7d1d] | 157 | return true;
|
|---|
| [97ebf8] | 158 | }
|
|---|
| [1fd675] | 159 | /** =========== end of function ====================== */
|
|---|