| 1 | /*
|
|---|
| 2 | * RepeatBoxAction.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: May 12, 2010
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | // include config.h
|
|---|
| 9 | #ifdef HAVE_CONFIG_H
|
|---|
| 10 | #include <config.h>
|
|---|
| 11 | #endif
|
|---|
| 12 |
|
|---|
| 13 | #include "Helpers/MemDebug.hpp"
|
|---|
| 14 |
|
|---|
| 15 | #include "Actions/WorldAction/RepeatBoxAction.hpp"
|
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 17 | #include "atom.hpp"
|
|---|
| 18 | #include "Helpers/Log.hpp"
|
|---|
| 19 | #include "molecule.hpp"
|
|---|
| 20 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 21 | #include "LinearAlgebra/Matrix.hpp"
|
|---|
| 22 | #include "Helpers/Verbose.hpp"
|
|---|
| 23 | #include "World.hpp"
|
|---|
| 24 | #include "Box.hpp"
|
|---|
| 25 |
|
|---|
| 26 | #include <iostream>
|
|---|
| 27 | #include <string>
|
|---|
| 28 | #include <vector>
|
|---|
| 29 |
|
|---|
| 30 | using namespace std;
|
|---|
| 31 |
|
|---|
| 32 | #include "UIElements/UIFactory.hpp"
|
|---|
| 33 | #include "UIElements/Dialog.hpp"
|
|---|
| 34 | #include "Actions/ValueStorage.hpp"
|
|---|
| 35 | #include "Descriptors/MoleculeDescriptor.hpp"
|
|---|
| 36 | #include "Descriptors/MoleculePtrDescriptor.hpp"
|
|---|
| 37 |
|
|---|
| 38 | const char WorldRepeatBoxAction::NAME[] = "repeat-box";
|
|---|
| 39 |
|
|---|
| 40 | WorldRepeatBoxAction::WorldRepeatBoxAction() :
|
|---|
| 41 | Action(NAME)
|
|---|
| 42 | {}
|
|---|
| 43 |
|
|---|
| 44 | WorldRepeatBoxAction::~WorldRepeatBoxAction()
|
|---|
| 45 | {}
|
|---|
| 46 |
|
|---|
| 47 | void WorldRepeatBox(Vector &Repeater) {
|
|---|
| 48 | ValueStorage::getInstance().setCurrentValue(WorldRepeatBoxAction::NAME, Repeater);
|
|---|
| 49 | ActionRegistry::getInstance().getActionByName(WorldRepeatBoxAction::NAME)->call(Action::NonInteractive);
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | Dialog * WorldRepeatBoxAction::fillDialog(Dialog *dialog) {
|
|---|
| 53 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| 54 |
|
|---|
| 55 | dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
|
|---|
| 56 |
|
|---|
| 57 | return dialog;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | Action::state_ptr WorldRepeatBoxAction::performCall() {
|
|---|
| 61 | Vector Repeater;
|
|---|
| 62 | int count;
|
|---|
| 63 | const element ** Elements;
|
|---|
| 64 | molecule *mol = NULL;
|
|---|
| 65 | int j = 0;
|
|---|
| 66 | atom *Walker = NULL;
|
|---|
| 67 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
|---|
| 68 |
|
|---|
| 69 | ValueStorage::getInstance().queryCurrentValue(NAME, Repeater);
|
|---|
| 70 |
|
|---|
| 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 | }
|
|---|
| 79 |
|
|---|
| 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;
|
|---|
| 91 | }
|
|---|
| 92 | repMat.at(axis,axis) = Repeater[axis];
|
|---|
| 93 | }
|
|---|
| 94 | newM *= repMat;
|
|---|
| 95 | World::getInstance().setDomain(newM);
|
|---|
| 96 |
|
|---|
| 97 | molecule *newmol = NULL;
|
|---|
| 98 | std::vector<Vector> vectors;
|
|---|
| 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];
|
|---|
| 113 | vectors.resize(count);
|
|---|
| 114 | j = 0;
|
|---|
| 115 | for(molecule::iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
|
|---|
| 116 | Elements[j] = (*AtomRunner)->getType();
|
|---|
| 117 | vectors[j] = (*AtomRunner)->getPosition();
|
|---|
| 118 | j++;
|
|---|
| 119 | }
|
|---|
| 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
|
|---|
| 128 | Walker->setPosition((vectors[k]) + x);
|
|---|
| 129 | Walker->setType(Elements[k]); // insert original element
|
|---|
| 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);
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | return Action::success;
|
|---|
| 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 | }
|
|---|