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