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