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