| 1 | /*
 | 
|---|
| 2 |  * RepeatBoxAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 12, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Actions/WorldAction/RepeatBoxAction.hpp"
 | 
|---|
| 9 | #include "CommandLineParser.hpp"
 | 
|---|
| 10 | #include "atom.hpp"
 | 
|---|
| 11 | #include "log.hpp"
 | 
|---|
| 12 | #include "molecule.hpp"
 | 
|---|
| 13 | #include "vector.hpp"
 | 
|---|
| 14 | #include "verbose.hpp"
 | 
|---|
| 15 | #include "World.hpp"
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <iostream>
 | 
|---|
| 18 | #include <string>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | using namespace std;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 23 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 24 | #include "Actions/MapOfActions.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | const char WorldRepeatBoxAction::NAME[] = "repeat-box";
 | 
|---|
| 27 | 
 | 
|---|
| 28 | WorldRepeatBoxAction::WorldRepeatBoxAction() :
 | 
|---|
| 29 |   Action(NAME)
 | 
|---|
| 30 | {}
 | 
|---|
| 31 | 
 | 
|---|
| 32 | WorldRepeatBoxAction::~WorldRepeatBoxAction()
 | 
|---|
| 33 | {}
 | 
|---|
| 34 | 
 | 
|---|
| 35 | Action::state_ptr WorldRepeatBoxAction::performCall() {
 | 
|---|
| 36 |   Dialog *dialog = UIFactory::getInstance().makeDialog();
 | 
|---|
| 37 |   Vector Repeater;
 | 
|---|
| 38 |   int count;
 | 
|---|
| 39 |   const element ** Elements;
 | 
|---|
| 40 |   molecule *mol = NULL;
 | 
|---|
| 41 |   int j = 0;
 | 
|---|
| 42 |   atom *Walker = NULL;
 | 
|---|
| 43 | 
 | 
|---|
| 44 |   dialog->queryVector(NAME, &Repeater, World::getInstance().getDomain(), false, MapOfActions::getInstance().getDescription(NAME));
 | 
|---|
| 45 |   dialog->queryMolecule("molecule-by-id", &mol,MapOfActions::getInstance().getDescription("molecule-by-id"));
 | 
|---|
| 46 | 
 | 
|---|
| 47 |   if(dialog->display()) {
 | 
|---|
| 48 |     double * const cell_size = World::getInstance().getDomain();
 | 
|---|
| 49 |     Vector x,y;
 | 
|---|
| 50 |     for (int axis = 1; axis <= NDIM; axis++) {
 | 
|---|
| 51 |       Vector ** vectors;
 | 
|---|
| 52 |       Repeater[axis] = floor(Repeater[axis]);
 | 
|---|
| 53 |       if (Repeater[axis] < 1) {
 | 
|---|
| 54 |         DoeLog(1) && (eLog()<< Verbose(1) << "Repetition factor must be greater than 1!" << endl);
 | 
|---|
| 55 |         Repeater[axis] = 1;
 | 
|---|
| 56 |       }
 | 
|---|
| 57 |       if (mol->getAtomCount() != 0) {  // if there is more than none
 | 
|---|
| 58 |         count = mol->getAtomCount();   // is changed becausing of adding, thus has to be stored away beforehand
 | 
|---|
| 59 |         Elements = new const element *[count];
 | 
|---|
| 60 |         vectors = new Vector *[count];
 | 
|---|
| 61 |         j = 0;
 | 
|---|
| 62 |         for(molecule::iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
 | 
|---|
| 63 |           Elements[j] = (*AtomRunner)->type;
 | 
|---|
| 64 |           vectors[j] = &(*AtomRunner)->x;
 | 
|---|
| 65 |           j++;
 | 
|---|
| 66 |         }
 | 
|---|
| 67 |         if (count != j)
 | 
|---|
| 68 |           DoeLog(1) && (eLog()<< Verbose(1) << "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl);
 | 
|---|
| 69 |         x.Zero();
 | 
|---|
| 70 |         y.Zero();
 | 
|---|
| 71 |         y[abs(axis)-1] = cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] * abs(axis)/axis; // last term is for sign, first is for magnitude
 | 
|---|
| 72 |         for (int i=1;i<Repeater[axis];i++) {  // then add this list with respective translation factor times
 | 
|---|
| 73 |           x += y; // per factor one cell width further
 | 
|---|
| 74 |           for (int k=count;k--;) { // go through every atom of the original cell
 | 
|---|
| 75 |             Walker = World::getInstance().createAtom(); // create a new body
 | 
|---|
| 76 |             Walker->x = (*vectors[k]) + x;
 | 
|---|
| 77 |             Walker->type = Elements[k];  // insert original element
 | 
|---|
| 78 |             mol->AddAtom(Walker);        // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
 | 
|---|
| 79 |           }
 | 
|---|
| 80 |         }
 | 
|---|
| 81 |         // free memory
 | 
|---|
| 82 |         delete[](Elements);
 | 
|---|
| 83 |         delete[](vectors);
 | 
|---|
| 84 |         // correct cell size
 | 
|---|
| 85 |         if (axis < 0) { // if sign was negative, we have to translate everything
 | 
|---|
| 86 |           x =(-(Repeater[axis]-1)) * y;
 | 
|---|
| 87 |           mol->Translate(&x);
 | 
|---|
| 88 |         }
 | 
|---|
| 89 |         cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] *= Repeater[axis];
 | 
|---|
| 90 |       }
 | 
|---|
| 91 |     }
 | 
|---|
| 92 |     delete dialog;
 | 
|---|
| 93 |     return Action::success;
 | 
|---|
| 94 |   } else {
 | 
|---|
| 95 |     delete dialog;
 | 
|---|
| 96 |     return Action::failure;
 | 
|---|
| 97 |   }
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | Action::state_ptr WorldRepeatBoxAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 101 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   return Action::failure;
 | 
|---|
| 104 | //  string newName = state->mol->getName();
 | 
|---|
| 105 | //  state->mol->setName(state->lastName);
 | 
|---|
| 106 | //
 | 
|---|
| 107 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | Action::state_ptr WorldRepeatBoxAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 111 |   return Action::failure;
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | bool WorldRepeatBoxAction::canUndo() {
 | 
|---|
| 115 |   return false;
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | bool WorldRepeatBoxAction::shouldUndo() {
 | 
|---|
| 119 |   return false;
 | 
|---|
| 120 | }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | const string WorldRepeatBoxAction::getName() {
 | 
|---|
| 123 |   return NAME;
 | 
|---|
| 124 | }
 | 
|---|