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