| [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 | 
 | 
|---|
| [1fd675] | 51 |   // obtain information
 | 
|---|
 | 52 |   getParametersfromValueStorage();
 | 
|---|
 | 53 | 
 | 
|---|
| [de7d1d] | 54 |   // create undo state
 | 
|---|
 | 55 |   std::stringstream undostream;
 | 
|---|
 | 56 |   boost::archive::text_oarchive oa(undostream);
 | 
|---|
 | 57 |   const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
 | 
|---|
 | 58 |   oa << matrix;
 | 
|---|
 | 59 |   std::vector< boost::shared_ptr<Vector> > OldPositions;
 | 
|---|
 | 60 |   std::vector<atom *> AllAtoms = World::getInstance().getAllAtoms();
 | 
|---|
 | 61 |   for (std::vector<atom *>::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner)
 | 
|---|
 | 62 |     OldPositions.push_back(
 | 
|---|
 | 63 |         boost::shared_ptr<Vector>(new Vector(
 | 
|---|
 | 64 |             (*AtomRunner)->getPosition()
 | 
|---|
 | 65 |             ))
 | 
|---|
 | 66 |         );
 | 
|---|
 | 67 | 
 | 
|---|
| [1ba67d] | 68 |   // get maximum and minimum
 | 
|---|
 | 69 |   ASSERT(AllAtoms.size() > 0, "For CenteronEdge atoms must be present.");
 | 
|---|
| [de7d1d] | 70 |   std::vector<atom *>::iterator AtomRunner = AllAtoms.begin();
 | 
|---|
| [d74077] | 71 |   Min = (*AtomRunner)->getPosition();
 | 
|---|
 | 72 |   Max = (*AtomRunner)->getPosition();
 | 
|---|
| [1ba67d] | 73 |   for (; AtomRunner != AllAtoms.end(); ++AtomRunner) {
 | 
|---|
| [97ebf8] | 74 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| [d74077] | 75 |       if ((*AtomRunner)->at(i) > Max[i])
 | 
|---|
 | 76 |         Max[i] = (*AtomRunner)->at(i);
 | 
|---|
 | 77 |       if ((*AtomRunner)->at(i) < Min[i])
 | 
|---|
 | 78 |         Min[i] = (*AtomRunner)->at(i);
 | 
|---|
| [97ebf8] | 79 |     }
 | 
|---|
 | 80 |   }
 | 
|---|
| [1ba67d] | 81 |   // set new box size
 | 
|---|
| [cca9ef] | 82 |   RealSpaceMatrix domain;
 | 
|---|
| [1ba67d] | 83 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 84 |     double tmp = Max[i]-Min[i];
 | 
|---|
 | 85 |     tmp = fabs(tmp)>=1. ? tmp : 1.0;
 | 
|---|
 | 86 |     domain.at(i,i) = tmp;
 | 
|---|
 | 87 |   }
 | 
|---|
 | 88 |   World::getInstance().setDomain(domain);
 | 
|---|
 | 89 |   // translate all atoms, such that Min is aty (0,0,0)
 | 
|---|
| [de7d1d] | 90 |   for (std::vector<atom*>::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner)
 | 
|---|
| [d74077] | 91 |     *(*AtomRunner) -= Min;
 | 
|---|
| [1ba67d] | 92 | 
 | 
|---|
| [3bd460a] | 93 |   // give final box size
 | 
|---|
 | 94 |   LOG(0, "Box domain is now " << World::getInstance().getDomain().getM());
 | 
|---|
 | 95 | 
 | 
|---|
| [de7d1d] | 96 |   // create undo state
 | 
|---|
 | 97 |   WorldCenterOnEdgeState *UndoState =
 | 
|---|
 | 98 |       new WorldCenterOnEdgeState(
 | 
|---|
 | 99 |           undostream.str(),
 | 
|---|
 | 100 |           Min,
 | 
|---|
 | 101 |           Max,
 | 
|---|
 | 102 |           params
 | 
|---|
 | 103 |           );
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |   return Action::state_ptr(UndoState);
 | 
|---|
| [97ebf8] | 106 | }
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | Action::state_ptr WorldCenterOnEdgeAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [de7d1d] | 109 |   WorldCenterOnEdgeState *state = assert_cast<WorldCenterOnEdgeState*>(_state.get());
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |   // restore domain
 | 
|---|
 | 112 |   RealSpaceMatrix matrix;
 | 
|---|
 | 113 |   std::stringstream undostream(state->undostring);
 | 
|---|
 | 114 |   boost::archive::text_iarchive ia(undostream);
 | 
|---|
 | 115 |   ia >> matrix;
 | 
|---|
 | 116 |   World::getInstance().setDomain(matrix);
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |   // translate all atoms back
 | 
|---|
 | 119 |   std::vector<atom *> AllAtoms = World::getInstance().getAllAtoms();
 | 
|---|
 | 120 |   for (vector<atom*>::iterator AtomRunner = AllAtoms.begin();
 | 
|---|
 | 121 |       AtomRunner != AllAtoms.end();
 | 
|---|
 | 122 |       ++AtomRunner)
 | 
|---|
 | 123 |     *(*AtomRunner) += state->Min;
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |   // give final box size
 | 
|---|
 | 126 |   LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM());
 | 
|---|
| [97ebf8] | 127 | 
 | 
|---|
| [de7d1d] | 128 |   return Action::state_ptr(_state);
 | 
|---|
| [97ebf8] | 129 | }
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | Action::state_ptr WorldCenterOnEdgeAction::performRedo(Action::state_ptr _state){
 | 
|---|
| [de7d1d] | 132 |   WorldCenterOnEdgeState *state = assert_cast<WorldCenterOnEdgeState*>(_state.get());
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 |   // set new box size
 | 
|---|
 | 135 |   RealSpaceMatrix rmatrix;
 | 
|---|
 | 136 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 137 |     double tmp = state->Max[i]-state->Min[i];
 | 
|---|
 | 138 |     tmp = fabs(tmp)>=1. ? tmp : 1.0;
 | 
|---|
 | 139 |     rmatrix.at(i,i) = tmp;
 | 
|---|
 | 140 |   }
 | 
|---|
 | 141 |   World::getInstance().setDomain(rmatrix);
 | 
|---|
 | 142 |   // translate all atoms, such that Min is aty (0,0,0)
 | 
|---|
 | 143 |   std::vector<atom *> AllAtoms = World::getInstance().getAllAtoms();
 | 
|---|
 | 144 |   for (vector<atom*>::iterator AtomRunner = AllAtoms.begin();
 | 
|---|
 | 145 |       AtomRunner != AllAtoms.end();
 | 
|---|
 | 146 |       ++AtomRunner)
 | 
|---|
 | 147 |     *(*AtomRunner) -= state->Min;
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 |   // give final box size
 | 
|---|
 | 150 |   LOG(0, "Box domain is again " << World::getInstance().getDomain().getM());
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 |   return Action::state_ptr(_state);
 | 
|---|
| [97ebf8] | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | bool WorldCenterOnEdgeAction::canUndo() {
 | 
|---|
| [de7d1d] | 156 |   return true;
 | 
|---|
| [97ebf8] | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | bool WorldCenterOnEdgeAction::shouldUndo() {
 | 
|---|
| [de7d1d] | 160 |   return true;
 | 
|---|
| [97ebf8] | 161 | }
 | 
|---|
| [1fd675] | 162 | /** =========== end of function ====================== */
 | 
|---|