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