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