[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>
|
---|
| 23 |
|
---|
| 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 | #include "UIElements/UIFactory.hpp"
|
---|
| 27 | #include "UIElements/Dialog.hpp"
|
---|
[861874] | 28 | #include "Actions/ValueStorage.hpp"
|
---|
[e30ce8] | 29 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 30 | #include "Descriptors/MoleculePtrDescriptor.hpp"
|
---|
[97ebf8] | 31 |
|
---|
| 32 | const char WorldRepeatBoxAction::NAME[] = "repeat-box";
|
---|
| 33 |
|
---|
| 34 | WorldRepeatBoxAction::WorldRepeatBoxAction() :
|
---|
| 35 | Action(NAME)
|
---|
| 36 | {}
|
---|
| 37 |
|
---|
| 38 | WorldRepeatBoxAction::~WorldRepeatBoxAction()
|
---|
| 39 | {}
|
---|
| 40 |
|
---|
[a8f6ae] | 41 | void WorldRepeatBox(Vector &Repeater) {
|
---|
| 42 | ValueStorage::getInstance().setCurrentValue(WorldRepeatBoxAction::NAME, Repeater);
|
---|
| 43 | ActionRegistry::getInstance().getActionByName(WorldRepeatBoxAction::NAME)->call(Action::NonInteractive);
|
---|
| 44 | };
|
---|
| 45 |
|
---|
[047878] | 46 | Dialog * WorldRepeatBoxAction::fillDialog(Dialog *dialog) {
|
---|
| 47 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
[bfe2c2] | 48 |
|
---|
| 49 | dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
|
---|
| 50 |
|
---|
| 51 | return dialog;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | Action::state_ptr WorldRepeatBoxAction::performCall() {
|
---|
[97ebf8] | 55 | Vector Repeater;
|
---|
| 56 | int count;
|
---|
| 57 | const element ** Elements;
|
---|
| 58 | molecule *mol = NULL;
|
---|
| 59 | int j = 0;
|
---|
[1024cb] | 60 | atom *Walker = NULL;
|
---|
[e30ce8] | 61 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[97ebf8] | 62 |
|
---|
[bfe2c2] | 63 | ValueStorage::getInstance().queryCurrentValue(NAME, Repeater);
|
---|
| 64 |
|
---|
[e30ce8] | 65 | vector<molecule *> AllMolecules;
|
---|
| 66 | if (mol != NULL) {
|
---|
| 67 | DoLog(0) && (Log() << Verbose(0) << "Using molecule " << mol->name << "." << endl);
|
---|
| 68 | AllMolecules = World::getInstance().getAllMolecules(MoleculeByPtr(mol));
|
---|
| 69 | } else {
|
---|
| 70 | DoLog(0) && (Log() << Verbose(0) << "Using all molecules." << endl);
|
---|
| 71 | AllMolecules = World::getInstance().getAllMolecules();
|
---|
| 72 | }
|
---|
[97ebf8] | 73 |
|
---|
[bfe2c2] | 74 | (cout << "Repeating box " << Repeater << " times for (x,y,z) axis." << endl);
|
---|
| 75 | Matrix M = World::getInstance().getDomain().getM();
|
---|
| 76 | Matrix newM = M;
|
---|
| 77 | Vector x,y;
|
---|
| 78 | int n[NDIM];
|
---|
| 79 | Matrix repMat;
|
---|
| 80 | for (int axis = 0; axis < NDIM; axis++) {
|
---|
| 81 | Repeater[axis] = floor(Repeater[axis]);
|
---|
| 82 | if (Repeater[axis] < 1) {
|
---|
| 83 | DoeLog(1) && (eLog()<< Verbose(1) << "Repetition factor must be greater than 1!" << endl);
|
---|
| 84 | Repeater[axis] = 1;
|
---|
[e30ce8] | 85 | }
|
---|
[bfe2c2] | 86 | repMat.at(axis,axis) = Repeater[axis];
|
---|
| 87 | }
|
---|
| 88 | newM *= repMat;
|
---|
| 89 | World::getInstance().setDomain(newM);
|
---|
| 90 |
|
---|
| 91 | molecule *newmol = NULL;
|
---|
| 92 | Vector ** vectors = NULL;
|
---|
| 93 | for (n[0] = 0; n[0] < Repeater[0]; n[0]++) {
|
---|
| 94 | y[0] = n[0];
|
---|
| 95 | for (n[1] = 0; n[1] < Repeater[1]; n[1]++) {
|
---|
| 96 | y[1] = n[1];
|
---|
| 97 | for (n[2] = 0; n[2] < Repeater[2]; n[2]++) {
|
---|
| 98 | y[2] = n[2];
|
---|
| 99 | if ((n[0] == 0) && (n[1] == 0) && (n[2] == 0))
|
---|
| 100 | continue;
|
---|
| 101 | for (vector<molecule *>::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
|
---|
| 102 | mol = *MolRunner;
|
---|
| 103 | DoLog(1) && (Log() << Verbose(1) << "Current mol is " << mol->name << "." << endl);
|
---|
| 104 | count = mol->getAtomCount(); // is changed becausing of adding, thus has to be stored away beforehand
|
---|
| 105 | if (count != 0) { // if there is more than none
|
---|
| 106 | Elements = new const element *[count];
|
---|
| 107 | vectors = new Vector *[count];
|
---|
| 108 | j = 0;
|
---|
| 109 | for(molecule::iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
|
---|
| 110 | Elements[j] = (*AtomRunner)->type;
|
---|
| 111 | vectors[j] = &(*AtomRunner)->x;
|
---|
| 112 | j++;
|
---|
[e30ce8] | 113 | }
|
---|
[bfe2c2] | 114 | if (count != j)
|
---|
| 115 | DoeLog(1) && (eLog()<< Verbose(1) << "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl);
|
---|
| 116 | x = y;
|
---|
| 117 | x *= M;
|
---|
| 118 | newmol = World::getInstance().createMolecule();
|
---|
| 119 | molecules->insert(newmol);
|
---|
| 120 | for (int k=count;k--;) { // go through every atom of the original cell
|
---|
| 121 | Walker = World::getInstance().createAtom(); // create a new body
|
---|
| 122 | Walker->x = (*vectors[k]) + x;
|
---|
| 123 | Walker->type = Elements[k]; // insert original element
|
---|
| 124 | cout << "new atom is " << *Walker << endl;
|
---|
| 125 | newmol->AddAtom(Walker); // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
|
---|
| 126 | }
|
---|
| 127 | // free memory
|
---|
| 128 | delete[](Elements);
|
---|
| 129 | delete[](vectors);
|
---|
| 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 | }
|
---|