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