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