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