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