| 1 | /*
 | 
|---|
| 2 |  * RepeatBoxAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 12, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Actions/WorldAction/RepeatBoxAction.hpp"
 | 
|---|
| 9 | #include "atom.hpp"
 | 
|---|
| 10 | #include "log.hpp"
 | 
|---|
| 11 | #include "molecule.hpp"
 | 
|---|
| 12 | #include "vector.hpp"
 | 
|---|
| 13 | #include "verbose.hpp"
 | 
|---|
| 14 | #include "World.hpp"
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <iostream>
 | 
|---|
| 17 | #include <string>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | using namespace std;
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 22 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 23 | #include "Actions/MapOfActions.hpp"
 | 
|---|
| 24 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 25 | #include "Descriptors/MoleculePtrDescriptor.hpp"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | const char WorldRepeatBoxAction::NAME[] = "repeat-box";
 | 
|---|
| 28 | 
 | 
|---|
| 29 | WorldRepeatBoxAction::WorldRepeatBoxAction() :
 | 
|---|
| 30 |   Action(NAME)
 | 
|---|
| 31 | {}
 | 
|---|
| 32 | 
 | 
|---|
| 33 | WorldRepeatBoxAction::~WorldRepeatBoxAction()
 | 
|---|
| 34 | {}
 | 
|---|
| 35 | 
 | 
|---|
| 36 | Action::state_ptr WorldRepeatBoxAction::performCall() {
 | 
|---|
| 37 |   Dialog *dialog = UIFactory::getInstance().makeDialog();
 | 
|---|
| 38 |   Vector Repeater;
 | 
|---|
| 39 |   int count;
 | 
|---|
| 40 |   const element ** Elements;
 | 
|---|
| 41 |   molecule *mol = NULL;
 | 
|---|
| 42 |   int j = 0;
 | 
|---|
| 43 |   atom *Walker = NULL;
 | 
|---|
| 44 |   MoleculeListClass *molecules = World::getInstance().getMolecules();
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   dialog->queryVector(NAME, &Repeater, World::getInstance().getDomain(), false, MapOfActions::getInstance().getDescription(NAME));
 | 
|---|
| 47 |   //dialog->queryMolecule("molecule-by-id", &mol,MapOfActions::getInstance().getDescription("molecule-by-id"));
 | 
|---|
| 48 |   vector<molecule *> AllMolecules;
 | 
|---|
| 49 |   if (mol != NULL) {
 | 
|---|
| 50 |     DoLog(0) && (Log() << Verbose(0) << "Using molecule " << mol->name << "." << endl);
 | 
|---|
| 51 |     AllMolecules = World::getInstance().getAllMolecules(MoleculeByPtr(mol));
 | 
|---|
| 52 |   } else {
 | 
|---|
| 53 |     DoLog(0) && (Log() << Verbose(0) << "Using all molecules." << endl);
 | 
|---|
| 54 |     AllMolecules = World::getInstance().getAllMolecules();
 | 
|---|
| 55 |   }
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   if(dialog->display()) {
 | 
|---|
| 58 |     (cout << "Repeating box " << Repeater << " times for (x,y,z) axis." << endl);
 | 
|---|
| 59 |     double * const cell_size = World::getInstance().getDomain();
 | 
|---|
| 60 |     double *M = ReturnFullMatrixforSymmetric(cell_size);
 | 
|---|
| 61 |     Vector x,y;
 | 
|---|
| 62 |     int n[NDIM];
 | 
|---|
| 63 |     for (int axis = 0; axis < NDIM; axis++) {
 | 
|---|
| 64 |       Repeater[axis] = floor(Repeater[axis]);
 | 
|---|
| 65 |       if (Repeater[axis] < 1) {
 | 
|---|
| 66 |         DoeLog(1) && (eLog()<< Verbose(1) << "Repetition factor must be greater than 1!" << endl);
 | 
|---|
| 67 |         Repeater[axis] = 1;
 | 
|---|
| 68 |       }
 | 
|---|
| 69 |       cell_size[(abs(axis+1) == 2) ? 2 : ((abs(axis+2) == 3) ? 5 : 0)] *= Repeater[axis];
 | 
|---|
| 70 |     }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |     molecule *newmol = NULL;
 | 
|---|
| 73 |     Vector ** vectors = NULL;
 | 
|---|
| 74 |     for (n[0] = 0; n[0] < Repeater[0]; n[0]++) {
 | 
|---|
| 75 |       y[0] = n[0];
 | 
|---|
| 76 |       for (n[1] = 0; n[1] < Repeater[1]; n[1]++) {
 | 
|---|
| 77 |         y[1] = n[1];
 | 
|---|
| 78 |         for (n[2] = 0; n[2] < Repeater[2]; n[2]++) {
 | 
|---|
| 79 |           y[2] = n[2];
 | 
|---|
| 80 |           if (n[0] == n[1] == n[2] == 0)
 | 
|---|
| 81 |             continue;
 | 
|---|
| 82 |           for (vector<molecule *>::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
 | 
|---|
| 83 |             mol = *MolRunner;
 | 
|---|
| 84 |             DoLog(1) && (Log() << Verbose(1) << "Current mol is " << mol->name << "." << endl);
 | 
|---|
| 85 |             count = mol->getAtomCount();   // is changed becausing of adding, thus has to be stored away beforehand
 | 
|---|
| 86 |             if (count != 0) {  // if there is more than none
 | 
|---|
| 87 |               Elements = new const element *[count];
 | 
|---|
| 88 |               vectors = new Vector *[count];
 | 
|---|
| 89 |               j = 0;
 | 
|---|
| 90 |               for(molecule::iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
 | 
|---|
| 91 |                 Elements[j] = (*AtomRunner)->type;
 | 
|---|
| 92 |                 vectors[j] = &(*AtomRunner)->x;
 | 
|---|
| 93 |                 j++;
 | 
|---|
| 94 |               }
 | 
|---|
| 95 |               if (count != j)
 | 
|---|
| 96 |                 DoeLog(1) && (eLog()<< Verbose(1) << "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl);
 | 
|---|
| 97 |               x = y;
 | 
|---|
| 98 |               x.MatrixMultiplication(M);
 | 
|---|
| 99 |               newmol = World::getInstance().createMolecule();
 | 
|---|
| 100 |               molecules->insert(newmol);
 | 
|---|
| 101 |               for (int k=count;k--;) { // go through every atom of the original cell
 | 
|---|
| 102 |                 Walker = World::getInstance().createAtom(); // create a new body
 | 
|---|
| 103 |                 Walker->x = (*vectors[k]) + x;
 | 
|---|
| 104 |                 Walker->type = Elements[k];  // insert original element
 | 
|---|
| 105 |                 cout << "new atom is " << *Walker << endl;
 | 
|---|
| 106 |                 newmol->AddAtom(Walker);        // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
 | 
|---|
| 107 |               }
 | 
|---|
| 108 |               // free memory
 | 
|---|
| 109 |               delete[](Elements);
 | 
|---|
| 110 |               delete[](vectors);
 | 
|---|
| 111 |             } else {
 | 
|---|
| 112 |               DoLog(1) && (Log() << Verbose(1) << "\t ... is empty." << endl);
 | 
|---|
| 113 |             }
 | 
|---|
| 114 |           }
 | 
|---|
| 115 |         }
 | 
|---|
| 116 |       }
 | 
|---|
| 117 |     }
 | 
|---|
| 118 |     delete(M);
 | 
|---|
| 119 |     delete dialog;
 | 
|---|
| 120 |     return Action::success;
 | 
|---|
| 121 |   } else {
 | 
|---|
| 122 |     delete dialog;
 | 
|---|
| 123 |     return Action::failure;
 | 
|---|
| 124 |   }
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | Action::state_ptr WorldRepeatBoxAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 128 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   return Action::failure;
 | 
|---|
| 131 | //  string newName = state->mol->getName();
 | 
|---|
| 132 | //  state->mol->setName(state->lastName);
 | 
|---|
| 133 | //
 | 
|---|
| 134 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | Action::state_ptr WorldRepeatBoxAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 138 |   return Action::failure;
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | bool WorldRepeatBoxAction::canUndo() {
 | 
|---|
| 142 |   return false;
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | bool WorldRepeatBoxAction::shouldUndo() {
 | 
|---|
| 146 |   return false;
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | const string WorldRepeatBoxAction::getName() {
 | 
|---|
| 150 |   return NAME;
 | 
|---|
| 151 | }
 | 
|---|