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