[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 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 21 |
|
---|
[1fd675] | 22 | #include "Descriptors/MoleculePtrDescriptor.hpp"
|
---|
[97ebf8] | 23 | #include "atom.hpp"
|
---|
[ad011c] | 24 | #include "CodePatterns/Log.hpp"
|
---|
[97ebf8] | 25 | #include "molecule.hpp"
|
---|
[42127c] | 26 | #include "MoleculeListClass.hpp"
|
---|
[57f243] | 27 | #include "LinearAlgebra/Vector.hpp"
|
---|
[cca9ef] | 28 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[ad011c] | 29 | #include "CodePatterns/Verbose.hpp"
|
---|
[97ebf8] | 30 | #include "World.hpp"
|
---|
[84c494] | 31 | #include "Box.hpp"
|
---|
[97ebf8] | 32 |
|
---|
| 33 | #include <iostream>
|
---|
[45f835] | 34 | #include <set>
|
---|
[97ebf8] | 35 | #include <string>
|
---|
[d74077] | 36 | #include <vector>
|
---|
[97ebf8] | 37 |
|
---|
[1fd675] | 38 | #include "Actions/WorldAction/RepeatBoxAction.hpp"
|
---|
[bfe2c2] | 39 |
|
---|
[ce7fdc] | 40 | using namespace MoleCuilder;
|
---|
| 41 |
|
---|
[1fd675] | 42 | // and construct the stuff
|
---|
| 43 | #include "RepeatBoxAction.def"
|
---|
| 44 | #include "Action_impl_pre.hpp"
|
---|
| 45 | /** =========== define the function ====================== */
|
---|
[45f835] | 46 |
|
---|
| 47 | void repeatMoleculesinDomain(Vector Repeater, const std::vector<molecule *> &AllMolecules)
|
---|
| 48 | {
|
---|
[97ebf8] | 49 | int count;
|
---|
| 50 | const element ** Elements;
|
---|
| 51 | int j = 0;
|
---|
[1024cb] | 52 | atom *Walker = NULL;
|
---|
[e30ce8] | 53 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[97ebf8] | 54 |
|
---|
[45f835] | 55 | LOG(0, "STATUS: Repeating box " << Repeater << " times for (x,y,z) axis.");
|
---|
[97ebf8] | 56 |
|
---|
[45f835] | 57 | // set new domain
|
---|
[cca9ef] | 58 | RealSpaceMatrix M = World::getInstance().getDomain().getM();
|
---|
| 59 | RealSpaceMatrix newM = M;
|
---|
[bfe2c2] | 60 | Vector x,y;
|
---|
| 61 | int n[NDIM];
|
---|
[cca9ef] | 62 | RealSpaceMatrix repMat;
|
---|
[bfe2c2] | 63 | for (int axis = 0; axis < NDIM; axis++) {
|
---|
[45f835] | 64 | Repeater[axis] = floor(Repeater[axis]);
|
---|
| 65 | if (Repeater[axis] < 1) {
|
---|
| 66 | ELOG(1, "Repetition factor must be greater than 1!");
|
---|
| 67 | Repeater[axis] = 1;
|
---|
[e30ce8] | 68 | }
|
---|
[45f835] | 69 | repMat.at(axis,axis) = Repeater[axis];
|
---|
[bfe2c2] | 70 | }
|
---|
| 71 | newM *= repMat;
|
---|
| 72 | World::getInstance().setDomain(newM);
|
---|
| 73 |
|
---|
[45f835] | 74 | // add molecules in each repeated domain part
|
---|
[bfe2c2] | 75 | molecule *newmol = NULL;
|
---|
[d74077] | 76 | std::vector<Vector> vectors;
|
---|
[45f835] | 77 | for (n[0] = 0; n[0] < Repeater[0]; n[0]++) {
|
---|
[bfe2c2] | 78 | y[0] = n[0];
|
---|
[45f835] | 79 | for (n[1] = 0; n[1] < Repeater[1]; n[1]++) {
|
---|
[bfe2c2] | 80 | y[1] = n[1];
|
---|
[45f835] | 81 | for (n[2] = 0; n[2] < Repeater[2]; n[2]++) {
|
---|
[bfe2c2] | 82 | y[2] = n[2];
|
---|
| 83 | if ((n[0] == 0) && (n[1] == 0) && (n[2] == 0))
|
---|
| 84 | continue;
|
---|
[45f835] | 85 | for (vector<molecule *>::const_iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
|
---|
| 86 | const molecule *mol = *MolRunner;
|
---|
| 87 | LOG(2, "INFO: Current mol is " << mol->name << ".");
|
---|
[bfe2c2] | 88 | count = mol->getAtomCount(); // is changed becausing of adding, thus has to be stored away beforehand
|
---|
| 89 | if (count != 0) { // if there is more than none
|
---|
| 90 | Elements = new const element *[count];
|
---|
[d74077] | 91 | vectors.resize(count);
|
---|
[bfe2c2] | 92 | j = 0;
|
---|
[45f835] | 93 | for(molecule::const_iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
|
---|
[d74077] | 94 | Elements[j] = (*AtomRunner)->getType();
|
---|
| 95 | vectors[j] = (*AtomRunner)->getPosition();
|
---|
[bfe2c2] | 96 | j++;
|
---|
[e30ce8] | 97 | }
|
---|
[bfe2c2] | 98 | if (count != j)
|
---|
[45f835] | 99 | ELOG(1, "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!");
|
---|
[bfe2c2] | 100 | x = y;
|
---|
| 101 | x *= M;
|
---|
| 102 | newmol = World::getInstance().createMolecule();
|
---|
[45f835] | 103 | // TODO: Remove this when World don't has deprecated MoleculeListClass anymore
|
---|
[bfe2c2] | 104 | molecules->insert(newmol);
|
---|
| 105 | for (int k=count;k--;) { // go through every atom of the original cell
|
---|
| 106 | Walker = World::getInstance().createAtom(); // create a new body
|
---|
[d74077] | 107 | Walker->setPosition((vectors[k]) + x);
|
---|
| 108 | Walker->setType(Elements[k]); // insert original element
|
---|
[bfe2c2] | 109 | cout << "new atom is " << *Walker << endl;
|
---|
| 110 | newmol->AddAtom(Walker); // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
|
---|
| 111 | }
|
---|
| 112 | // free memory
|
---|
| 113 | delete[](Elements);
|
---|
| 114 | } else {
|
---|
[45f835] | 115 | LOG(1, "\t ... is empty.");
|
---|
[97ebf8] | 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
[45f835] | 121 | }
|
---|
| 122 |
|
---|
| 123 | Action::state_ptr WorldRepeatBoxAction::performCall() {
|
---|
| 124 | molecule *mol = NULL;
|
---|
| 125 |
|
---|
| 126 | // obtain information
|
---|
| 127 | getParametersfromValueStorage();
|
---|
| 128 |
|
---|
| 129 | std::vector<molecule *> AllMolecules;
|
---|
| 130 | if (mol != NULL) {
|
---|
| 131 | DoLog(0) && (Log() << Verbose(0) << "Using molecule " << mol->name << "." << endl);
|
---|
| 132 | AllMolecules = World::getInstance().getAllMolecules(MoleculeByPtr(mol));
|
---|
| 133 | } else {
|
---|
| 134 | DoLog(0) && (Log() << Verbose(0) << "Using all molecules." << endl);
|
---|
| 135 | AllMolecules = World::getInstance().getAllMolecules();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | // prepare undo state
|
---|
| 139 | RealSpaceMatrix olddomain = World::getInstance().getDomain().getM();
|
---|
| 140 | std::set<molecule *> oldmolecules;
|
---|
| 141 | for(std::vector<molecule *>::const_iterator iter = AllMolecules.begin();
|
---|
| 142 | iter != AllMolecules.end();
|
---|
| 143 | ++iter)
|
---|
| 144 | oldmolecules.insert(*iter);
|
---|
| 145 | WorldRepeatBoxState *undostate = new WorldRepeatBoxState(olddomain, oldmolecules, params);
|
---|
| 146 |
|
---|
| 147 | repeatMoleculesinDomain(params.Repeater, AllMolecules);
|
---|
[3bd460a] | 148 |
|
---|
| 149 | // give final box size
|
---|
| 150 | LOG(0, "Box domain is now " << World::getInstance().getDomain().getM());
|
---|
| 151 |
|
---|
[45f835] | 152 | return Action::state_ptr(undostate);
|
---|
[97ebf8] | 153 | }
|
---|
| 154 |
|
---|
| 155 | Action::state_ptr WorldRepeatBoxAction::performUndo(Action::state_ptr _state) {
|
---|
[45f835] | 156 | WorldRepeatBoxState *state = assert_cast<WorldRepeatBoxState*>(_state.get());
|
---|
| 157 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
| 158 |
|
---|
| 159 | // set old domain
|
---|
| 160 | World::getInstance().setDomain(state->olddomain);
|
---|
| 161 |
|
---|
| 162 | // remove all added molecules (and their atoms)
|
---|
| 163 | std::vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 164 | for (std::vector<molecule *>::iterator iter = allmolecules.begin();
|
---|
| 165 | iter != allmolecules.end();
|
---|
| 166 | ++iter) {
|
---|
| 167 | if (state->oldmolecules.find(*iter) == state->oldmolecules.end()) {
|
---|
| 168 | (*iter)->removeAtomsinMolecule();
|
---|
| 169 | // TODO: Remove this when World don't has deprecated MoleculeListClass anymore
|
---|
| 170 | molecules->erase(*iter);
|
---|
| 171 | World::getInstance().destroyMolecule(*iter);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | // give final box size
|
---|
| 176 | LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM());
|
---|
[97ebf8] | 177 |
|
---|
[45f835] | 178 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 179 | }
|
---|
| 180 |
|
---|
| 181 | Action::state_ptr WorldRepeatBoxAction::performRedo(Action::state_ptr _state){
|
---|
[45f835] | 182 | WorldRepeatBoxState *state = assert_cast<WorldRepeatBoxState*>(_state.get());
|
---|
| 183 |
|
---|
| 184 | std::vector<molecule *> originalmolecules;
|
---|
| 185 | for(std::set<molecule *>::const_iterator iter = state->oldmolecules.begin();
|
---|
| 186 | iter != state->oldmolecules.end();
|
---|
| 187 | ++iter)
|
---|
| 188 | originalmolecules.push_back(*iter);
|
---|
| 189 | repeatMoleculesinDomain(state->params.Repeater, originalmolecules);
|
---|
| 190 |
|
---|
| 191 | // give final box size
|
---|
| 192 | LOG(0, "Box domain is again " << World::getInstance().getDomain().getM());
|
---|
| 193 |
|
---|
| 194 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 195 | }
|
---|
| 196 |
|
---|
| 197 | bool WorldRepeatBoxAction::canUndo() {
|
---|
[45f835] | 198 | return true;
|
---|
[97ebf8] | 199 | }
|
---|
| 200 |
|
---|
| 201 | bool WorldRepeatBoxAction::shouldUndo() {
|
---|
[45f835] | 202 | return true;
|
---|
[97ebf8] | 203 | }
|
---|
[1fd675] | 204 | /** =========== end of function ====================== */
|
---|